Merge branch 'pulls/143'

This commit is contained in:
clemahieu 2017-06-21 13:29:20 -05:00
commit 7639ea6e24
3 changed files with 251 additions and 6 deletions

View file

@ -2015,8 +2015,7 @@ TEST (rpc, accounts_balances)
system.poll ();
}
ASSERT_EQ (200, response.status);
std::vector <std::string> balances;
for (boost::property_tree::ptree::value_type &balances : response.json.get_child("balances"))
for (auto & balances : response.json.get_child ("balances"))
{
std::string account_text (balances.first);
ASSERT_EQ (rai::test_genesis_key.pub.to_account (), account_text);
@ -2046,8 +2045,7 @@ TEST (rpc, accounts_frontiers)
system.poll ();
}
ASSERT_EQ (200, response.status);
std::vector <std::string> frontiers;
for (boost::property_tree::ptree::value_type &frontiers : response.json.get_child("frontiers"))
for (auto & frontiers : response.json.get_child ("frontiers"))
{
std::string account_text (frontiers.first);
ASSERT_EQ (rai::test_genesis_key.pub.to_account (), account_text);
@ -2078,8 +2076,7 @@ TEST (rpc, accounts_pending)
system.poll ();
}
ASSERT_EQ (200, response.status);
std::vector <std::string> blocks;
for (boost::property_tree::ptree::value_type &blocks : response.json.get_child("blocks"))
for (auto & blocks : response.json.get_child("blocks"))
{
std::string account_text (blocks.first);
ASSERT_EQ (key1.pub.to_account (), account_text);
@ -2087,3 +2084,81 @@ TEST (rpc, accounts_pending)
ASSERT_EQ (block1->hash (), hash1);
}
}
TEST (rpc, blocks)
{
rai::system system (24000, 1);
rai::rpc rpc (system.service, *system.nodes [0], rai::rpc_config (true));
rpc.start ();
boost::property_tree::ptree request;
request.put ("action", "blocks");
boost::property_tree::ptree entry;
boost::property_tree::ptree peers_l;
entry.put ("", system.nodes [0]->latest (rai::genesis_account).to_string ());
peers_l.push_back (std::make_pair ("", entry));
request.add_child ("hashes", peers_l);
test_response response (request, rpc, system.service);
while (response.status == 0)
{
system.poll ();
}
ASSERT_EQ (200, response.status);
for (auto & blocks : response.json.get_child ("blocks"))
{
std::string hash_text (blocks.first);
ASSERT_EQ (system.nodes [0]->latest (rai::genesis_account).to_string (), hash_text);
std::string blocks_text (blocks.second.get <std::string> (""));
ASSERT_FALSE (blocks_text.empty ());
}
}
TEST (rpc, wallet_balance_total)
{
rai::system system (24000, 1);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::keypair key;
system.wallet (0)->insert_adhoc (key.prv);
auto send (system.wallet (0)->send_action (rai::test_genesis_key.pub, key.pub, 1));
rai::rpc rpc (system.service, *system.nodes [0], rai::rpc_config (true));
rpc.start ();
boost::property_tree::ptree request;
request.put ("action", "wallet_balance_total");
request.put ("wallet", system.nodes [0]->wallets.items.begin ()->first.to_string ());
test_response response (request, rpc, system.service);
while (response.status == 0)
{
system.poll ();
}
ASSERT_EQ (200, response.status);
std::string balance_text (response.json.get <std::string> ("balance"));
ASSERT_EQ ("340282366920938463463374607431768211454", balance_text);
std::string pending_text (response.json.get <std::string> ("pending"));
ASSERT_EQ ("1", pending_text);
}
TEST (rpc, wallet_balances)
{
rai::system system0 (24000, 1);
system0.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::rpc rpc (system0.service, *system0.nodes [0], rai::rpc_config (true));
rpc.start ();
boost::property_tree::ptree request;
request.put ("action", "wallet_balances");
request.put ("wallet", system0.nodes [0]->wallets.items.begin ()->first.to_string ());
test_response response (request, rpc, system0.service);
while (response.status == 0)
{
system0.poll ();
}
ASSERT_EQ (200, response.status);
std::vector <std::string> balances;
for (auto & balances : response.json.get_child("balances"))
{
std::string account_text (balances.first);
ASSERT_EQ (rai::test_genesis_key.pub.to_account (), account_text);
std::string balance_text (balances.second.get <std::string> ("balance"));
ASSERT_EQ ("340282366920938463463374607431768211455", balance_text);
std::string pending_text (balances.second.get <std::string> ("pending"));
ASSERT_EQ ("0", pending_text);
}
}

View file

