From 9b5bd12c67771a779e0042a67ff632e7e3ee4fbc Mon Sep 17 00:00:00 2001 From: Vikram Rajkumar Date: Tue, 7 Jul 2015 15:37:31 -0400 Subject: [PATCH] Add genesis_state_type::initial_timestamp; #17 --- libraries/chain/db_init.cpp | 7 +++++-- libraries/chain/db_witness_schedule.cpp | 3 --- .../chain/include/graphene/chain/config.hpp | 1 - .../include/graphene/chain/genesis_state.hpp | 2 +- tests/app/main.cpp | 3 ++- tests/common/database_fixture.cpp | 2 ++ tests/common/database_fixture.hpp | 4 ++-- tests/tests/block_tests.cpp | 19 +++++++++++-------- 8 files changed, 23 insertions(+), 18 deletions(-) diff --git a/libraries/chain/db_init.cpp b/libraries/chain/db_init.cpp index 03f24086..93be6dc9 100644 --- a/libraries/chain/db_init.cpp +++ b/libraries/chain/db_init.cpp @@ -138,6 +138,9 @@ void database::initialize_indexes() void database::init_genesis(const genesis_state_type& genesis_state) { try { + FC_ASSERT( genesis_state.initial_timestamp != time_point_sec(), "Must initialize genesis timestamp." ); + FC_ASSERT( genesis_state.initial_timestamp.sec_since_epoch() % GRAPHENE_DEFAULT_BLOCK_INTERVAL == 0, + "Genesis timestamp must be divisible by GRAPHENE_DEFAULT_BLOCK_INTERVAL." ); FC_ASSERT(genesis_state.initial_witness_candidates.size() > 0, "Cannot start a chain with zero witnesses."); FC_ASSERT(genesis_state.initial_active_witnesses <= genesis_state.initial_witness_candidates.size(), @@ -246,7 +249,7 @@ void database::init_genesis(const genesis_state_type& genesis_state) }); create( [&](dynamic_global_property_object& p) { - p.time = fc::time_point_sec(GRAPHENE_GENESIS_TIMESTAMP); + p.time = genesis_state.initial_timestamp; p.witness_budget = 0; }); create([&](block_summary_object&) {}); @@ -426,7 +429,7 @@ void database::init_genesis(const genesis_state_type& genesis_state) { worker_create_operation op; op.owner = get_account_id( worker.owner_name ); - op.work_begin_date = time_point_sec( GRAPHENE_GENESIS_TIMESTAMP ); + op.work_begin_date = genesis_state.initial_timestamp; op.work_end_date = time_point_sec::maximum(); op.daily_pay = worker.daily_pay; op.name = "Genesis-Worker-" + worker.owner_name; diff --git a/libraries/chain/db_witness_schedule.cpp b/libraries/chain/db_witness_schedule.cpp index a6d82bf6..98ce95dd 100644 --- a/libraries/chain/db_witness_schedule.cpp +++ b/libraries/chain/db_witness_schedule.cpp @@ -23,9 +23,6 @@ namespace graphene { namespace chain { -static_assert((GRAPHENE_GENESIS_TIMESTAMP % GRAPHENE_DEFAULT_BLOCK_INTERVAL) == 0, - "GRAPHENE_GENESIS_TIMESTAMP must be aligned to a block interval."); - pair database::get_scheduled_witness(uint32_t slot_num)const { if( slot_num == 0 ) diff --git a/libraries/chain/include/graphene/chain/config.hpp b/libraries/chain/include/graphene/chain/config.hpp index 33ae226c..aaa40b18 100644 --- a/libraries/chain/include/graphene/chain/config.hpp +++ b/libraries/chain/include/graphene/chain/config.hpp @@ -100,7 +100,6 @@ #define GRAPHENE_DEFAULT_FEE_LIQUIDATION_THRESHOLD GRAPHENE_BLOCKCHAIN_PRECISION * 100; #define GRAPHENE_DEFAULT_ACCOUNTS_PER_FEE_SCALE 1000 #define GRAPHENE_DEFAULT_ACCOUNT_FEE_SCALE_BITSHIFTS 4 -#define GRAPHENE_GENESIS_TIMESTAMP (1431700000) /// Should be divisible by GRAPHENE_DEFAULT_BLOCK_INTERVAL #define GRAPHENE_MAX_WORKER_NAME_LENGTH 63 diff --git a/libraries/chain/include/graphene/chain/genesis_state.hpp b/libraries/chain/include/graphene/chain/genesis_state.hpp index 16d95d59..2721fa95 100644 --- a/libraries/chain/include/graphene/chain/genesis_state.hpp +++ b/libraries/chain/include/graphene/chain/genesis_state.hpp @@ -124,6 +124,6 @@ FC_REFLECT(graphene::chain::genesis_state_type::initial_committee_member_type, ( FC_REFLECT(graphene::chain::genesis_state_type::initial_worker_type, (owner_name)(daily_pay)) FC_REFLECT(graphene::chain::genesis_state_type, - (initial_parameters)(initial_accounts)(initial_assets)(initial_balances) + (initial_timestamp)(initial_parameters)(initial_accounts)(initial_assets)(initial_balances) (initial_vesting_balances)(initial_active_witnesses)(initial_witness_candidates) (initial_committee_candidates)(initial_worker_candidates)) diff --git a/tests/app/main.cpp b/tests/app/main.cpp index c1d00537..086b0ba7 100644 --- a/tests/app/main.cpp +++ b/tests/app/main.cpp @@ -40,7 +40,8 @@ BOOST_AUTO_TEST_CASE( two_node_network ) fc::temp_directory app2_dir; fc::temp_file genesis_json; - fc::time_point_sec now( GRAPHENE_GENESIS_TIMESTAMP ); + // TODO: Time should be read from the blockchain + fc::time_point_sec now( 1431700000 ); graphene::app::application app1; app1.register_plugin(); diff --git a/tests/common/database_fixture.cpp b/tests/common/database_fixture.cpp index 9e9579c6..72ccccfe 100644 --- a/tests/common/database_fixture.cpp +++ b/tests/common/database_fixture.cpp @@ -60,6 +60,8 @@ database_fixture::database_fixture() mhplugin->plugin_set_app(&app); mhplugin->plugin_initialize(options); + genesis_state.initial_timestamp = time_point_sec( GRAPHENE_TESTING_GENESIS_TIMESTAMP ); + genesis_state.initial_active_witnesses = 10; for( int i = 0; i < genesis_state.initial_active_witnesses; ++i ) { diff --git a/tests/common/database_fixture.hpp b/tests/common/database_fixture.hpp index 75f6c5fb..834f9ee0 100644 --- a/tests/common/database_fixture.hpp +++ b/tests/common/database_fixture.hpp @@ -23,6 +23,8 @@ using namespace graphene::db; +#define GRAPHENE_TESTING_GENESIS_TIMESTAMP (1431700000) + #define PUSH_TX \ graphene::chain::test::_push_transaction @@ -102,8 +104,6 @@ struct database_fixture { fc::ecc::private_key delegate_priv_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("null_key")) ); public_key_type delegate_pub_key; - fc::time_point_sec genesis_time = fc::time_point_sec( GRAPHENE_GENESIS_TIMESTAMP ); - fc::time_point_sec now = fc::time_point_sec( GRAPHENE_GENESIS_TIMESTAMP ); optional data_dir; bool skip_key_index_test = false; uint32_t anon_acct_count; diff --git a/tests/tests/block_tests.cpp b/tests/tests/block_tests.cpp index ce3a5655..4c793ea6 100644 --- a/tests/tests/block_tests.cpp +++ b/tests/tests/block_tests.cpp @@ -36,6 +36,9 @@ using namespace graphene::chain; genesis_state_type make_genesis() { genesis_state_type genesis_state; + + genesis_state.initial_timestamp = time_point_sec( GRAPHENE_TESTING_GENESIS_TIMESTAMP ); + auto delegate_priv_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("null_key"))); genesis_state.initial_active_witnesses = 10; for( int i = 0; i < genesis_state.initial_active_witnesses; ++i ) @@ -117,7 +120,7 @@ BOOST_AUTO_TEST_CASE( block_database_test ) BOOST_AUTO_TEST_CASE( generate_empty_blocks ) { try { - fc::time_point_sec now( GRAPHENE_GENESIS_TIMESTAMP ); + fc::time_point_sec now( GRAPHENE_TESTING_GENESIS_TIMESTAMP ); fc::temp_directory data_dir; signed_block b; @@ -169,7 +172,7 @@ BOOST_AUTO_TEST_CASE( undo_block ) { database db; db.open(data_dir.path(), make_genesis() ); - fc::time_point_sec now( GRAPHENE_GENESIS_TIMESTAMP ); + fc::time_point_sec now( GRAPHENE_TESTING_GENESIS_TIMESTAMP ); auto delegate_priv_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("null_key")) ); for( uint32_t i = 0; i < 5; ++i ) @@ -205,7 +208,7 @@ BOOST_AUTO_TEST_CASE( fork_blocks ) try { fc::temp_directory data_dir1; fc::temp_directory data_dir2; - fc::time_point_sec now( GRAPHENE_GENESIS_TIMESTAMP ); + fc::time_point_sec now( GRAPHENE_TESTING_GENESIS_TIMESTAMP ); database db1; db1.open(data_dir1.path(), make_genesis()); @@ -269,7 +272,7 @@ BOOST_AUTO_TEST_CASE( fork_blocks ) BOOST_AUTO_TEST_CASE( undo_pending ) { try { - fc::time_point_sec now(GRAPHENE_GENESIS_TIMESTAMP); + fc::time_point_sec now(GRAPHENE_TESTING_GENESIS_TIMESTAMP); fc::temp_directory data_dir; { database db; @@ -334,7 +337,7 @@ BOOST_AUTO_TEST_CASE( switch_forks_undo_create ) db1.open(dir1.path(), make_genesis()); db2.open(dir2.path(), make_genesis()); - fc::time_point_sec now( GRAPHENE_GENESIS_TIMESTAMP ); + fc::time_point_sec now( GRAPHENE_TESTING_GENESIS_TIMESTAMP ); auto delegate_priv_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("null_key")) ); public_key_type delegate_pub_key = delegate_priv_key.get_public_key(); const graphene::db::index& account_idx = db1.get_index(protocol_ids, account_object_type); @@ -355,7 +358,7 @@ BOOST_AUTO_TEST_CASE( switch_forks_undo_create ) BOOST_CHECK(nathan_id(db1).name == "nathan"); - now = fc::time_point_sec( GRAPHENE_GENESIS_TIMESTAMP ); + now = fc::time_point_sec( GRAPHENE_TESTING_GENESIS_TIMESTAMP ); now += db2.block_interval(); b = db2.generate_block(now, db2.get_scheduled_witness(1).first, delegate_priv_key, database::skip_nothing); db1.push_block(b); @@ -384,7 +387,7 @@ BOOST_AUTO_TEST_CASE( switch_forks_undo_create ) BOOST_AUTO_TEST_CASE( duplicate_transactions ) { try { - fc::time_point_sec now( GRAPHENE_GENESIS_TIMESTAMP ); + fc::time_point_sec now( GRAPHENE_TESTING_GENESIS_TIMESTAMP ); fc::temp_directory dir1, dir2; database db1, @@ -433,7 +436,7 @@ BOOST_AUTO_TEST_CASE( duplicate_transactions ) BOOST_AUTO_TEST_CASE( tapos ) { try { - fc::time_point_sec now( GRAPHENE_GENESIS_TIMESTAMP ); + fc::time_point_sec now( GRAPHENE_TESTING_GENESIS_TIMESTAMP ); fc::temp_directory dir1, dir2; database db1,