Kagome
Polkadot Runtime Engine in C++17
transaction_pool_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_TRANSACTION_POOL_IMPL_HPP
7 #define KAGOME_TRANSACTION_POOL_IMPL_HPP
8 
10 #include "log/logger.hpp"
11 #include "metrics/metrics.hpp"
12 #include "outcome/outcome.hpp"
18 
19 namespace kagome::runtime {
20  class TaggedTransactionQueue;
21 }
22 namespace kagome::crypto {
23  class Hasher;
24 }
25 namespace kagome::network {
26  class TransactionsTransmitter;
27 }
28 
29 namespace kagome::transaction_pool {
30 
32  public:
34  std::shared_ptr<runtime::TaggedTransactionQueue> ttq,
35  std::shared_ptr<crypto::Hasher> hasher,
36  std::shared_ptr<network::TransactionsTransmitter> tx_transmitter,
37  std::unique_ptr<PoolModerator> moderator,
38  std::shared_ptr<blockchain::BlockHeaderRepository> header_repo,
39  std::shared_ptr<primitives::events::ExtrinsicSubscriptionEngine>
40  sub_engine,
41  std::shared_ptr<subscription::ExtrinsicEventKeyRepository> ext_key_repo,
42  Limits limits);
43 
45  TransactionPoolImpl(const TransactionPoolImpl &) = delete;
46 
47  ~TransactionPoolImpl() override = default;
48 
49  TransactionPoolImpl &operator=(TransactionPoolImpl &&) = delete;
50  TransactionPoolImpl &operator=(const TransactionPoolImpl &) = delete;
51 
52  const std::unordered_map<Transaction::Hash, std::shared_ptr<Transaction>>
53  &getPendingTransactions() const override;
54 
55  outcome::result<Transaction::Hash> submitExtrinsic(
57  primitives::Extrinsic extrinsic) override;
58 
59  outcome::result<void> submitOne(Transaction &&tx) override;
60 
61  outcome::result<Transaction> removeOne(
62  const Transaction::Hash &tx_hash) override;
63 
64  std::map<Transaction::Hash, std::shared_ptr<Transaction>>
65  getReadyTransactions() const override;
66 
67  outcome::result<std::vector<Transaction>> removeStale(
68  const primitives::BlockId &at) override;
69 
70  Status getStatus() const override;
71 
72  outcome::result<primitives::Transaction> constructTransaction(
74  primitives::Extrinsic extrinsic) const override;
75 
76  private:
77  outcome::result<void> submitOne(const std::shared_ptr<Transaction> &tx);
78 
79  outcome::result<void> processTransaction(
80  const std::shared_ptr<Transaction> &tx);
81 
82  outcome::result<void> processTransactionAsReady(
83  const std::shared_ptr<Transaction> &tx);
84 
85  outcome::result<void> processTransactionAsWaiting(
86  const std::shared_ptr<Transaction> &tx);
87 
88  outcome::result<void> ensureSpace() const;
89 
90  bool hasSpaceInReady() const;
91 
92  void addTransactionAsWaiting(const std::shared_ptr<Transaction> &tx);
93 
94  void delTransactionAsWaiting(const std::shared_ptr<Transaction> &tx);
95 
97  void postponeTransaction(const std::shared_ptr<Transaction> &tx);
98 
100  void processPostponedTransactions();
101 
102  void provideTag(const Transaction::Tag &tag);
103 
104  void unprovideTag(const Transaction::Tag &tag);
105 
106  void commitRequiredTags(const std::shared_ptr<Transaction> &tx);
107 
108  void commitProvidedTags(const std::shared_ptr<Transaction> &tx);
109 
110  void rollbackRequiredTags(const std::shared_ptr<Transaction> &tx);
111 
112  void rollbackProvidedTags(const std::shared_ptr<Transaction> &tx);
113 
114  bool checkForReady(const std::shared_ptr<const Transaction> &tx) const;
115 
116  void setReady(const std::shared_ptr<Transaction> &tx);
117 
118  void unsetReady(const std::shared_ptr<Transaction> &tx);
119 
120  bool isInReady(const std::shared_ptr<const Transaction> &tx) const;
121 
122  std::shared_ptr<blockchain::BlockHeaderRepository> header_repo_;
123 
124  log::Logger logger_ = log::createLogger("TransactionPool", "transactions");
125 
126  std::shared_ptr<primitives::events::ExtrinsicSubscriptionEngine>
128  std::shared_ptr<subscription::ExtrinsicEventKeyRepository> ext_key_repo_;
129 
130  std::shared_ptr<runtime::TaggedTransactionQueue> ttq_;
131  std::shared_ptr<crypto::Hasher> hasher_;
132  std::shared_ptr<network::TransactionsTransmitter> tx_transmitter_;
133 
135  std::unique_ptr<PoolModerator> moderator_;
136 
138  std::unordered_map<Transaction::Hash, std::shared_ptr<Transaction>>
140 
142  std::unordered_map<Transaction::Hash, std::weak_ptr<Transaction>>
144 
146  std::list<std::weak_ptr<Transaction>> postponed_txs_;
147 
149  std::multimap<Transaction::Tag, std::weak_ptr<Transaction>>
151 
153  std::multimap<Transaction::Tag, std::weak_ptr<Transaction>>
155 
157  std::multimap<Transaction::Tag, std::weak_ptr<Transaction>> tx_waits_tag_;
158 
160 
161  // Metrics
164  };
165 
166 } // namespace kagome::transaction_pool
167 
168 #endif // KAGOME_TRANSACTION_POOL_IMPL_HPP
std::shared_ptr< blockchain::BlockHeaderRepository > header_repo_
std::unique_ptr< PoolModerator > moderator_
bans stale and invalid transactions for some amount of time
common::Hash256 Hash
std::unique_ptr< Registry > RegistryPtr
Definition: metrics.hpp:15
RegistryPtr createRegistry()
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
std::shared_ptr< network::TransactionsTransmitter > tx_transmitter_
std::list< std::weak_ptr< Transaction > > postponed_txs_
List of ready transaction over limit. It will be process first of all.
std::multimap< Transaction::Tag, std::weak_ptr< Transaction > > tx_provides_tag_
Transactions which provides specific tags.
std::unordered_map< Transaction::Hash, std::shared_ptr< Transaction > > imported_txs_
All of imported transaction, contained in the pool.
std::multimap< Transaction::Tag, std::weak_ptr< Transaction > > tx_depends_on_tag_
Transactions with resolved requirement of a specific tag.
boost::variant< BlockHash, BlockNumber > BlockId
Block id is the variant over BlockHash and BlockNumber.
Definition: block_id.hpp:18
std::shared_ptr< runtime::TaggedTransactionQueue > ttq_
std::shared_ptr< subscription::ExtrinsicEventKeyRepository > ext_key_repo_
std::shared_ptr< primitives::events::ExtrinsicSubscriptionEngine > sub_engine_
Extrinsic class represents extrinsic.
Definition: extrinsic.hpp:24
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
std::unordered_map< Transaction::Hash, std::weak_ptr< Transaction > > ready_txs_
Collection transaction with full-satisfied dependencies.
A gauge metric to represent a value that can arbitrarily go up and down.
Definition: metrics.hpp:49
std::multimap< Transaction::Tag, std::weak_ptr< Transaction > > tx_waits_tag_
Transactions with unresolved require of specific tags.