Skip to content

Commit

Permalink
Add tracking of unconfirmed blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
clemahieu committed Feb 20, 2024
1 parent eb6e20d commit 0960c62
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
1 change: 1 addition & 0 deletions nano/core_test/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()));
}
49 changes: 41 additions & 8 deletions nano/secure/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -1632,11 +1627,49 @@ void nano::ledger::confirm (nano::store::write_transaction const & transaction,
}
}

void nano::ledger::track (std::shared_ptr<nano::block> block)
void nano::ledger::track (nano::store::transaction const & transaction, std::shared_ptr<nano::block> 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<nano::account_info> 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)
Expand Down
2 changes: 1 addition & 1 deletion nano/secure/ledger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class ledger final
bool pruning{ false };

private:
void track (std::shared_ptr<nano::block> block);
void track (nano::store::transaction const & transaction, std::shared_ptr<nano::block> block);
void confirm (nano::store::write_transaction const & transaction, nano::block const & block);
void initialize (nano::generate_cache const &);
nano::unconfirmed_set unconfirmed;
Expand Down

0 comments on commit 0960c62

Please sign in to comment.