Skip to content

Commit

Permalink
Removing ledger::amount_safe as the value can be retrieved from sideb…
Browse files Browse the repository at this point in the history
…and.
  • Loading branch information
clemahieu committed Feb 20, 2024
1 parent a95ff7e commit 2016dff
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 62 deletions.
3 changes: 0 additions & 3 deletions nano/core_test/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5493,9 +5493,6 @@ TEST (ledger, pruning_safe_functions)
ASSERT_EQ (nano::dev::constants.genesis_amount - nano::Gxrb_ratio * 2, ledger.balance_safe (transaction, send2->hash (), error));
ASSERT_FALSE (error);
error = false;
ASSERT_EQ (0, ledger.amount_safe (transaction, send2->hash (), error));
ASSERT_TRUE (error);
error = false;
ASSERT_TRUE (ledger.account_safe (transaction, send1->hash (), error).is_zero ());
ASSERT_TRUE (error);
error = false;
Expand Down
53 changes: 13 additions & 40 deletions nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1137,12 +1137,7 @@ void nano::json_handler::block_info ()
{
nano::account account (block->account ().is_zero () ? block->sideband ().account : block->account ());
response_l.put ("block_account", account.to_account ());
bool error_or_pruned (false);
auto amount (node.ledger.amount_safe (transaction, hash, error_or_pruned));
if (!error_or_pruned)
{
response_l.put ("amount", amount.convert_to<std::string> ());
}
response_l.put ("amount", block->sideband ().amount.number ().convert_to<std::string> ());
auto balance (node.ledger.balance (transaction, hash));
response_l.put ("balance", balance.convert_to<std::string> ());
response_l.put ("height", std::to_string (block->sideband ().height));
Expand Down Expand Up @@ -1202,17 +1197,13 @@ void nano::json_handler::block_confirm ()
node.active.recently_cemented.put (status);
// Trigger callback for confirmed block
auto account (node.ledger.account (transaction, hash));
bool error_or_pruned (false);
auto amount (node.ledger.amount_safe (transaction, hash, error_or_pruned));
auto amount = block_l->sideband ().amount.number ();
bool is_state_send (false);
bool is_state_epoch (false);
if (!error_or_pruned)
if (auto state = dynamic_cast<nano::state_block *> (block_l.get ()))
{
if (auto state = dynamic_cast<nano::state_block *> (block_l.get ()))
{
is_state_send = node.ledger.is_send (transaction, *state);
is_state_epoch = amount == 0 && node.ledger.is_epoch_link (state->link ());
}
is_state_send = node.ledger.is_send (transaction, *state);
is_state_epoch = amount == 0 && node.ledger.is_epoch_link (state->link ());
}
node.observers.blocks.notify (status, {}, account, amount, is_state_send, is_state_epoch);
}
Expand Down Expand Up @@ -1296,12 +1287,7 @@ void nano::json_handler::blocks_info ()
boost::property_tree::ptree entry;
nano::account account (block->account ().is_zero () ? block->sideband ().account : block->account ());
entry.put ("block_account", account.to_account ());
bool error_or_pruned (false);
auto amount (node.ledger.amount_safe (transaction, hash, error_or_pruned));
if (!error_or_pruned)
{
entry.put ("amount", amount.convert_to<std::string> ());
}
entry.put ("amount", block->sideband ().amount.number ().convert_to<std::string> ());
auto balance (node.ledger.balance (transaction, hash));
entry.put ("balance", balance.convert_to<std::string> ());
entry.put ("height", std::to_string (block->sideband ().height));
Expand Down Expand Up @@ -2382,12 +2368,7 @@ class history_visitor : public nano::block_visitor
tree.put ("type", "send");
auto account (block_a.hashables.destination.to_account ());
tree.put ("account", account);
bool error_or_pruned (false);
auto amount (handler.node.ledger.amount_safe (transaction, hash, error_or_pruned).convert_to<std::string> ());
if (!error_or_pruned)
{
tree.put ("amount", amount);
}
tree.put ("amount", block_a.sideband ().amount.number ());
if (raw)
{
tree.put ("destination", account);
Expand All @@ -2398,16 +2379,12 @@ class history_visitor : public nano::block_visitor
void receive_block (nano::receive_block const & block_a)
{
tree.put ("type", "receive");
tree.put ("amount", block_a.sideband ().amount.number ());
bool error_or_pruned (false);
auto amount (handler.node.ledger.amount_safe (transaction, hash, error_or_pruned).convert_to<std::string> ());
auto source_account (handler.node.ledger.account_safe (transaction, block_a.hashables.source, error_or_pruned));
if (!error_or_pruned)
{
auto source_account (handler.node.ledger.account_safe (transaction, block_a.hashables.source, error_or_pruned));
if (!error_or_pruned)
{
tree.put ("account", source_account.to_account ());
}
tree.put ("amount", amount);
tree.put ("account", source_account.to_account ());
}
if (raw)
{
Expand All @@ -2431,16 +2408,12 @@ class history_visitor : public nano::block_visitor
}
if (block_a.hashables.source != handler.node.ledger.constants.genesis->account ())
{
tree.put ("amount", block_a.sideband ().amount.number ());
bool error_or_pruned (false);
auto amount (handler.node.ledger.amount_safe (transaction, hash, error_or_pruned).convert_to<std::string> ());
auto source_account (handler.node.ledger.account_safe (transaction, block_a.hashables.source, error_or_pruned));
if (!error_or_pruned)
{
auto source_account (handler.node.ledger.account_safe (transaction, block_a.hashables.source, error_or_pruned));
if (!error_or_pruned)
{
tree.put ("account", source_account.to_account ());
}
tree.put ("amount", amount);
tree.put ("account", source_account.to_account ());
}
}
else
Expand Down
10 changes: 3 additions & 7 deletions nano/qt/qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,18 +522,14 @@ class short_text_visitor : public nano::block_visitor
type = "Send";
account = block_a.hashables.destination;
bool error_or_pruned (false);
amount = ledger.amount_safe (transaction, block_a.hash (), error_or_pruned);
if (error_or_pruned)
{
type = "Send (pruned)";
}
amount = block_a.sideband ().amount.number ();
}
void receive_block (nano::receive_block const & block_a)
{
type = "Receive";
bool error_or_pruned (false);
account = ledger.account_safe (transaction, block_a.hashables.source, error_or_pruned);
amount = ledger.amount_safe (transaction, block_a.hash (), error_or_pruned);
amount = block_a.sideband ().amount.number ();
if (error_or_pruned)
{
type = "Receive (pruned)";
Expand All @@ -546,7 +542,7 @@ class short_text_visitor : public nano::block_visitor
{
bool error_or_pruned (false);
account = ledger.account_safe (transaction, block_a.hashables.source, error_or_pruned);
amount = ledger.amount_safe (transaction, block_a.hash (), error_or_pruned);
amount = block_a.sideband ().amount.number ();
if (error_or_pruned)
{
type = "Receive (pruned)";
Expand Down
10 changes: 0 additions & 10 deletions nano/secure/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1089,16 +1089,6 @@ std::optional<nano::account_info> nano::ledger::account_info (store::transaction
return store.account.get (transaction, account);
}

nano::uint128_t nano::ledger::amount_safe (store::transaction const & transaction_a, nano::block_hash const & hash_a, bool & error_a) const
{
auto block (store.block.get (transaction_a, hash_a));
debug_assert (block);
auto block_balance (balance (transaction_a, hash_a));
auto previous_balance (balance_safe (transaction_a, block->previous (), error_a));
return error_a ? 0 : block_balance > previous_balance ? block_balance - previous_balance
: previous_balance - block_balance;
}

// Return latest block for account
nano::block_hash nano::ledger::latest (store::transaction const & transaction_a, nano::account const & account_a)
{
Expand Down
2 changes: 0 additions & 2 deletions nano/secure/ledger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ class ledger final
* Return account containing hash, returns zero account if account can not be found
*/
nano::account account_safe (store::transaction const &, nano::block_hash const &) const;
/** Safe for previous block, but block hash_a must exist */
nano::uint128_t amount_safe (store::transaction const &, nano::block_hash const & hash_a, bool &) const;
static nano::uint128_t balance (nano::block const & block);
nano::uint128_t balance (store::transaction const &, nano::block_hash const &) const;
nano::uint128_t balance_safe (store::transaction const &, nano::block_hash const &, bool &) const;
Expand Down

0 comments on commit 2016dff

Please sign in to comment.