Skip to content

Commit

Permalink
Wallet store adhoc keys inconsitency with reps container (nanocurrenc…
Browse files Browse the repository at this point in the history
  • Loading branch information
wezrule authored Jan 28, 2021
1 parent a28d9ed commit 4c4171d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 24 deletions.
11 changes: 8 additions & 3 deletions nano/node/lmdb/lmdb_txn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,21 @@ 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 ()
{
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
Expand Down
1 change: 1 addition & 0 deletions nano/node/lmdb/lmdb_txn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 15 additions & 10 deletions nano/node/rocksdb/rocksdb_txn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,30 @@ 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 ()
{
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
Expand Down
1 change: 1 addition & 0 deletions nano/node/rocksdb/rocksdb_txn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class write_rocksdb_txn final : public write_transaction_impl
std::vector<nano::tables> tables_requiring_locks;
std::vector<nano::tables> tables_no_locks;
std::unordered_map<nano::tables, std::mutex> & mutexes;
bool active{ true };

void lock ();
void unlock ();
Expand Down
17 changes: 7 additions & 10 deletions nano/node/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> lock (representatives_mutex);
Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion nano/node/wallet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ class wallet final : public std::enable_shared_from_this<nano::wallet>
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);
Expand Down

0 comments on commit 4c4171d

Please sign in to comment.