Skip to content

Commit

Permalink
Remove ledger::amount in favor of getting the information from sideband.
Browse files Browse the repository at this point in the history
  • Loading branch information
clemahieu committed Feb 20, 2024
1 parent 0960c62 commit a95ff7e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 35 deletions.
45 changes: 22 additions & 23 deletions nano/core_test/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ TEST (ledger, process_send)
ASSERT_EQ (nano::ledger_code::progress, return1);
ASSERT_EQ (nano::dev::genesis_key.pub, send->sideband ().account);
ASSERT_EQ (2, send->sideband ().height);
ASSERT_EQ (nano::dev::constants.genesis_amount - 50, ledger.amount (transaction, hash1));
ASSERT_EQ (nano::dev::constants.genesis_amount - 50, send->sideband ().amount.number ());
ASSERT_EQ (nano::dev::genesis_key.pub, ledger.account (*send));
ASSERT_EQ (50, ledger.account_balance (transaction, nano::dev::genesis_key.pub));
ASSERT_EQ (nano::dev::constants.genesis_amount - 50, ledger.account_receivable (transaction, key2.pub));
Expand Down Expand Up @@ -136,10 +136,9 @@ TEST (ledger, process_send)
ASSERT_EQ (key2.pub, open->sideband ().account);
ASSERT_EQ (nano::dev::constants.genesis_amount - 50, open->sideband ().balance.number ());
ASSERT_EQ (1, open->sideband ().height);
ASSERT_EQ (nano::dev::constants.genesis_amount - 50, ledger.amount (transaction, hash2));
ASSERT_EQ (nano::dev::constants.genesis_amount - 50, open->sideband ().amount.number ());
ASSERT_EQ (nano::ledger_code::progress, return2);
ASSERT_EQ (key2.pub, ledger.account (*open));
ASSERT_EQ (nano::dev::constants.genesis_amount - 50, ledger.amount (transaction, hash2));
ASSERT_EQ (nano::dev::constants.genesis_amount - 50, ledger.account_balance (transaction, key2.pub));
ASSERT_EQ (0, ledger.account_receivable (transaction, key2.pub));
ASSERT_EQ (50, ledger.weight (nano::dev::genesis_key.pub));
Expand Down Expand Up @@ -222,7 +221,7 @@ TEST (ledger, process_receive)
ASSERT_EQ (key2.pub, open->sideband ().account);
ASSERT_EQ (nano::dev::constants.genesis_amount - 50, open->sideband ().balance.number ());
ASSERT_EQ (1, open->sideband ().height);
ASSERT_EQ (nano::dev::constants.genesis_amount - 50, ledger.amount (transaction, hash2));
ASSERT_EQ (nano::dev::constants.genesis_amount - 50, open->sideband ().amount.number ());
ASSERT_EQ (nano::dev::constants.genesis_amount - 50, ledger.weight (key3.pub));
auto send2 = builder
.send ()
Expand All @@ -246,7 +245,7 @@ TEST (ledger, process_receive)
ASSERT_EQ (key2.pub, receive->sideband ().account);
ASSERT_EQ (nano::dev::constants.genesis_amount - 25, receive->sideband ().balance.number ());
ASSERT_EQ (2, receive->sideband ().height);
ASSERT_EQ (25, ledger.amount (transaction, hash4));
ASSERT_EQ (25, receive->sideband ().amount.number ());
ASSERT_EQ (nano::ledger_code::progress, return2);
ASSERT_EQ (key2.pub, ledger.account (*receive));
ASSERT_EQ (hash4, ledger.latest (transaction, key2.pub));
Expand Down Expand Up @@ -510,7 +509,7 @@ TEST (ledger, representative_change)
.build ();
auto return1 (ledger.process (transaction, block));
ASSERT_EQ (nano::ledger_code::progress, return1);
ASSERT_EQ (0, ledger.amount (transaction, block->hash ()));
ASSERT_EQ (0, block->sideband ().amount.number ());
ASSERT_EQ (nano::dev::genesis_key.pub, ledger.account (*block));
ASSERT_EQ (0, ledger.weight (nano::dev::genesis_key.pub));
ASSERT_EQ (nano::dev::constants.genesis_amount, ledger.weight (key2.pub));
Expand Down Expand Up @@ -2371,7 +2370,7 @@ TEST (ledger, state_send_receive)
ASSERT_NE (nullptr, send2);
ASSERT_EQ (*send1, *send2);
ASSERT_EQ (nano::dev::constants.genesis_amount - nano::Gxrb_ratio, ledger.balance (transaction, send1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, ledger.amount (transaction, send1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, send1->sideband ().amount.number ());
ASSERT_EQ (nano::dev::constants.genesis_amount - nano::Gxrb_ratio, ledger.weight (nano::dev::genesis->account ()));
ASSERT_TRUE (store.pending.exists (transaction, nano::pending_key (nano::dev::genesis->account (), send1->hash ())));
ASSERT_EQ (2, send2->sideband ().height);
Expand All @@ -2394,7 +2393,7 @@ TEST (ledger, state_send_receive)
ASSERT_NE (nullptr, receive2);
ASSERT_EQ (*receive1, *receive2);
ASSERT_EQ (nano::dev::constants.genesis_amount, ledger.balance (transaction, receive1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, ledger.amount (transaction, receive1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, receive1->sideband ().amount.number ());
ASSERT_EQ (nano::dev::constants.genesis_amount, ledger.weight (nano::dev::genesis->account ()));
ASSERT_FALSE (store.pending.exists (transaction, nano::pending_key (nano::dev::genesis->account (), send1->hash ())));
ASSERT_EQ (store.account.count (transaction), ledger.cache.account_count);
Expand Down Expand Up @@ -2426,7 +2425,7 @@ TEST (ledger, state_receive)
ASSERT_NE (nullptr, send2);
ASSERT_EQ (*send1, *send2);
ASSERT_EQ (nano::dev::constants.genesis_amount - nano::Gxrb_ratio, ledger.balance (transaction, send1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, ledger.amount (transaction, send1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, send1->sideband ().amount.number ());
ASSERT_EQ (nano::dev::constants.genesis_amount - nano::Gxrb_ratio, ledger.weight (nano::dev::genesis->account ()));
auto receive1 = builder
.state ()
Expand All @@ -2444,7 +2443,7 @@ TEST (ledger, state_receive)
ASSERT_NE (nullptr, receive2);
ASSERT_EQ (*receive1, *receive2);
ASSERT_EQ (nano::dev::constants.genesis_amount, ledger.balance (transaction, receive1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, ledger.amount (transaction, receive1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, receive1->sideband ().amount.number ());
ASSERT_EQ (nano::dev::constants.genesis_amount, ledger.weight (nano::dev::genesis->account ()));
ASSERT_EQ (3, receive2->sideband ().height);
ASSERT_FALSE (receive2->sideband ().details.is_send);
Expand Down Expand Up @@ -2477,7 +2476,7 @@ TEST (ledger, state_rep_change)
ASSERT_NE (nullptr, change2);
ASSERT_EQ (*change1, *change2);
ASSERT_EQ (nano::dev::constants.genesis_amount, ledger.balance (transaction, change1->hash ()));
ASSERT_EQ (0, ledger.amount (transaction, change1->hash ()));
ASSERT_EQ (0, change1->sideband ().amount.number ());
ASSERT_EQ (0, ledger.weight (nano::dev::genesis->account ()));
ASSERT_EQ (nano::dev::constants.genesis_amount, ledger.weight (rep.pub));
ASSERT_EQ (2, change2->sideband ().height);
Expand Down Expand Up @@ -2511,7 +2510,7 @@ TEST (ledger, state_open)
ASSERT_NE (nullptr, send2);
ASSERT_EQ (*send1, *send2);
ASSERT_EQ (nano::dev::constants.genesis_amount - nano::Gxrb_ratio, ledger.balance (transaction, send1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, ledger.amount (transaction, send1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, send1->sideband ().amount.number ());
ASSERT_EQ (nano::dev::constants.genesis_amount - nano::Gxrb_ratio, ledger.weight (nano::dev::genesis->account ()));
ASSERT_TRUE (store.pending.exists (transaction, nano::pending_key (destination.pub, send1->hash ())));
auto open1 = builder
Expand All @@ -2531,7 +2530,7 @@ TEST (ledger, state_open)
ASSERT_NE (nullptr, open2);
ASSERT_EQ (*open1, *open2);
ASSERT_EQ (nano::Gxrb_ratio, ledger.balance (transaction, open1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, ledger.amount (transaction, open1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, open1->sideband ().amount.number ());
ASSERT_EQ (nano::dev::constants.genesis_amount, ledger.weight (nano::dev::genesis->account ()));
ASSERT_EQ (ledger.cache.account_count, store.account.count (transaction));
ASSERT_EQ (1, open2->sideband ().height);
Expand Down Expand Up @@ -2654,7 +2653,7 @@ TEST (ledger, state_unreceivable_fail)
ASSERT_NE (nullptr, send2);
ASSERT_EQ (*send1, *send2);
ASSERT_EQ (nano::dev::constants.genesis_amount - nano::Gxrb_ratio, ledger.balance (transaction, send1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, ledger.amount (transaction, send1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, send1->sideband ().amount.number ());
ASSERT_EQ (nano::dev::constants.genesis_amount - nano::Gxrb_ratio, ledger.weight (nano::dev::genesis->account ()));
auto receive1 = builder
.state ()
Expand Down Expand Up @@ -2691,7 +2690,7 @@ TEST (ledger, state_receive_bad_amount_fail)
ASSERT_NE (nullptr, send2);
ASSERT_EQ (*send1, *send2);
ASSERT_EQ (nano::dev::constants.genesis_amount - nano::Gxrb_ratio, ledger.balance (transaction, send1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, ledger.amount (transaction, send1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, send1->sideband ().amount.number ());
ASSERT_EQ (nano::dev::constants.genesis_amount - nano::Gxrb_ratio, ledger.weight (nano::dev::genesis->account ()));
auto receive1 = builder
.state ()
Expand Down Expand Up @@ -2763,7 +2762,7 @@ TEST (ledger, state_receive_wrong_account_fail)
ASSERT_NE (nullptr, send2);
ASSERT_EQ (*send1, *send2);
ASSERT_EQ (nano::dev::constants.genesis_amount - nano::Gxrb_ratio, ledger.balance (transaction, send1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, ledger.amount (transaction, send1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, send1->sideband ().amount.number ());
ASSERT_EQ (nano::dev::constants.genesis_amount - nano::Gxrb_ratio, ledger.weight (nano::dev::genesis->account ()));
nano::keypair key;
auto receive1 = builder
Expand Down Expand Up @@ -2957,7 +2956,7 @@ TEST (ledger, state_send_change)
ASSERT_NE (nullptr, send2);
ASSERT_EQ (*send1, *send2);
ASSERT_EQ (nano::dev::constants.genesis_amount - nano::Gxrb_ratio, ledger.balance (transaction, send1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, ledger.amount (transaction, send1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, send1->sideband ().amount.number ());
ASSERT_EQ (0, ledger.weight (nano::dev::genesis->account ()));
ASSERT_EQ (nano::dev::constants.genesis_amount - nano::Gxrb_ratio, ledger.weight (rep.pub));
ASSERT_EQ (2, send2->sideband ().height);
Expand Down Expand Up @@ -2990,7 +2989,7 @@ TEST (ledger, state_receive_change)
ASSERT_NE (nullptr, send2);
ASSERT_EQ (*send1, *send2);
ASSERT_EQ (nano::dev::constants.genesis_amount - nano::Gxrb_ratio, ledger.balance (transaction, send1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, ledger.amount (transaction, send1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, send1->sideband ().amount.number ());
ASSERT_EQ (nano::dev::constants.genesis_amount - nano::Gxrb_ratio, ledger.weight (nano::dev::genesis->account ()));
nano::keypair rep;
auto receive1 = builder
Expand All @@ -3009,7 +3008,7 @@ TEST (ledger, state_receive_change)
ASSERT_NE (nullptr, receive2);
ASSERT_EQ (*receive1, *receive2);
ASSERT_EQ (nano::dev::constants.genesis_amount, ledger.balance (transaction, receive1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, ledger.amount (transaction, receive1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, receive1->sideband ().amount.number ());
ASSERT_EQ (0, ledger.weight (nano::dev::genesis->account ()));
ASSERT_EQ (nano::dev::constants.genesis_amount, ledger.weight (rep.pub));
ASSERT_EQ (3, receive2->sideband ().height);
Expand Down Expand Up @@ -3048,7 +3047,7 @@ TEST (ledger, state_open_old)
.build ();
ASSERT_EQ (nano::ledger_code::progress, ledger.process (transaction, open1));
ASSERT_EQ (nano::Gxrb_ratio, ledger.balance (transaction, open1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, ledger.amount (transaction, open1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, open1->sideband ().amount.number ());
ASSERT_EQ (nano::dev::constants.genesis_amount, ledger.weight (nano::dev::genesis->account ()));
}

Expand Down Expand Up @@ -3101,7 +3100,7 @@ TEST (ledger, state_receive_old)
.build ();
ASSERT_EQ (nano::ledger_code::progress, ledger.process (transaction, receive1));
ASSERT_EQ (2 * nano::Gxrb_ratio, ledger.balance (transaction, receive1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, ledger.amount (transaction, receive1->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, receive1->sideband ().amount.number ());
ASSERT_EQ (nano::dev::constants.genesis_amount, ledger.weight (nano::dev::genesis->account ()));
}

Expand Down Expand Up @@ -3499,7 +3498,7 @@ TEST (ledger, epoch_blocks_v1_general)
ASSERT_EQ (nano::epoch::epoch_1, receive2->sideband ().source_epoch);
ASSERT_EQ (0, ledger.balance (transaction, epoch4->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, ledger.balance (transaction, receive2->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, ledger.amount (transaction, receive2->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, receive2->sideband ().amount.number ());
ASSERT_EQ (nano::dev::constants.genesis_amount - nano::Gxrb_ratio, ledger.weight (nano::dev::genesis->account ()));
ASSERT_EQ (nano::Gxrb_ratio, ledger.weight (destination.pub));
ASSERT_FALSE (receive2->sideband ().details.is_send);
Expand Down Expand Up @@ -3667,7 +3666,7 @@ TEST (ledger, epoch_blocks_v2_general)
ASSERT_EQ (nano::epoch::epoch_1, receive2->sideband ().source_epoch);
ASSERT_EQ (0, ledger.balance (transaction, epoch6->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, ledger.balance (transaction, receive2->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, ledger.amount (transaction, receive2->hash ()));
ASSERT_EQ (nano::Gxrb_ratio, receive2->sideband ().amount.number ());
ASSERT_EQ (nano::dev::constants.genesis_amount - nano::Gxrb_ratio, ledger.weight (nano::dev::genesis->account ()));
ASSERT_EQ (nano::Gxrb_ratio, ledger.weight (destination.pub));
}
Expand Down
3 changes: 2 additions & 1 deletion nano/core_test/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ TEST (system, DISABLED_generate_send_existing)
ASSERT_EQ (info2->balance, nano::dev::constants.genesis_amount / 3);
{
auto transaction (node1.store.tx_begin_read ());
ASSERT_NE (node1.ledger.amount (transaction, info2->head), 0);
auto block = node1.block (info2->head);
ASSERT_NE (block->sideband ().amount, 0);
}
system.stop ();
runner.join ();
Expand Down
3 changes: 1 addition & 2 deletions nano/nano_node/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1719,8 +1719,7 @@ int main (int argc, char * const * argv)
// Check if pending amount is correct
if (!pruned && !previous_pruned)
{
auto amount (node->ledger.amount (transaction, key.hash));
if (info.amount != amount)
if (info.amount != block->sideband ().amount)
{
print_error_message (boost::str (boost::format ("Incorrect amount for pending block %1%\n") % key.hash.to_string ()));
}
Expand Down
10 changes: 2 additions & 8 deletions nano/secure/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class rollback_visitor : public nano::block_visitor
void receive_block (nano::receive_block const & block_a) override
{
auto hash (block_a.hash ());
auto amount (ledger.amount (transaction, hash));
auto amount = block_a.sideband ().amount.number ();
auto destination_account (ledger.account (transaction, hash));
// Pending account entry can be incorrect if source block was pruned. But it's not affecting correct ledger processing
[[maybe_unused]] bool is_pruned (false);
Expand All @@ -78,7 +78,7 @@ class rollback_visitor : public nano::block_visitor
void open_block (nano::open_block const & block_a) override
{
auto hash (block_a.hash ());
auto amount (ledger.amount (transaction, hash));
auto amount = block_a.sideband ().amount.number ();
auto destination_account (ledger.account (transaction, hash));
// Pending account entry can be incorrect if source block was pruned. But it's not affecting correct ledger processing
[[maybe_unused]] bool is_pruned (false);
Expand Down Expand Up @@ -1089,12 +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 (store::transaction const & transaction_a, nano::block_hash const & hash_a)
{
auto block = store.block.get (transaction_a, hash_a);
return block->sideband ().amount.number ();
}

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));
Expand Down
1 change: 0 additions & 1 deletion nano/secure/ledger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +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;
nano::uint128_t amount (store::transaction const &, nano::block_hash 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);
Expand Down

0 comments on commit a95ff7e

Please sign in to comment.