Skip to content

Commit

Permalink
Change block::link to return an std::optional if the block actually c…
Browse files Browse the repository at this point in the history
…ontains the field.
  • Loading branch information
clemahieu committed Mar 7, 2024
1 parent e587f0e commit 64e753a
Show file tree
Hide file tree
Showing 17 changed files with 75 additions and 75 deletions.
10 changes: 5 additions & 5 deletions nano/core_test/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ TEST (block_builder, state)
ASSERT_EQ (block->hash ().to_string (), "2D243F8F92CDD0AD94A1D456A6B15F3BE7A6FCBD98D4C5831D06D15C818CD81F");
ASSERT_FALSE (block->source_field ());
ASSERT_FALSE (block->destination_field ());
ASSERT_EQ (block->link ().to_string (), "E16DD58C1EFA8B521545B0A74375AA994D9FC43828A4266D75ECF57F07A7EE86");
ASSERT_EQ (block->link ().value ().to_string (), "E16DD58C1EFA8B521545B0A74375AA994D9FC43828A4266D75ECF57F07A7EE86");
}

TEST (block_builder, state_missing_rep)
Expand Down Expand Up @@ -732,7 +732,7 @@ TEST (block_builder, open)
ASSERT_EQ (block->hash ().to_string (), "991CF190094C00F0B68E2E5F75F6BEE95A2E0BD93CEAA4A6734DB9F19B728948");
ASSERT_EQ (block->source_field ().value ().to_string (), "E89208DD038FBB269987689621D52292AE9C35941A7484756ECCED92A65093BA");
ASSERT_FALSE (block->destination_field ());
ASSERT_TRUE (block->link ().is_zero ());
ASSERT_FALSE (block->link ());
}

TEST (block_builder, open_equality)
Expand Down Expand Up @@ -771,7 +771,7 @@ TEST (block_builder, change)
ASSERT_EQ (block->hash ().to_string (), "13552AC3928E93B5C6C215F61879358E248D4A5246B8B3D1EEC5A566EDCEE077");
ASSERT_FALSE (block->source_field ());
ASSERT_FALSE (block->destination_field ());
ASSERT_TRUE (block->link ().is_zero ());
ASSERT_FALSE (block->link ());
}

TEST (block_builder, change_equality)
Expand Down Expand Up @@ -810,7 +810,7 @@ TEST (block_builder, send)
ASSERT_EQ (block->hash ().to_string (), "4560E7B1F3735D082700CFC2852F5D1F378F7418FD24CEF1AD45AB69316F15CD");
ASSERT_FALSE (block->source_field ());
ASSERT_EQ (block->destination_field ().value ().to_account (), "nano_1gys8r4crpxhp94n4uho5cshaho81na6454qni5gu9n53gksoyy1wcd4udyb");
ASSERT_TRUE (block->link ().is_zero ());
ASSERT_FALSE (block->link ());
}

TEST (block_builder, send_equality)
Expand Down Expand Up @@ -872,5 +872,5 @@ TEST (block_builder, receive)
ASSERT_EQ (block->hash ().to_string (), "6C004BF911D9CF2ED75CF6EC45E795122AD5D093FF5A83EDFBA43EC4A3EDC722");
ASSERT_EQ (block->source_field ().value ().to_string (), "7B2B0A29C1B235FDF9B4DEF2984BB3573BD1A52D28246396FBB3E4C5FE662135");
ASSERT_FALSE (block->destination_field ());
ASSERT_TRUE (block->link ().is_zero ());
ASSERT_FALSE (block->link ());
}
12 changes: 6 additions & 6 deletions nano/core_test/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ TEST (wallet, epoch_2_validation)
ASSERT_EQ (nano::epoch::epoch_2, send->sideband ().details.epoch);
ASSERT_EQ (nano::epoch::epoch_0, send->sideband ().source_epoch); // Not used for send state blocks

