Kagome
Polkadot Runtime Engine in C++17
system_api_impl.cpp
Go to the documentation of this file.
1 
7 
8 #include <queue>
9 
10 #include <jsonrpc-lean/request.h>
11 
14 #include "scale/scale.hpp"
16 
17 namespace kagome::api {
18 
20  std::shared_ptr<application::ChainSpec> config,
21  std::shared_ptr<consensus::babe::Babe> babe,
22  std::shared_ptr<network::PeerManager> peer_manager,
23  std::shared_ptr<runtime::AccountNonceApi> account_nonce_api,
24  std::shared_ptr<transaction_pool::TransactionPool> transaction_pool,
25  std::shared_ptr<const blockchain::BlockTree> block_tree,
26  std::shared_ptr<crypto::Hasher> hasher)
27  : config_(std::move(config)),
28  babe_(std::move(babe)),
29  peer_manager_(std::move(peer_manager)),
30  account_nonce_api_(std::move(account_nonce_api)),
31  transaction_pool_(std::move(transaction_pool)),
32  block_tree_(std::move(block_tree)),
33  hasher_{std::move(hasher)} {
34  BOOST_ASSERT(config_ != nullptr);
35  BOOST_ASSERT(babe_ != nullptr);
36  BOOST_ASSERT(peer_manager_ != nullptr);
37  BOOST_ASSERT(account_nonce_api_ != nullptr);
38  BOOST_ASSERT(transaction_pool_ != nullptr);
39  BOOST_ASSERT(hasher_ != nullptr);
40  BOOST_ASSERT(block_tree_ != nullptr);
41  }
42 
43  std::shared_ptr<application::ChainSpec> SystemApiImpl::getConfig() const {
44  return config_;
45  }
46 
47  std::shared_ptr<consensus::babe::Babe> SystemApiImpl::getBabe() const {
48  return babe_;
49  }
50 
51  std::shared_ptr<network::PeerManager> SystemApiImpl::getPeerManager() const {
52  return peer_manager_;
53  }
54 
55  outcome::result<primitives::AccountNonce> SystemApiImpl::getNonceFor(
56  std::string_view account_address) const {
57  OUTCOME_TRY(account_id, primitives::decodeSs58(account_address, *hasher_));
58  OUTCOME_TRY(nonce,
59  account_nonce_api_->account_nonce(
60  block_tree_->deepestLeaf().hash, account_id));
61 
62  return adjustNonce(account_id, nonce);
63  }
64 
66  const primitives::AccountId &account_id,
67  primitives::AccountNonce current_nonce) const {
68  auto txs = transaction_pool_->getReadyTransactions();
69 
70  // txs authored by the provided account sorted by nonce
71  std::map<primitives::AccountNonce,
72  std::reference_wrapper<const primitives::Transaction>>
73  sorted_txs;
74 
75  for (auto &[tx_hash, tx_ptr] : txs) {
76  if (tx_ptr->provides.empty()) continue;
77 
78  // the assumption that the tag with nonce is encoded this way is taken
79  // from substrate
80  auto tag_decode_res = scale::decode<
81  std::tuple<primitives::AccountId, primitives::AccountNonce>>(
82  tx_ptr->provides.at(0)); // substrate assumes that the tag with
83  // nonce is the first one
84 
85  if (tag_decode_res.has_value()) {
86  auto &&[id, nonce] = std::move(tag_decode_res.value());
87  if (id == account_id) {
88  sorted_txs.emplace(nonce, std::ref(*tx_ptr));
89  }
90  }
91  }
92  // ensure that we consider only continuously growing nonce
93  for (auto &&[tx_nonce, tx] : sorted_txs) {
94  if (tx_nonce == current_nonce) {
95  current_nonce++;
96  } else {
97  break;
98  }
99  }
100 
101  return current_nonce;
102  }
103 
104 } // namespace kagome::api
std::shared_ptr< runtime::AccountNonceApi > account_nonce_api_
std::shared_ptr< transaction_pool::TransactionPool > transaction_pool_
uint32_t AccountNonce
Definition: account.hpp:15
outcome::result< primitives::AccountNonce > getNonceFor(std::string_view account_address) const override
STL namespace.
std::shared_ptr< crypto::Hasher > hasher_
std::shared_ptr< network::PeerManager > getPeerManager() const override
std::shared_ptr< consensus::babe::Babe > babe_
primitives::AccountNonce adjustNonce(const primitives::AccountId &account_id, primitives::AccountNonce current_nonce) const
std::shared_ptr< const blockchain::BlockTree > block_tree_
outcome::result< AccountId > decodeSs58(std::string_view account_address, const crypto::Hasher &hasher) noexcept
Definition: ss58_codec.cpp:37
std::shared_ptr< application::ChainSpec > getConfig() const override
crypto::Sr25519PublicKey AccountId
Definition: account.hpp:13
std::shared_ptr< network::PeerManager > peer_manager_
std::shared_ptr< application::ChainSpec > config_
std::shared_ptr< consensus::babe::Babe > getBabe() const override
SystemApiImpl(std::shared_ptr< application::ChainSpec > config, std::shared_ptr< consensus::babe::Babe > babe, std::shared_ptr< network::PeerManager > peer_manager, std::shared_ptr< runtime::AccountNonceApi > account_nonce_api, std::shared_ptr< transaction_pool::TransactionPool > transaction_pool, std::shared_ptr< const blockchain::BlockTree > block_tree, std::shared_ptr< crypto::Hasher > hasher)