allow rewinding to block 0

This commit is contained in:
zhiqiangxu 2024-05-29 13:56:05 +08:00
parent cc22e0cdf0
commit 7b10aa443b
4 changed files with 10 additions and 1 deletions

View file

@ -354,7 +354,7 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, triedb *triedb.Database, g
return newcfg, stored, errors.New("missing head header") return newcfg, stored, errors.New("missing head header")
} }
compatErr := storedcfg.CheckCompatible(newcfg, head.Number.Uint64(), head.Time) compatErr := storedcfg.CheckCompatible(newcfg, head.Number.Uint64(), head.Time)
if compatErr != nil && ((head.Number.Uint64() != 0 && compatErr.RewindToBlock != 0) || (head.Time != 0 && compatErr.RewindToTime != 0)) { if compatErr != nil && ((head.Number.Uint64() != 0 && compatErr.RewindByBlock) || (head.Time != 0 && compatErr.RewindToTime != 0)) {
return newcfg, stored, compatErr return newcfg, stored, compatErr
} }
// Don't overwrite if the old is identical to the new // Don't overwrite if the old is identical to the new

View file

@ -148,6 +148,7 @@ func testSetupGenesis(t *testing.T, scheme string) {
What: "Homestead fork block", What: "Homestead fork block",
StoredBlock: big.NewInt(2), StoredBlock: big.NewInt(2),
NewBlock: big.NewInt(3), NewBlock: big.NewInt(3),
RewindByBlock: true,
RewindToBlock: 1, RewindToBlock: 1,
}, },
}, },

View file

@ -843,6 +843,9 @@ type ConfigCompatError struct {
// the block number to which the local chain must be rewound to correct the error // the block number to which the local chain must be rewound to correct the error
RewindToBlock uint64 RewindToBlock uint64
// the flag to tell whether it's rewinding by block or time
RewindByBlock bool
// the timestamp to which the local chain must be rewound to correct the error // the timestamp to which the local chain must be rewound to correct the error
RewindToTime uint64 RewindToTime uint64
} }
@ -862,6 +865,7 @@ func newBlockCompatError(what string, storedblock, newblock *big.Int) *ConfigCom
StoredBlock: storedblock, StoredBlock: storedblock,
NewBlock: newblock, NewBlock: newblock,
RewindToBlock: 0, RewindToBlock: 0,
RewindByBlock: true,
} }
if rew != nil && rew.Sign() > 0 { if rew != nil && rew.Sign() > 0 {
err.RewindToBlock = rew.Uint64() - 1 err.RewindToBlock = rew.Uint64() - 1

View file

@ -52,6 +52,7 @@ func TestCheckCompatible(t *testing.T) {
StoredBlock: big.NewInt(0), StoredBlock: big.NewInt(0),
NewBlock: nil, NewBlock: nil,
RewindToBlock: 0, RewindToBlock: 0,
RewindByBlock: true,
}, },
}, },
{ {
@ -63,6 +64,7 @@ func TestCheckCompatible(t *testing.T) {
StoredBlock: big.NewInt(0), StoredBlock: big.NewInt(0),
NewBlock: big.NewInt(1), NewBlock: big.NewInt(1),
RewindToBlock: 0, RewindToBlock: 0,
RewindByBlock: true,
}, },
}, },
{ {
@ -74,6 +76,7 @@ func TestCheckCompatible(t *testing.T) {
StoredBlock: big.NewInt(10), StoredBlock: big.NewInt(10),
NewBlock: big.NewInt(20), NewBlock: big.NewInt(20),
RewindToBlock: 9, RewindToBlock: 9,
RewindByBlock: true,
}, },
}, },
{ {
@ -91,6 +94,7 @@ func TestCheckCompatible(t *testing.T) {
StoredBlock: nil, StoredBlock: nil,
NewBlock: big.NewInt(31), NewBlock: big.NewInt(31),
RewindToBlock: 30, RewindToBlock: 30,
RewindByBlock: true,
}, },
}, },
{ {