From 8d52dc32fd0aaf97f0e8f62ec9154f1b161a4e44 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Sat, 25 Nov 2017 22:15:52 +0300 Subject: [PATCH] RPC pending with optional "source" --- rai/core_test/rpc.cpp | 85 ++++++++++++++++++++++++++++++++++++++----- rai/node/rpc.cpp | 60 +++++++++++++++++++++++++++--- 2 files changed, 130 insertions(+), 15 deletions(-) diff --git a/rai/core_test/rpc.cpp b/rai/core_test/rpc.cpp index c2bcde8a..9d5bcd90 100644 --- a/rai/core_test/rpc.cpp +++ b/rai/core_test/rpc.cpp @@ -1288,6 +1288,27 @@ TEST (rpc, pending) ASSERT_EQ (200, response1.status); blocks_node = response1.json.get_child ("blocks"); ASSERT_EQ (0, blocks_node.size ()); + request.put ("threshold", "0"); + request.put ("source", "true"); + test_response response2 (request, rpc, system.service); + while (response2.status == 0) + { + system.poll (); + } + ASSERT_EQ (200, response2.status); + blocks_node = response2.json.get_child ("blocks"); + ASSERT_EQ (1, blocks_node.size ()); + std::unordered_map amounts; + std::unordered_map sources; + for (auto i (blocks_node.begin ()), j (blocks_node.end ()); i != j; ++i) + { + rai::block_hash hash; + hash.decode_hex (i->first); + amounts[hash].decode_dec (i->second.get ("amount")); + sources[hash].decode_account (i->second.get ("source")); + } + ASSERT_EQ (amounts[block1->hash ()], 100); + ASSERT_EQ (sources[block1->hash ()], rai::test_genesis_key.pub); } TEST (rpc_config, serialization) @@ -2166,11 +2187,11 @@ TEST (rpc, accounts_pending) system.poll (); } ASSERT_EQ (200, response1.status); + std::unordered_map blocks; for (auto & pending : response1.json.get_child("blocks")) { std::string account_text (pending.first); ASSERT_EQ (key1.pub.to_account (), account_text); - std::unordered_map blocks; for (auto i (pending.second.begin ()), j (pending.second.end ()); i != j; ++i) { rai::block_hash hash; @@ -2179,8 +2200,31 @@ TEST (rpc, accounts_pending) amount.decode_dec (i->second.get ("")); blocks [hash] = amount; } - ASSERT_EQ (blocks[block1->hash ()], 100); } + ASSERT_EQ (blocks[block1->hash ()], 100); + request.put ("source", "true"); + test_response response2 (request, rpc, system.service); + while (response2.status == 0) + { + system.poll (); + } + ASSERT_EQ (200, response2.status); + std::unordered_map amounts; + std::unordered_map sources; + for (auto & pending : response2.json.get_child("blocks")) + { + std::string account_text (pending.first); + ASSERT_EQ (key1.pub.to_account (), account_text); + for (auto i (pending.second.begin ()), j (pending.second.end ()); i != j; ++i) + { + rai::block_hash hash; + hash.decode_hex (i->first); + amounts[hash].decode_dec (i->second.get ("amount")); + sources[hash].decode_account (i->second.get ("source")); + } + } + ASSERT_EQ (amounts[block1->hash ()], 100); + ASSERT_EQ (sources[block1->hash ()], rai::test_genesis_key.pub); } TEST (rpc, blocks) @@ -2315,6 +2359,7 @@ TEST (rpc, wallet_pending) rai::system system0 (24000, 1); rai::keypair key1; 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)); rai::rpc rpc (system0.service, *system0.nodes [0], rai::rpc_config (true)); rpc.start (); @@ -2331,10 +2376,8 @@ TEST (rpc, wallet_pending) for (auto & pending : response.json.get_child("blocks")) { std::string account_text (pending.first); - ASSERT_EQ (rai::test_genesis_key.pub.to_account (), account_text); - auto & blocks_node (pending.second.get_child (rai::test_genesis_key.pub.to_account ())); - ASSERT_EQ (1, blocks_node.size ()); - rai::block_hash hash1 (blocks_node.begin ()->second.get ("")); + ASSERT_EQ (key1.pub.to_account (), account_text); + rai::block_hash hash1 (pending.second.begin ()->second.get ("")); ASSERT_EQ (block1->hash (), hash1); } request.put ("threshold", "100"); // Threshold test @@ -2344,11 +2387,11 @@ TEST (rpc, wallet_pending) system0.poll (); } ASSERT_EQ (200, response0.status); + std::unordered_map blocks; for (auto & pending : response0.json.get_child("blocks")) { std::string account_text (pending.first); - ASSERT_EQ (rai::test_genesis_key.pub.to_account (), account_text); - std::unordered_map blocks; + ASSERT_EQ (key1.pub.to_account (), account_text); for (auto i (pending.second.begin ()), j (pending.second.end ()); i != j; ++i) { rai::block_hash hash; @@ -2357,8 +2400,8 @@ TEST (rpc, wallet_pending) amount.decode_dec (i->second.get ("")); blocks [hash] = amount; } - ASSERT_EQ (blocks[block1->hash ()], 100); } + ASSERT_EQ (blocks[block1->hash ()], 100); request.put ("threshold", "101"); test_response response1 (request, rpc, system0.service); while (response1.status == 0) @@ -2368,6 +2411,30 @@ TEST (rpc, wallet_pending) ASSERT_EQ (200, response1.status); auto & pending1 (response1.json.get_child ("blocks")); ASSERT_EQ (0, pending1.size ()); + request.put ("threshold", "0"); + request.put ("source", "true"); + test_response response2 (request, rpc, system0.service); + while (response2.status == 0) + { + system0.poll (); + } + ASSERT_EQ (200, response2.status); + std::unordered_map amounts; + std::unordered_map sources; + for (auto & pending : response2.json.get_child("blocks")) + { + std::string account_text (pending.first); + ASSERT_EQ (key1.pub.to_account (), account_text); + for (auto i (pending.second.begin ()), j (pending.second.end ()); i != j; ++i) + { + rai::block_hash hash; + hash.decode_hex (i->first); + amounts[hash].decode_dec (i->second.get ("amount")); + sources[hash].decode_account (i->second.get ("source")); + } + } + ASSERT_EQ (amounts[block1->hash ()], 100); + ASSERT_EQ (sources[block1->hash ()], rai::test_genesis_key.pub); } TEST (rpc, receive_minimum) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 3d406f10..4def7a33 100755 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -751,6 +751,7 @@ void rai::rpc_handler::accounts_pending () { uint64_t count (std::numeric_limits ::max ()); rai::uint128_union threshold (0); + bool source (false); boost::optional count_text (request.get_optional ("count")); if (count_text.is_initialized ()) { @@ -769,6 +770,11 @@ void rai::rpc_handler::accounts_pending () error_response (response, "Bad threshold number"); } } + boost::optional source_optional (request.get_optional ("source")); + if (source_optional.is_initialized ()) + { + source = source_optional.get (); + } boost::property_tree::ptree response_l; boost::property_tree::ptree pending; rai::transaction transaction (node.store.environment, nullptr, false); @@ -783,7 +789,7 @@ 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 ()) + if (threshold.is_zero () && !source) { boost::property_tree::ptree entry; entry.put ("", key.hash.to_string ()); @@ -794,7 +800,17 @@ void rai::rpc_handler::accounts_pending () rai::pending_info info (i->second); if (info.amount.number () >= threshold.number ()) { - peers_l.put (key.hash.to_string (), info.amount.number ().convert_to ()); + if (source) + { + boost::property_tree::ptree pending_tree; + pending_tree.put ("amount", info.amount.number ().convert_to ()); + 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 ()); + } } } } @@ -2040,6 +2056,7 @@ void rai::rpc_handler::pending () { uint64_t count (std::numeric_limits ::max ()); rai::uint128_union threshold (0); + bool source (false); boost::optional count_text (request.get_optional ("count")); if (count_text.is_initialized ()) { @@ -2058,6 +2075,11 @@ void rai::rpc_handler::pending () error_response (response, "Bad threshold number"); } } + boost::optional source_optional (request.get_optional ("source")); + if (source_optional.is_initialized ()) + { + source = source_optional.get (); + } boost::property_tree::ptree response_l; boost::property_tree::ptree peers_l; { @@ -2066,7 +2088,7 @@ 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 ()) + if (threshold.is_zero () && !source) { boost::property_tree::ptree entry; entry.put ("", key.hash.to_string ()); @@ -2077,7 +2099,17 @@ void rai::rpc_handler::pending () rai::pending_info info (i->second); if (info.amount.number () >= threshold.number ()) { - peers_l.put (key.hash.to_string (), info.amount.number ().convert_to ()); + if (source) + { + boost::property_tree::ptree pending_tree; + pending_tree.put ("amount", info.amount.number ().convert_to ()); + 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 ()); + } } } } @@ -3520,6 +3552,7 @@ void rai::rpc_handler::wallet_pending () { uint64_t count (std::numeric_limits ::max ()); rai::uint128_union threshold (0); + bool source (false); boost::optional count_text (request.get_optional ("count")); if (count_text.is_initialized ()) { @@ -3538,6 +3571,11 @@ void rai::rpc_handler::wallet_pending () error_response (response, "Bad threshold number"); } } + boost::optional source_optional (request.get_optional ("source")); + if (source_optional.is_initialized ()) + { + source = source_optional.get (); + } boost::property_tree::ptree response_l; boost::property_tree::ptree pending; rai::transaction transaction (node.store.environment, nullptr, false); @@ -3549,7 +3587,7 @@ 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 ()) + if (threshold.is_zero () && !source) { boost::property_tree::ptree entry; entry.put ("", key.hash.to_string ()); @@ -3560,7 +3598,17 @@ void rai::rpc_handler::wallet_pending () rai::pending_info info (ii->second); if (info.amount.number () >= threshold.number ()) { - peers_l.put (key.hash.to_string (), info.amount.number ().convert_to ()); + if (source) + { + boost::property_tree::ptree pending_tree; + pending_tree.put ("amount", info.amount.number ().convert_to ()); + 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 ()); + } } } }