From 9015df2e296c21bdeeef3791809741f0be18fcfd Mon Sep 17 00:00:00 2001 From: clemahieu Date: Fri, 13 Jan 2017 19:11:17 -0600 Subject: [PATCH] Adding RPC to get account representative. --- rai/core_test/rpc.cpp | 24 +++++++- rai/node/rpc.cpp | 139 ++++++++++++++++++++++++++---------------- rai/node/rpc.hpp | 1 + 3 files changed, 109 insertions(+), 55 deletions(-) diff --git a/rai/core_test/rpc.cpp b/rai/core_test/rpc.cpp index 26bb736d..f443a21c 100644 --- a/rai/core_test/rpc.cpp +++ b/rai/core_test/rpc.cpp @@ -374,7 +374,7 @@ TEST (rpc, wallet_password_enter) thread1.join(); } -TEST (rpc, representative) +TEST (rpc, wallet_representative) { rai::system system (24000, 1); auto pool (boost::make_shared ()); @@ -394,7 +394,7 @@ TEST (rpc, representative) thread1.join(); } -TEST (rpc, representative_set) +TEST (rpc, wallet_representative_set) { rai::system system (24000, 1); auto pool (boost::make_shared ()); @@ -1579,3 +1579,23 @@ TEST (rpc, rai_from_raw) node1.stop (); thread1.join (); } + +TEST (rpc, representative) +{ + rai::system system (24000, 1); + auto pool (boost::make_shared ()); + rai::rpc rpc (system.service, pool, *system.nodes [0], rai::rpc_config (true)); + rpc.start (); + std::thread thread1 ([&rpc] () {rpc.server.run();}); + boost::property_tree::ptree request; + std::string wallet; + request.put ("account", rai::genesis_account.to_account ()); + request.put ("action", "representative"); + auto response (test_response (request, rpc, system.service)); + ASSERT_EQ (boost::network::http::server ::response::ok, static_cast (boost::network::http::status (response.second))); + std::string account_text1 (response.first.get ("representative")); + ASSERT_EQ (account_text1, rai::genesis_account.to_account ()); + rpc.stop(); + thread1.join(); +} + diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index d3996f38..221c59cc 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -1031,24 +1031,27 @@ void rai::rpc_handler::rai_to_raw () } } -void rai::rpc_handler::wallet_representative () +void rai::rpc_handler::representative () { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); + std::string account_text (request.get ("account")); + rai::account account; + auto error (account.decode_account (account_text)); if (!error) { - auto existing (rpc.node.wallets.items.find (wallet)); - if (existing != rpc.node.wallets.items.end ()) + rai::transaction transaction (rpc.node.store.environment, nullptr, false); + rai::account_info info; + auto error (rpc.node.store.account_get (transaction, account, info)); + if (!error) { - rai::transaction transaction (rpc.node.store.environment, nullptr, false); + auto block (rpc.node.store.block_get (transaction, info.rep_block)); + assert (block != nullptr); boost::property_tree::ptree response_l; - response_l.put ("representative", existing->second->store.representative (transaction).to_account ()); + response_l.put ("representative", block->representative ().to_account ()); rpc.send_response (connection, response_l); } else { - rpc.error_response (connection, "Wallet not found"); + rpc.error_response (connection, "Account not found"); } } else @@ -1057,50 +1060,6 @@ void rai::rpc_handler::wallet_representative () } } -void rai::rpc_handler::wallet_representative_set () -{ - if (rpc.config.enable_control) - { - std::string wallet_text (request.get ("wallet")); - rai::uint256_union wallet; - auto error (wallet.decode_hex (wallet_text)); - if (!error) - { - auto existing (rpc.node.wallets.items.find (wallet)); - if (existing != rpc.node.wallets.items.end ()) - { - std::string representative_text (request.get ("representative")); - rai::account representative; - auto error (representative.decode_account (representative_text)); - if (!error) - { - rai::transaction transaction (rpc.node.store.environment, nullptr, true); - existing->second->store.representative_set (transaction, representative); - boost::property_tree::ptree response_l; - response_l.put ("set", "1"); - rpc.send_response (connection, response_l); - } - else - { - rpc.error_response (connection, "Invalid account number"); - } - } - else - { - rpc.error_response (connection, "Wallet not found"); - } - } - else - { - rpc.error_response (connection, "Bad account number"); - } - } - else - { - rpc.error_response (connection, "RPC control is disabled"); - } -} - void rai::rpc_handler::search_pending () { if (rpc.config.enable_control) @@ -1423,6 +1382,76 @@ void rai::rpc_handler::wallet_key_valid () } } +void rai::rpc_handler::wallet_representative () +{ + std::string wallet_text (request.get ("wallet")); + rai::uint256_union wallet; + auto error (wallet.decode_hex (wallet_text)); + if (!error) + { + auto existing (rpc.node.wallets.items.find (wallet)); + if (existing != rpc.node.wallets.items.end ()) + { + rai::transaction transaction (rpc.node.store.environment, nullptr, false); + boost::property_tree::ptree response_l; + response_l.put ("representative", existing->second->store.representative (transaction).to_account ()); + rpc.send_response (connection, response_l); + } + else + { + rpc.error_response (connection, "Wallet not found"); + } + } + else + { + rpc.error_response (connection, "Bad account number"); + } +} + +void rai::rpc_handler::wallet_representative_set () +{ + if (rpc.config.enable_control) + { + std::string wallet_text (request.get ("wallet")); + rai::uint256_union wallet; + auto error (wallet.decode_hex (wallet_text)); + if (!error) + { + auto existing (rpc.node.wallets.items.find (wallet)); + if (existing != rpc.node.wallets.items.end ()) + { + std::string representative_text (request.get ("representative")); + rai::account representative; + auto error (representative.decode_account (representative_text)); + if (!error) + { + rai::transaction transaction (rpc.node.store.environment, nullptr, true); + existing->second->store.representative_set (transaction, representative); + boost::property_tree::ptree response_l; + response_l.put ("set", "1"); + rpc.send_response (connection, response_l); + } + else + { + rpc.error_response (connection, "Invalid account number"); + } + } + else + { + rpc.error_response (connection, "Wallet not found"); + } + } + else + { + rpc.error_response (connection, "Bad account number"); + } + } + else + { + rpc.error_response (connection, "RPC control is disabled"); + } +} + void rai::rpc_handler::work_generate () { if (rpc.config.enable_control) @@ -1697,6 +1726,10 @@ void rai::rpc_handler::process_request () { rai_to_raw (); } + else if (action == "representative") + { + representative (); + } else if (action == "search_pending") { search_pending (); diff --git a/rai/node/rpc.hpp b/rai/node/rpc.hpp index d919586b..a3e9c25f 100644 --- a/rai/node/rpc.hpp +++ b/rai/node/rpc.hpp @@ -112,6 +112,7 @@ public: void process (); void rai_to_raw (); void rai_from_raw (); + void representative (); void search_pending (); void send (); void stop ();