From 0960c62a85aff789ae389124bbeeefc4c45c474c Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Mon, 19 Feb 2024 20:44:13 +0000 Subject: [PATCH] Add tracking of unconfirmed blocks. --- nano/core_test/ledger.cpp | 1 + nano/secure/ledger.cpp | 49 ++++++++++++++++++++++++++++++++------- nano/secure/ledger.hpp | 2 +- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/nano/core_test/ledger.cpp b/nano/core_test/ledger.cpp index f894397fbc..c315979f4a 100644 --- a/nano/core_test/ledger.cpp +++ b/nano/core_test/ledger.cpp @@ -5729,4 +5729,5 @@ TEST (ledger, confirm_one) .build (); ASSERT_EQ (nano::ledger_code::progress, ledger.process (ctx.store ().tx_begin_write (), send)); ledger.confirm (ctx.store ().tx_begin_write (), send->hash ()); + ASSERT_TRUE (ctx.store ().block.exists (ctx.store ().tx_begin_read (), send->hash ())); } diff --git a/nano/secure/ledger.cpp b/nano/secure/ledger.cpp index 95843b09df..1154454831 100644 --- a/nano/secure/ledger.cpp +++ b/nano/secure/ledger.cpp @@ -832,12 +832,7 @@ nano::ledger_code nano::ledger::process (store::write_transaction const & transa auto code = ctx.check (); if (code == nano::ledger_code::progress) { - debug_assert (block_a->has_sideband ()); - ledger_processor processor (*this, transaction_a); - block_a->visit (processor); - debug_assert (processor.result == nano::ledger_code::progress); - ++cache.block_count; - return processor.result; + track (transaction_a, block_a); } return code; } @@ -1632,11 +1627,49 @@ void nano::ledger::confirm (nano::store::write_transaction const & transaction, } } -void nano::ledger::track (std::shared_ptr block) +void nano::ledger::track (nano::store::transaction const & transaction, std::shared_ptr block) { + debug_assert (block); debug_assert (block->has_sideband ()); debug_assert (unconfirmed.block.count (block->hash ()) == 0); - unconfirmed.block.emplace (block->hash (), block); + auto hash = block->hash (); + unconfirmed.block.emplace (hash, block); + auto account_l = account (*block); + std::optional info = unconfirmed.account.count (account_l) == 1 ? unconfirmed.account[account_l] : store.account.get (transaction, account_l); + auto representative = [&info, &block] () { + switch (block->type ()) + { + case nano::block_type::state: + case nano::block_type::open: + case nano::block_type::change: + return block->representative (); + case nano::block_type::send: + case nano::block_type::receive: + debug_assert (info.has_value ()); + return info->representative; + default: + release_assert (false); + } + }; + auto open = [&info, &hash] () { + return info.has_value () ? info->open_block : hash; + }; + unconfirmed.account[account_l] = nano::account_info{ hash, representative (), open (), balance (*block), nano::seconds_since_epoch (), block->sideband ().height, block->sideband ().details.epoch }; + if (block->sideband ().details.is_send) + { + auto destination_l = destination (*block); + debug_assert (destination_l.has_value ()); + nano::pending_key key{ *destination_l, hash }; + nano::pending_info info{ account_l, block->sideband ().amount, block->sideband ().details.epoch }; + debug_assert (unconfirmed.receivable.count (key) == 0); + unconfirmed.receivable.emplace (key, info); + } + else if (block->sideband ().details.is_receive) + { + nano::pending_key key{ account_l, hash }; + debug_assert (unconfirmed.received.count (key) == 0); + unconfirmed.received.emplace (key); + } } void nano::ledger::confirm (nano::store::write_transaction const & transaction, nano::block const & block) diff --git a/nano/secure/ledger.hpp b/nano/secure/ledger.hpp index 7488215390..1038781fe1 100644 --- a/nano/secure/ledger.hpp +++ b/nano/secure/ledger.hpp @@ -109,7 +109,7 @@ class ledger final bool pruning{ false }; private: - void track (std::shared_ptr block); + void track (nano::store::transaction const & transaction, std::shared_ptr block); void confirm (nano::store::write_transaction const & transaction, nano::block const & block); void initialize (nano::generate_cache const &); nano::unconfirmed_set unconfirmed;