Skip to content

Commit 6122f03

Browse files
committed
test: all new hooks
1 parent fbe5e53 commit 6122f03

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
lines changed

libevm/hookstest/stub.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import (
1414
// Register clears any registered [params.Extras] and then registers `extras`
1515
// for the lifetime of the current test, clearing them via tb's
1616
// [testing.TB.Cleanup].
17-
func Register[C params.ChainConfigHooks, R params.RulesHooks](tb testing.TB, extras params.Extras[C, R]) {
17+
func Register[C params.ChainConfigHooks, R params.RulesHooks](tb testing.TB, extras params.Extras[C, R]) params.ExtraPayloads[C, R] {
1818
tb.Helper()
1919
params.TestOnlyClearRegisteredExtras()
2020
tb.Cleanup(params.TestOnlyClearRegisteredExtras)
21-
params.RegisterExtras(extras)
21+
return params.RegisterExtras(extras)
2222
}
2323

2424
// A Stub is a test double for [params.ChainConfigHooks] and
@@ -27,17 +27,17 @@ func Register[C params.ChainConfigHooks, R params.RulesHooks](tb testing.TB, ext
2727
type Stub struct {
2828
CheckConfigForkOrderFn func() error
2929
CheckConfigCompatibleFn func(*params.ChainConfig, *big.Int, uint64) *params.ConfigCompatError
30-
DescriptionValue string
30+
DescriptionSuffix string
3131
PrecompileOverrides map[common.Address]libevm.PrecompiledContract
3232
CanExecuteTransactionFn func(common.Address, *common.Address, libevm.StateReader) error
3333
CanCreateContractFn func(*libevm.AddressContext, uint64, libevm.StateReader) (uint64, error)
3434
}
3535

3636
// Register is a convenience wrapper for registering s as both the
3737
// [params.ChainConfigHooks] and [params.RulesHooks] via [Register].
38-
func (s *Stub) Register(tb testing.TB) {
38+
func (s *Stub) Register(tb testing.TB) params.ExtraPayloads[*Stub, *Stub] {
3939
tb.Helper()
40-
Register(tb, params.Extras[*Stub, *Stub]{
40+
return Register(tb, params.Extras[*Stub, *Stub]{
4141
NewRules: func(_ *params.ChainConfig, _ *params.Rules, _ *Stub, blockNum *big.Int, isMerge bool, timestamp uint64) *Stub {
4242
return s
4343
},
@@ -75,7 +75,7 @@ func (s Stub) CheckConfigCompatible(newcfg *params.ChainConfig, headNumber *big.
7575

7676
// Description returns s.DescriptionValue.
7777
func (s Stub) Description() string {
78-
return s.DescriptionValue
78+
return s.DescriptionSuffix
7979
}
8080

8181
// CanExecuteTransaction proxies arguments to the s.CanExecuteTransactionFn

params/hooks.libevm_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 := &params.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 &params.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

Comments
 (0)