Changing RPC name from 'representative' to 'account_representative'. Adding 'account_representative_set'. Removing 'representation' as it's already performed by account_weight.

This commit is contained in:
clemahieu 2017-01-17 16:53:38 -06:00
commit 0be2f4b828
4 changed files with 106 additions and 67 deletions

View file

@ -1100,4 +1100,4 @@ TEST (node, stopped_rollback)
ASSERT_TRUE (node3.ledger.block_exists (block1->hash ()));
ASSERT_FALSE (node3.ledger.block_exists (block2->hash ()));
}
}
}

View file

@ -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 <boost::network::utils::thread_pool> ());
@ -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 <rai::rpc>::response::ok, static_cast <uint16_t> (boost::network::http::status (response.second)));
std::string account_text1 (response.first.get <std::string> ("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 <boost::network::utils::thread_pool> ());
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 <rai::rpc>::response::ok, static_cast <uint16_t> (boost::network::http::status (response.second)));
std::string amount_text1 (response.first.get <std::string> ("representation"));
ASSERT_EQ (amount_text1, rai::genesis_amount.convert_to <std::string> ());
std::string block_text1 (response.first.get <std::string> ("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();
}

View file

@ -289,6 +289,86 @@ void rai::rpc_handler::account_move ()
}
}
void rai::rpc_handler::account_representative ()
{
std::string account_text (request.get <std::string> ("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 <std::string> ("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 <std::string> ("account"));
rai::account account;
auto error (account.decode_account (account_text));
if (!error)
{
std::string representative_text (request.get <std::string> ("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 <rai::block> 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 <std::string> ("account"));
@ -1031,54 +1111,6 @@ void rai::rpc_handler::rai_to_raw ()
}
}
void rai::rpc_handler::representation ()
{
std::string account_text (request.get <std::string> ("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 <std::string> ());
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 <std::string> ("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 ();

View file

@ -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 ();