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

Refactor fork database #347

Merged
merged 32 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
064f238
Remove cout in unittest, replace with dlog
heifner Jul 2, 2024
93511f3
GH-333 Remove unneeded calls to forkdb->head()
heifner Jul 2, 2024
eac6bc7
GH-333 Make public controller fork_db_head* return fork db pending head
heifner Jul 2, 2024
096c3a1
GH-333 Send branch from chain_head, also rename fork_head in connecti…
heifner Jul 3, 2024
171cda4
GH-333 Use local fork_db_head_irreversible_blocknum with chain_head i…
heifner Jul 5, 2024
04945ce
GH-333 Use pending head for branch of irreversible
heifner Jul 5, 2024
ca11e84
GH-333 Use pending head during startup instead of head
heifner Jul 5, 2024
a6f1ba9
GH-333 Remove fork_db_head_or_pending
heifner Jul 5, 2024
c9de9a3
GH-333 Optimize get_info by extracting block_num from id
heifner Jul 5, 2024
44bcc6c
GH-333 Update test to use head
heifner Jul 5, 2024
13ef4f1
GH-333 Remove valid from fork choice rule. Optionally return root fro…
heifner Jul 8, 2024
a411680
GH-333 Store if_irreversible_block_id in fork database
heifner Jul 8, 2024
06b2c88
GH-333 Use fork database to track savanna irreversible block id
heifner Jul 8, 2024
aa4ba63
GH-333 Add debug log output of maybe_switch_forks
heifner Jul 8, 2024
201a1d5
GH-333 Verify correct type of block state
heifner Jul 8, 2024
3c1dfe1
GH-333 Transition valid blocks
heifner Jul 9, 2024
0d5c8c2
GH-333 Add startup tests in irreversible mode
heifner Jul 9, 2024
c60e459
GH-333 Maintain legacy only advancing LIB via validated blocks. Trans…
heifner Jul 9, 2024
53a3e53
GH-333 Remove mark_valid from fork_database since validated no longer…
heifner Jul 9, 2024
6379dcf
Merge remote-tracking branch 'spring/replay_test' into GH-333-forkdb-…
heifner Jul 9, 2024
7aa505d
GH-333 Mark block state in forkdb as valid
heifner Jul 9, 2024
53ba6e2
GH-333 Fix bad block legacy fork test to allow forking on second to l…
heifner Jul 9, 2024
5cc3030
GH-333 Renamed pending_head() to head()
heifner Jul 9, 2024
cebccfc
GH-333 Fix comment
heifner Jul 9, 2024
d67c298
GH-333 Modify fork_database to use block_state is_valid, set_valid vi…
heifner Jul 9, 2024
099c53b
GH-333 Use valueless_by_exception() instead of index() != std::varian…
heifner Jul 9, 2024
ad3336c
GH-333 Remove unneeded mark_all_invalid()
heifner Jul 9, 2024
a600ff1
GH-333 Replay only validated reversible blocks from forkdb
heifner Jul 10, 2024
834a2b1
Merge branch 'main' into GH-333-forkdb-head
heifner Jul 10, 2024
add318f
GH-333 Simplify implementation
heifner Jul 10, 2024
28557f1
GH-333 Fix log statement
heifner Jul 10, 2024
0cb2d77
GH-333 Add additional comments. Move pending_lib_id impl to cpp file
heifner Jul 10, 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
1 change: 1 addition & 0 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,7 @@ struct controller_impl {
("lib_num", lib_num)("bn", fork_db_root_block_num()) );
}

// maintain legacy only advancing LIB via validated blocks, hence pass in chain_head id for use
const block_id_type new_lib_id = fork_db.pending_lib_id(irreversible_mode() ? block_id_type{} : chain_head.id());
const block_num_type new_lib_num = block_header::num_from_id(new_lib_id);

Expand Down
20 changes: 20 additions & 0 deletions libraries/chain/fork_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,26 @@ namespace eosio::chain {
});
}

block_id_type fork_database::pending_lib_id(const block_id_type& head_id) const {
if (in_use.load() == in_use_t::legacy) {
block_state_legacy_ptr head;
if (head_id.empty()) {
head = fork_db_l.head();
} else {
head = fork_db_l.get_block(head_id);
}
if (!head)
return {};
block_num_type lib_num = head->irreversible_blocknum();
auto lib = fork_db_l.search_on_branch(head->id(), lib_num, include_root_t::no);
if (!lib)
return {};
return lib->id();
} else {
return fork_db_s.pending_savanna_lib_id();
}
}

// do class instantiations
template class fork_database_t<block_state_legacy_ptr>;
template class fork_database_t<block_state_ptr>;
Expand Down
40 changes: 19 additions & 21 deletions libraries/chain/include/eosio/chain/fork_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,22 @@ namespace eosio::chain {
bool is_valid() const; // sanity checks on this fork_db

bool has_root() const;
bsp_t root() const; // undefined if !has_root()

/**
* Root of the fork database, not part of the index. Corresponds to head of the block log. Is an irreversible block.
* Undefined if !has_root()
*/
bsp_t root() const;

/**
* The best branch head of blocks in the fork database, can be null if include_root_t::no and forkdb is empty
* @param include_root yes if root should be returned if no blocks in fork database
*/
bsp_t head(include_root_t include_root = include_root_t::no) const;

/**
* The calculated pending savanna LIB ID that will become LIB or is currently LIB
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those comments are very helpful!

block_id_type pending_savanna_lib_id() const;
bool set_pending_savanna_lib_id( const block_id_type& id );

Expand Down Expand Up @@ -163,26 +177,10 @@ namespace eosio::chain {
// see fork_database_t::fetch_branch(forkdb->head()->id())
block_branch_t fetch_branch_from_head() const;

block_id_type pending_lib_id(const block_id_type& head_id) const {
if (in_use.load() == in_use_t::legacy) {
block_state_legacy_ptr head;
if (head_id.empty()) {
head = fork_db_l.head();
} else {
// maintain legacy only advancing LIB via validated blocks
head = fork_db_l.get_block(head_id);
}
if (!head)
return {};
block_num_type lib_num = head->irreversible_blocknum();
auto lib = fork_db_l.search_on_branch(head->id(), lib_num, include_root_t::no);
if (!lib)
return {};
return lib->id();
} else {
return fork_db_s.pending_savanna_lib_id();
}
}
/// The pending LIB.
/// - legacy returns dpos_irreversible_blocknum according to head_id or pending head if head_id is empty
/// - savanna returns the current pending_savanna_lib_id
block_id_type pending_lib_id(const block_id_type& head_id) const;

template <class R, class F>
R apply(const F& f) const {
Expand Down