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

Add subtype field for state blocks in RPC block_info, blocks_info #1774

Merged
merged 5 commits into from
Feb 25, 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
42 changes: 42 additions & 0 deletions nano/core_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3496,6 +3496,48 @@ TEST (rpc, blocks_info)
}
}

TEST (rpc, blocks_info_subtype)
{
nano::system system (24000, 1);
auto & node1 (*system.nodes[0]);
nano::keypair key;
system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv);
system.wallet (0)->insert_adhoc (key.prv);
auto send (system.wallet (0)->send_action (nano::test_genesis_key.pub, nano::test_genesis_key.pub, nano::Gxrb_ratio));
ASSERT_NE (nullptr, send);
auto receive (system.wallet (0)->receive_action (*send, key.pub, nano::Gxrb_ratio));
ASSERT_NE (nullptr, receive);
auto change (system.wallet (0)->change_action (nano::test_genesis_key.pub, key.pub));
ASSERT_NE (nullptr, change);
nano::rpc rpc (system.io_ctx, node1, nano::rpc_config (true));
rpc.start ();
boost::property_tree::ptree request;
request.put ("action", "blocks_info");
boost::property_tree::ptree peers_l;
boost::property_tree::ptree entry;
entry.put ("", send->hash ().to_string ());
peers_l.push_back (std::make_pair ("", entry));
entry.put ("", receive->hash ().to_string ());
peers_l.push_back (std::make_pair ("", entry));
entry.put ("", change->hash ().to_string ());
peers_l.push_back (std::make_pair ("", entry));
request.add_child ("hashes", peers_l);
test_response response (request, rpc, system.io_ctx);
system.deadline_set (5s);
while (response.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
}
auto & blocks (response.json.get_child ("blocks"));
ASSERT_EQ (3, blocks.size ());
auto send_subtype (blocks.get_child (send->hash ().to_string ()).get<std::string> ("subtype"));
ASSERT_EQ (send_subtype, "send");
auto receive_subtype (blocks.get_child (receive->hash ().to_string ()).get<std::string> ("subtype"));
ASSERT_EQ (receive_subtype, "receive");
auto change_subtype (blocks.get_child (change->hash ().to_string ()).get<std::string> ("subtype"));
ASSERT_EQ (change_subtype, "change");
}

TEST (rpc, work_peers_all)
{
nano::system system (24000, 1);
Expand Down
17 changes: 17 additions & 0 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1464,6 +1464,23 @@ startup_time (std::chrono::steady_clock::now ())
if (is_state_send_a)
{
event.add ("is_send", is_state_send_a);
event.add ("subtype", "send");
}
// Subtype field
else if (block_a->type () == nano::block_type::state)
{
if (block_a->link ().is_zero ())
{
event.add ("subtype", "change");
}
else if (amount_a == 0 && !node_l->ledger.epoch_link.is_zero () && node_l->ledger.is_epoch_link (block_a->link ()))
{
event.add ("subtype", "epoch");
}
else
{
event.add ("subtype", "receive");
}
}
std::stringstream ostream;
boost::property_tree::write_json (ostream, event);
Expand Down
34 changes: 33 additions & 1 deletion nano/node/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,31 @@ void nano::rpc_handler::available_supply ()
response_errors ();
}

void state_subtype (nano::transaction const & transaction_a, nano::node & node_a, std::shared_ptr<nano::block> block_a, nano::uint128_t const & balance_a, boost::property_tree::ptree & tree_a)
{
// Subtype check
auto previous_balance (node_a.ledger.balance (transaction_a, block_a->previous ()));
if (balance_a < previous_balance)
{
tree_a.put ("subtype", "send");
}
else
{
if (block_a->link ().is_zero ())
{
tree_a.put ("subtype", "change");
}
else if (balance_a == previous_balance && !node_a.ledger.epoch_link.is_zero () && node_a.ledger.is_epoch_link (block_a->link ()))
{
tree_a.put ("subtype", "epoch");
}
else
{
tree_a.put ("subtype", "receive");
}
}
}

void nano::rpc_handler::block_info ()
{
auto hash (hash_impl ());
Expand Down Expand Up @@ -834,6 +859,10 @@ void nano::rpc_handler::block_info ()
block->serialize_json (contents);
response_l.put ("contents", contents);
}
if (block->type () == nano::block_type::state)
{
state_subtype (transaction, node, block, balance, response_l);
}
}
else
{
Expand Down Expand Up @@ -941,7 +970,10 @@ void nano::rpc_handler::blocks_info ()
block->serialize_json (contents);
entry.put ("contents", contents);
}

if (block->type () == nano::block_type::state)
{
wezrule marked this conversation as resolved.
Show resolved Hide resolved
state_subtype (transaction, node, block, balance, entry);
}
if (pending)
{
bool exists (false);
Expand Down