Skip to content

Commit

Permalink
warp sync last justification (#2324)
Browse files Browse the repository at this point in the history
Signed-off-by: turuslan <[email protected]>
Co-authored-by: kamilsa <[email protected]>
  • Loading branch information
turuslan and kamilsa authored Dec 24, 2024
1 parent 26a52d5 commit 39f55a2
Showing 1 changed file with 23 additions and 31 deletions.
54 changes: 23 additions & 31 deletions core/network/impl/synchronizer_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "network/protocols/state_protocol.hpp"
#include "network/protocols/sync_protocol.hpp"
#include "network/types/block_attributes.hpp"
#include "network/warp/protocol.hpp"
#include "primitives/common.hpp"
#include "storage/predefined_keys.hpp"
#include "storage/trie/serialization/trie_serializer.hpp"
Expand Down Expand Up @@ -1326,20 +1327,18 @@ namespace kagome::network {

bool SynchronizerImpl::fetchJustificationRange(primitives::BlockNumber min,
FetchJustificationRangeCb cb) {
BlocksRequest request{
.fields = BlockAttribute::JUSTIFICATION,
.from = min,
.direction = Direction::ASCENDING,
.max = std::nullopt,
.multiple_justifications = false,
};
auto chosen = chooseJustificationPeer(min, request.fingerprint());
auto hash_res = block_tree_->getHashByNumber(min);
if (not hash_res) {
return false;
}
auto &hash = hash_res.value();
auto chosen = chooseJustificationPeer(min, min);
if (not chosen) {
return false;
}
busy_peers_.emplace(*chosen);
auto cb2 = [weak{weak_from_this()}, min, cb{std::move(cb)}, peer{*chosen}](
outcome::result<BlocksResponse> r) mutable {
outcome::result<WarpResponse> r) mutable {
auto self = weak.lock();
if (not self) {
return;
Expand All @@ -1348,30 +1347,23 @@ namespace kagome::network {
if (not r) {
return cb(r.error());
}
auto &blocks = r.value().blocks;
if (blocks.empty()) {
return cb(Error::EMPTY_RESPONSE);
}
auto number = min;
for (auto &block : blocks) {
if (block.justification) {
self->grandpa_environment_->applyJustification(
{number, block.hash},
*block.justification,
[cb{std::move(cb)}](outcome::result<void> r) {
if (not r) {
cb(r.error());
} else {
cb(std::nullopt);
}
});
return;
}
++number;
auto &blocks = r.value().proofs;
for (const auto &block : blocks) {
self->grandpa_environment_->applyJustification(
block.justification.block_info,
{scale::encode(block.justification).value()},
[cb{std::move(cb)}](outcome::result<void> r) {
if (not r) {
cb(r.error());
} else {
cb(std::nullopt);
}
});
return;
}
cb(min + blocks.size());
cb(min);
};
fetch(*chosen, std::move(request), "justification range", std::move(cb2));
router_->getWarpProtocol()->doRequest(*chosen, hash, std::move(cb2));
return true;
}

Expand Down

0 comments on commit 39f55a2

Please sign in to comment.