Skip to content

Commit

Permalink
Merge pull request #921 from AntelopeIO/GH-284-limit-apply-blocks
Browse files Browse the repository at this point in the history
[1.0.3] Limit apply blocks
  • Loading branch information
heifner authored Oct 11, 2024
2 parents 97dc35e + 6c36b95 commit 87079b3
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1689,8 +1689,6 @@ struct controller_impl {
}

void replay(startup_t startup) {
replaying = true;

bool replay_block_log_needed = should_replay_block_log();

auto blog_head = blog.head();
Expand Down Expand Up @@ -1838,8 +1836,6 @@ struct controller_impl {
};
fork_db.apply<void>(replay_fork_db);

replaying = false;

if( except_ptr ) {
std::rethrow_exception( except_ptr );
}
Expand Down Expand Up @@ -1988,6 +1984,7 @@ struct controller_impl {
ilog( "chain database started with hash: ${hash}", ("hash", calculate_integrity_hash()) );
okay_to_print_integrity_hash_on_stop = true;

fc::scoped_set_value r(replaying, true);
replay( startup ); // replay any irreversible and reversible blocks ahead of current head

if( check_shutdown() ) return;
Expand Down Expand Up @@ -3630,8 +3627,6 @@ struct controller_impl {

template<typename BSP>
void log_applied(controller::block_report& br, const BSP& bsp) const {
if (replaying) // fork_db_root_block_num not available during replay
return;
fc::time_point now = fc::time_point::now();
if (now - bsp->timestamp() < fc::minutes(5) || (bsp->block_num() % 1000 == 0)) {
ilog("Received block ${id}... #${n} @ ${t} signed by ${p} " // "Received" instead of "Applied" so it matches existing log output
Expand Down Expand Up @@ -4413,6 +4408,7 @@ struct controller_impl {
const forked_callback_t& forked_cb, const trx_meta_cache_lookup& trx_lookup )
{
auto do_maybe_switch_forks = [&](auto& forkdb) {
auto start = fc::time_point::now();
if( new_head->header.previous == chain_head.id() ) {
try {
apply_block( br, new_head, s, trx_lookup );
Expand Down Expand Up @@ -4468,10 +4464,17 @@ struct controller_impl {
br = controller::block_report();
bool applied = apply_block( br, bsp, bsp->is_valid() ? controller::block_status::validated
: controller::block_status::complete, trx_lookup );
if (!switch_fork && (!applied || check_shutdown())) {
shutdown();
break;
if (!switch_fork) { // always complete a switch fork
if (!applied || check_shutdown()) {
shutdown();
break;
}
// Break every ~500ms to allow other tasks (e.g. get_info, SHiP) opportunity to run. There is a post
// for every incoming blocks; enough posted tasks to apply all blocks queued to the fork db.
if (!replaying && fc::time_point::now() - start > fc::milliseconds(500))
break;
}

} catch ( const std::bad_alloc& ) {
throw;
} catch ( const boost::interprocess::bad_alloc& ) {
Expand Down

0 comments on commit 87079b3

Please sign in to comment.