From 0954b8edf975b390b13c270645b7045088d0cdfc Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Wed, 14 Aug 2019 08:19:02 -0500 Subject: [PATCH 1/4] emit accepted block before updating block_log lib since accepted block does not depend on lib and we should ship out block as soon as possible --- libraries/chain/controller.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/chain/controller.cpp b/libraries/chain/controller.cpp index 8e72a43b387..1cea3ac8862 100644 --- a/libraries/chain/controller.cpp +++ b/libraries/chain/controller.cpp @@ -1654,11 +1654,11 @@ struct controller_impl { }); } + emit( self.accepted_block, bsp ); + if( add_to_fork_db ) { log_irreversible(); } - - emit( self.accepted_block, bsp ); } catch (...) { // dont bother resetting pending, instead abort the block reset_pending_on_exit.cancel(); From 63ff4f8f253b71a0260fb88823d5f3c24aee8e44 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Tue, 20 Aug 2019 11:45:10 -0500 Subject: [PATCH 2/4] Optimize getting block id from block log --- libraries/chain/block_log.cpp | 27 +++++++++++++++++++ libraries/chain/controller.cpp | 14 +++++----- .../chain/include/eosio/chain/block_log.hpp | 3 +++ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/libraries/chain/block_log.cpp b/libraries/chain/block_log.cpp index d16a84213a7..5b8a93cc92d 100644 --- a/libraries/chain/block_log.cpp +++ b/libraries/chain/block_log.cpp @@ -319,6 +319,14 @@ namespace eosio { namespace chain { return result; } + void block_log::read_block_header(block_header& bh, uint64_t pos)const { + my->check_open_files(); + + my->block_file.seek(pos); + auto ds = my->block_file.create_datastream(); + fc::raw::unpack(ds, bh); + } + signed_block_ptr block_log::read_block_by_num(uint32_t block_num)const { try { signed_block_ptr b; @@ -332,6 +340,21 @@ namespace eosio { namespace chain { } FC_LOG_AND_RETHROW() } + block_id_type block_log::read_block_id_by_num(uint32_t block_num)const { + try { + signed_block_ptr b; + uint64_t pos = get_block_pos(block_num); + if (pos != npos) { + block_header bh; + read_block_header(bh, pos); + EOS_ASSERT(b->block_num() == block_num, reversible_blocks_exception, + "Wrong block header was read from block log.", ("returned", b->block_num())("expected", block_num)); + return bh.id(); + } + return {}; + } FC_LOG_AND_RETHROW() + } + uint64_t block_log::get_block_pos(uint32_t block_num) const { my->check_open_files(); if (!(my->head && block_num <= block_header::num_from_id(my->head_id) && block_num >= my->first_block_num)) @@ -365,6 +388,10 @@ namespace eosio { namespace chain { return my->head; } + const block_id_type& block_log::head_id()const { + return my->head_id; + } + uint32_t block_log::first_block_num() const { return my->first_block_num; } diff --git a/libraries/chain/controller.cpp b/libraries/chain/controller.cpp index 1cea3ac8862..a4397ece63f 100644 --- a/libraries/chain/controller.cpp +++ b/libraries/chain/controller.cpp @@ -377,7 +377,7 @@ struct controller_impl { auto root_id = fork_db.root()->id; if( log_head ) { - EOS_ASSERT( root_id == log_head->id(), fork_database_exception, "fork database root does not match block log head" ); + EOS_ASSERT( root_id == blog.head_id(), fork_database_exception, "fork database root does not match block log head" ); } else { EOS_ASSERT( fork_db.root()->block_num == lib_num, fork_database_exception, "empty block log expects the first appended block to build off a block that is not the fork database root" ); @@ -2697,12 +2697,12 @@ block_id_type controller::last_irreversible_block_id() const { if( block_header::num_from_id(tapos_block_summary.block_id) == lib_num ) return tapos_block_summary.block_id; - auto signed_blk = my->blog.read_block_by_num( lib_num ); + auto id = my->blog.read_block_id_by_num( lib_num ); - EOS_ASSERT( BOOST_LIKELY( signed_blk != nullptr ), unknown_block_exception, + EOS_ASSERT( BOOST_LIKELY( id != block_id_type() ), unknown_block_exception, "Could not find block: ${block}", ("block", lib_num) ); - return signed_blk->id(); + return id; } const dynamic_global_property_object& controller::get_dynamic_global_properties()const { @@ -2768,12 +2768,12 @@ block_id_type controller::get_block_id_for_num( uint32_t block_num )const { try } } - auto signed_blk = my->blog.read_block_by_num(block_num); + auto id = my->blog.read_block_id_by_num(block_num); - EOS_ASSERT( BOOST_LIKELY( signed_blk != nullptr ), unknown_block_exception, + EOS_ASSERT( BOOST_LIKELY( id != block_id_type() ), unknown_block_exception, "Could not find block: ${block}", ("block", block_num) ); - return signed_blk->id(); + return id; } FC_CAPTURE_AND_RETHROW( (block_num) ) } sha256 controller::calculate_integrity_hash()const { try { diff --git a/libraries/chain/include/eosio/chain/block_log.hpp b/libraries/chain/include/eosio/chain/block_log.hpp index 93cdde1e822..9cf525628f2 100644 --- a/libraries/chain/include/eosio/chain/block_log.hpp +++ b/libraries/chain/include/eosio/chain/block_log.hpp @@ -43,7 +43,9 @@ namespace eosio { namespace chain { void reset( const genesis_state& gs, const signed_block_ptr& genesis_block, uint32_t first_block_num = 1 ); signed_block_ptr read_block(uint64_t file_pos)const; + void read_block_header(block_header& bh, uint64_t file_pos)const; signed_block_ptr read_block_by_num(uint32_t block_num)const; + block_id_type read_block_id_by_num(uint32_t block_num)const; signed_block_ptr read_block_by_id(const block_id_type& id)const { return read_block_by_num(block_header::num_from_id(id)); } @@ -54,6 +56,7 @@ namespace eosio { namespace chain { uint64_t get_block_pos(uint32_t block_num) const; signed_block_ptr read_head()const; const signed_block_ptr& head()const; + const block_id_type& head_id()const; uint32_t first_block_num() const; static const uint64_t npos = std::numeric_limits::max(); From 7c92091dc4ca555ce4c87ed6f517d9eca901d143 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Tue, 20 Aug 2019 13:49:47 -0500 Subject: [PATCH 3/4] Fix read_block_id_by_num to use block_header --- libraries/chain/block_log.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libraries/chain/block_log.cpp b/libraries/chain/block_log.cpp index 5b8a93cc92d..d9c971e7447 100644 --- a/libraries/chain/block_log.cpp +++ b/libraries/chain/block_log.cpp @@ -342,13 +342,12 @@ namespace eosio { namespace chain { block_id_type block_log::read_block_id_by_num(uint32_t block_num)const { try { - signed_block_ptr b; uint64_t pos = get_block_pos(block_num); if (pos != npos) { block_header bh; read_block_header(bh, pos); - EOS_ASSERT(b->block_num() == block_num, reversible_blocks_exception, - "Wrong block header was read from block log.", ("returned", b->block_num())("expected", block_num)); + EOS_ASSERT(bh.block_num() == block_num, reversible_blocks_exception, + "Wrong block header was read from block log.", ("returned", bh.block_num())("expected", block_num)); return bh.id(); } return {}; From 4e5c7da34722d3a1d31d57b9d09371165541aa77 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Tue, 27 Aug 2019 08:13:51 -0500 Subject: [PATCH 4/4] Changed last_irreversible_block_num to call get_block_id_for_num and optimized get_block_id_for_num to check tapos_block_summary first --- libraries/chain/controller.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/libraries/chain/controller.cpp b/libraries/chain/controller.cpp index aa9e8cc5dfa..de04d7f77af 100644 --- a/libraries/chain/controller.cpp +++ b/libraries/chain/controller.cpp @@ -2713,17 +2713,8 @@ uint32_t controller::last_irreversible_block_num() const { block_id_type controller::last_irreversible_block_id() const { auto lib_num = last_irreversible_block_num(); - const auto& tapos_block_summary = db().get((uint16_t)lib_num); - if( block_header::num_from_id(tapos_block_summary.block_id) == lib_num ) - return tapos_block_summary.block_id; - - auto id = my->blog.read_block_id_by_num( lib_num ); - - EOS_ASSERT( BOOST_LIKELY( id != block_id_type() ), unknown_block_exception, - "Could not find block: ${block}", ("block", lib_num) ); - - return id; + return get_block_id_for_num( lib_num ); } const dynamic_global_property_object& controller::get_dynamic_global_properties()const { @@ -2771,6 +2762,11 @@ block_state_ptr controller::fetch_block_state_by_number( uint32_t block_num )con } FC_CAPTURE_AND_RETHROW( (block_num) ) } block_id_type controller::get_block_id_for_num( uint32_t block_num )const { try { + const auto& tapos_block_summary = db().get((uint16_t)block_num); + + if( block_header::num_from_id(tapos_block_summary.block_id) == block_num ) + return tapos_block_summary.block_id; + const auto& blog_head = my->blog.head(); bool find_in_blog = (blog_head && block_num <= blog_head->block_num());