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 {
bc.SetHeadWithTimestamp(compat.RewindToTime)
} else {
bc.SetHead(compat.RewindToBlock)
bc.SetHead(uint64(compat.RewindToBlock))
}
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")
}
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
}
// 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",
StoredBlock: big.NewInt(2),
NewBlock: big.NewInt(3),
RewindByBlock: true,
RewindToBlock: 1,
},
},

View file

@ -605,7 +605,7 @@ func (c *ChainConfig) CheckCompatible(newcfg *ChainConfig, height uint64, time u
if err.RewindToTime > 0 {
btime = err.RewindToTime
} else {
bhead.SetUint64(err.RewindToBlock)
bhead.SetUint64(uint64(err.RewindToBlock))
}
}
return lasterr
@ -841,10 +841,8 @@ type ConfigCompatError struct {
StoredTime, NewTime *uint64
// the block number to which the local chain must be rewound to correct the error
RewindToBlock uint64
// the flag to tell whether it's rewinding by block or time
RewindByBlock bool
// "-1" is used as a "not used" flag to denote it's rewinding by time instead
RewindToBlock int64
// the timestamp to which the local chain must be rewound to correct the error
RewindToTime uint64
@ -865,10 +863,9 @@ func newBlockCompatError(what string, storedblock, newblock *big.Int) *ConfigCom
StoredBlock: storedblock,
NewBlock: newblock,
RewindToBlock: 0,
RewindByBlock: true,
}
if rew != nil && rew.Sign() > 0 {
err.RewindToBlock = rew.Uint64() - 1
err.RewindToBlock = int64(rew.Uint64() - 1)
}
return err
}
@ -887,6 +884,7 @@ func newTimestampCompatError(what string, storedtime, newtime *uint64) *ConfigCo
What: what,
StoredTime: storedtime,
NewTime: newtime,
RewindToBlock: -1,
RewindToTime: 0,
}
if rew != nil && *rew != 0 {
@ -895,6 +893,10 @@ func newTimestampCompatError(what string, storedtime, newtime *uint64) *ConfigCo
return err
}
func (err *ConfigCompatError) IsRewindByBlock() bool {
return err.RewindToBlock != -1
}
func (err *ConfigCompatError) Error() string {
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)

View file

@ -52,7 +52,6 @@ func TestCheckCompatible(t *testing.T) {
StoredBlock: big.NewInt(0),
NewBlock: nil,
RewindToBlock: 0,
RewindByBlock: true,
},
},
{
@ -64,7 +63,6 @@ func TestCheckCompatible(t *testing.T) {
StoredBlock: big.NewInt(0),
NewBlock: big.NewInt(1),
RewindToBlock: 0,
RewindByBlock: true,
},
},
{
@ -76,7 +74,6 @@ func TestCheckCompatible(t *testing.T) {
StoredBlock: big.NewInt(10),
NewBlock: big.NewInt(20),
RewindToBlock: 9,
RewindByBlock: true,
},
},
{
@ -94,7 +91,6 @@ func TestCheckCompatible(t *testing.T) {
StoredBlock: nil,
NewBlock: big.NewInt(31),
RewindToBlock: 30,
RewindByBlock: true,
},
},
{
@ -112,14 +108,15 @@ func TestCheckCompatible(t *testing.T) {
StoredTime: newUint64(10),
NewTime: newUint64(20),
RewindToTime: 9,
RewindToBlock: -1,
},
},
}
for _, test := range tests {
for i, test := range tests {
err := test.stored.CheckCompatible(test.new, test.headBlock, test.headTimestamp)
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)
}
}
}