Skip to content

Commit

Permalink
ledger::source is moved to block::source_field and ledger::block_sour…
Browse files Browse the repository at this point in the history
…ce is moved to block::source which accesses the source field for open/receive/state blocks that are receives.
  • Loading branch information
clemahieu committed Mar 7, 2024
1 parent b22e5c4 commit 9648d4d
Show file tree
Hide file tree
Showing 13 changed files with 57 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 @@ -657,7 +657,7 @@ TEST (block_builder, state)
.link_hex ("E16DD58C1EFA8B521545B0A74375AA994D9FC43828A4266D75ECF57F07A7EE86")
.build (ec);
ASSERT_EQ (block->hash ().to_string (), "2D243F8F92CDD0AD94A1D456A6B15F3BE7A6FCBD98D4C5831D06D15C818CD81F");
ASSERT_FALSE (block->source ());
ASSERT_FALSE (block->source_field ());
ASSERT_FALSE (block->destination_field ());
ASSERT_EQ (block->link ().to_string (), "E16DD58C1EFA8B521545B0A74375AA994D9FC43828A4266D75ECF57F07A7EE86");
}
Expand Down Expand Up @@ -730,7 +730,7 @@ TEST (block_builder, open)
.source_hex ("E89208DD038FBB269987689621D52292AE9C35941A7484756ECCED92A65093BA")
.build (ec);
ASSERT_EQ (block->hash ().to_string (), "991CF190094C00F0B68E2E5F75F6BEE95A2E0BD93CEAA4A6734DB9F19B728948");
ASSERT_EQ (block->source ().value ().to_string (), "E89208DD038FBB269987689621D52292AE9C35941A7484756ECCED92A65093BA");
ASSERT_EQ (block->source_field ().value ().to_string (), "E89208DD038FBB269987689621D52292AE9C35941A7484756ECCED92A65093BA");
ASSERT_FALSE (block->destination_field ());
ASSERT_TRUE (block->link ().is_zero ());
}
Expand Down Expand Up @@ -769,7 +769,7 @@ TEST (block_builder, change)
.previous_hex ("088EE46429CA936F76C4EAA20B97F6D33E5D872971433EE0C1311BCB98764456")
.build (ec);
ASSERT_EQ (block->hash ().to_string (), "13552AC3928E93B5C6C215F61879358E248D4A5246B8B3D1EEC5A566EDCEE077");
ASSERT_FALSE (block->source ());
ASSERT_FALSE (block->source_field ());
ASSERT_FALSE (block->destination_field ());
ASSERT_TRUE (block->link ().is_zero ());
}
Expand Down Expand Up @@ -808,7 +808,7 @@ TEST (block_builder, send)
.balance_hex ("00F035A9C7D818E7C34148C524FFFFEE")
.build (ec);
ASSERT_EQ (block->hash ().to_string (), "4560E7B1F3735D082700CFC2852F5D1F378F7418FD24CEF1AD45AB69316F15CD");
ASSERT_FALSE (block->source ());
ASSERT_FALSE (block->source_field ());
ASSERT_EQ (block->destination_field ().value ().to_account (), "nano_1gys8r4crpxhp94n4uho5cshaho81na6454qni5gu9n53gksoyy1wcd4udyb");
ASSERT_TRUE (block->link ().is_zero ());
}
Expand Down Expand Up @@ -870,7 +870,7 @@ TEST (block_builder, receive)
.source_hex ("7B2B0A29C1B235FDF9B4DEF2984BB3573BD1A52D28246396FBB3E4C5FE662135")
.build (ec);
ASSERT_EQ (block->hash ().to_string (), "6C004BF911D9CF2ED75CF6EC45E795122AD5D093FF5A83EDFBA43EC4A3EDC722");
ASSERT_EQ (block->source ().value ().to_string (), "7B2B0A29C1B235FDF9B4DEF2984BB3573BD1A52D28246396FBB3E4C5FE662135");
ASSERT_EQ (block->source_field ().value ().to_string (), "7B2B0A29C1B235FDF9B4DEF2984BB3573BD1A52D28246396FBB3E4C5FE662135");
ASSERT_FALSE (block->destination_field ());
ASSERT_TRUE (block->link ().is_zero ());
}
18 changes: 9 additions & 9 deletions nano/core_test/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2329,17 +2329,17 @@ TEST (ledger, block_destination_source)
ASSERT_EQ (nano::block_status::progress, ledger.process (transaction, block6));
ASSERT_EQ (balance, ledger.balance (transaction, block6->hash ()));
ASSERT_EQ (dest.pub, ledger.block_destination (transaction, *block1));
ASSERT_TRUE (ledger.block_source (transaction, *block1).is_zero ());
ASSERT_FALSE (block1->source_field ());
ASSERT_EQ (nano::dev::genesis_key.pub, ledger.block_destination (transaction, *block2));
ASSERT_TRUE (ledger.block_source (transaction, *block2).is_zero ());
ASSERT_FALSE (block2->source_field ());
ASSERT_EQ (ledger.block_destination (transaction, *block3), nullptr);
ASSERT_EQ (block2->hash (), ledger.block_source (transaction, *block3));
ASSERT_EQ (block2->hash (), block3->source ());
ASSERT_EQ (dest.pub, ledger.block_destination (transaction, *block4));
ASSERT_TRUE (ledger.block_source (transaction, *block4).is_zero ());
ASSERT_FALSE (block4->source_field ());
ASSERT_EQ (nano::dev::genesis_key.pub, ledger.block_destination (transaction, *block5));
ASSERT_TRUE (ledger.block_source (transaction, *block5).is_zero ());
ASSERT_FALSE (block5->source_field ());
ASSERT_EQ (ledger.block_destination (transaction, *block6), nullptr);
ASSERT_EQ (block5->hash (), ledger.block_source (transaction, *block6));
ASSERT_EQ (block5->hash (), block6->source ());
}

