Skip to content

2127-Replace epoch table with maintained finalized + live current views#2130

Open
Cmdv wants to merge 3 commits into
masterfrom
2127-replace-epoch-table-with-views
Open

2127-Replace epoch table with maintained finalized + live current views#2130
Cmdv wants to merge 3 commits into
masterfrom
2127-replace-epoch-table-with-views

Conversation

@Cmdv
Copy link
Copy Markdown
Contributor

@Cmdv Cmdv commented May 25, 2026

Description

Drop the epoch table and its in-memory cache layer. The public epoch name is preserved as a UNION ALL view of epoch_finalized (table, appended per boundary, rolled back via DELETE WHERE no > $epoch) and epoch_current (live view of the unfinalised epoch).

A one-time backfill from block/tx runs on the first start after upgrade. Replaces the --disable-epoch CLI flag with the disable_epoch option in insert_options. Epoch true by default using disable_epoch will disable all epoch table related logic.

This fixes #2127

Checklist

  • Commit sequence broadly makes sense
  • Commits have useful messages
  • New tests are added if needed and existing tests are updated
  • Any changes are noted in the changelog
  • Code is formatted with fourmolu on version 0.17.0.0 (which can be run with scripts/fourmolize.sh)
  • Self-reviewed the diff

Migrations

  • The pr causes a breaking change of type a,b or c
  • If there is a breaking change, the pr includes a database migration and/or a fix process for old values, so that upgrade is possible
  • Resyncing and running the migrations provided will result in the same database semantically

If there is a breaking change, especially a big one, please add a justification here. Please elaborate
more what the migration achieves, what it cannot achieve or why a migration is not possible.

@Cmdv Cmdv requested a review from a team as a code owner May 25, 2026 23:18
@Cmdv Cmdv force-pushed the 2127-replace-epoch-table-with-views branch from 0001cda to 2de367d Compare May 25, 2026 23:53
@Cmdv Cmdv force-pushed the 2127-replace-epoch-table-with-views branch from 2de367d to 7f25aa6 Compare May 26, 2026 12:31
Copy link
Copy Markdown
Contributor

@kderme kderme left a comment

Choose a reason for hiding this comment

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

I added a test that shows a bug with rollbacks on epoch boundaries.

next_version int ;
BEGIN
SELECT stage_two + 1 INTO next_version FROM schema_version ;
IF next_version = 48 THEN
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

change to 49 after rebase

@@ -3,22 +3,25 @@
module Test.Cardano.Db.Mock.Unit.Conway.CommandLineArg.EpochDisabled (
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could rename to Test.Cardano.Db.Mock.Unit.Conway.Epoch

rollbackCache cache blockId
rollbackCache cache
-- The surviving block's epoch is no longer finalised; drop its (now stale) row so epoch_current takes over.
when (soptEpochViewEnabled (envOptions syncEnv)) $
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

put inside queryBlockNoAndEpoch

nBlocks <- lift $ DB.queryBlockCountAfterBlockNo (unBlockNo blkNo) True
mres <- lift $ DB.queryBlockNoAndEpoch (unBlockNo blkNo)
-- Use whenJust to silently skip if block not found
whenJust mres $ \(blockId, epochNo) -> do
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Add a test that this correctly rollbacks for blocks that are first or last of an epoch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace epoch table with views (hybrid: live + materialised, exposed via UNION)

2 participants