From 3bbb4c52ea48b8893df2fadf9012517815b8679a Mon Sep 17 00:00:00 2001 From: clemahieu Date: Thu, 3 Mar 2022 03:13:04 +0000 Subject: [PATCH] Removing context::previous_balance function in favor of using value in account_info. --- nano/core_test/account_state_decorator.cpp | 6 ++--- nano/core_test/link_filter.cpp | 14 ++++++++++ nano/core_test/signer_decorator.cpp | 6 +++++ nano/node/block_pipeline/context.cpp | 30 +++------------------- nano/node/block_pipeline/context.hpp | 1 - nano/node/block_pipeline/link_filter.cpp | 3 ++- 6 files changed, 29 insertions(+), 31 deletions(-) diff --git a/nano/core_test/account_state_decorator.cpp b/nano/core_test/account_state_decorator.cpp index a718c7b3ff..918e871187 100644 --- a/nano/core_test/account_state_decorator.cpp +++ b/nano/core_test/account_state_decorator.cpp @@ -104,7 +104,7 @@ TEST (account_state_decorator, previous_open) context.filter.sink (block); ASSERT_EQ (1, context.output.size ()); auto const & output0 = context.output[0]; - ASSERT_EQ (nano::amount{ nano::dev::constants.genesis_amount }, output0.previous_balance ()); + ASSERT_EQ (nano::amount{ nano::dev::constants.genesis_amount }, output0.state->balance); } TEST (account_state_decorator, previous_send) @@ -115,7 +115,7 @@ TEST (account_state_decorator, previous_send) context.filter.sink (block); ASSERT_EQ (1, context.output.size ()); auto const & output0 = context.output[0]; - ASSERT_EQ (nano::amount{ nano::dev::constants.genesis_amount - 1 }, output0.previous_balance ()); + ASSERT_EQ (nano::amount{ nano::dev::constants.genesis_amount - 1 }, output0.state->balance); } TEST (account_state_decorator, previous_state) @@ -126,5 +126,5 @@ TEST (account_state_decorator, previous_state) context.filter.sink (block); ASSERT_EQ (1, context.output.size ()); auto const & output0 = context.output[0]; - ASSERT_EQ (nano::amount{ nano::dev::constants.genesis_amount - 1 }, output0.previous_balance ()); + ASSERT_EQ (nano::amount{ nano::dev::constants.genesis_amount - 1 }, output0.state->balance); } diff --git a/nano/core_test/link_filter.cpp b/nano/core_test/link_filter.cpp index cb5aa5ed10..9ac7c7cbe1 100644 --- a/nano/core_test/link_filter.cpp +++ b/nano/core_test/link_filter.cpp @@ -45,6 +45,8 @@ nano::block_pipeline::context noop_state_blocks () .work (0) .build_shared (); result.previous = nano::dev::genesis; + result.state = nano::account_info{}; + result.state->balance = nano::dev::constants.genesis_amount; return result; } nano::block_pipeline::context noop_change_blocks () @@ -58,6 +60,8 @@ nano::block_pipeline::context noop_change_blocks () .work (0) .build_shared (); result.previous = nano::dev::genesis; + result.state = nano::account_info{}; + result.state->balance = nano::dev::constants.genesis_amount; return result; } nano::block_pipeline::context account_state_blocks () @@ -74,6 +78,8 @@ nano::block_pipeline::context account_state_blocks () .work (0) .build_shared (); result.previous = nano::dev::genesis; + result.state = nano::account_info{}; + result.state->balance = nano::dev::constants.genesis_amount; return result; } nano::block_pipeline::context account_send_blocks () @@ -88,6 +94,8 @@ nano::block_pipeline::context account_send_blocks () .work (0) .build_shared (); result.previous = nano::dev::genesis; + result.state = nano::account_info{}; + result.state->balance = nano::dev::constants.genesis_amount; return result; } nano::block_pipeline::context hash_state_blocks () @@ -101,6 +109,8 @@ nano::block_pipeline::context hash_state_blocks () .sign (nano::dev::genesis_key.prv, nano::dev::genesis_key.pub) .work (0) .build_shared (); + result.state = nano::account_info{}; + result.state->balance = nano::dev::constants.genesis_amount - 1; result.block = builder.state () .account (nano::dev::genesis_key.pub) .previous (result.previous->hash ()) @@ -123,6 +133,8 @@ nano::block_pipeline::context hash_receive_blocks () .sign (nano::dev::genesis_key.prv, nano::dev::genesis_key.pub) .work (0) .build_shared (); + result.state = nano::account_info{}; + result.state->balance = nano::dev::constants.genesis_amount - 1; result.block = builder.receive () // Receive block is a hash .previous (nano::dev::genesis->hash ()) .source (result.previous->hash ()) @@ -146,6 +158,8 @@ nano::block_pipeline::context epoch_state_blocks () .work (0) .build_shared (); result.previous = nano::dev::genesis; + result.state = nano::account_info{}; + result.state->balance = nano::dev::constants.genesis_amount; return result; } } diff --git a/nano/core_test/signer_decorator.cpp b/nano/core_test/signer_decorator.cpp index 5009474f2d..f0240efd5e 100644 --- a/nano/core_test/signer_decorator.cpp +++ b/nano/core_test/signer_decorator.cpp @@ -55,6 +55,8 @@ nano::block_pipeline::context exists_block () .work (0) .build_shared (); result.previous = nano::dev::genesis; + result.state = nano::account_info{}; + result.state->balance = nano::dev::constants.genesis_amount; return result; } nano::block_pipeline::context epoch_block () @@ -72,6 +74,8 @@ nano::block_pipeline::context epoch_block () .work (0) .build_shared (); result.previous = nano::dev::genesis; + result.state = nano::account_info{}; + result.state->balance = nano::dev::constants.genesis_amount; return result; } nano::block_pipeline::context epoch_link_block () @@ -89,6 +93,8 @@ nano::block_pipeline::context epoch_link_block () .work (0) .build_shared (); result.previous = nano::dev::genesis; + result.state = nano::account_info{}; + result.state->balance = nano::dev::constants.genesis_amount; return result; } std::shared_ptr epoch_gap () diff --git a/nano/node/block_pipeline/context.cpp b/nano/node/block_pipeline/context.cpp index 7279f08616..537ae6e377 100644 --- a/nano/node/block_pipeline/context.cpp +++ b/nano/node/block_pipeline/context.cpp @@ -2,33 +2,11 @@ bool nano::block_pipeline::context::is_send () const { + debug_assert (state.has_value ()); auto legacy_send = block->type () == nano::block_type::send; - auto state = block->type () == nano::block_type::state; - auto decreased = block->balance () < previous_balance (); - return legacy_send || (state && decreased); -} - -nano::amount nano::block_pipeline::context::previous_balance () const -{ - if (previous == nullptr) - { - return 0; - } - switch (previous->type ()) - { - // Balance is not written in to legacy blocks though the node will store it in the block's sideband data - case nano::block_type::change: - case nano::block_type::open: - case nano::block_type::receive: - return previous->sideband ().balance; - case nano::block_type::state: - case nano::block_type::send: - return previous->balance (); - case nano::block_type::not_a_block: - case nano::block_type::invalid: - debug_assert (false); - return 0; - } + auto type = block->type () == nano::block_type::state; + auto decreased = block->balance () < state->balance; + return legacy_send || (type && decreased); } nano::account nano::block_pipeline::context::account () const diff --git a/nano/node/block_pipeline/context.hpp b/nano/node/block_pipeline/context.hpp index 86ef703bda..301637fd45 100644 --- a/nano/node/block_pipeline/context.hpp +++ b/nano/node/block_pipeline/context.hpp @@ -17,7 +17,6 @@ namespace block_pipeline context () = default; bool is_send () const; nano::account account () const; - nano::amount previous_balance () const; std::shared_ptr block; std::shared_ptr previous; std::optional signer; diff --git a/nano/node/block_pipeline/link_filter.cpp b/nano/node/block_pipeline/link_filter.cpp index ccd7dc984a..51e5471256 100644 --- a/nano/node/block_pipeline/link_filter.cpp +++ b/nano/node/block_pipeline/link_filter.cpp @@ -8,10 +8,11 @@ nano::block_pipeline::link_filter::link_filter (nano::epochs & epochs) : void nano::block_pipeline::link_filter::sink (context & context) { + debug_assert (context.state.has_value ()); switch (context.block->type ()) { case nano::block_type::state: - if (context.block->balance () < context.previous_balance ()) + if (context.block->balance () < context.state->balance) { account (context); break;