@@ -20,62 +20,62 @@ import (
20
20
"encoding/json"
21
21
"errors"
22
22
"fmt"
23
- "io"
24
23
"testing"
25
24
26
25
"github.com/stretchr/testify/assert"
27
26
"github.com/stretchr/testify/require"
28
27
28
+ "github.com/ava-labs/libevm/common"
29
29
. "github.com/ava-labs/libevm/core/types"
30
- "github.com/ava-labs/libevm/crypto"
31
30
"github.com/ava-labs/libevm/libevm/ethtest"
32
31
"github.com/ava-labs/libevm/libevm/pseudo"
33
32
"github.com/ava-labs/libevm/rlp"
34
33
)
35
34
36
35
type 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
42
41
43
- errMarshal , errUnmarshal , errEncode , errDecode error
42
+ errMarshal , errUnmarshal error
44
43
}
45
44
46
45
func fakeHeaderJSON (h * Header , suffix []byte ) []byte {
47
46
return []byte (fmt .Sprintf (`"%#x:%#x"` , h .ParentHash , suffix ))
48
47
}
49
48
50
- func fakeHeaderRLP (h * Header , suffix []byte ) []byte {
51
- return append (crypto .Keccak256 (h .ParentHash [:]), suffix ... )
52
- }
53
-
54
49
func (hh * stubHeaderHooks ) MarshalJSON (h * Header ) ([]byte , error ) { //nolint:govet
55
50
return fakeHeaderJSON (h , hh .suffix ), hh .errMarshal
56
51
}
57
52
58
53
func (hh * stubHeaderHooks ) UnmarshalJSON (h * Header , b []byte ) error { //nolint:govet
59
54
hh .gotRawJSONToUnmarshal = b
60
- * h = hh .setHeaderToOnUnmarshalOrDecode
55
+ * h = hh .setHeaderToOnUnmarshal
61
56
return hh .errUnmarshal
62
57
}
63
58
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
69
71
}
70
72
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 }}
79
79
}
80
80
81
81
func (hh * stubHeaderHooks ) PostCopy (dst * Header ) {
@@ -104,7 +104,7 @@ func TestHeaderHooks(t *testing.T) {
104
104
t .Run ("UnmarshalJSON" , func (t * testing.T ) {
105
105
hdr := new (Header )
106
106
stub := & stubHeaderHooks {
107
- setHeaderToOnUnmarshalOrDecode : Header {
107
+ setHeaderToOnUnmarshal : Header {
108
108
Extra : []byte ("can you solve this puzzle? 0xbda01b6cf56c303bd3f581599c0d5c0b" ),
109
109
},
110
110
}
@@ -115,31 +115,26 @@ func TestHeaderHooks(t *testing.T) {
115
115
require .NoErrorf (t , err , "json.Unmarshal()" )
116
116
117
117
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 )
119
119
})
120
120
121
121
t .Run ("EncodeRLP" , func (t * testing.T ) {
122
122
got , err := rlp .EncodeToBytes (hdr )
123
123
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 )
125
125
})
126
126
127
127
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 )
130
129
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 )
139
134
require .NoErrorf (t , err , "rlp.DecodeBytes(%#x)" , input )
140
135
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 )
143
138
})
144
139
145
140
t .Run ("PostCopy" , func (t * testing.T ) {
@@ -159,16 +154,12 @@ func TestHeaderHooks(t *testing.T) {
159
154
t .Run ("error_propagation" , func (t * testing.T ) {
160
155
errMarshal := errors .New ("whoops" )
161
156
errUnmarshal := errors .New ("is it broken?" )
162
- errEncode := errors .New ("uh oh" )
163
- errDecode := errors .New ("something bad happened" )
164
157
165
158
hdr := new (Header )
166
159
setStub := func () {
167
160
extras .Header .Set (hdr , & stubHeaderHooks {
168
161
errMarshal : errMarshal ,
169
162
errUnmarshal : errUnmarshal ,
170
- errEncode : errEncode ,
171
- errDecode : errDecode ,
172
163
})
173
164
}
174
165
@@ -184,15 +175,5 @@ func TestHeaderHooks(t *testing.T) {
184
175
err := json .Unmarshal ([]byte ("{}" ), hdr )
185
176
assert .Equal (t , errUnmarshal , err , "via json.Unmarshal()" )
186
177
}
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
- }
197
178
})
198
179
}
0 commit comments