xrpld
Loading...
Searching...
No Matches
InfoSub.h
1#pragma once
2
3#include <xrpl/basics/CountedObject.h>
4#include <xrpl/basics/UnorderedContainers.h>
5#include <xrpl/beast/utility/Journal.h>
6#include <xrpl/json/json_value.h>
7#include <xrpl/protocol/AccountID.h>
8#include <xrpl/protocol/Book.h>
9#include <xrpl/protocol/ErrorCodes.h>
10#include <xrpl/resource/Consumer.h>
11#include <xrpl/server/Manifest.h>
12
13#include <atomic>
14#include <cstddef>
15#include <cstdint>
16#include <functional>
17#include <memory>
18#include <mutex>
19#include <string>
20
21namespace xrpl {
22
23// Operations that clients may wish to perform against the network
24// Master operational handler, server sequencer, network tracker
25
36
50[[nodiscard]] constexpr bool
52 std::size_t current,
53 std::size_t additional,
55{
56 return additional > cap || current > cap - additional;
57}
58
59class InfoSubRequest : public CountedObject<InfoSubRequest>
60{
61public:
63
64 virtual ~InfoSubRequest() = default;
65
66 virtual json::Value
67 doClose() = 0;
68 virtual json::Value
69 doStatus(json::Value const&) = 0;
70};
71
88class InfoSub : public CountedObject<InfoSub>
89{
90public:
92
93 // VFALCO TODO Standardize on the names of weak / strong pointer type
94 // aliases.
96
98
100
101public:
105 class Source
106 {
107 public:
108 virtual ~Source() = default;
109
110 // For some reason, these were originally called "rt"
111 // for "real time". They actually refer to whether
112 // you get transactions as they occur or once their
113 // results are confirmed
114 virtual void
115 subAccount(ref ispListener, hash_set<AccountID> const& vnaAccountIDs, bool realTime) = 0;
116
117 // for normal use, removes from InfoSub and server
118 virtual void
119 unsubAccount(ref isplistener, hash_set<AccountID> const& vnaAccountIDs, bool realTime) = 0;
120
121 // for use during InfoSub destruction
122 // Removes only from the server
123 virtual void
125 std::uint64_t uListener,
126 hash_set<AccountID> const& vnaAccountIDs,
127 bool realTime) = 0;
128
134 virtual ErrorCodeI
135 subAccountHistory(ref ispListener, AccountID const& account) = 0;
136
145 virtual void
146 unsubAccountHistory(ref ispListener, AccountID const& account, bool historyOnly) = 0;
147
148 virtual void
150 std::uint64_t uListener,
151 AccountID const& account,
152 bool historyOnly) = 0;
153
175 virtual void
177 std::uint64_t seq,
178 hash_set<AccountID> rtAccounts,
179 hash_set<AccountID> normalAccounts,
180 hash_set<AccountID> historyAccounts) = 0;
181
182 // VFALCO TODO Document the bool return value
183 virtual bool
184 subLedger(ref ispListener, json::Value& jvResult) = 0;
185 virtual bool
187
188 virtual bool
189 subBookChanges(ref ispListener) = 0;
190 virtual bool
192
193 virtual bool
194 subManifests(ref ispListener) = 0;
195 virtual bool
197 virtual void
199
200 virtual bool
201 subServer(ref ispListener, json::Value& jvResult, bool admin) = 0;
202 virtual bool
204
205 virtual bool
206 subBook(ref ispListener, Book const&) = 0;
207
225 virtual bool
226 unsubBook(ref ispListener, Book const&) = 0;
227
242 virtual bool
243 unsubBookInternal(std::uint64_t uListener, Book const&) = 0;
244
245 virtual bool
246 subTransactions(ref ispListener) = 0;
247 virtual bool
249
250 virtual bool
251 subRTTransactions(ref ispListener) = 0;
252 virtual bool
254
255 virtual bool
256 subValidations(ref ispListener) = 0;
257 virtual bool
259
260 virtual bool
261 subPeerStatus(ref ispListener) = 0;
262
263 virtual bool
265 virtual void
267
268 virtual bool
269 subConsensus(ref ispListener) = 0;
270 virtual bool
272
273 // VFALCO TODO Remove
274 // This was added for one particular partner, it
275 // "pushes" subscription data to a particular URL.
276 //
277 virtual pointer
278 findRpcSub(std::string const& strUrl) = 0;
279 virtual pointer
280 addRpcSub(std::string const& strUrl, ref rspEntry) = 0;
281 virtual bool
282 tryRemoveRpcSub(std::string const& strUrl) = 0;
283
289 [[nodiscard]] virtual beast::Journal const&
290 journal() const = 0;
291 };
292
293public:
294 InfoSub(Source& source);
295 InfoSub(Source& source, Consumer consumer);
296
297 virtual ~InfoSub();
298
299 Consumer&
300 getConsumer();
301
302 virtual void
303 send(json::Value const& jvObj, bool broadcast) = 0;
304
305 [[nodiscard]] std::uint64_t
306 getSeq() const;
307
320 [[nodiscard]] std::size_t
322
339 [[nodiscard]] bool
341 hash_set<AccountID> const& proposedAccounts,
342 hash_set<AccountID> const& normalAccounts,
343 std::size_t cap);
344
355 [[nodiscard]] bool
356 hasAccountHistorySubscription(AccountID const& account) const;
357
358 void
359 onSendEmpty();
360
361 void
362 insertSubAccountInfo(AccountID const& account, bool rt);
363
364 void
365 deleteSubAccountInfo(AccountID const& account, bool rt);
366
378 void
379 insertBookSubscription(Book const& book);
380
391 void
392 deleteBookSubscription(Book const& book);
393
394 // return false if already subscribed to this account
395 bool
396 insertSubAccountHistory(AccountID const& account);
397
398 void
399 deleteSubAccountHistory(AccountID const& account);
400
401 void
402 clearRequest();
403
404 void
406
408 getRequest();
409
410 void
411 setApiVersion(unsigned int apiVersion);
412
413 [[nodiscard]] unsigned int
414 getApiVersion() const noexcept;
415
416protected:
417 // Mutable so the read-only totalSubscriptionCount() accessor can lock it
418 // from a const method; locking semantics are otherwise unchanged.
419 mutable std::mutex lock_;
420
421private:
427 std::uint64_t seq_;
430 unsigned int apiVersion_ = 0;
431
432 static int
434 {
435 static std::atomic<std::uint64_t> kID(0);
436 return ++kID;
437 }
438};
439
440} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Represents a JSON value.
Definition json_value.h:117
Specifies an order book.
Definition Book.h:28
std::shared_ptr< InfoSubRequest > pointer
Definition InfoSub.h:62
virtual json::Value doClose()=0
virtual json::Value doStatus(json::Value const &)=0
virtual ~InfoSubRequest()=default
Abstracts the source of subscription data.
Definition InfoSub.h:106
virtual ErrorCodeI subAccountHistory(ref ispListener, AccountID const &account)=0
subscribe an account's new transactions and retrieve the account's historical transactions
virtual void pubPeerStatus(std::function< json::Value(void)> const &)=0
virtual void unsubAccountHistoryInternal(std::uint64_t uListener, AccountID const &account, bool historyOnly)=0
virtual bool unsubRTTransactions(std::uint64_t uListener)=0
virtual bool subTransactions(ref ispListener)=0
virtual void pubManifest(Manifest const &)=0
virtual bool unsubConsensus(std::uint64_t uListener)=0
virtual bool unsubTransactions(std::uint64_t uListener)=0
virtual void unsubAccountInternal(std::uint64_t uListener, hash_set< AccountID > const &vnaAccountIDs, bool realTime)=0
virtual bool unsubServer(std::uint64_t uListener)=0
virtual beast::Journal const & journal() const =0
Journal used by InfoSub for diagnostics that occur after the owning subsystem (e.g.
virtual bool tryRemoveRpcSub(std::string const &strUrl)=0
virtual bool unsubBookChanges(std::uint64_t uListener)=0
virtual void unsubAccountHistory(ref ispListener, AccountID const &account, bool historyOnly)=0
unsubscribe an account's transactions
virtual bool subPeerStatus(ref ispListener)=0
virtual bool subServer(ref ispListener, json::Value &jvResult, bool admin)=0
virtual bool subConsensus(ref ispListener)=0
virtual void subAccount(ref ispListener, hash_set< AccountID > const &vnaAccountIDs, bool realTime)=0
virtual bool subBook(ref ispListener, Book const &)=0
virtual bool subValidations(ref ispListener)=0
virtual ~Source()=default
virtual bool subRTTransactions(ref ispListener)=0
virtual bool unsubBook(ref ispListener, Book const &)=0
Remove a book subscription for a live subscriber.
virtual bool unsubLedger(std::uint64_t uListener)=0
virtual bool subLedger(ref ispListener, json::Value &jvResult)=0
virtual bool unsubPeerStatus(std::uint64_t uListener)=0
virtual bool subBookChanges(ref ispListener)=0
virtual bool unsubManifests(std::uint64_t uListener)=0
virtual pointer addRpcSub(std::string const &strUrl, ref rspEntry)=0
virtual void scheduleAccountCleanup(std::uint64_t seq, hash_set< AccountID > rtAccounts, hash_set< AccountID > normalAccounts, hash_set< AccountID > historyAccounts)=0
Schedule the server-side teardown of a disconnecting connection's account subscriptions off the destr...
virtual bool unsubBookInternal(std::uint64_t uListener, Book const &)=0
Remove a book subscription during InfoSub teardown.
virtual bool subManifests(ref ispListener)=0
virtual bool unsubValidations(std::uint64_t uListener)=0
virtual pointer findRpcSub(std::string const &strUrl)=0
virtual void unsubAccount(ref isplistener, hash_set< AccountID > const &vnaAccountIDs, bool realTime)=0
void setRequest(std::shared_ptr< InfoSubRequest > const &req)
void insertBookSubscription(Book const &book)
Record that this subscriber is following book.
virtual void send(json::Value const &jvObj, bool broadcast)=0
std::shared_ptr< InfoSub > pointer
Definition InfoSub.h:91
InfoSub(Source &source)
Consumer consumer_
Definition InfoSub.h:422
bool insertSubAccountHistory(AccountID const &account)
bool tryReserveAccountSubscriptions(hash_set< AccountID > const &proposedAccounts, hash_set< AccountID > const &normalAccounts, std::size_t cap)
Enforce the cap and reserve a request's net-new accounts, atomically.
std::size_t totalSubscriptionCount() const
Return the number of subscriptions currently tracked on this connection.
void setApiVersion(unsigned int apiVersion)
static int assignId()
Definition InfoSub.h:433
std::uint64_t getSeq() const
resource::Consumer Consumer
Definition InfoSub.h:99
void insertSubAccountInfo(AccountID const &account, bool rt)
std::shared_ptr< InfoSub > const & ref
Definition InfoSub.h:97
void deleteBookSubscription(Book const &book)
Stop tracking book for this subscriber.
bool hasAccountHistorySubscription(AccountID const &account) const
Whether this connection already tracks an account-history for account.
Source & source_
Definition InfoSub.h:423
hash_set< AccountID > accountHistorySubscriptions_
Definition InfoSub.h:428
hash_set< AccountID > normalSubscriptions_
Definition InfoSub.h:425
hash_set< Book > bookSubscriptions_
Definition InfoSub.h:429
std::weak_ptr< InfoSub > wptr
Definition InfoSub.h:95
hash_set< AccountID > realTimeSubscriptions_
Definition InfoSub.h:424
void deleteSubAccountInfo(AccountID const &account, bool rt)
void deleteSubAccountHistory(AccountID const &account)
unsigned int getApiVersion() const noexcept
std::uint64_t seq_
Definition InfoSub.h:427
std::mutex lock_
Definition InfoSub.h:419
unsigned int apiVersion_
Definition InfoSub.h:430
std::shared_ptr< InfoSubRequest > const & getRequest()
std::shared_ptr< InfoSubRequest > request_
Definition InfoSub.h:426
An endpoint that consumes resources.
Definition Consumer.h:20
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
ErrorCodeI
Definition ErrorCodes.h:23
std::unordered_set< Value, Hash, Pred, Allocator > hash_set
constexpr std::size_t kMaxSubscriptionsPerConnection
Maximum number of subscriptions a single client connection may hold at once.
Definition InfoSub.h:35
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
constexpr bool exceedsSubscriptionCap(std::size_t current, std::size_t additional, std::size_t cap=kMaxSubscriptionsPerConnection)
Whether adding additional subscriptions to a connection already holding current would exceed the cap.
Definition InfoSub.h:51