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/BackendInterface.hpp"
23#include "data/Types.hpp"
24#include "feed/Types.hpp"
25#include "feed/impl/TrackableSignal.hpp"
26#include "feed/impl/TrackableSignalMap.hpp"
27#include "feed/impl/Util.hpp"
28#include "util/async/AnyExecutionContext.hpp"
29#include "util/async/AnyStrand.hpp"
30#include "util/log/Logger.hpp"
31#include "util/prometheus/Gauge.hpp"
32
33#include <boost/asio/io_context.hpp>
34#include <boost/asio/strand.hpp>
35#include <fmt/core.h>
36#include <xrpl/protocol/AccountID.h>
37#include <xrpl/protocol/Book.h>
38#include <xrpl/protocol/LedgerHeader.h>
39
40#include <array>
41#include <cstdint>
42#include <functional>
43#include <memory>
44#include <string>
45#include <unordered_set>
46
47namespace feed::impl {
48
50 // Hold two versions of transaction messages
51 using AllVersionTransactionsType = std::array<std::shared_ptr<std::string>, 2>;
52
53 struct TransactionSlot {
54 std::reference_wrapper<TransactionFeed> feed;
55 std::weak_ptr<Subscriber> subscriptionContextWeakPtr;
56
57 TransactionSlot(TransactionFeed& feed, SubscriberSharedPtr const& connection)
58 : feed(feed), subscriptionContextWeakPtr(connection)
59 {
60 }
61
62 void
63 operator()(AllVersionTransactionsType const& allVersionMsgs) const;
64 };
65
66 util::Logger logger_{"Subscriptions"};
67
69 std::reference_wrapper<util::prometheus::GaugeInt> subAllCount_;
70 std::reference_wrapper<util::prometheus::GaugeInt> subAccountCount_;
71 std::reference_wrapper<util::prometheus::GaugeInt> subBookCount_;
72
76
77 // Signals for proposed tx subscribers
80
81 std::unordered_set<SubscriberPtr>
82 notified_; // Used by slots to prevent double notifications if tx contains multiple subscribed accounts
83
84public:
90 : strand_(executionCtx.makeStrand())
91 , subAllCount_(getSubscriptionsGaugeInt("tx"))
92 , subAccountCount_(getSubscriptionsGaugeInt("account"))
93 , subBookCount_(getSubscriptionsGaugeInt("book"))
94 {
95 }
96
101 void
102 sub(SubscriberSharedPtr const& subscriber);
103
109 void
110 sub(ripple::AccountID const& account, SubscriberSharedPtr const& subscriber);
111
117 void
118 sub(ripple::Book const& book, SubscriberSharedPtr const& subscriber);
119
124 void
125 subProposed(SubscriberSharedPtr const& subscriber);
126
133 void
134 subProposed(ripple::AccountID const& account, SubscriberSharedPtr const& subscriber);
135
140 void
141 unsub(SubscriberSharedPtr const& subscriber);
142
148 void
149 unsub(ripple::AccountID const& account, SubscriberSharedPtr const& subscriber);
150
155 void
156 unsubProposed(SubscriberSharedPtr const& subscriber);
157
163 void
164 unsubProposed(ripple::AccountID const& account, SubscriberSharedPtr const& subscriber);
165
171 void
172 unsub(ripple::Book const& book, SubscriberSharedPtr const& subscriber);
173
180 void
181 pub(data::TransactionAndMetadata const& txMeta,
182 ripple::LedgerHeader const& lgrInfo,
183 std::shared_ptr<data::BackendInterface const> const& backend);
184
188 std::uint64_t
189 transactionSubCount() const;
190
194 std::uint64_t
195 accountSubCount() const;
196
200 std::uint64_t
201 bookSubCount() const;
202
203private:
204 void
205 unsubInternal(SubscriberPtr subscriber);
206
207 void
208 unsubInternal(ripple::AccountID const& account, SubscriberPtr subscriber);
209
210 void
211 unsubProposedInternal(SubscriberPtr subscriber);
212
213 void
214 unsubProposedInternal(ripple::AccountID const& account, SubscriberPtr subscriber);
215
216 void
217 unsubInternal(ripple::Book const& book, SubscriberPtr subscriber);
218};
219} // 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:49
void unsub(SubscriberSharedPtr const &subscriber)
Unsubscribe to the transaction feed.
Definition TransactionFeed.cpp:126
void sub(SubscriberSharedPtr const &subscriber)
Subscribe to the transaction feed.
Definition TransactionFeed.cpp:72
void unsubProposed(SubscriberSharedPtr const &subscriber)
Unsubscribe to the transaction feed for proposed transaction stream.
Definition TransactionFeed.cpp:138
std::uint64_t bookSubCount() const
Get the number of books subscribers.
Definition TransactionFeed.cpp:168
std::uint64_t transactionSubCount() const
Get the number of subscribers of the transaction feed.
Definition TransactionFeed.cpp:156
std::uint64_t accountSubCount() const
Get the number of accounts subscribers.
Definition TransactionFeed.cpp:162
void pub(data::TransactionAndMetadata const &txMeta, ripple::LedgerHeader const &lgrInfo, std::shared_ptr< data::BackendInterface const > const &backend)
Publishes the transaction feed.
Definition TransactionFeed.cpp:174
TransactionFeed(util::async::AnyExecutionContext &executionCtx)
Construct a new Transaction Feed object.
Definition TransactionFeed.hpp:89
void subProposed(SubscriberSharedPtr const &subscriber)
Subscribe to the transaction feed for proposed transaction stream.
Definition TransactionFeed.cpp:94
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:110
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