Skip to content

Commit 4304438

Browse files
committed
fix(core/genesis): more fixes
- Read stored config using a local copy of extras to avoid data races. - Still write using the attached extras to persist upgrade bytes. - Re-read with a fresh copy for compatibility comparison.
1 parent 1d2a414 commit 4304438

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

core/genesis.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,32 +182,34 @@ func SetupGenesisBlock(
182182
return newcfg, common.Hash{}, err
183183
}
184184

185-
// Read stored config. We'll persist the new config (including upgrade bytes)
186-
// prior to compatibility checks to ensure on-disk state is up to date.
187-
extra := params.GetExtra(newcfg)
188-
storedcfg := customrawdb.ReadChainConfig(db, stored, extra)
185+
// Read stored config into a local extras copy to avoid mutating the
186+
// caller's attached extras (which may be concurrently accessed in tests).
187+
// We'll persist the new config (including upgrade bytes) using the attached
188+
// extras below to ensure on-disk state is up to date.
189+
readExtra := *params.GetExtra(newcfg)
190+
storedCfg := customrawdb.ReadChainConfig(db, stored, &readExtra)
189191
// If there is no previously stored chain config, write the chain config to disk.
190-
if storedcfg == nil {
192+
if storedCfg == nil {
191193
// Note: this can happen since we did not previously write the genesis block and chain config in the same batch.
192194
log.Warn("Found genesis block without chain config")
193-
customrawdb.WriteChainConfig(db, stored, newcfg, *extra)
195+
customrawdb.WriteChainConfig(db, stored, newcfg, *params.GetExtra(newcfg))
194196
return newcfg, stored, nil
195197
}
196198

197199
// Persist the new chain config (and upgrade bytes) to disk now to avoid
198200
// spurious compatibility failures due to missing upgrade bytes on older databases.
199-
customrawdb.WriteChainConfig(db, stored, newcfg, *extra)
201+
customrawdb.WriteChainConfig(db, stored, newcfg, *params.GetExtra(newcfg))
200202

201203
// Re-read stored config into a fresh extras copy for a clean comparison.
202-
storedExtra := *params.GetExtra(newcfg)
203-
storedcfg = customrawdb.ReadChainConfig(db, stored, &storedExtra)
204+
compareExtra := *params.GetExtra(newcfg)
205+
storedCfg = customrawdb.ReadChainConfig(db, stored, &compareExtra)
204206

205207
// Notes on the following line:
206208
// - this is needed in coreth to handle the case where existing nodes do not
207209
// have the Berlin or London forks initialized by block number on disk.
208210
// See https://github.com/ava-labs/coreth/pull/667/files
209211
// - this is not needed in subnet-evm but it does not impact it either
210-
if err := params.SetEthUpgrades(storedcfg); err != nil {
212+
if err := params.SetEthUpgrades(storedCfg); err != nil {
211213
return genesis.Config, common.Hash{}, err
212214
}
213215
// Check config compatibility and write the config. Compatibility errors
@@ -227,9 +229,9 @@ func SetupGenesisBlock(
227229
if skipChainConfigCheckCompatible {
228230
log.Info("skipping verifying activated network upgrades on chain config")
229231
} else {
230-
compatErr := storedcfg.CheckCompatible(newcfg, height, timestamp)
232+
compatErr := storedCfg.CheckCompatible(newcfg, height, timestamp)
231233
if compatErr != nil && ((height != 0 && compatErr.RewindToBlock != 0) || (timestamp != 0 && compatErr.RewindToTime != 0)) {
232-
storedData, _ := params.ToWithUpgradesJSON(storedcfg).MarshalJSON()
234+
storedData, _ := params.ToWithUpgradesJSON(storedCfg).MarshalJSON()
233235
newData, _ := params.ToWithUpgradesJSON(newcfg).MarshalJSON()
234236
log.Error("found mismatch between config on database vs. new config", "storedConfig", string(storedData), "newConfig", string(newData), "err", compatErr)
235237
return newcfg, stored, compatErr

0 commit comments

Comments
 (0)