From 0b8d09db7138b9c8fa31c93406b520c699d8431d Mon Sep 17 00:00:00 2001 From: SergiySW Date: Tue, 18 Jul 2017 21:02:12 +0300 Subject: [PATCH] RPC: merge pending_threshold with pending --- rai/core_test/rpc.cpp | 6 +- rai/node/rpc.cpp | 345 +++++++++++++++++------------------------- rai/node/rpc.hpp | 3 - 3 files changed, 145 insertions(+), 209 deletions(-) diff --git a/rai/core_test/rpc.cpp b/rai/core_test/rpc.cpp index c4739971..60c6d92c 100644 --- a/rai/core_test/rpc.cpp +++ b/rai/core_test/rpc.cpp @@ -2171,7 +2171,7 @@ TEST (rpc, pending_threshold) rai::rpc rpc (system.service, *system.nodes [0], rai::rpc_config (true)); rpc.start (); boost::property_tree::ptree request; - request.put ("action", "pending_threshold"); + request.put ("action", "pending"); request.put ("account", key1.pub.to_account ()); request.put ("threshold", "100"); test_response response0 (request, rpc, system.service); @@ -2204,7 +2204,7 @@ TEST (rpc, accounts_pending_threshold) rai::rpc rpc (system.service, *system.nodes [0], rai::rpc_config (true)); rpc.start (); boost::property_tree::ptree request; - request.put ("action", "accounts_pending_threshold"); + request.put ("action", "accounts_pending"); boost::property_tree::ptree entry; boost::property_tree::ptree peers_l; entry.put ("", key1.pub.to_account ()); @@ -2295,7 +2295,7 @@ TEST (rpc, wallet_pending_threshold) rai::rpc rpc (system0.service, *system0.nodes [0], rai::rpc_config (true)); rpc.start (); boost::property_tree::ptree request; - request.put ("action", "wallet_pending_threshold"); + request.put ("action", "wallet_pending"); request.put ("wallet", system0.nodes [0]->wallets.items.begin ()->first.to_string ()); request.put ("threshold", "100"); test_response response0 (request, rpc, system0.service); diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index df9f4d67..f384ff9f 100755 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -588,86 +588,73 @@ void rai::rpc_handler::accounts_frontiers () void rai::rpc_handler::accounts_pending () { - std::string count_text (request.get ("count")); - uint64_t count; - if (!decode_unsigned (count_text, count)) + uint64_t count (18446744073709551615U); + rai::uint128_union threshold (0); + try { - boost::property_tree::ptree response_l; - boost::property_tree::ptree pending; - rai::transaction transaction (node.store.environment, nullptr, false); - for (auto &accounts : request.get_child("accounts")) + std::string count_text (request.get ("count")); + auto error (decode_unsigned (count_text, count)); + if (error) { - std::string account_text = accounts.second.data (); - rai::uint256_union account; - if (!account.decode_account (account_text)) + error_response (response, "Invalid count"); + } + } + catch (std::runtime_error &) + { + // If there is no "count" in request + } + try + { + std::string threshold_text (request.get ("threshold")); + auto error_threshold (threshold.decode_dec (threshold_text)); + if (error_threshold) + { + error_response (response, "Bad threshold number"); + } + } + catch (std::runtime_error &) + { + // If there is no "threshold" in request + } + boost::property_tree::ptree response_l; + boost::property_tree::ptree pending; + rai::transaction transaction (node.store.environment, nullptr, false); + for (auto &accounts : request.get_child("accounts")) + { + std::string account_text = accounts.second.data (); + rai::uint256_union account; + if (!account.decode_account (account_text)) + { + boost::property_tree::ptree peers_l; + rai::account end (account.number () + 1); + 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) { - boost::property_tree::ptree peers_l; - rai::account end (account.number () + 1); - 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); + boost::property_tree::ptree entry; + if (threshold.is_zero ()) { - rai::pending_key key (i->first); - boost::property_tree::ptree entry; entry.put ("", key.hash.to_string ()); peers_l.push_back (std::make_pair ("", entry)); } - pending.add_child (account.to_account (), peers_l); - } - else - { - error_response (response, "Bad account number"); - } - } - response_l.add_child ("blocks", pending); - response (response_l); - } - else - { - error_response (response, "Invalid count"); - } -} - -void rai::rpc_handler::accounts_pending_threshold () -{ - std::string threshold_text (request.get ("threshold")); - rai::uint128_union threshold; - if (!threshold.decode_dec (threshold_text)) - { - boost::property_tree::ptree response_l; - boost::property_tree::ptree pending; - rai::transaction transaction (node.store.environment, nullptr, false); - for (auto &accounts : request.get_child("accounts")) - { - std::string account_text = accounts.second.data (); - rai::uint256_union account; - if (!account.decode_account (account_text)) - { - boost::property_tree::ptree peers_l; - rai::account end (account.number () + 1); - 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; ++i) + else { - rai::pending_key key (i->first); rai::pending_info info (i->second); if (info.amount.number () >= threshold.number ()) { - boost::property_tree::ptree entry; entry.put (key.hash.to_string (), info.amount.number ().convert_to ()); peers_l.push_back (std::make_pair ("", entry)); } } - pending.add_child (account.to_account (), peers_l); - } - else - { - error_response (response, "Bad account number"); } + pending.add_child (account.to_account (), peers_l); + } + else + { + error_response (response, "Bad account number"); } - response_l.add_child ("blocks", pending); - response (response_l); - } - else - { - error_response (response, "Bad threshold number"); } + response_l.add_child ("blocks", pending); + response (response_l); } void rai::rpc_handler::available_supply () @@ -1361,30 +1348,61 @@ void rai::rpc_handler::pending () rai::account account; if (!account.decode_account(account_text)) { - std::string count_text (request.get ("count")); - uint64_t count; - if (!decode_unsigned (count_text, count)) + uint64_t count (18446744073709551615U); + rai::uint128_union threshold (0); + try { - boost::property_tree::ptree response_l; - boost::property_tree::ptree peers_l; + std::string count_text (request.get ("count")); + auto error (decode_unsigned (count_text, count)); + if (error) { - rai::transaction transaction (node.store.environment, nullptr, false); - rai::account end (account.number () + 1); - 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) + error_response (response, "Invalid count"); + } + } + catch (std::runtime_error &) + { + // If there is no "count" in request + } + try + { + std::string threshold_text (request.get ("threshold")); + auto error_threshold (threshold.decode_dec (threshold_text)); + if (error_threshold) + { + error_response (response, "Bad threshold number"); + } + } + catch (std::runtime_error &) + { + // If there is no "threshold" in request + } + boost::property_tree::ptree response_l; + boost::property_tree::ptree peers_l; + { + rai::transaction transaction (node.store.environment, nullptr, false); + rai::account end (account.number () + 1); + 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); + boost::property_tree::ptree entry; + if (threshold.is_zero ()) { - rai::pending_key key (i->first); - 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 ()) + { + entry.put (key.hash.to_string (), info.amount.number ().convert_to ()); + peers_l.push_back (std::make_pair ("", entry)); + } + } } - response_l.add_child ("blocks", peers_l); - response (response_l); - } - else - { - error_response (response, "Invalid count"); } + response_l.add_child ("blocks", peers_l); + response (response_l); } else { @@ -1421,47 +1439,6 @@ void rai::rpc_handler::pending_exists () } } -void rai::rpc_handler::pending_threshold () -{ - std::string account_text (request.get ("account")); - rai::account account; - if (!account.decode_account(account_text)) - { - std::string threshold_text (request.get ("threshold")); - rai::uint128_union threshold; - if (!threshold.decode_dec (threshold_text)) - { - boost::property_tree::ptree response_l; - boost::property_tree::ptree peers_l; - { - rai::transaction transaction (node.store.environment, nullptr, false); - rai::account end (account.number () + 1); - 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; ++i) - { - rai::pending_key key (i->first); - rai::pending_info info (i->second); - if (info.amount.number () >= threshold.number ()) - { - boost::property_tree::ptree entry; - entry.put (key.hash.to_string (), info.amount.number ().convert_to ()); - peers_l.push_back (std::make_pair ("", entry)); - } - } - } - response_l.add_child ("blocks", peers_l); - response (response_l); - } - else - { - error_response (response, "Bad threshold number"); - } - } - else - { - error_response (response, "Bad account number"); - } -} - void rai::rpc_handler::payment_begin () { std::string id_text (request.get ("wallet")); @@ -2394,94 +2371,68 @@ void rai::rpc_handler::wallet_pending () auto existing (node.wallets.items.find (wallet)); if (existing != node.wallets.items.end ()) { - std::string count_text (request.get ("count")); - uint64_t count; - if (!decode_unsigned (count_text, count)) + uint64_t count (18446744073709551615U); + rai::uint128_union threshold (0); + try { - boost::property_tree::ptree response_l; - boost::property_tree::ptree pending; - rai::transaction transaction (node.store.environment, nullptr, false); - for (auto i (existing->second->store.begin (transaction)), n (existing->second->store.end ()); i != n; ++i) + std::string count_text (request.get ("count")); + auto error_count (decode_unsigned (count_text, count)); + if (error_count) { - rai::account account(i->first); - boost::property_tree::ptree peers_l; - rai::account end (account.number () + 1); - 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) + error_response (response, "Invalid count"); + } + } + catch (std::runtime_error &) + { + // If there is no "count" in request + } + try + { + std::string threshold_text (request.get ("threshold")); + auto error_threshold (threshold.decode_dec (threshold_text)); + if (error_threshold) + { + error_response (response, "Bad threshold number"); + } + } + catch (std::runtime_error &) + { + // If there is no "threshold" in request + } + boost::property_tree::ptree response_l; + boost::property_tree::ptree pending; + rai::transaction transaction (node.store.environment, nullptr, false); + for (auto i (existing->second->store.begin (transaction)), n (existing->second->store.end ()); i != n; ++i) + { + rai::account account(i->first); + boost::property_tree::ptree peers_l; + rai::account end (account.number () + 1); + 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); + boost::property_tree::ptree entry; + if (threshold.is_zero ()) { - rai::pending_key key (ii->first); - boost::property_tree::ptree entry; entry.put ("", key.hash.to_string ()); peers_l.push_back (std::make_pair ("", entry)); } - if (!peers_l.empty ()) + else { - pending.add_child (account.to_account (), peers_l); - } - } - response_l.add_child ("pending", pending); - response (response_l); - } - else - { - error_response (response, "Invalid count"); - } - } - else - { - error_response (response, "Wallet not found"); - } - } - else - { - error_response (response, "Bad wallet number"); - } -} - -void rai::rpc_handler::wallet_pending_threshold () -{ - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) - { - auto existing (node.wallets.items.find (wallet)); - if (existing != node.wallets.items.end ()) - { - std::string threshold_text (request.get ("threshold")); - rai::uint128_union threshold; - if (!threshold.decode_dec (threshold_text)) - { - boost::property_tree::ptree response_l; - boost::property_tree::ptree pending; - rai::transaction transaction (node.store.environment, nullptr, false); - for (auto i (existing->second->store.begin (transaction)), n (existing->second->store.end ()); i != n; ++i) - { - rai::account account(i->first); - boost::property_tree::ptree peers_l; - rai::account end (account.number () + 1); - 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; ++ii) - { - rai::pending_key key (ii->first); rai::pending_info info (ii->second); if (info.amount.number () >= threshold.number ()) { - boost::property_tree::ptree entry; entry.put (key.hash.to_string (), info.amount.number ().convert_to ()); peers_l.push_back (std::make_pair ("", entry)); } } - if (!peers_l.empty ()) - { - pending.add_child (account.to_account (), peers_l); - } } - response_l.add_child ("pending", pending); - response (response_l); - } - else - { - error_response (response, "Bad threshold number"); + if (!peers_l.empty ()) + { + pending.add_child (account.to_account (), peers_l); + } } + response_l.add_child ("pending", pending); + response (response_l); } else { @@ -2945,10 +2896,6 @@ void rai::rpc_handler::process_request () { accounts_pending (); } - else if (action == "accounts_pending_threshold") - { - accounts_pending_threshold (); - } else if (action == "available_supply") { available_supply (); @@ -3069,10 +3016,6 @@ void rai::rpc_handler::process_request () { pending_exists (); } - else if (action == "pending_threshold") - { - pending_threshold (); - } else if (action == "process") { process (); @@ -3173,10 +3116,6 @@ void rai::rpc_handler::process_request () { wallet_pending (); } - else if (action == "wallet_pending_threshold") - { - wallet_pending_threshold (); - } else if (action == "wallet_representative") { wallet_representative (); diff --git a/rai/node/rpc.hpp b/rai/node/rpc.hpp index ce62e3fb..a3b3d139 100644 --- a/rai/node/rpc.hpp +++ b/rai/node/rpc.hpp @@ -104,7 +104,6 @@ public: void accounts_balances (); void accounts_frontiers (); void accounts_pending (); - void accounts_pending_threshold (); void available_supply (); void block (); void blocks (); @@ -134,7 +133,6 @@ public: void peers (); void pending (); void pending_exists (); - void pending_threshold (); void process (); void rai_to_raw (); void rai_from_raw (); @@ -161,7 +159,6 @@ public: void wallet_frontiers (); void wallet_key_valid (); void wallet_pending (); - void wallet_pending_threshold (); void wallet_representative (); void wallet_representative_set (); void wallet_work_get ();