Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
TransactionFeed.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "data/AmendmentCenterInterface.hpp"
23#include "data/BackendInterface.hpp"
24#include "data/Types.hpp"
25#include "feed/Types.hpp"
26#include "feed/impl/TrackableSignal.hpp"
27#include "feed/impl/TrackableSignalMap.hpp"
28#include "feed/impl/Util.hpp"
29#include "util/async/AnyExecutionContext.hpp"
30#include "util/async/AnyStrand.hpp"
31#include "util/log/Logger.hpp"
32#include "util/prometheus/Gauge.hpp"
33
34#include <boost/asio/io_context.hpp>
35#include <boost/asio/strand.hpp>
36#include <fmt/format.h>
37#include <xrpl/protocol/AccountID.h>
38#include <xrpl/protocol/Book.h>
39#include <xrpl/protocol/LedgerHeader.h>
40
41#include <array>
42#include <cstdint>
43#include <functional>
44#include <memory>
45#include <string>
46#include <unordered_set>
47
48namespace feed::impl {
49
51 // Hold two versions of transaction messages
52 using AllVersionTransactionsType = std::array<std::shared_ptr<std::string>, 2>;
53
54 struct TransactionSlot {
55 std::reference_wrapper<TransactionFeed> feed;
56 std::weak_ptr<Subscriber> subscriptionContextWeakPtr;
57
58 TransactionSlot(TransactionFeed& feed, SubscriberSharedPtr const& connection)
59 : feed(feed), subscriptionContextWeakPtr(connection)
60 {
61 }
62
63 void
64 operator()(AllVersionTransactionsType const& allVersionMsgs) const;
65 };
66
67 util::Logger logger_{"Subscriptions"};
68
70 std::reference_wrapper<util::prometheus::GaugeInt> subAllCount_;
71 std::reference_wrapper<util::prometheus::GaugeInt> subAccountCount_;
72 std::reference_wrapper<util::prometheus::GaugeInt> subBookCount_;
73
75 accountSignal_;
78
79 // Signals for proposed tx subscribers
81 accountProposedSignal_;
83
84 std::unordered_set<SubscriberPtr> notified_; // Used by slots to prevent double notifications
85 // if tx contains multiple subscribed accounts
86
87public:
93 : strand_(executionCtx.makeStrand())
94 , subAllCount_(getSubscriptionsGaugeInt("tx"))
95 , subAccountCount_(getSubscriptionsGaugeInt("account"))
96 , subBookCount_(getSubscriptionsGaugeInt("book"))
97 {
98 }
99
104
109 void
110 sub(SubscriberSharedPtr const& subscriber);
111
118 void
119 sub(ripple::AccountID const& account, SubscriberSharedPtr const& subscriber);
120
127 void
128 sub(ripple::Book const& book, SubscriberSharedPtr const& subscriber);
129
134 void
135 subProposed(SubscriberSharedPtr const& subscriber);
136
143 void
144 subProposed(ripple::AccountID const& account, SubscriberSharedPtr const& subscriber);
145
150 void
151 unsub(SubscriberSharedPtr const& subscriber);
152
158 void
159 unsub(ripple::AccountID const& account, SubscriberSharedPtr const& subscriber);
160
165 void
166 unsubProposed(SubscriberSharedPtr const& subscriber);
167
173 void
174 unsubProposed(ripple::AccountID const& account, SubscriberSharedPtr const& subscriber);
175
181 void
182 unsub(ripple::Book const& book, SubscriberSharedPtr const& subscriber);
183
191 void
192 pub(data::TransactionAndMetadata const& txMeta,
193 ripple::LedgerHeader const& lgrInfo,
194 std::shared_ptr<data::BackendInterface const> const& backend,
195 std::shared_ptr<data::AmendmentCenterInterface const> const& amendmentCenter,
196 uint32_t networkID);
197
201 std::uint64_t
202 transactionSubCount() const;
203
207 std::uint64_t
208 accountSubCount() const;
209
213 std::uint64_t
214 bookSubCount() const;
215
216private:
217 void
218 unsubInternal(SubscriberPtr subscriber);
219
220 void
221 unsubInternal(ripple::AccountID const& account, SubscriberPtr subscriber);
222
223 void
224 unsubProposedInternal(SubscriberPtr subscriber);
225
226 void
227 unsubProposedInternal(ripple::AccountID const& account, SubscriberPtr subscriber);
228
229 void
230 unsubInternal(ripple::Book const& book, SubscriberPtr subscriber);
231};
232} // namespace feed::impl
Class to manage a map of key and its associative signal.
Definition TrackableSignalMap.hpp:49
A thread-safe class to manage a signal and its tracking connections.
Definition TrackableSignal.hpp:45
void unsub(SubscriberSharedPtr const &subscriber)
Unsubscribe to the transaction feed.
Definition TransactionFeed.cpp:145
void sub(SubscriberSharedPtr const &subscriber)
Subscribe to the transaction feed.
Definition TransactionFeed.cpp:77
void unsubProposed(SubscriberSharedPtr const &subscriber)
Unsubscribe to the transaction feed for proposed transaction stream.
Definition TransactionFeed.cpp:157
std::uint64_t bookSubCount() const
Get the number of books subscribers.
Definition TransactionFeed.cpp:190
std::uint64_t transactionSubCount() const
Get the number of subscribers of the transaction feed.
Definition TransactionFeed.cpp:178
TransactionFeed(TransactionFeed &&)=delete
Move constructor is deleted because TransactionSlot takes TransactionFeed by reference.
std::uint64_t accountSubCount() const
Get the number of accounts subscribers.
Definition TransactionFeed.cpp:184
TransactionFeed(util::async::AnyExecutionContext &executionCtx)
Construct a new Transaction Feed object.
Definition TransactionFeed.hpp:92
void pub(data::TransactionAndMetadata const &txMeta, ripple::LedgerHeader const &lgrInfo, std::shared_ptr< data::BackendInterface const > const &backend, std::shared_ptr< data::AmendmentCenterInterface const > const &amendmentCenter, uint32_t networkID)
Publishes the transaction feed.
Definition TransactionFeed.cpp:196
void subProposed(SubscriberSharedPtr const &subscriber)
Subscribe to the transaction feed for proposed transaction stream.
Definition TransactionFeed.cpp:103
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:96
A type-erased execution context.
Definition AnyExecutionContext.hpp:41
A type-erased execution context.
Definition AnyStrand.hpp:40
This namespace implements everything related to subscriptions.
Definition BookChangesFeed.hpp:33
Represents a transaction and its metadata bundled together.
Definition Types.hpp:68