Skip to content

Commit

Permalink
refactor: inline generation of random bloom, nonce, and extra bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
ARR4N committed Dec 12, 2024
1 parent b688ff7 commit 4c3534c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
23 changes: 10 additions & 13 deletions core/types/rlp_backwards_compat.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,27 @@ func TestHeaderRLPBackwardsCompatibility(t *testing.T) {
Root: rng.Hash(),
TxHash: rng.Hash(),
ReceiptHash: rng.Hash(),
// Bloom populated below
Difficulty: rng.Uint256().ToBig(),
Number: rng.BigUint64(),
GasLimit: rng.Uint64(),
GasUsed: rng.Uint64(),
Time: rng.Uint64(),
Extra: make([]byte, numExtraBytes), // populated below
MixDigest: rng.Hash(),
// Nonce populated below
Bloom: rng.Bloom(),
Difficulty: rng.Uint256().ToBig(),
Number: rng.BigUint64(),
GasLimit: rng.Uint64(),
GasUsed: rng.Uint64(),
Time: rng.Uint64(),
Extra: rng.Bytes(numExtraBytes),
MixDigest: rng.Hash(),
Nonce: rng.BlockNonce(),

BaseFee: rng.BigUint64(),
WithdrawalsHash: rng.HashPtr(),
BlobGasUsed: rng.Uint64Ptr(),
ExcessBlobGas: rng.Uint64Ptr(),
ParentBeaconRoot: rng.HashPtr(),
}
require.Equal(t, BloomByteLength, rng.Read(hdr.Bloom[:]))
require.Equal(t, len(BlockNonce{}), rng.Read(hdr.Nonce[:]))
require.Equal(t, numExtraBytes, rng.Read(hdr.Extra))
t.Logf("%T:\n%+v", hdr, hdr)

// WARNING: changing this hex might break backwards compatibility of RLP
// encoding (i.e. block hashes might change)!
const wantHex = `f9029aa01a571e7e4d774caf46053201cfe0001b3c355ffcc93f510e671e8809741f0eeda0756095410506ec72a2c287fe83ebf68efb0be177e61acec1c985277e90e52087941bfc3bc193012ba58912c01fb35a3454831a8971a00bc9f064144eb5965c5e5d1020f9f90392e7e06ded9225966abc7c754b410e61a0d942eab201424f4320ec1e1ffa9390baf941629b9349977b5d48e0502dbb9386a035d9d550a9c113f78689b4c161c4605609bb57b83061914c42ad244daa7fc38eb90100718d155798390a6c6782181d1bac1dd64cd956332b008412ddc735f2994e297c8a088c6bb4c637542295ba3cbc3cd399c8127076f4d834d74d5b11a36b6d02e2fe3a583216aa4ccef052df9a96e7a454256bebabdfc38c429079f25913e0f1d7416b2f056c4a115fc757012b1757d2d69f0e5fb87c08605098d9031fa37cd0df6942c5a2da12a4424b978febf5479896165caf573cf82fb3aa10f6ebf6b62bef8ed36b8ea3d4b1ddb80c99afafa37cb8f3393eb6d802f5bc6c8cd6bcd168a7e0061a718218b848d945135b6dff228a4e66bade4717e6f4d318ac98fca12a053af6f98805a764fb5d523cb6f69029522cab9ced907cc75718f7e2c79154ef3fc7a04b31d39ae246d689f23176d679a62ff328f530407cbafd0146f45b2ed635282e88b36f6a5752feff5b881fc7fa9ef217f81d889f073433138e6ba58857515405d28f2a8e904bcda3066d382675f37dd1a18507b5fba02812f2701021506f27190adb52a1313f6d28c77d66ae1aa3d3d6757a762476f488294c7768cddd9ccf881b5da1b6a47970a3a0c8a2b7b2c44161190c82d5e1c8b55e05c7354f1e5f6512924c941fb3d93667dc889bc9df25654e163c88859405c51041475fa03a8c304a732153e20300c3482832d07b65f97958360da414cb438ce252aec6c2`
const wantHex = `f9029aa01a571e7e4d774caf46053201cfe0001b3c355ffcc93f510e671e8809741f0eeda0756095410506ec72a2c287fe83ebf68efb0be177e61acec1c985277e90e52087941bfc3bc193012ba58912c01fb35a3454831a8971a00bc9f064144eb5965c5e5d1020f9f90392e7e06ded9225966abc7c754b410e61a0d942eab201424f4320ec1e1ffa9390baf941629b9349977b5d48e0502dbb9386a035d9d550a9c113f78689b4c161c4605609bb57b83061914c42ad244daa7fc38eb901004b31d39ae246d689f23176d679a62ff328f530407cbafd0146f45b2ed635282e2812f2705bfffe52576a6fb31df817f29efac71fa56b8e133334079f8e2a8fd2055451571021506f27190adb52a1313f6d28c77d66ae1aa3d3d6757a762476f4c8a2b7b2a37079a4b6a15d1bc44161190c82d5e1c8b55e05c7354f1e5f6512924c941fb3d93667dc3a8c304a3c164e6525dfc99b5f474110c5059485732153e20300c3482832d07b65f97958360da414cb438ce252aec6c2718d155798390a6c6782181d1bac1dd64cd956332b008412ddc735f2994e297c8a088c6bb4c637542295ba3cbc3cd399c8127076f4d834d74d5b11a36b6d02e2fe3a583216aa4ccea0f052df9a96e7a454256bebabdfc38c429079f25913e0f1d7416b2f056c4a115f88b85f0e9fd6d25717881f03d9985060087c88a2c54269dfd07ca388eb8f974b42a412da90c757012bf5479896165caf573cf82fb3a0aa10f6ebf6b62bef8ed36b8ea3d4b1ddb80c99afafa37cb8f3393eb6d802f5bc886c8cd6bcd168a7e0886d5b1345d948b818a0061a7182ff228a4e66bade4717e6f4d318ac98fca12a053af6f98805a764fb5d8890ed9cab2c5229908891c7e2f71857c77ca0523cb6f654ef3fc7294c7768cddd9ccf4bcda3066d382675f37dd1a18507b5fb`
want, err := hex.DecodeString(wantHex)
require.NoError(t, err, "hex.DecodeString()")

Expand Down
13 changes: 13 additions & 0 deletions libevm/ethtest/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"golang.org/x/exp/rand"

"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/types"
)

// PseudoRand extends [rand.Rand] (*not* crypto/rand).
Expand Down Expand Up @@ -88,3 +89,15 @@ func (r *PseudoRand) Uint64Ptr() *uint64 {
func (r *PseudoRand) Uint256() *uint256.Int {
return new(uint256.Int).SetBytes(r.Bytes(32))
}

// Bloom returns a pseudorandom Bloom.
func (r *PseudoRand) Bloom() (b types.Bloom) {
r.Read(b[:])
return b
}

// BlockNonce returns a pseudorandom BlockNonce.
func (r *PseudoRand) BlockNonce() (n types.BlockNonce) {
r.Read(n[:])
return n
}

0 comments on commit 4c3534c

Please sign in to comment.