Skip to content

Commit

Permalink
Merge pull request #2361 from AntelopeIO/gh_2333
Browse files Browse the repository at this point in the history
IF: Create new efficient incremental Merkle tree
  • Loading branch information
greg7mdp authored Apr 2, 2024
2 parents f44409b + 82178a7 commit 8c03a62
Show file tree
Hide file tree
Showing 17 changed files with 673 additions and 516 deletions.
1 change: 0 additions & 1 deletion libraries/chain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ target_include_directories(eosio_rapidjson INTERFACE ../rapidjson/include)

## SORT .cpp by most likely to change / break compile
add_library( eosio_chain
merkle.cpp
name.cpp
transaction.cpp
block.cpp
Expand Down
4 changes: 2 additions & 2 deletions libraries/chain/block_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ block_state_ptr block_state::create_if_genesis_block(const block_state_legacy& b
result.valid_qc = {}; // best qc received from the network inside block extension, empty until first savanna proper IF block

// Calculate Merkle tree root in Savanna way so that it is stored in Leaf Node when building block_state.
auto digests = *bsp.action_receipt_digests_savanna;
auto action_mroot_svnn = calculate_merkle(std::move(digests));
const auto& digests = *bsp.action_receipt_digests_savanna;
auto action_mroot_svnn = calculate_merkle(digests);

// build leaf_node and validation_tree
valid_t::finality_leaf_node_t leaf_node {
Expand Down
31 changes: 15 additions & 16 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,13 +674,13 @@ struct building_block {
auto [transaction_mroot, action_mroot] = std::visit(
overloaded{[&](digests_t& trx_receipts) { // calculate the two merkle roots in separate threads
auto trx_merkle_fut =
post_async_task(ioc, [&]() { return legacy_merkle(std::move(trx_receipts)); });
post_async_task(ioc, [&]() { return calculate_merkle_legacy(std::move(trx_receipts)); });
auto action_merkle_fut =
post_async_task(ioc, [&]() { return legacy_merkle(std::move(*action_receipts.digests_l)); });
post_async_task(ioc, [&]() { return calculate_merkle_legacy(std::move(*action_receipts.digests_l)); });
return std::make_pair(trx_merkle_fut.get(), action_merkle_fut.get());
},
[&](const checksum256_type& trx_checksum) {
return std::make_pair(trx_checksum, legacy_merkle(std::move(*action_receipts.digests_l)));
return std::make_pair(trx_checksum, calculate_merkle_legacy(std::move(*action_receipts.digests_l)));
}},
trx_mroot_or_receipt_digests());

Expand All @@ -706,15 +706,14 @@ struct building_block {
[&](building_block_if& bb) -> assembled_block {
// compute the action_mroot and transaction_mroot
auto [transaction_mroot, action_mroot] = std::visit(
overloaded{[&](digests_t& trx_receipts) { // calculate the two merkle roots in separate threads
auto trx_merkle_fut =
post_async_task(ioc, [&]() { return calculate_merkle(std::move(trx_receipts)); });
auto action_merkle_fut =
post_async_task(ioc, [&]() { return calculate_merkle(std::move(*action_receipts.digests_s)); });
return std::make_pair(trx_merkle_fut.get(), action_merkle_fut.get());
overloaded{[&](digests_t& trx_receipts) {
// calculate_merkle takes 3.2ms for 50,000 digests (legacy version took 11.1ms)
return std::make_pair(calculate_merkle(trx_receipts),
calculate_merkle(*action_receipts.digests_s));
},
[&](const checksum256_type& trx_checksum) {
return std::make_pair(trx_checksum, calculate_merkle(std::move(*action_receipts.digests_s)));
return std::make_pair(trx_checksum,
calculate_merkle(*action_receipts.digests_s));
}},
trx_mroot_or_receipt_digests());

Expand Down Expand Up @@ -1308,8 +1307,8 @@ struct controller_impl {
// IRREVERSIBLE applies (validates) blocks when irreversible, new_valid will be done after apply in log_irreversible
assert(read_mode == db_read_mode::IRREVERSIBLE || legacy->action_receipt_digests_savanna);
if (legacy->action_receipt_digests_savanna) {
auto digests = *legacy->action_receipt_digests_savanna;
auto action_mroot = calculate_merkle(std::move(digests));
const auto& digests = *legacy->action_receipt_digests_savanna;
auto action_mroot = calculate_merkle(digests);
// Create the valid structure for producing
new_bsp->valid = prev->new_valid(*new_bsp, action_mroot, new_bsp->strong_digest);
}
Expand Down Expand Up @@ -1522,8 +1521,8 @@ struct controller_impl {
validator_t{}, skip_validate_signee);
// legacy_branch is from head, all should be validated
assert(bspl->action_receipt_digests_savanna);
auto digests = *bspl->action_receipt_digests_savanna;
auto action_mroot = calculate_merkle(std::move(digests));
const auto& digests = *bspl->action_receipt_digests_savanna;
auto action_mroot = calculate_merkle(digests);
// Create the valid structure for producing
new_bsp->valid = prev->new_valid(*new_bsp, action_mroot, new_bsp->strong_digest);
prev = new_bsp;
Expand Down Expand Up @@ -4066,9 +4065,9 @@ struct controller_impl {
// @param if_active true if instant finality is active
static checksum256_type calc_merkle( deque<digest_type>&& digests, bool if_active ) {
if (if_active) {
return calculate_merkle( std::move(digests) );
return calculate_merkle( digests );
} else {
return legacy_merkle( std::move(digests) );
return calculate_merkle_legacy( std::move(digests) );
}
}

Expand Down
1 change: 0 additions & 1 deletion libraries/chain/include/eosio/chain/block_header_state.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once
#include <eosio/chain/block_header.hpp>
#include <eosio/chain/finality_core.hpp>
#include <eosio/chain/incremental_merkle.hpp>
#include <eosio/chain/protocol_feature_manager.hpp>
#include <eosio/chain/hotstuff/hotstuff.hpp>
#include <eosio/chain/hotstuff/finalizer_policy.hpp>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include <eosio/chain/block_header.hpp>
#include <eosio/chain/incremental_merkle.hpp>
#include <eosio/chain/incremental_merkle_legacy.hpp>
#include <eosio/chain/protocol_feature_manager.hpp>
#include <eosio/chain/chain_snapshot.hpp>
#include <future>
Expand Down Expand Up @@ -34,7 +34,7 @@ namespace detail {
uint32_t dpos_proposed_irreversible_blocknum = 0;
uint32_t dpos_irreversible_blocknum = 0;
producer_authority_schedule active_schedule;
incremental_legacy_merkle_tree blockroot_merkle;
incremental_merkle_tree_legacy blockroot_merkle;
flat_map<account_name,uint32_t> producer_to_last_produced;
flat_map<account_name,uint32_t> producer_to_last_implied_irb;
block_signing_authority valid_block_signing_authority;
Expand Down
1 change: 1 addition & 0 deletions libraries/chain/include/eosio/chain/block_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <eosio/chain/block.hpp>
#include <eosio/chain/transaction_metadata.hpp>
#include <eosio/chain/action_receipt.hpp>
#include <eosio/chain/incremental_merkle.hpp>

namespace eosio::chain {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <eosio/chain/kv_config.hpp>
#include <eosio/chain/wasm_config.hpp>
#include <eosio/chain/producer_schedule.hpp>
#include <eosio/chain/incremental_merkle.hpp>
#include <eosio/chain/snapshot.hpp>
#include <chainbase/chainbase.hpp>
#include "multi_index_includes.hpp"
Expand Down
Loading

0 comments on commit 8c03a62

Please sign in to comment.