Commit 0b33f332 authored by zhiqiangxu's avatar zhiqiangxu Committed by GitHub

check prior fork consistently (#9255)

* check prior fork

* move inside cfg.Rollup.Check

* reuse checkFork logic

* fix testcase

* move checkFork to top level

* add testcases
parent e9172f60
......@@ -66,6 +66,7 @@ func mockConfig(t *testing.T) Config {
ChannelTimeout: 300,
L1ChainID: big.NewInt(1),
L2ChainID: big.NewInt(2),
RegolithTime: &now,
CanyonTime: &now,
BatchInboxAddress: [20]byte{1, 2},
DepositContractAddress: [20]byte{2, 3},
......
......@@ -277,6 +277,37 @@ func (cfg *Config) Check() error {
if cfg.L2ChainID.Sign() < 1 {
return ErrL2ChainIDNotPositive
}
if err := checkFork(cfg.RegolithTime, cfg.CanyonTime, "regolith", "canyon"); err != nil {
return err
}
if err := checkFork(cfg.CanyonTime, cfg.DeltaTime, "canyon", "delta"); err != nil {
return err
}
if err := checkFork(cfg.DeltaTime, cfg.EcotoneTime, "delta", "ecotone"); err != nil {
return err
}
if err := checkFork(cfg.EcotoneTime, cfg.FjordTime, "ecotone", "fjord"); err != nil {
return err
}
return nil
}
// checkFork checks that fork A is before or at the same time as fork B
func checkFork(a, b *uint64, aName, bName string) error {
if a == nil && b == nil {
return nil
}
if a == nil && b != nil {
return fmt.Errorf("fork %s set (to %d), but prior fork %s missing", bName, *b, aName)
}
if a != nil && b == nil {
return nil
}
if *a > *b {
return fmt.Errorf("fork %s set to %d, but prior fork %s has higher offset %d", bName, *b, aName, *a)
}
return nil
}
......
......@@ -408,6 +408,54 @@ func TestConfig_Check(t *testing.T) {
assert.Same(t, err, test.expectedErr)
})
}
forkTests := []struct {
name string
modifier func(cfg *Config)
expectedErr error
}{
{
name: "PriorForkMissing",
modifier: func(cfg *Config) {
ecotoneTime := uint64(1)
cfg.EcotoneTime = &ecotoneTime
},
expectedErr: fmt.Errorf("fork ecotone set (to 1), but prior fork delta missing"),
},
{
name: "PriorForkHasHigherOffset",
modifier: func(cfg *Config) {
regolithTime := uint64(2)
canyonTime := uint64(1)
cfg.RegolithTime = &regolithTime
cfg.CanyonTime = &canyonTime
},
expectedErr: fmt.Errorf("fork canyon set to 1, but prior fork regolith has higher offset 2"),
},
{
name: "PriorForkOK",
modifier: func(cfg *Config) {
regolithTime := uint64(1)
canyonTime := uint64(2)
deltaTime := uint64(3)
ecotoneTime := uint64(4)
cfg.RegolithTime = &regolithTime
cfg.CanyonTime = &canyonTime
cfg.DeltaTime = &deltaTime
cfg.EcotoneTime = &ecotoneTime
},
expectedErr: nil,
},
}
for _, test := range forkTests {
t.Run(test.name, func(t *testing.T) {
cfg := randConfig()
test.modifier(cfg)
err := cfg.Check()
assert.Equal(t, err, test.expectedErr)
})
}
}
func TestTimestampForBlock(t *testing.T) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment