Skip to content

Commit 0db9528

Browse files
committed
test: lock default types.SlimAccount RLP encoding
1 parent 095415f commit 0db9528

File tree

1 file changed

+78
-5
lines changed

1 file changed

+78
-5
lines changed

core/types/state_account.libevm_test.go

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"strings"
2121
"testing"
2222

23+
"github.com/google/go-cmp/cmp"
24+
"github.com/google/go-cmp/cmp/cmpopts"
2325
"github.com/holiman/uint256"
2426
"github.com/stretchr/testify/require"
2527

@@ -118,13 +120,84 @@ func TestStateAccountRLP(t *testing.T) {
118120
registeredExtras = nil
119121
})
120122
}
123+
assertRLPEncodingAndReturn(t, tt.acc, tt.wantHex)
124+
})
125+
}
126+
}
127+
128+
func assertRLPEncodingAndReturn(t *testing.T, val any, wantHex string) []byte {
129+
t.Helper()
130+
got, err := rlp.EncodeToBytes(val)
131+
require.NoError(t, err, "rlp.EncodeToBytes()")
132+
133+
t.Logf("got RLP: %#x", got)
134+
wantHex = strings.TrimPrefix(wantHex, "0x")
135+
require.Equalf(t, common.Hex2Bytes(wantHex), got, "RLP encoding of %T", val)
136+
137+
return got
138+
}
139+
140+
func TestSlimAccountRLP(t *testing.T) {
141+
// All RLP encodings were generated on geth SlimAccounts *before* libevm
142+
// modifications, to lock in default behaviour.
143+
tests := []struct {
144+
name string
145+
acc SlimAccount
146+
wantHex string
147+
}{
148+
{
149+
acc: SlimAccount{
150+
Nonce: 0x444444,
151+
Balance: uint256.NewInt(0x777777),
152+
},
153+
wantHex: `0xca83444444837777778080`,
154+
},
155+
{
156+
acc: SlimAccount{
157+
Nonce: 0x444444,
158+
Balance: uint256.NewInt(0x777777),
159+
Root: common.MaxHash[:],
160+
},
161+
wantHex: `0xea8344444483777777a0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80`,
162+
},
163+
{
164+
acc: SlimAccount{
165+
Nonce: 0x444444,
166+
Balance: uint256.NewInt(0x777777),
167+
CodeHash: common.MaxHash[:],
168+
},
169+
wantHex: `0xea834444448377777780a0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff`,
170+
},
171+
{
172+
acc: SlimAccount{
173+
Nonce: 0x444444,
174+
Balance: uint256.NewInt(0x777777),
175+
Root: common.MaxHash[:],
176+
CodeHash: repeatAsHash(0xee).Bytes(),
177+
},
178+
wantHex: `0xf84a8344444483777777a0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa0eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee`,
179+
},
180+
}
121181

122-
got, err := rlp.EncodeToBytes(tt.acc)
123-
require.NoError(t, err)
124-
t.Logf("got: %#x", got)
182+
for _, tt := range tests {
183+
t.Run(tt.name, func(t *testing.T) {
184+
buf := assertRLPEncodingAndReturn(t, tt.acc, tt.wantHex)
125185

126-
tt.wantHex = strings.TrimPrefix(tt.wantHex, "0x")
127-
require.Equal(t, common.Hex2Bytes(tt.wantHex), got)
186+
var got SlimAccount
187+
require.NoError(t, rlp.DecodeBytes(buf, &got), "rlp.DecodeBytes()")
188+
189+
// The require package differentiates between empty and nil slices
190+
// and doesn't have a configuration mechanism.
191+
if diff := cmp.Diff(tt.acc, got, cmpopts.EquateEmpty()); diff != "" {
192+
t.Errorf("rlp.DecodeBytes(rlp.EncodeToBytes(%T), ...) round trip; diff (-want +got):\n%s", tt.acc, diff)
193+
}
128194
})
129195
}
130196
}
197+
198+
func repeatAsHash(x byte) (h common.Hash) {
199+
for i := range h {
200+
h[i] = x
201+
}
202+
return h
203+
}

0 commit comments

Comments
 (0)