Skip to content

Commit

Permalink
core,params: Don't rewind chain on pre-genesis time changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianst committed Mar 4, 2024
1 parent 0402d54 commit 1c88f2c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
13 changes: 9 additions & 4 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,16 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis

// Rewind the chain in case of an incompatible config upgrade.
if compat, ok := genesisErr.(*params.ConfigCompatError); ok {
log.Warn("Rewinding chain to upgrade configuration", "err", compat)
if compat.RewindToTime > 0 {
bc.SetHeadWithTimestamp(compat.RewindToTime)
if compat.NewTime != nil && compat.StoredTime != nil &&
genesis.Timestamp > *compat.NewTime && genesis.Timestamp > *compat.StoredTime {
log.Warn("Ignoring chain rewind because of pre-genesis timestamp changes", "err", compat)
} else {
bc.SetHead(compat.RewindToBlock)
log.Warn("Rewinding chain to upgrade configuration", "err", compat)
if compat.NewTime != nil {
bc.SetHeadWithTimestamp(compat.RewindToTime)
} else {
bc.SetHead(compat.RewindToBlock)
}
}
rawdb.WriteChainConfig(db, genesisHash, chainConfig)
}
Expand Down
3 changes: 2 additions & 1 deletion params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,8 @@ func newTimestampCompatError(what string, storedtime, newtime *uint64) *ConfigCo
NewTime: newtime,
RewindToTime: 0,
}
if rew != nil {
if rew != nil && *rew > 0 {
// if *rew == 0 this would cause an underflow
err.RewindToTime = *rew - 1
}
return err
Expand Down

0 comments on commit 1c88f2c

Please sign in to comment.