From 79e5be06d467c2e1d818fb991ba525441d6abcfc Mon Sep 17 00:00:00 2001 From: clemahieu Date: Fri, 13 Jan 2017 19:21:04 -0600 Subject: [PATCH] Adding RPC to find out how much representation an account has. --- rai/core_test/rpc.cpp | 19 +++++++++++++++++++ rai/node/rpc.cpp | 23 +++++++++++++++++++++++ rai/node/rpc.hpp | 1 + 3 files changed, 43 insertions(+) diff --git a/rai/core_test/rpc.cpp b/rai/core_test/rpc.cpp index f443a21c..fa84dd10 100644 --- a/rai/core_test/rpc.cpp +++ b/rai/core_test/rpc.cpp @@ -1599,3 +1599,22 @@ TEST (rpc, representative) thread1.join(); } +TEST (rpc, representation) +{ + 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", "representation"); + 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 amount_text1 (response.first.get ("representation")); + ASSERT_EQ (amount_text1, rai::genesis_amount.convert_to ()); + rpc.stop(); + thread1.join(); +} + diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 221c59cc..e601447c 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -1031,6 +1031,25 @@ void rai::rpc_handler::rai_to_raw () } } +void rai::rpc_handler::representation () +{ + std::string account_text (request.get ("account")); + rai::account account; + auto error (account.decode_account (account_text)); + if (!error) + { + rai::transaction transaction (rpc.node.store.environment, nullptr, false); + auto representation (rpc.node.store.representation_get (transaction, account)); + boost::property_tree::ptree response_l; + response_l.put ("representation", representation.convert_to ()); + rpc.send_response (connection, response_l); + } + else + { + rpc.error_response (connection, "Bad account number"); + } +} + void rai::rpc_handler::representative () { std::string account_text (request.get ("account")); @@ -1726,6 +1745,10 @@ void rai::rpc_handler::process_request () { rai_to_raw (); } + else if (action == "representation") + { + representation (); + } else if (action == "representative") { representative (); diff --git a/rai/node/rpc.hpp b/rai/node/rpc.hpp index a3e9c25f..f45e8009 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 representation (); void representative (); void search_pending (); void send ();