Commit a97c1e7d authored by Sebastian Stammler's avatar Sebastian Stammler

op-node/rollup/derive/test: Add RandomL2Block

Also adds a function RandomBlockPrependTxs to package op-node/testutils
to avoid code duplication with RandomBlock.
parent 05503827
package test
import (
"math/rand"
"github.com/ethereum-optimism/optimism/op-node/eth"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
"github.com/ethereum-optimism/optimism/op-node/testutils"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/trie"
)
// RandomL2Block returns a random block whose first transaction is a random
// L1 Info Deposit transaction.
func RandomL2Block(rng *rand.Rand, txCount int) (*types.Block, []*types.Receipt) {
l1Block := types.NewBlock(testutils.RandomHeader(rng),
nil, nil, nil, trie.NewStackTrie(nil))
l1InfoTx, err := derive.L1InfoDeposit(0, l1Block, eth.SystemConfig{}, testutils.RandomBool(rng))
if err != nil {
panic("L1InfoDeposit: " + err.Error())
}
return testutils.RandomBlockPrependTxs(rng, txCount, types.NewTx(l1InfoTx))
}
......@@ -206,13 +206,21 @@ func RandomHeader(rng *rand.Rand) *types.Header {
}
func RandomBlock(rng *rand.Rand, txCount uint64) (*types.Block, []*types.Receipt) {
return RandomBlockPrependTxs(rng, int(txCount))
}
// RandomBlockPrependTxs returns a random block with txCount randomly generated
// transactions and additionally the transactions ptxs prepended. So the total
// number of transactions is len(ptxs) + txCount.
func RandomBlockPrependTxs(rng *rand.Rand, txCount int, ptxs ...*types.Transaction) (*types.Block, []*types.Receipt) {
header := RandomHeader(rng)
signer := types.NewLondonSigner(big.NewInt(rng.Int63n(1000)))
txs := make([]*types.Transaction, 0, txCount)
for i := uint64(0); i < txCount; i++ {
txs := make([]*types.Transaction, 0, txCount+len(ptxs))
txs = append(txs, ptxs...)
for i := 0; i < txCount; i++ {
txs = append(txs, RandomTx(rng, header.BaseFee, signer))
}
receipts := make([]*types.Receipt, 0, txCount)
receipts := make([]*types.Receipt, 0, len(txs))
cumulativeGasUsed := uint64(0)
for i, tx := range txs {
r := RandomReceipt(rng, signer, tx, uint64(i), cumulativeGasUsed)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment