go-ethereum/libevm/pseudo/rlp_test.go
Arran Schlosberg 336a289f42
feat: pseudo.Type RLP round-tripping (#43)
All commits except the last two constitute PRs #43 and #44. The last two reverted files such that only changes to the `pseudo` and `ethtest` packages remain; once this is merged into the `libevm` branch then `libevm` will be merged into the branch for #44 too. Cherry-picking commits was not possible as some touched both halves of the changes; the squash-merges will, however, make this convoluted history irrelevant.

* feat: `types.StateAccount` pseudo-generic payload

* feat: registration of `StateAccount` payload type

* chore: mark `eth/tracers/logger` flaky

* chore: copyright header + `gci`

* test: lock default `types.SlimAccount` RLP encoding

* feat: `vm.SlimAccount.Extra` from `StateAccount` equiv

* chore: placate the linter

* test: `pseudo.Type.EncodeRLP()`

* test: `pseudo.Type.DecodeRLP()`

* fix: `pseudo.Type.DecodeRLP()` with non-pointer type

* feat: `pseudo.Type.IsZero()` and `Type.Equal(*Type)`

* feat: `types.StateAccountExtra.DecodeRLP()`

* chore: revert non-pseudo-package modifications

* chore: delete non-pseudo-package additions
2024-10-01 08:23:51 -07:00

86 lines
2.8 KiB
Go

// Copyright 2024 the libevm authors.
//
// The libevm additions to go-ethereum are free software: you can redistribute
// them and/or modify them under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// The libevm additions are distributed in the hope that they will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see
// <http://www.gnu.org/licenses/>.
package pseudo_test
import (
"math/big"
"testing"
"github.com/stretchr/testify/require"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/libevm/ethtest"
"github.com/ethereum/go-ethereum/libevm/pseudo"
"github.com/ethereum/go-ethereum/rlp"
)
func TestRLPEquivalence(t *testing.T) {
t.Parallel()
for seed := uint64(0); seed < 20; seed++ {
seed := seed
t.Run("fuzz pointer-type round trip", func(t *testing.T) {
t.Parallel()
rng := ethtest.NewPseudoRand(seed)
hdr := &types.Header{
ParentHash: rng.Hash(),
UncleHash: rng.Hash(),
Coinbase: rng.Address(),
Root: rng.Hash(),
TxHash: rng.Hash(),
ReceiptHash: rng.Hash(),
Difficulty: big.NewInt(rng.Int63()),
Number: big.NewInt(rng.Int63()),
GasLimit: rng.Uint64(),
GasUsed: rng.Uint64(),
Time: rng.Uint64(),
Extra: rng.Bytes(uint(rng.Uint64n(128))),
MixDigest: rng.Hash(),
}
rng.Read(hdr.Bloom[:])
rng.Read(hdr.Nonce[:])
want, err := rlp.EncodeToBytes(hdr)
require.NoErrorf(t, err, "rlp.EncodeToBytes(%T)", hdr)
typ := pseudo.From(hdr).Type
gotRLP, err := rlp.EncodeToBytes(typ)
require.NoErrorf(t, err, "rlp.EncodeToBytes(%T)", typ)
require.Equalf(t, want, gotRLP, "RLP encoding of %T (canonical) vs %T (under test)", hdr, typ)
t.Run("decode", func(t *testing.T) {
pseudo := pseudo.Zero[*types.Header]()
require.NoErrorf(t, rlp.DecodeBytes(gotRLP, pseudo.Type), "rlp.DecodeBytes(..., %T[%T])", pseudo.Type, hdr)
require.Equal(t, hdr, pseudo.Value.Get(), "RLP-decoded value")
})
})
t.Run("fuzz non-pointer decode", func(t *testing.T) {
rng := ethtest.NewPseudoRand(seed)
x := rng.Uint64()
buf, err := rlp.EncodeToBytes(x)
require.NoErrorf(t, err, "rlp.EncodeToBytes(%T)", x)
pseudo := pseudo.Zero[uint64]()
require.NoErrorf(t, rlp.DecodeBytes(buf, pseudo.Type), "rlp.DecodeBytes(..., %T[%T])", pseudo.Type, x)
require.Equal(t, x, pseudo.Value.Get(), "RLP-decoded value")
})
}
}