Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup/era points #2309

Merged
merged 7 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/blockchain/block_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ namespace kagome::blockchain {
* @return collection of the leaves
*/
virtual std::vector<primitives::BlockHash> getLeaves() const = 0;
virtual std::vector<primitives::BlockInfo> getLeavesInfo() const = 0;

/**
* Get children of the block with specified hash
Expand Down
29 changes: 17 additions & 12 deletions core/blockchain/impl/block_tree_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,18 +425,18 @@ namespace kagome::blockchain {
std::shared_ptr<storage::trie_pruner::TriePruner> state_pruner,
common::MainThreadPool &main_thread_pool)
: block_tree_data_{BlockTreeData{
.header_repo_ = std::move(header_repo),
.storage_ = std::move(storage),
.state_pruner_ = std::move(state_pruner),
.tree_ = std::make_unique<CachedTree>(finalized),
.extrinsic_observer_ = std::move(extrinsic_observer),
.hasher_ = std::move(hasher),
.extrinsic_event_key_repo_ = std::move(extrinsic_event_key_repo),
.justification_storage_policy_ =
std::move(justification_storage_policy),
.genesis_block_hash_ = {},
.blocks_pruning_ = {app_config.blocksPruning(), finalized.number},
}},
.header_repo_ = std::move(header_repo),
.storage_ = std::move(storage),
.state_pruner_ = std::move(state_pruner),
.tree_ = std::make_unique<CachedTree>(finalized),
.extrinsic_observer_ = std::move(extrinsic_observer),
.hasher_ = std::move(hasher),
.extrinsic_event_key_repo_ = std::move(extrinsic_event_key_repo),
.justification_storage_policy_ =
std::move(justification_storage_policy),
.genesis_block_hash_ = {},
.blocks_pruning_ = {app_config.blocksPruning(), finalized.number},
}},
chain_events_engine_{std::move(chain_events_engine)},
main_pool_handler_{main_thread_pool.handlerStarted()},
extrinsic_events_engine_{std::move(extrinsic_events_engine)} {
Expand Down Expand Up @@ -1252,6 +1252,11 @@ namespace kagome::blockchain {
[&](const BlockTreeData &p) { return getLeavesNoLock(p); });
}

std::vector<primitives::BlockInfo> BlockTreeImpl::getLeavesInfo() const {
return block_tree_data_.sharedAccess(
[&](const BlockTreeData &p) { return p.tree_->leafInfo(); });
}

BlockTreeImpl::BlockHashVecRes BlockTreeImpl::getChildren(
const primitives::BlockHash &block) const {
return block_tree_data_.sharedAccess([&](const BlockTreeData &p)
Expand Down
1 change: 1 addition & 0 deletions core/blockchain/impl/block_tree_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ namespace kagome::blockchain {
const primitives::BlockHash &target_hash) const override;

std::vector<primitives::BlockHash> getLeaves() const override;
std::vector<primitives::BlockInfo> getLeavesInfo() const override;

BlockHashVecRes getChildren(
const primitives::BlockHash &block) const override;
Expand Down
30 changes: 23 additions & 7 deletions core/blockchain/impl/cached_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace kagome::blockchain {
void CachedTree::forceRefreshBest() {
std::set<std::shared_ptr<TreeNode>, Cmp> candidates;
for (auto &leaf : leaves_) {
auto node = find(leaf);
auto node = find(leaf.first);
BOOST_ASSERT(node);
candidates.emplace(std::move(node));
}
Expand All @@ -125,8 +125,9 @@ namespace kagome::blockchain {
CachedTree::CachedTree(const primitives::BlockInfo &root)
: root_{std::make_shared<TreeNode>(root)},
best_{root_},
nodes_{{root.hash, root_}},
leaves_{root.hash} {}
nodes_{{root.hash, root_}} {
leaves_.emplace(root.hash, root.number);
}

primitives::BlockInfo CachedTree::finalized() const {
return root_->info;
Expand All @@ -140,8 +141,23 @@ namespace kagome::blockchain {
return leaves_.size();
}

std::vector<primitives::BlockInfo> CachedTree::leafInfo() const {
std::vector<primitives::BlockInfo> output;
output.reserve(leaves_.size());
std::ranges::transform(
leaves_, std::back_inserter(output), [](const auto &v) {
return primitives::BlockInfo(v.first, v.second);
});
return output;
}

std::vector<primitives::BlockHash> CachedTree::leafHashes() const {
return {leaves_.begin(), leaves_.end()};
std::vector<primitives::BlockHash> output;
output.reserve(leaves_.size());
std::ranges::transform(leaves_,
std::back_inserter(output),
[](const auto &v) { return v.first; });
return output;
}

bool CachedTree::isLeaf(const primitives::BlockHash &hash) const {
Expand All @@ -151,7 +167,7 @@ namespace kagome::blockchain {
primitives::BlockInfo CachedTree::bestWith(
const std::shared_ptr<TreeNode> &required) const {
std::set<std::shared_ptr<TreeNode>, Cmp> candidates;
for (auto &leaf : leaves_) {
for (auto &[leaf, _] : leaves_) {
auto node = find(leaf);
BOOST_ASSERT(node);
candidates.emplace(std::move(node));
Expand Down Expand Up @@ -197,7 +213,7 @@ namespace kagome::blockchain {
parent->children.emplace_back(new_node);
nodes_.emplace(new_node->info.hash, new_node);
leaves_.erase(parent->info.hash);
leaves_.emplace(new_node->info.hash);
leaves_.emplace(new_node->info.hash, new_node->info.number);
if (not new_node->reverted and new_node->weight() > best_->weight()) {
auto old_best = best_;
best_ = new_node;
Expand Down Expand Up @@ -279,7 +295,7 @@ namespace kagome::blockchain {
changes.prune.emplace_back(node->info);
parent->children.erase(child_it);
if (parent->children.empty()) {
leaves_.emplace(parent->info.hash);
leaves_.emplace(parent->info.hash, parent->info.number);
}
leaves_.erase(leaf_it);
if (node == best_) {
Expand Down
3 changes: 2 additions & 1 deletion core/blockchain/impl/cached_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ namespace kagome::blockchain {
primitives::BlockInfo best() const;
size_t leafCount() const;
std::vector<primitives::BlockHash> leafHashes() const;
std::vector<primitives::BlockInfo> leafInfo() const;
bool isLeaf(const primitives::BlockHash &hash) const;
primitives::BlockInfo bestWith(
const std::shared_ptr<TreeNode> &required) const;
Expand Down Expand Up @@ -106,6 +107,6 @@ namespace kagome::blockchain {
std::shared_ptr<TreeNode> root_;
std::shared_ptr<TreeNode> best_;
std::unordered_map<primitives::BlockHash, std::shared_ptr<TreeNode>> nodes_;
std::unordered_set<primitives::BlockHash> leaves_;
std::unordered_map<primitives::BlockHash, primitives::BlockNumber> leaves_;
};
} // namespace kagome::blockchain
13 changes: 11 additions & 2 deletions core/network/impl/peer_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@

namespace kagome::network {
inline View makeView(const LazySPtr<blockchain::BlockTree> &block_tree) {
auto last_finalized = block_tree.get()->getLastFinalized().number;

std::vector<primitives::BlockHash> heads;
for (const auto &bi : block_tree.get()->getLeavesInfo()) {
if (bi.number >= last_finalized) {
heads.emplace_back(bi.hash);
}
}

View view{
.heads_ = block_tree.get()->getLeaves(),
.finalized_number_ = block_tree.get()->getLastFinalized().number,
.heads_ = std::move(heads),
.finalized_number_ = last_finalized,
};
std::ranges::sort(view.heads_);
return view;
Expand Down
1 change: 1 addition & 0 deletions core/parachain/availability/recovery/recovery_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ namespace kagome::parachain {
candidate_hash,
validator_index,
peer->id);
lock.unlock();
send_fetch_available_data_request(
peer->id,
candidate_hash,
Expand Down
4 changes: 2 additions & 2 deletions core/parachain/validator/impl/parachain_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,12 +722,12 @@ namespace kagome::parachain {
auto rps_result = construct_per_relay_parent_state(maybe_new, mode_);
if (rps_result.has_value()) {
our_current_state_.state_by_relay_parent.insert_or_assign(
relay_parent, std::move(rps_result.value()));
maybe_new, std::move(rps_result.value()));
} else if (rps_result.error() != Error::KEY_NOT_PRESENT) {
SL_TRACE(
logger_,
"Relay parent state was not created. (relay parent={}, error={})",
relay_parent,
maybe_new,
rps_result.error());
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/mock/core/blockchain/block_tree_mock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ namespace kagome::blockchain {

MOCK_METHOD(primitives::BlockInfo, bestBlock, (), (const, override));

MOCK_METHOD(std::vector<primitives::BlockInfo>,
getLeavesInfo,
(),
(const, override));

MOCK_METHOD(std::vector<primitives::BlockHash>,
getLeaves,
(),
Expand Down
Loading