@ -665,6 +665,40 @@ void rai::rpc_handler::block ()
}
}
void rai::rpc_handler::blocks ()
{
std::vector <std::string> hashes;
boost::property_tree::ptree response_l;
boost::property_tree::ptree blocks;
rai::transaction transaction (node.store.environment, nullptr, false);
for (boost::property_tree::ptree::value_type &hashes : request.get_child("hashes"))
{
std::string hash_text = hashes.second.data();
rai::uint256_union hash;
auto error (hash.decode_hex (hash_text));
if (!error)
{
auto block (node.store.block_get (transaction, hash));
if (block != nullptr)
{
std::string contents;
block->serialize_json (contents);
blocks.put (hash_text, contents);
}
else
{
error_response (response, "Block not found");
}
}
else
{
error_response (response, "Bad hash number");
}
}
response_l.add_child ("blocks", blocks);
response (response_l);
}
void rai::rpc_handler::block_account ()
{
std::string hash_text (request.get <std::string> ("hash"));
@ -991,6 +1025,50 @@ void rai::rpc_handler::history ()
}
}
void rai::rpc_handler::account_history ()
{
std::string account_text (request.get <std::string> ("account"));
std::string count_text (request.get <std::string> ("count"));
rai::uint256_union account;
auto error (account.decode_account (account_text));
if (!error)
{
uint64_t count;
if (!decode_unsigned (count_text, count))
{
boost::property_tree::ptree response_l;
boost::property_tree::ptree history;
rai::transaction transaction (node.store.environment, nullptr, false);
auto hash (node.ledger.latest (transaction, account));
auto block (node.store.block_get (transaction, hash));
while (block != nullptr && count > 0)
{
boost::property_tree::ptree entry;
history_visitor visitor (*this, transaction, entry, hash);
block->visit (visitor);
if (!entry.empty ())
{
entry.put ("hash", hash.to_string ());
history.push_back (std::make_pair ("", entry));
}
hash = block->previous ();
block = node.store.block_get (transaction, hash);
--count;
}
response_l.add_child ("history", history);
response (response_l);
}
else
{
error_response (response, "Invalid count limit");
}
}
else
{
error_response (response, "Bad account number");
}
}
void rai::rpc_handler::keepalive ()
{
if (rpc.config.enable_control)
@ -1758,6 +1836,78 @@ void rai::rpc_handler::wallet_add ()
}
}
void rai::rpc_handler::wallet_balance_total ()
{
std::string wallet_text (request.get <std::string> ("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 ())
{
rai::uint128_t balance (0);
rai::uint128_t pending (0);
rai::transaction transaction (node.store.environment, nullptr, false);
for (auto i (existing->second->store.begin (transaction)), n (existing->second->store.end ()); i != n; ++i)
{
rai::account account(i->first);
balance = balance + node.ledger.account_balance (transaction, account);
pending = pending + node.ledger.account_pending (transaction, account);
}
boost::property_tree::ptree response_l;
response_l.put ("balance", balance.convert_to <std::string> ());
response_l.put ("pending", pending.convert_to <std::string> ());
response (response_l);
}
else
{
error_response (response, "Wallet not found");
}
}
else
{
error_response (response, "Bad wallet number");
}
}
void rai::rpc_handler::wallet_balances ()
{
std::string wallet_text (request.get <std::string> ("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 ())
{
boost::property_tree::ptree response_l;
boost::property_tree::ptree balances;
rai::transaction transaction (node.store.environment, nullptr, false);
for (auto i (existing->second->store.begin (transaction)), n (existing->second->store.end ()); i != n; ++i)
{
rai::account account(i->first);
boost::property_tree::ptree entry;
rai::uint128_t balance = node.ledger.account_balance (transaction, account);
rai::uint128_t pending = node.ledger.account_pending (transaction, account);
entry.put ("balance", balance.convert_to <std::string> ());
entry.put ("pending", pending.convert_to <std::string> ());
balances.push_back (std::make_pair (account.to_account (), entry));
}
response_l.add_child ("balances", balances);
response (response_l);
}
else
{
error_response (response, "Wallet not found");
}
}
else
{
error_response (response, "Bad wallet number");
}
}
void rai::rpc_handler::wallet_change_seed ()
{
if (rpc.config.enable_control)
@ -2243,6 +2393,10 @@ void rai::rpc_handler::process_request ()
{
account_get ();
}
else if (action == "account_history")
{
account_history ();
}
else if (action == "account_key")
{
account_key ();
@ -2291,6 +2445,10 @@ void rai::rpc_handler::process_request ()
{
block ();
}
else if (action == "blocks")
{
blocks ();
}
else if (action == "block_account")
{
block_account ();
@ -2439,6 +2597,14 @@ void rai::rpc_handler::process_request ()
{
wallet_add ();
}
else if (action == "wallet_balance_total")
{
wallet_balance_total ();
}
else if (action == "wallet_balances")
{
wallet_balances ();
}
else if (action == "wallet_change_seed")
{
wallet_change_seed ();

View file

@ -93,6 +93,7 @@ public:
void account_block_count ();
void account_create ();
void account_get ();
void account_history ();
void account_key ();
void account_list ();
void account_move ();
@ -105,6 +106,7 @@ public:
void accounts_pending ();
void available_supply ();
void block ();
void blocks ();
void block_account ();
void block_count ();
void bootstrap ();
@ -142,6 +144,8 @@ public:
void validate_account_number ();
void version ();
void wallet_add ();
void wallet_balance_total ();
void wallet_balances ();
void wallet_change_seed ();
void wallet_contains ();
void wallet_create ();