Skip to content

Conversation

amesgen
Copy link
Member

@amesgen amesgen commented Aug 4, 2025

This PR tries to isolate as much code as possible from the exact criterion for whether a block is immutable. Instead of directly using the Praos criterion ("a block is immutable iff it is buried under at least k blocks"), we instead rely on getCurrentChain to return the volatile suffix of our chain, with its anchor being the most recent immutable block.

This way, the upcoming implementation of Ouroboros Peras (#1594) will not require touching this code, despite modifying the criterion for immutability to be "a block is buried under at least k weight".

Concretely, this PR adapts the following places:

  • A trace message emitted during chain selection.
  • The ChainDB background logic for copying blocks to the ImmutableDB and garbage-collecting them from the VolatileDB.
  • The LedgerDB logic, namely acquiring Forkers and rolling back.
  • An overzealous assertion in the ChainSync client.

For now, without Peras being implemented, the actual logic doesn't change at all; we still always use Praos-based immutability.


Note that there are still millions of comments that implicitly consider "immutable" and "buried under k blocks" to be the same thing; it seems acceptable to defer adapting those.


Based on top of #1513 to avoid conflicts

Necessary for tweag/cardano-peras#71, preparation for #1594

@amesgen amesgen added the Peras label Aug 4, 2025
@amesgen amesgen force-pushed the amesgen/decouple-immutability branch from b0f3982 to cb5ff23 Compare August 4, 2025 13:48
@amesgen amesgen force-pushed the amesgen/ledgerdb-garbage-collect-states branch from d791dfb to 247c489 Compare August 8, 2025 14:13
@amesgen amesgen force-pushed the amesgen/decouple-immutability branch 4 times, most recently from 0ab3170 to de3bf4a Compare August 11, 2025 13:28
@amesgen amesgen moved this to 👀 In review in Consensus Team Backlog Aug 11, 2025
@amesgen amesgen marked this pull request as ready for review August 11, 2025 13:39
-- We watch the chain for changes. Whenever the chain is longer than @k@, then
-- the headers older than @k@ are copied from the VolatileDB to the ImmutableDB
-- (using 'copyToImmutableDB'). Once that is complete,
-- We watch the 'cdbChain' for changes. Whenever the chain is longer than
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the previous comment, it was clear when blocks were copied from the immutable to the volatile DB. However, I find the current condition a bit hard to understand. Isn't getCurrentChain always a suffix of "the chain"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about

-- We watch the 'cdbChain' for changes. Whenever it is longer than
-- 'getCurrentChain' (the volatile suffix of 'cdbChain'), then new blocks (a
-- prefix of 'cdbChain') became immutable, and are copied from the VolatileDB to
-- the ImmutableDB (using 'copyToImmutableDB').

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now:

-- Wait until the current chain ('cdbChain') is longer than its volatile suffix
-- ('getCurrentChain'). When this occurs, it indicates that new blocks have
-- become immutable. These newly immutable blocks are then copied are copied
-- from the VolatileDB to the ImmutableDB (using 'copyToImmutableDB').

@amesgen amesgen force-pushed the amesgen/decouple-immutability branch 2 times, most recently from 2988210 to 13b1c27 Compare August 12, 2025 14:20
@amesgen amesgen force-pushed the amesgen/ledgerdb-garbage-collect-states branch from 247c489 to 08bda65 Compare August 13, 2025 14:41
@amesgen amesgen force-pushed the amesgen/decouple-immutability branch from 13b1c27 to 63069cf Compare August 13, 2025 14:41
@amesgen amesgen force-pushed the amesgen/ledgerdb-garbage-collect-states branch from 08bda65 to dec284f Compare August 13, 2025 15:27
This is preparing for Peras, which will introduce a refined immutability
criterion. Due to this change, we defer the immutability criterion to the
implementation of `getCurrentChain`, reducing the impact of Peras.
This is a preparatory change for Peras, which uses a different immutability
criterion (based on weight of chains, instead of (just) length).

The only remaining use of the `SecurityParam` is for the snapshot policy 🙃
This requires some knot tying as we initialize the LedgerDB before the ChainDB
is fully initialized.

This is preparing for Peras, which will introduce a refined immutability
criterion. Due to this change, the LedgerDB will automatically use the same
criterion as the ChainDB (via `getCurrentChain`).
In case of data loss in the VolatileDB and a node restart, this condition can be
violated until we received `k` headers again.

Additionally, Ouroboros Peras will use a different immutability criterion where
it is perfectly fine to have less than `k` headers on our chain (as long as they
have sufficient weight).
@amesgen amesgen force-pushed the amesgen/decouple-immutability branch from 63069cf to 572b241 Compare August 13, 2025 15:28
-- (using 'copyToImmutableDB'). Once that is complete,
-- Wait until the current chain ('cdbChain') is longer than its volatile suffix
-- ('getCurrentChain'). When this occurs, it indicates that new blocks have
-- become immutable. These newly immutable blocks are then copied are copied
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove "are copied"

Base automatically changed from amesgen/ledgerdb-garbage-collect-states to main September 2, 2025 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: 👀 In review
Development

Successfully merging this pull request may close these issues.

3 participants