From 45722d874954d7c349b6fb079f5b1f4c863cd2a6 Mon Sep 17 00:00:00 2001 From: Wesley Shillingford Date: Tue, 4 Feb 2020 09:33:09 +0000 Subject: [PATCH] Cache genesis block hash in ledger constants (#2512) * Cache genesis block hash in ledger constants * Use cached genesis hash with node telemetry data * Serg comment --- nano/core_test/active_transactions.cpp | 3 +-- nano/core_test/block_store.cpp | 12 ++++++------ nano/core_test/confirmation_height.cpp | 6 +++--- nano/core_test/confirmation_solicitor.cpp | 3 +-- nano/core_test/memory_pool.cpp | 2 +- nano/core_test/node.cpp | 5 ++--- nano/core_test/testutil.hpp | 2 ++ nano/node/json_handler.cpp | 2 +- nano/node/network.cpp | 2 +- nano/rpc_test/rpc.cpp | 20 +++++++------------- nano/secure/common.cpp | 15 +++++++++++---- nano/secure/common.hpp | 1 + nano/slow_test/node.cpp | 3 +-- 13 files changed, 38 insertions(+), 38 deletions(-) diff --git a/nano/core_test/active_transactions.cpp b/nano/core_test/active_transactions.cpp index 34936344f4..f5bb667ba3 100644 --- a/nano/core_test/active_transactions.cpp +++ b/nano/core_test/active_transactions.cpp @@ -40,10 +40,9 @@ TEST (active_transactions, adjusted_difficulty_priority) node_config.enable_voting = false; node_config.frontiers_confirmation = nano::frontiers_confirmation_mode::disabled; auto & node1 = *system.add_node (node_config); - nano::genesis genesis; nano::keypair key1, key2, key3; - auto send1 (std::make_shared (nano::test_genesis_key.pub, genesis.hash (), nano::test_genesis_key.pub, nano::genesis_amount - 10 * nano::xrb_ratio, key1.pub, nano::test_genesis_key.prv, nano::test_genesis_key.pub, *system.work.generate (genesis.hash ()))); + auto send1 (std::make_shared (nano::test_genesis_key.pub, nano::genesis_hash, nano::test_genesis_key.pub, nano::genesis_amount - 10 * nano::xrb_ratio, key1.pub, nano::test_genesis_key.prv, nano::test_genesis_key.pub, *system.work.generate (nano::genesis_hash))); auto send2 (std::make_shared (nano::test_genesis_key.pub, send1->hash (), nano::test_genesis_key.pub, nano::genesis_amount - 20 * nano::xrb_ratio, key2.pub, nano::test_genesis_key.prv, nano::test_genesis_key.pub, *system.work.generate (send1->hash ()))); auto open1 (std::make_shared (key1.pub, 0, key1.pub, 10 * nano::xrb_ratio, send1->hash (), key1.prv, key1.pub, *system.work.generate (key1.pub))); auto open2 (std::make_shared (key2.pub, 0, key2.pub, 10 * nano::xrb_ratio, send2->hash (), key2.prv, key2.pub, *system.work.generate (key2.pub))); diff --git a/nano/core_test/block_store.cpp b/nano/core_test/block_store.cpp index 145c9ed436..e51cc5a7bf 100644 --- a/nano/core_test/block_store.cpp +++ b/nano/core_test/block_store.cpp @@ -969,7 +969,7 @@ TEST (mdb_block_store, upgrade_v6_v7) nano::ledger_cache ledger_cache; store.initialize (transaction, genesis, ledger_cache); store.version_put (transaction, 6); - modify_account_info_to_v13 (store, transaction, nano::genesis_account, genesis.open->hash ()); + modify_account_info_to_v13 (store, transaction, nano::genesis_account, nano::genesis_hash); auto send1 (std::make_shared (0, 0, 0, nano::test_genesis_key.prv, nano::test_genesis_key.pub, 0)); store.unchecked_put (transaction, send1->hash (), send1); store.flush (transaction); @@ -1083,7 +1083,7 @@ TEST (block_store, sequence_flush_by_hash) auto transaction (store->tx_begin_write ()); nano::keypair key1; std::vector blocks1; - blocks1.push_back (nano::genesis ().hash ()); + blocks1.push_back (nano::genesis_hash); blocks1.push_back (1234); blocks1.push_back (5678); auto vote1 (store->vote_generate (transaction, key1.pub, key1.prv, blocks1)); @@ -1163,7 +1163,7 @@ TEST (mdb_block_store, upgrade_sideband_genesis) store.version_put (transaction, 11); nano::ledger_cache ledger_cache; store.initialize (transaction, genesis, ledger_cache); - modify_account_info_to_v13 (store, transaction, nano::genesis_account, genesis.open->hash ()); + modify_account_info_to_v13 (store, transaction, nano::genesis_account, nano::genesis_hash); nano::block_sideband sideband; auto genesis_block (store.block_get (transaction, genesis.hash (), &sideband)); ASSERT_NE (nullptr, genesis_block); @@ -1346,7 +1346,7 @@ TEST (mdb_block_store, upgrade_sideband_epoch) nano::mdb_val value; ASSERT_FALSE (mdb_get (store.env.tx (transaction), store.state_blocks_v1, nano::mdb_val (hash2), value)); - ASSERT_FALSE (mdb_get (store.env.tx (transaction), store.open_blocks, nano::mdb_val (genesis.open->hash ()), value)); + ASSERT_FALSE (mdb_get (store.env.tx (transaction), store.open_blocks, nano::mdb_val (nano::genesis_hash), value)); ASSERT_FALSE (mdb_dbi_open (store.env.tx (transaction), "accounts_v1", MDB_CREATE, &store.accounts_v1)); modify_account_info_to_v13 (store, transaction, nano::genesis_account, hash2); @@ -1582,7 +1582,7 @@ TEST (mdb_block_store, upgrade_v13_v14) nano::account_info account_info; ASSERT_FALSE (store.account_get (transaction, nano::genesis_account, account_info)); store.version_put (transaction, 13); - modify_account_info_to_v13 (store, transaction, nano::genesis_account, genesis.open->hash ()); + modify_account_info_to_v13 (store, transaction, nano::genesis_account, nano::genesis_hash); // This should fail as sizes are no longer correct for account_info_v14 nano::mdb_val value; @@ -1917,7 +1917,7 @@ TEST (mdb_block_store, upgrade_confirmation_height_many) store.version_put (transaction, 13); nano::ledger_cache ledger_cache; store.initialize (transaction, genesis, ledger_cache); - modify_account_info_to_v13 (store, transaction, nano::genesis_account, genesis.open->hash ()); + modify_account_info_to_v13 (store, transaction, nano::genesis_account, nano::genesis_hash); // Add many accounts for (auto i = 0; i < total_num_accounts - 1; ++i) diff --git a/nano/core_test/confirmation_height.cpp b/nano/core_test/confirmation_height.cpp index f73662d06b..fab2e8fd97 100644 --- a/nano/core_test/confirmation_height.cpp +++ b/nano/core_test/confirmation_height.cpp @@ -34,7 +34,7 @@ TEST (confirmation_height, single) auto transaction = node->store.tx_begin_read (); ASSERT_FALSE (node->store.confirmation_height_get (transaction, nano::test_genesis_key.pub, confirmation_height_info)); ASSERT_EQ (1, confirmation_height_info.height); - ASSERT_EQ (nano::genesis ().hash (), confirmation_height_info.frontier); + ASSERT_EQ (nano::genesis_hash, confirmation_height_info.frontier); node->process_active (send1); node->block_processor.flush (); @@ -123,7 +123,7 @@ TEST (confirmation_height, multiple_accounts) nano::confirmation_height_info confirmation_height_info; ASSERT_FALSE (node->store.confirmation_height_get (transaction, nano::test_genesis_key.pub, confirmation_height_info)); ASSERT_EQ (1, confirmation_height_info.height); - ASSERT_EQ (nano::genesis ().hash (), confirmation_height_info.frontier); + ASSERT_EQ (nano::genesis_hash, confirmation_height_info.frontier); ASSERT_FALSE (node->store.confirmation_height_get (transaction, key1.pub, confirmation_height_info)); ASSERT_EQ (0, confirmation_height_info.height); ASSERT_EQ (nano::block_hash (0), confirmation_height_info.frontier); @@ -317,7 +317,7 @@ TEST (confirmation_height, gap_live) nano::confirmation_height_info confirmation_height_info; ASSERT_FALSE (node->store.confirmation_height_get (transaction, nano::test_genesis_key.pub, confirmation_height_info)); ASSERT_EQ (1, confirmation_height_info.height); - ASSERT_EQ (nano::genesis ().hash (), confirmation_height_info.frontier); + ASSERT_EQ (nano::genesis_hash, confirmation_height_info.frontier); } // Now complete the chain where the block comes in on the live network diff --git a/nano/core_test/confirmation_solicitor.cpp b/nano/core_test/confirmation_solicitor.cpp index 4d83b7b2ef..cb4b512249 100644 --- a/nano/core_test/confirmation_solicitor.cpp +++ b/nano/core_test/confirmation_solicitor.cpp @@ -30,8 +30,7 @@ TEST (confirmation_solicitor, batches) ASSERT_EQ (1, representatives.size ()); ASSERT_EQ (channel1, representatives.front ().channel); ASSERT_EQ (nano::test_genesis_key.pub, representatives.front ().account); - nano::genesis genesis; - auto send (std::make_shared (genesis.open->hash (), nano::keypair ().pub, nano::genesis_amount - 100, nano::test_genesis_key.prv, nano::test_genesis_key.pub, *system.work.generate (genesis.open->hash ()))); + auto send (std::make_shared (nano::genesis_hash, nano::keypair ().pub, nano::genesis_amount - 100, nano::test_genesis_key.prv, nano::test_genesis_key.pub, *system.work.generate (nano::genesis_hash))); for (size_t i (0); i < nano::network::confirm_req_hashes_max; ++i) { auto election (std::make_shared (node2, send, false, nullptr)); diff --git a/nano/core_test/memory_pool.cpp b/nano/core_test/memory_pool.cpp index e9e5ae8e2b..0a97764a71 100644 --- a/nano/core_test/memory_pool.cpp +++ b/nano/core_test/memory_pool.cpp @@ -49,7 +49,7 @@ size_t get_allocated_size () { std::vector allocated; record_allocations_new_delete_allocator alloc (&allocated); - std::allocate_shared> (alloc); + (void)std::allocate_shared> (alloc); assert (allocated.size () == 1); return allocated.front (); } diff --git a/nano/core_test/node.cpp b/nano/core_test/node.cpp index 20af8cc222..224a5062d0 100644 --- a/nano/core_test/node.cpp +++ b/nano/core_test/node.cpp @@ -2742,14 +2742,13 @@ TEST (node, local_votes_cache_size) node_config.vote_minimum = 0; // wallet will pick up the second account as voting even if unopened auto & node (*system.add_node (node_config)); ASSERT_EQ (node.network_params.voting.max_cache, 2); // effective cache size is 1 with 2 voting accounts - nano::genesis genesis; nano::keypair key; auto & wallet (*system.wallet (0)); wallet.insert_adhoc (nano::test_genesis_key.prv); wallet.insert_adhoc (nano::keypair ().prv); ASSERT_EQ (2, node.wallets.rep_counts ().voting); auto transaction (node.store.tx_begin_read ()); - auto vote1 (node.store.vote_generate (transaction, nano::test_genesis_key.pub, nano::test_genesis_key.prv, { genesis.open->hash () })); + auto vote1 (node.store.vote_generate (transaction, nano::test_genesis_key.pub, nano::test_genesis_key.prv, { nano::genesis_hash })); nano::block_hash hash (1); auto vote2 (node.store.vote_generate (transaction, nano::test_genesis_key.pub, nano::test_genesis_key.prv, { hash })); node.votes_cache.add (vote1); @@ -2757,7 +2756,7 @@ TEST (node, local_votes_cache_size) auto existing2 (node.votes_cache.find (hash)); ASSERT_EQ (1, existing2.size ()); ASSERT_EQ (vote2, existing2.front ()); - ASSERT_EQ (0, node.votes_cache.find (genesis.open->hash ()).size ()); + ASSERT_EQ (0, node.votes_cache.find (nano::genesis_hash).size ()); } TEST (node, vote_republish) diff --git a/nano/core_test/testutil.hpp b/nano/core_test/testutil.hpp index afd7849a58..24202a4ccc 100644 --- a/nano/core_test/testutil.hpp +++ b/nano/core_test/testutil.hpp @@ -38,10 +38,12 @@ namespace nano using uint128_t = boost::multiprecision::uint128_t; class keypair; class public_key; +class block_hash; extern nano::keypair const & zero_key; extern nano::keypair const & test_genesis_key; extern std::string const & nano_test_genesis; extern std::string const & genesis_block; +extern nano::block_hash const & genesis_hash; extern nano::public_key const & nano_test_account; extern nano::public_key const & genesis_account; extern nano::public_key const & burn_account; diff --git a/nano/node/json_handler.cpp b/nano/node/json_handler.cpp index 25598ed749..94cb7aca71 100644 --- a/nano/node/json_handler.cpp +++ b/nano/node/json_handler.cpp @@ -4214,7 +4214,7 @@ void nano::json_handler::version () response_l.put ("node_vendor", boost::str (boost::format ("Nano %1%") % NANO_VERSION_STRING)); response_l.put ("store_vendor", node.store.vendor_get ()); response_l.put ("network", node.network_params.network.get_current_network_as_string ()); - response_l.put ("network_identifier", nano::genesis ().hash ().to_string ()); + response_l.put ("network_identifier", node.network_params.ledger.genesis_hash.to_string ()); response_l.put ("build_info", BUILD_INFO); response_errors (); } diff --git a/nano/node/network.cpp b/nano/node/network.cpp index 4dca823b06..ba82542689 100644 --- a/nano/node/network.cpp +++ b/nano/node/network.cpp @@ -545,7 +545,7 @@ class network_message_visitor : public nano::message_visitor telemetry_data.protocol_version = node.network_params.protocol.protocol_version; telemetry_data.uptime = std::chrono::duration_cast (std::chrono::steady_clock::now () - node.startup_time).count (); telemetry_data.unchecked_count = node.ledger.cache.unchecked_count; - telemetry_data.genesis_block = nano::genesis ().hash (); + telemetry_data.genesis_block = node.network_params.ledger.genesis_hash; telemetry_data.peer_count = node.network.size (); telemetry_data.account_count = node.ledger.cache.account_count; telemetry_data.major_version = nano::get_major_node_version (); diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index bf60ca50af..5eadf5c68d 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -6391,7 +6391,7 @@ TEST (rpc, block_confirm_confirmed) // Check confirmation history auto confirmed (node->active.list_confirmed ()); ASSERT_EQ (1, confirmed.size ()); - ASSERT_EQ (genesis.hash (), confirmed.begin ()->winner->hash ()); + ASSERT_EQ (nano::genesis_hash, confirmed.begin ()->winner->hash ()); // Check callback system.deadline_set (5s); while (node->stats.count (nano::stat::type::error, nano::stat::detail::http_callback, nano::stat::dir::out) == 0) @@ -6781,7 +6781,6 @@ TEST (rpc, wallet_history) { nano::system system; auto node = add_ipc_enabled_node (system); - nano::genesis genesis; system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv); auto timestamp1 (nano::seconds_since_epoch ()); auto send (system.wallet (0)->send_action (nano::test_genesis_key.pub, nano::test_genesis_key.pub, node->config.receive_minimum.number ())); @@ -6843,7 +6842,7 @@ TEST (rpc, wallet_history) ASSERT_EQ ("receive", std::get<0> (history_l[3])); ASSERT_EQ (nano::test_genesis_key.pub.to_account (), std::get<1> (history_l[3])); ASSERT_EQ (nano::genesis_amount.convert_to (), std::get<2> (history_l[3])); - ASSERT_EQ (genesis.hash ().to_string (), std::get<3> (history_l[3])); + ASSERT_EQ (nano::genesis_hash.to_string (), std::get<3> (history_l[3])); ASSERT_EQ (nano::test_genesis_key.pub.to_account (), std::get<4> (history_l[3])); } @@ -7415,7 +7414,6 @@ TEST (rpc_config, migrate) TEST (rpc, deprecated_account_format) { nano::system system; - nano::genesis genesis; auto node = add_ipc_enabled_node (system); nano::node_rpc_config node_rpc_config; nano::ipc::ipc_server ipc_server (*node, node_rpc_config); @@ -7447,7 +7445,7 @@ TEST (rpc, deprecated_account_format) } ASSERT_EQ (200, response2.status); std::string frontier (response.json.get ("frontier")); - ASSERT_EQ (genesis.hash ().to_string (), frontier); + ASSERT_EQ (nano::genesis_hash.to_string (), frontier); boost::optional deprecated_account_format2 (response2.json.get_optional ("deprecated_account_format")); ASSERT_TRUE (deprecated_account_format2.is_initialized ()); } @@ -7457,9 +7455,8 @@ TEST (rpc, epoch_upgrade) nano::system system; auto node = add_ipc_enabled_node (system); nano::keypair key1, key2, key3; - nano::genesis genesis; nano::keypair epoch_signer (nano::test_genesis_key); - auto send1 (std::make_shared (nano::test_genesis_key.pub, genesis.hash (), nano::test_genesis_key.pub, nano::genesis_amount - 1, key1.pub, nano::test_genesis_key.prv, nano::test_genesis_key.pub, *system.work.generate (genesis.hash ()))); // to opened account + auto send1 (std::make_shared (nano::test_genesis_key.pub, nano::genesis_hash, nano::test_genesis_key.pub, nano::genesis_amount - 1, key1.pub, nano::test_genesis_key.prv, nano::test_genesis_key.pub, *system.work.generate (nano::genesis_hash))); // to opened account ASSERT_EQ (nano::process_result::progress, node->process (*send1).code); auto send2 (std::make_shared (nano::test_genesis_key.pub, send1->hash (), nano::test_genesis_key.pub, nano::genesis_amount - 2, key2.pub, nano::test_genesis_key.prv, nano::test_genesis_key.pub, *system.work.generate (send1->hash ()))); // to unopened account (pending) ASSERT_EQ (nano::process_result::progress, node->process (*send2).code); @@ -7579,10 +7576,9 @@ TEST (rpc, account_lazy_start) nano::node_flags node_flags; node_flags.disable_legacy_bootstrap = true; auto node1 = system.add_node (node_flags); - nano::genesis genesis; nano::keypair key; // Generating test chain - auto send1 (std::make_shared (nano::test_genesis_key.pub, genesis.hash (), nano::test_genesis_key.pub, nano::genesis_amount - nano::Gxrb_ratio, key.pub, nano::test_genesis_key.prv, nano::test_genesis_key.pub, *system.work.generate (genesis.hash ()))); + auto send1 (std::make_shared (nano::test_genesis_key.pub, nano::genesis_hash, nano::test_genesis_key.pub, nano::genesis_amount - nano::Gxrb_ratio, key.pub, nano::test_genesis_key.prv, nano::test_genesis_key.pub, *system.work.generate (nano::genesis_hash))); ASSERT_EQ (nano::process_result::progress, node1->process (*send1).code); auto open (std::make_shared (send1->hash (), key.pub, key.pub, key.prv, key.pub, *system.work.generate (key.pub))); ASSERT_EQ (nano::process_result::progress, node1->process (*open).code); @@ -7634,8 +7630,7 @@ TEST (rpc, receive) wallet->insert_adhoc (nano::test_genesis_key.prv); nano::keypair key1; wallet->insert_adhoc (key1.prv); - nano::genesis genesis; - auto send1 (wallet->send_action (nano::test_genesis_key.pub, key1.pub, node.config.receive_minimum.number (), *node.work_generate_blocking (genesis.hash ()))); + auto send1 (wallet->send_action (nano::test_genesis_key.pub, key1.pub, node.config.receive_minimum.number (), *node.work_generate_blocking (nano::genesis_hash))); system.deadline_set (5s); while (node.balance (nano::test_genesis_key.pub) == nano::genesis_amount) { @@ -7708,8 +7703,7 @@ TEST (rpc, receive_unopened) wallet->insert_adhoc (nano::test_genesis_key.prv); // Test receiving for unopened account nano::keypair key1; - nano::genesis genesis; - auto send1 (wallet->send_action (nano::test_genesis_key.pub, key1.pub, node.config.receive_minimum.number () - 1, *node.work_generate_blocking (genesis.hash ()))); + auto send1 (wallet->send_action (nano::test_genesis_key.pub, key1.pub, node.config.receive_minimum.number () - 1, *node.work_generate_blocking (nano::genesis_hash))); system.deadline_set (5s); while (node.balance (nano::test_genesis_key.pub) == nano::genesis_amount) { diff --git a/nano/secure/common.cpp b/nano/secure/common.cpp index 9adb292112..b8d126be8d 100644 --- a/nano/secure/common.cpp +++ b/nano/secure/common.cpp @@ -56,6 +56,14 @@ char const * live_genesis_data = R"%%%({ "work": "62f05417dd3fb691", "signature": "9F0C933C8ADE004D808EA1985FA746A7E95BA2A38F867640F53EC8F180BDFE9E2C1268DEAD7C2664F356E37ABA362BC58E46DBA03E523A7B5A19E4B6EB12BB02" })%%%"; + +std::shared_ptr parse_block_from_genesis_data (std::string const & genesis_data_a) +{ + boost::property_tree::ptree tree; + std::stringstream istream (genesis_data_a); + boost::property_tree::read_json (istream, tree); + return nano::deserialize_block_json (tree); +} } nano::network_params::network_params () : @@ -92,6 +100,7 @@ nano_beta_genesis (beta_genesis_data), nano_live_genesis (live_genesis_data), genesis_account (network_a == nano::nano_networks::nano_test_network ? nano_test_account : network_a == nano::nano_networks::nano_beta_network ? nano_beta_account : nano_live_account), genesis_block (network_a == nano::nano_networks::nano_test_network ? nano_test_genesis : network_a == nano::nano_networks::nano_beta_network ? nano_beta_genesis : nano_live_genesis), +genesis_hash (parse_block_from_genesis_data (genesis_block)->hash ()), genesis_amount (std::numeric_limits::max ()), burn_account (0) { @@ -161,6 +170,7 @@ nano::keypair const & nano::test_genesis_key (test_constants.test_genesis_key); nano::account const & nano::nano_test_account (test_constants.nano_test_account); std::string const & nano::nano_test_genesis (test_constants.nano_test_genesis); nano::account const & nano::genesis_account (test_constants.genesis_account); +nano::block_hash const & nano::genesis_hash (test_constants.genesis_hash); nano::uint128_t const & nano::genesis_amount (test_constants.genesis_amount); nano::account const & nano::burn_account (test_constants.burn_account); @@ -795,10 +805,7 @@ std::unique_ptr nano::collect_container_info (vo nano::genesis::genesis () { static nano::network_params network_params; - boost::property_tree::ptree tree; - std::stringstream istream (network_params.ledger.genesis_block); - boost::property_tree::read_json (istream, tree); - open = nano::deserialize_block_json (tree); + open = parse_block_from_genesis_data (network_params.ledger.genesis_block); assert (open != nullptr); } diff --git a/nano/secure/common.hpp b/nano/secure/common.hpp index 650bcbd9ee..6325e95c08 100644 --- a/nano/secure/common.hpp +++ b/nano/secure/common.hpp @@ -381,6 +381,7 @@ class ledger_constants std::string nano_live_genesis; nano::account genesis_account; std::string genesis_block; + nano::block_hash genesis_hash; nano::uint128_t genesis_amount; nano::account burn_account; nano::epochs epochs; diff --git a/nano/slow_test/node.cpp b/nano/slow_test/node.cpp index cf1c2a678a..3c4e1d7d14 100644 --- a/nano/slow_test/node.cpp +++ b/nano/slow_test/node.cpp @@ -436,8 +436,7 @@ TEST (node, mass_vote_by_hash) { nano::system system (1); system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv); - nano::genesis genesis; - nano::block_hash previous (genesis.hash ()); + nano::block_hash previous (nano::genesis_hash); nano::keypair key; std::vector> blocks; for (auto i (0); i < 10000; ++i)