auto receive = wallet.receive_action (send->hash (), nano::dev::genesis_key.pub, amount, send->link ().as_account (), 1);
auto receive = wallet.receive_action (send->hash (), nano::dev::genesis_key.pub, amount, send->link ().value ().as_account (), 1);
ASSERT_NE (nullptr, receive);
if (nano::dev::network_params.work.difficulty (*receive) < node.network_params.work.base)
{
Expand Down Expand Up @@ -1043,7 +1043,7 @@ TEST (wallet, epoch_2_receive_propagation)
auto amount = node.config.receive_minimum.number ();
auto send1 = wallet.send_action (nano::dev::genesis_key.pub, key.pub, amount, 1);
ASSERT_NE (nullptr, send1);
ASSERT_NE (nullptr, wallet.receive_action (send1->hash (), nano::dev::genesis_key.pub, amount, send1->link ().as_account (), 1));
ASSERT_NE (nullptr, wallet.receive_action (send1->hash (), nano::dev::genesis_key.pub, amount, send1->link ().value ().as_account (), 1));

// Upgrade the genesis account to epoch 2
auto epoch2 = system.upgrade_genesis_epoch (node, nano::epoch::epoch_2);
Expand All @@ -1053,7 +1053,7 @@ TEST (wallet, epoch_2_receive_propagation)
auto send2 = wallet.send_action (nano::dev::genesis_key.pub, key.pub, amount, 1);
ASSERT_NE (nullptr, send2);

auto receive2 = wallet.receive_action (send2->hash (), key.pub, amount, send2->link ().as_account (), 1);
auto receive2 = wallet.receive_action (send2->hash (), key.pub, amount, send2->link ().value ().as_account (), 1);
ASSERT_NE (nullptr, receive2);
if (nano::dev::network_params.work.difficulty (*receive2) < node.network_params.work.base)
{
Expand Down Expand Up @@ -1106,7 +1106,7 @@ TEST (wallet, epoch_2_receive_unopened)

wallet.insert_adhoc (key.prv, false);

auto receive1 = wallet.receive_action (send1->hash (), key.pub, amount, send1->link ().as_account (), 1);
auto receive1 = wallet.receive_action (send1->hash (), key.pub, amount, send1->link ().value ().as_account (), 1);
ASSERT_NE (nullptr, receive1);
if (nano::dev::network_params.work.difficulty (*receive1) < node.network_params.work.base)
{
Expand Down Expand Up @@ -1190,7 +1190,7 @@ TEST (wallet, search_receivable)
auto receive = node.block (receive_hash);
ASSERT_NE (nullptr, receive);
ASSERT_EQ (receive->sideband ().height, 3);
ASSERT_EQ (send->hash (), receive->link ().as_block_hash ());
ASSERT_EQ (send->hash (), receive->link ().value ().as_block_hash ());
}

TEST (wallet, receive_pruned)
Expand Down Expand Up @@ -1228,7 +1228,7 @@ TEST (wallet, receive_pruned)

wallet2.insert_adhoc (key.prv, false);

auto open1 = wallet2.receive_action (send1->hash (), key.pub, amount, send1->link ().as_account (), 1);
auto open1 = wallet2.receive_action (send1->hash (), key.pub, amount, send1->link ().value ().as_account (), 1);
ASSERT_NE (nullptr, open1);
ASSERT_EQ (amount, node2.ledger.balance (node2.store.tx_begin_read (), open1->hash ()));
ASSERT_TIMELY_EQ (5s, node2.ledger.cache.cemented_count, 4);
Expand Down
2 changes: 1 addition & 1 deletion nano/core_test/wallets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,6 @@ TEST (wallets, search_receivable)
auto receive = node.block (receive_hash);
ASSERT_NE (nullptr, receive);
ASSERT_EQ (receive->sideband ().height, 3);
ASSERT_EQ (send->hash (), receive->link ().as_block_hash ());
ASSERT_EQ (send->hash (), receive->link ().value ().as_block_hash ());
}
}
11 changes: 5 additions & 6 deletions nano/lib/blocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,9 @@ std::optional<nano::account> nano::block::destination_field () const
return std::nullopt;
}

nano::link const & nano::block::link () const
std::optional<nano::link> nano::block::link () const
{
static nano::link link{ 0 };
return link;
return std::nullopt;
}

nano::account nano::block::account () const
Expand Down Expand Up @@ -226,7 +225,7 @@ nano::account nano::block::destination () const
return destination_field ().value ();
case nano::block_type::state:
release_assert (sideband ().details.is_send);
return link ().as_account ();
return link ().value ().as_account ();
default:
release_assert (false);
}
Expand All @@ -242,7 +241,7 @@ nano::block_hash nano::block::source ()
return source_field ().value ();
case nano::block_type::state:
release_assert (sideband ().details.is_receive);
return link ().as_block_hash ();
return link ().value ().as_block_hash ();
default:
release_assert (false);
}
Expand Down Expand Up @@ -1424,7 +1423,7 @@ nano::root const & nano::state_block::root () const
}
}

