From 27dedd70f0801fb64ae8863edb44feaaa915cba0 Mon Sep 17 00:00:00 2001 From: SergiySW Date: Tue, 18 Jul 2017 17:37:36 +0300 Subject: [PATCH] RPC work_get & work_set --- rai/node/rpc.cpp | 123 +++++++++++++++++++++++++++++++++++++++++++++++ rai/node/rpc.hpp | 2 + 2 files changed, 125 insertions(+) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 1897f765..db296f7c 100755 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -2623,6 +2623,121 @@ void rai::rpc_handler::work_cancel () } } +void rai::rpc_handler::work_get () +{ + 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 (node.wallets.items.find (wallet)); + if (existing != node.wallets.items.end ()) + { + std::string account_text (request.get ("account")); + rai::account account; + auto error (account.decode_account (account_text)); + if (!error) + { + rai::transaction transaction (node.store.environment, nullptr, false); + auto account_check (existing->second->store.find (transaction, account)); + if (account_check != existing->second->store.end ()) + { + uint64_t work (0); + auto error (existing->second->store.work_get (transaction, account, work)); + boost::property_tree::ptree response_l; + response_l.put ("work", rai::to_string_hex (work)); + response (response_l); + } + else + { + error_response (response, "Account not found in wallet"); + } + } + else + { + error_response (response, "Bad account number"); + } + } + else + { + error_response (response, "Wallet not found"); + } + } + else + { + error_response (response, "Bad wallet number"); + } + } + else + { + error_response (response, "RPC control is disabled"); + } +} + +void rai::rpc_handler::work_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 (node.wallets.items.find (wallet)); + if (existing != node.wallets.items.end ()) + { + std::string account_text (request.get ("account")); + rai::account account; + auto error (account.decode_account (account_text)); + if (!error) + { + rai::transaction transaction (node.store.environment, nullptr, true); + auto account_check (existing->second->store.find (transaction, account)); + if (account_check != existing->second->store.end ()) + { + std::string work_text (request.get ("work")); + uint64_t work; + auto work_error (rai::from_string_hex (work_text, work)); + if (!work_error) + { + existing->second->store.work_put (transaction, account, work); + boost::property_tree::ptree response_l; + response_l.put ("success", ""); + response (response_l); + } + else + { + error_response (response, "Bad work"); + } + } + else + { + error_response (response, "Account not found in wallet"); + } + } + else + { + error_response (response, "Bad account number"); + } + } + else + { + error_response (response, "Wallet not found"); + } + } + else + { + error_response (response, "Bad wallet number"); + } + } + else + { + error_response (response, "RPC control is disabled"); + } +} + void rai::rpc_handler::work_validate () { std::string hash_text (request.get ("hash")); @@ -3044,6 +3159,14 @@ void rai::rpc_handler::process_request () { work_cancel (); } + else if (action == "work_get") + { + work_get (); + } + else if (action == "work_set") + { + work_set (); + } else if (action == "work_validate") { work_validate (); diff --git a/rai/node/rpc.hpp b/rai/node/rpc.hpp index ed3c81d0..cce2a1d8 100644 --- a/rai/node/rpc.hpp +++ b/rai/node/rpc.hpp @@ -166,6 +166,8 @@ public: void wallet_representative_set (); void work_generate (); void work_cancel (); + void work_get (); + void work_set (); void work_validate (); std::string body; rai::node & node;