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

IF: Update fork database fork-choice rule - complex #2265

Merged
merged 28 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
472b1d4
GH-2125 Optimize fetch_block_branch
heifner Feb 8, 2024
7c19670
GH-2125 Update best fork logic of fork_database.
heifner Feb 13, 2024
8101af0
GH-2125 Update fork_database on strong vote
heifner Feb 13, 2024
5d2a839
GH-2125 Make last_qc_block_num non-optional
heifner Feb 14, 2024
29ddf49
Merge remote-tracking branch 'origin/hotstuff_integration' into GH-21…
heifner Feb 14, 2024
57371bd
GH-2125 Move get_activated_protocol_features to block_header_state[_l…
heifner Feb 14, 2024
6ab1146
GH-2125 Remove fork database adaptors and use fork database accessors…
heifner Feb 14, 2024
f8828ac
Merge remote-tracking branch 'origin/hotstuff_integration' into GH-21…
heifner Feb 15, 2024
d0f7bf7
Merge remote-tracking branch 'origin/hotstuff_integration' into GH-21…
heifner Feb 23, 2024
970528c
GH-2125 Add updated_core and most_recent_ancestor_with_qc to block_st…
heifner Feb 26, 2024
9e0a226
GH-2125 Revert unneeded changes
heifner Feb 26, 2024
17274e2
GH-2125 Potentially switch forks before beginning to produce a block
heifner Feb 27, 2024
842ed6c
GH-2125 Can't have pending block when applying a block
heifner Feb 27, 2024
37a7a17
GH-2125 Use vector instead of deque
heifner Feb 27, 2024
042ffde
GH-2125 Return current state even on error
heifner Feb 27, 2024
1eda923
GH-2125 Rename method to make more clear
heifner Feb 27, 2024
2767821
GH-2125 Use descriptive types
heifner Feb 27, 2024
bc5d464
GH-2125 Change order of eval
heifner Feb 27, 2024
c3847eb
GH-2125 Improve logging
heifner Feb 27, 2024
6a45921
GH-2125 Use descriptive enums instead of bools
heifner Feb 27, 2024
69373f4
GH-2125 Revert unneeded change
heifner Feb 27, 2024
f9dd443
GH-2125 Remove duplicate
heifner Feb 27, 2024
772d74e
GH-2125 Rename most_recent_ancestor_with_qc to best_qc_claim
heifner Feb 27, 2024
2153ba2
Merge remote-tracking branch 'origin/hotstuff_integration' into GH-21…
heifner Feb 27, 2024
39b3d3f
GH-2125 Only update best qc on state changes
heifner Feb 27, 2024
3ec3d79
GH-2125 Fix update_best_qc logic for processing descendants
heifner Feb 28, 2024
d9c1b87
GH-2125 use a counter instead of time to ensure unique
heifner Feb 28, 2024
e2f0d4d
GH-2125 Add some comments
heifner Feb 28, 2024
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
7 changes: 3 additions & 4 deletions libraries/chain/fork_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ namespace eosio::chain {
:my( new fork_database_impl<BSP>(magic_number) )
{}

template<class BSP>
fork_database_t<BSP>::~fork_database_t() = default;

template<class BSP>
void fork_database_t<BSP>::open( const std::filesystem::path& fork_db_file, validator_t& validator ) {
Expand Down Expand Up @@ -722,11 +724,8 @@ namespace eosio::chain {
previdx.modify( previtr, [&](auto& i) {
BSAccessor::update_best_qc(*i, best_qc_claim);
});
} else {
break;
descendants.emplace_back( (*previtr)->id() );
}

descendants.emplace_back( (*previtr)->id() );
++previtr;
}
}
Expand Down
1 change: 0 additions & 1 deletion libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <eosio/chain/trace.hpp>
#include <eosio/chain/genesis_state.hpp>
#include <eosio/chain/snapshot.hpp>
#include <eosio/chain/fork_database.hpp>
#include <eosio/chain/protocol_feature_manager.hpp>
#include <eosio/chain/webassembly/eos-vm-oc/config.hpp>
#include <eosio/chain/hotstuff/hotstuff.hpp>
Expand Down
1 change: 1 addition & 0 deletions libraries/chain/include/eosio/chain/fork_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace eosio::chain {
using branch_type_pair = pair<branch_type, branch_type>;

explicit fork_database_t(uint32_t magic_number = legacy_magic_number);
~fork_database_t();

void open( const std::filesystem::path& fork_db_file, validator_t& validator );
void close( const std::filesystem::path& fork_db_file );
Expand Down
131 changes: 131 additions & 0 deletions unittests/fork_db_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#include <eosio/chain/types.hpp>
#include <eosio/chain/fork_database.hpp>
#include <eosio/testing/tester.hpp>
#include <fc/bitutil.hpp>
#include <boost/test/unit_test.hpp>


namespace eosio::chain {

inline block_id_type make_block_id(block_num_type block_num) {
block_id_type id = fc::sha256::hash(std::to_string(block_num) + std::to_string(fc::time_point::now().time_since_epoch().count()));
id._hash[0] &= 0xffffffff00000000;
id._hash[0] += fc::endian_reverse_u32(block_num); // store the block num in the ID, 160 bits is plenty for the hash
return id;
}

struct block_state_accessor {

static auto make_genesis_block_state() {
block_state_ptr root = std::make_shared<block_state>();
block_id_type genesis_id = make_block_id(10);
root->block_id = genesis_id;
root->header.timestamp = block_timestamp_type{10};
root->core = finality_core::create_core_for_genesis_block(10);
root->best_qc_claim = {.block_num = 10, .is_strong_qc = false};
return root;
}

// use block_num > 10
static auto make_unique_block_state(block_num_type block_num, const block_state_ptr& prev) {
block_state_ptr bsp = std::make_shared<block_state>();
bsp->block_id = make_block_id(block_num);
bsp->header.timestamp.slot = prev->header.timestamp.slot + 1;
bsp->header.previous = prev->id();
block_ref parent_block {
.block_id = prev->id(),
.timestamp = prev->timestamp()
};
bsp->core = prev->core.next(parent_block, prev->best_qc_claim);
bsp->best_qc_claim = bsp->core.latest_qc_claim();
bsp->updated_core = bsp->core.next_metadata(bsp->best_qc_claim);
return bsp;
}

static auto get_best_qc_claim(const block_state_ptr& bs) {
return bs->best_qc_claim;
}
};

} // namespace eosio::chain

using namespace eosio::chain;

BOOST_AUTO_TEST_SUITE(fork_database_tests)

BOOST_AUTO_TEST_CASE(update_best_qc) try {

linh2931 marked this conversation as resolved.
Show resolved Hide resolved
fc::temp_directory temp_dir;
const auto& temp = temp_dir.path();
fork_database fdb(temp);
fork_database_if_t forkdb;
auto root = block_state_accessor::make_genesis_block_state();
auto bsp11a = block_state_accessor::make_unique_block_state(11, root);
auto bsp12a = block_state_accessor::make_unique_block_state(12, bsp11a);
auto bsp13a = block_state_accessor::make_unique_block_state(13, bsp12a);
auto bsp11b = block_state_accessor::make_unique_block_state(11, root);
auto bsp12b = block_state_accessor::make_unique_block_state(12, bsp11b);
auto bsp13b = block_state_accessor::make_unique_block_state(13, bsp12b);
auto bsp14b = block_state_accessor::make_unique_block_state(14, bsp13b);
auto bsp12bb = block_state_accessor::make_unique_block_state(12, bsp11b);
auto bsp13bb = block_state_accessor::make_unique_block_state(13, bsp12bb);
auto bsp13bbb = block_state_accessor::make_unique_block_state(13, bsp12bb);
auto bsp12bbb = block_state_accessor::make_unique_block_state(12, bsp11b);
auto bsp11c = block_state_accessor::make_unique_block_state(11, root);
auto bsp12c = block_state_accessor::make_unique_block_state(12, bsp11c);
auto bsp13c = block_state_accessor::make_unique_block_state(13, bsp12c);

std::vector<block_state_ptr> all { bsp11a, bsp12a, bsp13a, bsp11b, bsp12b, bsp12bb, bsp12bbb, bsp13b, bsp13bb, bsp13bbb, bsp14b, bsp11c, bsp12c, bsp13c };

forkdb.reset_root(*root);
forkdb.add(bsp11a, mark_valid_t::no, ignore_duplicate_t::no);
forkdb.add(bsp11b, mark_valid_t::no, ignore_duplicate_t::no);
forkdb.add(bsp11c, mark_valid_t::no, ignore_duplicate_t::no);
forkdb.add(bsp12a, mark_valid_t::no, ignore_duplicate_t::no);
forkdb.add(bsp13a, mark_valid_t::no, ignore_duplicate_t::no);
forkdb.add(bsp12b, mark_valid_t::no, ignore_duplicate_t::no);
forkdb.add(bsp12bb, mark_valid_t::no, ignore_duplicate_t::no);
forkdb.add(bsp12bbb, mark_valid_t::no, ignore_duplicate_t::no);
forkdb.add(bsp12c, mark_valid_t::no, ignore_duplicate_t::no);
forkdb.add(bsp13b, mark_valid_t::no, ignore_duplicate_t::no);
forkdb.add(bsp13bb, mark_valid_t::no, ignore_duplicate_t::no);
forkdb.add(bsp13bbb, mark_valid_t::no, ignore_duplicate_t::no);
forkdb.add(bsp14b, mark_valid_t::no, ignore_duplicate_t::no);
forkdb.add(bsp13c, mark_valid_t::no, ignore_duplicate_t::no);

// test get_block
for (auto& i : all) {
BOOST_TEST(forkdb.get_block(i->id()) == i);
}

// test remove
forkdb.remove(bsp12b->id());
BOOST_TEST(!forkdb.get_block(bsp12b->id()));
BOOST_TEST(!forkdb.get_block(bsp13b->id()));
BOOST_TEST(!forkdb.get_block(bsp14b->id()));
forkdb.add(bsp12b, mark_valid_t::no, ignore_duplicate_t::no);
forkdb.add(bsp13b, mark_valid_t::no, ignore_duplicate_t::no);
forkdb.add(bsp14b, mark_valid_t::no, ignore_duplicate_t::no);

// test update_best_qc
BOOST_TEST(block_state_accessor::get_best_qc_claim(bsp11b).block_num == 10);
BOOST_TEST(block_state_accessor::get_best_qc_claim(bsp12b).block_num == 10);
forkdb.update_best_qc(bsp11b->id(), {.block_num = 11, .is_strong_qc = false});
BOOST_TEST(block_state_accessor::get_best_qc_claim(bsp12b).block_num == 11);
BOOST_TEST(block_state_accessor::get_best_qc_claim(bsp12b).is_strong_qc == false);
BOOST_TEST(block_state_accessor::get_best_qc_claim(bsp13b).block_num == 11);
BOOST_TEST(block_state_accessor::get_best_qc_claim(bsp14b).block_num == 11);
BOOST_TEST(block_state_accessor::get_best_qc_claim(bsp12bb).block_num == 11);
BOOST_TEST(block_state_accessor::get_best_qc_claim(bsp13bb).block_num == 11);
BOOST_TEST(block_state_accessor::get_best_qc_claim(bsp13bbb).block_num == 11);
BOOST_TEST(block_state_accessor::get_best_qc_claim(bsp12bbb).block_num == 11);
forkdb.update_best_qc(bsp13bb->id(), {.block_num = 11, .is_strong_qc = true});
BOOST_TEST(block_state_accessor::get_best_qc_claim(bsp13bb).block_num == 11);
BOOST_TEST(block_state_accessor::get_best_qc_claim(bsp13bb).is_strong_qc == true);
forkdb.update_best_qc(bsp11b->id(), {.block_num = 11, .is_strong_qc = true});
BOOST_TEST(block_state_accessor::get_best_qc_claim(bsp12b).is_strong_qc == true);
BOOST_TEST(block_state_accessor::get_best_qc_claim(bsp13bbb).is_strong_qc == true);

} FC_LOG_AND_RETHROW();

BOOST_AUTO_TEST_SUITE_END()
1 change: 1 addition & 0 deletions unittests/unapplied_transaction_queue_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <boost/test/unit_test.hpp>
#include <eosio/testing/tester.hpp>
#include <eosio/chain/fork_database.hpp>
#include <eosio/chain/unapplied_transaction_queue.hpp>
#include <eosio/chain/contract_types.hpp>

Expand Down
Loading