Skip to content

Commit

Permalink
Remove block generation implementation from database #2717
Browse files Browse the repository at this point in the history
  • Loading branch information
sgerbino committed Sep 5, 2018
1 parent 576a3b8 commit fa575fc
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 170 deletions.
158 changes: 0 additions & 158 deletions libraries/chain/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -813,164 +813,6 @@ void database::_push_transaction( const signed_transaction& trx )
temp_session.squash();
}

signed_block database::generate_block(
fc::time_point_sec when,
const account_name_type& witness_owner,
const fc::ecc::private_key& block_signing_private_key,
uint32_t skip /* = 0 */
)
{
signed_block result;
detail::with_skip_flags( *this, skip, [&]()
{
try
{
result = _generate_block( when, witness_owner, block_signing_private_key );
}
FC_CAPTURE_AND_RETHROW( (witness_owner) )
});
return result;
}


signed_block database::_generate_block(
fc::time_point_sec when,
const account_name_type& witness_owner,
const fc::ecc::private_key& block_signing_private_key
)
{
uint32_t skip = get_node_properties().skip_flags;
uint32_t slot_num = get_slot_at_time( when );
FC_ASSERT( slot_num > 0 );
string scheduled_witness = get_scheduled_witness( slot_num );
FC_ASSERT( scheduled_witness == witness_owner );

const auto& witness_obj = get_witness( witness_owner );

if( !(skip & skip_witness_signature) )
FC_ASSERT( witness_obj.signing_key == block_signing_private_key.get_public_key() );

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.
//
// This rebuild is necessary because pending transactions' validity
// and semantics may have changed since they were received, because
// time-based semantics are evaluated based on the current block
// time. These changes can only be reflected in the database when
// the value of the "when" variable is known, which means we need to
// re-apply pending transactions in this method.
//
_pending_tx_session.reset();
_pending_tx_session = start_undo_session();

FC_TODO( "Safe to remove after HF20 occurs because no more pre HF20 blocks will be generated" );
if( has_hardfork( STEEM_HARDFORK_0_20 ) )
{
/// modify current witness so transaction evaluators can know who included the transaction
modify( get_dynamic_global_properties(), [&]( dynamic_global_property_object& dgp )
{
dgp.current_witness = scheduled_witness;
});
}

uint64_t postponed_tx_count = 0;
// pop pending state (reset to head block state)
for( const signed_transaction& tx : _pending_tx )
{
// Only include transactions that have not expired yet for currently generating block,
// this should clear problem transactions and allow block production to continue

if( tx.expiration < when )
continue;

uint64_t new_total_size = total_block_size + fc::raw::pack_size( tx );

// postpone transaction if it would make block too big
if( new_total_size >= maximum_block_size )
{
postponed_tx_count++;
continue;
}

try
{
auto temp_session = start_undo_session();
_apply_transaction( tx );
temp_session.squash();

total_block_size += fc::raw::pack_size( tx );
pending_block.transactions.push_back( tx );
}
catch ( const fc::exception& e )
{
// Do nothing, transaction will not be re-applied
//wlog( "Transaction was not processed while generating block due to ${e}", ("e", e) );
//wlog( "The transaction was ${t}", ("t", tx) );
}
}
if( postponed_tx_count > 0 )
{
wlog( "Postponed ${n} transactions due to block size limit", ("n", postponed_tx_count) );
}

_pending_tx_session.reset();

// We have temporarily broken the invariant that
// _pending_tx_session is the result of applying _pending_tx, as
// _pending_tx now consists of the set of postponed transactions.
// However, the push_block() call below will re-create the
// _pending_tx_session.

pending_block.transaction_merkle_root = pending_block.calculate_merkle_root();

if( !(skip & skip_witness_signature) )
pending_block.sign( block_signing_private_key, has_hardfork( STEEM_HARDFORK_0_20__1944 ) ? fc::ecc::bip_0062 : fc::ecc::fc_canonical );

// TODO: Move this to _push_block() so session is restored.
if( !(skip & skip_block_size_check) )
{
FC_ASSERT( fc::raw::pack_size(pending_block) <= STEEM_MAX_BLOCK_SIZE );
}

push_block( pending_block, skip );

return pending_block;
}

/**
* Removes the most recent block from the database and
* undoes any changes it made.
Expand Down
12 changes: 0 additions & 12 deletions libraries/chain/include/steem/chain/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,6 @@ namespace steem { namespace chain {
bool _push_block( const signed_block& b );
void _push_transaction( const signed_transaction& trx );

signed_block generate_block(
const fc::time_point_sec when,
const account_name_type& witness_owner,
const fc::ecc::private_key& block_signing_private_key,
uint32_t skip
);
signed_block _generate_block(
const fc::time_point_sec when,
const account_name_type& witness_owner,
const fc::ecc::private_key& block_signing_private_key
);

void pop_block();
void clear_pending();

Expand Down

0 comments on commit fa575fc

Please sign in to comment.