diff --git a/rai/core_test/node.cpp b/rai/core_test/node.cpp index a0423e69..8fc97734 100644 --- a/rai/core_test/node.cpp +++ b/rai/core_test/node.cpp @@ -1100,4 +1100,4 @@ TEST (node, stopped_rollback) ASSERT_TRUE (node3.ledger.block_exists (block1->hash ())); ASSERT_FALSE (node3.ledger.block_exists (block2->hash ())); } -} \ No newline at end of file +} diff --git a/rai/core_test/rpc.cpp b/rai/core_test/rpc.cpp index fa84dd10..4c48ba84 100644 --- a/rai/core_test/rpc.cpp +++ b/rai/core_test/rpc.cpp @@ -1580,7 +1580,7 @@ TEST (rpc, rai_from_raw) thread1.join (); } -TEST (rpc, representative) +TEST (rpc, account_representative) { rai::system system (24000, 1); auto pool (boost::make_shared ()); @@ -1590,7 +1590,7 @@ TEST (rpc, representative) boost::property_tree::ptree request; std::string wallet; request.put ("account", rai::genesis_account.to_account ()); - request.put ("action", "representative"); + request.put ("action", "account_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")); @@ -1599,22 +1599,29 @@ TEST (rpc, representative) thread1.join(); } -TEST (rpc, representation) +TEST (rpc, account_representative_set) { rai::system system (24000, 1); + system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv); 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; + rai::keypair rep; request.put ("account", rai::genesis_account.to_account ()); - request.put ("action", "representation"); + request.put ("representative", rep.pub.to_account ()); + request.put ("wallet", system.nodes [0]->wallets.items.begin ()->first.to_string ()); + request.put ("action", "account_representative_set"); 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 ()); + std::string block_text1 (response.first.get ("block")); + rai::block_hash hash; + ASSERT_FALSE (hash.decode_hex (block_text1)); + ASSERT_FALSE (hash.is_zero ()); + rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false); + ASSERT_TRUE (system.nodes [0]->store.block_exists (transaction, hash)); + ASSERT_EQ (rep.pub, system.nodes [0]->store.block_get (transaction, hash)->representative ()); rpc.stop(); thread1.join(); } - diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index e601447c..6888e413 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -289,6 +289,86 @@ void rai::rpc_handler::account_move () } } +void rai::rpc_handler::account_representative () +{ + 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); + rai::account_info info; + auto error (rpc.node.store.account_get (transaction, account, info)); + if (!error) + { + auto block (rpc.node.store.block_get (transaction, info.rep_block)); + assert (block != nullptr); + boost::property_tree::ptree response_l; + response_l.put ("representative", block->representative ().to_account ()); + rpc.send_response (connection, response_l); + } + else + { + rpc.error_response (connection, "Account not found"); + } + } + else + { + rpc.error_response (connection, "Bad account number"); + } +} + +void rai::rpc_handler::account_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 ()) + { + auto wallet (existing->second); + std::string account_text (request.get ("account")); + rai::account account; + auto error (account.decode_account (account_text)); + if (!error) + { + std::string representative_text (request.get ("representative")); + rai::account representative; + auto error (representative.decode_account (representative_text)); + if (!error) + { + auto connection_l (connection); + auto rpc_l (shared_from_this ()); + wallet->change_async (account, representative, [rpc_l] (std::unique_ptr block) + { + rai::block_hash hash (0); + if (block != nullptr) + { + hash = block->hash (); + } + boost::property_tree::ptree response_l; + response_l.put ("block", hash.to_string ()); + rpc_l->rpc.send_response (rpc_l->connection, response_l); + }); + } + } + else + { + rpc.error_response (connection, "Bad account number"); + } + } + } + } + else + { + rpc.error_response (connection, "RPC control is disabled"); + } +} + void rai::rpc_handler::account_weight () { std::string account_text (request.get ("account")); @@ -1031,54 +1111,6 @@ 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")); - rai::account account; - auto error (account.decode_account (account_text)); - if (!error) - { - 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) - { - auto block (rpc.node.store.block_get (transaction, info.rep_block)); - assert (block != nullptr); - boost::property_tree::ptree response_l; - response_l.put ("representative", block->representative ().to_account ()); - rpc.send_response (connection, response_l); - } - else - { - rpc.error_response (connection, "Account not found"); - } - } - else - { - rpc.error_response (connection, "Bad account number"); - } -} - void rai::rpc_handler::search_pending () { if (rpc.config.enable_control) @@ -1645,6 +1677,14 @@ void rai::rpc_handler::process_request () { account_move (); } + else if (action == "account_representative") + { + account_representative (); + } + else if (action == "account_representative_set") + { + account_representative_set (); + } else if (action == "account_weight") { account_weight (); @@ -1745,14 +1785,6 @@ void rai::rpc_handler::process_request () { rai_to_raw (); } - else if (action == "representation") - { - representation (); - } - 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 f45e8009..ca56d5e5 100644 --- a/rai/node/rpc.hpp +++ b/rai/node/rpc.hpp @@ -87,6 +87,8 @@ public: void account_create (); void account_list (); void account_move (); + void account_representative (); + void account_representative_set (); void account_weight (); void available_supply (); void block (); @@ -112,8 +114,6 @@ public: void process (); void rai_to_raw (); void rai_from_raw (); - void representation (); - void representative (); void search_pending (); void send (); void stop ();