Adding RPC to get account representative.

This commit is contained in:
clemahieu 2017-01-13 19:11:17 -06:00
commit 9015df2e29
3 changed files with 109 additions and 55 deletions

View file

@ -374,7 +374,7 @@ TEST (rpc, wallet_password_enter)
thread1.join();
}
TEST (rpc, representative)
TEST (rpc, wallet_representative)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
@ -394,7 +394,7 @@ TEST (rpc, representative)
thread1.join();
}
TEST (rpc, representative_set)
TEST (rpc, wallet_representative_set)
{
rai::system system (24000, 1);
auto pool (boost::make_shared <boost::network::utils::thread_pool> ());
@ -1579,3 +1579,23 @@ TEST (rpc, rai_from_raw)
node1.stop ();
thread1.join ();
}
TEST (rpc, representative)
{
rai::system system (24000, 1);
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;
request.put ("account", rai::genesis_account.to_account ());
request.put ("action", "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"));
ASSERT_EQ (account_text1, rai::genesis_account.to_account ());
rpc.stop();
thread1.join();
}

View file

@ -1031,24 +1031,27 @@ void rai::rpc_handler::rai_to_raw ()
}
}
void rai::rpc_handler::wallet_representative ()
void rai::rpc_handler::representative ()
{
std::string wallet_text (request.get <std::string> ("wallet"));
rai::uint256_union wallet;
auto error (wallet.decode_hex (wallet_text));
std::string account_text (request.get <std::string> ("account"));
rai::account account;
auto error (account.decode_account (account_text));
if (!error)
{
auto existing (rpc.node.wallets.items.find (wallet));
if (existing != rpc.node.wallets.items.end ())
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)
{
rai::transaction transaction (rpc.node.store.environment, nullptr, false);
auto block (rpc.node.store.block_get (transaction, info.rep_block));
assert (block != nullptr);
boost::property_tree::ptree response_l;
response_l.put ("representative", existing->second->store.representative (transaction).to_account ());
response_l.put ("representative", block->representative ().to_account ());
rpc.send_response (connection, response_l);
}
else
{
rpc.error_response (connection, "Wallet not found");
rpc.error_response (connection, "Account not found");
}
}
else
@ -1057,50 +1060,6 @@ void rai::rpc_handler::wallet_representative ()
}
}
void rai::rpc_handler::wallet_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 ())
{
std::string representative_text (request.get <std::string> ("representative"));
rai::account representative;
auto error (representative.decode_account (representative_text));
if (!error)
{
rai::transaction transaction (rpc.node.store.environment, nullptr, true);
existing->second->store.representative_set (transaction, representative);
boost::property_tree::ptree response_l;
response_l.put ("set", "1");
rpc.send_response (connection, response_l);
}
else
{
rpc.error_response (connection, "Invalid account number");
}
}
else
{
rpc.error_response (connection, "Wallet not found");
}
}
else
{
rpc.error_response (connection, "Bad account number");
}
}
else
{
rpc.error_response (connection, "RPC control is disabled");
}
}
void rai::rpc_handler::search_pending ()
{
if (rpc.config.enable_control)
@ -1423,6 +1382,76 @@ void rai::rpc_handler::wallet_key_valid ()
}
}
void rai::rpc_handler::wallet_representative ()
{
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 ())
{
rai::transaction transaction (rpc.node.store.environment, nullptr, false);
boost::property_tree::ptree response_l;
response_l.put ("representative", existing->second->store.representative (transaction).to_account ());
rpc.send_response (connection, response_l);
}
else
{
rpc.error_response (connection, "Wallet not found");
}
}
else
{
rpc.error_response (connection, "Bad account number");
}
}
void rai::rpc_handler::wallet_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 ())
{
std::string representative_text (request.get <std::string> ("representative"));
rai::account representative;
auto error (representative.decode_account (representative_text));
if (!error)
{
rai::transaction transaction (rpc.node.store.environment, nullptr, true);
existing->second->store.representative_set (transaction, representative);
boost::property_tree::ptree response_l;
response_l.put ("set", "1");
rpc.send_response (connection, response_l);
}
else
{
rpc.error_response (connection, "Invalid account number");
}
}
else
{
rpc.error_response (connection, "Wallet not found");
}
}
else
{
rpc.error_response (connection, "Bad account number");
}
}
else
{
rpc.error_response (connection, "RPC control is disabled");
}
}
void rai::rpc_handler::work_generate ()
{
if (rpc.config.enable_control)
@ -1697,6 +1726,10 @@ void rai::rpc_handler::process_request ()
{
rai_to_raw ();
}
else if (action == "representative")
{
representative ();
}
else if (action == "search_pending")
{
search_pending ();

View file

@ -112,6 +112,7 @@ public:
void process ();
void rai_to_raw ();
void rai_from_raw ();
void representative ();
void search_pending ();
void send ();
void stop ();