From 460e4140fde991eff79e8fa69a188a5e1062f377 Mon Sep 17 00:00:00 2001 From: Sergey Kroshnin Date: Wed, 23 Jan 2019 21:32:48 +0300 Subject: [PATCH] Optional search by hash in confirmation_history (#974) --- nano/core_test/rpc.cpp | 46 +++++++++++++++++++++++++++++++++++++++++- nano/node/rpc.cpp | 22 ++++++++++++++------ 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/nano/core_test/rpc.cpp b/nano/core_test/rpc.cpp index c48184a0..7d9f6d9a 100644 --- a/nano/core_test/rpc.cpp +++ b/nano/core_test/rpc.cpp @@ -3912,9 +3912,10 @@ TEST (rpc, online_reps) TEST (rpc, confirmation_history) { nano::system system (24000, 1); + nano::keypair key; system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv); - auto block (system.wallet (0)->send_action (nano::test_genesis_key.pub, nano::test_genesis_key.pub, nano::Gxrb_ratio)); ASSERT_TRUE (system.nodes[0]->active.confirmed.empty ()); + auto block (system.wallet (0)->send_action (nano::test_genesis_key.pub, key.pub, nano::Gxrb_ratio)); system.deadline_set (10s); while (system.nodes[0]->active.confirmed.empty ()) { @@ -3936,6 +3937,8 @@ TEST (rpc, confirmation_history) ASSERT_NE (representatives.end (), item); auto hash (item->second.get ("hash")); auto tally (item->second.get ("tally")); + ASSERT_FALSE (item->second.get ("duration", "").empty ()); + ASSERT_FALSE (item->second.get ("time", "").empty ()); ASSERT_EQ (block->hash ().to_string (), hash); nano::amount tally_num; tally_num.decode_dec (tally); @@ -3943,6 +3946,47 @@ TEST (rpc, confirmation_history) system.stop (); } +TEST (rpc, confirmation_history_hash) +{ + nano::system system (24000, 1); + nano::keypair key; + system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv); + ASSERT_TRUE (system.nodes[0]->active.confirmed.empty ()); + auto send1 (system.wallet (0)->send_action (nano::test_genesis_key.pub, key.pub, nano::Gxrb_ratio)); + auto send2 (system.wallet (0)->send_action (nano::test_genesis_key.pub, key.pub, nano::Gxrb_ratio)); + auto send3 (system.wallet (0)->send_action (nano::test_genesis_key.pub, key.pub, nano::Gxrb_ratio)); + system.deadline_set (10s); + while (system.nodes[0]->active.confirmed.size () != 3) + { + ASSERT_NO_ERROR (system.poll ()); + } + nano::rpc rpc (system.io_ctx, *system.nodes[0], nano::rpc_config (true)); + rpc.start (); + boost::property_tree::ptree request; + request.put ("action", "confirmation_history"); + request.put ("hash", send2->hash ().to_string ()); + 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 representatives (response.json.get_child ("confirmations")); + ASSERT_EQ (representatives.size (), 1); + auto item (representatives.begin ()); + ASSERT_NE (representatives.end (), item); + auto hash (item->second.get ("hash")); + auto tally (item->second.get ("tally")); + ASSERT_FALSE (item->second.get ("duration", "").empty ()); + ASSERT_FALSE (item->second.get ("time", "").empty ()); + ASSERT_EQ (send2->hash ().to_string (), hash); + nano::amount tally_num; + tally_num.decode_dec (tally); + assert (tally_num == nano::genesis_amount || tally_num == (nano::genesis_amount - nano::Gxrb_ratio) || tally_num == (nano::genesis_amount - 2 * nano::Gxrb_ratio) || tally_num == (nano::genesis_amount - 3 * nano::Gxrb_ratio)); + system.stop (); +} + TEST (rpc, block_confirm) { nano::system system (24000, 1); diff --git a/nano/node/rpc.cpp b/nano/node/rpc.cpp index 63c5efaf..9dad36e6 100644 --- a/nano/node/rpc.cpp +++ b/nano/node/rpc.cpp @@ -1444,16 +1444,26 @@ void nano::rpc_handler::confirmation_history () boost::property_tree::ptree elections; boost::property_tree::ptree confirmation_stats; std::chrono::milliseconds running_total (0); + nano::block_hash hash (0); + boost::optional hash_text (request.get_optional ("hash")); + if (hash_text.is_initialized ()) + { + hash = hash_impl (); + } + if (!ec) { std::lock_guard lock (node.active.mutex); for (auto i (node.active.confirmed.begin ()), n (node.active.confirmed.end ()); i != n; ++i) { - boost::property_tree::ptree election; - election.put ("hash", i->winner->hash ().to_string ()); - election.put ("duration", i->election_duration.count ()); - election.put ("time", i->election_end.count ()); - election.put ("tally", i->tally.to_string_dec ()); - elections.push_back (std::make_pair ("", election)); + if (hash.is_zero () || i->winner->hash () == hash) + { + boost::property_tree::ptree election; + election.put ("hash", i->winner->hash ().to_string ()); + election.put ("duration", i->election_duration.count ()); + election.put ("time", i->election_end.count ()); + election.put ("tally", i->tally.to_string_dec ()); + elections.push_back (std::make_pair ("", election)); + } running_total += i->election_duration; } }