Skip to content

Commit

Permalink
Test case to reproduce #125 block producing issue
Browse files Browse the repository at this point in the history
When signing key is updated in the same block, producing will fail.
  • Loading branch information
abitmore committed Jul 29, 2018
1 parent 57f7ee4 commit b8bafdd
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions tests/tests/block_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,114 @@ BOOST_AUTO_TEST_CASE( undo_block )
}
}

BOOST_AUTO_TEST_CASE( change_signing_key )
{
try {
fc::temp_directory data_dir( graphene::utilities::temp_directory_path() );
{
database db;
auto init_account_priv_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("null_key")) );
auto init_pub_key = init_account_priv_key.get_public_key();
auto new_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("new_key")) );
auto new_pub_key = new_key.get_public_key();

std::map< public_key_type, fc::ecc::private_key > key_map;
key_map[init_pub_key] = init_account_priv_key;
key_map[new_pub_key] = new_key;

auto change_signing_key = [&db,&init_account_priv_key]( witness_id_type wit, public_key_type new_signing_key ) {
witness_update_operation wuop;
wuop.witness_account = wit(db).witness_account;
wuop.witness = wit;
wuop.new_signing_key = new_signing_key;
signed_transaction wu_trx;
wu_trx.operations.push_back( wuop );
wu_trx.set_reference_block( db.head_block_id() );
wu_trx.set_expiration( db.head_block_time()
+ fc::seconds( 0x1000 * db.get_global_properties().parameters.block_interval ) );
wu_trx.sign( init_account_priv_key, db.get_chain_id() );
PUSH_TX( db, wu_trx, 0 );
};

// open database
db.open(data_dir.path(), make_genesis, "TEST");

// generate some empty blocks with init keys
for( uint32_t i = 0; i < 30; ++i )
{
auto now = db.get_slot_time(1);
auto next_witness = db.get_scheduled_witness( 1 );
db.generate_block( now, next_witness, init_account_priv_key, database::skip_nothing );
}

// generate some blocks and change keys in same block
for( uint32_t i = 0; i < 9; ++i )
{
auto now = db.get_slot_time(1);
auto next_witness = db.get_scheduled_witness( 1 );
public_key_type current_key = next_witness(db).signing_key;
change_signing_key( next_witness, new_key.get_public_key() );
idump( (i)(now)(next_witness) );
auto b = db.generate_block( now, next_witness, key_map[current_key], database::skip_nothing );
idump( (b) );
}

// pop a few blocks and clear pending, some signing keys should be changed back
for( uint32_t i = 0; i < 4; ++i )
{
db.pop_block();
}
db._popped_tx.clear();
db.clear_pending();

// generate a few blocks and change keys in same block
for( uint32_t i = 0; i < 2; ++i )
{
auto now = db.get_slot_time(1);
auto next_witness = db.get_scheduled_witness( 1 );
public_key_type current_key = next_witness(db).signing_key;
change_signing_key( next_witness, new_key.get_public_key() );
idump( (i)(now)(next_witness) );
auto b = db.generate_block( now, next_witness, key_map[current_key], database::skip_nothing );
idump( (b) );
}

// generate some blocks but don't change a key
for( uint32_t i = 0; i < 25; ++i )
{
auto now = db.get_slot_time(1);
auto next_witness = db.get_scheduled_witness( 1 );
public_key_type current_key = next_witness(db).signing_key;
idump( (i)(now)(next_witness) );
auto b = db.generate_block( now, next_witness, key_map[current_key], database::skip_nothing );
idump( (b) );
}

// close the database, flush all data to disk
db.close();

// reopen database, all data should be unchanged
db.open(data_dir.path(), make_genesis, "TEST");

// generate more blocks and change keys in same block
for( uint32_t i = 0; i < 25; ++i )
{
auto now = db.get_slot_time(1);
auto next_witness = db.get_scheduled_witness( 1 );
public_key_type current_key = next_witness(db).signing_key;
change_signing_key( next_witness, new_key.get_public_key() );
idump( (i)(now)(next_witness) );
auto b = db.generate_block( now, next_witness, key_map[current_key], database::skip_nothing );
idump( (b) );
}

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

BOOST_AUTO_TEST_CASE( fork_blocks )
{
try {
Expand Down

0 comments on commit b8bafdd

Please sign in to comment.