Skip to content

fix: allow snapshots reset on upgraded datadirs by restricting table config#22281

Merged
AskAlexSharov merged 2 commits into
erigontech:mainfrom
Sahil-4555:fix/snapshots-reset-old-datadir
Jul 7, 2026
Merged

fix: allow snapshots reset on upgraded datadirs by restricting table config#22281
AskAlexSharov merged 2 commits into
erigontech:mainfrom
Sahil-4555:fix/snapshots-reset-old-datadir

Conversation

@Sahil-4555

Copy link
Copy Markdown
Collaborator

Issue

Basically, when upgrading Erigon (for example, from v3.4.1 to v3.5.0), we introduce new tables to the schema. The getChainNameFromChainData helper is called by the snapshots reset command to automatically read the chain name from the database. It opens the database with Accede(true) and Readonly(true) options.

But since it was using the default ChainDB configuration, it tried to validate the existence of all ~138 tables registered in the schema. Because migrations have not run yet on the upgraded datadir, newly added tables (like BuilderPendingPayments or AccountHistoryKeys) are physically missing. In Accede mode, MDBX cannot create missing tables, so the database open fails and the reset command crashes with a misleading error about the chain flag not being set.

Fix

To fix this, we need to decouple this simple metadata lookup from the rest of the database schema. The helper only needs to read the genesis canonical hash and the chain config JSON.

We have modified the database open call in getChainNameFromChainData to use WithTableCfg. This allows us to pass a minimal table configuration containing only the HeaderCanonical and ConfigTable tables. Now, it only checks for these two metadata tables and safely ignores any other execution-layer tables.

Closes: #22275

@AskAlexSharov

Copy link
Copy Markdown
Collaborator

ReadOnly: i think we don’t need it. Non-ReadOnly allow mdbx to auto-recover from “safe_no_sync power-off” state. i think we must not use ReadOnly anywhere in Erigon tooling.

Accede: we need because we don’t know all that cli flags to create db (geometry and pageSize).

WithConfig - nice trick.

But if we don’t use ReadOnly - then maybe we can run_migrations on db to create tables. But it’s big question - which tool is lower level “migrations” or “seg reset” (what depends on what).

Also 1 long-standing issue: Capli using separated DB but same schema with ChainDB - i thin we need split Caplin schema in future.
(And drop some Erigon2 legacy tables - which using in couple unit-tests only. And don’t crate new tables for every new feature :-) )

Comment thread cmd/utils/app/reset-datadir.go Outdated
@AskAlexSharov AskAlexSharov enabled auto-merge July 7, 2026 05:43
@AskAlexSharov

Copy link
Copy Markdown
Collaborator

Also “read chaindata vs pass —chain flag” is very old chiken-egg problem - i think we must have good tooling for this problem.

@AskAlexSharov

Copy link
Copy Markdown
Collaborator

Drop legacy Table from schema - also old problem which we didn’t invest time into. It-theory it must be doable - in practice likely various erigon versions will return error - if some table will not exist (or maybe it will be not real problem if we will remove ReadOnly mode usage).

Also Acceede allows us to start RPCD with Erigon in-parallel and don’t scare of race-risk (but it’a alao ephomeral risk - mecause mdbx support cross-process guarantee of 1-rwtx-at-a-time a means no race, no conflicts)

auto-merge was automatically disabled July 7, 2026 05:54

Head branch was pushed to by a user without write access

@Sahil-4555

Sahil-4555 commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks so much for the detailed review @AskAlexSharov.

So ReadOnly - yes you are correct, I have dropped .Readonly(true) now. MDBX can do its auto-recovery correctly without it, if the DB wasn't shutdown cleanly (like after power cut or crash). And this is also in line with how dbCfg in snapshots_cmd.go opens the DB - just Accede(true) without Readonly.

For Accede - agreed, we need it because we simply don't have the geometry, page size and map size configuration available here to build the DB from scratch. One interesting thing I noticed going through CreateTable in kv_mdbx.go - both ReadOnly and Accede independently suppress the mdbx.Create flag. Even after removing the ReadOnly, Accede alone will still prevent the automatic creation of missing tables. For auto-creation we'd need normal RW mode (like node.OpenDatabase) but that seems risky for a maintenance tool like snapshots reset.

Regarding the named config - done, I extracted it to ChaindataSchemaForGenesis as you suggested and updated the tests to use the same as well.

For the migrations question I think snapshots reset should stay a lower level recovery tool. That's the whole reason why someone runs it is because their snapshots are broken or mismatched. If we try to run migrations inside it we might end up in a bad state mutating the DB. WithTableCfg(ChaindataSchemaForGenesis) keeps it clean, we just read the genesis hash and chain config, nothing more.

And yes, To open the DB we need the schema but to run migrations we need the chain name first. ChaindataSchemaForGenesis (which is used by the genesis hash and config) gives us a minimal profile with meta-data only that is valid across all chain versions.

For legacy tables - actually the infrastructure is already in place. In tables.go, ChaindataDeprecatedTables marks old E2 tables as deprecated and openDBIs ignores them. But completely removing them from old databases would require careful ordering of migration across versions, so that is a separate effort.

For RPCD and Accede - yes, mostly it is about avoiding geometry mismatches. Race conditions are not really an issue with MDBX since it has native handling of the cross-process 1-RW-tx guarantee.

My feeling is that we could generalise ChaindataSchemaForGenesis so that other tools, like the downloader's checkChainName or snapshot subcommands, could also use it for chain detection without crashing on unmigrated databases.

@AskAlexSharov AskAlexSharov enabled auto-merge July 7, 2026 06:43
@AskAlexSharov

Copy link
Copy Markdown
Collaborator

will need cherry-pick this PR to release/3.5 branch

@AskAlexSharov AskAlexSharov added this pull request to the merge queue Jul 7, 2026
Merged via the queue into erigontech:main with commit 15ac970 Jul 7, 2026
90 checks passed
@Sahil-4555 Sahil-4555 deleted the fix/snapshots-reset-old-datadir branch July 7, 2026 11:10
AskAlexSharov pushed a commit that referenced this pull request Jul 8, 2026
…estricting table config (#22291)

cherry pick of #22281 in
`release/3.5`
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.

snapshot reset not seeing chain name

2 participants