From 452931dd0b9ad3bff58de5c491292562d1e3bbc7 Mon Sep 17 00:00:00 2001 From: Wesley Shillingford Date: Thu, 14 Jan 2021 12:41:35 +0000 Subject: [PATCH] Wallet store adhoc keys inconsitency with reps container --- nano/node/lmdb/lmdb_txn.cpp | 11 ++++++++--- nano/node/lmdb/lmdb_txn.hpp | 1 + nano/node/rocksdb/rocksdb_txn.cpp | 25 +++++++++++++++---------- nano/node/rocksdb/rocksdb_txn.hpp | 1 + nano/node/wallet.cpp | 17 +++++++---------- nano/node/wallet.hpp | 1 - 6 files changed, 32 insertions(+), 24 deletions(-) diff --git a/nano/node/lmdb/lmdb_txn.cpp b/nano/node/lmdb/lmdb_txn.cpp index b7c438a5e4..cfc687efa9 100644 --- a/nano/node/lmdb/lmdb_txn.cpp +++ b/nano/node/lmdb/lmdb_txn.cpp @@ -97,9 +97,13 @@ nano::write_mdb_txn::~write_mdb_txn () void nano::write_mdb_txn::commit () { - auto status (mdb_txn_commit (handle)); - release_assert (status == MDB_SUCCESS); - txn_callbacks.txn_end (this); + if (active) + { + auto status (mdb_txn_commit (handle)); + release_assert (status == MDB_SUCCESS); + txn_callbacks.txn_end (this); + active = false; + } } void nano::write_mdb_txn::renew () @@ -107,6 +111,7 @@ void nano::write_mdb_txn::renew () auto status (mdb_txn_begin (env, nullptr, 0, &handle)); release_assert (status == MDB_SUCCESS); txn_callbacks.txn_start (this); + active = true; } void * nano::write_mdb_txn::get_handle () const diff --git a/nano/node/lmdb/lmdb_txn.hpp b/nano/node/lmdb/lmdb_txn.hpp index 8e645d74af..34b68902fd 100644 --- a/nano/node/lmdb/lmdb_txn.hpp +++ b/nano/node/lmdb/lmdb_txn.hpp @@ -48,6 +48,7 @@ class write_mdb_txn final : public write_transaction_impl MDB_txn * handle; nano::mdb_env const & env; mdb_txn_callbacks txn_callbacks; + bool active{ true }; }; class mdb_txn_stats diff --git a/nano/node/rocksdb/rocksdb_txn.cpp b/nano/node/rocksdb/rocksdb_txn.cpp index e03207b683..76bd48e54b 100644 --- a/nano/node/rocksdb/rocksdb_txn.cpp +++ b/nano/node/rocksdb/rocksdb_txn.cpp @@ -53,18 +53,22 @@ nano::write_rocksdb_txn::~write_rocksdb_txn () void nano::write_rocksdb_txn::commit () { - auto status = txn->Commit (); - - // If there are no available memtables try again a few more times - constexpr auto num_attempts = 10; - auto attempt_num = 0; - while (status.IsTryAgain () && attempt_num < num_attempts) + if (active) { - status = txn->Commit (); - ++attempt_num; - } + auto status = txn->Commit (); - release_assert (status.ok ()); + // If there are no available memtables try again a few more times + constexpr auto num_attempts = 10; + auto attempt_num = 0; + while (status.IsTryAgain () && attempt_num < num_attempts) + { + status = txn->Commit (); + ++attempt_num; + } + + release_assert (status.ok ()); + active = false; + } } void nano::write_rocksdb_txn::renew () @@ -72,6 +76,7 @@ void nano::write_rocksdb_txn::renew () rocksdb::OptimisticTransactionOptions txn_options; txn_options.set_snapshot = true; db->BeginTransaction (rocksdb::WriteOptions (), txn_options, txn); + active = true; } void * nano::write_rocksdb_txn::get_handle () const diff --git a/nano/node/rocksdb/rocksdb_txn.hpp b/nano/node/rocksdb/rocksdb_txn.hpp index 3dc15c13d9..859d877e0e 100644 --- a/nano/node/rocksdb/rocksdb_txn.hpp +++ b/nano/node/rocksdb/rocksdb_txn.hpp @@ -41,6 +41,7 @@ class write_rocksdb_txn final : public write_transaction_impl std::vector tables_requiring_locks; std::vector tables_no_locks; std::unordered_map & mutexes; + bool active{ true }; void lock (); void unlock (); diff --git a/nano/node/wallet.cpp b/nano/node/wallet.cpp index 734d4ed41e..b1e8d39420 100644 --- a/nano/node/wallet.cpp +++ b/nano/node/wallet.cpp @@ -751,18 +751,22 @@ nano::public_key nano::wallet::deterministic_insert (bool generate_work_a) return result; } -nano::public_key nano::wallet::insert_adhoc (nano::transaction const & transaction_a, nano::raw_key const & key_a, bool generate_work_a) +nano::public_key nano::wallet::insert_adhoc (nano::raw_key const & key_a, bool generate_work_a) { nano::public_key key (0); - if (store.valid_password (transaction_a)) + auto transaction (wallets.tx_begin_write ()); + if (store.valid_password (transaction)) { - key = store.insert_adhoc (transaction_a, key_a); + key = store.insert_adhoc (transaction, key_a); auto block_transaction (wallets.node.store.tx_begin_read ()); if (generate_work_a) { work_ensure (key, wallets.node.ledger.latest_root (block_transaction, key)); } auto half_principal_weight (wallets.node.minimum_principal_weight () / 2); + // Makes sure that the representatives container will + // be in sync with any added keys. + transaction.commit (); if (wallets.check_rep (key, half_principal_weight)) { nano::lock_guard lock (representatives_mutex); @@ -772,13 +776,6 @@ nano::public_key nano::wallet::insert_adhoc (nano::transaction const & transacti return key; } -nano::public_key nano::wallet::insert_adhoc (nano::raw_key const & account_a, bool generate_work_a) -{ - auto transaction (wallets.tx_begin_write ()); - auto result (insert_adhoc (transaction, account_a, generate_work_a)); - return result; -} - bool nano::wallet::insert_watch (nano::transaction const & transaction_a, nano::public_key const & pub_a) { return store.insert_watch (transaction_a, pub_a); diff --git a/nano/node/wallet.hpp b/nano/node/wallet.hpp index e4be18cacb..f0cda5347a 100644 --- a/nano/node/wallet.hpp +++ b/nano/node/wallet.hpp @@ -128,7 +128,6 @@ class wallet final : public std::enable_shared_from_this void enter_initial_password (); bool enter_password (nano::transaction const &, std::string const &); nano::public_key insert_adhoc (nano::raw_key const &, bool = true); - nano::public_key insert_adhoc (nano::transaction const &, nano::raw_key const &, bool = true); bool insert_watch (nano::transaction const &, nano::public_key const &); nano::public_key deterministic_insert (nano::transaction const &, bool = true); nano::public_key deterministic_insert (uint32_t, bool = true);