TEST (ledger, state_account)
Expand Down Expand Up @@ -4344,7 +4344,7 @@ TEST (ledger, unchecked_open)
// Waits for the last blocks to pass through block_processor and unchecked.put queues
ASSERT_TIMELY_EQ (10s, 1, node1.unchecked.count ());
// Get the next peer for attempting a tcp bootstrap connection
auto blocks = node1.unchecked.get (open1->source ().value ());
auto blocks = node1.unchecked.get (open1->source_field ().value ());
ASSERT_EQ (blocks.size (), 1);
}
node1.block_processor.add (send1);
Expand Down Expand Up @@ -4412,11 +4412,11 @@ TEST (ledger, unchecked_receive)
}
// Waits for the open1 block to pass through block_processor and unchecked.put queues
node1.block_processor.add (open1);
ASSERT_TIMELY (15s, check_block_is_listed (node1.store.tx_begin_read (), receive1->source ().value ()));
ASSERT_TIMELY (15s, check_block_is_listed (node1.store.tx_begin_read (), receive1->source_field ().value ()));
// Previous block for receive1 is known, signature was validated
{
auto transaction = node1.store.tx_begin_read ();
auto blocks (node1.unchecked.get (receive1->source ().value ()));
auto blocks (node1.unchecked.get (receive1->source_field ().value ()));
ASSERT_EQ (blocks.size (), 1);
}
node1.block_processor.add (send2);
Expand Down
22 changes: 19 additions & 3 deletions nano/lib/blocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ nano::account const & nano::block::representative () const
return representative;
}

std::optional<nano::block_hash> nano::block::source () const
std::optional<nano::block_hash> nano::block::source_field () const
{
return std::nullopt;
}
Expand Down Expand Up @@ -232,6 +232,22 @@ nano::account nano::block::destination () const
}
}

nano::block_hash nano::block::source ()
{
release_assert (has_sideband ());
switch (type ())
{
case nano::block_type::open:
case nano::block_type::receive:
return source_field ().value ();
case nano::block_type::state:
release_assert (sideband ().details.is_receive);
return link ().as_block_hash ();
default:
release_assert (false);
}
}

std::optional<nano::account> nano::block::account_field () const
{
return std::nullopt;
Expand Down Expand Up @@ -808,7 +824,7 @@ bool nano::open_block::valid_predecessor (nano::block const & block_a) const
return false;
}

std::optional<nano::block_hash> nano::open_block::source () const
std::optional<nano::block_hash> nano::open_block::source_field () const
{
return hashables.source;
}
Expand Down Expand Up @@ -1745,7 +1761,7 @@ nano::block_hash const & nano::receive_block::previous () const
return hashables.previous;
}

