Skip to content

Commit 4bc3383

Browse files
committed
test: types.WithTempRegisteredExtras() hooks and payloads
1 parent 3cf1438 commit 4bc3383

File tree

1 file changed

+38
-24
lines changed

1 file changed

+38
-24
lines changed

core/types/tempextras.libevm_test.go

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,58 @@ package types
1818

1919
import (
2020
"testing"
21+
22+
"github.com/ava-labs/libevm/rlp"
23+
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/require"
2125
)
2226

27+
type tempBlockBodyHooks struct {
28+
X string
29+
NOOPBlockBodyHooks
30+
}
31+
32+
func (b *tempBlockBodyHooks) Copy() *tempBlockBodyHooks {
33+
return &tempBlockBodyHooks{X: b.X}
34+
}
35+
36+
func (b *tempBlockBodyHooks) BlockRLPFieldsForEncoding(*BlockRLPProxy) *rlp.Fields {
37+
return &rlp.Fields{
38+
Required: []any{b.X},
39+
}
40+
}
41+
2342
func TestTempRegisteredExtras(t *testing.T) {
2443
TestOnlyClearRegisteredExtras()
2544
t.Cleanup(TestOnlyClearRegisteredExtras)
2645

27-
type (
28-
primary struct {
29-
NOOPHeaderHooks
30-
}
31-
override struct {
32-
NOOPHeaderHooks
33-
}
34-
)
46+
rlpWithoutHooks, err := rlp.EncodeToBytes(&Block{})
47+
require.NoErrorf(t, err, "rlp.EncodeToBytes(%T) without hooks", &Block{})
3548

36-
RegisterExtras[primary, *primary, NOOPBlockBodyHooks, *NOOPBlockBodyHooks, bool]()
49+
extras := RegisterExtras[NOOPHeaderHooks, *NOOPHeaderHooks, NOOPBlockBodyHooks, *NOOPBlockBodyHooks, bool]()
3750
testPrimaryExtras := func(t *testing.T) {
3851
t.Helper()
39-
assertHeaderHooksConcreteType[*primary](t)
52+
b := new(Block)
53+
got, err := rlp.EncodeToBytes(b)
54+
require.NoErrorf(t, err, "rlp.EncodeToBytes(%T) with %T hooks", b, extras.Block.Get(b))
55+
assert.Equalf(t, rlpWithoutHooks, got, "rlp.EncodeToBytes(%T) with noop hooks; expect same as without hooks", b)
4056
}
4157

4258
t.Run("before_temp", testPrimaryExtras)
4359
t.Run("WithTempRegisteredExtras", func(t *testing.T) {
44-
WithTempRegisteredExtras(func(ExtraPayloads[*override, *NOOPBlockBodyHooks, bool]) {
45-
assertHeaderHooksConcreteType[*override](t)
60+
WithTempRegisteredExtras(func(extras ExtraPayloads[*NOOPHeaderHooks, *tempBlockBodyHooks, bool]) {
61+
const val = "Hello, world"
62+
b := new(Block)
63+
payload := &tempBlockBodyHooks{X: val}
64+
extras.Block.Set(b, payload)
65+
66+
got, err := rlp.EncodeToBytes(b)
67+
require.NoErrorf(t, err, "rlp.EncodeToBytes(%T) with %T hooks", b, extras.Block.Get(b))
68+
want, err := rlp.EncodeToBytes([]string{val})
69+
require.NoErrorf(t, err, "rlp.EncodeToBytes(%T{%[1]v})", []string{val})
70+
71+
assert.Equalf(t, want, got, "rlp.EncodeToBytes(%T) with %T hooks", b, payload)
4672
})
4773
})
4874
t.Run("after_temp", testPrimaryExtras)
4975
}
50-
51-
func assertHeaderHooksConcreteType[WantT any](t *testing.T) {
52-
t.Helper()
53-
54-
hdr := new(Header)
55-
switch got := hdr.hooks().(type) {
56-
case WantT:
57-
default:
58-
var want WantT
59-
t.Errorf("%T.hooks() got concrete type %T; want %T", hdr, got, want)
60-
}
61-
}

0 commit comments

Comments
 (0)