Commit 37739095 authored by Adrian Sutton's avatar Adrian Sutton

op-batcher: Fix intermittency in TestChannelBuilder_PendingFrames_TotalFrames

Different random seeds would result in varying compression levels which caused intermittency. Now uses a fixed seed for the rng and InsecureRandomKey is now deterministic.
parent a83387cc
...@@ -8,14 +8,12 @@ import ( ...@@ -8,14 +8,12 @@ import (
"math/big" "math/big"
"math/rand" "math/rand"
"testing" "testing"
"time"
"github.com/ethereum-optimism/optimism/op-batcher/compressor" "github.com/ethereum-optimism/optimism/op-batcher/compressor"
"github.com/ethereum-optimism/optimism/op-node/eth" "github.com/ethereum-optimism/optimism/op-node/eth"
"github.com/ethereum-optimism/optimism/op-node/rollup" "github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive" "github.com/ethereum-optimism/optimism/op-node/rollup/derive"
dtest "github.com/ethereum-optimism/optimism/op-node/rollup/derive/test" dtest "github.com/ethereum-optimism/optimism/op-node/rollup/derive/test"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
...@@ -686,7 +684,7 @@ func TestFramePublished(t *testing.T) { ...@@ -686,7 +684,7 @@ func TestFramePublished(t *testing.T) {
func TestChannelBuilder_PendingFrames_TotalFrames(t *testing.T) { func TestChannelBuilder_PendingFrames_TotalFrames(t *testing.T) {
const tnf = 8 const tnf = 8
rng := rand.New(rand.NewSource(time.Now().UnixNano())) rng := rand.New(rand.NewSource(94572314))
require := require.New(t) require := require.New(t)
cfg := defaultTestChannelConfig cfg := defaultTestChannelConfig
cfg.CompressorConfig.TargetFrameSize = 1000 cfg.CompressorConfig.TargetFrameSize = 1000
...@@ -729,7 +727,7 @@ func TestChannelBuilder_PendingFrames_TotalFrames(t *testing.T) { ...@@ -729,7 +727,7 @@ func TestChannelBuilder_PendingFrames_TotalFrames(t *testing.T) {
func TestChannelBuilder_InputBytes(t *testing.T) { func TestChannelBuilder_InputBytes(t *testing.T) {
require := require.New(t) require := require.New(t)
rng := rand.New(rand.NewSource(time.Now().UnixNano())) rng := rand.New(rand.NewSource(4982432))
cb, _ := defaultChannelBuilderSetup(t) cb, _ := defaultChannelBuilderSetup(t)
require.Zero(cb.InputBytes()) require.Zero(cb.InputBytes())
...@@ -747,7 +745,7 @@ func TestChannelBuilder_InputBytes(t *testing.T) { ...@@ -747,7 +745,7 @@ func TestChannelBuilder_InputBytes(t *testing.T) {
func TestChannelBuilder_OutputBytes(t *testing.T) { func TestChannelBuilder_OutputBytes(t *testing.T) {
require := require.New(t) require := require.New(t)
rng := rand.New(rand.NewSource(time.Now().UnixNano())) rng := rand.New(rand.NewSource(9860372))
cfg := defaultTestChannelConfig cfg := defaultTestChannelConfig
cfg.CompressorConfig.TargetFrameSize = 1000 cfg.CompressorConfig.TargetFrameSize = 1000
cfg.MaxFrameSize = 1000 cfg.MaxFrameSize = 1000
......
...@@ -2,16 +2,16 @@ package testutils ...@@ -2,16 +2,16 @@ package testutils
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"fmt"
"math/big" "math/big"
"math/rand" "math/rand"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum-optimism/optimism/op-node/eth" "github.com/ethereum-optimism/optimism/op-node/eth"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/trie"
) )
func RandomBool(rng *rand.Rand) bool { func RandomBool(rng *rand.Rand) bool {
...@@ -102,10 +102,14 @@ func NextRandomL2Ref(rng *rand.Rand, l2BlockTime uint64, parent eth.L2BlockRef, ...@@ -102,10 +102,14 @@ func NextRandomL2Ref(rng *rand.Rand, l2BlockTime uint64, parent eth.L2BlockRef,
} }
} }
// InsecureRandomKey returns a random private key from a limited set of keys.
// Output is deterministic when the supplied rng generates the same random sequence.
func InsecureRandomKey(rng *rand.Rand) *ecdsa.PrivateKey { func InsecureRandomKey(rng *rand.Rand) *ecdsa.PrivateKey {
key, err := ecdsa.GenerateKey(crypto.S256(), rng) idx := rng.Intn(len(randomEcdsaKeys))
key, err := crypto.ToECDSA(common.Hex2Bytes(randomEcdsaKeys[idx]))
if err != nil { if err != nil {
panic(err) // Should never happen because the list of keys is hard coded and known to be valid.
panic(fmt.Errorf("invalid pre-generated ecdsa key at index %v: %w", idx, err))
} }
return key return key
} }
......
This diff is collapsed.
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