diff --git a/nano/core_test/wallet.cpp b/nano/core_test/wallet.cpp index 6aad11bc15..3c835e209e 100644 --- a/nano/core_test/wallet.cpp +++ b/nano/core_test/wallet.cpp @@ -19,7 +19,7 @@ TEST (wallet, no_special_keys_accounts) ASSERT_FALSE (init); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0"); + nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis->account (), 1, "0"); ASSERT_FALSE (init); nano::keypair key1; ASSERT_FALSE (wallet.exists (transaction, key1.pub)); @@ -40,7 +40,7 @@ TEST (wallet, no_key) ASSERT_FALSE (init); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0"); + nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis->account (), 1, "0"); ASSERT_FALSE (init); nano::keypair key1; nano::raw_key prv1; @@ -55,7 +55,7 @@ TEST (wallet, fetch_locked) ASSERT_FALSE (init); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0"); + nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis->account (), 1, "0"); ASSERT_TRUE (wallet.valid_password (transaction)); nano::keypair key1; ASSERT_EQ (key1.pub, wallet.insert_adhoc (transaction, key1.prv)); @@ -77,7 +77,7 @@ TEST (wallet, retrieval) ASSERT_FALSE (init); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0"); + nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis->account (), 1, "0"); ASSERT_FALSE (init); nano::keypair key1; ASSERT_TRUE (wallet.valid_password (transaction)); @@ -99,7 +99,7 @@ TEST (wallet, empty_iteration) ASSERT_FALSE (init); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0"); + nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis->account (), 1, "0"); ASSERT_FALSE (init); auto i (wallet.begin (transaction)); auto j (wallet.end ()); @@ -113,7 +113,7 @@ TEST (wallet, one_item_iteration) ASSERT_FALSE (init); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0"); + nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis->account (), 1, "0"); ASSERT_FALSE (init); nano::keypair key1; wallet.insert_adhoc (transaction, key1.prv); @@ -141,7 +141,7 @@ TEST (wallet, two_item_iteration) 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"); + nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis->account (), 1, "0"); ASSERT_FALSE (init); wallet.insert_adhoc (transaction, key1.prv); wallet.insert_adhoc (transaction, key2.prv); @@ -274,7 +274,7 @@ TEST (wallet, find_none) ASSERT_FALSE (init); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0"); + nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis->account (), 1, "0"); ASSERT_FALSE (init); nano::account account (1000); ASSERT_EQ (wallet.end (), wallet.find (transaction, account)); @@ -287,7 +287,7 @@ TEST (wallet, find_existing) ASSERT_FALSE (init); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0"); + nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis->account (), 1, "0"); ASSERT_FALSE (init); nano::keypair key1; ASSERT_FALSE (wallet.exists (transaction, key1.pub)); @@ -306,7 +306,7 @@ TEST (wallet, rekey) ASSERT_FALSE (init); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0"); + nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis->account (), 1, "0"); ASSERT_FALSE (init); nano::raw_key password; wallet.password.value (password); @@ -378,7 +378,7 @@ TEST (wallet, hash_password) ASSERT_FALSE (init); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0"); + nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis->account (), 1, "0"); ASSERT_FALSE (init); nano::raw_key hash1; wallet.derive_key (hash1, transaction, ""); @@ -428,25 +428,25 @@ TEST (wallet, reopen_default_password) ASSERT_FALSE (init); nano::kdf kdf{ nano::dev::network_params.kdf_work }; { - nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0"); + nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis->account (), 1, "0"); ASSERT_FALSE (init); ASSERT_TRUE (wallet.valid_password (transaction)); } { bool init; - nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0"); + nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis->account (), 1, "0"); ASSERT_FALSE (init); ASSERT_TRUE (wallet.valid_password (transaction)); } { - nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0"); + nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis->account (), 1, "0"); ASSERT_FALSE (init); wallet.rekey (transaction, ""); ASSERT_TRUE (wallet.valid_password (transaction)); } { bool init; - nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0"); + nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis->account (), 1, "0"); ASSERT_FALSE (init); ASSERT_FALSE (wallet.valid_password (transaction)); wallet.attempt_password (transaction, " "); @@ -463,7 +463,7 @@ TEST (wallet, representative) ASSERT_FALSE (error); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (error, kdf, transaction, nano::dev::genesis->account (), 1, "0"); + nano::wallet_store wallet (error, kdf, transaction, env, nano::dev::genesis->account (), 1, "0"); ASSERT_FALSE (error); ASSERT_FALSE (wallet.is_representative (transaction)); ASSERT_EQ (nano::dev::genesis->account (), wallet.representative (transaction)); @@ -484,11 +484,11 @@ TEST (wallet, serialize_json_empty) ASSERT_FALSE (error); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet1 (error, kdf, transaction, nano::dev::genesis->account (), 1, "0"); + nano::wallet_store wallet1 (error, kdf, transaction, env, nano::dev::genesis->account (), 1, "0"); ASSERT_FALSE (error); std::string serialized; wallet1.serialize_json (transaction, serialized); - nano::wallet_store wallet2 (error, kdf, transaction, nano::dev::genesis->account (), 1, "1", serialized); + nano::wallet_store wallet2 (error, kdf, transaction, env, nano::dev::genesis->account (), 1, "1", serialized); ASSERT_FALSE (error); nano::raw_key password1; nano::raw_key password2; @@ -509,13 +509,13 @@ TEST (wallet, serialize_json_one) ASSERT_FALSE (error); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet1 (error, kdf, transaction, nano::dev::genesis->account (), 1, "0"); + nano::wallet_store wallet1 (error, kdf, transaction, env, nano::dev::genesis->account (), 1, "0"); ASSERT_FALSE (error); nano::keypair key; wallet1.insert_adhoc (transaction, key.prv); std::string serialized; wallet1.serialize_json (transaction, serialized); - nano::wallet_store wallet2 (error, kdf, transaction, nano::dev::genesis->account (), 1, "1", serialized); + nano::wallet_store wallet2 (error, kdf, transaction, env, nano::dev::genesis->account (), 1, "1", serialized); ASSERT_FALSE (error); nano::raw_key password1; nano::raw_key password2; @@ -538,14 +538,14 @@ TEST (wallet, serialize_json_password) ASSERT_FALSE (error); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet1 (error, kdf, transaction, nano::dev::genesis->account (), 1, "0"); + nano::wallet_store wallet1 (error, kdf, transaction, env, nano::dev::genesis->account (), 1, "0"); ASSERT_FALSE (error); nano::keypair key; wallet1.rekey (transaction, "password"); wallet1.insert_adhoc (transaction, key.prv); std::string serialized; wallet1.serialize_json (transaction, serialized); - nano::wallet_store wallet2 (error, kdf, transaction, nano::dev::genesis->account (), 1, "1", serialized); + nano::wallet_store wallet2 (error, kdf, transaction, env, nano::dev::genesis->account (), 1, "1", serialized); ASSERT_FALSE (error); ASSERT_FALSE (wallet2.valid_password (transaction)); ASSERT_FALSE (wallet2.attempt_password (transaction, "password")); @@ -571,11 +571,11 @@ TEST (wallet_store, move) ASSERT_FALSE (error); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet1 (error, kdf, transaction, nano::dev::genesis->account (), 1, "0"); + nano::wallet_store wallet1 (error, kdf, transaction, env, nano::dev::genesis->account (), 1, "0"); ASSERT_FALSE (error); nano::keypair key1; wallet1.insert_adhoc (transaction, key1.prv); - nano::wallet_store wallet2 (error, kdf, transaction, nano::dev::genesis->account (), 1, "1"); + nano::wallet_store wallet2 (error, kdf, transaction, env, nano::dev::genesis->account (), 1, "1"); ASSERT_FALSE (error); nano::keypair key2; wallet2.insert_adhoc (transaction, key2.prv); @@ -729,7 +729,7 @@ TEST (wallet, deterministic_keys) ASSERT_FALSE (init); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0"); + nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis->account (), 1, "0"); auto key1 = wallet.deterministic_key (transaction, 0); auto key2 = wallet.deterministic_key (transaction, 0); ASSERT_EQ (key1, key2); @@ -772,7 +772,7 @@ TEST (wallet, reseed) ASSERT_FALSE (init); auto transaction (env.tx_begin_write ()); nano::kdf kdf{ nano::dev::network_params.kdf_work }; - nano::wallet_store wallet (init, kdf, transaction, nano::dev::genesis->account (), 1, "0"); + nano::wallet_store wallet (init, kdf, transaction, env, nano::dev::genesis->account (), 1, "0"); nano::raw_key seed1; seed1 = 1; nano::raw_key seed2; diff --git a/nano/node/wallet.cpp b/nano/node/wallet.cpp index 8f2ec7e0ca..4369d96eed 100644 --- a/nano/node/wallet.cpp +++ b/nano/node/wallet.cpp @@ -255,17 +255,18 @@ int const nano::wallet_store::special_count (7); std::size_t const nano::wallet_store::check_iv_index (0); std::size_t const nano::wallet_store::seed_iv_index (1); -nano::wallet_store::wallet_store (bool & init_a, nano::kdf & kdf_a, store::transaction & transaction_a, nano::account representative_a, unsigned fanout_a, std::string const & wallet_a, std::string const & json_a) : +nano::wallet_store::wallet_store (bool & init_a, nano::kdf & kdf_a, store::transaction & transaction_a, store::lmdb::env & env_a, nano::account representative_a, unsigned fanout_a, std::string const & wallet_a, std::string const & json_a) : password (0, fanout_a), wallet_key_mem (0, fanout_a), - kdf (kdf_a) + kdf (kdf_a), + env{ env_a } { init_a = false; initialize (transaction_a, init_a, wallet_a); if (!init_a) { MDB_val junk; - debug_assert (mdb_get (tx (transaction_a), handle, nano::store::lmdb::db_val (version_special), &junk) == MDB_NOTFOUND); + debug_assert (mdb_get (env.tx (transaction_a), handle, nano::store::lmdb::db_val (version_special), &junk) == MDB_NOTFOUND); boost::property_tree::ptree wallet_l; std::stringstream istream (json_a); try @@ -298,11 +299,11 @@ nano::wallet_store::wallet_store (bool & init_a, nano::kdf & kdf_a, store::trans init_a = true; } } - init_a |= mdb_get (tx (transaction_a), handle, nano::store::lmdb::db_val (version_special), &junk) != 0; - init_a |= mdb_get (tx (transaction_a), handle, nano::store::lmdb::db_val (wallet_key_special), &junk) != 0; - init_a |= mdb_get (tx (transaction_a), handle, nano::store::lmdb::db_val (salt_special), &junk) != 0; - init_a |= mdb_get (tx (transaction_a), handle, nano::store::lmdb::db_val (check_special), &junk) != 0; - init_a |= mdb_get (tx (transaction_a), handle, nano::store::lmdb::db_val (representative_special), &junk) != 0; + init_a |= mdb_get (env.tx (transaction_a), handle, nano::store::lmdb::db_val (version_special), &junk) != 0; + init_a |= mdb_get (env.tx (transaction_a), handle, nano::store::lmdb::db_val (wallet_key_special), &junk) != 0; + init_a |= mdb_get (env.tx (transaction_a), handle, nano::store::lmdb::db_val (salt_special), &junk) != 0; + init_a |= mdb_get (env.tx (transaction_a), handle, nano::store::lmdb::db_val (check_special), &junk) != 0; + init_a |= mdb_get (env.tx (transaction_a), handle, nano::store::lmdb::db_val (representative_special), &junk) != 0; nano::raw_key key; key.clear (); password.value_set (key); @@ -311,10 +312,11 @@ nano::wallet_store::wallet_store (bool & init_a, nano::kdf & kdf_a, store::trans } } -nano::wallet_store::wallet_store (bool & init_a, nano::kdf & kdf_a, store::transaction & transaction_a, nano::account representative_a, unsigned fanout_a, std::string const & wallet_a) : +nano::wallet_store::wallet_store (bool & init_a, nano::kdf & kdf_a, store::transaction & transaction_a, store::lmdb::env & env_a, nano::account representative_a, unsigned fanout_a, std::string const & wallet_a) : password (0, fanout_a), wallet_key_mem (0, fanout_a), - kdf (kdf_a) + kdf (kdf_a), + env{ env_a } { init_a = false; initialize (transaction_a, init_a, wallet_a); @@ -322,7 +324,7 @@ nano::wallet_store::wallet_store (bool & init_a, nano::kdf & kdf_a, store::trans { int version_status; MDB_val version_value; - version_status = mdb_get (tx (transaction_a), handle, nano::store::lmdb::db_val (version_special), &version_value); + version_status = mdb_get (env.tx (transaction_a), handle, nano::store::lmdb::db_val (version_special), &version_value); if (version_status == MDB_NOTFOUND) { version_put (transaction_a, version_current); @@ -377,7 +379,7 @@ void nano::wallet_store::initialize (store::transaction const & transaction_a, b debug_assert (strlen (path_a.c_str ()) == path_a.size ()); auto error (0); MDB_dbi handle_l; - error |= mdb_dbi_open (tx (transaction_a), path_a.c_str (), MDB_CREATE, &handle_l); + error |= mdb_dbi_open (env.tx (transaction_a), path_a.c_str (), MDB_CREATE, &handle_l); handle = handle_l; init_a = error != 0; } @@ -424,7 +426,7 @@ bool nano::wallet_store::insert_watch (store::transaction const & transaction_a, void nano::wallet_store::erase (store::transaction const & transaction_a, nano::account const & pub) { - auto status (mdb_del (tx (transaction_a), handle, nano::store::lmdb::db_val (pub), nullptr)); + auto status (mdb_del (env.tx (transaction_a), handle, nano::store::lmdb::db_val (pub), nullptr)); (void)status; debug_assert (status == 0); } @@ -433,7 +435,7 @@ nano::wallet_value nano::wallet_store::entry_get_raw (store::transaction const & { nano::wallet_value result; nano::store::lmdb::db_val value; - auto status (mdb_get (tx (transaction_a), handle, nano::store::lmdb::db_val (pub_a), value)); + auto status (mdb_get (env.tx (transaction_a), handle, nano::store::lmdb::db_val (pub_a), value)); if (status == 0) { result = nano::wallet_value (value); @@ -448,7 +450,7 @@ nano::wallet_value nano::wallet_store::entry_get_raw (store::transaction const & void nano::wallet_store::entry_put_raw (store::transaction const & transaction_a, nano::account const & pub_a, nano::wallet_value const & entry_a) { - auto status (mdb_put (tx (transaction_a), handle, nano::store::lmdb::db_val (pub_a), nano::store::lmdb::db_val (sizeof (entry_a), const_cast (&entry_a)), 0)); + auto status (mdb_put (env.tx (transaction_a), handle, nano::store::lmdb::db_val (pub_a), nano::store::lmdb::db_val (sizeof (entry_a), const_cast (&entry_a)), 0)); (void)status; debug_assert (status == 0); } @@ -542,7 +544,7 @@ bool nano::wallet_store::exists (store::transaction const & transaction_a, nano: void nano::wallet_store::serialize_json (store::transaction const & transaction_a, std::string & string_a) { boost::property_tree::ptree tree; - for (store::iterator i (std::make_unique> (transaction_a, handle)), n (nullptr); i != n; ++i) + for (store::iterator i (std::make_unique> (transaction_a, env, handle)), n (nullptr); i != n; ++i) { tree.put (i->first.to_string (), i->second.key.to_string ()); } @@ -659,14 +661,14 @@ void nano::kdf::phs (nano::raw_key & result_a, std::string const & password_a, n nano::wallet::wallet (bool & init_a, store::transaction & transaction_a, nano::wallets & wallets_a, std::string const & wallet_a) : lock_observer ([] (bool, bool) {}), - store (init_a, wallets_a.kdf, transaction_a, wallets_a.node.config.random_representative (), wallets_a.node.config.password_fanout, wallet_a), + store (init_a, wallets_a.kdf, transaction_a, wallets_a.env, wallets_a.node.config.random_representative (), wallets_a.node.config.password_fanout, wallet_a), wallets (wallets_a) { } nano::wallet::wallet (bool & init_a, store::transaction & transaction_a, nano::wallets & wallets_a, std::string const & wallet_a, std::string const & json) : lock_observer ([] (bool, bool) {}), - store (init_a, wallets_a.kdf, transaction_a, wallets_a.node.config.random_representative (), wallets_a.node.config.password_fanout, wallet_a, json), + store (init_a, wallets_a.kdf, transaction_a, wallets_a.env, wallets_a.node.config.random_representative (), wallets_a.node.config.password_fanout, wallet_a, json), wallets (wallets_a) { } @@ -798,7 +800,7 @@ bool nano::wallet::import (std::string const & json_a, std::string const & passw auto transaction (wallets.tx_begin_write ()); nano::uint256_union id; random_pool::generate_block (id.bytes.data (), id.bytes.size ()); - temp = std::make_unique (error, wallets.node.wallets.kdf, transaction, 0, 1, id.to_string (), json_a); + temp = std::make_unique (error, wallets.node.wallets.kdf, transaction, wallets.env, 0, 1, id.to_string (), json_a); } if (!error) { @@ -822,7 +824,7 @@ void nano::wallet::serialize (std::string & json_a) void nano::wallet_store::destroy (store::transaction const & transaction_a) { - auto status (mdb_drop (tx (transaction_a), handle, 1)); + auto status (mdb_drop (env.tx (transaction_a), handle, 1)); (void)status; debug_assert (status == 0); handle = 0; @@ -1348,8 +1350,8 @@ nano::wallets::wallets (bool error_a, nano::node & node_a) : release_assert (status == 0); std::string beginning (nano::uint256_union (0).to_string ()); std::string end ((nano::uint256_union (nano::uint256_t (0) - nano::uint256_t (1))).to_string ()); - store::iterator, nano::no_value> i (std::make_unique, nano::no_value>> (transaction, handle, nano::store::lmdb::db_val (beginning.size (), const_cast (beginning.c_str ())))); - store::iterator, nano::no_value> n (std::make_unique, nano::no_value>> (transaction, handle, nano::store::lmdb::db_val (end.size (), const_cast (end.c_str ())))); + store::iterator, nano::no_value> i (std::make_unique, nano::no_value>> (transaction, env, handle, nano::store::lmdb::db_val (beginning.size (), const_cast (beginning.c_str ())))); + store::iterator, nano::no_value> n (std::make_unique, nano::no_value>> (transaction, env, handle, nano::store::lmdb::db_val (end.size (), const_cast (end.c_str ())))); for (; i != n; ++i) { nano::wallet_id id; @@ -1477,8 +1479,8 @@ void nano::wallets::reload () std::unordered_set stored_items; std::string beginning (nano::uint256_union (0).to_string ()); std::string end ((nano::uint256_union (nano::uint256_t (0) - nano::uint256_t (1))).to_string ()); - store::iterator, nano::no_value> i (std::make_unique, nano::no_value>> (transaction, handle, nano::store::lmdb::db_val (beginning.size (), const_cast (beginning.c_str ())))); - store::iterator, nano::no_value> n (std::make_unique, nano::no_value>> (transaction, handle, nano::store::lmdb::db_val (end.size (), const_cast (end.c_str ())))); + store::iterator, nano::no_value> i (std::make_unique, nano::no_value>> (transaction, env, handle, nano::store::lmdb::db_val (beginning.size (), const_cast (beginning.c_str ())))); + store::iterator, nano::no_value> n (std::make_unique, nano::no_value>> (transaction, env, handle, nano::store::lmdb::db_val (end.size (), const_cast (end.c_str ())))); for (; i != n; ++i) { nano::wallet_id id; @@ -1706,8 +1708,8 @@ void nano::wallets::split_if_needed (store::transaction & transaction_destinatio std::string beginning (nano::uint256_union (0).to_string ()); std::string end ((nano::uint256_union (nano::uint256_t (0) - nano::uint256_t (1))).to_string ()); - auto get_store_it = [&handle = handle] (store::transaction const & transaction_source, std::string const & hash) { - return store::iterator, nano::no_value> (std::make_unique, nano::no_value>> (transaction_source, handle, nano::store::lmdb::db_val (hash.size (), const_cast (hash.c_str ())))); + auto get_store_it = [&handle = handle, &env = env] (store::transaction const & transaction_source, std::string const & hash) { + return store::iterator, nano::no_value> (std::make_unique, nano::no_value>> (transaction_source, env, handle, nano::store::lmdb::db_val (hash.size (), const_cast (hash.c_str ())))); }; // First do a read pass to check if there are any wallets that need extracting (to save holding a write lock and potentially being blocked) @@ -1781,13 +1783,13 @@ nano::uint128_t const nano::wallets::high_priority = std::numeric_limits nano::wallet_store::begin (store::transaction const & transaction_a) { - store::iterator result (std::make_unique> (transaction_a, handle, nano::store::lmdb::db_val (nano::account (special_count)))); + store::iterator result (std::make_unique> (transaction_a, env, handle, nano::store::lmdb::db_val (nano::account (special_count)))); return result; } nano::store::iterator nano::wallet_store::begin (store::transaction const & transaction_a, nano::account const & key) { - store::iterator result (std::make_unique> (transaction_a, handle, nano::store::lmdb::db_val (key))); + store::iterator result (std::make_unique> (transaction_a, env, handle, nano::store::lmdb::db_val (key))); return result; } @@ -1827,11 +1829,6 @@ bool nano::mdb_wallets_store::init_error () const return error; } -MDB_txn * nano::wallet_store::tx (store::transaction const & transaction_a) const -{ - return static_cast (transaction_a.get_handle ()); -} - std::unique_ptr nano::collect_container_info (wallets & wallets, std::string const & name) { std::size_t items_count; diff --git a/nano/node/wallet.hpp b/nano/node/wallet.hpp index c585ae2995..9f3e86830e 100644 --- a/nano/node/wallet.hpp +++ b/nano/node/wallet.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -13,11 +14,13 @@ #include #include #include + namespace nano { class node; class node_config; class wallets; + // The fan spreads a key out over the heap to decrease the likelihood of it being recovered by memory inspection class fan final { @@ -31,6 +34,7 @@ class fan final nano::mutex mutex; void value_get (nano::raw_key &); }; + class kdf final { public: @@ -42,6 +46,7 @@ class kdf final nano::mutex mutex; unsigned & kdf_work; }; + enum class key_type { not_a_type, @@ -49,11 +54,12 @@ enum class key_type adhoc, deterministic }; + class wallet_store final { public: - wallet_store (bool &, nano::kdf &, store::transaction &, nano::account, unsigned, std::string const &); - wallet_store (bool &, nano::kdf &, store::transaction &, nano::account, unsigned, std::string const &, std::string const &); + wallet_store (bool &, nano::kdf &, store::transaction &, store::lmdb::env &, nano::account, unsigned, std::string const &); + wallet_store (bool &, nano::kdf &, store::transaction &, store::lmdb::env &, nano::account, unsigned, std::string const &, std::string const &); std::vector accounts (store::transaction const &); void initialize (store::transaction const &, bool &, std::string const &); nano::uint256_union check (store::transaction const &); @@ -118,8 +124,9 @@ class wallet_store final std::recursive_mutex mutex; private: - MDB_txn * tx (store::transaction const &) const; + nano::store::lmdb::env & env; }; + // A wallet is a set of account keys encrypted by a common encryption key class wallet final : public std::enable_shared_from_this { @@ -233,9 +240,9 @@ class wallets final std::thread thread; static nano::uint128_t const generate_priority; static nano::uint128_t const high_priority; + /** Start read-write transaction */ store::write_transaction tx_begin_write (); - /** Start read-only transaction */ store::read_transaction tx_begin_read (); @@ -252,6 +259,7 @@ class wallets_store virtual ~wallets_store () = default; virtual bool init_error () const = 0; }; + class mdb_wallets_store final : public wallets_store { public: diff --git a/nano/store/lmdb/iterator.hpp b/nano/store/lmdb/iterator.hpp index 9a9addf3ff..ff7cb0977c 100644 --- a/nano/store/lmdb/iterator.hpp +++ b/nano/store/lmdb/iterator.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -13,9 +14,9 @@ template class iterator : public iterator_impl { public: - iterator (store::transaction const & transaction_a, MDB_dbi db_a, MDB_val const & val_a = MDB_val{}, bool const direction_asc = true) + iterator (store::transaction const & transaction_a, env const & env_a, MDB_dbi db_a, MDB_val const & val_a = MDB_val{}, bool const direction_asc = true) { - auto status (mdb_cursor_open (tx (transaction_a), db_a, &cursor)); + auto status (mdb_cursor_open (env_a.tx (transaction_a), db_a, &cursor)); release_assert (status == 0); auto operation (MDB_SET_RANGE); if (val_a.mv_size != 0) @@ -94,7 +95,7 @@ class iterator : public iterator_impl return *this; } - std::pair, store::db_val> * operator-> () + std::pair, store::db_val> * operator->() { return ¤t; } @@ -165,12 +166,6 @@ class iterator : public iterator_impl store::iterator_impl & operator= (store::iterator_impl const &) = delete; MDB_cursor * cursor{ nullptr }; std::pair, store::db_val> current; - -private: - MDB_txn * tx (store::transaction const & transaction_a) const - { - return static_cast (transaction_a.get_handle ()); - } }; /** @@ -218,9 +213,9 @@ class merge_iterator : public iterator_impl return *this; } - std::pair, store::db_val> * operator-> () + std::pair, store::db_val> * operator->() { - return least_iterator ().operator-> (); + return least_iterator ().operator->(); } bool operator== (merge_iterator const & other) const diff --git a/nano/store/lmdb/lmdb.cpp b/nano/store/lmdb/lmdb.cpp index 310ef25c48..1e419bae35 100644 --- a/nano/store/lmdb/lmdb.cpp +++ b/nano/store/lmdb/lmdb.cpp @@ -406,7 +406,7 @@ void nano::store::lmdb::component::rebuild_db (store::write_transaction const & MDB_dbi temp; mdb_dbi_open (env.tx (transaction_a), "temp_table", MDB_CREATE, &temp); // Copy all values to temporary table - for (auto i (store::iterator (std::make_unique> (transaction_a, table))), n (store::iterator (nullptr)); i != n; ++i) + for (auto i (store::iterator (std::make_unique> (transaction_a, env, table))), n (store::iterator (nullptr)); i != n; ++i) { auto s = mdb_put (env.tx (transaction_a), temp, nano::store::lmdb::db_val (i->first), i->second, MDB_APPEND); release_assert_success (s); @@ -415,7 +415,7 @@ void nano::store::lmdb::component::rebuild_db (store::write_transaction const & // Clear existing table mdb_drop (env.tx (transaction_a), table, 0); // Put values from copy - for (auto i (store::iterator (std::make_unique> (transaction_a, temp))), n (store::iterator (nullptr)); i != n; ++i) + for (auto i (store::iterator (std::make_unique> (transaction_a, env, temp))), n (store::iterator (nullptr)); i != n; ++i) { auto s = mdb_put (env.tx (transaction_a), table, nano::store::lmdb::db_val (i->first), i->second, MDB_APPEND); release_assert_success (s); @@ -429,7 +429,7 @@ void nano::store::lmdb::component::rebuild_db (store::write_transaction const & MDB_dbi temp; mdb_dbi_open (env.tx (transaction_a), "temp_table", MDB_CREATE, &temp); // Copy all values to temporary table - for (auto i (store::iterator (std::make_unique> (transaction_a, pending_store.pending_handle))), n (store::iterator (nullptr)); i != n; ++i) + for (auto i (store::iterator (std::make_unique> (transaction_a, env, pending_store.pending_handle))), n (store::iterator (nullptr)); i != n; ++i) { auto s = mdb_put (env.tx (transaction_a), temp, nano::store::lmdb::db_val (i->first), nano::store::lmdb::db_val (i->second), MDB_APPEND); release_assert_success (s); @@ -437,7 +437,7 @@ void nano::store::lmdb::component::rebuild_db (store::write_transaction const & release_assert (count (transaction_a, pending_store.pending_handle) == count (transaction_a, temp)); mdb_drop (env.tx (transaction_a), pending_store.pending_handle, 0); // Put values from copy - for (auto i (store::iterator (std::make_unique> (transaction_a, temp))), n (store::iterator (nullptr)); i != n; ++i) + for (auto i (store::iterator (std::make_unique> (transaction_a, env, temp))), n (store::iterator (nullptr)); i != n; ++i) { auto s = mdb_put (env.tx (transaction_a), pending_store.pending_handle, nano::store::lmdb::db_val (i->first), nano::store::lmdb::db_val (i->second), MDB_APPEND); release_assert_success (s); diff --git a/nano/store/lmdb/lmdb.hpp b/nano/store/lmdb/lmdb.hpp index 6e935ed5fc..eda13d4c98 100644 --- a/nano/store/lmdb/lmdb.hpp +++ b/nano/store/lmdb/lmdb.hpp @@ -105,13 +105,13 @@ class component : public nano::store::component template store::iterator make_iterator (store::transaction const & transaction_a, tables table_a, bool const direction_asc = true) const { - return store::iterator (std::make_unique> (transaction_a, table_to_dbi (table_a), nano::store::lmdb::db_val{}, direction_asc)); + return store::iterator (std::make_unique> (transaction_a, env, table_to_dbi (table_a), nano::store::lmdb::db_val{}, direction_asc)); } template store::iterator make_iterator (store::transaction const & transaction_a, tables table_a, nano::store::lmdb::db_val const & key) const { - return store::iterator (std::make_unique> (transaction_a, table_to_dbi (table_a), key)); + return store::iterator (std::make_unique> (transaction_a, env, table_to_dbi (table_a), key)); } bool init_error () const override; diff --git a/nano/store/lmdb/lmdb_env.cpp b/nano/store/lmdb/lmdb_env.cpp index b34289e7f1..69eea72a49 100644 --- a/nano/store/lmdb/lmdb_env.cpp +++ b/nano/store/lmdb/lmdb_env.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -95,15 +96,20 @@ nano::store::lmdb::env::operator MDB_env * () const nano::store::read_transaction nano::store::lmdb::env::tx_begin_read (store::lmdb::txn_callbacks mdb_txn_callbacks) const { - return store::read_transaction{ std::make_unique (*this, mdb_txn_callbacks) }; + auto tx = store::read_transaction{ std::make_unique (*this, mdb_txn_callbacks) }; + tx.store_id = store_id; + return tx; } nano::store::write_transaction nano::store::lmdb::env::tx_begin_write (store::lmdb::txn_callbacks mdb_txn_callbacks) const { - return store::write_transaction{ std::make_unique (*this, mdb_txn_callbacks) }; + auto tx = store::write_transaction{ std::make_unique (*this, mdb_txn_callbacks) }; + tx.store_id = store_id; + return tx; } MDB_txn * nano::store::lmdb::env::tx (store::transaction const & transaction_a) const { + release_assert (transaction_a.store_id == store_id); return static_cast (transaction_a.get_handle ()); } diff --git a/nano/store/lmdb/lmdb_env.hpp b/nano/store/lmdb/lmdb_env.hpp index 21634509db..42f653531f 100644 --- a/nano/store/lmdb/lmdb_env.hpp +++ b/nano/store/lmdb/lmdb_env.hpp @@ -1,9 +1,15 @@ #pragma once +#include #include #include #include +namespace +{ +nano::id_dispenser id_gen; +} + namespace nano::store::lmdb { /** @@ -62,5 +68,8 @@ class env final store::write_transaction tx_begin_write (txn_callbacks callbacks = txn_callbacks{}) const; MDB_txn * tx (store::transaction const & transaction_a) const; MDB_env * environment; + +private: + nano::id_dispenser::id_t store_id{ id_gen.next_id () }; }; } // namespace nano::store::lmdb diff --git a/nano/store/transaction.hpp b/nano/store/transaction.hpp index 5061886907..18d82d9cbc 100644 --- a/nano/store/transaction.hpp +++ b/nano/store/transaction.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -30,6 +31,9 @@ class write_transaction_impl : public transaction_impl class transaction { +public: + nano::id_dispenser::id_t store_id; + public: virtual ~transaction () = default; virtual void * get_handle () const = 0;