Kagome
Polkadot Runtime Engine in C++17
core.cpp
Go to the documentation of this file.
1 
7 
9 #include "log/logger.hpp"
11 
12 namespace kagome::runtime {
13 
15  std::shared_ptr<Executor> executor,
16  std::shared_ptr<storage::changes_trie::ChangesTracker> changes_tracker,
17  std::shared_ptr<const blockchain::BlockHeaderRepository> header_repo)
18  : executor_{std::move(executor)},
19  changes_tracker_{std::move(changes_tracker)},
20  header_repo_{std::move(header_repo)} {
21  BOOST_ASSERT(executor_ != nullptr);
22  BOOST_ASSERT(changes_tracker_ != nullptr);
23  BOOST_ASSERT(header_repo_ != nullptr);
24  }
25 
26  outcome::result<primitives::Version> CoreImpl::version(
27  const primitives::BlockHash &block) {
28  return executor_->callAt<primitives::Version>(block, "Core_version");
29  }
30 
31  outcome::result<primitives::Version> CoreImpl::version() {
32  return executor_->callAtGenesis<primitives::Version>("Core_version");
33  }
34 
35  outcome::result<void> CoreImpl::execute_block(
36  const primitives::Block &block) {
37  BOOST_ASSERT([&] {
38  auto parent_res = header_repo_->getBlockHeader(block.header.parent_hash);
39  return parent_res.has_value()
40  and parent_res.value().number == block.header.number - 1;
41  }());
42  changes_tracker_->onBlockExecutionStart(block.header.parent_hash);
43  OUTCOME_TRY(executor_->persistentCallAt<void>(
44  block.header.parent_hash, "Core_execute_block", block));
45  return outcome::success();
46  }
47 
48  outcome::result<storage::trie::RootHash> CoreImpl::initialize_block(
49  const primitives::BlockHeader &header) {
50  changes_tracker_->onBlockExecutionStart(header.parent_hash);
51  const auto res = executor_->persistentCallAt<void>(
52  header.parent_hash, "Core_initialize_block", header);
53  if (res.has_value()) {
54  return res.value().new_storage_root;
55  }
56  return res.error();
57  }
58 
59 } // namespace kagome::runtime
Block class represents polkadot block primitive.
Definition: block.hpp:19
std::shared_ptr< storage::changes_trie::ChangesTracker > changes_tracker_
Definition: core.hpp:40
std::shared_ptr< Executor > executor_
Definition: core.hpp:39
outcome::result< primitives::Version > version() override
Returns the version of the runtime - version for nested calls, such as in MiscExtension.
Definition: core.cpp:31
outcome::result< storage::trie::RootHash > initialize_block(const primitives::BlockHeader &header) override
Initialize a block with the given header.
Definition: core.cpp:48
CoreImpl(std::shared_ptr< Executor > executor, std::shared_ptr< storage::changes_trie::ChangesTracker > changes_tracker, std::shared_ptr< const blockchain::BlockHeaderRepository > header_repo)
Definition: core.cpp:14
BlockNumber number
index of the block in the chain
BlockHeader header
block header
Definition: block.hpp:22
outcome::result< void > execute_block(const primitives::Block &block) override
Executes the given block.
Definition: core.cpp:35
BlockHash parent_hash
32-byte Blake2s hash of parent header
std::shared_ptr< const blockchain::BlockHeaderRepository > header_repo_
Definition: core.hpp:41