From fdb74e07e1caf11f634e7fd9b85eff0031d4ef72 Mon Sep 17 00:00:00 2001 From: Sergey Kroshnin Date: Wed, 23 Jan 2019 21:33:48 +0300 Subject: [PATCH] Returning list in RPC representatives_online (#777) --- nano/core_test/rpc.cpp | 22 +++++++++++++++++++--- nano/node/rpc.cpp | 13 ++++++++++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/nano/core_test/rpc.cpp b/nano/core_test/rpc.cpp index 7d9f6d9a..0953dff7 100644 --- a/nano/core_test/rpc.cpp +++ b/nano/core_test/rpc.cpp @@ -3839,7 +3839,8 @@ TEST (rpc, online_reps) nano::system system (24000, 2); system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv); ASSERT_TRUE (system.nodes[1]->online_reps.online_stake () == system.nodes[1]->config.online_weight_minimum.number ()); - system.wallet (0)->send_action (nano::test_genesis_key.pub, nano::test_genesis_key.pub, nano::Gxrb_ratio); + auto send_block (system.wallet (0)->send_action (nano::test_genesis_key.pub, nano::test_genesis_key.pub, nano::Gxrb_ratio)); + ASSERT_NE (nullptr, send_block); system.deadline_set (10s); while (system.nodes[1]->online_reps.online_stake () == system.nodes[1]->config.online_weight_minimum.number ()) { @@ -3851,7 +3852,6 @@ TEST (rpc, online_reps) request.put ("action", "representatives_online"); test_response response (request, rpc, system.io_ctx); system.deadline_set (5s); - system.deadline_set (5s); while (response.status == 0) { ASSERT_NO_ERROR (system.poll ()); @@ -3860,9 +3860,13 @@ TEST (rpc, online_reps) auto representatives (response.json.get_child ("representatives")); auto item (representatives.begin ()); ASSERT_NE (representatives.end (), item); - ASSERT_EQ (nano::test_genesis_key.pub.to_account (), item->first); + ASSERT_EQ (nano::test_genesis_key.pub.to_account (), item->second.get ("")); boost::optional weight (item->second.get_optional ("weight")); ASSERT_FALSE (weight.is_initialized ()); + while (system.nodes[1]->block (send_block->hash ()) == nullptr) + { + ASSERT_NO_ERROR (system.poll ()); + } //Test weight option request.put ("weight", "true"); test_response response2 (request, rpc, system.io_ctx); @@ -3881,10 +3885,22 @@ TEST (rpc, online_reps) auto new_rep (system.wallet (1)->deterministic_insert ()); auto send (system.wallet (1)->send_action (nano::test_genesis_key.pub, new_rep, system.nodes[0]->config.receive_minimum.number ())); ASSERT_NE (nullptr, send); + while (system.nodes[1]->block (send->hash ()) == nullptr) + { + ASSERT_NO_ERROR (system.poll ()); + } auto receive (system.wallet (1)->receive_action (*send, new_rep, system.nodes[0]->config.receive_minimum.number ())); ASSERT_NE (nullptr, receive); + while (system.nodes[1]->block (receive->hash ()) == nullptr) + { + ASSERT_NO_ERROR (system.poll ()); + } auto change (system.wallet (1)->change_action (nano::test_genesis_key.pub, new_rep)); ASSERT_NE (nullptr, change); + while (system.nodes[1]->block (change->hash ()) == nullptr) + { + ASSERT_NO_ERROR (system.poll ()); + } system.deadline_set (5s); while (system.nodes[1]->online_reps.list ().size () != 2) { diff --git a/nano/node/rpc.cpp b/nano/node/rpc.cpp index 9dad36e6..3ebbfb11 100644 --- a/nano/node/rpc.cpp +++ b/nano/node/rpc.cpp @@ -2667,6 +2667,7 @@ void nano::rpc_handler::representatives_online () if (!ec) { boost::property_tree::ptree representatives; + auto transaction (node.store.tx_begin_read ()); auto reps (node.online_reps.list ()); for (auto & i : reps) { @@ -2686,13 +2687,19 @@ void nano::rpc_handler::representatives_online () accounts_to_filter.erase (found_acc); } } - boost::property_tree::ptree weight_node; if (weight) { - auto account_weight (node.weight (i)); + boost::property_tree::ptree weight_node; + auto account_weight (node.ledger.weight (transaction, i)); weight_node.put ("weight", account_weight.convert_to ()); + representatives.add_child (i.to_account (), weight_node); + } + else + { + boost::property_tree::ptree entry; + entry.put ("", i.to_account ()); + representatives.push_back (std::make_pair ("", entry)); } - representatives.add_child (i.to_account (), weight_node); } response_l.add_child ("representatives", representatives); }