Skip to content

Commit

Permalink
Merge pull request #289 from pmconrad/partial_op_history
Browse files Browse the repository at this point in the history
Added partial-operations flag to reduce operations log (1.11.x) as suggested in #192
  • Loading branch information
abitmore authored May 27, 2017
2 parents 9d2e5ca + 5327159 commit 19f0ec5
Showing 1 changed file with 44 additions and 14 deletions.
58 changes: 44 additions & 14 deletions libraries/plugins/account_history/account_history_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class account_history_plugin_impl

account_history_plugin& _self;
flat_set<account_id_type> _tracked_accounts;
bool _partial_operations = false;
primary_index< simple_index< operation_history_object > >* _oho_index;
};

account_history_plugin_impl::~account_history_plugin_impl()
Expand All @@ -77,18 +79,35 @@ void account_history_plugin_impl::update_account_histories( const signed_block&
const vector<optional< operation_history_object > >& 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>( [&]( operation_history_object& h )
{
if( o_op.valid() )
h = *o_op;
} );
optional<operation_history_object> oho;

auto create_oho = [&]() {
return optional<operation_history_object>( db.create<operation_history_object>( [&]( 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;
Expand All @@ -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<object_id_type>() );
{
if (!oho.valid()) { oho = create_oho(); }
impacted.insert( oho->result.get<object_id_type>() );
}
else
graphene::app::operation_get_impacted_accounts( op.op, impacted );

Expand All @@ -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
Expand All @@ -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>( [&]( 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;
Expand All @@ -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>( [&]( 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;
Expand All @@ -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
Expand Down Expand Up @@ -180,17 +206,21 @@ void account_history_plugin::plugin_set_program_options(
{
cli.add_options()
("track-account", boost::program_options::value<std::vector<std::string>>()->composing()->multitoken(), "Account ID to track history for (may specify multiple times)")
("partial-operations", boost::program_options::value<bool>(), "Keep only those operations in memory that are related to account history tracking")
;
cfg.add(cli);
}

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<bool>();
}
}

void account_history_plugin::plugin_startup()
Expand Down

0 comments on commit 19f0ec5

Please sign in to comment.