@@ -21,6 +21,7 @@ import (
2121 "testing"
2222
2323 "github.com/google/go-cmp/cmp"
24+ "github.com/google/go-cmp/cmp/cmpopts"
2425 "github.com/kr/pretty"
2526 "github.com/stretchr/testify/assert"
2627 "github.com/stretchr/testify/require"
@@ -56,14 +57,18 @@ func TestBodyRLPBackwardsCompatibility(t *testing.T) {
5657 for _ , tx := range txMatrix {
5758 for _ , u := range uncleMatrix {
5859 for _ , w := range withdrawMatrix {
59- bodies = append (bodies , & Body {tx , u , w })
60+ bodies = append (bodies , & Body {tx , u , w , nil /* extra field */ })
6061 }
6162 }
6263 }
6364
6465 for _ , body := range bodies {
6566 t .Run ("" , func (t * testing.T ) {
66- t .Logf ("\n %s" , pretty .Sprint (body ))
67+ t .Cleanup (func () {
68+ if t .Failed () {
69+ t .Logf ("\n %s" , pretty .Sprint (body ))
70+ }
71+ })
6772
6873 // The original [Body] doesn't implement [rlp.Encoder] nor
6974 // [rlp.Decoder] so we can use a methodless equivalent as the gold
@@ -74,14 +79,15 @@ func TestBodyRLPBackwardsCompatibility(t *testing.T) {
7479
7580 t .Run ("Encode" , func (t * testing.T ) {
7681 got , err := rlp .EncodeToBytes (body )
77- require .NoErrorf (t , err , "rlp.EncodeToBytes(%#v )" , body )
78- assert .Equalf (t , wantRLP , got , "rlp.EncodeToBytes(%#v )" , body )
82+ require .NoErrorf (t , err , "rlp.EncodeToBytes(%T )" , body )
83+ assert .Equalf (t , wantRLP , got , "rlp.EncodeToBytes(%T )" , body )
7984 })
8085
8186 t .Run ("Decode" , func (t * testing.T ) {
8287 got := new (Body )
8388 err := rlp .DecodeBytes (wantRLP , got )
84- require .NoErrorf (t , err , "rlp.DecodeBytes(%v, %T)" , wantRLP , got )
89+ require .NoErrorf (t , err , "rlp.DecodeBytes(rlp.EncodeToBytes(%T), %T) resulted in %s" ,
90+ (* withoutMethods )(body ), got , pretty .Sprint (got ))
8591
8692 want := body
8793 // Regular RLP decoding will never leave these non-optional
@@ -96,9 +102,10 @@ func TestBodyRLPBackwardsCompatibility(t *testing.T) {
96102 opts := cmp.Options {
97103 cmp .Comparer ((* Header ).equalHash ),
98104 cmp .Comparer ((* Transaction ).equalHash ),
105+ cmpopts .IgnoreUnexported (Body {}),
99106 }
100- if diff := cmp .Diff (body , got , opts ); diff != "" {
101- t .Errorf ("rlp.DecodeBytes(rlp.EncodeToBytes(%#v )) diff (-want +got):\n %s" , body , diff )
107+ if diff := cmp .Diff (want , got , opts ); diff != "" {
108+ t .Errorf ("rlp.DecodeBytes(rlp.EncodeToBytes(%T )) diff (-want +got):\n %s" , ( * withoutMethods )( body ) , diff )
102109 }
103110 })
104111 })
@@ -148,10 +155,13 @@ func TestBodyRLPCChainCompat(t *testing.T) {
148155 // The inputs to this test were used to generate the expected RLP with
149156 // ava-labs/coreth. This serves as both an example of how to use [BodyHooks]
150157 // and a test of compatibility.
151-
152- t .Cleanup (func () {
153- TestOnlyRegisterBodyHooks (NOOPBodyHooks {})
154- })
158+ TestOnlyClearRegisteredExtras ()
159+ t .Cleanup (TestOnlyClearRegisteredExtras )
160+ extras := RegisterExtras [
161+ NOOPHeaderHooks , * NOOPHeaderHooks ,
162+ cChainBodyExtras , * cChainBodyExtras ,
163+ struct {},
164+ ]()
155165
156166 body := & Body {
157167 Transactions : []* Transaction {
@@ -194,24 +204,24 @@ func TestBodyRLPCChainCompat(t *testing.T) {
194204 require .NoErrorf (t , err , "hex.DecodeString(%q)" , tt .wantRLPHex )
195205
196206 t .Run ("Encode" , func (t * testing.T ) {
197- TestOnlyRegisterBodyHooks ( tt .extra )
207+ extras . Body . Set ( body , tt .extra )
198208 got , err := rlp .EncodeToBytes (body )
199209 require .NoErrorf (t , err , "rlp.EncodeToBytes(%+v)" , body )
200210 assert .Equalf (t , wantRLP , got , "rlp.EncodeToBytes(%+v)" , body )
201211 })
202212
203213 t .Run ("Decode" , func (t * testing.T ) {
204214 var extra cChainBodyExtras
205- TestOnlyRegisterBodyHooks (& extra )
206-
207215 got := new (Body )
216+ extras .Body .Set (got , & extra )
208217 err := rlp .DecodeBytes (wantRLP , got )
209218 require .NoErrorf (t , err , "rlp.DecodeBytes(%#x, %T)" , wantRLP , got )
210219 assert .Equal (t , tt .extra , & extra , "rlp.DecodeBytes(%#x, [%T as registered extra in %T carrier])" , wantRLP , & extra , got )
211220
212221 opts := cmp.Options {
213222 cmp .Comparer ((* Header ).equalHash ),
214223 cmp .Comparer ((* Transaction ).equalHash ),
224+ cmpopts .IgnoreUnexported (Body {}),
215225 }
216226 if diff := cmp .Diff (body , got , opts ); diff != "" {
217227 t .Errorf ("rlp.DecodeBytes(%#x, [%T while carrying registered %T extra payload]) diff (-want +got):\n %s" , wantRLP , got , & extra , diff )
0 commit comments