Skip to content

Commit

Permalink
Fix: state history get block from forkdb null
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank-AFN committed Jun 6, 2020
1 parent 58ff806 commit 7d5a128
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 2 deletions.
24 changes: 24 additions & 0 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2482,6 +2482,15 @@ signed_block_ptr controller::fetch_block_by_number( uint32_t block_num )const {
return my->blog.read_block_by_num(block_num);
} FC_CAPTURE_AND_RETHROW( (block_num) ) }

signed_block_ptr controller::fetch_block_by_number_state_history( uint32_t block_num )const { try {
auto blk_state = fetch_block_state_by_number_state_history( block_num );
if( blk_state ) {
return blk_state->block;
}

return my->blog.read_block_by_num(block_num);
} FC_CAPTURE_AND_RETHROW( (block_num) ) }

block_state_ptr controller::fetch_block_state_by_id( block_id_type id )const {
auto state = my->fork_db.get_block(id);
return state;
Expand All @@ -2492,6 +2501,21 @@ block_state_ptr controller::fetch_block_state_by_number( uint32_t block_num )con
return blk_state;
} FC_CAPTURE_AND_RETHROW( (block_num) ) }

block_state_ptr controller::fetch_block_state_by_number_state_history( uint32_t block_num )const { try {
const auto& rev_blocks = my->reversible_blocks.get_index<reversible_block_index,by_num>();
auto objitr = rev_blocks.find(block_num);

if( objitr == rev_blocks.end() ) {
if( my->read_mode == db_read_mode::IRREVERSIBLE ) {
return my->fork_db.search_on_branch( my->pending->_pending_block_state->id, block_num );
} else {
return block_state_ptr();
}
}

return my->fork_db.get_block( objitr->get_block_id() );
} FC_CAPTURE_AND_RETHROW( (block_num) ) }

block_id_type controller::get_block_id_for_num( uint32_t block_num )const { try {
auto blk_state = my->fork_db.get_block_in_current_chain_by_num( block_num );
if( blk_state ) {
Expand Down
9 changes: 9 additions & 0 deletions libraries/chain/fork_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,15 @@ namespace eosio { namespace chain {
return result;
} /// fetch_branch_from

block_state_ptr fork_database::search_on_branch( const block_id_type& h, uint32_t block_num )const {
for( auto s = get_block(h); s; s = get_block( s->header.previous ) ) {
if( s->block_num == block_num )
return s;
}

return {};
}

/// remove all of the invalid forks built of this id including this id
void fork_database::remove( const block_id_type& id ) {
vector<block_id_type> remove_queue{id};
Expand Down
2 changes: 2 additions & 0 deletions libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,11 @@ namespace eosio { namespace chain {
block_id_type last_irreversible_block_id() const;

signed_block_ptr fetch_block_by_number( uint32_t block_num )const;
signed_block_ptr fetch_block_by_number_state_history( uint32_t block_num )const;
signed_block_ptr fetch_block_by_id( block_id_type id )const;

block_state_ptr fetch_block_state_by_number( uint32_t block_num )const;
block_state_ptr fetch_block_state_by_number_state_history( uint32_t block_num )const;
block_state_ptr fetch_block_state_by_id( block_id_type id )const;

block_id_type get_block_id_for_num( uint32_t block_num )const;
Expand Down
1 change: 1 addition & 0 deletions libraries/chain/include/eosio/chain/fork_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace eosio { namespace chain {
pair< branch_type, branch_type > fetch_branch_from( const block_id_type& first,
const block_id_type& second )const;

block_state_ptr search_on_branch( const block_id_type& h, uint32_t block_num )const;

/**
* If the block is invalid, it will be removed. If it is valid, then blocks older
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ namespace eosio { namespace chain {
fc::raw::unpack( ds, *result );
return result;
}

block_id_type get_block_id()const {
fc::datastream<const char*> ds( packedblock.data(), packedblock.size() );
block_header h;
fc::raw::unpack( ds, h );
// Only need the block id to then look up the block state in fork database, so just unpack the block_header from the stored packed data.
// Avoid calling get_block() since that constructs a new signed_block in heap memory and unpacks the full signed_block from the stored packed data.
return h.id();
}
};

struct by_num;
Expand Down
4 changes: 2 additions & 2 deletions plugins/state_history_plugin/state_history_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ struct state_history_plugin_impl : std::enable_shared_from_this<state_history_pl
void get_block(uint32_t block_num, fc::optional<bytes>& result) {
chain::signed_block_ptr p;
try {
p = chain_plug->chain().fetch_block_by_number(block_num);
p = chain_plug->chain().fetch_block_by_number_state_history(block_num);
} catch (...) {
return;
}
Expand All @@ -161,7 +161,7 @@ struct state_history_plugin_impl : std::enable_shared_from_this<state_history_pl
if (chain_state_log && block_num >= chain_state_log->begin_block() && block_num < chain_state_log->end_block())
return chain_state_log->get_block_id(block_num);
try {
auto block = chain_plug->chain().fetch_block_by_number(block_num);
auto block = chain_plug->chain().fetch_block_by_number_state_history(block_num);
if (block)
return block->id();
} catch (...) {
Expand Down

0 comments on commit 7d5a128

Please sign in to comment.