Skip to content

Commit

Permalink
Protect the case where we have yet to begin tracking steemit#2458
Browse files Browse the repository at this point in the history
  • Loading branch information
sgerbino committed Oct 23, 2018
1 parent 7be5735 commit 6a6d3c2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,59 +24,64 @@ class transaction_status_api_impl

DEFINE_API_IMPL( transaction_status_api_impl, find_transaction )
{
auto last_irreversible_block_num = _db.get_dynamic_global_properties().last_irreversible_block_num;
auto tso = _db.find< transaction_status::transaction_status_object, transaction_status::by_trx_id >( args.transaction_id );
uint32_t earliest_tracked_block_num = _tsp.earliest_tracked_block_num();

// If we are actively tracking this transaction
if ( tso != nullptr)
// Have we begun tracking?
if ( _db.head_block_num() >= earliest_tracked_block_num )
{
// If we're not within a block
if ( !tso->block_num )
return {
.status = transaction_status::within_mempool
};

// If we're in an irreversible block
if ( tso->block_num <= last_irreversible_block_num )
return {
.status = transaction_status::within_irreversible_block,
.block_num = tso->block_num
};
// We're in a reversible block
else
return {
.status = transaction_status::within_reversible_block,
.block_num = tso->block_num
};
}
auto last_irreversible_block_num = _db.get_dynamic_global_properties().last_irreversible_block_num;
auto tso = _db.find< transaction_status::transaction_status_object, transaction_status::by_trx_id >( args.transaction_id );

// If the user has provided us with an expiration
if ( args.expiration.valid() )
{
const auto& expiration = *args.expiration;

// Check if the expiration is before our earliest tracked block
uint32_t earliest_tracked_block_num = _tsp.earliest_tracked_block_num();
auto earliest_tracked_block = _db.fetch_block_by_number( earliest_tracked_block_num );
if ( expiration < earliest_tracked_block->timestamp + STEEM_MAX_TIME_UNTIL_EXPIRATION )
return {
.status = transaction_status::too_old
};

// If the expiration is on or before our last irreversible block
if ( last_irreversible_block_num > 0 )
// If we are actively tracking this transaction
if ( tso != nullptr)
{
// If we're not within a block
if ( !tso->block_num )
return {
.status = transaction_status::within_mempool
};

// If we're in an irreversible block
if ( tso->block_num <= last_irreversible_block_num )
return {
.status = transaction_status::within_irreversible_block,
.block_num = tso->block_num
};
// We're in a reversible block
else
return {
.status = transaction_status::within_reversible_block,
.block_num = tso->block_num
};
}

// If the user has provided us with an expiration
if ( args.expiration.valid() )
{
auto last_irreversible_block = _db.fetch_block_by_number( last_irreversible_block_num );
if ( expiration <= last_irreversible_block->timestamp )
const auto& expiration = *args.expiration;

// Check if the expiration is before our earliest tracked block
auto earliest_tracked_block = _db.fetch_block_by_number( earliest_tracked_block_num );
if ( expiration < earliest_tracked_block->timestamp + STEEM_MAX_TIME_UNTIL_EXPIRATION )
return {
.status = transaction_status::too_old
};

// If the expiration is on or before our last irreversible block
if ( last_irreversible_block_num > 0 )
{
auto last_irreversible_block = _db.fetch_block_by_number( last_irreversible_block_num );
if ( expiration <= last_irreversible_block->timestamp )
return {
.status = transaction_status::expired_irreversible
};
}
// If our expiration is in the past
if ( expiration < _db.head_block_time() )
return {
.status = transaction_status::expired_irreversible
.status = transaction_status::expired_reversible
};
}
// If our expiration is in the past
if ( expiration < _db.head_block_time() )
return {
.status = transaction_status::expired_reversible
};
}

// Either the user did not provide an expiration or it is not expired and we didn't hear about this transaction
Expand Down
4 changes: 4 additions & 0 deletions tests/plugin_tests/transaction_status.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ BOOST_AUTO_TEST_CASE( transaction_status_functionality_test )
BOOST_REQUIRE( api_return.status == unknown );
BOOST_REQUIRE( api_return.block_num.valid() == false );

api_return = tx_status_api->api->find_transaction( { .transaction_id = tx0.id(), tx0_expiration } );
BOOST_REQUIRE( api_return.status == unknown );
BOOST_REQUIRE( api_return.block_num.valid() == false );

generate_blocks( TRANSCATION_STATUS_TRACK_AFTER_BLOCK - db->head_block_num() );

signed_transaction tx1;
Expand Down

0 comments on commit 6a6d3c2

Please sign in to comment.