@@ -20,62 +20,62 @@ import (
2020 "encoding/json"
2121 "errors"
2222 "fmt"
23- "io"
2423 "testing"
2524
2625 "github.com/stretchr/testify/assert"
2726 "github.com/stretchr/testify/require"
2827
28+ "github.com/ava-labs/libevm/common"
2929 . "github.com/ava-labs/libevm/core/types"
30- "github.com/ava-labs/libevm/crypto"
3130 "github.com/ava-labs/libevm/libevm/ethtest"
3231 "github.com/ava-labs/libevm/libevm/pseudo"
3332 "github.com/ava-labs/libevm/rlp"
3433)
3534
3635type stubHeaderHooks struct {
37- suffix []byte
38- gotRawJSONToUnmarshal , gotRawRLPToDecode []byte
39- setHeaderToOnUnmarshalOrDecode Header
40- accessor pseudo.Accessor [* Header , * stubHeaderHooks ]
41- toCopy * stubHeaderHooks
36+ suffix []byte
37+ gotRawJSONToUnmarshal []byte
38+ setHeaderToOnUnmarshal Header
39+ accessor pseudo.Accessor [* Header , * stubHeaderHooks ]
40+ toCopy * stubHeaderHooks
4241
43- errMarshal , errUnmarshal , errEncode , errDecode error
42+ errMarshal , errUnmarshal error
4443}
4544
4645func fakeHeaderJSON (h * Header , suffix []byte ) []byte {
4746 return []byte (fmt .Sprintf (`"%#x:%#x"` , h .ParentHash , suffix ))
4847}
4948
50- func fakeHeaderRLP (h * Header , suffix []byte ) []byte {
51- return append (crypto .Keccak256 (h .ParentHash [:]), suffix ... )
52- }
53-
5449func (hh * stubHeaderHooks ) MarshalJSON (h * Header ) ([]byte , error ) { //nolint:govet
5550 return fakeHeaderJSON (h , hh .suffix ), hh .errMarshal
5651}
5752
5853func (hh * stubHeaderHooks ) UnmarshalJSON (h * Header , b []byte ) error { //nolint:govet
5954 hh .gotRawJSONToUnmarshal = b
60- * h = hh .setHeaderToOnUnmarshalOrDecode
55+ * h = hh .setHeaderToOnUnmarshal
6156 return hh .errUnmarshal
6257}
6358
64- func (hh * stubHeaderHooks ) EncodeRLP (h * Header , w io.Writer ) error {
65- if _ , err := w .Write (fakeHeaderRLP (h , hh .suffix )); err != nil {
66- return err
67- }
68- return hh .errEncode
59+ func directEncodeHeaderRLP (tb testing.TB , h * Header , extraPayloadSuffix []byte ) []byte {
60+ tb .Helper ()
61+
62+ // The encoded type mirrors the fields returned by
63+ // [stubHeaderHooks.RLPFieldsForEncoding].
64+ buf , err := rlp .EncodeToBytes (struct {
65+ ParentHash common.Hash
66+ Suffix []byte
67+ }{h .ParentHash , extraPayloadSuffix })
68+
69+ require .NoError (tb , err )
70+ return buf
6971}
7072
71- func (hh * stubHeaderHooks ) DecodeRLP (h * Header , s * rlp.Stream ) error {
72- r , err := s .Raw ()
73- if err != nil {
74- return err
75- }
76- hh .gotRawRLPToDecode = r
77- * h = hh .setHeaderToOnUnmarshalOrDecode
78- return hh .errDecode
73+ func (hh * stubHeaderHooks ) RLPFieldsForEncoding (h * Header ) * rlp.Fields {
74+ return & rlp.Fields {Required : []any {h .ParentHash , hh .suffix }}
75+ }
76+
77+ func (hh * stubHeaderHooks ) RLPFieldPointersForDecoding (h * Header ) * rlp.Fields {
78+ return & rlp.Fields {Required : []any {& h .ParentHash , & hh .suffix }}
7979}
8080
8181func (hh * stubHeaderHooks ) PostCopy (dst * Header ) {
@@ -104,7 +104,7 @@ func TestHeaderHooks(t *testing.T) {
104104 t .Run ("UnmarshalJSON" , func (t * testing.T ) {
105105 hdr := new (Header )
106106 stub := & stubHeaderHooks {
107- setHeaderToOnUnmarshalOrDecode : Header {
107+ setHeaderToOnUnmarshal : Header {
108108 Extra : []byte ("can you solve this puzzle? 0xbda01b6cf56c303bd3f581599c0d5c0b" ),
109109 },
110110 }
@@ -115,31 +115,26 @@ func TestHeaderHooks(t *testing.T) {
115115 require .NoErrorf (t , err , "json.Unmarshal()" )
116116
117117 assert .Equal (t , input , string (stub .gotRawJSONToUnmarshal ), "raw JSON received by hook" )
118- assert .Equal (t , & stub .setHeaderToOnUnmarshalOrDecode , hdr , "%T after JSON unmarshalling with hook" , hdr )
118+ assert .Equal (t , & stub .setHeaderToOnUnmarshal , hdr , "%T after JSON unmarshalling with hook" , hdr )
119119 })
120120
121121 t .Run ("EncodeRLP" , func (t * testing.T ) {
122122 got , err := rlp .EncodeToBytes (hdr )
123123 require .NoError (t , err , "rlp.EncodeToBytes(%T)" , hdr )
124- assert .Equal (t , fakeHeaderRLP ( hdr , suffix ), got )
124+ assert .Equal (t , directEncodeHeaderRLP ( t , hdr , suffix ), got )
125125 })
126126
127127 t .Run ("DecodeRLP" , func (t * testing.T ) {
128- input , err := rlp .EncodeToBytes (rng .Bytes (8 ))
129- require .NoError (t , err )
128+ input := directEncodeHeaderRLP (t , hdr , suffix )
130129
131- hdr := new (Header )
132- stub := & stubHeaderHooks {
133- setHeaderToOnUnmarshalOrDecode : Header {
134- Extra : []byte ("arr4n was here" ),
135- },
136- }
137- extras .Header .Set (hdr , stub )
138- err = rlp .DecodeBytes (input , hdr )
130+ got := new (Header )
131+ stub := & stubHeaderHooks {}
132+ extras .Header .Set (got , stub )
133+ err := rlp .DecodeBytes (input , got )
139134 require .NoErrorf (t , err , "rlp.DecodeBytes(%#x)" , input )
140135
141- assert .Equal (t , input , stub . gotRawRLPToDecode , "raw RLP received by hooks" )
142- assert .Equalf (t , & stub .setHeaderToOnUnmarshalOrDecode , hdr , "%T after RLP decoding with hook " , hdr )
136+ assert .Equalf (t , hdr . ParentHash , got . ParentHash , "RLP-decoded %T.ParentHash" , hdr )
137+ assert .Equalf (t , suffix , stub .suffix , " RLP-decoded %T.suffix " , stub )
143138 })
144139
145140 t .Run ("PostCopy" , func (t * testing.T ) {
@@ -159,16 +154,12 @@ func TestHeaderHooks(t *testing.T) {
159154 t .Run ("error_propagation" , func (t * testing.T ) {
160155 errMarshal := errors .New ("whoops" )
161156 errUnmarshal := errors .New ("is it broken?" )
162- errEncode := errors .New ("uh oh" )
163- errDecode := errors .New ("something bad happened" )
164157
165158 hdr := new (Header )
166159 setStub := func () {
167160 extras .Header .Set (hdr , & stubHeaderHooks {
168161 errMarshal : errMarshal ,
169162 errUnmarshal : errUnmarshal ,
170- errEncode : errEncode ,
171- errDecode : errDecode ,
172163 })
173164 }
174165
@@ -184,15 +175,5 @@ func TestHeaderHooks(t *testing.T) {
184175 err := json .Unmarshal ([]byte ("{}" ), hdr )
185176 assert .Equal (t , errUnmarshal , err , "via json.Unmarshal()" )
186177 }
187-
188- setStub () // [stubHeaderHooks] completely overrides the Header
189- {
190- err := rlp .Encode (io .Discard , hdr )
191- assert .Equal (t , errEncode , err , "via rlp.Encode()" )
192- }
193- {
194- err := rlp .DecodeBytes ([]byte {0 }, hdr )
195- assert .Equal (t , errDecode , err , "via rlp.DecodeBytes()" )
196- }
197178 })
198179}
0 commit comments