Skip to content

Commit

Permalink
Merge pull request #4709 from clemahieu/state_open_account_history
Browse files Browse the repository at this point in the history
Fix regression account history when querying open state blocks
  • Loading branch information
clemahieu authored Aug 20, 2024
2 parents 67c88aa + ed11b94 commit be764b0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
12 changes: 7 additions & 5 deletions nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2525,9 +2525,11 @@ class history_visitor : public nano::block_visitor
tree.put ("previous", block_a.hashables.previous.to_string ());
}
auto balance (block_a.hashables.balance.number ());
auto previous_balance = handler.node.ledger.any.block_balance (transaction, block_a.hashables.previous);
if (!previous_balance)
auto previous_balance_raw = handler.node.ledger.any.block_balance (transaction, block_a.hashables.previous);
auto previous_balance = previous_balance_raw.value_or (0);
if (!block_a.hashables.previous.is_zero () && !previous_balance_raw.has_value ())
{
// If previous hash is non-zero and we can't query the balance, e.g. it's pruned, we can't determine the block type
if (raw)
{
tree.put ("subtype", "unknown");
Expand All @@ -2537,7 +2539,7 @@ class history_visitor : public nano::block_visitor
tree.put ("type", "unknown");
}
}
else if (balance < previous_balance.value ().number ())
else if (balance < previous_balance.number ())
{
if (should_ignore_account (block_a.hashables.link.as_account ()))
{
Expand All @@ -2553,7 +2555,7 @@ class history_visitor : public nano::block_visitor
tree.put ("type", "send");
}
tree.put ("account", block_a.hashables.link.to_account ());
tree.put ("amount", (previous_balance.value ().number () - balance).convert_to<std::string> ());
tree.put ("amount", (previous_balance.number () - balance).convert_to<std::string> ());
}
else
{
Expand Down Expand Up @@ -2592,7 +2594,7 @@ class history_visitor : public nano::block_visitor
{
tree.put ("account", source_account.value ().to_account ());
}
tree.put ("amount", (balance - previous_balance.value ().number ()).convert_to<std::string> ());
tree.put ("amount", (balance - previous_balance.number ()).convert_to<std::string> ());
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <nano/secure/ledger.hpp>
#include <nano/secure/ledger_set_any.hpp>
#include <nano/secure/ledger_set_confirmed.hpp>
#include <nano/test_common/chains.hpp>
#include <nano/test_common/network.hpp>
#include <nano/test_common/system.hpp>
#include <nano/test_common/telemetry.hpp>
Expand Down Expand Up @@ -1329,6 +1330,26 @@ TEST (rpc, history_pruning)
ASSERT_EQ (usend->hash ().to_string (), entry.get<std::string> ("hash"));
}

TEST (rpc, account_history_state_open)
{
nano::test::system system;
nano::keypair key;
auto node0 = add_ipc_enabled_node (system);
auto blocks = nano::test::setup_new_account (system, *node0, 1, nano::dev::genesis_key, key, key.pub, true);
auto const rpc_ctx = add_rpc (system, node0);
boost::property_tree::ptree request;
request.put ("action", "account_history");
request.put ("account", key.pub.to_account ());
request.put ("count", 1);
auto response (wait_response (system, rpc_ctx, request, 10s));
auto & history_node (response.get_child ("history"));
ASSERT_EQ (1, history_node.size ());
auto history0 = *history_node.begin ();
ASSERT_EQ ("1", history0.second.get<std::string> ("height"));
ASSERT_EQ ("receive", history0.second.get<std::string> ("type"));
ASSERT_EQ (blocks.second->hash ().to_string (), history0.second.get<std::string> ("hash"));
}

TEST (rpc, process_block)
{
nano::test::system system;
Expand Down

0 comments on commit be764b0

Please sign in to comment.