Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPC Account history filter #1355

Merged
merged 8 commits into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions nano/core_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,91 @@ TEST (rpc, history)
ASSERT_EQ (genesis.hash ().to_string (), std::get<3> (history_l[4]));
}

TEST (rpc, account_history)
{
nano::system system (24000, 1);
system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv);
auto change (system.wallet (0)->change_action (nano::test_genesis_key.pub, nano::test_genesis_key.pub));
ASSERT_NE (nullptr, change);
auto send (system.wallet (0)->send_action (nano::test_genesis_key.pub, nano::test_genesis_key.pub, system.nodes[0]->config.receive_minimum.number ()));
ASSERT_NE (nullptr, send);
auto receive (system.wallet (0)->receive_action (*send, nano::test_genesis_key.pub, system.nodes[0]->config.receive_minimum.number ()));
ASSERT_NE (nullptr, receive);
auto node0 (system.nodes[0]);
nano::genesis genesis;
nano::state_block usend (nano::genesis_account, node0->latest (nano::genesis_account), nano::genesis_account, nano::genesis_amount - nano::Gxrb_ratio, nano::genesis_account, nano::test_genesis_key.prv, nano::test_genesis_key.pub, 0);
nano::state_block ureceive (nano::genesis_account, usend.hash (), nano::genesis_account, nano::genesis_amount, usend.hash (), nano::test_genesis_key.prv, nano::test_genesis_key.pub, 0);
nano::state_block uchange (nano::genesis_account, ureceive.hash (), nano::keypair ().pub, nano::genesis_amount, 0, nano::test_genesis_key.prv, nano::test_genesis_key.pub, 0);
{
auto transaction (node0->store.tx_begin (true));
ASSERT_EQ (nano::process_result::progress, node0->ledger.process (transaction, usend).code);
ASSERT_EQ (nano::process_result::progress, node0->ledger.process (transaction, ureceive).code);
ASSERT_EQ (nano::process_result::progress, node0->ledger.process (transaction, uchange).code);
}
nano::rpc rpc (system.io_ctx, *node0, nano::rpc_config (true));
rpc.start ();
boost::property_tree::ptree request;
request.put ("action", "account_history");
request.put ("account", nano::genesis_account.to_account ());
request.put ("count", 100);
test_response response (request, rpc, system.io_ctx);
while (response.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
}
ASSERT_EQ (200, response.status);
std::vector<std::tuple<std::string, std::string, std::string, std::string>> history_l;
auto & history_node (response.json.get_child ("history"));
for (auto i (history_node.begin ()), n (history_node.end ()); i != n; ++i)
{
history_l.push_back (std::make_tuple (i->second.get<std::string> ("type"), i->second.get<std::string> ("account"), i->second.get<std::string> ("amount"), i->second.get<std::string> ("hash")));
}
ASSERT_EQ (5, history_l.size ());
ASSERT_EQ ("receive", std::get<0> (history_l[0]));
ASSERT_EQ (ureceive.hash ().to_string (), std::get<3> (history_l[0]));
ASSERT_EQ (nano::test_genesis_key.pub.to_account (), std::get<1> (history_l[0]));
ASSERT_EQ (nano::Gxrb_ratio.convert_to<std::string> (), std::get<2> (history_l[0]));
ASSERT_EQ (5, history_l.size ());
ASSERT_EQ ("send", std::get<0> (history_l[1]));
ASSERT_EQ (usend.hash ().to_string (), std::get<3> (history_l[1]));
ASSERT_EQ (nano::test_genesis_key.pub.to_account (), std::get<1> (history_l[1]));
ASSERT_EQ (nano::Gxrb_ratio.convert_to<std::string> (), std::get<2> (history_l[1]));
ASSERT_EQ ("receive", std::get<0> (history_l[2]));
ASSERT_EQ (nano::test_genesis_key.pub.to_account (), std::get<1> (history_l[2]));
ASSERT_EQ (system.nodes[0]->config.receive_minimum.to_string_dec (), std::get<2> (history_l[2]));
ASSERT_EQ (receive->hash ().to_string (), std::get<3> (history_l[2]));
ASSERT_EQ ("send", std::get<0> (history_l[3]));
ASSERT_EQ (nano::test_genesis_key.pub.to_account (), std::get<1> (history_l[3]));
ASSERT_EQ (system.nodes[0]->config.receive_minimum.to_string_dec (), std::get<2> (history_l[3]));
ASSERT_EQ (send->hash ().to_string (), std::get<3> (history_l[3]));
ASSERT_EQ ("receive", std::get<0> (history_l[4]));
ASSERT_EQ (nano::test_genesis_key.pub.to_account (), std::get<1> (history_l[4]));
ASSERT_EQ (nano::genesis_amount.convert_to<std::string> (), std::get<2> (history_l[4]));
ASSERT_EQ (genesis.hash ().to_string (), std::get<3> (history_l[4]));
//Tests filtering
auto account2 (system.wallet (0)->deterministic_insert ());
auto send2 (system.wallet (0)->send_action (nano::test_genesis_key.pub, account2, system.nodes[0]->config.receive_minimum.number ()));
ASSERT_NE (nullptr, send2);
auto receive2 (system.wallet (0)->receive_action (static_cast<nano::send_block &> (*send2), account2, system.nodes[0]->config.receive_minimum.number ()));
ASSERT_NE (nullptr, receive2);
boost::property_tree::ptree request2;
request2.put ("action", "account_history");
request2.put ("account", nano::test_genesis_key.pub.to_account ());
boost::property_tree::ptree other_account;
other_account.put ("", account2.to_account ());
boost::property_tree::ptree filtered_accounts;
filtered_accounts.push_back (std::make_pair ("", other_account));
request2.add_child ("account_filter", filtered_accounts);
request2.put ("count", 100);
test_response response2 (request2, rpc, system.io_ctx);
while (response2.status == 0)
{
system.poll ();
}
auto history_node2 (response2.json.get_child ("history"));
ASSERT_EQ (history_node2.size (), 1);
}

