Kagome
Polkadot Runtime Engine in C++17
consistency_keeper_impl.cpp
Go to the documentation of this file.
1 
7 
8 #include <boost/assert.hpp>
9 #include <scale/scale.hpp>
10 
15 
16 namespace kagome::consensus::babe {
17 
19  std::shared_ptr<application::AppStateManager> app_state_manager,
20  std::shared_ptr<storage::BufferStorage> storage,
21  std::shared_ptr<blockchain::BlockTree> block_tree,
22  std::shared_ptr<blockchain::DigestTracker> digest_tracker)
23  : app_state_manager_(std::move(app_state_manager)),
24  storage_(std::move(storage)),
25  block_tree_(std::move(block_tree)),
26  digest_tracker_(std::move(digest_tracker)),
27  logger_{log::createLogger("ConsistencyKeeper", "block_executor")} {
28  BOOST_ASSERT(app_state_manager_ != nullptr);
29  BOOST_ASSERT(storage_ != nullptr);
30  BOOST_ASSERT(block_tree_ != nullptr);
31  BOOST_ASSERT(digest_tracker_ != nullptr);
32 
33  app_state_manager_->atPrepare([&] { return prepare(); });
34  }
35 
37  // try to get record
38  auto buf_opt_res = storage_->tryLoad(storage::kApplyingBlockInfoLookupKey);
39  if (buf_opt_res.has_error()) {
40  SL_WARN(logger_,
41  "Can't check existence of partial applied block",
42  buf_opt_res.error().message());
43  return false;
44  }
45 
46  // check if record exists
47  auto buf_opt = buf_opt_res.value();
48  if (not buf_opt.has_value()) {
49  return true;
50  }
51 
52  // decode obtained record
53  auto block_res = scale::decode<primitives::BlockInfo>(buf_opt.value());
54  if (block_res.has_value()) {
55  auto &block = block_res.value();
56 
57  SL_WARN(logger_,
58  "Found partial applied block {}. Trying to rollback it",
59  block);
60 
61  rollback(block);
62  return true;
63  }
64 
65  SL_WARN(logger_,
66  "Can't check existence of partial applied block",
67  block_res.error().message());
68  return false;
69  }
70 
72  primitives::BlockInfo block) {
73  bool val = false;
74  BOOST_VERIFY_MSG(in_progress_.compare_exchange_strong(val, true),
75  "Allowed only one block applying in any time");
76 
77  // remove record from storage
79  common::Buffer(scale::encode(block).value()));
80 
81  if (put_res.has_error()) {
82  SL_WARN(logger_,
83  "Can't store record of partial applied block",
84  put_res.error().message());
85  }
86 
87  SL_DEBUG(logger_, "Start applying of block {}", block);
88  return ConsistencyKeeper::Guard(*this, block);
89  }
90 
92  cleanup();
93  SL_DEBUG(logger_, "Applying of block {} finished successfully", block);
94  }
95 
97  // Cancel tracked block digest
98  digest_tracker_->cancel(block);
99 
100  // Remove block as leaf of block tree
101  auto removal_res = block_tree_->removeLeaf(block.hash);
102  if (removal_res.has_error()) {
103  SL_WARN(logger_,
104  "Rolling back of block {} is failed: {}",
105  block,
106  removal_res.error().message());
107  }
108 
109  cleanup();
110  SL_DEBUG(logger_, "Applying of block {} was rolled back", block);
111  }
112 
114  // remove record from storage
115  auto removal_res = storage_->remove(storage::kApplyingBlockInfoLookupKey);
116  in_progress_ = false;
117 
118  if (removal_res.has_error()) {
119  SL_WARN(logger_,
120  "Can't remove record of partial applied block",
121  removal_res.error().message());
122  return;
123  }
124  }
125 
126 } // namespace kagome::consensus::babe
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
STL namespace.
void rollback(primitives::BlockInfo block) override
std::shared_ptr< application::AppStateManager > app_state_manager_
ConsistencyKeeperImpl(std::shared_ptr< application::AppStateManager > app_state_manager, std::shared_ptr< storage::BufferStorage > storage, std::shared_ptr< blockchain::BlockTree > block_tree, std::shared_ptr< blockchain::DigestTracker > digest_tracker)
Guard start(primitives::BlockInfo block) override
std::shared_ptr< blockchain::BlockTree > block_tree_
void commit(primitives::BlockInfo block) override
std::shared_ptr< storage::BufferStorage > storage_
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
std::shared_ptr< blockchain::DigestTracker > digest_tracker_
const common::Buffer kApplyingBlockInfoLookupKey