From ba6196ca86c6882d45a3459f1654bcd73d00d819 Mon Sep 17 00:00:00 2001 From: Lee Bousfield Date: Mon, 14 Jan 2019 07:55:26 -0500 Subject: [PATCH] Add accounts parameter to wallet_change_seed (#775) * Add accounts parameter to wallet_change_seed * Fix merge * Update lmdb * merge fix * merge fix * Disable work generation for change_seed & use optional "count" in RPC wallet_change_seed * Typo --- nano/node/rpc.cpp | 3 ++- nano/node/wallet.cpp | 44 +++++++++++++++++++++++--------------------- nano/node/wallet.hpp | 2 +- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/nano/node/rpc.cpp b/nano/node/rpc.cpp index b4b9aaa5..60dfc7f4 100644 --- a/nano/node/rpc.cpp +++ b/nano/node/rpc.cpp @@ -3187,10 +3187,11 @@ void nano::rpc_handler::wallet_change_seed () nano::raw_key seed; if (!seed.data.decode_hex (seed_text)) { + auto count (count_optional_impl ()); auto transaction (node.store.tx_begin_write ()); if (wallet->store.valid_password (transaction)) { - wallet->change_seed (transaction, seed); + wallet->change_seed (transaction, seed, count); response_l.put ("success", ""); } else diff --git a/nano/node/wallet.cpp b/nano/node/wallet.cpp index 6cf3b045..ae40e217 100644 --- a/nano/node/wallet.cpp +++ b/nano/node/wallet.cpp @@ -1207,40 +1207,42 @@ void nano::wallet::init_free_accounts (nano::transaction const & transaction_a) } } -nano::public_key nano::wallet::change_seed (nano::transaction const & transaction_a, nano::raw_key const & prv_a) +nano::public_key nano::wallet::change_seed (nano::transaction const & transaction_a, nano::raw_key const & prv_a, uint32_t count) { store.seed_set (transaction_a, prv_a); auto account = deterministic_insert (transaction_a); - uint32_t count (0); - for (uint32_t i (1), n (64); i < n; ++i) + if (count == 0) { - nano::raw_key prv; - store.deterministic_key (prv, transaction_a, i); - nano::keypair pair (prv.data.to_string ()); - // Check if account received at least 1 block - auto latest (wallets.node.ledger.latest (transaction_a, pair.pub)); - if (!latest.is_zero ()) + for (uint32_t i (1), n (64); i < n; ++i) { - count = i; - // i + 64 - Check additional 64 accounts - // i/64 - Check additional accounts for large wallets. I.e. 64000/64 = 1000 accounts to check - n = i + 64 + (i / 64); - } - else - { - // Check if there are pending blocks for account - for (auto ii (wallets.node.store.pending_begin (transaction_a, nano::pending_key (pair.pub, 0))); nano::pending_key (ii->first).account == pair.pub; ++ii) + nano::raw_key prv; + store.deterministic_key (prv, transaction_a, i); + nano::keypair pair (prv.data.to_string ()); + // Check if account received at least 1 block + auto latest (wallets.node.ledger.latest (transaction_a, pair.pub)); + if (!latest.is_zero ()) { count = i; + // i + 64 - Check additional 64 accounts + // i/64 - Check additional accounts for large wallets. I.e. 64000/64 = 1000 accounts to check n = i + 64 + (i / 64); - break; + } + else + { + // Check if there are pending blocks for account + for (auto ii (wallets.node.store.pending_begin (transaction_a, nano::pending_key (pair.pub, 0))); nano::pending_key (ii->first).account == pair.pub; ++ii) + { + count = i; + n = i + 64 + (i / 64); + break; + } } } } for (uint32_t i (0); i < count; ++i) { - // Generate work for first 4 accounts only to prevent weak CPU nodes stuck - account = deterministic_insert (transaction_a, i < 4); + // Disable work generation to prevent weak CPU nodes stuck + account = deterministic_insert (transaction_a, false); } return account; diff --git a/nano/node/wallet.hpp b/nano/node/wallet.hpp index 26cd6f22..66311c64 100644 --- a/nano/node/wallet.hpp +++ b/nano/node/wallet.hpp @@ -151,7 +151,7 @@ public: bool search_pending (); void init_free_accounts (nano::transaction const &); /** Changes the wallet seed and returns the first account */ - nano::public_key change_seed (nano::transaction const & transaction_a, nano::raw_key const & prv_a); + nano::public_key change_seed (nano::transaction const & transaction_a, nano::raw_key const & prv_a, uint32_t = 0); bool live (); std::unordered_set free_accounts; std::function lock_observer;