nano::link const & nano::state_block::link () const
std::optional<nano::link> nano::state_block::link () const
{
return hashables.link;
}
Expand Down
6 changes: 3 additions & 3 deletions nano/lib/blocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ class block
virtual nano::root const & root () const = 0;
// Qualified root value based on previous() and root()
virtual nano::qualified_root qualified_root () const;
// Link field for state blocks, zero otherwise.
virtual nano::link const & link () const;
virtual nano::account const & representative () const;
virtual void serialize (nano::stream &) const = 0;
virtual void serialize_json (std::string &, bool = false) const = 0;
Expand Down Expand Up @@ -71,6 +69,8 @@ class block
nano::account destination () const;
// Destination account for send blocks
virtual std::optional<nano::account> destination_field () const;
// Link field for state blocks
virtual std::optional<nano::link> link () const;
// Returns the source block hash for open/receive/state blocks that are receives
nano::block_hash source ();
// Source block for open/receive blocks
Expand Down Expand Up @@ -344,7 +344,6 @@ class state_block : public nano::block
void block_work_set (uint64_t) override;
nano::block_hash const & previous () const override;
nano::root const & root () const override;
nano::link const & link () const override;
nano::account const & representative () const override;
void serialize (nano::stream &) const override;
bool deserialize (nano::stream &);
Expand All @@ -367,6 +366,7 @@ class state_block : public nano::block
public: // State block fields
std::optional<nano::account> account_field () const override;
std::optional<nano::amount> balance_field () const override;
std::optional<nano::link> link () const override;

