Skip to content

Commit

Permalink
address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
huangminghuang committed Mar 15, 2023
1 parent 75fb80b commit e6020fb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
3 changes: 2 additions & 1 deletion libraries/chain/block_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ namespace eosio { namespace chain {
virtual void reset(const chain_id_type& chain_id, uint32_t first_block_num) = 0;
virtual void flush() = 0;

virtual signed_block_ptr read_block_by_num(uint32_t block_num) = 0;
virtual signed_block_ptr read_block_by_num(uint32_t block_num) = 0;
virtual std::optional<signed_block_header> read_block_header_by_num(uint32_t block_num) = 0;

virtual uint32_t version() const = 0;
Expand Down Expand Up @@ -1231,6 +1231,7 @@ namespace eosio { namespace chain {
}

block_id_type block_log::read_block_id_by_num(uint32_t block_num) const {
// read_block_header_by_num acquires mutex
auto bh = read_block_header_by_num(block_num);
if (bh) { return bh->calculate_id(); }
return {};
Expand Down
8 changes: 5 additions & 3 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3093,18 +3093,20 @@ const global_property_object& controller::get_global_properties()const {
return my->db.get<global_property_object>();
}

signed_block_ptr controller::fetch_block_by_id( block_id_type id )const {
signed_block_ptr controller::fetch_block_by_id( const block_id_type& id )const {
auto state = my->fork_db.get_block(id);
if( state && state->block ) return state->block;
auto bptr = my->blog.read_block_by_num( block_header::num_from_id(id) );
if( bptr && bptr->calculate_id() == id ) return bptr;
return signed_block_ptr();
}

std::optional<signed_block_header> controller::fetch_block_header_by_id( block_id_type id )const {
std::optional<signed_block_header> controller::fetch_block_header_by_id( const block_id_type& id )const {
auto state = my->fork_db.get_block(id);
if( state && state->block ) return state->header;
return my->blog.read_block_header_by_num( block_header::num_from_id(id) );
auto result = my->blog.read_block_header_by_num( block_header::num_from_id(id) );
if( result && result->calculate_id() == id ) return result;
return {};
}

signed_block_ptr controller::fetch_block_by_number( uint32_t block_num )const { try {
Expand Down
4 changes: 2 additions & 2 deletions libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,11 @@ namespace eosio { namespace chain {
// thread-safe
signed_block_ptr fetch_block_by_number( uint32_t block_num )const;
// thread-safe
signed_block_ptr fetch_block_by_id( block_id_type id )const;
signed_block_ptr fetch_block_by_id( const block_id_type& id )const;
// thread-safe
std::optional<signed_block_header> fetch_block_header_by_number( uint32_t block_num )const;
// thread-safe
std::optional<signed_block_header> fetch_block_header_by_id( block_id_type id )const;
std::optional<signed_block_header> fetch_block_header_by_id( const block_id_type& id )const;
// return block_state from forkdb, thread-safe
block_state_ptr fetch_block_state_by_number( uint32_t block_num )const;
// return block_state from forkdb, thread-safe
Expand Down
8 changes: 8 additions & 0 deletions tests/chain_plugin_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ BOOST_FIXTURE_TEST_CASE( get_block_with_invalid_abi, TESTER ) try {
BOOST_TEST(block_str2.find("Should Not Assert!") == std::string::npos); // decode failed
BOOST_TEST(block_str2.find("011253686f756c64204e6f742041737365727421") != std::string::npos); //action data


chain_apis::read_only::get_block_header_params bh_param{headnumstr, false};
auto get_bh_result = plugin.get_block_header(bh_param, fc::time_point::maximum());

BOOST_TEST(get_bh_result.id == block->calculate_id());
BOOST_TEST(json::to_string(get_bh_result.signed_block_header, fc::time_point::maximum()) ==
json::to_string(fc::variant{static_cast<signed_block_header&>(*block)}, fc::time_point::maximum()));

} FC_LOG_AND_RETHROW() /// get_block_with_invalid_abi

BOOST_FIXTURE_TEST_CASE( get_consensus_parameters, TESTER ) try {
Expand Down

0 comments on commit e6020fb

Please sign in to comment.