Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove active blocks from *_pending RPC calls #1028

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions rai/core_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2335,6 +2335,13 @@ TEST (rpc, accounts_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));
auto iterations (0);
while (system.nodes[0]->active.active (*block1))
{
system.poll ();
++iterations;
ASSERT_LT (iterations, 200);
}
rai::rpc rpc (system.service, *system.nodes[0], rai::rpc_config (true));
rpc.start ();
boost::property_tree::ptree request;
Expand Down Expand Up @@ -2541,6 +2548,13 @@ TEST (rpc, wallet_pending)
system0.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
system0.wallet (0)->insert_adhoc (key1.prv);
auto block1 (system0.wallet (0)->send_action (rai::test_genesis_key.pub, key1.pub, 100));
auto iterations (0);
while (system0.nodes[0]->active.active (*block1))
{
system0.poll ();
++iterations;
ASSERT_LT (iterations, 200);
}
rai::rpc rpc (system0.service, *system0.nodes[0], rai::rpc_config (true));
rpc.start ();
boost::property_tree::ptree request;
Expand All @@ -2553,6 +2567,7 @@ TEST (rpc, wallet_pending)
system0.poll ();
}
ASSERT_EQ (200, response.status);
ASSERT_EQ (1, response.json.get_child ("blocks").size ());
for (auto & pending : response.json.get_child ("blocks"))
{
std::string account_text (pending.first);
Expand All @@ -2568,6 +2583,7 @@ TEST (rpc, wallet_pending)
}
ASSERT_EQ (200, response0.status);
std::unordered_map<rai::block_hash, rai::uint128_union> blocks;
ASSERT_EQ (1, response0.json.get_child ("blocks").size ());
for (auto & pending : response0.json.get_child ("blocks"))
{
std::string account_text (pending.first);
Expand Down Expand Up @@ -2606,6 +2622,7 @@ TEST (rpc, wallet_pending)
ASSERT_EQ (200, response2.status);
std::unordered_map<rai::block_hash, rai::uint128_union> amounts;
std::unordered_map<rai::block_hash, rai::account> sources;
ASSERT_EQ (1, response0.json.get_child ("blocks").size ());
for (auto & pending : response2.json.get_child ("blocks"))
{
std::string account_text (pending.first);
Expand Down
90 changes: 51 additions & 39 deletions rai/node/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,7 @@ void rai::rpc_handler::accounts_pending ()
auto count (count_optional_impl ());
auto threshold (threshold_optional_impl ());
const bool source = request.get<bool> ("source", false);
const bool include_active = request.get<bool> ("include_active", false);
boost::property_tree::ptree pending;
rai::transaction transaction (node.store.environment, nullptr, false);
for (auto & accounts : request.get_child ("accounts"))
Expand All @@ -769,27 +770,32 @@ void rai::rpc_handler::accounts_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)
std::shared_ptr<rai::block> 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, i->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0);
if (info.amount.number () >= threshold.number ())
if (threshold.is_zero () && !source)
{
if (source)
{
boost::property_tree::ptree pending_tree;
pending_tree.put ("amount", info.amount.number ().convert_to<std::string> ());
pending_tree.put ("source", info.source.to_account ());
peers_l.add_child (key.hash.to_string (), pending_tree);
}
else
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, i->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0);
if (info.amount.number () >= threshold.number ())
{
peers_l.put (key.hash.to_string (), info.amount.number ().convert_to<std::string> ());
if (source)
{
boost::property_tree::ptree pending_tree;
pending_tree.put ("amount", info.amount.number ().convert_to<std::string> ());
pending_tree.put ("source", info.source.to_account ());
peers_l.add_child (key.hash.to_string (), pending_tree);
}
else
{
peers_l.put (key.hash.to_string (), info.amount.number ().convert_to<std::string> ());
}
}
}
}
Expand Down Expand Up @@ -3111,6 +3117,7 @@ void rai::rpc_handler::wallet_pending ()
auto threshold (threshold_optional_impl ());
const bool source = request.get<bool> ("source", false);
const bool min_version = request.get<bool> ("min_version", false);
const bool include_active = request.get<bool> ("include_active", false);
if (!ec)
{
boost::property_tree::ptree pending;
Expand All @@ -3123,34 +3130,39 @@ void rai::rpc_handler::wallet_pending ()
for (auto ii (node.store.pending_begin (transaction, rai::pending_key (account, 0))), nn (node.store.pending_begin (transaction, rai::pending_key (end, 0))); ii != nn && peers_l.size () < count; ++ii)
{
rai::pending_key key (ii->first);
if (threshold.is_zero () && !source)
{
boost::property_tree::ptree entry;
entry.put ("", key.hash.to_string ());
peers_l.push_back (std::make_pair ("", entry));
}
else
std::shared_ptr<rai::block> block (node.store.block_get (transaction, key.hash));
assert (block);
if (include_active || (block && !node.active.active (*block)))
{
rai::pending_info info (ii->second, ii->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0);
if (info.amount.number () >= threshold.number ())
if (threshold.is_zero () && !source)
{
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 (ii->second, ii->from_secondary_store ? rai::epoch::epoch_1 : rai::epoch::epoch_0);
if (info.amount.number () >= threshold.number ())
{
boost::property_tree::ptree pending_tree;
pending_tree.put ("amount", info.amount.number ().convert_to<std::string> ());
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<std::string> ());
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<std::string> ());
}
peers_l.add_child (key.hash.to_string (), pending_tree);
}
else
{
peers_l.put (key.hash.to_string (), info.amount.number ().convert_to<std::string> ());
}
}
}
Expand Down