Skip to content

Commit

Permalink
Cache genesis block hash in ledger constants (#2512)
Browse files Browse the repository at this point in the history
* Cache genesis block hash in ledger constants

* Use cached genesis hash with node telemetry data

* Serg comment
  • Loading branch information
wezrule committed Feb 4, 2020
1 parent e32b6b8 commit 45722d8
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 38 deletions.
3 changes: 1 addition & 2 deletions nano/core_test/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::state_block> (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::state_block> (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::state_block> (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<nano::state_block> (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<nano::state_block> (key2.pub, 0, key2.pub, 10 * nano::xrb_ratio, send2->hash (), key2.prv, key2.pub, *system.work.generate (key2.pub)));
Expand Down
12 changes: 6 additions & 6 deletions nano/core_test/block_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<nano::send_block> (0, 0, 0, nano::test_genesis_key.prv, nano::test_genesis_key.pub, 0));
store.unchecked_put (transaction, send1->hash (), send1);
store.flush (transaction);
Expand Down Expand Up @@ -1083,7 +1083,7 @@ TEST (block_store, sequence_flush_by_hash)
auto transaction (store->tx_begin_write ());
nano::keypair key1;
std::vector<nano::block_hash> 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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions nano/core_test/confirmation_height.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions nano/core_test/confirmation_solicitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<nano::send_block> (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::send_block> (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<nano::election> (node2, send, false, nullptr));
Expand Down
2 changes: 1 addition & 1 deletion nano/core_test/memory_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ size_t get_allocated_size ()
{
std::vector<size_t> allocated;
record_allocations_new_delete_allocator<T> alloc (&allocated);
std::allocate_shared<T, record_allocations_new_delete_allocator<T>> (alloc);
(void)std::allocate_shared<T, record_allocations_new_delete_allocator<T>> (alloc);
assert (allocated.size () == 1);
return allocated.front ();
}
Expand Down
5 changes: 2 additions & 3 deletions nano/core_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2742,22 +2742,21 @@ 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);
node.votes_cache.add (vote2);
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)
Expand Down
2 changes: 2 additions & 0 deletions nano/core_test/testutil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
}
Expand Down
2 changes: 1 addition & 1 deletion nano/node/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::seconds> (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 ();
Expand Down
20 changes: 7 additions & 13 deletions nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 ()));
Expand Down Expand Up @@ -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::string> (), 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]));
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -7447,7 +7445,7 @@ TEST (rpc, deprecated_account_format)
}
ASSERT_EQ (200, response2.status);
std::string frontier (response.json.get<std::string> ("frontier"));
ASSERT_EQ (genesis.hash ().to_string (), frontier);
ASSERT_EQ (nano::genesis_hash.to_string (), frontier);
boost::optional<std::string> deprecated_account_format2 (response2.json.get_optional<std::string> ("deprecated_account_format"));
ASSERT_TRUE (deprecated_account_format2.is_initialized ());
}
Expand All @@ -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::state_block> (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::state_block> (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::state_block> (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);
Expand Down Expand Up @@ -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::state_block> (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::state_block> (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<nano::open_block> (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);
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
15 changes: 11 additions & 4 deletions nano/secure/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ char const * live_genesis_data = R"%%%({
"work": "62f05417dd3fb691",
"signature": "9F0C933C8ADE004D808EA1985FA746A7E95BA2A38F867640F53EC8F180BDFE9E2C1268DEAD7C2664F356E37ABA362BC58E46DBA03E523A7B5A19E4B6EB12BB02"
})%%%";

std::shared_ptr<nano::block> 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 () :
Expand Down Expand Up @@ -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<nano::uint128_t>::max ()),
burn_account (0)
{
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -795,10 +805,7 @@ std::unique_ptr<nano::container_info_component> 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);
}

Expand Down
1 change: 1 addition & 0 deletions nano/secure/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions nano/slow_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::shared_ptr<nano::state_block>> blocks;
for (auto i (0); i < 10000; ++i)
Expand Down

0 comments on commit 45722d8

Please sign in to comment.