public: // Logging
void operator() (nano::object_stream &) const override;
Expand Down
12 changes: 6 additions & 6 deletions nano/nano_node/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1501,7 +1501,7 @@ int main (int argc, char * const * argv)
{
if ((state_block.hashables.balance == prev_balance && !error_or_pruned) || (node->ledger.pruning && error_or_pruned && block->sideband ().details.is_epoch))
{
invalid = validate_message (node->ledger.epoch_signer (block->link ()), hash, block->block_signature ());
invalid = validate_message (node->ledger.epoch_signer (block->link ().value ()), hash, block->block_signature ());
}
}
}
Expand Down Expand Up @@ -1529,12 +1529,12 @@ int main (int argc, char * const * argv)
}
else
{
if (block->link ().is_zero ())
if (block->link ().value ().is_zero ())
{
// State change
block_details_error = sideband.details.is_send || sideband.details.is_receive || sideband.details.is_epoch;
}
else if (block->balance () == prev_balance.value () && node->ledger.is_epoch_link (block->link ()))
else if (block->balance () == prev_balance.value () && node->ledger.is_epoch_link (block->link ().value ()))
{
// State epoch
block_details_error = !sideband.details.is_epoch || sideband.details.is_send || sideband.details.is_receive;
Expand All @@ -1543,7 +1543,7 @@ int main (int argc, char * const * argv)
{
// State receive
block_details_error = !sideband.details.is_receive || sideband.details.is_send || sideband.details.is_epoch;
block_details_error |= !node->ledger.block_or_pruned_exists (transaction, block->link ().as_block_hash ());
block_details_error |= !node->ledger.block_or_pruned_exists (transaction, block->link ().value ().as_block_hash ());
}
}
}
Expand All @@ -1557,7 +1557,7 @@ int main (int argc, char * const * argv)
print_error_message (boost::str (boost::format ("Incorrect sideband block details for block %1%\n") % hash.to_string ()));
}
// Check link epoch version
if (sideband.details.is_receive && (!node->ledger.pruning || !node->store.pruned.exists (transaction, block->link ().as_block_hash ())))
if (sideband.details.is_receive && (!node->ledger.pruning || !node->store.pruned.exists (transaction, block->source ())))
{
if (sideband.source_epoch != node->ledger.version (*block))
{
Expand Down Expand Up @@ -1809,7 +1809,7 @@ int main (int argc, char * const * argv)
std::cout << boost::str (boost::format ("%1% blocks retrieved") % count) << std::endl;
}
node.node->block_processor.add (block);
if (block->type () == nano::block_type::state && block->previous ().is_zero () && source_node->ledger.is_epoch_link (block->link ()))
if (block->type () == nano::block_type::state && block->previous ().is_zero () && source_node->ledger.is_epoch_link (block->link ().value ()))
{
// Epoch open blocks can be rejected without processed pending blocks to account, push it later again
epoch_open_blocks.push_back (block);
Expand Down
3 changes: 2 additions & 1 deletion nano/node/blockprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@ nano::block_status nano::block_processor::process_one (store::write_transaction
}
case nano::block_status::gap_source:
{
node.unchecked.put (block->source_field ().value_or (block->link ().as_block_hash ()), block);
release_assert (block->source_field () || block->link ());
node.unchecked.put (block->source_field ().value_or (block->link ().value_or (0).as_block_hash ()), block);
node.stats.inc (nano::stat::type::ledger, nano::stat::detail::gap_source);
break;
}
Expand Down
4 changes: 2 additions & 2 deletions nano/node/bootstrap_ascending/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void nano::bootstrap_ascending::service::inspect (store::transaction const & tx,
destination = block.destination_field ().value ();
break;
case nano::block_type::state:
destination = block.link ().as_account ();
destination = block.link ().value ().as_account ();
break;
default:
debug_assert (false, "unexpected block type");
Expand All @@ -168,7 +168,7 @@ void nano::bootstrap_ascending::service::inspect (store::transaction const & tx,
case nano::block_status::gap_source:
{
const auto account = block.previous ().is_zero () ? block.account_field ().value () : ledger.account (tx, block.previous ()).value ();
const auto source = block.source_field ().value_or (block.link ().as_block_hash ());
const auto source = block.source_field ().value_or (block.link ().value_or (0).as_block_hash ());

// Mark account as blocked because it is missing the source block
accounts.block (account, source);
Expand Down
2 changes: 1 addition & 1 deletion nano/node/confirmation_height_bounded.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ bool nano::confirmation_height_bounded::iterate (store::read_transaction const &
// Once a receive is cemented, we can cement all blocks above it until the next receive, so store those details for later.
++num_blocks;
auto block = ledger.block (transaction_a, hash);
auto source = block->source_field ().value_or (block->link ().as_block_hash ());
auto source = block->source_field ().value_or (block->link ().value_or (0).as_block_hash ());
if (!source.is_zero () && !ledger.is_epoch_link (source) && ledger.block_exists (transaction_a, source))
{
hit_receive = true;
Expand Down
2 changes: 1 addition & 1 deletion nano/node/confirmation_height_unbounded.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ void nano::confirmation_height_unbounded::collect_unconfirmed_receive_and_source

if (block)
{
auto source = block->source_field ().value_or (block->link ().as_block_hash ());
auto source = block->source_field ().value_or (block->link ().value_or (0).as_block_hash ());
if (!source.is_zero () && !ledger.is_epoch_link (source) && ledger.block_exists (transaction_a, source))
{
if (!hit_receive && !block_callback_data_a.empty ())
Expand Down
6 changes: 3 additions & 3 deletions nano/node/ipc/flatbuffers_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ std::unique_ptr<nanoapi::BlockStateT> nano::ipc::flatbuffers_builder::from (nano
block->previous = block_a.previous ().to_string ();
block->representative = block_a.representative ().to_account ();
block->balance = block_a.balance ().to_string_dec ();
block->link = block_a.link ().to_string ();
block->link_as_account = block_a.link ().to_account ();
block->link = block_a.link ().value ().to_string ();
block->link_as_account = block_a.link ().value ().to_account ();
block_a.signature.encode_hex (block->signature);
block->work = nano::to_string_hex (block_a.work);

if (is_state_send_a)
{
block->subtype = nanoapi::BlockSubType::BlockSubType_send;
}
else if (block_a.link ().is_zero ())
else if (block_a.link ().value ().is_zero ())
{
block->subtype = nanoapi::BlockSubType::BlockSubType_change;
}
Expand Down
16 changes: 8 additions & 8 deletions nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,12 @@ uint64_t nano::json_handler::difficulty_ledger (nano::block const & block_a)
{
details.epoch = block_previous->sideband ().details.epoch;
}
auto link (block_a.link ());
if (!link.is_zero () && !details.is_send)
auto link = block_a.link ();
if (link && !details.is_send)
{
auto block_link = node.ledger.block (transaction, link.as_block_hash ());
auto block_link = node.ledger.block (transaction, link.value ().as_block_hash ());
auto account = block_a.account_field ().value (); // Link is non-zero therefore it's a state block and has an account field;
if (block_link != nullptr && node.store.pending.exists (transaction, nano::pending_key (account, link.as_block_hash ())))
if (block_link != nullptr && node.store.pending.exists (transaction, nano::pending_key (account, link.value ().as_block_hash ())))
{
details.epoch = std::max (details.epoch, block_link->sideband ().details.epoch);
details.is_receive = true;
Expand Down Expand Up @@ -1224,7 +1224,7 @@ void nano::json_handler::block_confirm ()
if (auto state = dynamic_cast<nano::state_block *> (block_l.get ()))
{
is_state_send = node.ledger.is_send (transaction, *state);
is_state_epoch = amount.value () == 0 && node.ledger.is_epoch_link (state->link ());
is_state_epoch = amount.value () == 0 && node.ledger.is_epoch_link (state->link ().value ());
}
}
node.observers.blocks.notify (status, {}, account, amount ? amount.value () : 0, is_state_send, is_state_epoch);
Expand Down Expand Up @@ -2522,7 +2522,7 @@ class history_visitor : public nano::block_visitor
if (raw && accounts_filter.empty ())
{
tree.put ("subtype", "epoch");
tree.put ("account", handler.node.ledger.epoch_signer (block_a.link ()).to_account ());
tree.put ("account", handler.node.ledger.epoch_signer (block_a.link ().value ()).to_account ());
}
}
else
Expand Down Expand Up @@ -3641,7 +3641,7 @@ void nano::json_handler::republish ()
block = node.ledger.block (transaction, hash);
if (sources != 0) // Republish source chain
{
nano::block_hash source = block->source_field ().value_or (block->link ().as_block_hash ());
nano::block_hash source = block->source_field ().value_or (block->link ().value_or (0).as_block_hash ());
auto block_a = node.ledger.block (transaction, source);
std::vector<nano::block_hash> hashes;
while (block_a != nullptr && hashes.size () < sources)
Expand Down Expand Up @@ -3679,7 +3679,7 @@ void nano::json_handler::republish ()
while (block_d != nullptr && hash != source)
{
hashes.push_back (previous);
source = block_d->source_field ().value_or (block_d->sideband ().details.is_send ? 0 : block_d->link ().as_block_hash ());
source = block_d->source_field ().value_or (block_d->sideband ().details.is_send ? 0 : block_d->link ().value_or (0).as_block_hash ());
previous = block_d->previous ();
block_d = node.ledger.block (transaction, previous);
}
Expand Down
6 changes: 3 additions & 3 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,13 @@ nano::node::node (boost::asio::io_context & io_ctx_a, std::filesystem::path cons
// Subtype field
else if (block_a->type () == nano::block_type::state)
{
if (block_a->link ().is_zero ())
if (block_a->link ().value ().is_zero ())
{
event.add ("subtype", "change");
}
else if (is_state_epoch_a)
{
debug_assert (amount_a == 0 && node_l->ledger.is_epoch_link (block_a->link ()));
debug_assert (amount_a == 0 && node_l->ledger.is_epoch_link (block_a->link ().value ()));
event.add ("subtype", "epoch");
}
else
Expand Down Expand Up @@ -1252,7 +1252,7 @@ void nano::node::process_confirmed_data (store::transaction const & transaction_
{
is_state_send_a = true;
}
if (amount_a == 0 && network_params.ledger.epochs.is_epoch_link (state->link ()))
if (amount_a == 0 && network_params.ledger.epochs.is_epoch_link (state->link ().value ()))
{
is_state_epoch_a = true;
}
Expand Down
4 changes: 2 additions & 2 deletions nano/node/websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1012,13 +1012,13 @@ nano::websocket_server::websocket_server (nano::websocket::config & config_a, na
}
else if (block_a->type () == nano::block_type::state)
{
if (block_a->link ().is_zero ())
if (block_a->link ().value ().is_zero ())
{
subtype = "change";
}
else if (is_state_epoch_a)
{
debug_assert (amount_a == 0 && ledger.is_epoch_link (block_a->link ()));
debug_assert (amount_a == 0 && ledger.is_epoch_link (block_a->link ().value ()));
subtype = "epoch";
}
else
Expand Down
Loading

0 comments on commit 64e753a

Please sign in to comment.