8 #include <boost/assert.hpp> 20 case E::DUPLICATE_REQUEST_ID:
21 return "Request with a same id is handling right now";
23 return "unknown error";
29 std::shared_ptr<blockchain::BlockTree> block_tree,
30 std::shared_ptr<blockchain::BlockHeaderRepository> blocks_headers)
31 : block_tree_{std::move(block_tree)},
38 outcome::result<network::BlocksResponse>
50 if (not from_hash_res.has_value()) {
51 log_->warn(
"cannot find a requested block with id {}", request.
from);
55 const auto &from_hash = from_hash_res.value();
59 if (not chain_hash_res.has_value()) {
60 log_->warn(
"cannot retrieve a chain of blocks: {}",
61 chain_hash_res.error().message());
65 const auto &chain_hash = chain_hash_res.value();
69 if (response.
blocks.empty()) {
70 SL_DEBUG(
log_,
"Return response id={}: no blocks", request_id);
71 }
else if (response.
blocks.size() == 1) {
72 if (response.
blocks.front().header.has_value()) {
74 "Return response id={}: {}, count 1",
77 response.
blocks.front().hash));
80 "Return response id={}: {}, count 1",
82 response.
blocks.front().hash);
85 if (response.
blocks.front().header.has_value()
86 and response.
blocks.back().header.has_value()) {
88 "Return response id={}: from {} to {}, count {}",
91 response.
blocks.front().hash),
93 response.
blocks.back().hash),
97 "Return response id={}: from {} to {}, count {}",
99 response.
blocks.front().hash,
100 response.
blocks.back().hash,
118 uint32_t request_count =
120 if (request.
max.has_value()) {
121 request_count = std::clamp(
133 block_tree_->getBestChainFromBlock(from_hash, request_count));
134 return std::move(chain_hash);
139 block_tree_->getDescendingChainToBlock(from_hash, request_count));
140 return std::move(chain_hash);
143 BOOST_UNREACHABLE_RETURN({});
150 const std::vector<primitives::BlockHash> &hash_chain)
const {
154 auto justification_needed =
157 for (
const auto &hash : hash_chain) {
164 new_block.header = std::move(header_res.value());
166 response.
blocks.pop_back();
173 new_block.body = std::move(body_res.value());
175 response.
blocks.pop_back();
179 if (justification_needed) {
180 auto justification_res =
block_tree_->getBlockJustification(hash);
181 if (justification_res) {
182 new_block.justification = std::move(justification_res.value());
blockchain::BlockTree::BlockHashVecRes retrieveRequestedHashes(const network::BlocksRequest &request, const primitives::BlockHash &from_hash) const
Include a justification for the block.
OUTCOME_CPP_DEFINE_CATEGORY(kagome::network, SyncProtocolObserverImpl::Error, e)
bool attributeIsSet(const BlockAttribute &attribute) const
std::shared_ptr< blockchain::BlockTree > block_tree_
static constexpr uint32_t kAbsolutMinBlocksInResponse
static constexpr uint32_t kAbsolutMaxBlocksInResponse
outcome::result< std::vector< primitives::BlockHash >> BlockHashVecRes
primitives::BlockId from
start from this block
outcome::result< BlocksResponse > onBlocksRequest(const BlocksRequest &request) const override
SyncProtocolObserverImpl(std::shared_ptr< blockchain::BlockTree > block_tree, std::shared_ptr< blockchain::BlockHeaderRepository > blocks_headers)
std::unordered_set< BlocksRequest::Fingerprint > requested_ids_
Logger createLogger(const std::string &tag)
Fingerprint fingerprint() const
std::optional< uint32_t > max
void fillBlocksResponse(const network::BlocksRequest &request, network::BlocksResponse &response, const std::vector< primitives::BlockHash > &hash_chain) const
Direction direction
sequence direction
common::SLVector< primitives::BlockData, kMaxBlocksInResponse > blocks
std::shared_ptr< blockchain::BlockHeaderRepository > blocks_headers_