diff --git a/libraries/plugins/account_history/account_history_plugin.cpp b/libraries/plugins/account_history/account_history_plugin.cpp index 56d1f2787e..a5f90ea098 100644 --- a/libraries/plugins/account_history/account_history_plugin.cpp +++ b/libraries/plugins/account_history/account_history_plugin.cpp @@ -64,6 +64,8 @@ class account_history_plugin_impl account_history_plugin& _self; flat_set _tracked_accounts; + bool _partial_operations = false; + primary_index< simple_index< operation_history_object > >* _oho_index; }; account_history_plugin_impl::~account_history_plugin_impl() @@ -77,18 +79,35 @@ void account_history_plugin_impl::update_account_histories( const signed_block& const vector >& hist = db.get_applied_operations(); for( const optional< operation_history_object >& o_op : hist ) { - // add to the operation history index - const auto& oho = db.create( [&]( operation_history_object& h ) - { - if( o_op.valid() ) - h = *o_op; - } ); + optional oho; + + auto create_oho = [&]() { + return optional( db.create( [&]( operation_history_object& h ) + { + if( o_op.valid() ) + h = *o_op; + } ) ); + }; - if( !o_op.valid() ) + if (_partial_operations) + { + if( !o_op.valid() ) + { + _oho_index->use_next_id(); + continue; + } + } + else { - ilog( "removing failed operation with ID: ${id}", ("id", oho.id) ); - db.remove( oho ); - continue; + // add to the operation history index + oho = create_oho(); + + if( !o_op.valid() ) + { + ilog( "removing failed operation with ID: ${id}", ("id", oho->id) ); + db.remove( *oho ); + continue; + } } const operation_history_object& op = *o_op; @@ -99,7 +118,10 @@ void account_history_plugin_impl::update_account_histories( const signed_block& operation_get_required_authorities( op.op, impacted, impacted, other ); if( op.op.which() == operation::tag< account_create_operation >::value ) - impacted.insert( oho.result.get() ); + { + if (!oho.valid()) { oho = create_oho(); } + impacted.insert( oho->result.get() ); + } else graphene::app::operation_get_impacted_accounts( op.op, impacted ); @@ -110,6 +132,7 @@ void account_history_plugin_impl::update_account_histories( const signed_block& // for each operation this account applies to that is in the config link it into the history if( _tracked_accounts.size() == 0 ) { + if (!impacted.empty() && !oho.valid()) { oho = create_oho(); } for( auto& account_id : impacted ) { // we don't do index_account_keys here anymore, because @@ -118,7 +141,7 @@ void account_history_plugin_impl::update_account_histories( const signed_block& // add history const auto& stats_obj = account_id(db).statistics(db); const auto& ath = db.create( [&]( account_transaction_history_object& obj ){ - obj.operation_id = oho.id; + obj.operation_id = oho->id; obj.account = account_id; obj.sequence = stats_obj.total_ops+1; obj.next = stats_obj.most_recent_op; @@ -135,10 +158,11 @@ void account_history_plugin_impl::update_account_histories( const signed_block& { if( impacted.find( account_id ) != impacted.end() ) { + if (!oho.valid()) { oho = create_oho(); } // add history const auto& stats_obj = account_id(db).statistics(db); const auto& ath = db.create( [&]( account_transaction_history_object& obj ){ - obj.operation_id = oho.id; + obj.operation_id = oho->id; obj.account = account_id; obj.sequence = stats_obj.total_ops+1; obj.next = stats_obj.most_recent_op; @@ -150,6 +174,8 @@ void account_history_plugin_impl::update_account_histories( const signed_block& } } } + if (_partial_operations && ! oho.valid()) + _oho_index->use_next_id(); } } } // end namespace detail @@ -180,6 +206,7 @@ void account_history_plugin::plugin_set_program_options( { cli.add_options() ("track-account", boost::program_options::value>()->composing()->multitoken(), "Account ID to track history for (may specify multiple times)") + ("partial-operations", boost::program_options::value(), "Keep only those operations in memory that are related to account history tracking") ; cfg.add(cli); } @@ -187,10 +214,13 @@ void account_history_plugin::plugin_set_program_options( void account_history_plugin::plugin_initialize(const boost::program_options::variables_map& options) { database().applied_block.connect( [&]( const signed_block& b){ my->update_account_histories(b); } ); - database().add_index< primary_index< simple_index< operation_history_object > > >(); + my->_oho_index = database().add_index< primary_index< simple_index< operation_history_object > > >(); database().add_index< primary_index< account_transaction_history_index > >(); LOAD_VALUE_SET(options, "track-account", my->_tracked_accounts, graphene::chain::account_id_type); + if (options.count("partial-operations")) { + my->_partial_operations = options["partial-operations"].as(); + } } void account_history_plugin::plugin_startup()