Skip to content

Commit

Permalink
Merge pull request #918 from AntelopeIO/GH-806-lib
Browse files Browse the repository at this point in the history
Normalize usage of LIB
  • Loading branch information
heifner authored Oct 11, 2024
2 parents b565770 + 9fd0a30 commit 460af47
Show file tree
Hide file tree
Showing 25 changed files with 552 additions and 564 deletions.
6 changes: 3 additions & 3 deletions docs/01_nodeos/04_replays/how-to-replay-from-a-blocks.log.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
content_title: How to replay from a blocks.log file
---

Once you have obtained a copy of the `blocks.log` file which you wish to replay the blockchain from, copy it to your `data/blocks` directory, backing up any existing contents if you wish to keep them, and remove the `blocks.index`, `forkdb.dat`, `shared_memory.bin`, and `shared_memory.meta`.
Once you have obtained a copy of the `blocks.log` file which you wish to replay the blockchain from, copy it to your `data/blocks` directory, backing up any existing contents if you wish to keep them, and remove the `blocks.index`, `fork_db.dat`, `shared_memory.bin`, and `shared_memory.meta`.

The table below sumarizes the actions you should take for each of the files enumerated above:

Folder name | File name | Action
----------------------- | ------------------ | ------
----------------------- |--------------------| ------
data/blocks | blocks.index | Remove
data/blocks | blocks.log | Replace this file with the `block.log` you want to replay
data/blocks/reversible | forkdb.dat | Remove
data/blocks/reversible | fork_db.dat | Remove
data/blocks/reversible | shared_memory.bin | Remove
data/blocks/reversible | shared_memory.meta | Remove

Expand Down
354 changes: 174 additions & 180 deletions libraries/chain/controller.cpp

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions libraries/chain/fork_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ namespace eosio::chain {

auto prev_bh = get_block_impl( n->previous(), include_root_t::yes );
EOS_ASSERT( prev_bh, unlinkable_block_exception,
"forkdb unlinkable block ${id} previous ${p}", ("id", n->id())("p", n->previous()) );
"fork_db unlinkable block ${id} previous ${p}", ("id", n->id())("p", n->previous()) );

