Optional search by hash in confirmation_history (#974)

This commit is contained in:
Sergey Kroshnin 2019-01-23 21:32:48 +03:00 committed by Roy Keene
commit 460e4140fd
2 changed files with 61 additions and 7 deletions

View file

@ -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<std::string> ("hash"));
auto tally (item->second.get<std::string> ("tally"));
ASSERT_FALSE (item->second.get<std::string> ("duration", "").empty ());
ASSERT_FALSE (item->second.get<std::string> ("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<std::string> ("hash"));
auto tally (item->second.get<std::string> ("tally"));
ASSERT_FALSE (item->second.get<std::string> ("duration", "").empty ());
ASSERT_FALSE (item->second.get<std::string> ("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);

View file

@ -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<std::string> hash_text (request.get_optional<std::string> ("hash"));
if (hash_text.is_initialized ())
{
hash = hash_impl ();
}
if (!ec)
{
std::lock_guard<std::mutex> 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;
}
}