1414// along with the go-ethereum library. If not, see 
1515// <http://www.gnu.org/licenses/>. 
1616
17- package  types_test 
17+ package  types 
1818
1919import  (
2020	"encoding/hex" 
@@ -26,91 +26,9 @@ import (
2626	"github.com/stretchr/testify/require" 
2727
2828	"github.com/ava-labs/libevm/common" 
29- 	. "github.com/ava-labs/libevm/core/types" 
30- 	"github.com/ava-labs/libevm/libevm/cmpeth" 
31- 	"github.com/ava-labs/libevm/libevm/ethtest" 
3229	"github.com/ava-labs/libevm/rlp" 
3330)
3431
35- func  TestHeaderRLPBackwardsCompatibility (t  * testing.T ) {
36- 	tests  :=  []struct  {
37- 		name      string 
38- 		register  func ()
39- 	}{
40- 		{
41- 			name :     "no registered extras" ,
42- 			register : func () {},
43- 		},
44- 		{
45- 			name : "no-op header hooks" ,
46- 			register : func () {
47- 				RegisterExtras [NOOPHeaderHooks , * NOOPHeaderHooks , struct {}]()
48- 			},
49- 		},
50- 	}
51- 
52- 	for  _ , tt  :=  range  tests  {
53- 		t .Run (tt .name , func (t  * testing.T ) {
54- 			TestOnlyClearRegisteredExtras ()
55- 			defer  TestOnlyClearRegisteredExtras ()
56- 			tt .register ()
57- 			testHeaderRLPBackwardsCompatibility (t )
58- 		})
59- 	}
60- }
61- 
62- //nolint:thelper 
63- func  testHeaderRLPBackwardsCompatibility (t  * testing.T ) {
64- 	// This is a deliberate change-detector test that locks in backwards 
65- 	// compatibility of RLP encoding. 
66- 	rng  :=  ethtest .NewPseudoRand (42 )
67- 
68- 	const  numExtraBytes  =  16 
69- 	hdr  :=  & Header {
70- 		ParentHash :  rng .Hash (),
71- 		UncleHash :   rng .Hash (),
72- 		Coinbase :    rng .Address (),
73- 		Root :        rng .Hash (),
74- 		TxHash :      rng .Hash (),
75- 		ReceiptHash : rng .Hash (),
76- 		Bloom :       rng .Bloom (),
77- 		Difficulty :  rng .Uint256 ().ToBig (),
78- 		Number :      rng .BigUint64 (),
79- 		GasLimit :    rng .Uint64 (),
80- 		GasUsed :     rng .Uint64 (),
81- 		Time :        rng .Uint64 (),
82- 		Extra :       rng .Bytes (numExtraBytes ),
83- 		MixDigest :   rng .Hash (),
84- 		Nonce :       rng .BlockNonce (),
85- 
86- 		BaseFee :          rng .BigUint64 (),
87- 		WithdrawalsHash :  rng .HashPtr (),
88- 		BlobGasUsed :      rng .Uint64Ptr (),
89- 		ExcessBlobGas :    rng .Uint64Ptr (),
90- 		ParentBeaconRoot : rng .HashPtr (),
91- 	}
92- 	t .Logf ("%T:\n %+v" , hdr , hdr )
93- 
94- 	// WARNING: changing this hex might break backwards compatibility of RLP 
95- 	// encoding (i.e. block hashes might change)! 
96- 	const  wantHex  =  `f9029aa01a571e7e4d774caf46053201cfe0001b3c355ffcc93f510e671e8809741f0eeda0756095410506ec72a2c287fe83ebf68efb0be177e61acec1c985277e90e52087941bfc3bc193012ba58912c01fb35a3454831a8971a00bc9f064144eb5965c5e5d1020f9f90392e7e06ded9225966abc7c754b410e61a0d942eab201424f4320ec1e1ffa9390baf941629b9349977b5d48e0502dbb9386a035d9d550a9c113f78689b4c161c4605609bb57b83061914c42ad244daa7fc38eb901004b31d39ae246d689f23176d679a62ff328f530407cbafd0146f45b2ed635282e2812f2705bfffe52576a6fb31df817f29efac71fa56b8e133334079f8e2a8fd2055451571021506f27190adb52a1313f6d28c77d66ae1aa3d3d6757a762476f4c8a2b7b2a37079a4b6a15d1bc44161190c82d5e1c8b55e05c7354f1e5f6512924c941fb3d93667dc3a8c304a3c164e6525dfc99b5f474110c5059485732153e20300c3482832d07b65f97958360da414cb438ce252aec6c2718d155798390a6c6782181d1bac1dd64cd956332b008412ddc735f2994e297c8a088c6bb4c637542295ba3cbc3cd399c8127076f4d834d74d5b11a36b6d02e2fe3a583216aa4ccea0f052df9a96e7a454256bebabdfc38c429079f25913e0f1d7416b2f056c4a115f88b85f0e9fd6d25717881f03d9985060087c88a2c54269dfd07ca388eb8f974b42a412da90c757012bf5479896165caf573cf82fb3a0aa10f6ebf6b62bef8ed36b8ea3d4b1ddb80c99afafa37cb8f3393eb6d802f5bc886c8cd6bcd168a7e0886d5b1345d948b818a0061a7182ff228a4e66bade4717e6f4d318ac98fca12a053af6f98805a764fb5d8890ed9cab2c5229908891c7e2f71857c77ca0523cb6f654ef3fc7294c7768cddd9ccf4bcda3066d382675f37dd1a18507b5fb` 
97- 	wantRLP , err  :=  hex .DecodeString (wantHex )
98- 	require .NoError (t , err , "hex.DecodeString()" )
99- 
100- 	t .Run ("Encode" , func (t  * testing.T ) {
101- 		got , err  :=  rlp .EncodeToBytes (hdr )
102- 		require .NoErrorf (t , err , "rlp.EncodeToBytes(%T)" , hdr )
103- 		assert .Equalf (t , wantRLP , got , "rlp.EncodeToBytes(%T)" , hdr )
104- 	})
105- 
106- 	t .Run ("Decode" , func (t  * testing.T ) {
107- 		got  :=  new (Header )
108- 		err  :=  rlp .DecodeBytes (wantRLP , got )
109- 		require .NoErrorf (t , err , "rlp.DecodeBytes(..., %T)" , hdr )
110- 		assert .Equal (t , hdr , got )
111- 	})
112- }
113- 
11432func  TestBodyRLPBackwardsCompatibility (t  * testing.T ) {
11533	newTx  :=  func (nonce  uint64 ) * Transaction  { return  NewTx (& LegacyTx {Nonce : nonce }) }
11634	newHdr  :=  func (hashLow  byte ) * Header  { return  & Header {ParentHash : common.Hash {hashLow }} }
@@ -176,8 +94,8 @@ func TestBodyRLPBackwardsCompatibility(t *testing.T) {
17694				}
17795
17896				opts  :=  cmp.Options {
179- 					cmpeth . CompareHeadersByHash ( ),
180- 					cmpeth . CompareTransactionsByBinary ( t ),
97+ 					cmp . Comparer (( * Header ). equalHash ),
98+ 					cmp . Comparer (( * Transaction ). equalHash ),
18199				}
182100				if  diff  :=  cmp .Diff (body , got , opts ); diff  !=  ""  {
183101					t .Errorf ("rlp.DecodeBytes(rlp.EncodeToBytes(%#v)) diff (-want +got):\n %s" , body , diff )
@@ -292,8 +210,8 @@ func TestBodyRLPCChainCompat(t *testing.T) {
292210				assert .Equal (t , tt .extra , & extra , "rlp.DecodeBytes(%#x, [%T as registered extra in %T carrier])" , wantRLP , & extra , got )
293211
294212				opts  :=  cmp.Options {
295- 					cmpeth . CompareHeadersByHash ( ),
296- 					cmpeth . CompareTransactionsByBinary ( t ),
213+ 					cmp . Comparer (( * Header ). equalHash ),
214+ 					cmp . Comparer (( * Transaction ). equalHash ),
297215				}
298216				if  diff  :=  cmp .Diff (body , got , opts ); diff  !=  ""  {
299217					t .Errorf ("rlp.DecodeBytes(%#x, [%T while carrying registered %T extra payload]) diff (-want +got):\n %s" , wantRLP , got , & extra , diff )
@@ -302,3 +220,24 @@ func TestBodyRLPCChainCompat(t *testing.T) {
302220		})
303221	}
304222}
223+ 
224+ // equalHash reports whether `a` and `b` have equal hashes. It allows for nil 
225+ // arguments, returning `true` if both are nil, `false` if only one is nil, 
226+ // otherwise `a.Hash() == b.Hash()`. 
227+ func  equalHash [
228+ 	T  any , P  interface  {
229+ 		Hash () common.Hash 
230+ 		* T 
231+ 	},
232+ ](a , b  P ) bool  {
233+ 	if  a  ==  nil  &&  b  ==  nil  {
234+ 		return  true 
235+ 	}
236+ 	if  a  ==  nil  ||  b  ==  nil  {
237+ 		return  false 
238+ 	}
239+ 	return  a .Hash () ==  b .Hash ()
240+ }
241+ 
242+ func  (h  * Header ) equalHash (hh  * Header ) bool            { return  equalHash (h , hh ) }
243+ func  (tx  * Transaction ) equalHash (u  * Transaction ) bool  { return  equalHash (tx , u ) }
0 commit comments