Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
SubscriptionManager.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2022, 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/SubscriptionManagerInterface.hpp"
26#include "feed/Types.hpp"
27#include "feed/impl/BookChangesFeed.hpp"
28#include "feed/impl/ForwardFeed.hpp"
29#include "feed/impl/LedgerFeed.hpp"
30#include "feed/impl/ProposedTransactionFeed.hpp"
31#include "feed/impl/TransactionFeed.hpp"
32#include "util/async/AnyExecutionContext.hpp"
33#include "util/async/context/BasicExecutionContext.hpp"
34#include "util/log/Logger.hpp"
35#include "util/newconfig/ConfigDefinition.hpp"
36
37#include <boost/asio/executor_work_guard.hpp>
38#include <boost/asio/io_context.hpp>
39#include <boost/asio/spawn.hpp>
40#include <boost/json/object.hpp>
41#include <xrpl/protocol/AccountID.h>
42#include <xrpl/protocol/Book.h>
43#include <xrpl/protocol/Fees.h>
44#include <xrpl/protocol/LedgerHeader.h>
45
46#include <cstdint>
47#include <memory>
48#include <string>
49#include <utility>
50#include <vector>
51
57namespace feed {
58
63 std::shared_ptr<data::BackendInterface const> backend_;
64 std::shared_ptr<data::AmendmentCenterInterface const> amendmentCenter_;
66 impl::ForwardFeed manifestFeed_;
67 impl::ForwardFeed validationsFeed_;
68 impl::LedgerFeed ledgerFeed_;
69 impl::BookChangesFeed bookChangesFeed_;
70 impl::TransactionFeed transactionFeed_;
71 impl::ProposedTransactionFeed proposedTransactionFeed_;
72
73public:
82 static std::shared_ptr<SubscriptionManager>
85 std::shared_ptr<data::BackendInterface const> const& backend,
86 std::shared_ptr<data::AmendmentCenterInterface const> const& amendmentCenter
87 )
88 {
89 auto const workersNum = config.get<uint64_t>("subscription_workers");
90
91 util::Logger const logger{"Subscriptions"};
92 LOG(logger.info()) << "Starting subscription manager with " << workersNum << " workers";
93
94 return std::make_shared<feed::SubscriptionManager>(
95 util::async::PoolExecutionContext(workersNum), backend, amendmentCenter
96 );
97 }
98
108 std::shared_ptr<data::BackendInterface const> const& backend,
109 std::shared_ptr<data::AmendmentCenterInterface const> const& amendmentCenter
110 )
111 : backend_(backend)
112 , amendmentCenter_(amendmentCenter)
113 , ctx_(std::move(executor))
114 , manifestFeed_(ctx_, "manifest")
115 , validationsFeed_(ctx_, "validations")
116 , ledgerFeed_(ctx_)
117 , bookChangesFeed_(ctx_)
118 , transactionFeed_(ctx_)
119 , proposedTransactionFeed_(ctx_)
120 {
121 }
122
127 {
128 stop();
129 }
130
134 void
135 stop() override
136 {
137 ctx_.stop();
138 ctx_.join();
139 }
140
145 void
146 subBookChanges(SubscriberSharedPtr const& subscriber) final;
147
152 void
153 unsubBookChanges(SubscriberSharedPtr const& subscriber) final;
154
160 void
161 pubBookChanges(ripple::LedgerHeader const& lgrInfo, std::vector<data::TransactionAndMetadata> const& transactions)
162 final;
163
168 void
169 subProposedTransactions(SubscriberSharedPtr const& subscriber) final;
170
175 void
176 unsubProposedTransactions(SubscriberSharedPtr const& subscriber) final;
177
183 void
184 subProposedAccount(ripple::AccountID const& account, SubscriberSharedPtr const& subscriber) final;
185
191 void
192 unsubProposedAccount(ripple::AccountID const& account, SubscriberSharedPtr const& subscriber) final;
193
198 void
199 forwardProposedTransaction(boost::json::object const& receivedTxJson) final;
200
207 boost::json::object
208 subLedger(boost::asio::yield_context yield, SubscriberSharedPtr const& subscriber) final;
209
214 void
215 unsubLedger(SubscriberSharedPtr const& subscriber) final;
216
224 void
225 pubLedger(
226 ripple::LedgerHeader const& lgrInfo,
227 ripple::Fees const& fees,
228 std::string const& ledgerRange,
229 std::uint32_t txnCount
230 ) final;
231
236 void
237 subManifest(SubscriberSharedPtr const& subscriber) final;
238
243 void
244 unsubManifest(SubscriberSharedPtr const& subscriber) final;
245
250 void
251 forwardManifest(boost::json::object const& manifestJson) final;
252
257 void
258 subValidation(SubscriberSharedPtr const& subscriber) final;
259
264 void
265 unsubValidation(SubscriberSharedPtr const& subscriber) final;
266
271 void
272 forwardValidation(boost::json::object const& validationJson) final;
273
278 void
279 subTransactions(SubscriberSharedPtr const& subscriber) final;
280
285 void
286 unsubTransactions(SubscriberSharedPtr const& subscriber) final;
287
293 void
294 subAccount(ripple::AccountID const& account, SubscriberSharedPtr const& subscriber) final;
295
301 void
302 unsubAccount(ripple::AccountID const& account, SubscriberSharedPtr const& subscriber) final;
303
309 void
310 subBook(ripple::Book const& book, SubscriberSharedPtr const& subscriber) final;
311
317 void
318 unsubBook(ripple::Book const& book, SubscriberSharedPtr const& subscriber) final;
319
325 void
326 pubTransaction(data::TransactionAndMetadata const& txMeta, ripple::LedgerHeader const& lgrInfo) final;
327
333 boost::json::object
334 report() const final;
335};
336
337} // namespace feed
Interface of subscription manager. A subscription manager is responsible for managing the subscriptio...
Definition SubscriptionManagerInterface.hpp:44
A subscription manager is responsible for managing the subscriptions and publishing the feeds.
Definition SubscriptionManager.hpp:62
void forwardValidation(boost::json::object const &validationJson) final
Forward the validation feed.
Definition SubscriptionManager.cpp:150
void subBookChanges(SubscriberSharedPtr const &subscriber) final
Subscribe to the book changes feed.
Definition SubscriptionManager.cpp:38
void unsubProposedAccount(ripple::AccountID const &account, SubscriberSharedPtr const &subscriber) final
Unsubscribe to the proposed transactions feed for particular account.
Definition SubscriptionManager.cpp:84
void unsubProposedTransactions(SubscriberSharedPtr const &subscriber) final
Unsubscribe to the proposed transactions feed.
Definition SubscriptionManager.cpp:68
void subManifest(SubscriberSharedPtr const &subscriber) final
Subscribe to the manifest feed.
Definition SubscriptionManager.cpp:120
void subAccount(ripple::AccountID const &account, SubscriberSharedPtr const &subscriber) final
Subscribe to the transactions feed, only receive the feed when particular account is affected.
Definition SubscriptionManager.cpp:168
void unsubBook(ripple::Book const &book, SubscriberSharedPtr const &subscriber) final
Unsubscribe to the transactions feed for particular order book.
Definition SubscriptionManager.cpp:186
boost::json::object report() const final
Get the number of subscribers.
Definition SubscriptionManager.cpp:198
void forwardManifest(boost::json::object const &manifestJson) final
Forward the manifest feed.
Definition SubscriptionManager.cpp:132
void pubTransaction(data::TransactionAndMetadata const &txMeta, ripple::LedgerHeader const &lgrInfo) final
Forward the transactions feed.
Definition SubscriptionManager.cpp:192
void pubLedger(ripple::LedgerHeader const &lgrInfo, ripple::Fees const &fees, std::string const &ledgerRange, std::uint32_t txnCount) final
Publish the ledger feed.
Definition SubscriptionManager.cpp:109
void unsubBookChanges(SubscriberSharedPtr const &subscriber) final
Unsubscribe to the book changes feed.
Definition SubscriptionManager.cpp:44
void pubBookChanges(ripple::LedgerHeader const &lgrInfo, std::vector< data::TransactionAndMetadata > const &transactions) final
Publish the book changes feed.
Definition SubscriptionManager.cpp:50
void unsubTransactions(SubscriberSharedPtr const &subscriber) final
Unsubscribe to the transactions feed.
Definition SubscriptionManager.cpp:162
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:83
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:106
boost::json::object subLedger(boost::asio::yield_context yield, SubscriberSharedPtr const &subscriber) final
Subscribe to the ledger feed.
Definition SubscriptionManager.cpp:97
void subBook(ripple::Book const &book, SubscriberSharedPtr const &subscriber) final
Subscribe to the transactions feed, only receive feed when particular order book is affected.
Definition SubscriptionManager.cpp:180
void subProposedTransactions(SubscriberSharedPtr const &subscriber) final
Subscribe to the proposed transactions feed.
Definition SubscriptionManager.cpp:59
void subValidation(SubscriberSharedPtr const &subscriber) final
Subscribe to the validation feed.
Definition SubscriptionManager.cpp:138
void unsubManifest(SubscriberSharedPtr const &subscriber) final
Unsubscribe to the manifest feed.
Definition SubscriptionManager.cpp:126
void unsubAccount(ripple::AccountID const &account, SubscriberSharedPtr const &subscriber) final
Unsubscribe to the transactions feed for particular account.
Definition SubscriptionManager.cpp:174
void forwardProposedTransaction(boost::json::object const &receivedTxJson) final
Forward the proposed transactions feed.
Definition SubscriptionManager.cpp:91
void unsubValidation(SubscriberSharedPtr const &subscriber) final
Unsubscribe to the validation feed.
Definition SubscriptionManager.cpp:144
void stop() override
Stop the SubscriptionManager and wait for all jobs to finish.
Definition SubscriptionManager.hpp:135
void subProposedAccount(ripple::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:75
void unsubLedger(SubscriberSharedPtr const &subscriber) final
Unsubscribe to the ledger feed.
Definition SubscriptionManager.cpp:103
void subTransactions(SubscriberSharedPtr const &subscriber) final
Subscribe to the transactions feed.
Definition SubscriptionManager.cpp:156
~SubscriptionManager() override
Destructor of the SubscriptionManager object. It will block until all running jobs finished.
Definition SubscriptionManager.hpp:126
Feed that publishes the ledger info. Example : {'type': 'ledgerClosed', 'ledger_index': 2647935,...
Definition LedgerFeed.hpp:46
Feed that publishes the Proposed Transactions.
Definition ProposedTransactionFeed.hpp:51
Definition TransactionFeed.hpp:50
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:111
A type-erased execution context.
Definition AnyExecutionContext.hpp:41
void stop() const
Stop the execution context.
Definition AnyExecutionContext.hpp:255
void join() const
Join the execution context.
Definition AnyExecutionContext.hpp:264
A highly configurable execution context.
Definition BasicExecutionContext.hpp:132
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:54
T get(std::string_view fullKey) const
Returns the specified value of given string if value exists.
Definition ConfigDefinition.hpp:108
This namespace implements everything related to subscriptions.
Definition BookChangesFeed.hpp:33
Represents a transaction and its metadata bundled together.
Definition Types.hpp:68
Feed that publishes book changes. This feed will be published every ledger, even if there are no chan...
Definition BookChangesFeed.hpp:40
Feed that publishes the json object as it is.
Definition ForwardFeed.hpp:32