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/core.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
77
78 // Signals for proposed tx subscribers
81
82 std::unordered_set<SubscriberPtr>
83 notified_; // Used by slots to prevent double notifications if tx contains multiple subscribed accounts
84
85public:
91 : strand_(executionCtx.makeStrand())
92 , subAllCount_(getSubscriptionsGaugeInt("tx"))
93 , subAccountCount_(getSubscriptionsGaugeInt("account"))
94 , subBookCount_(getSubscriptionsGaugeInt("book"))
95 {
96 }
97
102
107 void
108 sub(SubscriberSharedPtr const& subscriber);
109
115 void
116 sub(ripple::AccountID const& account, SubscriberSharedPtr const& subscriber);
117
123 void
124 sub(ripple::Book const& book, SubscriberSharedPtr const& subscriber);
125
130 void
131 subProposed(SubscriberSharedPtr const& subscriber);
132
139 void
140 subProposed(ripple::AccountID const& account, SubscriberSharedPtr const& subscriber);
141
146 void
147 unsub(SubscriberSharedPtr const& subscriber);
148
154 void
155 unsub(ripple::AccountID const& account, SubscriberSharedPtr const& subscriber);
156
161 void
162 unsubProposed(SubscriberSharedPtr const& subscriber);
163
169 void
170 unsubProposed(ripple::AccountID const& account, SubscriberSharedPtr const& subscriber);
171
177 void
178 unsub(ripple::Book const& book, SubscriberSharedPtr const& subscriber);
179
187 void
188 pub(data::TransactionAndMetadata const& txMeta,
189 ripple::LedgerHeader const& lgrInfo,
190 std::shared_ptr<data::BackendInterface const> const& backend,
191 std::shared_ptr<data::AmendmentCenterInterface const> const& amendmentCenter,
192 uint32_t networkID);
193
197 std::uint64_t
198 transactionSubCount() const;
199
203 std::uint64_t
204 accountSubCount() const;
205
209 std::uint64_t
210 bookSubCount() const;
211
212private:
213 void
214 unsubInternal(SubscriberPtr subscriber);
215
216 void
217 unsubInternal(ripple::AccountID const& account, SubscriberPtr subscriber);
218
219 void
220 unsubProposedInternal(SubscriberPtr subscriber);
221
222 void
223 unsubProposedInternal(ripple::AccountID const& account, SubscriberPtr subscriber);
224
225 void
226 unsubInternal(ripple::Book const& book, SubscriberPtr subscriber);
227};
228} // 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:44
Definition TransactionFeed.hpp:50
void unsub(SubscriberSharedPtr const &subscriber)
Unsubscribe to the transaction feed.
Definition TransactionFeed.cpp:128
void sub(SubscriberSharedPtr const &subscriber)
Subscribe to the transaction feed.
Definition TransactionFeed.cpp:74
void unsubProposed(SubscriberSharedPtr const &subscriber)
Unsubscribe to the transaction feed for proposed transaction stream.
Definition TransactionFeed.cpp:140
std::uint64_t bookSubCount() const
Get the number of books subscribers.
Definition TransactionFeed.cpp:170
std::uint64_t transactionSubCount() const
Get the number of subscribers of the transaction feed.
Definition TransactionFeed.cpp:158
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:164
TransactionFeed(util::async::AnyExecutionContext &executionCtx)
Construct a new Transaction Feed object.
Definition TransactionFeed.hpp:90
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:176
void subProposed(SubscriberSharedPtr const &subscriber)
Subscribe to the transaction feed for proposed transaction stream.
Definition TransactionFeed.cpp:96
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
A type-erased execution context.
Definition AnyStrand.hpp:40
An interface to provide connection functionality for subscriptions.
Definition SubscriptionContextInterface.hpp:39
This namespace implements everything related to subscriptions.
Definition BookChangesFeed.hpp:33
Represents a transaction and its metadata bundled together.
Definition Types.hpp:68