std::optional<nano::block_hash> nano::receive_block::source () const
std::optional<nano::block_hash> nano::receive_block::source_field () const
{
return hashables.source;
}
Expand Down
8 changes: 5 additions & 3 deletions nano/lib/blocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ class block
nano::account destination () const;
// Destination account for send blocks
virtual std::optional<nano::account> destination_field () const;
// Returns the source block hash for open/receive/state blocks that are receives
nano::block_hash source ();
// Source block for open/receive blocks
virtual std::optional<nano::block_hash> source () const;
virtual std::optional<nano::block_hash> source_field () const;

protected:
mutable nano::block_hash cached_hash{ 0 };
Expand Down Expand Up @@ -190,7 +192,7 @@ class receive_block : public nano::block
static std::size_t constexpr size = nano::receive_hashables::size + sizeof (signature) + sizeof (work);

public: // Receive block fields
std::optional<nano::block_hash> source () const override;
std::optional<nano::block_hash> source_field () const override;

public: // Logging
void operator() (nano::object_stream &) const override;
Expand Down Expand Up @@ -246,7 +248,7 @@ class open_block : public nano::block

public: // Open block fields
std::optional<nano::account> account_field () const override;
std::optional<nano::block_hash> source () const override;
std::optional<nano::block_hash> source_field () const override;

