|
| 1 | +package params_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "math/big" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/ethereum/go-ethereum/libevm/ethtest" |
| 10 | + "github.com/ethereum/go-ethereum/libevm/hookstest" |
| 11 | + "github.com/ethereum/go-ethereum/params" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestChainConfigHooks_Description(t *testing.T) { |
| 16 | + const suffix = "Arran was here" |
| 17 | + c := new(params.ChainConfig) |
| 18 | + want := c.Description() + suffix |
| 19 | + |
| 20 | + hooks := &hookstest.Stub{ |
| 21 | + DescriptionSuffix: "Arran was here", |
| 22 | + } |
| 23 | + hooks.Register(t).SetOnChainConfig(c, hooks) |
| 24 | + require.Equal(t, want, c.Description(), "ChainConfigHooks.Description() is appended to non-extras equivalent") |
| 25 | +} |
| 26 | + |
| 27 | +func TestChainConfigHooks_CheckConfigForkOrder(t *testing.T) { |
| 28 | + err := errors.New("uh oh") |
| 29 | + |
| 30 | + c := new(params.ChainConfig) |
| 31 | + require.NoError(t, c.CheckConfigForkOrder(), "CheckConfigForkOrder() with no hooks") |
| 32 | + |
| 33 | + hooks := &hookstest.Stub{ |
| 34 | + CheckConfigForkOrderFn: func() error { return err }, |
| 35 | + } |
| 36 | + hooks.Register(t).SetOnChainConfig(c, hooks) |
| 37 | + require.Equal(t, err, c.CheckConfigForkOrder(), "CheckConfigForkOrder() with error-producing hook") |
| 38 | +} |
| 39 | + |
| 40 | +func TestChainConfigHooks_CheckConfigCompatible(t *testing.T) { |
| 41 | + rng := ethtest.NewPseudoRand(1234567890) |
| 42 | + newcfg := ¶ms.ChainConfig{ |
| 43 | + ChainID: rng.BigUint64(), |
| 44 | + } |
| 45 | + headNumber := rng.Uint64() |
| 46 | + headTimestamp := rng.Uint64() |
| 47 | + |
| 48 | + c := new(params.ChainConfig) |
| 49 | + require.Nil(t, c.CheckCompatible(newcfg, headNumber, headTimestamp), "CheckCompatible() with no hooks") |
| 50 | + |
| 51 | + makeCompatErr := func(newcfg *params.ChainConfig, headNumber *big.Int, headTimestamp uint64) *params.ConfigCompatError { |
| 52 | + return ¶ms.ConfigCompatError{ |
| 53 | + What: fmt.Sprintf("ChainID: %v Head #: %v Head Time: %d", newcfg.ChainID, headNumber, headTimestamp), |
| 54 | + } |
| 55 | + } |
| 56 | + hooks := &hookstest.Stub{ |
| 57 | + CheckConfigCompatibleFn: makeCompatErr, |
| 58 | + } |
| 59 | + hooks.Register(t).SetOnChainConfig(c, hooks) |
| 60 | + want := makeCompatErr(newcfg, new(big.Int).SetUint64(headNumber), headTimestamp) |
| 61 | + require.Equal(t, want, c.CheckCompatible(newcfg, headNumber, headTimestamp), "CheckCompatible() with error-producing hook") |
| 62 | +} |
0 commit comments