Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
SubscriptionManager.hpp
1#pragma once
2
3#include "data/AmendmentCenterInterface.hpp"
4#include "data/BackendInterface.hpp"
5#include "data/Types.hpp"
6#include "feed/SubscriptionManagerInterface.hpp"
7#include "feed/Types.hpp"
8#include "feed/impl/BookChangesFeed.hpp"
9#include "feed/impl/ForwardFeed.hpp"
10#include "feed/impl/LedgerFeed.hpp"
11#include "feed/impl/ProposedTransactionFeed.hpp"
12#include "feed/impl/TransactionFeed.hpp"
13#include "util/async/AnyExecutionContext.hpp"
14#include "util/async/context/BasicExecutionContext.hpp"
15#include "util/config/ConfigDefinition.hpp"
16#include "util/log/Logger.hpp"
17
18#include <boost/asio/executor_work_guard.hpp>
19#include <boost/asio/io_context.hpp>
20#include <boost/asio/spawn.hpp>
21#include <boost/json/object.hpp>
22#include <xrpl/protocol/AccountID.h>
23#include <xrpl/protocol/Book.h>
24#include <xrpl/protocol/Fees.h>
25#include <xrpl/protocol/LedgerHeader.h>
26#include <xrpl/protocol/UintTypes.h>
27
28#include <cstdint>
29#include <memory>
30#include <string>
31#include <utility>
32#include <vector>
33
39namespace feed {
40
46 std::shared_ptr<data::BackendInterface const> backend_;
47 std::shared_ptr<data::AmendmentCenterInterface const> amendmentCenter_;
49 impl::ForwardFeed manifestFeed_;
50 impl::ForwardFeed validationsFeed_;
51 impl::LedgerFeed ledgerFeed_;
52 impl::BookChangesFeed bookChangesFeed_;
53 impl::TransactionFeed transactionFeed_;
54 impl::ProposedTransactionFeed proposedTransactionFeed_;
55 uint32_t networkID_{0};
56
57public:
66 static std::shared_ptr<SubscriptionManager>
69 std::shared_ptr<data::BackendInterface const> const& backend,
70 std::shared_ptr<data::AmendmentCenterInterface const> const& amendmentCenter
71 )
72 {
73 auto const workersNum = config.get<uint64_t>("subscription_workers");
74
75 util::Logger const logger{"Subscriptions"};
76 LOG(logger.info()) << "Starting subscription manager with " << workersNum << " workers";
77
78 return std::make_shared<feed::SubscriptionManager>(
79 util::async::PoolExecutionContext(workersNum), backend, amendmentCenter
80 );
81 }
82
92 std::shared_ptr<data::BackendInterface const> const& backend,
93 std::shared_ptr<data::AmendmentCenterInterface const> const& amendmentCenter
94 )
95 : backend_(backend)
96 , amendmentCenter_(amendmentCenter)
97 , ctx_(std::move(executor))
98 , manifestFeed_(ctx_, "manifest")
99 , validationsFeed_(ctx_, "validations")
100 , ledgerFeed_(ctx_)
101 , bookChangesFeed_(ctx_)
102 , transactionFeed_(ctx_)
103 , proposedTransactionFeed_(ctx_)
104 {
105 }
106
112 {
113 stop();
114 }
115
119 void
120 stop() override
121 {
122 ctx_.stop();
123 ctx_.join();
124 }
125
130 void
131 subBookChanges(SubscriberSharedPtr const& subscriber) final;
132
137 void
138 unsubBookChanges(SubscriberSharedPtr const& subscriber) final;
139
145 void
147 xrpl::LedgerHeader const& lgrInfo,
148 std::vector<data::TransactionAndMetadata> const& transactions
149 ) final;
150
155 void
156 subProposedTransactions(SubscriberSharedPtr const& subscriber) final;
157
162 void
163 unsubProposedTransactions(SubscriberSharedPtr const& subscriber) final;
164
171 void
172 subProposedAccount(xrpl::AccountID const& account, SubscriberSharedPtr const& subscriber) final;
173
179 void
181 xrpl::AccountID const& account,
182 SubscriberSharedPtr const& subscriber
183 ) final;
184
189 void
190 forwardProposedTransaction(boost::json::object const& receivedTxJson) final;
191
198 boost::json::object
199 subLedger(boost::asio::yield_context yield, SubscriberSharedPtr const& subscriber) final;
200
205 void
206 unsubLedger(SubscriberSharedPtr const& subscriber) final;
207
215 void
216 pubLedger(
217 xrpl::LedgerHeader const& lgrInfo,
218 xrpl::Fees const& fees,
219 std::string const& ledgerRange,
220 std::uint32_t txnCount
221 ) final;
222
227 void
228 subManifest(SubscriberSharedPtr const& subscriber) final;
229
234 void
235 unsubManifest(SubscriberSharedPtr const& subscriber) final;
236
241 void
242 forwardManifest(boost::json::object const& manifestJson) final;
243
248 void
249 subValidation(SubscriberSharedPtr const& subscriber) final;
250
255 void
256 unsubValidation(SubscriberSharedPtr const& subscriber) final;
257
262 void
263 forwardValidation(boost::json::object const& validationJson) final;
264
269 void
270 subTransactions(SubscriberSharedPtr const& subscriber) final;
271
276 void
277 unsubTransactions(SubscriberSharedPtr const& subscriber) final;
278
285 void
286 subAccount(xrpl::AccountID const& account, SubscriberSharedPtr const& subscriber) final;
287
293 void
294 unsubAccount(xrpl::AccountID const& account, SubscriberSharedPtr const& subscriber) final;
295
302 void
303 subBook(xrpl::Book const& book, SubscriberSharedPtr const& subscriber) final;
304
310 void
311 unsubBook(xrpl::Book const& book, SubscriberSharedPtr const& subscriber) final;
312
319 void
320 subMPTIssuance(xrpl::MPTID const& mptIssuanceID, SubscriberSharedPtr const& subscriber) final;
321
327 void
328 unsubMPTIssuance(xrpl::MPTID const& mptIssuanceID, SubscriberSharedPtr const& subscriber) final;
329
335 void
337 data::TransactionAndMetadata const& txMeta,
338 xrpl::LedgerHeader const& lgrInfo
339 ) final;
340
346 boost::json::object
347 report() const final;
348
353 void
354 setNetworkID(uint32_t networkID) final;
355
361 uint32_t
362 getNetworkID() const final;
363};
364
365} // namespace feed
Interface of subscription manager. A subscription manager is responsible for managing the subscriptio...
Definition SubscriptionManagerInterface.hpp:26
void forwardValidation(boost::json::object const &validationJson) final
Forward the validation feed.
Definition SubscriptionManager.cpp:143
void subBookChanges(SubscriberSharedPtr const &subscriber) final
Subscribe to the book changes feed.
Definition SubscriptionManager.cpp:20
void unsubProposedTransactions(SubscriberSharedPtr const &subscriber) final
Unsubscribe to the proposed transactions feed.
Definition SubscriptionManager.cpp:51
void subManifest(SubscriberSharedPtr const &subscriber) final
Subscribe to the manifest feed.
Definition SubscriptionManager.cpp:113
void setNetworkID(uint32_t networkID) final
Set the networkID.
Definition SubscriptionManager.cpp:235
boost::json::object report() const final
Get the number of subscribers.
Definition SubscriptionManager.cpp:218
void forwardManifest(boost::json::object const &manifestJson) final
Forward the manifest feed.
Definition SubscriptionManager.cpp:125
uint32_t getNetworkID() const final
Get the networkID.
Definition SubscriptionManager.cpp:241
void subProposedAccount(xrpl::AccountID const &account, SubscriberSharedPtr const &subscriber) final
Subscribe to the proposed transactions feed, only receive the feed when particular account is affecte...
Definition SubscriptionManager.cpp:58
void unsubAccount(xrpl::AccountID const &account, SubscriberSharedPtr const &subscriber) final
Unsubscribe to the transactions feed for particular account.
Definition SubscriptionManager.cpp:170
void unsubBookChanges(SubscriberSharedPtr const &subscriber) final
Unsubscribe to the book changes feed.
Definition SubscriptionManager.cpp:26
void unsubMPTIssuance(xrpl::MPTID const &mptIssuanceID, SubscriberSharedPtr const &subscriber) final
Unsubscribe to the transactions feed for particular MPT issuance.
Definition SubscriptionManager.cpp:200
void subBook(xrpl::Book const &book, SubscriberSharedPtr const &subscriber) final
Subscribe to the transactions feed, only receive feed when particular order book is affected.
Definition SubscriptionManager.cpp:179
void unsubTransactions(SubscriberSharedPtr const &subscriber) final
Unsubscribe to the transactions feed.
Definition SubscriptionManager.cpp:155
static std::shared_ptr< SubscriptionManager > makeSubscriptionManager(util::config::ClioConfigDefinition const &config, std::shared_ptr< data::BackendInterface const > const &backend, std::shared_ptr< data::AmendmentCenterInterface const > const &amendmentCenter)
Factory function to create a new SubscriptionManager with a PoolExecutionContext.
Definition SubscriptionManager.hpp:67
void subAccount(xrpl::AccountID const &account, SubscriberSharedPtr const &subscriber) final
Subscribe to the transactions feed, only receive the feed when particular account is affected.
Definition SubscriptionManager.cpp:161
SubscriptionManager(util::async::AnyExecutionContext &&executor, std::shared_ptr< data::BackendInterface const > const &backend, std::shared_ptr< data::AmendmentCenterInterface const > const &amendmentCenter)
Construct a new Subscription Manager object.
Definition SubscriptionManager.hpp:90
void unsubBook(xrpl::Book const &book, SubscriberSharedPtr const &subscriber) final
Unsubscribe to the transactions feed for particular order book.
Definition SubscriptionManager.cpp:185
boost::json::object subLedger(boost::asio::yield_context yield, SubscriberSharedPtr const &subscriber) final
Subscribe to the ledger feed.
Definition SubscriptionManager.cpp:87
void subMPTIssuance(xrpl::MPTID const &mptIssuanceID, SubscriberSharedPtr const &subscriber) final
Subscribe to the transactions feed, only receive the feed when particular MPT issuance is affected.
Definition SubscriptionManager.cpp:191
void subProposedTransactions(SubscriberSharedPtr const &subscriber) final
Subscribe to the proposed transactions feed.
Definition SubscriptionManager.cpp:41
void subValidation(SubscriberSharedPtr const &subscriber) final
Subscribe to the validation feed.
Definition SubscriptionManager.cpp:131
void unsubManifest(SubscriberSharedPtr const &subscriber) final
Unsubscribe to the manifest feed.
Definition SubscriptionManager.cpp:119
void forwardProposedTransaction(boost::json::object const &receivedTxJson) final
Forward the proposed transactions feed.
Definition SubscriptionManager.cpp:81
void unsubValidation(SubscriberSharedPtr const &subscriber) final
Unsubscribe to the validation feed.
Definition SubscriptionManager.cpp:137
void stop() override
Stop the SubscriptionManager and wait for all jobs to finish.
Definition SubscriptionManager.hpp:120
void pubTransaction(data::TransactionAndMetadata const &txMeta, xrpl::LedgerHeader const &lgrInfo) final
Forward the transactions feed.
Definition SubscriptionManager.cpp:209
void unsubLedger(SubscriberSharedPtr const &subscriber) final
Unsubscribe to the ledger feed.
Definition SubscriptionManager.cpp:96
void subTransactions(SubscriberSharedPtr const &subscriber) final
Subscribe to the transactions feed.
Definition SubscriptionManager.cpp:149
void pubBookChanges(xrpl::LedgerHeader const &lgrInfo, std::vector< data::TransactionAndMetadata > const &transactions) final
Publish the book changes feed.
Definition SubscriptionManager.cpp:32
~SubscriptionManager() override
Destructor of the SubscriptionManager object. It will block until all running jobs finished.
Definition SubscriptionManager.hpp:111
void pubLedger(xrpl::LedgerHeader const &lgrInfo, xrpl::Fees const &fees, std::string const &ledgerRange, std::uint32_t txnCount) final
Publish the ledger feed.
Definition SubscriptionManager.cpp:102
void unsubProposedAccount(xrpl::AccountID const &account, SubscriberSharedPtr const &subscriber) final
Unsubscribe to the proposed transactions feed for particular account.
Definition SubscriptionManager.cpp:71
Feed that publishes the ledger info. Example : {'type': 'ledgerClosed', 'ledger_index': 2647935,...
Definition LedgerFeed.hpp:28
Feed that publishes the Proposed Transactions.
Definition ProposedTransactionFeed.hpp:31
Definition TransactionFeed.hpp:32
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:78
A type-erased execution context.
Definition AnyExecutionContext.hpp:23
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:31
T get(std::string_view fullKey) const
Returns the specified value of given string if value exists.
Definition ConfigDefinition.hpp:85
This namespace implements everything related to subscriptions.
Definition BookChangesFeed.hpp:14
BasicExecutionContext< impl::AsioPoolContext, impl::BasicStopSource, impl::PostDispatchStrategy > PoolExecutionContext
A asio::thread_pool-based execution context.
Definition BasicExecutionContext.hpp:459
Represents a transaction and its metadata bundled together.
Definition Types.hpp:49
Feed that publishes book changes. This feed will be published every ledger, even if there are no chan...
Definition BookChangesFeed.hpp:22
Feed that publishes the json object as it is.
Definition ForwardFeed.hpp:13