make -1 denote the "not used" value

This commit is contained in:
alan 2024-06-19 22:01:39 +08:00
parent 7b10aa443b
commit a647b36fe3
5 changed files with 22 additions and 24 deletions

View file

@ -454,7 +454,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
if compat.RewindToTime > 0 { if compat.RewindToTime > 0 {
bc.SetHeadWithTimestamp(compat.RewindToTime) bc.SetHeadWithTimestamp(compat.RewindToTime)
} else { } else {
bc.SetHead(compat.RewindToBlock) bc.SetHead(uint64(compat.RewindToBlock))
} }
rawdb.WriteChainConfig(db, genesisHash, chainConfig) rawdb.WriteChainConfig(db, genesisHash, chainConfig)
} }

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.RewindByBlock) || (head.Time != 0 && compatErr.RewindToTime != 0)) { if compatErr != nil && ((head.Number.Uint64() != 0 && compatErr.IsRewindByBlock()) || (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,7 +148,6 @@ 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

@ -605,7 +605,7 @@ func (c *ChainConfig) CheckCompatible(newcfg *ChainConfig, height uint64, time u
if err.RewindToTime > 0 { if err.RewindToTime > 0 {
btime = err.RewindToTime btime = err.RewindToTime
} else { } else {
bhead.SetUint64(err.RewindToBlock) bhead.SetUint64(uint64(err.RewindToBlock))
} }
} }
return lasterr return lasterr
@ -841,10 +841,8 @@ type ConfigCompatError struct {
StoredTime, NewTime *uint64 StoredTime, NewTime *uint64
// 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 // "-1" is used as a "not used" flag to denote it's rewinding by time instead
RewindToBlock int64
// 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
@ -865,10 +863,9 @@ 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 = int64(rew.Uint64() - 1)
} }
return err return err
} }
@ -884,10 +881,11 @@ func newTimestampCompatError(what string, storedtime, newtime *uint64) *ConfigCo
rew = newtime rew = newtime
} }
err := &ConfigCompatError{ err := &ConfigCompatError{
What: what, What: what,
StoredTime: storedtime, StoredTime: storedtime,
NewTime: newtime, NewTime: newtime,
RewindToTime: 0, RewindToBlock: -1,
RewindToTime: 0,
} }
if rew != nil && *rew != 0 { if rew != nil && *rew != 0 {
err.RewindToTime = *rew - 1 err.RewindToTime = *rew - 1
@ -895,6 +893,10 @@ func newTimestampCompatError(what string, storedtime, newtime *uint64) *ConfigCo
return err return err
} }
func (err *ConfigCompatError) IsRewindByBlock() bool {
return err.RewindToBlock != -1
}
func (err *ConfigCompatError) Error() string { func (err *ConfigCompatError) Error() string {
if err.StoredBlock != nil { if err.StoredBlock != nil {
return fmt.Sprintf("mismatching %s in database (have block %d, want block %d, rewindto block %d)", err.What, err.StoredBlock, err.NewBlock, err.RewindToBlock) return fmt.Sprintf("mismatching %s in database (have block %d, want block %d, rewindto block %d)", err.What, err.StoredBlock, err.NewBlock, err.RewindToBlock)

View file

@ -52,7 +52,6 @@ func TestCheckCompatible(t *testing.T) {
StoredBlock: big.NewInt(0), StoredBlock: big.NewInt(0),
NewBlock: nil, NewBlock: nil,
RewindToBlock: 0, RewindToBlock: 0,
RewindByBlock: true,
}, },
}, },
{ {
@ -64,7 +63,6 @@ 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,
}, },
}, },
{ {
@ -76,7 +74,6 @@ 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,
}, },
}, },
{ {
@ -94,7 +91,6 @@ func TestCheckCompatible(t *testing.T) {
StoredBlock: nil, StoredBlock: nil,
NewBlock: big.NewInt(31), NewBlock: big.NewInt(31),
RewindToBlock: 30, RewindToBlock: 30,
RewindByBlock: true,
}, },
}, },
{ {
@ -108,18 +104,19 @@ func TestCheckCompatible(t *testing.T) {
new: &ChainConfig{ShanghaiTime: newUint64(20)}, new: &ChainConfig{ShanghaiTime: newUint64(20)},
headTimestamp: 25, headTimestamp: 25,
wantErr: &ConfigCompatError{ wantErr: &ConfigCompatError{
What: "Shanghai fork timestamp", What: "Shanghai fork timestamp",
StoredTime: newUint64(10), StoredTime: newUint64(10),
NewTime: newUint64(20), NewTime: newUint64(20),
RewindToTime: 9, RewindToTime: 9,
RewindToBlock: -1,
}, },
}, },
} }
for _, test := range tests { for i, test := range tests {
err := test.stored.CheckCompatible(test.new, test.headBlock, test.headTimestamp) err := test.stored.CheckCompatible(test.new, test.headBlock, test.headTimestamp)
if !reflect.DeepEqual(err, test.wantErr) { if !reflect.DeepEqual(err, test.wantErr) {
t.Errorf("error mismatch:\nstored: %v\nnew: %v\nheadBlock: %v\nheadTimestamp: %v\nerr: %v\nwant: %v", test.stored, test.new, test.headBlock, test.headTimestamp, err, test.wantErr) t.Errorf("error mismatch:\nstored: %v\nnew: %v\nheadBlock: %v\nheadTimestamp: %v\nerr: %v\nwant: %v\nindex:%d", test.stored, test.new, test.headBlock, test.headTimestamp, err, test.wantErr, i)
} }
} }
} }