Skip to content

Commit b688ff7

Browse files
committed
test: lock in types.Header RLP encoding
1 parent aa183c5 commit b688ff7

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2024 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+
// This is a deliberate change-detector test that locks in backwards
33+
// compatibility of RLP encoding.
34+
rng := ethtest.NewPseudoRand(42)
35+
36+
const numExtraBytes = 16
37+
hdr := &Header{
38+
ParentHash: rng.Hash(),
39+
UncleHash: rng.Hash(),
40+
Coinbase: rng.Address(),
41+
Root: rng.Hash(),
42+
TxHash: rng.Hash(),
43+
ReceiptHash: rng.Hash(),
44+
// Bloom populated below
45+
Difficulty: rng.Uint256().ToBig(),
46+
Number: rng.BigUint64(),
47+
GasLimit: rng.Uint64(),
48+
GasUsed: rng.Uint64(),
49+
Time: rng.Uint64(),
50+
Extra: make([]byte, numExtraBytes), // populated below
51+
MixDigest: rng.Hash(),
52+
// Nonce populated below
53+
54+
BaseFee: rng.BigUint64(),
55+
WithdrawalsHash: rng.HashPtr(),
56+
BlobGasUsed: rng.Uint64Ptr(),
57+
ExcessBlobGas: rng.Uint64Ptr(),
58+
ParentBeaconRoot: rng.HashPtr(),
59+
}
60+
require.Equal(t, BloomByteLength, rng.Read(hdr.Bloom[:]))
61+
require.Equal(t, len(BlockNonce{}), rng.Read(hdr.Nonce[:]))
62+
require.Equal(t, numExtraBytes, rng.Read(hdr.Extra))
63+
t.Logf("%T:\n%+v", hdr, hdr)
64+
65+
// WARNING: changing this hex might break backwards compatibility of RLP
66+
// encoding (i.e. block hashes might change)!
67+
const wantHex = `f9029aa01a571e7e4d774caf46053201cfe0001b3c355ffcc93f510e671e8809741f0eeda0756095410506ec72a2c287fe83ebf68efb0be177e61acec1c985277e90e52087941bfc3bc193012ba58912c01fb35a3454831a8971a00bc9f064144eb5965c5e5d1020f9f90392e7e06ded9225966abc7c754b410e61a0d942eab201424f4320ec1e1ffa9390baf941629b9349977b5d48e0502dbb9386a035d9d550a9c113f78689b4c161c4605609bb57b83061914c42ad244daa7fc38eb90100718d155798390a6c6782181d1bac1dd64cd956332b008412ddc735f2994e297c8a088c6bb4c637542295ba3cbc3cd399c8127076f4d834d74d5b11a36b6d02e2fe3a583216aa4ccef052df9a96e7a454256bebabdfc38c429079f25913e0f1d7416b2f056c4a115fc757012b1757d2d69f0e5fb87c08605098d9031fa37cd0df6942c5a2da12a4424b978febf5479896165caf573cf82fb3aa10f6ebf6b62bef8ed36b8ea3d4b1ddb80c99afafa37cb8f3393eb6d802f5bc6c8cd6bcd168a7e0061a718218b848d945135b6dff228a4e66bade4717e6f4d318ac98fca12a053af6f98805a764fb5d523cb6f69029522cab9ced907cc75718f7e2c79154ef3fc7a04b31d39ae246d689f23176d679a62ff328f530407cbafd0146f45b2ed635282e88b36f6a5752feff5b881fc7fa9ef217f81d889f073433138e6ba58857515405d28f2a8e904bcda3066d382675f37dd1a18507b5fba02812f2701021506f27190adb52a1313f6d28c77d66ae1aa3d3d6757a762476f488294c7768cddd9ccf881b5da1b6a47970a3a0c8a2b7b2c44161190c82d5e1c8b55e05c7354f1e5f6512924c941fb3d93667dc889bc9df25654e163c88859405c51041475fa03a8c304a732153e20300c3482832d07b65f97958360da414cb438ce252aec6c2`
68+
want, err := hex.DecodeString(wantHex)
69+
require.NoError(t, err, "hex.DecodeString()")
70+
71+
got, err := rlp.EncodeToBytes(hdr)
72+
require.NoErrorf(t, err, "rlp.EncodeToBytes(%T)", hdr)
73+
assert.Equalf(t, want, got, "rlp.EncodeToBytes(%T)", hdr)
74+
}

0 commit comments

Comments
 (0)