Skip to content

Commit

Permalink
Merge pull request #167 from eosiosg/statehistoryplugin
Browse files Browse the repository at this point in the history
Statehistoryplugin
  • Loading branch information
Thaipanda authored Jun 6, 2020
2 parents 68ca592 + 064a989 commit dc3b8e4
Show file tree
Hide file tree
Showing 14 changed files with 320 additions and 257 deletions.
38 changes: 31 additions & 7 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ struct controller_impl {
trace->scheduled = true;
trace->receipt = push_receipt( gtrx.trx_id, transaction_receipt::expired, billed_cpu_time_us, 0 ); // expire the transaction
emit( self.accepted_transaction, trx );
emit( self.applied_transaction, trace );
emit( self.applied_transaction, std::tie(trace, dtrx) );
undo_session.squash();
return trace;
}
Expand Down Expand Up @@ -1241,7 +1241,7 @@ struct controller_impl {
fc::move_append( pending->_actions, move(trx_context.executed) );

emit( self.accepted_transaction, trx );
emit( self.applied_transaction, trace );
emit( self.applied_transaction, std::tie(trace, dtrx) );

trx_context.squash();
undo_session.squash();
Expand Down Expand Up @@ -1269,7 +1269,7 @@ struct controller_impl {
trace = error_trace;
if( !trace->except_ptr ) {
emit( self.accepted_transaction, trx );
emit( self.applied_transaction, trace );
emit( self.applied_transaction, std::tie(trace, dtrx) );
undo_session.squash();
return trace;
}
Expand Down Expand Up @@ -1306,12 +1306,12 @@ struct controller_impl {
trace->receipt = push_receipt(gtrx.trx_id, transaction_receipt::hard_fail, cpu_time_to_bill_us, 0);

emit( self.accepted_transaction, trx );
emit( self.applied_transaction, trace );
emit( self.applied_transaction, std::tie(trace, dtrx) );

undo_session.squash();
} else {
emit( self.accepted_transaction, trx );
emit( self.applied_transaction, trace );
emit( self.applied_transaction, std::tie(trace, dtrx) );
}

return trace;
Expand Down Expand Up @@ -1421,7 +1421,7 @@ struct controller_impl {
emit( self.accepted_transaction, trx);
}

emit(self.applied_transaction, trace);
emit(self.applied_transaction, std::tie(trace, trn));


if ( read_mode != db_read_mode::SPECULATIVE && pending->_block_status == controller::block_status::incomplete ) {
Expand All @@ -1446,7 +1446,7 @@ struct controller_impl {
}

emit( self.accepted_transaction, trx );
emit( self.applied_transaction, trace );
emit( self.applied_transaction, std::tie(trace, trn) );

return trace;
} FC_CAPTURE_AND_RETHROW((trace))
Expand Down 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
4 changes: 3 additions & 1 deletion 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 Expand Up @@ -311,7 +313,7 @@ namespace eosio { namespace chain {
signal<void(const block_state_ptr&)> irreversible_block;
signal<void(const block_state_ptr&)> new_irreversible_block;
signal<void(const transaction_metadata_ptr&)> accepted_transaction;
signal<void(const transaction_trace_ptr&)> applied_transaction;
signal<void(std::tuple<const transaction_trace_ptr&, const signed_transaction&>)> applied_transaction;
signal<void(const header_confirmation&)> accepted_confirmation;
signal<void(const int&)> bad_alloc;

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/chain_plugin/chain_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -829,8 +829,8 @@ void chain_plugin::plugin_initialize(const variables_map& options) {
} );

my->applied_transaction_connection = my->chain->applied_transaction.connect(
[this]( const transaction_trace_ptr& trace ) {
my->applied_transaction_channel.publish( trace );
[this]( std::tuple<const transaction_trace_ptr&, const signed_transaction&> t ) {
my->applied_transaction_channel.publish( std::get<0>(t) );
} );

my->accepted_confirmation_connection = my->chain->accepted_confirmation.connect(
Expand Down
4 changes: 2 additions & 2 deletions plugins/history_plugin/history_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ namespace eosio {
db.add_index<public_key_history_multi_index>();

my->applied_transaction_connection.emplace(
chain.applied_transaction.connect( [&]( const transaction_trace_ptr& p ) {
my->on_applied_transaction( p );
chain.applied_transaction.connect( [&]( std::tuple<const transaction_trace_ptr&, const signed_transaction&> t ) {
my->on_applied_transaction( std::get<0>(t) );
} ));
} FC_LOG_AND_RETHROW()
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/kafka_plugin/kafka_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ void kafka_plugin::plugin_initialize(const variables_map& options) {
}
handle([=] { kafka_->push_block(b, true); }, "push irreversible block");
});
transaction_conn_ = chain.applied_transaction.connect([=](const chain::transaction_trace_ptr& t) {
transaction_conn_ = chain.applied_transaction.connect([=]( std::tuple<const chain::transaction_trace_ptr&, const chain::signed_transaction&> t) {
if (not start_sync_) return;
handle([=] { kafka_->push_transaction_trace(t); }, "push transaction");
handle([=] { kafka_->push_transaction_trace(std::get<0>(t)); }, "push transaction");
});
}

Expand Down
4 changes: 2 additions & 2 deletions plugins/mongo_db_plugin/mongo_db_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1616,8 +1616,8 @@ void mongo_db_plugin::plugin_initialize(const variables_map& options)
my->accepted_transaction( t );
} ));
my->applied_transaction_connection.emplace(
chain.applied_transaction.connect( [&]( const chain::transaction_trace_ptr& t ) {
my->applied_transaction( t );
chain.applied_transaction.connect( [&]( std::tuple<const chain::transaction_trace_ptr&, const chain::signed_transaction&> t ) {
my->applied_transaction( std::get<0>(t) );
} ));

if( my->wipe_database_on_startup ) {
Expand Down
4 changes: 2 additions & 2 deletions plugins/notify_plugin/notify_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ void notify_plugin::plugin_initialize(const variables_map &options)
}));

my->applied_tx_conn.emplace(chain.applied_transaction.connect(
[&](const transaction_trace_ptr &tx) {
my->on_applied_tx(tx);
[&](std::tuple<const chain::transaction_trace_ptr&, const chain::signed_transaction&> t) {
my->on_applied_tx(std::get<0>(t));
}));
}
FC_LOG_AND_RETHROW()
Expand Down
Loading

0 comments on commit dc3b8e4

Please sign in to comment.