From 0b16ae1e369210f1244fdd69adac1663415cd866 Mon Sep 17 00:00:00 2001 From: Lee Bousfield Date: Mon, 10 Sep 2018 18:57:11 -0500 Subject: [PATCH] Exclude active blocks from the pending and pending_exists RPC (#1169) --- rai/core_test/rpc.cpp | 8 +++++++ rai/node/rpc.cpp | 50 +++++++++++++++++++++++++------------------ 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/rai/core_test/rpc.cpp b/rai/core_test/rpc.cpp index 1626e8b3..8a4c9c99 100644 --- a/rai/core_test/rpc.cpp +++ b/rai/core_test/rpc.cpp @@ -1375,6 +1375,10 @@ TEST (rpc, pending) rai::keypair key1; system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv); auto block1 (system.wallet (0)->send_action (rai::test_genesis_key.pub, key1.pub, 100)); + while (system.nodes[0]->active.active (*block1)) + { + system.poll (); + } rai::rpc rpc (system.service, *system.nodes[0], rai::rpc_config (true)); rpc.start (); boost::property_tree::ptree request; @@ -2517,6 +2521,10 @@ TEST (rpc, pending_exists) system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv); auto hash0 (system.nodes[0]->latest (rai::genesis_account)); auto block1 (system.wallet (0)->send_action (rai::test_genesis_key.pub, key1.pub, 100)); + while (system.nodes[0]->active.active (*block1)) + { + system.poll (); + } rai::rpc rpc (system.service, *system.nodes[0], rai::rpc_config (true)); rpc.start (); boost::property_tree::ptree request; diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index b13fbbff..91389d1e 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -2064,6 +2064,7 @@ void rai::rpc_handler::pending () auto threshold (threshold_optional_impl ()); const bool source = request.get ("source", false); const bool min_version = request.get ("min_version", false); + const bool include_active = request.get ("include_active", false); if (!ec) { boost::property_tree::ptree peers_l; @@ -2072,34 +2073,39 @@ void rai::rpc_handler::pending () for (auto i (node.store.pending_begin (transaction, rai::pending_key (account, 0))), n (node.store.pending_begin (transaction, rai::pending_key (end, 0))); i != n && peers_l.size () < count; ++i) { rai::pending_key key (i->first); - if (threshold.is_zero () && !source && !min_version) + std::shared_ptr block (node.store.block_get (transaction, key.hash)); + assert (block); + if (include_active || (block && !node.active.active (*block))) { - boost::property_tree::ptree entry; - entry.put ("", key.hash.to_string ()); - peers_l.push_back (std::make_pair ("", entry)); - } - else - { - rai::pending_info info (i->second); - if (info.amount.number () >= threshold.number ()) + if (threshold.is_zero () && !source && !min_version) { - if (source || min_version) + boost::property_tree::ptree entry; + entry.put ("", key.hash.to_string ()); + peers_l.push_back (std::make_pair ("", entry)); + } + else + { + rai::pending_info info (i->second); + if (info.amount.number () >= threshold.number ()) { - boost::property_tree::ptree pending_tree; - pending_tree.put ("amount", info.amount.number ().convert_to ()); - if (source) + if (source || min_version) { - pending_tree.put ("source", info.source.to_account ()); + boost::property_tree::ptree pending_tree; + pending_tree.put ("amount", info.amount.number ().convert_to ()); + if (source) + { + pending_tree.put ("source", info.source.to_account ()); + } + if (min_version) + { + pending_tree.put ("min_version", info.epoch == rai::epoch::epoch_1 ? "1" : "0"); + } + peers_l.add_child (key.hash.to_string (), pending_tree); } - if (min_version) + else { - pending_tree.put ("min_version", info.epoch == rai::epoch::epoch_1 ? "1" : "0"); + peers_l.put (key.hash.to_string (), info.amount.number ().convert_to ()); } - peers_l.add_child (key.hash.to_string (), pending_tree); - } - else - { - peers_l.put (key.hash.to_string (), info.amount.number ().convert_to ()); } } } @@ -2112,6 +2118,7 @@ void rai::rpc_handler::pending () void rai::rpc_handler::pending_exists () { auto hash (hash_impl ()); + const bool include_active = request.get ("include_active", false); if (!ec) { auto transaction (node.store.tx_begin_read ()); @@ -2124,6 +2131,7 @@ void rai::rpc_handler::pending_exists () { exists = node.store.pending_exists (transaction, rai::pending_key (destination, hash)); } + exists = exists && (include_active || !node.active.active (*block)); response_l.put ("exists", exists ? "1" : "0"); } else