|  | 
|  | 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 pseudo_test | 
|  | 18 | + | 
|  | 19 | +import ( | 
|  | 20 | +	"math/big" | 
|  | 21 | +	"testing" | 
|  | 22 | + | 
|  | 23 | +	"github.com/stretchr/testify/require" | 
|  | 24 | + | 
|  | 25 | +	"github.com/ethereum/go-ethereum/core/types" | 
|  | 26 | +	"github.com/ethereum/go-ethereum/libevm/ethtest" | 
|  | 27 | +	"github.com/ethereum/go-ethereum/libevm/pseudo" | 
|  | 28 | +	"github.com/ethereum/go-ethereum/rlp" | 
|  | 29 | +) | 
|  | 30 | + | 
|  | 31 | +func TestRLPEquivalence(t *testing.T) { | 
|  | 32 | +	t.Parallel() | 
|  | 33 | + | 
|  | 34 | +	for seed := uint64(0); seed < 20; seed++ { | 
|  | 35 | +		rng := ethtest.NewPseudoRand(seed) | 
|  | 36 | + | 
|  | 37 | +		t.Run("fuzz", func(t *testing.T) { | 
|  | 38 | +			t.Parallel() | 
|  | 39 | + | 
|  | 40 | +			hdr := &types.Header{ | 
|  | 41 | +				ParentHash:  rng.Hash(), | 
|  | 42 | +				UncleHash:   rng.Hash(), | 
|  | 43 | +				Coinbase:    rng.Address(), | 
|  | 44 | +				Root:        rng.Hash(), | 
|  | 45 | +				TxHash:      rng.Hash(), | 
|  | 46 | +				ReceiptHash: rng.Hash(), | 
|  | 47 | +				Difficulty:  big.NewInt(rng.Int63()), | 
|  | 48 | +				Number:      big.NewInt(rng.Int63()), | 
|  | 49 | +				GasLimit:    rng.Uint64(), | 
|  | 50 | +				GasUsed:     rng.Uint64(), | 
|  | 51 | +				Time:        rng.Uint64(), | 
|  | 52 | +				Extra:       rng.Bytes(uint(rng.Uint64n(128))), | 
|  | 53 | +				MixDigest:   rng.Hash(), | 
|  | 54 | +			} | 
|  | 55 | +			rng.Read(hdr.Bloom[:]) | 
|  | 56 | +			rng.Read(hdr.Nonce[:]) | 
|  | 57 | + | 
|  | 58 | +			want, err := rlp.EncodeToBytes(hdr) | 
|  | 59 | +			require.NoErrorf(t, err, "rlp.EncodeToBytes(%T)", hdr) | 
|  | 60 | + | 
|  | 61 | +			typ := pseudo.From(hdr).Type | 
|  | 62 | +			got, err := rlp.EncodeToBytes(typ) | 
|  | 63 | +			require.NoErrorf(t, err, "rlp.EncodeToBytes(%T)", typ) | 
|  | 64 | + | 
|  | 65 | +			require.Equalf(t, want, got, "RLP encoding of %T (canonical) vs %T (under test)", hdr, typ) | 
|  | 66 | +		}) | 
|  | 67 | +	} | 
|  | 68 | +} | 
0 commit comments