From d7bc9f9d9d979c1128124c946926cf01867a9d2b Mon Sep 17 00:00:00 2001 From: SergiySW Date: Sun, 23 Jul 2017 21:44:08 +0300 Subject: [PATCH] RPC delegators & delegators_count --- rai/node/rpc.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ rai/node/rpc.hpp | 2 ++ 2 files changed, 68 insertions(+) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 7764a4b9..945c94c9 100755 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -852,6 +852,64 @@ void rai::rpc_handler::chain () } } +void rai::rpc_handler::delegators () +{ + std::string account_text (request.get ("account")); + rai::account account; + auto error (account.decode_account (account_text)); + if (!error) + { + boost::property_tree::ptree response_l; + boost::property_tree::ptree delegators; + rai::transaction transaction (node.store.environment, nullptr, false); + for (auto i (node.store.latest_begin (transaction)), n (node.store.latest_end ()); i != n; ++i) + { + rai::account_info info (i->second); + auto block (node.store.block_get (transaction, info.rep_block)); + assert (block != nullptr); + if (block->representative() == account) { + rai::account account_l (i->first); + auto balance (node.ledger.account_balance (transaction, account_l)); + delegators.put (account_l.to_account (), balance.convert_to ()); + } + } + response_l.add_child ("delegators", delegators); + response (response_l); + } + else + { + error_response (response, "Bad account number"); + } +} + +void rai::rpc_handler::delegators_count () +{ + std::string account_text (request.get ("account")); + rai::account account; + auto error (account.decode_account (account_text)); + if (!error) + { + uint64_t count (0); + rai::transaction transaction (node.store.environment, nullptr, false); + for (auto i (node.store.latest_begin (transaction)), n (node.store.latest_end ()); i != n; ++i) + { + rai::account_info info (i->second); + auto block (node.store.block_get (transaction, info.rep_block)); + assert (block != nullptr); + if (block->representative() == account) { + ++count; + } + } + boost::property_tree::ptree response_l; + response_l.put ("count", std::to_string (count)); + response (response_l); + } + else + { + error_response (response, "Bad account number"); + } +} + void rai::rpc_handler::deterministic_key () { std::string seed_text (request.get ("seed")); @@ -2558,6 +2616,14 @@ void rai::rpc_handler::process_request () { chain (); } + else if (action == "delegators") + { + delegators (); + } + else if (action == "delegators_count") + { + delegators_count (); + } else if (action == "deterministic_key") { deterministic_key (); diff --git a/rai/node/rpc.hpp b/rai/node/rpc.hpp index 7cc8d98d..a39557af 100644 --- a/rai/node/rpc.hpp +++ b/rai/node/rpc.hpp @@ -112,6 +112,8 @@ public: void bootstrap (); void bootstrap_any (); void chain (); + void delegators (); + void delegators_count (); void deterministic_key (); void frontiers (); void frontier_count ();