From 57dcaf2aea8dc5715976dd953edbbc131ad8811e Mon Sep 17 00:00:00 2001 From: Sergey Kroshnin Date: Thu, 3 Jan 2019 01:11:37 +0300 Subject: [PATCH] "update_existing_accounts" option for RPC wallet_representative_set (#1531) --- nano/core_test/rpc.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ nano/node/rpc.cpp | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/nano/core_test/rpc.cpp b/nano/core_test/rpc.cpp index 93f99d86..5b3d3126 100644 --- a/nano/core_test/rpc.cpp +++ b/nano/core_test/rpc.cpp @@ -567,6 +567,46 @@ TEST (rpc, wallet_representative_set) ASSERT_EQ (key.pub, system.nodes[0]->wallets.items.begin ()->second->store.representative (transaction)); } +TEST (rpc, wallet_representative_set_force) +{ + nano::system system (24000, 1); + nano::rpc rpc (system.io_ctx, *system.nodes[0], nano::rpc_config (true)); + system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv); + rpc.start (); + boost::property_tree::ptree request; + std::string wallet; + system.nodes[0]->wallets.items.begin ()->first.encode_hex (wallet); + request.put ("wallet", wallet); + nano::keypair key; + request.put ("action", "wallet_representative_set"); + request.put ("representative", key.pub.to_account ()); + request.put ("update_existing_accounts", true); + test_response response (request, rpc, system.io_ctx); + system.deadline_set (5s); + while (response.status == 0) + { + ASSERT_NO_ERROR (system.poll ()); + } + ASSERT_EQ (200, response.status); + { + auto transaction (system.nodes[0]->wallets.tx_begin ()); + ASSERT_EQ (key.pub, system.nodes[0]->wallets.items.begin ()->second->store.representative (transaction)); + } + nano::account representative (0); + while (representative != key.pub) + { + auto transaction (system.nodes[0]->store.tx_begin_read ()); + nano::account_info info; + if (!system.nodes[0]->store.account_get (transaction, nano::test_genesis_key.pub, info)) + { + auto block (system.nodes[0]->store.block_get (transaction, info.rep_block)); + assert (block != nullptr); + representative = block->representative (); + } + ASSERT_NO_ERROR (system.poll ()); + } +} + TEST (rpc, account_list) { nano::system system (24000, 1); diff --git a/nano/node/rpc.cpp b/nano/node/rpc.cpp index 299c6dec..037c57a4 100644 --- a/nano/node/rpc.cpp +++ b/nano/node/rpc.cpp @@ -3486,8 +3486,36 @@ void nano::rpc_handler::wallet_representative_set () nano::account representative; if (!representative.decode_account (representative_text)) { - auto transaction (node.store.tx_begin_write ()); - wallet->store.representative_set (transaction, representative); + { + auto transaction (node.store.tx_begin_write ()); + wallet->store.representative_set (transaction, representative); + } + // Change representative for all wallet accounts + if (request.get ("update_existing_accounts", false)) + { + std::vector accounts; + { + auto transaction (node.store.tx_begin_read ()); + for (auto i (wallet->store.begin (transaction)), n (wallet->store.end ()); i != n; ++i) + { + nano::account account (i->first); + nano::account_info info; + if (!node.store.account_get (transaction, account, info)) + { + auto block (node.store.block_get (transaction, info.rep_block)); + assert (block != nullptr); + if (block->representative () != representative) + { + accounts.push_back (account); + } + } + } + } + for (auto & account : accounts) + { + wallet->change_async (account, representative, [](std::shared_ptr) {}, false); + } + } response_l.put ("set", "1"); } else