diff --git a/core/blockchain.go b/core/blockchain.go index 7c8ab3abc4..b04edf0561 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -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) } diff --git a/core/genesis.go b/core/genesis.go index 956ea66a89..b20187ce6c 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -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 diff --git a/core/genesis_test.go b/core/genesis_test.go index 828a99f1f2..31401e214c 100644 --- a/core/genesis_test.go +++ b/core/genesis_test.go @@ -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, }, }, diff --git a/params/config.go b/params/config.go index 59060a0966..fbb7ce3952 100644 --- a/params/config.go +++ b/params/config.go @@ -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 } @@ -884,10 +881,11 @@ func newTimestampCompatError(what string, storedtime, newtime *uint64) *ConfigCo rew = newtime } err := &ConfigCompatError{ - What: what, - StoredTime: storedtime, - NewTime: newtime, - RewindToTime: 0, + What: what, + StoredTime: storedtime, + NewTime: newtime, + RewindToBlock: -1, + RewindToTime: 0, } if rew != nil && *rew != 0 { err.RewindToTime = *rew - 1 @@ -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) diff --git a/params/config_test.go b/params/config_test.go index 82f872bdfb..007f61b9a2 100644 --- a/params/config_test.go +++ b/params/config_test.go @@ -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, }, }, { @@ -108,18 +104,19 @@ func TestCheckCompatible(t *testing.T) { new: &ChainConfig{ShanghaiTime: newUint64(20)}, headTimestamp: 25, wantErr: &ConfigCompatError{ - What: "Shanghai fork timestamp", - StoredTime: newUint64(10), - NewTime: newUint64(20), - RewindToTime: 9, + What: "Shanghai fork timestamp", + 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) } } }