if (validate) {
try {
Expand Down Expand Up @@ -350,7 +350,7 @@ namespace eosio::chain {
block_num_type new_lib = block_header::num_from_id(id);
block_num_type old_lib = block_header::num_from_id(pending_savanna_lib_id);
if (new_lib > old_lib) {
dlog("set pending savanna lib ${bn}: ${id}", ("bn", block_header::num_from_id(id))("id", id));
dlog("set fork db pending savanna lib ${bn}: ${id}", ("bn", block_header::num_from_id(id))("id", id));
pending_savanna_lib_id = id;
return true;
}
Expand Down Expand Up @@ -641,7 +641,7 @@ namespace eosio::chain {
}

void fork_database::close() {
auto fork_db_file {data_dir / config::forkdb_filename};
auto fork_db_file {data_dir / config::fork_db_filename};
bool legacy_valid = fork_db_l.is_valid();
bool savanna_valid = fork_db_s.is_valid();

Expand Down Expand Up @@ -684,7 +684,7 @@ namespace eosio::chain {
}

bool fork_database::file_exists() const {
auto fork_db_file = data_dir / config::forkdb_filename;
auto fork_db_file = data_dir / config::fork_db_filename;
return std::filesystem::exists( fork_db_file );
};

Expand All @@ -694,7 +694,7 @@ namespace eosio::chain {

assert(!fork_db_l.is_valid() && !fork_db_s.is_valid());

auto fork_db_file = data_dir / config::forkdb_filename;
auto fork_db_file = data_dir / config::fork_db_filename;
if( std::filesystem::exists( fork_db_file ) ) {
try {
fc::cfile f;
Expand Down Expand Up @@ -760,37 +760,37 @@ namespace eosio::chain {
}

size_t fork_database::size() const {
return apply<size_t>([](const auto& forkdb) {
return forkdb.size();
return apply<size_t>([](const auto& fork_db) {
return fork_db.size();
});
}

// only called from the main thread
void fork_database::switch_from_legacy(const block_state_ptr& root) {
// no need to close fork_db because we don't want to write anything out, file is removed on open
// threads may be accessing (or locked on mutex about to access legacy forkdb) so don't delete it until program exit
// threads may be accessing (or locked on mutex about to access legacy fork_db) so don't delete it until program exit
if (in_use == in_use_t::legacy) {
fork_db_s.reset_root(root);
if (fork_db_l.has_root()) {
dlog("Switching forkdb from legacy to both");
dlog("Switching fork_db from legacy to both");
in_use = in_use_t::both;
} else {
dlog("Switching forkdb from legacy to savanna");
dlog("Switching fork_db from legacy to savanna");
in_use = in_use_t::savanna;
}
} else if (in_use == in_use_t::both) {
dlog("Switching forkdb from legacy, already both root ${rid}, forkdb root ${fid}", ("rid", root->id())("fid", fork_db_s.root()->id()));
dlog("Switching fork_db from legacy, already both root ${rid}, fork_db root ${fid}", ("rid", root->id())("fid", fork_db_s.root()->id()));
assert(fork_db_s.root()->id() == root->id()); // should always set the same root
} else {
assert(false);
}
}

block_branch_t fork_database::fetch_branch_from_head() const {
return apply<block_branch_t>([&](auto& forkdb) {
auto head = forkdb.head();
return apply<block_branch_t>([&](auto& fork_db) {
auto head = fork_db.head();
if (head)
return forkdb.fetch_block_branch(head->id());
return fork_db.fetch_block_branch(head->id());
return block_branch_t{};
});
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/include/eosio/chain/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const static auto default_blocks_dir_name = "blocks";
const static auto reversible_blocks_dir_name = "reversible";

const static auto default_state_dir_name = "state";
const static auto forkdb_filename = "fork_db.dat";
const static auto fork_db_filename = "fork_db.dat";
const static auto safety_filename = "safety.dat";
const static auto chain_head_filename = "chain_head.dat";
const static auto default_state_size = 1*1024*1024*1024ll;
Expand Down
17 changes: 7 additions & 10 deletions libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ namespace eosio::chain {
void set_async_voting(async_t val);
void set_async_aggregation(async_t val);

/// Apply any blocks that are ready from the forkdb
/// Apply any blocks that are ready from the fork_db
void apply_blocks(const forked_callback_t& cb, const trx_meta_cache_lookup& trx_lookup);

struct accepted_block_result {
Expand Down Expand Up @@ -328,15 +328,12 @@ namespace eosio::chain {

void set_savanna_lib_id(const block_id_type& id);

bool fork_db_has_root() const;
size_t fork_db_size() const;

// thread-safe, applied LIB, fork db root
uint32_t last_irreversible_block_num() const;
// thread-safe, applied LIB, fork db root
block_id_type last_irreversible_block_id() const;
// thread-safe, applied LIB, fork db root
time_point last_irreversible_block_time() const;
// thread-safe
bool fork_db_has_root() const;
// thread-safe
block_handle fork_db_root()const;
// thread-safe
size_t fork_db_size() const;

// thread-safe, retrieves block according to fork db best branch which can change at any moment
signed_block_ptr fetch_block_by_number( uint32_t block_num )const;
Expand Down
4 changes: 2 additions & 2 deletions libraries/chain/include/eosio/chain/fork_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ namespace eosio::chain {
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
* The best branch head of blocks in the fork database, can be null if include_root_t::no and fork_db 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;
Expand Down Expand Up @@ -181,7 +181,7 @@ namespace eosio::chain {

in_use_t version_in_use() const { return in_use.load(); }

// see fork_database_t::fetch_branch(forkdb->head()->id())
// see fork_database_t::fetch_branch(fork_db->head()->id())
block_branch_t fetch_branch_from_head() const;

template <class R, class F>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace appbase {
// Add entries for each new non-unique handler type.
enum class handler_id {
unique, // identifies handler is unique, will not de-dup
process_incoming_block // process blocks already added to forkdb
process_incoming_block // process blocks already added to fork_db
};

enum class exec_queue {
Expand Down
4 changes: 2 additions & 2 deletions libraries/testing/include/eosio/testing/tester.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,8 @@ namespace eosio::testing {
block_handle fork_db_head() const { return control->fork_db_head(); }

chain_id_type get_chain_id() const { return control->get_chain_id(); }
block_id_type last_irreversible_block_id() const { return control->last_irreversible_block_id(); }
uint32_t last_irreversible_block_num() const { return control->last_irreversible_block_num(); }
block_id_type last_irreversible_block_id() const { return control->fork_db_root().id(); }
uint32_t last_irreversible_block_num() const { return control->fork_db_root().block_num(); }
bool block_exists(const block_id_type& id) const { return control->block_exists(id); }

signed_block_ptr fetch_block_by_id(const block_id_type& id) const {
Expand Down
6 changes: 3 additions & 3 deletions libraries/testing/tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ namespace eosio::testing {
control->set_async_voting(async_t::no); // vote synchronously so we don't have to wait for votes
control->set_async_aggregation(async_t::no); // aggregate votes synchronously for `_check_for_vote_if_needed`

lib_id = control->fork_db_has_root() ? control->last_irreversible_block_id() : block_id_type{};
lib_id = control->fork_db_has_root() ? control->fork_db_root().id() : block_id_type{};
lib_number = block_header::num_from_id(lib_id);
lib_block = control->fetch_block_by_id(lib_id);
[[maybe_unused]] auto lib_connection = control->irreversible_block().connect([&](const block_signal_params& t) {
Expand Down Expand Up @@ -515,10 +515,10 @@ namespace eosio::testing {
auto head_block_number = control->head().block_num();
auto producer = control->head_active_producers().get_scheduled_producer(block_time);

auto last_produced_block_num = control->last_irreversible_block_num();
auto last_produced_block_num = control->fork_db_root().block_num();
auto itr = last_produced_block.find(producer.producer_name);
if (itr != last_produced_block.end()) {
last_produced_block_num = std::max(control->last_irreversible_block_num(), block_header::num_from_id(itr->second));
last_produced_block_num = std::max(control->fork_db_root().block_num(), block_header::num_from_id(itr->second));
}

unapplied_transactions.add_aborted( control->abort_block() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ namespace eosio::chain::plugin_interface {
namespace methods {
using get_block_by_id = method_decl<chain_plugin_interface, signed_block_ptr(const block_id_type& block_id)>;
using get_head_block_id = method_decl<chain_plugin_interface, block_id_type ()>;

using get_last_irreversible_block_number = method_decl<chain_plugin_interface, uint32_t ()>;
}

namespace incoming {
Expand Down
17 changes: 9 additions & 8 deletions plugins/chain_plugin/account_query_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ namespace eosio::chain_apis {
auto start = fc::time_point::now();
const auto& index = controller.db().get_index<chain::permission_index>().indices().get<by_id>();

// build a initial time to block number map
const auto lib_num = controller.last_irreversible_block_num();
const auto head_num = controller.head().block_num();
// build an initial time to block number map
const auto fork_db_root_num = controller.fork_db_root().block_num();
const auto head_num = controller.head().block_num();

for (uint32_t block_num = lib_num + 1; block_num <= head_num; block_num++) {
for (uint32_t block_num = fork_db_root_num + 1; block_num <= head_num; block_num++) {
const auto block_p = controller.fetch_block_by_number(block_num);
EOS_ASSERT(block_p, chain::plugin_exception, "cannot fetch reversible block ${block_num}, required for account_db initialization", ("block_num", block_num));
time_to_block_num.emplace(block_p->timestamp.to_time_point(), block_num);
Expand Down Expand Up @@ -212,11 +212,12 @@ namespace eosio::chain_apis {
}

uint32_t last_updated_time_to_height( const fc::time_point& last_updated) {
const auto lib_num = controller.last_irreversible_block_num();
const auto lib_time = controller.last_irreversible_block_time();
const auto fork_db_root = controller.fork_db_root();
const auto fork_db_root_num = fork_db_root.block_num();
const auto fork_db_root_time = fork_db_root.block_time();

uint32_t last_updated_height = lib_num;
if (last_updated > lib_time) {
uint32_t last_updated_height = fork_db_root_num;
if (last_updated > fork_db_root_time) {
const auto iter = time_to_block_num.find(last_updated);
EOS_ASSERT(iter != time_to_block_num.end(), chain::plugin_exception, "invalid block time encountered in on-chain accounts ${time}", ("time", last_updated));
last_updated_height = iter->second;
Expand Down
29 changes: 13 additions & 16 deletions plugins/chain_plugin/chain_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ class chain_plugin_impl {
// method provider handles
methods::get_block_by_id::method_type::handle get_block_by_id_provider;
methods::get_head_block_id::method_type::handle get_head_block_id_provider;
methods::get_last_irreversible_block_number::method_type::handle get_last_irreversible_block_number_provider;

// scoped connections for chain controller
std::optional<scoped_connection> accepted_block_header_connection;
Expand Down Expand Up @@ -452,8 +451,8 @@ void clear_directory_contents( const std::filesystem::path& p ) {
namespace {
// This can be removed when versions of eosio that support reversible chainbase state file no longer supported.
void upgrade_from_reversible_to_fork_db(chain_plugin_impl* my) {
std::filesystem::path old_fork_db = my->chain_config->state_dir / config::forkdb_filename;
std::filesystem::path new_fork_db = my->blocks_dir / config::reversible_blocks_dir_name / config::forkdb_filename;
std::filesystem::path old_fork_db = my->chain_config->state_dir / config::fork_db_filename;
std::filesystem::path new_fork_db = my->blocks_dir / config::reversible_blocks_dir_name / config::fork_db_filename;
if( std::filesystem::exists( old_fork_db ) && std::filesystem::is_regular_file( old_fork_db ) ) {
bool copy_file = false;
if( std::filesystem::exists( new_fork_db ) && std::filesystem::is_regular_file( new_fork_db ) ) {
Expand Down Expand Up @@ -1028,11 +1027,6 @@ void chain_plugin_impl::plugin_initialize(const variables_map& options) {
return chain->head().id();
} );

get_last_irreversible_block_number_provider = app().get_method<methods::get_last_irreversible_block_number>().register_provider(
[this]() {
return chain->last_irreversible_block_num();
} );

// relay signals to channels
accepted_block_header_connection = chain->accepted_block_header().connect(
[this]( const block_signal_params& t ) {
Expand Down Expand Up @@ -1290,16 +1284,19 @@ const string read_only::KEYi64 = "i64";
read_only::get_info_results read_only::get_info(const read_only::get_info_params&, const fc::time_point&) const {
const auto& rm = db.get_resource_limits_manager();

auto head_id = db.head().id();
auto lib_id = db.last_irreversible_block_id();
auto fhead_id = db.fork_db_head().id();
auto head = db.head();
auto head_id = head.id();
auto fork_db_root = db.fork_db_root();
auto fork_db_head = db.fork_db_head();
auto fork_db_root_id = fork_db_root.id();
auto fork_db_head_id = fork_db_head.id();

return {
itoh(static_cast<uint32_t>(app().version())),
db.get_chain_id(),
block_header::num_from_id(head_id),
block_header::num_from_id(lib_id),
lib_id,
block_header::num_from_id(fork_db_root_id),
fork_db_root_id,
head_id,
db.head().block_time(),
db.head().producer(),
Expand All @@ -1310,13 +1307,13 @@ read_only::get_info_results read_only::get_info(const read_only::get_info_params
//std::bitset<64>(db.get_dynamic_global_properties().recent_slots_filled).to_string(),
//__builtin_popcountll(db.get_dynamic_global_properties().recent_slots_filled) / 64.0,
app().version_string(),
block_header::num_from_id(fhead_id),
fhead_id,
block_header::num_from_id(fork_db_head_id),
fork_db_head_id,
app().full_version_string(),
rm.get_total_cpu_weight(),
rm.get_total_net_weight(),
db.earliest_available_block_num(),
db.last_irreversible_block_time()
fork_db_root.block_time()
};
}

Expand Down
12 changes: 6 additions & 6 deletions plugins/net_plugin/include/eosio/net_plugin/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ namespace eosio {
fc::sha256 token; ///< digest of time to prove we own the private key of the key above
chain::signature_type sig; ///< signature for the digest
string p2p_address;
uint32_t last_irreversible_block_num = 0;
block_id_type last_irreversible_block_id;
uint32_t fork_head_num = 0;
block_id_type fork_head_id;
uint32_t fork_db_root_num = 0;
block_id_type fork_db_root_id;
uint32_t fork_db_head_num = 0;
block_id_type fork_db_head_id;
string os;
string agent;
int16_t generation = 0;
Expand Down Expand Up @@ -154,8 +154,8 @@ FC_REFLECT( eosio::chain_size_message,
FC_REFLECT( eosio::handshake_message,
(network_version)(chain_id)(node_id)(key)
(time)(token)(sig)(p2p_address)
(last_irreversible_block_num)(last_irreversible_block_id)
(fork_head_num)(fork_head_id)
(fork_db_root_num)(fork_db_root_id)
(fork_db_head_num)(fork_db_head_id)
(os)(agent)(generation) )
FC_REFLECT( eosio::go_away_message, (reason)(node_id) )
FC_REFLECT( eosio::time_message, (org)(rec)(xmt)(dst) )
Expand Down
Loading

0 comments on commit 460af47

Please sign in to comment.