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/protocol/UintTypes.h>
11#include <xrpl/resource/Consumer.h>
12#include <xrpl/server/Manifest.h>
13
14#include <atomic>
15#include <cstddef>
16#include <cstdint>
17#include <functional>
18#include <memory>
19#include <mutex>
20#include <string>
21
22namespace xrpl {
23
24// Operations that clients may wish to perform against the network
25// Master operational handler, server sequencer, network tracker
26
37
51[[nodiscard]] constexpr bool
53 std::size_t current,
54 std::size_t additional,
56{
57 return additional > cap || current > cap - additional;
58}
59
60class InfoSubRequest : public CountedObject<InfoSubRequest>
61{
62public:
64
65 virtual ~InfoSubRequest() = default;
66
67 virtual json::Value
68 doClose() = 0;
69 virtual json::Value
70 doStatus(json::Value const&) = 0;
71};
72
89class InfoSub : public CountedObject<InfoSub>
90{
91public:
93
94 // VFALCO TODO Standardize on the names of weak / strong pointer type
95 // aliases.
97
99
101
102public:
106 class Source
107 {
108 public:
109 virtual ~Source() = default;
110
111 // For some reason, these were originally called "rt"
112 // for "real time". They actually refer to whether
113 // you get transactions as they occur or once their
114 // results are confirmed
115 virtual void
116 subAccount(Ref ispListener, HashSet<AccountID> const& vnaAccountIDs, bool realTime) = 0;
117
118 // for normal use, removes from InfoSub and server
119 virtual void
120 unsubAccount(Ref isplistener, HashSet<AccountID> const& vnaAccountIDs, bool realTime) = 0;
121
122 // for use during InfoSub destruction
123 // Removes only from the server
124 virtual void
126 std::uint64_t uListener,
127 HashSet<AccountID> const& vnaAccountIDs,
128 bool realTime) = 0;
129
135 virtual ErrorCodeI
136 subAccountHistory(Ref ispListener, AccountID const& account) = 0;
137
146 virtual void
147 unsubAccountHistory(Ref ispListener, AccountID const& account, bool historyOnly) = 0;
148
149 virtual void
151 std::uint64_t uListener,
152 AccountID const& account,
153 bool historyOnly) = 0;
154
167 virtual void
168 unsubMPTInternal(std::uint64_t uListener, MPTID const& mptID) = 0;
169
191 virtual void
193 std::uint64_t seq,
194 HashSet<AccountID> rtAccounts,
195 HashSet<AccountID> normalAccounts,
196 HashSet<AccountID> historyAccounts) = 0;
197
198 // VFALCO TODO Document the bool return value
199 virtual bool
200 subLedger(Ref ispListener, json::Value& jvResult) = 0;
201 virtual bool
203
204 virtual bool
205 subBookChanges(Ref ispListener) = 0;
206 virtual bool
208
209 virtual bool
210 subManifests(Ref ispListener) = 0;
211 virtual bool
213 virtual void
215
216 virtual bool
217 subServer(Ref ispListener, json::Value& jvResult, bool admin) = 0;
218 virtual bool
220
221 virtual bool
222 subBook(Ref ispListener, Book const&) = 0;
223
241 virtual bool
242 unsubBook(Ref ispListener, Book const&) = 0;
243
258 virtual bool
259 unsubBookInternal(std::uint64_t uListener, Book const&) = 0;
260
261 virtual bool
262 subTransactions(Ref ispListener) = 0;
263 virtual bool
265
266 virtual bool
267 subRTTransactions(Ref ispListener) = 0;
268 virtual bool
270
271 virtual bool
272 subValidations(Ref ispListener) = 0;
273 virtual bool
275
276 virtual bool
277 subPeerStatus(Ref ispListener) = 0;
278
279 virtual bool
281 virtual void
283
284 virtual bool
285 subConsensus(Ref ispListener) = 0;
286 virtual bool
288
289 virtual void
290 subMPT(InfoSub::Ref ispListener, HashSet<MPTID> const& mptIDs) = 0;
291 virtual void
292 unsubMPT(InfoSub::Ref ispListener, HashSet<MPTID> const& mptIDs) = 0;
293
294 // VFALCO TODO Remove
295 // This was added for one particular partner, it
296 // "pushes" subscription data to a particular URL.
297 //
298 virtual pointer
299 findRpcSub(std::string const& strUrl) = 0;
300 virtual pointer
301 addRpcSub(std::string const& strUrl, Ref rspEntry) = 0;
302 virtual bool
303 tryRemoveRpcSub(std::string const& strUrl) = 0;
304
310 [[nodiscard]] virtual beast::Journal const&
311 journal() const = 0;
312 };
313
314public:
315 InfoSub(Source& source);
316 InfoSub(Source& source, Consumer consumer);
317
318 virtual ~InfoSub();
319
320 Consumer&
321 getConsumer();
322
323 virtual void
324 send(json::Value const& jvObj, bool broadcast) = 0;
325
326 [[nodiscard]] std::uint64_t
327 getSeq() const;
328
341 [[nodiscard]] std::size_t
343
360 [[nodiscard]] bool
362 HashSet<AccountID> const& proposedAccounts,
363 HashSet<AccountID> const& normalAccounts,
364 std::size_t cap);
365
382 [[nodiscard]] bool
384
395 [[nodiscard]] bool
396 hasAccountHistorySubscription(AccountID const& account) const;
397
398 void
399 onSendEmpty();
400
401 void
402 insertSubAccountInfo(AccountID const& account, bool rt);
403
404 void
405 deleteSubAccountInfo(AccountID const& account, bool rt);
406
418 void
419 insertBookSubscription(Book const& book);
420
431 void
432 deleteBookSubscription(Book const& book);
433
434 // return false if already subscribed to this account
435 bool
436 insertSubAccountHistory(AccountID const& account);
437
438 void
439 deleteSubAccountHistory(AccountID const& account);
440
441 void
442 clearRequest();
443
444 void
446
448 getRequest();
449
450 void
451 setApiVersion(unsigned int apiVersion);
452
453 [[nodiscard]] unsigned int
454 getApiVersion() const noexcept;
455
456 void
457 insertSubMPTInfo(MPTID const& mptID);
458
459 void
460 deleteSubMPTInfo(MPTID const& mptID);
461
462protected:
463 // Mutable so the read-only totalSubscriptionCount() accessor can lock it
464 // from a const method; locking semantics are otherwise unchanged.
465 mutable std::mutex lock_;
466
467private:
468 // The lock type guarding this instance's subscription sets.
469 using ScopedLock = std::scoped_lock<decltype(lock_)>;
470
476 [[nodiscard]] std::size_t
477 subscriptionCount(ScopedLock const& lock) const;
478
484 std::uint64_t seq_;
488 unsigned int apiVersion_ = 0;
489
490 static int
492 {
493 static std::atomic<std::uint64_t> kID(0);
494 return ++kID;
495 }
496};
497
498} // 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:63
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:107
virtual void pubPeerStatus(std::function< json::Value(void)> const &)=0
virtual void scheduleAccountCleanup(std::uint64_t seq, HashSet< AccountID > rtAccounts, HashSet< AccountID > normalAccounts, HashSet< AccountID > historyAccounts)=0
Schedule the server-side teardown of a disconnecting connection's account subscriptions off the destr...
virtual void unsubAccountHistoryInternal(std::uint64_t uListener, AccountID const &account, bool historyOnly)=0
virtual bool unsubRTTransactions(std::uint64_t uListener)=0
virtual void pubManifest(Manifest const &)=0
virtual bool unsubConsensus(std::uint64_t uListener)=0
virtual bool unsubTransactions(std::uint64_t uListener)=0
virtual bool subBookChanges(Ref ispListener)=0
virtual bool subServer(Ref ispListener, json::Value &jvResult, bool admin)=0
virtual bool subPeerStatus(Ref ispListener)=0
virtual void unsubAccount(Ref isplistener, HashSet< 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 bool subRTTransactions(Ref ispListener)=0
virtual bool subTransactions(Ref ispListener)=0
virtual ~Source()=default
virtual bool unsubLedger(std::uint64_t uListener)=0
virtual void subAccount(Ref ispListener, HashSet< AccountID > const &vnaAccountIDs, bool realTime)=0
virtual bool subManifests(Ref ispListener)=0
virtual bool unsubPeerStatus(std::uint64_t uListener)=0
virtual bool subBook(Ref ispListener, Book const &)=0
virtual ErrorCodeI subAccountHistory(Ref ispListener, AccountID const &account)=0
subscribe an account's new transactions and retrieve the account's historical transactions
virtual bool unsubBook(Ref ispListener, Book const &)=0
Remove a book subscription for a live subscriber.
virtual bool unsubManifests(std::uint64_t uListener)=0
virtual void unsubAccountInternal(std::uint64_t uListener, HashSet< AccountID > const &vnaAccountIDs, bool realTime)=0
virtual bool subLedger(Ref ispListener, json::Value &jvResult)=0
virtual void unsubMPT(InfoSub::Ref ispListener, HashSet< MPTID > const &mptIDs)=0
virtual void unsubMPTInternal(std::uint64_t uListener, MPTID const &mptID)=0
Remove an MPT issuance subscription during InfoSub teardown.
virtual bool unsubBookInternal(std::uint64_t uListener, Book const &)=0
Remove a book subscription during InfoSub teardown.
virtual bool unsubValidations(std::uint64_t uListener)=0
virtual pointer findRpcSub(std::string const &strUrl)=0
virtual void subMPT(InfoSub::Ref ispListener, HashSet< MPTID > const &mptIDs)=0
virtual bool subValidations(Ref ispListener)=0
virtual void unsubAccountHistory(Ref ispListener, AccountID const &account, bool historyOnly)=0
unsubscribe an account's transactions
virtual pointer addRpcSub(std::string const &strUrl, Ref rspEntry)=0
virtual bool subConsensus(Ref ispListener)=0
void setRequest(std::shared_ptr< InfoSubRequest > const &req)
void insertBookSubscription(Book const &book)
Record that this subscriber is following book.
std::shared_ptr< InfoSub > const & Ref
Definition InfoSub.h:98
virtual void send(json::Value const &jvObj, bool broadcast)=0
std::shared_ptr< InfoSub > pointer
Definition InfoSub.h:92
InfoSub(Source &source)
Consumer consumer_
Definition InfoSub.h:479
bool insertSubAccountHistory(AccountID const &account)
bool tryReserveMPTSubscriptions(HashSet< MPTID > const &mptIDs, std::size_t cap)
Enforce the cap and reserve a request's net-new MPT issuances, atomically.
HashSet< MPTID > mptSubscriptions_
Definition InfoSub.h:487
bool tryReserveAccountSubscriptions(HashSet< AccountID > const &proposedAccounts, HashSet< AccountID > const &normalAccounts, std::size_t cap)
Enforce the cap and reserve a request's net-new accounts, atomically.
void deleteSubMPTInfo(MPTID const &mptID)
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:491
std::uint64_t getSeq() const
HashSet< AccountID > realTimeSubscriptions_
Definition InfoSub.h:481
std::scoped_lock< decltype(lock_)> ScopedLock
Definition InfoSub.h:469
resource::Consumer Consumer
Definition InfoSub.h:100
void insertSubAccountInfo(AccountID const &account, bool rt)
std::size_t subscriptionCount(ScopedLock const &lock) const
The combined tally the per-connection cap is enforced against.
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:480
HashSet< AccountID > normalSubscriptions_
Definition InfoSub.h:482
HashSet< Book > bookSubscriptions_
Definition InfoSub.h:486
void insertSubMPTInfo(MPTID const &mptID)
HashSet< AccountID > accountHistorySubscriptions_
Definition InfoSub.h:485
void deleteSubAccountInfo(AccountID const &account, bool rt)
void deleteSubAccountHistory(AccountID const &account)
unsigned int getApiVersion() const noexcept
std::uint64_t seq_
Definition InfoSub.h:484
std::mutex lock_
Definition InfoSub.h:465
unsigned int apiVersion_
Definition InfoSub.h:488
std::weak_ptr< InfoSub > Wptr
Definition InfoSub.h:96
std::shared_ptr< InfoSubRequest > const & getRequest()
std::shared_ptr< InfoSubRequest > request_
Definition InfoSub.h:483
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
BaseUInt< 192 > MPTID
MPTID is a 192-bit value representing MPT Issuance ID, which is a concatenation of a 32-bit sequence ...
Definition UintTypes.h:54
std::unordered_set< Value, Hash, Pred, Allocator > HashSet
constexpr std::size_t kMaxSubscriptionsPerConnection
Maximum number of subscriptions a single client connection may hold at once.
Definition InfoSub.h:36
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:52