Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add block_time and is_virtual fields to operation_history_object #2642

Merged
merged 5 commits into from
Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions libraries/chain/db_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ processed_transaction database::push_proposal(const proposal_object& proposal)
_undo_db.set_max_size( _undo_db.size() + 1 );
auto session = _undo_db.start_undo_session(true);
for( auto& op : proposal.proposed_transaction.operations )
eval_state.operation_results.emplace_back(apply_operation(eval_state, op));
eval_state.operation_results.emplace_back(apply_operation(eval_state, op)); // This is a virtual operation
// Make sure there is no unpaid samet fund debt
const auto& samet_fund_idx = get_index_type<samet_fund_index>().indices().get<by_unpaid>();
FC_ASSERT( samet_fund_idx.empty() || samet_fund_idx.begin()->unpaid_amount == 0,
Expand Down Expand Up @@ -532,14 +532,11 @@ void database::clear_pending()
_pending_tx_session.reset();
} FC_CAPTURE_AND_RETHROW() }

uint32_t database::push_applied_operation( const operation& op )
uint32_t database::push_applied_operation( const operation& op, bool is_virtual /* = true */ )
{
_applied_ops.emplace_back(op);
operation_history_object& oh = *(_applied_ops.back());
oh.block_num = _current_block_num;
oh.trx_in_block = _current_trx_in_block;
oh.op_in_trx = _current_op_in_trx;
oh.virtual_op = _current_virtual_op++;
_applied_ops.emplace_back( operation_history_object( op, _current_block_num, _current_trx_in_block,
_current_op_in_trx, _current_virtual_op, is_virtual, _current_block_time ) );
++_current_virtual_op;
return _applied_ops.size() - 1;
}
void database::set_applied_operation_result( uint32_t op_id, const operation_result& result )
Expand Down Expand Up @@ -758,7 +755,7 @@ processed_transaction database::_apply_transaction(const signed_transaction& trx
for( const auto& op : ptrx.operations )
{
_current_virtual_op = 0;
eval_state.operation_results.emplace_back(apply_operation(eval_state, op));
eval_state.operation_results.emplace_back(apply_operation(eval_state, op, false)); // This is NOT a virtual op
++_current_op_in_trx;
}
ptrx.operation_results = std::move(eval_state.operation_results);
Expand All @@ -771,15 +768,16 @@ processed_transaction database::_apply_transaction(const signed_transaction& trx
return ptrx;
} FC_CAPTURE_AND_RETHROW( (trx) ) }

