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

Fix block size corner case on generation #2637

Merged
merged 2 commits into from
Jul 13, 2018
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
60 changes: 31 additions & 29 deletions libraries/chain/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -834,12 +834,39 @@ signed_block database::_generate_block(
if( !(skip & skip_witness_signature) )
FC_ASSERT( witness_obj.signing_key == block_signing_private_key.get_public_key() );

static const size_t max_block_header_size = fc::raw::pack_size( signed_block_header() ) + 4;
auto maximum_block_size = get_dynamic_global_properties().maximum_block_size; //STEEM_MAX_BLOCK_SIZE;
size_t total_block_size = max_block_header_size;

signed_block pending_block;

pending_block.previous = head_block_id();
pending_block.timestamp = when;
pending_block.witness = witness_owner;

if( has_hardfork( STEEM_HARDFORK_0_5__54 ) )
{
const auto& witness = get_witness( witness_owner );

if( witness.running_version != STEEM_BLOCKCHAIN_VERSION )
pending_block.extensions.insert( block_header_extensions( STEEM_BLOCKCHAIN_VERSION ) );

const auto& hfp = get_hardfork_property_object();

if( hfp.current_hardfork_version < STEEM_BLOCKCHAIN_VERSION // Binary is newer hardfork than has been applied
&& ( witness.hardfork_version_vote != _hardfork_versions[ hfp.last_hardfork + 1 ] || witness.hardfork_time_vote != _hardfork_times[ hfp.last_hardfork + 1 ] ) ) // Witness vote does not match binary configuration
{
// Make vote match binary configuration
pending_block.extensions.insert( block_header_extensions( hardfork_version_vote( _hardfork_versions[ hfp.last_hardfork + 1 ], _hardfork_times[ hfp.last_hardfork + 1 ] ) ) );
}
else if( hfp.current_hardfork_version == STEEM_BLOCKCHAIN_VERSION // Binary does not know of a new hardfork
&& witness.hardfork_version_vote > STEEM_BLOCKCHAIN_VERSION ) // Voting for hardfork in the future, that we do not know of...
{
// Make vote match binary configuration. This is vote to not apply the new hardfork.
pending_block.extensions.insert( block_header_extensions( hardfork_version_vote( _hardfork_versions[ hfp.last_hardfork ], _hardfork_times[ hfp.last_hardfork ] ) ) );
}
}

// The 4 is for the max size of the transaction vector length
size_t total_block_size = fc::raw::pack_size( pending_block ) + 4;
auto maximum_block_size = get_dynamic_global_properties().maximum_block_size; //STEEM_MAX_BLOCK_SIZE;

//
// The following code throws away existing pending_tx_session and
// rebuilds it by re-applying pending transactions.
Expand Down Expand Up @@ -902,32 +929,7 @@ signed_block database::_generate_block(
// However, the push_block() call below will re-create the
// _pending_tx_session.

pending_block.previous = head_block_id();
pending_block.timestamp = when;
pending_block.transaction_merkle_root = pending_block.calculate_merkle_root();
pending_block.witness = witness_owner;
if( has_hardfork( STEEM_HARDFORK_0_5__54 ) )
{
const auto& witness = get_witness( witness_owner );

if( witness.running_version != STEEM_BLOCKCHAIN_VERSION )
pending_block.extensions.insert( block_header_extensions( STEEM_BLOCKCHAIN_VERSION ) );

const auto& hfp = get_hardfork_property_object();

if( hfp.current_hardfork_version < STEEM_BLOCKCHAIN_VERSION // Binary is newer hardfork than has been applied
&& ( witness.hardfork_version_vote != _hardfork_versions[ hfp.last_hardfork + 1 ] || witness.hardfork_time_vote != _hardfork_times[ hfp.last_hardfork + 1 ] ) ) // Witness vote does not match binary configuration
{
// Make vote match binary configuration
pending_block.extensions.insert( block_header_extensions( hardfork_version_vote( _hardfork_versions[ hfp.last_hardfork + 1 ], _hardfork_times[ hfp.last_hardfork + 1 ] ) ) );
}
else if( hfp.current_hardfork_version == STEEM_BLOCKCHAIN_VERSION // Binary does not know of a new hardfork
&& witness.hardfork_version_vote > STEEM_BLOCKCHAIN_VERSION ) // Voting for hardfork in the future, that we do not know of...
{
// Make vote match binary configuration. This is vote to not apply the new hardfork.
pending_block.extensions.insert( block_header_extensions( hardfork_version_vote( _hardfork_versions[ hfp.last_hardfork ], _hardfork_times[ hfp.last_hardfork ] ) ) );
}
}

if( !(skip & skip_witness_signature) )
pending_block.sign( block_signing_private_key );
Expand Down
53 changes: 53 additions & 0 deletions tests/tests/block_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,5 +807,58 @@ BOOST_FIXTURE_TEST_CASE( hardfork_test, database_fixture )
FC_LOG_AND_RETHROW()
}

BOOST_FIXTURE_TEST_CASE( generate_block_size, clean_database_fixture )
{
try
{
db_plugin->debug_update( [=]( database& db )
{
db.modify( db.get_dynamic_global_properties(), [&]( dynamic_global_property_object& gpo )
{
gpo.maximum_block_size = STEEM_MIN_BLOCK_SIZE_LIMIT;
});
});
generate_block();

signed_transaction tx;
tx.set_expiration( db->head_block_time() + STEEM_MAX_TIME_UNTIL_EXPIRATION );

transfer_operation op;
op.from = STEEM_INIT_MINER_NAME;
op.to = STEEM_TEMP_ACCOUNT;
op.amount = asset( 1000, STEEM_SYMBOL );

// tx minus op is 79 bytes
// op is 33 bytes (32 for op + 1 byte static variant tag)
// total is 65254
// Original generation logic only allowed 115 bytes for the header
// We are targetting a size (minus header) of 65421 which creates a block of "size" 65535
// This block will actually be larger because the header estimates is too small

for( size_t i = 0; i < 1975; i++ )
{
tx.operations.push_back( op );
}

sign( tx, init_account_priv_key );
db->push_transaction( tx, 0 );

// Second transaction, tx minus op is 78 (one less byte for operation vector size)
// We need a 88 byte op. We need a 22 character memo (1 byte for length) 55 = 32 (old op) + 55 + 1
op.memo = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123";
tx.clear();
tx.operations.push_back( op );
sign( tx, init_account_priv_key );
db->push_transaction( tx, 0 );

generate_block();

// The last transfer should have been delayed due to size
auto head_block = db->fetch_block_by_number( db->head_block_num() );
BOOST_REQUIRE( head_block->transactions.size() == 1 );
}
FC_LOG_AND_RETHROW()
}

BOOST_AUTO_TEST_SUITE_END()
#endif