TEST (rpc, history_count)
{
nano::system system (24000, 1);
Expand Down
67 changes: 61 additions & 6 deletions nano/node/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1796,17 +1796,22 @@ namespace
class history_visitor : public nano::block_visitor
{
public:
history_visitor (nano::rpc_handler & handler_a, bool raw_a, nano::transaction & transaction_a, boost::property_tree::ptree & tree_a, nano::block_hash const & hash_a) :
history_visitor (nano::rpc_handler & handler_a, bool raw_a, nano::transaction & transaction_a, boost::property_tree::ptree & tree_a, nano::block_hash const & hash_a, std::vector<nano::public_key> const & accounts_filter_a = {}) :
handler (handler_a),
raw (raw_a),
transaction (transaction_a),
tree (tree_a),
hash (hash_a)
hash (hash_a),
accounts_filter (accounts_filter_a)
{
}
virtual ~history_visitor () = default;
void send_block (nano::send_block const & block_a)
{
if (should_ignore_account (block_a.hashables.destination))
{
return;
}
tree.put ("type", "send");
auto account (block_a.hashables.destination.to_account ());
tree.put ("account", account);
Expand All @@ -1821,6 +1826,10 @@ class history_visitor : public nano::block_visitor
}
void receive_block (nano::receive_block const & block_a)
{
if (should_ignore_account (block_a.hashables.source))
{
return;
}
tree.put ("type", "receive");
auto account (handler.node.ledger.account (transaction, block_a.hashables.source).to_account ());
tree.put ("account", account);
Expand All @@ -1834,6 +1843,10 @@ class history_visitor : public nano::block_visitor
}
void open_block (nano::open_block const & block_a)
{
if (should_ignore_account (block_a.hashables.source))
{
return;
}
if (raw)
{
tree.put ("type", "open");
Expand All @@ -1859,7 +1872,7 @@ class history_visitor : public nano::block_visitor
}
void change_block (nano::change_block const & block_a)
{
if (raw)
if (raw && accounts_filter.empty ())
{
tree.put ("type", "change");
tree.put ("representative", block_a.hashables.representative.to_account ());
Expand All @@ -1880,6 +1893,11 @@ class history_visitor : public nano::block_visitor
auto previous_balance (handler.node.ledger.balance (transaction, block_a.hashables.previous));
if (balance < previous_balance)
{
if (should_ignore_account (block_a.hashables.link))
{
tree.clear ();
return;
}
if (raw)
{
tree.put ("subtype", "send");
Expand All @@ -1895,21 +1913,26 @@ class history_visitor : public nano::block_visitor
{
if (block_a.hashables.link.is_zero ())
{
if (raw)
if (raw && accounts_filter.empty ())
{
tree.put ("subtype", "change");
}
}
else if (balance == previous_balance && !handler.node.ledger.epoch_link.is_zero () && handler.node.ledger.is_epoch_link (block_a.hashables.link))
{
if (raw)
if (raw && accounts_filter.empty ())
{
tree.put ("subtype", "epoch");
tree.put ("account", handler.node.ledger.epoch_signer.to_account ());
}
}
else
{
if (should_ignore_account (block_a.hashables.link))
{
tree.clear ();
return;
}
if (raw)
{
tree.put ("subtype", "receive");
Expand All @@ -1923,17 +1946,49 @@ class history_visitor : public nano::block_visitor
}
}
}
bool should_ignore_account (nano::public_key const & account)
{
bool ignore (false);
if (!accounts_filter.empty ())
{
if (std::find (accounts_filter.begin (), accounts_filter.end (), account) == accounts_filter.end ())
{
ignore = true;
}
}
return ignore;
}
nano::rpc_handler & handler;
bool raw;
nano::transaction & transaction;
boost::property_tree::ptree & tree;
nano::block_hash const & hash;
nano::network_params network_params;
std::vector<nano::public_key> const & accounts_filter;
};
}

void nano::rpc_handler::account_history ()
{
std::vector<nano::public_key> accounts_to_filter;
const auto accounts_filter_node = request.get_child_optional ("account_filter");
if (accounts_filter_node.is_initialized ())
{
for (auto & a : (*accounts_filter_node))
{
nano::public_key account;
auto error (account.decode_account (a.second.get<std::string> ("")));
if (!error)
{
accounts_to_filter.push_back (account);
}
else
{
ec = nano::error_common::bad_account_number;
break;
}
}
}
nano::account account;
bool output_raw (request.get_optional<bool> ("raw") == true);
nano::block_hash hash;
Expand Down Expand Up @@ -1982,7 +2037,7 @@ void nano::rpc_handler::account_history ()
else
{
boost::property_tree::ptree entry;
history_visitor visitor (*this, output_raw, transaction, entry, hash);
history_visitor visitor (*this, output_raw, transaction, entry, hash, accounts_to_filter);
block->visit (visitor);
if (!entry.empty ())
{
Expand Down