Skip to content

Commit dd502c5

Browse files
committed
test(core/types): lock in Body RLP encoding
1 parent d08d0f0 commit dd502c5

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

core/types/rlp_backwards_compat.libevm_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,67 @@ func testHeaderRLPBackwardsCompatibility(t *testing.T) {
106106
assert.Equal(t, hdr, got)
107107
})
108108
}
109+
110+
func TestBodyRLPBackwardsCompatibility(t *testing.T) {
111+
rng := ethtest.NewPseudoRand(0)
112+
113+
body := new(Body)
114+
for i, n := 0, rng.Intn(3)+1; i < n; i++ {
115+
tx := NewTx(&LegacyTx{
116+
Nonce: rng.Uint64(),
117+
})
118+
body.Transactions = append(body.Transactions, tx)
119+
}
120+
for i, n := 0, rng.Intn(3)+1; i < n; i++ {
121+
body.Uncles = append(body.Uncles, &Header{
122+
ParentHash: rng.Hash(),
123+
})
124+
}
125+
126+
var withdrawals Withdrawals
127+
for i, n := 0, rng.Intn(3)+1; i < n; i++ {
128+
withdrawals = append(withdrawals, &Withdrawal{
129+
Index: rng.Uint64(),
130+
})
131+
}
132+
133+
tests := []struct {
134+
name string
135+
withdrawals []*Withdrawal
136+
// WARNING: changing these hex values might break backwards
137+
// compatibility of RLP encoding!
138+
wantHex string
139+
}{
140+
{
141+
name: "without_Withdrawals",
142+
wantHex: `f903f6d2d1884d98b91b8d3568708080808080808080f903e0f901eda093449fc540c83e70fa443a4b915449e55e28b904f20f13961ab2ce35f5de9f7da00000000000000000000000000000000000000000000000000000000000000000940000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000808080808080a00000000000000000000000000000000000000000000000000000000000000000880000000000000000f901eda0a019122ed4ee6f666f32c82157681f98da4dab6e0d7180ad29a037b080c402e2a00000000000000000000000000000000000000000000000000000000000000000940000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000808080808080a00000000000000000000000000000000000000000000000000000000000000000880000000000000000`,
143+
},
144+
{
145+
name: "with_Withdrawals",
146+
withdrawals: withdrawals,
147+
wantHex: `f90418d2d1884d98b91b8d3568708080808080808080f903e0f901eda093449fc540c83e70fa443a4b915449e55e28b904f20f13961ab2ce35f5de9f7da00000000000000000000000000000000000000000000000000000000000000000940000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000808080808080a00000000000000000000000000000000000000000000000000000000000000000880000000000000000f901eda0a019122ed4ee6f666f32c82157681f98da4dab6e0d7180ad29a037b080c402e2a00000000000000000000000000000000000000000000000000000000000000000940000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000808080808080a00000000000000000000000000000000000000000000000000000000000000000880000000000000000e1e0880756befb39c6b8aa8094000000000000000000000000000000000000000080`,
148+
},
149+
}
150+
151+
for _, tt := range tests {
152+
t.Run(tt.name, func(t *testing.T) {
153+
body.Withdrawals = tt.withdrawals
154+
155+
wantRLP, err := hex.DecodeString(tt.wantHex)
156+
require.NoError(t, err, "hex.DecodeString()")
157+
158+
t.Run("Encode", func(t *testing.T) {
159+
got, err := rlp.EncodeToBytes(body)
160+
require.NoErrorf(t, err, "rlp.EncodeToBytes(%T)", body)
161+
t.Logf("got RLP: %#x", got)
162+
assert.Equalf(t, wantRLP, got, "rlp.EncodeToBytes(%T)", body)
163+
})
164+
165+
t.Run("Decode", func(t *testing.T) {
166+
got := new(Body)
167+
err := rlp.DecodeBytes(wantRLP, got)
168+
require.NoErrorf(t, err, "rlp.DecodeBytes(..., %T)", got)
169+
})
170+
})
171+
}
172+
}

0 commit comments

Comments
 (0)