Skip to content

Commit

Permalink
Merge pull request #3406 from nanocurrency/network_params_ref
Browse files Browse the repository at this point in the history
Converting sever static initializations of nano::network_params to references
  • Loading branch information
clemahieu committed Jul 29, 2021
2 parents 95b7cf9 + e0387c5 commit c2d7ddb
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 95 deletions.
38 changes: 19 additions & 19 deletions nano/core_test/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ TEST (wallet, no_special_keys_accounts)
nano::mdb_env env (init, nano::unique_path ());
ASSERT_FALSE (init);
auto transaction (env.tx_begin_write ());
nano::kdf kdf;
nano::kdf kdf{ nano::dev::network_params.kdf_work };
nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0");
ASSERT_FALSE (init);
nano::keypair key1;
Expand All @@ -38,7 +38,7 @@ TEST (wallet, no_key)
nano::mdb_env env (init, nano::unique_path ());
ASSERT_FALSE (init);
auto transaction (env.tx_begin_write ());
nano::kdf kdf;
nano::kdf kdf{ nano::dev::network_params.kdf_work };
nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0");
ASSERT_FALSE (init);
nano::keypair key1;
Expand All @@ -53,7 +53,7 @@ TEST (wallet, fetch_locked)
nano::mdb_env env (init, nano::unique_path ());
ASSERT_FALSE (init);
auto transaction (env.tx_begin_write ());
nano::kdf kdf;
nano::kdf kdf{ nano::dev::network_params.kdf_work };
nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0");
ASSERT_TRUE (wallet.valid_password (transaction));
nano::keypair key1;
Expand All @@ -75,7 +75,7 @@ TEST (wallet, retrieval)
nano::mdb_env env (init, nano::unique_path ());
ASSERT_FALSE (init);
auto transaction (env.tx_begin_write ());
nano::kdf kdf;
nano::kdf kdf{ nano::dev::network_params.kdf_work };
nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0");
ASSERT_FALSE (init);
nano::keypair key1;
Expand All @@ -97,7 +97,7 @@ TEST (wallet, empty_iteration)
nano::mdb_env env (init, nano::unique_path ());
ASSERT_FALSE (init);
auto transaction (env.tx_begin_write ());
nano::kdf kdf;
nano::kdf kdf{ nano::dev::network_params.kdf_work };
nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0");
ASSERT_FALSE (init);
auto i (wallet.begin (transaction));
Expand All @@ -111,7 +111,7 @@ TEST (wallet, one_item_iteration)
nano::mdb_env env (init, nano::unique_path ());
ASSERT_FALSE (init);
auto transaction (env.tx_begin_write ());
nano::kdf kdf;
nano::kdf kdf{ nano::dev::network_params.kdf_work };
nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0");
ASSERT_FALSE (init);
nano::keypair key1;
Expand All @@ -137,7 +137,7 @@ TEST (wallet, two_item_iteration)
ASSERT_NE (key1.pub, key2.pub);
std::unordered_set<nano::public_key> pubs;
std::unordered_set<nano::raw_key> prvs;
nano::kdf kdf;
nano::kdf kdf{ nano::dev::network_params.kdf_work };
{
auto transaction (env.tx_begin_write ());
nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0");
Expand Down Expand Up @@ -277,7 +277,7 @@ TEST (wallet, find_none)
nano::mdb_env env (init, nano::unique_path ());
ASSERT_FALSE (init);
auto transaction (env.tx_begin_write ());
nano::kdf kdf;
nano::kdf kdf{ nano::dev::network_params.kdf_work };
nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0");
ASSERT_FALSE (init);
nano::account account (1000);
Expand All @@ -290,7 +290,7 @@ TEST (wallet, find_existing)
nano::mdb_env env (init, nano::unique_path ());
ASSERT_FALSE (init);
auto transaction (env.tx_begin_write ());
nano::kdf kdf;
nano::kdf kdf{ nano::dev::network_params.kdf_work };
nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0");
ASSERT_FALSE (init);
nano::keypair key1;
Expand All @@ -309,7 +309,7 @@ TEST (wallet, rekey)
nano::mdb_env env (init, nano::unique_path ());
ASSERT_FALSE (init);
auto transaction (env.tx_begin_write ());
nano::kdf kdf;
nano::kdf kdf{ nano::dev::network_params.kdf_work };
nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0");
ASSERT_FALSE (init);
nano::raw_key password;
Expand Down Expand Up @@ -381,7 +381,7 @@ TEST (wallet, hash_password)
nano::mdb_env env (init, nano::unique_path ());
ASSERT_FALSE (init);
auto transaction (env.tx_begin_write ());
nano::kdf kdf;
nano::kdf kdf{ nano::dev::network_params.kdf_work };
nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0");
ASSERT_FALSE (init);
nano::raw_key hash1;
Expand Down Expand Up @@ -430,7 +430,7 @@ TEST (wallet, reopen_default_password)
nano::mdb_env env (init, nano::unique_path ());
auto transaction (env.tx_begin_write ());
ASSERT_FALSE (init);
nano::kdf kdf;
nano::kdf kdf{ nano::dev::network_params.kdf_work };
{
nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0");
ASSERT_FALSE (init);
Expand Down Expand Up @@ -466,7 +466,7 @@ TEST (wallet, representative)
nano::mdb_env env (error, nano::unique_path ());
ASSERT_FALSE (error);
auto transaction (env.tx_begin_write ());
nano::kdf kdf;
nano::kdf kdf{ nano::dev::network_params.kdf_work };
nano::wallet_store wallet (error, kdf, transaction, nano::dev::genesis->account (), 1, "0");
ASSERT_FALSE (error);
ASSERT_FALSE (wallet.is_representative (transaction));
Expand All @@ -487,7 +487,7 @@ TEST (wallet, serialize_json_empty)
nano::mdb_env env (error, nano::unique_path ());
ASSERT_FALSE (error);
auto transaction (env.tx_begin_write ());
nano::kdf kdf;
nano::kdf kdf{ nano::dev::network_params.kdf_work };
nano::wallet_store wallet1 (error, kdf, transaction, nano::dev::genesis->account (), 1, "0");
ASSERT_FALSE (error);
std::string serialized;
Expand All @@ -512,7 +512,7 @@ TEST (wallet, serialize_json_one)
nano::mdb_env env (error, nano::unique_path ());
ASSERT_FALSE (error);
auto transaction (env.tx_begin_write ());
nano::kdf kdf;
nano::kdf kdf{ nano::dev::network_params.kdf_work };
nano::wallet_store wallet1 (error, kdf, transaction, nano::dev::genesis->account (), 1, "0");
ASSERT_FALSE (error);
nano::keypair key;
Expand Down Expand Up @@ -541,7 +541,7 @@ TEST (wallet, serialize_json_password)
nano::mdb_env env (error, nano::unique_path ());
ASSERT_FALSE (error);
auto transaction (env.tx_begin_write ());
nano::kdf kdf;
nano::kdf kdf{ nano::dev::network_params.kdf_work };
nano::wallet_store wallet1 (error, kdf, transaction, nano::dev::genesis->account (), 1, "0");
ASSERT_FALSE (error);
nano::keypair key;
Expand Down Expand Up @@ -574,7 +574,7 @@ TEST (wallet_store, move)
nano::mdb_env env (error, nano::unique_path ());
ASSERT_FALSE (error);
auto transaction (env.tx_begin_write ());
nano::kdf kdf;
nano::kdf kdf{ nano::dev::network_params.kdf_work };
nano::wallet_store wallet1 (error, kdf, transaction, nano::dev::genesis->account (), 1, "0");
ASSERT_FALSE (error);
nano::keypair key1;
Expand Down Expand Up @@ -732,7 +732,7 @@ TEST (wallet, deterministic_keys)
nano::mdb_env env (init, nano::unique_path ());
ASSERT_FALSE (init);
auto transaction (env.tx_begin_write ());
nano::kdf kdf;
nano::kdf kdf{ nano::dev::network_params.kdf_work };
nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0");
auto key1 = wallet.deterministic_key (transaction, 0);
auto key2 = wallet.deterministic_key (transaction, 0);
Expand Down Expand Up @@ -775,7 +775,7 @@ TEST (wallet, reseed)
nano::mdb_env env (init, nano::unique_path ());
ASSERT_FALSE (init);
auto transaction (env.tx_begin_write ());
nano::kdf kdf;
nano::kdf kdf{ nano::dev::network_params.kdf_work };
nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0");
nano::raw_key seed1;
seed1 = 1;
Expand Down
1 change: 0 additions & 1 deletion nano/nano_node/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1786,7 +1786,6 @@ int main (int argc, char * const * argv)
node_flags.read_only = false;
nano::update_flags (node_flags, vm);
nano::inactive_node node (nano::unique_path (), node_flags);
nano::genesis genesis;
auto begin (std::chrono::high_resolution_clock::now ());
uint64_t block_count (0);
size_t count (0);
Expand Down
2 changes: 1 addition & 1 deletion nano/node/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
nano::raw_key junk1;
junk1.clear ();
nano::uint256_union junk2 (0);
nano::kdf kdf;
nano::kdf kdf{ inactive_node->node->config.network_params.kdf_work};
kdf.phs (junk1, "", junk2);
std::cout << "Testing time retrieval latency... " << std::flush;
nano::timer<std::chrono::nanoseconds> timer (nano::timer_state::started);
Expand Down
4 changes: 2 additions & 2 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ nano::node::node (boost::asio::io_context & io_ctx_a, boost::filesystem::path co
io_ctx (io_ctx_a),
node_initialized_latch (1),
config (config_a),
network_params{ config.network_params },
stats (config.stat_config),
workers (std::max (3u, config.io_threads / 4), nano::thread_role::name::worker),
flags (flags_a),
Expand Down Expand Up @@ -344,15 +345,14 @@ nano::node::node (boost::asio::io_context & io_ctx_a, boost::filesystem::path co
is_initialized = (store.account.begin (transaction) != store.account.end ());
}

nano::genesis genesis;
if (!is_initialized && !flags.read_only)
{
auto transaction (store.tx_begin_write ({ tables::accounts, tables::blocks, tables::confirmation_height, tables::frontiers }));
// Store was empty meaning we just created it, add the genesis block
store.initialize (transaction, ledger.cache);
}

if (!ledger.block_or_pruned_exists (genesis.hash ()))
if (!ledger.block_or_pruned_exists (config.network_params.ledger.genesis->hash ()))
{
std::stringstream ss;
ss << "Genesis block not found. This commonly indicates a configuration issue, check that the --network or --data_path command line arguments are correct, "
Expand Down
2 changes: 1 addition & 1 deletion nano/node/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ class node final : public std::enable_shared_from_this<nano::node>
nano::write_database_queue write_database_queue;
boost::asio::io_context & io_ctx;
boost::latch node_initialized_latch;
nano::network_params network_params;
nano::node_config config;
nano::network_params & network_params;
nano::stat stats;
nano::thread_pool workers;
std::shared_ptr<nano::websocket::listener> websocket_server;
Expand Down
11 changes: 6 additions & 5 deletions nano/node/nodeconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ const std::string default_test_peer_network = nano::get_env_or_default ("NANO_TE
}

nano::node_config::node_config () :
node_config (0, nano::logging ())
node_config (0, nano::logging (), nano::dev::network_params)
{
}

nano::node_config::node_config (uint16_t peering_port_a, nano::logging const & logging_a) :
peering_port (peering_port_a),
logging (logging_a),
external_address (boost::asio::ip::address_v6{}.to_string ())
nano::node_config::node_config (uint16_t peering_port_a, nano::logging const & logging_a, nano::network_params network_params) :
network_params{ network_params },
peering_port{ peering_port_a },
logging{ logging_a },
external_address{ boost::asio::ip::address_v6{}.to_string () }
{
// The default constructor passes 0 to indicate we should use the default port,
// which is determined at node startup based on active network.
Expand Down
2 changes: 1 addition & 1 deletion nano/node/nodeconfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class node_config
{
public:
node_config ();
node_config (uint16_t, nano::logging const &);
node_config (uint16_t, nano::logging const &, nano::network_params network_params = nano::dev::network_params);
nano::error serialize_json (nano::jsonconfig &) const;
nano::error deserialize_json (bool &, nano::jsonconfig &);
nano::error serialize_toml (nano::tomlconfig &) const;
Expand Down
5 changes: 3 additions & 2 deletions nano/node/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,9 +651,8 @@ void nano::wallet_store::version_put (nano::transaction const & transaction_a, u

void nano::kdf::phs (nano::raw_key & result_a, std::string const & password_a, nano::uint256_union const & salt_a)
{
static nano::network_params network_params;
nano::lock_guard<nano::mutex> lock (mutex);
auto success (argon2_hash (1, network_params.kdf_work, 1, password_a.data (), password_a.size (), salt_a.bytes.data (), salt_a.bytes.size (), result_a.bytes.data (), result_a.bytes.size (), NULL, 0, Argon2_d, 0x10));
auto success (argon2_hash (1, kdf_work, 1, password_a.data (), password_a.size (), salt_a.bytes.data (), salt_a.bytes.size (), result_a.bytes.data (), result_a.bytes.size (), NULL, 0, Argon2_d, 0x10));
debug_assert (success == 0);
(void)success;
}
Expand Down Expand Up @@ -1335,7 +1334,9 @@ void nano::wallets::do_wallet_actions ()
}

nano::wallets::wallets (bool error_a, nano::node & node_a) :
network_params{ node_a.config.network_params },
observer ([] (bool) {}),
kdf{ node_a.config.network_params.kdf_work },
node (node_a),
env (boost::polymorphic_downcast<nano::mdb_wallets_store *> (node_a.wallets_store_impl.get ())->environment),
stopped (false),
Expand Down
7 changes: 6 additions & 1 deletion nano/node/wallet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ class fan final
class kdf final
{
public:
kdf (unsigned & kdf_work) :
kdf_work{ kdf_work }
{
}
void phs (nano::raw_key &, std::string const &, nano::uint256_union const &);
nano::mutex mutex;
unsigned & kdf_work;
};
enum class key_type
{
Expand Down Expand Up @@ -211,7 +216,7 @@ class wallets final
void split_if_needed (nano::transaction &, nano::store &);
void move_table (std::string const &, MDB_txn *, MDB_txn *);
std::unordered_map<nano::wallet_id, std::shared_ptr<nano::wallet>> get_wallets ();
nano::network_params network_params;
nano::network_params & network_params;
std::function<void (bool)> observer;
std::unordered_map<nano::wallet_id, std::shared_ptr<nano::wallet>> items;
std::multimap<nano::uint128_t, std::pair<std::shared_ptr<nano::wallet>, std::function<void (nano::wallet &)>>, std::greater<nano::uint128_t>> actions;
Expand Down
2 changes: 0 additions & 2 deletions nano/qt_test/qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,6 @@ TEST (history, short_text)
auto wallet (std::make_shared<nano_qt::wallet> (*test_application, processor, *system.nodes[0], system.wallet (0), account));
auto store = nano::make_store (system.nodes[0]->logger, nano::unique_path (), nano::dev::constants);
ASSERT_TRUE (!store->init_error ());
nano::genesis genesis;
nano::ledger ledger (*store, system.nodes[0]->stats, nano::dev::constants);
{
auto transaction (store->tx_begin_write ());
Expand Down Expand Up @@ -553,7 +552,6 @@ TEST (history, pruned_source)
auto wallet (std::make_shared<nano_qt::wallet> (*test_application, processor, *system.nodes[0], system.wallet (0), account));
auto store = nano::make_store (system.nodes[0]->logger, nano::unique_path (), nano::dev::constants);
ASSERT_TRUE (!store->init_error ());
nano::genesis genesis;
nano::ledger ledger (*store, system.nodes[0]->stats, nano::dev::constants);
ledger.pruning = true;
nano::block_hash next_pruning;
Expand Down
Loading

0 comments on commit c2d7ddb

Please sign in to comment.