public: // Logging
void operator() (nano::object_stream &) const override;
Expand Down
2 changes: 1 addition & 1 deletion nano/node/blockprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ nano::block_status nano::block_processor::process_one (store::write_transaction
}
case nano::block_status::gap_source:
{
node.unchecked.put (node.ledger.block_source (transaction_a, *block), block);
node.unchecked.put (block->source_field ().value_or (block->link ().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/bootstrap_lazy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ bool nano::bootstrap_attempt_lazy::process_block_lazy (std::shared_ptr<nano::blo
if (!lazy_blocks_processed (hash))
{
// Search for new dependencies
if (block_a->source () && !node->ledger.block_or_pruned_exists (block_a->source ().value ()) && block_a->source ().value () != node->network_params.ledger.genesis->account ())
if (block_a->source_field () && !node->ledger.block_or_pruned_exists (block_a->source_field ().value ()) && block_a->source_field ().value () != node->network_params.ledger.genesis->account ())
{
lazy_add (block_a->source ().value (), retry_limit);
lazy_add (block_a->source_field ().value (), retry_limit);
}
else if (block_a->type () == nano::block_type::state)
{
Expand Down
2 changes: 1 addition & 1 deletion nano/node/bootstrap_ascending/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,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 () ? block.link ().as_block_hash () : block.source ().value ();
const auto source = block.source_field ().value_or (block.link ().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 ().value_or (block->link ().as_block_hash ());
auto source = block->source_field ().value_or (block->link ().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 ().value_or (block->link ().as_block_hash ());
auto source = block->source_field ().value_or (block->link ().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
4 changes: 2 additions & 2 deletions nano/node/ipc/flatbuffers_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ std::unique_ptr<nanoapi::BlockReceiveT> nano::ipc::flatbuffers_builder::from (na
{
auto block (std::make_unique<nanoapi::BlockReceiveT> ());
block->hash = block_a.hash ().to_string ();
block->source = block_a.source ().value ().to_string ();
block->source = block_a.source_field ().value ().to_string ();
block->previous = block_a.previous ().to_string ();
block_a.signature.encode_hex (block->signature);
block->work = nano::to_string_hex (block_a.work);
Expand All @@ -62,7 +62,7 @@ std::unique_ptr<nanoapi::BlockOpenT> nano::ipc::flatbuffers_builder::from (nano:
{
auto block (std::make_unique<nanoapi::BlockOpenT> ());
block->hash = block_a.hash ().to_string ();
block->source = block_a.source ().value ().to_string ();
block->source = block_a.source_field ().value ().to_string ();
block->account = block_a.account ().to_account ();
block->representative = block_a.representative ().to_account ();
block_a.signature.encode_hex (block->signature);
Expand Down
14 changes: 7 additions & 7 deletions nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1383,15 +1383,15 @@ void nano::json_handler::blocks_info ()
}
if (source)
{
nano::block_hash source_hash (node.ledger.block_source (transaction, *block));
auto block_a = node.ledger.block (transaction, source_hash);
if (block_a != nullptr)
if (!block->sideband ().details.is_receive || !node.ledger.block_exists (transaction, block->source ()))
{
entry.put ("source_account", block_a->account ().to_account ());
entry.put ("source_account", "0");
}
else
{
entry.put ("source_account", "0");
auto block_a = node.ledger.block (transaction, block->source ());
release_assert (block_a);
entry.put ("source_account", block_a->account ().to_account ());
}
}
blocks.push_back (std::make_pair (hash_text, entry));
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 (node.ledger.block_source (transaction, *block));
nano::block_hash source = block->source_field ().value_or (block->link ().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 = node.ledger.block_source (transaction, *block_d);
source = block_d->source_field ().value_or (block_d->sideband ().details.is_send ? 0 : block_d->link ().as_block_hash ());
previous = block_d->previous ();
block_d = node.ledger.block (transaction, previous);
}
Expand Down
43 changes: 4 additions & 39 deletions nano/secure/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -998,26 +998,6 @@ nano::account const & nano::ledger::block_destination (store::transaction const
return nano::account::null ();
}

nano::block_hash nano::ledger::block_source (store::transaction const & transaction_a, nano::block const & block_a)
{
/*
* block_source() requires that the previous block of the block
* passed in exist in the database. This is because it will try
* to check account balances to determine if it is a send block.
*/
debug_assert (block_a.previous ().is_zero () || block_exists (transaction_a, block_a.previous ()));

// If block_a.source () is nonzero, then we have our source.
// However, universal blocks will always return zero.
nano::block_hash result = block_a.source ().value_or (0);
nano::state_block const * state_block (dynamic_cast<nano::state_block const *> (&block_a));
if (state_block != nullptr && !is_send (transaction_a, *state_block))
{
result = state_block->hashables.link.as_block_hash ();
}
return result;
}

std::pair<nano::block_hash, nano::block_hash> nano::ledger::hash_root_random (store::transaction const & transaction_a) const
{
nano::block_hash hash (0);
Expand Down Expand Up @@ -1210,13 +1190,13 @@ class dependent_block_visitor : public nano::block_visitor
void receive_block (nano::receive_block const & block_a) override
{
result[0] = block_a.previous ();
result[1] = block_a.source ().value ();
result[1] = block_a.source_field ().value ();
}
void open_block (nano::open_block const & block_a) override
{
if (block_a.source () != ledger.constants.genesis->account ())
if (block_a.source_field ().value () != ledger.constants.genesis->account ())
{
result[0] = block_a.source ().value ();
result[0] = block_a.source_field ().value ();
}
}
void change_block (nano::change_block const & block_a) override
Expand Down Expand Up @@ -1265,22 +1245,7 @@ std::shared_ptr<nano::block> nano::ledger::find_receive_block_by_send_hash (stor
// walk down the chain until the source field of a receive block matches the send block hash
while (possible_receive_block != nullptr)
{
// if source is non-zero then it is a legacy receive or open block
nano::block_hash source = possible_receive_block->source ().value_or (0);

// if source is zero then it could be a state block, which needs a different kind of access
auto state_block = dynamic_cast<nano::state_block const *> (possible_receive_block.get ());
if (state_block != nullptr)
{
// we read the block from the database, so we expect it to have sideband
debug_assert (state_block->has_sideband ());
if (state_block->sideband ().details.is_receive)
{
source = state_block->hashables.link.as_block_hash ();
}
}

if (send_block_hash == source)
if (possible_receive_block->sideband ().details.is_receive && send_block_hash == possible_receive_block->source ())
{
// we have a match
result = possible_receive_block;
Expand Down
1 change: 0 additions & 1 deletion nano/secure/ledger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class ledger final
std::string block_text (nano::block_hash const &);
bool is_send (store::transaction const &, nano::block const &) const;
nano::account const & block_destination (store::transaction const &, nano::block const &);
nano::block_hash block_source (store::transaction const &, nano::block const &);
std::pair<nano::block_hash, nano::block_hash> hash_root_random (store::transaction const &) const;
std::optional<nano::pending_info> pending_info (store::transaction const & transaction, nano::pending_key const & key) const;
nano::block_status process (store::write_transaction const & transaction, std::shared_ptr<nano::block> block);
Expand Down

0 comments on commit 9648d4d

Please sign in to comment.