Kagome
Polkadot Runtime Engine in C++17
block_builder_impl.cpp
Go to the documentation of this file.
1 
7 
9 #include "common/visitor.hpp"
11 
12 namespace kagome::authorship {
13 
16  using primitives::events::InBlockEventParams;
17 
19  primitives::BlockHeader block_header,
20  const storage::trie::RootHash &storage_state,
21  std::shared_ptr<runtime::BlockBuilder> block_builder_api)
22  : block_header_{std::move(block_header)},
23  block_builder_api_{std::move(block_builder_api)},
24  storage_state_{std::move(storage_state)},
25  logger_{log::createLogger("BlockBuilder", "authorship")} {
26  BOOST_ASSERT(block_builder_api_ != nullptr);
27  }
28 
29  outcome::result<std::vector<primitives::Extrinsic>>
31  const primitives::InherentData &data) const {
32  return block_builder_api_->inherent_extrinsics(
35  data);
36  }
37 
38  outcome::result<primitives::ExtrinsicIndex> BlockBuilderImpl::pushExtrinsic(
39  const primitives::Extrinsic &extrinsic) {
40  auto apply_res = block_builder_api_->apply_extrinsic(
43  extrinsic);
44  if (not apply_res) {
45  // Takes place when API method execution fails for some technical kind of
46  // problem. This WON'T be executed when apply_extrinsic returned an error
47  // regarding the business-logic part.
48  logger_->warn(
49  "Extrinsic {} was not pushed to block. Error during xt application: "
50  "{}",
51  extrinsic.data.toHex().substr(0, 8),
52  apply_res.error().message());
53  return apply_res.error();
54  }
55  storage_state_ = apply_res.value().new_storage_root;
56 
57  using return_type = outcome::result<primitives::ExtrinsicIndex>;
58  return visit_in_place(
59  apply_res.value().result,
60  [this, &extrinsic](
61  const primitives::DispatchOutcome &outcome) -> return_type {
62  if (1 == outcome.which()) { // DispatchError
63  SL_WARN(
64  logger_,
65  "Extrinsic {} pushed to the block, but dispatch error occurred",
66  extrinsic.data.toHex().substr(0, 8));
67  }
68  extrinsics_.push_back(extrinsic);
69  return extrinsics_.size() - 1;
70  },
71  [this, &extrinsic](const primitives::TransactionValidityError &tx_error)
72  -> return_type {
73  return visit_in_place(
74  tx_error,
75  [this, &extrinsic](
76  const primitives::InvalidTransaction &reason) -> return_type {
77  switch (reason) {
82  default:
83  SL_WARN(
84  logger_,
85  "Extrinsic {} cannot be applied and was not pushed to "
86  "the block. (InvalidTransaction response, code {})",
87  extrinsic.data.toHex().substr(0, 8),
88  static_cast<uint8_t>(reason));
90  }
91  },
92  [this, &extrinsic](
93  const primitives::UnknownTransaction &reason) -> return_type {
94  SL_WARN(logger_,
95  "Extrinsic {} cannot be applied and was not pushed to "
96  "the block. (UnknownTransaction response, code {})",
97  extrinsic.data.toHex().substr(0, 8),
98  static_cast<uint8_t>(reason));
100  });
101  });
102  }
103 
104  outcome::result<primitives::Block> BlockBuilderImpl::bake() const {
105  OUTCOME_TRY(finalized_header,
106  block_builder_api_->finalize_block(
107  {block_header_.number - 1, block_header_.parent_hash},
108  storage_state_));
109  return primitives::Block{finalized_header, extrinsics_};
110  }
111 
113  scale::ScaleEncoderStream s(true);
114  for (const auto &xt : extrinsics_) {
115  s << xt;
116  }
117  return estimatedBlockHeaderSize() + s.size();
118  }
119 
121  static std::optional<size_t> size = std::nullopt;
122  if (not size) {
123  scale::ScaleEncoderStream s(true);
124  s << block_header_;
125  size = s.size();
126  }
127  return size.value();
128  }
129 } // namespace kagome::authorship
std::shared_ptr< runtime::BlockBuilder > block_builder_api_
Block class represents polkadot block primitive.
Definition: block.hpp:19
subscription::SubscriptionEngine< SubscribedExtrinsicId, std::shared_ptr< api::Session >, primitives::events::ExtrinsicLifecycleEvent > ExtrinsicSubscriptionEngine
boost::variant< InvalidTransaction, UnknownTransaction > TransactionValidityError
boost::variant< DispatchSuccess, DispatchError > DispatchOutcome
outcome::result< primitives::Block > bake() const override
common::Buffer data
extrinsic content as byte array
Definition: extrinsic.hpp:27
UnknownTransaction
An unknown transaction validity.
outcome::result< std::vector< primitives::Extrinsic > > getInherentExtrinsics(const primitives::InherentData &data) const override
BlockBuilderImpl(primitives::BlockHeader block_header, const storage::trie::RootHash &storage_state, std::shared_ptr< runtime::BlockBuilder > block_builder_api)
std::string toHex() const
encode bytearray as hex
Definition: buffer.hpp:167
outcome::result< primitives::ExtrinsicIndex > pushExtrinsic(const primitives::Extrinsic &extrinsic) override
std::vector< primitives::Extrinsic > extrinsics_
BlockNumber number
index of the block in the chain
Extrinsic class represents extrinsic.
Definition: extrinsic.hpp:24
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
BlockHash parent_hash
32-byte Blake2s hash of parent header
InvalidTransaction
Transaction is invalid. Details are described by the error code.