operation_result database::apply_operation(transaction_evaluation_state& eval_state, const operation& op)
operation_result database::apply_operation( transaction_evaluation_state& eval_state, const operation& op,
bool is_virtual /* = true */ )
{ try {
int i_which = op.which();
uint64_t u_which = uint64_t( i_which );
FC_ASSERT( i_which >= 0, "Negative operation tag in operation ${op}", ("op",op) );
FC_ASSERT( u_which < _operation_evaluators.size(), "No registered evaluator for operation ${op}", ("op",op) );
unique_ptr<op_evaluator>& eval = _operation_evaluators[ u_which ];
FC_ASSERT( eval, "No registered evaluator for operation ${op}", ("op",op) );
auto op_id = push_applied_operation( op );
auto op_id = push_applied_operation( op, is_virtual );
auto result = eval->evaluate( eval_state, op, true );
set_applied_operation_result( op_id, result );
return result;
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/include/graphene/chain/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

#define GRAPHENE_MAX_NESTED_OBJECTS (200)

const std::string GRAPHENE_CURRENT_DB_VERSION = "20220913";
const std::string GRAPHENE_CURRENT_DB_VERSION = "20220916";

#define GRAPHENE_RECENTLY_MISSED_COUNT_INCREMENT 4
#define GRAPHENE_RECENTLY_MISSED_COUNT_DECREMENT 3
Expand Down
7 changes: 5 additions & 2 deletions libraries/chain/include/graphene/chain/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,10 +585,12 @@ namespace graphene { namespace chain {
* as any implied/virtual operations that resulted, such as filling an order. The
* applied operations is cleared after applying each block and calling the block
* observers which may want to index these operations.
* @param The operation to push
* @param is_virtual Whether the operation is a virtual operation
*
* @return the op_id which can be used to set the result after it has finished being applied.
*/
uint32_t push_applied_operation( const operation& op );
uint32_t push_applied_operation( const operation& op, bool is_virtual = true );
void set_applied_operation_result( uint32_t op_id, const operation_result& r );
const vector<optional< operation_history_object > >& get_applied_operations()const;

Expand Down Expand Up @@ -681,7 +683,8 @@ namespace graphene { namespace chain {
// these were formerly private, but they have a fairly well-defined API, so let's make them public
void apply_block( const signed_block& next_block, uint32_t skip = skip_nothing );
processed_transaction apply_transaction( const signed_transaction& trx, uint32_t skip = skip_nothing );
operation_result apply_operation( transaction_evaluation_state& eval_state, const operation& op );
operation_result apply_operation( transaction_evaluation_state& eval_state, const operation& op,
bool is_virtual = true );

private:
void _apply_block( const signed_block& next_block );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ namespace graphene { namespace chain {
static constexpr uint8_t space_id = protocol_ids;
static constexpr uint8_t type_id = operation_history_object_type;

operation_history_object( const operation& o ):op(o){}
explicit operation_history_object( const operation& o ):op(o){}
operation_history_object(){}
operation_history_object( const operation& o, uint32_t bn, uint16_t tib, uint16_t oit, uint32_t vo, bool iv,
const time_point_sec& bt )
: op(o), block_num(bn), trx_in_block(tib), op_in_trx(oit), virtual_op(vo), is_virtual(iv), block_time(bt) {}

operation op;
operation_result result;
Expand All @@ -64,6 +67,10 @@ namespace graphene { namespace chain {
uint16_t op_in_trx = 0;
/** any virtual operations implied by operation in block */
uint32_t virtual_op = 0;
/** Whether this is a virtual operation */
bool is_virtual = false;
/** The timestamp of the block that caused this operation */
time_point_sec block_time;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/small_objects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ FC_REFLECT_DERIVED_NO_TYPENAME( graphene::chain::htlc_object, (graphene::db::obj
(transfer) (conditions) (memo) )

FC_REFLECT_DERIVED_NO_TYPENAME( graphene::chain::operation_history_object, (graphene::chain::object),
(op)(result)(block_num)(trx_in_block)(op_in_trx)(virtual_op) )
(op)(result)(block_num)(trx_in_block)(op_in_trx)(virtual_op)(is_virtual)(block_time) )

FC_REFLECT_DERIVED_NO_TYPENAME( graphene::chain::account_history_object, (graphene::chain::object),
(account)(operation_id)(sequence)(next) )
Expand Down
2 changes: 2 additions & 0 deletions libraries/plugins/account_history/account_history_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ void account_history_plugin_impl::update_account_histories( const signed_block&
h.trx_in_block = o_op->trx_in_block;
h.op_in_trx = o_op->op_in_trx;
h.virtual_op = o_op->virtual_op;
h.is_virtual = o_op->is_virtual;
h.block_time = o_op->block_time;
}
} ) );
};
Expand Down
2 changes: 2 additions & 0 deletions libraries/plugins/elasticsearch/elasticsearch_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ void elasticsearch_plugin_impl::update_account_histories( const signed_block& b
h.trx_in_block = o_op->trx_in_block;
h.op_in_trx = o_op->op_in_trx;
h.virtual_op = o_op->virtual_op;
h.is_virtual = o_op->is_virtual;
h.block_time = o_op->block_time;
}
}));
};
Expand Down
34 changes: 34 additions & 0 deletions tests/tests/history_api_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ BOOST_AUTO_TEST_CASE(get_account_history) {
BOOST_CHECK_EQUAL(histories.size(), 3u);
BOOST_CHECK_EQUAL(histories[2].id.instance(), 0u);
BOOST_CHECK_EQUAL(histories[2].op.which(), asset_create_op_id);
BOOST_CHECK( histories[2].block_time == db.head_block_time() );
BOOST_CHECK( !histories[2].is_virtual );

// 1 account_create op larger than id1
histories = hist_api.get_account_history("1.2.0", operation_history_id_type(1),
Expand Down Expand Up @@ -100,6 +102,38 @@ BOOST_AUTO_TEST_CASE(get_account_history) {
}
}

BOOST_AUTO_TEST_CASE(get_account_history_virtual_operation_test) {
try {
graphene::app::history_api hist_api(app);

asset_id_type usd_id = create_user_issued_asset("USD").id;

ACTORS( (dan)(bob) );
fund( dan, asset(100) );
issue_uia( bob_id, asset(100, usd_id) );

create_sell_order( dan_id, asset(100), asset(100, usd_id) );
create_sell_order( bob_id, asset(100, usd_id), asset(100) );

generate_block();
fc::usleep(fc::milliseconds(200));

auto fill_order_op_id = operation::tag<fill_order_operation>::value;

vector<operation_history_object> histories = hist_api.get_account_history("dan", operation_history_id_type(),
100, operation_history_id_type());

BOOST_REQUIRE_GT( histories.size(), 0 );
BOOST_CHECK_EQUAL( histories.front().op.which(), fill_order_op_id );
BOOST_CHECK( histories.front().block_time == db.head_block_time() );
BOOST_CHECK( histories.front().is_virtual );

} catch (fc::exception &e) {
edump((e.to_detail_string()));
throw;
}
}

BOOST_AUTO_TEST_CASE(get_account_history_notify_all_on_creation) {
try {
// Pass hard fork time
Expand Down