Skip to content

Commit 7494fa3

Browse files
committed
refactor(core/types): move Body backwards-compatibility tests into package
1 parent 67f879a commit 7494fa3

File tree

3 files changed

+134
-153
lines changed

3 files changed

+134
-153
lines changed

core/types/rlp_backwards_compat.libevm_test.go renamed to core/types/backwards_compat.libevm_test.go

Lines changed: 26 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
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

1919
import (
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-
11432
func 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) }
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright 2024-2025 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
16+
17+
package types_test
18+
19+
import (
20+
"encoding/hex"
21+
"testing"
22+
23+
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/require"
25+
26+
. "github.com/ava-labs/libevm/core/types"
27+
"github.com/ava-labs/libevm/libevm/ethtest"
28+
"github.com/ava-labs/libevm/rlp"
29+
)
30+
31+
func TestHeaderRLPBackwardsCompatibility(t *testing.T) {
32+
tests := []struct {
33+
name string
34+
register func()
35+
}{
36+
{
37+
name: "no registered extras",
38+
register: func() {},
39+
},
40+
{
41+
name: "no-op header hooks",
42+
register: func() {
43+
RegisterExtras[NOOPHeaderHooks, *NOOPHeaderHooks, struct{}]()
44+
},
45+
},
46+
}
47+
48+
for _, tt := range tests {
49+
t.Run(tt.name, func(t *testing.T) {
50+
TestOnlyClearRegisteredExtras()
51+
defer TestOnlyClearRegisteredExtras()
52+
tt.register()
53+
testHeaderRLPBackwardsCompatibility(t)
54+
})
55+
}
56+
}
57+
58+
//nolint:thelper
59+
func testHeaderRLPBackwardsCompatibility(t *testing.T) {
60+
// This is a deliberate change-detector test that locks in backwards
61+
// compatibility of RLP encoding.
62+
rng := ethtest.NewPseudoRand(42)
63+
64+
const numExtraBytes = 16
65+
hdr := &Header{
66+
ParentHash: rng.Hash(),
67+
UncleHash: rng.Hash(),
68+
Coinbase: rng.Address(),
69+
Root: rng.Hash(),
70+
TxHash: rng.Hash(),
71+
ReceiptHash: rng.Hash(),
72+
Bloom: rng.Bloom(),
73+
Difficulty: rng.Uint256().ToBig(),
74+
Number: rng.BigUint64(),
75+
GasLimit: rng.Uint64(),
76+
GasUsed: rng.Uint64(),
77+
Time: rng.Uint64(),
78+
Extra: rng.Bytes(numExtraBytes),
79+
MixDigest: rng.Hash(),
80+
Nonce: rng.BlockNonce(),
81+
82+
BaseFee: rng.BigUint64(),
83+
WithdrawalsHash: rng.HashPtr(),
84+
BlobGasUsed: rng.Uint64Ptr(),
85+
ExcessBlobGas: rng.Uint64Ptr(),
86+
ParentBeaconRoot: rng.HashPtr(),
87+
}
88+
t.Logf("%T:\n%+v", hdr, hdr)
89+
90+
// WARNING: changing this hex might break backwards compatibility of RLP
91+
// encoding (i.e. block hashes might change)!
92+
const wantHex = `f9029aa01a571e7e4d774caf46053201cfe0001b3c355ffcc93f510e671e8809741f0eeda0756095410506ec72a2c287fe83ebf68efb0be177e61acec1c985277e90e52087941bfc3bc193012ba58912c01fb35a3454831a8971a00bc9f064144eb5965c5e5d1020f9f90392e7e06ded9225966abc7c754b410e61a0d942eab201424f4320ec1e1ffa9390baf941629b9349977b5d48e0502dbb9386a035d9d550a9c113f78689b4c161c4605609bb57b83061914c42ad244daa7fc38eb901004b31d39ae246d689f23176d679a62ff328f530407cbafd0146f45b2ed635282e2812f2705bfffe52576a6fb31df817f29efac71fa56b8e133334079f8e2a8fd2055451571021506f27190adb52a1313f6d28c77d66ae1aa3d3d6757a762476f4c8a2b7b2a37079a4b6a15d1bc44161190c82d5e1c8b55e05c7354f1e5f6512924c941fb3d93667dc3a8c304a3c164e6525dfc99b5f474110c5059485732153e20300c3482832d07b65f97958360da414cb438ce252aec6c2718d155798390a6c6782181d1bac1dd64cd956332b008412ddc735f2994e297c8a088c6bb4c637542295ba3cbc3cd399c8127076f4d834d74d5b11a36b6d02e2fe3a583216aa4ccea0f052df9a96e7a454256bebabdfc38c429079f25913e0f1d7416b2f056c4a115f88b85f0e9fd6d25717881f03d9985060087c88a2c54269dfd07ca388eb8f974b42a412da90c757012bf5479896165caf573cf82fb3a0aa10f6ebf6b62bef8ed36b8ea3d4b1ddb80c99afafa37cb8f3393eb6d802f5bc886c8cd6bcd168a7e0886d5b1345d948b818a0061a7182ff228a4e66bade4717e6f4d318ac98fca12a053af6f98805a764fb5d8890ed9cab2c5229908891c7e2f71857c77ca0523cb6f654ef3fc7294c7768cddd9ccf4bcda3066d382675f37dd1a18507b5fb`
93+
wantRLP, err := hex.DecodeString(wantHex)
94+
require.NoError(t, err, "hex.DecodeString()")
95+
96+
t.Run("Encode", func(t *testing.T) {
97+
got, err := rlp.EncodeToBytes(hdr)
98+
require.NoErrorf(t, err, "rlp.EncodeToBytes(%T)", hdr)
99+
assert.Equalf(t, wantRLP, got, "rlp.EncodeToBytes(%T)", hdr)
100+
})
101+
102+
t.Run("Decode", func(t *testing.T) {
103+
got := new(Header)
104+
err := rlp.DecodeBytes(wantRLP, got)
105+
require.NoErrorf(t, err, "rlp.DecodeBytes(..., %T)", hdr)
106+
assert.Equal(t, hdr, got)
107+
})
108+
}

libevm/cmpeth/cmpeth.go

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)