Commit 884af5ed authored by Maurelian's avatar Maurelian

op-node: Update ToB tests to current implementation

op-node: Use assert rather than require
parent 63072ca1
...@@ -606,6 +606,7 @@ google.golang.org/genproto v0.0.0-20220429170224-98d788798c3e/go.mod h1:8w6bsBMX ...@@ -606,6 +606,7 @@ google.golang.org/genproto v0.0.0-20220429170224-98d788798c3e/go.mod h1:8w6bsBMX
google.golang.org/genproto v0.0.0-20220505152158-f39f71e6c8f3/go.mod h1:RAyBrSAP7Fh3Nc84ghnVLDPuV51xc9agzmm4Ph6i0Q4= google.golang.org/genproto v0.0.0-20220505152158-f39f71e6c8f3/go.mod h1:RAyBrSAP7Fh3Nc84ghnVLDPuV51xc9agzmm4Ph6i0Q4=
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd/go.mod h1:RAyBrSAP7Fh3Nc84ghnVLDPuV51xc9agzmm4Ph6i0Q4= google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd/go.mod h1:RAyBrSAP7Fh3Nc84ghnVLDPuV51xc9agzmm4Ph6i0Q4=
google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0= google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0=
google.golang.org/grpc v1.33.2 h1:EQyQC3sa8M+p6Ulc8yy9SWSS2GVwyRc83gAbG8lrl4o=
google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc=
google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU=
google.golang.org/grpc v1.36.1/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= google.golang.org/grpc v1.36.1/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU=
......
...@@ -5,7 +5,7 @@ import ( ...@@ -5,7 +5,7 @@ import (
"github.com/ethereum-optimism/optimism/op-node/testutils/fuzzerutils" "github.com/ethereum-optimism/optimism/op-node/testutils/fuzzerutils"
fuzz "github.com/google/gofuzz" fuzz "github.com/google/gofuzz"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/require"
) )
// FuzzBatchRoundTrip executes a fuzz test similar to TestBatchRoundTrip, which tests that arbitrary BatchData will be // FuzzBatchRoundTrip executes a fuzz test similar to TestBatchRoundTrip, which tests that arbitrary BatchData will be
...@@ -22,14 +22,14 @@ func FuzzBatchRoundTrip(f *testing.F) { ...@@ -22,14 +22,14 @@ func FuzzBatchRoundTrip(f *testing.F) {
// Encode our batch data // Encode our batch data
enc, err := batchData.MarshalBinary() enc, err := batchData.MarshalBinary()
assert.NoError(t, err) require.NoError(t, err)
// Decode our encoded batch data // Decode our encoded batch data
var dec BatchData var dec BatchData
err = dec.UnmarshalBinary(enc) err = dec.UnmarshalBinary(enc)
assert.NoError(t, err) require.NoError(t, err)
// Ensure the round trip encoding of batch data did not result in data loss // Ensure the round trip encoding of batch data did not result in data loss
assert.Equal(t, &batchData, &dec, "round trip batch encoding/decoding did not match original values") require.Equal(t, &batchData, &dec, "round trip batch encoding/decoding did not match original values")
}) })
} }
package derive package derive
import ( import (
"math/big"
"testing"
"github.com/ethereum-optimism/optimism/op-node/testutils" "github.com/ethereum-optimism/optimism/op-node/testutils"
"github.com/ethereum-optimism/optimism/op-node/testutils/fuzzerutils" "github.com/ethereum-optimism/optimism/op-node/testutils/fuzzerutils"
"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"
fuzz "github.com/google/gofuzz" fuzz "github.com/google/gofuzz"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"math/big"
"testing"
) )
// fuzzReceipts is similar to makeReceipts except it uses the fuzzer to populate DepositTx fields. // fuzzReceipts is similar to makeReceipts except it uses the fuzzer to populate DepositTx fields.
...@@ -42,6 +43,7 @@ func fuzzReceipts(typeProvider *fuzz.Fuzzer, blockHash common.Hash, depositContr ...@@ -42,6 +43,7 @@ func fuzzReceipts(typeProvider *fuzz.Fuzzer, blockHash common.Hash, depositContr
// Determine if this log will be a deposit log or not and generate it accordingly // Determine if this log will be a deposit log or not and generate it accordingly
for _, isDeposit := range txReceiptValues.DepositLogs { for _, isDeposit := range txReceiptValues.DepositLogs {
var ev *types.Log var ev *types.Log
var err error
if isDeposit { if isDeposit {
// Generate a user deposit source // Generate a user deposit source
source := UserDepositSource{L1BlockHash: blockHash, LogIndex: uint64(logIndex)} source := UserDepositSource{L1BlockHash: blockHash, LogIndex: uint64(logIndex)}
...@@ -70,14 +72,16 @@ func fuzzReceipts(typeProvider *fuzz.Fuzzer, blockHash common.Hash, depositContr ...@@ -70,14 +72,16 @@ func fuzzReceipts(typeProvider *fuzz.Fuzzer, blockHash common.Hash, depositContr
} }
// Marshal our actual log event // Marshal our actual log event
ev = MarshalDepositLogEvent(depositContractAddr, dep) ev, err = MarshalDepositLogEvent(depositContractAddr, dep)
if err != nil {
panic(err)
}
// If we have a good version and our tx succeeded, we add this to our list of expected deposits to // If we have a good version and our tx succeeded, we add this to our list of expected deposits to
// return. // return.
if status == types.ReceiptStatusSuccessful { if status == types.ReceiptStatusSuccessful {
expectedDeposits = append(expectedDeposits, dep) expectedDeposits = append(expectedDeposits, dep)
} }
} else { } else {
// If we're generated an unrelated log event (not deposit), fuzz some random parameters to use. // If we're generated an unrelated log event (not deposit), fuzz some random parameters to use.
var randomUnrelatedLogInfo struct { var randomUnrelatedLogInfo struct {
......
package derive package derive
import ( import (
"math/big"
"math/rand"
"testing"
"github.com/ethereum-optimism/optimism/op-node/eth"
"github.com/ethereum-optimism/optimism/op-node/testutils" "github.com/ethereum-optimism/optimism/op-node/testutils"
"github.com/ethereum-optimism/optimism/op-node/testutils/fuzzerutils" "github.com/ethereum-optimism/optimism/op-node/testutils/fuzzerutils"
"github.com/ethereum/go-ethereum/common"
fuzz "github.com/google/gofuzz" fuzz "github.com/google/gofuzz"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"math/big"
"math/rand"
"testing"
) )
// FuzzParseL1InfoDepositTxDataValid is a fuzz test built from TestParseL1InfoDepositTxData, which constructs random // FuzzParseL1InfoDepositTxDataValid is a fuzz test built from TestParseL1InfoDepositTxData, which constructs random
// L1 deposit tx info and derives a tx from it, then derives the info back from the tx, to ensure round-trip // L1 deposit tx info and derives a tx from it, then derives the info back from the tx, to ensure round-trip
// derivation is upheld. This generates "valid" data and ensures it is always derived back to original values. // derivation is upheld. This generates "valid" data and ensures it is always derived back to original values.
func FuzzParseL1InfoDepositTxDataValid(f *testing.F) { func FuzzParseL1InfoDepositTxDataValid(f *testing.F) {
f.Fuzz(func(t *testing.T, fuzzedData []byte, rngSeed int64) { f.Fuzz(func(t *testing.T, fuzzedData []byte, seqNr uint64, rngSeed int64) {
// Create our fuzzer wrapper to generate complex values // Create our fuzzer wrapper to generate complex values
typeProvider := fuzz.NewFromGoFuzz(fuzzedData).NilChance(0).MaxDepth(10000).NumElements(0, 0x100) typeProvider := fuzz.NewFromGoFuzz(fuzzedData).NilChance(0).MaxDepth(10000).NumElements(0, 0x100)
fuzzerutils.AddFuzzerFunctions(typeProvider) fuzzerutils.AddFuzzerFunctions(typeProvider)
// Generate our fuzzed value types to construct our L1 info // Generate our fuzzed value types to construct our L1 info
var fuzzVars struct { var fuzzVars struct {
InfoBaseFee *big.Int InfoBaseFee *big.Int
InfoTime uint64 InfoTime uint64
InfoNum uint64 InfoNum uint64
InfoSequenceNumber uint64 // InfoSequenceNumber uint64
} }
typeProvider.Fuzz(&fuzzVars) typeProvider.Fuzz(&fuzzVars)
// Create an rng provider and construct an L1 info from random + fuzzed data. // Create an rng provider and construct an L1 info from random + fuzzed data.
rng := rand.New(rand.NewSource(rngSeed)) rng := rand.New(rand.NewSource(rngSeed))
l1Info := testutils.MakeL1Info(func(l *testutils.MockL1Info) { // just go instantiate the struct instead of calling MakeL1Info
// see what has become of it.
l1Info := testutils.MakeBlockInfo(func(l *testutils.MockBlockInfo) {
l.InfoBaseFee = fuzzVars.InfoBaseFee l.InfoBaseFee = fuzzVars.InfoBaseFee
l.InfoTime = fuzzVars.InfoTime l.InfoTime = fuzzVars.InfoTime
l.InfoNum = fuzzVars.InfoNum l.InfoNum = fuzzVars.InfoNum
l.InfoSequenceNumber = fuzzVars.InfoSequenceNumber
})(rng) })(rng)
// Create our deposit tx from our info // Create our deposit tx from our info
depTx, err := L1InfoDeposit(l1Info.SequenceNumber(), l1Info) testSysCfg := eth.SystemConfig{
BatcherAddr: common.Address{42},
Overhead: [32]byte{},
Scalar: [32]byte{},
}
depTx, err := L1InfoDeposit(seqNr, l1Info, testSysCfg)
require.NoError(t, err) require.NoError(t, err)
// Get our info from out deposit tx // Get our info from out deposit tx
...@@ -47,11 +55,14 @@ func FuzzParseL1InfoDepositTxDataValid(f *testing.F) { ...@@ -47,11 +55,14 @@ func FuzzParseL1InfoDepositTxDataValid(f *testing.F) {
require.NoError(t, err, "expected valid deposit info") require.NoError(t, err, "expected valid deposit info")
// Verify all parameters match in our round trip deriving operations // Verify all parameters match in our round trip deriving operations
assert.Equal(t, res.Number, l1Info.NumberU64()) require.Equal(t, res.Number, l1Info.NumberU64())
assert.Equal(t, res.Time, l1Info.Time()) require.Equal(t, res.Time, l1Info.Time())
assert.True(t, res.BaseFee.Sign() >= 0) require.True(t, res.BaseFee.Sign() >= 0)
assert.Equal(t, res.BaseFee.Bytes(), l1Info.BaseFee().Bytes()) require.Equal(t, res.BaseFee.Bytes(), l1Info.BaseFee().Bytes())
assert.Equal(t, res.BlockHash, l1Info.Hash()) require.Equal(t, res.BlockHash, l1Info.Hash())
require.Equal(t, res.SequenceNumber, seqNr)
l1CfgFetcher := &testutils.MockL2Client{}
l1CfgFetcher.ExpectSystemConfigByL2Hash(res.BlockHash, testSysCfg, nil)
}) })
} }
...@@ -66,7 +77,7 @@ func FuzzParseL1InfoDepositTxDataBadLength(f *testing.F) { ...@@ -66,7 +77,7 @@ func FuzzParseL1InfoDepositTxDataBadLength(f *testing.F) {
// If the data is null, or too short or too long, we expect an error // If the data is null, or too short or too long, we expect an error
if fuzzedData == nil || len(fuzzedData) != expectedDepositTxDataLength { if fuzzedData == nil || len(fuzzedData) != expectedDepositTxDataLength {
assert.Error(t, err) require.Error(t, err)
} }
}) })
} }
// On develop
package driver package driver
import ( import (
"context" "context"
"errors" "errors"
"math/rand"
"testing"
"github.com/ethereum-optimism/optimism/op-node/eth" "github.com/ethereum-optimism/optimism/op-node/eth"
"github.com/ethereum-optimism/optimism/op-node/metrics" "github.com/ethereum-optimism/optimism/op-node/metrics"
"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/testutils" "github.com/ethereum-optimism/optimism/op-node/testutils"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"math/rand" "github.com/stretchr/testify/require"
"testing"
) )
type TestDummyOutputImpl struct { type TestDummyOutputImpl struct {
willError bool willError bool
outputInterface SequencerIface
} }
func (d TestDummyOutputImpl) createNewBlock(ctx context.Context, l2Head eth.L2BlockRef, l2SafeHead eth.BlockID, l2Finalized eth.BlockID, l1Origin eth.L1BlockRef) (eth.L2BlockRef, *eth.ExecutionPayload, error) { func (d TestDummyOutputImpl) CreateNewBlock(ctx context.Context, l2Head eth.L2BlockRef, l2SafeHead eth.BlockID, l2Finalized eth.BlockID, l1Origin eth.L1BlockRef) (eth.L2BlockRef, *eth.ExecutionPayload, error) {
// If we're meant to error, return one // If we're meant to error, return one
if d.willError { if d.willError {
return l2Head, nil, errors.New("the TestDummyOutputImpl.createNewBlock operation failed") return l2Head, nil, errors.New("the TestDummyOutputImpl.createNewBlock operation failed")
...@@ -48,20 +50,25 @@ func (d TestDummyOutputImpl) createNewBlock(ctx context.Context, l2Head eth.L2Bl ...@@ -48,20 +50,25 @@ func (d TestDummyOutputImpl) createNewBlock(ctx context.Context, l2Head eth.L2Bl
type TestDummyDerivationPipeline struct { type TestDummyDerivationPipeline struct {
DerivationPipeline DerivationPipeline
l2Head eth.L2BlockRef
l2SafeHead eth.L2BlockRef
l2Finalized eth.L2BlockRef
} }
func (d TestDummyDerivationPipeline) Reset() {} func (d TestDummyDerivationPipeline) Reset() {}
func (d TestDummyDerivationPipeline) Step(ctx context.Context) error { return nil } func (d TestDummyDerivationPipeline) Step(ctx context.Context) error { return nil }
func (d TestDummyDerivationPipeline) SetUnsafeHead(head eth.L2BlockRef) {} func (d TestDummyDerivationPipeline) SetUnsafeHead(head eth.L2BlockRef) {}
func (d TestDummyDerivationPipeline) AddUnsafePayload(payload *eth.ExecutionPayload) {} func (d TestDummyDerivationPipeline) AddUnsafePayload(payload *eth.ExecutionPayload) {}
func (d TestDummyDerivationPipeline) Finalized() eth.L2BlockRef { return eth.L2BlockRef{} } func (d TestDummyDerivationPipeline) Finalized() eth.L2BlockRef { return d.l2Head }
func (d TestDummyDerivationPipeline) SafeL2Head() eth.L2BlockRef { return eth.L2BlockRef{} } func (d TestDummyDerivationPipeline) SafeL2Head() eth.L2BlockRef { return d.l2SafeHead }
func (d TestDummyDerivationPipeline) UnsafeL2Head() eth.L2BlockRef { return eth.L2BlockRef{} } func (d TestDummyDerivationPipeline) UnsafeL2Head() eth.L2BlockRef { return d.l2Finalized }
func (d TestDummyDerivationPipeline) Progress() derive.Progress {
return derive.Progress{ type TestDummyL1OriginSelector struct {
Origin: eth.L1BlockRef{}, retval eth.L1BlockRef
Closed: false, }
}
func (l TestDummyL1OriginSelector) FindL1Origin(ctx context.Context, l1Head eth.L1BlockRef, l2Head eth.L2BlockRef) (eth.L1BlockRef, error) {
return l.retval, nil
} }
// TestRejectCreateBlockBadTimestamp tests that a block creation with invalid timestamps will be caught. // TestRejectCreateBlockBadTimestamp tests that a block creation with invalid timestamps will be caught.
...@@ -101,16 +108,18 @@ func TestRejectCreateBlockBadTimestamp(t *testing.T) { ...@@ -101,16 +108,18 @@ func TestRejectCreateBlockBadTimestamp(t *testing.T) {
outputProvider := TestDummyOutputImpl{willError: false} outputProvider := TestDummyOutputImpl{willError: false}
// Create our state // Create our state
s := state{ s := Driver{
l1Head: l1HeadRef, l1State: &L1State{
l2Head: l2HeadRef, l1Head: l1HeadRef,
l2SafeHead: l2HeadRef, log: log.New(),
l2Finalized: l2HeadRef, metrics: &metrics.Metrics{TransactionsSequencedTotal: prometheus.NewCounter(prometheus.CounterOpts{})},
Config: &cfg, },
log: log.New(), log: log.New(),
output: outputProvider, l1OriginSelector: TestDummyL1OriginSelector{retval: l1HeadRef},
derivation: TestDummyDerivationPipeline{}, config: &cfg,
metrics: &metrics.Metrics{TransactionsSequencedTotal: prometheus.NewCounter(prometheus.CounterOpts{})}, sequencer: outputProvider,
derivation: TestDummyDerivationPipeline{},
metrics: &metrics.Metrics{TransactionsSequencedTotal: prometheus.NewCounter(prometheus.CounterOpts{})},
} }
// Create a new block // Create a new block
...@@ -118,10 +127,9 @@ func TestRejectCreateBlockBadTimestamp(t *testing.T) { ...@@ -118,10 +127,9 @@ func TestRejectCreateBlockBadTimestamp(t *testing.T) {
// - L2Head timestamp + BlockTime should be greater than or equal to the L1 Time. // - L2Head timestamp + BlockTime should be greater than or equal to the L1 Time.
err := s.createNewL2Block(ctx) err := s.createNewL2Block(ctx)
// Verify the L1Origin's timestamp is greater than L1 genesis in our config. // Verify the L1Origin's block number is greater than L1 genesis in our config.
if l2l1OriginBlock.Number < s.Config.Genesis.L1.Number { if l2l1OriginBlock.Number < s.config.Genesis.L1.Number {
assert.Nil(t, err) require.NoError(t, err, "L1Origin block number should be greater than the L1 genesis block number")
return
} }
// Verify the new L2 block to create will have a time stamp equal or newer than our L1 origin block we derive from. // Verify the new L2 block to create will have a time stamp equal or newer than our L1 origin block we derive from.
...@@ -186,16 +194,18 @@ func FuzzRejectCreateBlockBadTimestamp(f *testing.F) { ...@@ -186,16 +194,18 @@ func FuzzRejectCreateBlockBadTimestamp(f *testing.F) {
outputProvider := TestDummyOutputImpl{willError: forceOutputFail} outputProvider := TestDummyOutputImpl{willError: forceOutputFail}
// Create our state // Create our state
s := state{ s := Driver{
l1Head: l1HeadRef, l1State: &L1State{
l2Head: l2HeadRef, l1Head: l1HeadRef,
l2SafeHead: l2HeadRef, log: log.New(),
l2Finalized: l2HeadRef, metrics: &metrics.Metrics{TransactionsSequencedTotal: prometheus.NewCounter(prometheus.CounterOpts{})},
Config: &cfg, },
log: log.New(), log: log.New(),
output: outputProvider, l1OriginSelector: TestDummyL1OriginSelector{retval: l1HeadRef},
derivation: TestDummyDerivationPipeline{}, config: &cfg,
metrics: &metrics.Metrics{TransactionsSequencedTotal: prometheus.NewCounter(prometheus.CounterOpts{})}, sequencer: outputProvider,
derivation: TestDummyDerivationPipeline{},
metrics: &metrics.Metrics{TransactionsSequencedTotal: prometheus.NewCounter(prometheus.CounterOpts{})},
} }
// Create a new block // Create a new block
...@@ -204,7 +214,7 @@ func FuzzRejectCreateBlockBadTimestamp(f *testing.F) { ...@@ -204,7 +214,7 @@ func FuzzRejectCreateBlockBadTimestamp(f *testing.F) {
err := s.createNewL2Block(ctx) err := s.createNewL2Block(ctx)
// Verify the L1Origin's timestamp is greater than L1 genesis in our config. // Verify the L1Origin's timestamp is greater than L1 genesis in our config.
if l2l1OriginBlock.Number < s.Config.Genesis.L1.Number { if l2l1OriginBlock.Number < s.config.Genesis.L1.Number {
assert.Nil(t, err) assert.Nil(t, err)
return return
} }
...@@ -213,10 +223,10 @@ func FuzzRejectCreateBlockBadTimestamp(f *testing.F) { ...@@ -213,10 +223,10 @@ func FuzzRejectCreateBlockBadTimestamp(f *testing.F) {
if l2HeadRef.Time+cfg.BlockTime < l2l1OriginBlock.Time { if l2HeadRef.Time+cfg.BlockTime < l2l1OriginBlock.Time {
// If not, we expect a specific error. // If not, we expect a specific error.
// TODO: This isn't the cleanest, we should construct + compare the whole error message. // TODO: This isn't the cleanest, we should construct + compare the whole error message.
assert.NotNil(t, err) require.NotNil(t, err)
assert.Contains(t, err.Error(), "cannot build L2 block on top") require.Contains(t, err.Error(), "cannot build L2 block on top")
assert.Contains(t, err.Error(), "for time") require.Contains(t, err.Error(), "for time")
assert.Contains(t, err.Error(), "before L1 origin") require.Contains(t, err.Error(), "before L1 origin")
return return
} }
...@@ -230,6 +240,6 @@ func FuzzRejectCreateBlockBadTimestamp(f *testing.F) { ...@@ -230,6 +240,6 @@ func FuzzRejectCreateBlockBadTimestamp(f *testing.F) {
} }
// Otherwise we should have no error. // Otherwise we should have no error.
assert.Nil(t, err) require.NoError(t, err, "L1Origin block number should be greater than the L1 genesis block number")
}) })
} }
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