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
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/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.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU=
google.golang.org/grpc v1.36.1/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU=
......
......@@ -5,7 +5,7 @@ import (
"github.com/ethereum-optimism/optimism/op-node/testutils/fuzzerutils"
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
......@@ -22,14 +22,14 @@ func FuzzBatchRoundTrip(f *testing.F) {
// Encode our batch data
enc, err := batchData.MarshalBinary()
assert.NoError(t, err)
require.NoError(t, err)
// Decode our encoded batch data
var dec BatchData
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
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
import (
"math/big"
"testing"
"github.com/ethereum-optimism/optimism/op-node/testutils"
"github.com/ethereum-optimism/optimism/op-node/testutils/fuzzerutils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
fuzz "github.com/google/gofuzz"
"github.com/stretchr/testify/require"
"math/big"
"testing"
)
// 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
// Determine if this log will be a deposit log or not and generate it accordingly
for _, isDeposit := range txReceiptValues.DepositLogs {
var ev *types.Log
var err error
if isDeposit {
// Generate a user deposit source
source := UserDepositSource{L1BlockHash: blockHash, LogIndex: uint64(logIndex)}
......@@ -70,14 +72,16 @@ func fuzzReceipts(typeProvider *fuzz.Fuzzer, blockHash common.Hash, depositContr
}
// 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
// return.
if status == types.ReceiptStatusSuccessful {
expectedDeposits = append(expectedDeposits, dep)
}
} else {
// If we're generated an unrelated log event (not deposit), fuzz some random parameters to use.
var randomUnrelatedLogInfo struct {
......
package derive
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/fuzzerutils"
"github.com/ethereum/go-ethereum/common"
fuzz "github.com/google/gofuzz"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"math/big"
"math/rand"
"testing"
)
// 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
// derivation is upheld. This generates "valid" data and ensures it is always derived back to original values.
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
typeProvider := fuzz.NewFromGoFuzz(fuzzedData).NilChance(0).MaxDepth(10000).NumElements(0, 0x100)
fuzzerutils.AddFuzzerFunctions(typeProvider)
......@@ -25,21 +27,27 @@ func FuzzParseL1InfoDepositTxDataValid(f *testing.F) {
InfoBaseFee *big.Int
InfoTime uint64
InfoNum uint64
InfoSequenceNumber uint64
// InfoSequenceNumber uint64
}
typeProvider.Fuzz(&fuzzVars)
// Create an rng provider and construct an L1 info from random + fuzzed data.
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.InfoTime = fuzzVars.InfoTime
l.InfoNum = fuzzVars.InfoNum
l.InfoSequenceNumber = fuzzVars.InfoSequenceNumber
})(rng)
// 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)
// Get our info from out deposit tx
......@@ -47,11 +55,14 @@ func FuzzParseL1InfoDepositTxDataValid(f *testing.F) {
require.NoError(t, err, "expected valid deposit info")
// Verify all parameters match in our round trip deriving operations
assert.Equal(t, res.Number, l1Info.NumberU64())
assert.Equal(t, res.Time, l1Info.Time())
assert.True(t, res.BaseFee.Sign() >= 0)
assert.Equal(t, res.BaseFee.Bytes(), l1Info.BaseFee().Bytes())
assert.Equal(t, res.BlockHash, l1Info.Hash())
require.Equal(t, res.Number, l1Info.NumberU64())
require.Equal(t, res.Time, l1Info.Time())
require.True(t, res.BaseFee.Sign() >= 0)
require.Equal(t, res.BaseFee.Bytes(), l1Info.BaseFee().Bytes())
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) {
// If the data is null, or too short or too long, we expect an error
if fuzzedData == nil || len(fuzzedData) != expectedDepositTxDataLength {
assert.Error(t, err)
require.Error(t, err)
}
})
}
// On develop
package driver
import (
"context"
"errors"
"math/rand"
"testing"
"github.com/ethereum-optimism/optimism/op-node/eth"
"github.com/ethereum-optimism/optimism/op-node/metrics"
"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/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"math/rand"
"testing"
"github.com/stretchr/testify/require"
)
type TestDummyOutputImpl struct {
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 d.willError {
return l2Head, nil, errors.New("the TestDummyOutputImpl.createNewBlock operation failed")
......@@ -48,20 +50,25 @@ func (d TestDummyOutputImpl) createNewBlock(ctx context.Context, l2Head eth.L2Bl
type TestDummyDerivationPipeline struct {
DerivationPipeline
l2Head eth.L2BlockRef
l2SafeHead eth.L2BlockRef
l2Finalized eth.L2BlockRef
}
func (d TestDummyDerivationPipeline) Reset() {}
func (d TestDummyDerivationPipeline) Step(ctx context.Context) error { return nil }
func (d TestDummyDerivationPipeline) SetUnsafeHead(head eth.L2BlockRef) {}
func (d TestDummyDerivationPipeline) AddUnsafePayload(payload *eth.ExecutionPayload) {}
func (d TestDummyDerivationPipeline) Finalized() eth.L2BlockRef { return eth.L2BlockRef{} }
func (d TestDummyDerivationPipeline) SafeL2Head() eth.L2BlockRef { return eth.L2BlockRef{} }
func (d TestDummyDerivationPipeline) UnsafeL2Head() eth.L2BlockRef { return eth.L2BlockRef{} }
func (d TestDummyDerivationPipeline) Progress() derive.Progress {
return derive.Progress{
Origin: eth.L1BlockRef{},
Closed: false,
}
func (d TestDummyDerivationPipeline) Finalized() eth.L2BlockRef { return d.l2Head }
func (d TestDummyDerivationPipeline) SafeL2Head() eth.L2BlockRef { return d.l2SafeHead }
func (d TestDummyDerivationPipeline) UnsafeL2Head() eth.L2BlockRef { return d.l2Finalized }
type TestDummyL1OriginSelector struct {
retval eth.L1BlockRef
}
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.
......@@ -101,14 +108,16 @@ func TestRejectCreateBlockBadTimestamp(t *testing.T) {
outputProvider := TestDummyOutputImpl{willError: false}
// Create our state
s := state{
s := Driver{
l1State: &L1State{
l1Head: l1HeadRef,
l2Head: l2HeadRef,
l2SafeHead: l2HeadRef,
l2Finalized: l2HeadRef,
Config: &cfg,
log: log.New(),
output: outputProvider,
metrics: &metrics.Metrics{TransactionsSequencedTotal: prometheus.NewCounter(prometheus.CounterOpts{})},
},
log: log.New(),
l1OriginSelector: TestDummyL1OriginSelector{retval: l1HeadRef},
config: &cfg,
sequencer: outputProvider,
derivation: TestDummyDerivationPipeline{},
metrics: &metrics.Metrics{TransactionsSequencedTotal: prometheus.NewCounter(prometheus.CounterOpts{})},
}
......@@ -118,10 +127,9 @@ func TestRejectCreateBlockBadTimestamp(t *testing.T) {
// - L2Head timestamp + BlockTime should be greater than or equal to the L1 Time.
err := s.createNewL2Block(ctx)
// Verify the L1Origin's timestamp is greater than L1 genesis in our config.
if l2l1OriginBlock.Number < s.Config.Genesis.L1.Number {
assert.Nil(t, err)
return
// Verify the L1Origin's block number is greater than L1 genesis in our config.
if l2l1OriginBlock.Number < s.config.Genesis.L1.Number {
require.NoError(t, err, "L1Origin block number should be greater than the L1 genesis block number")
}
// Verify the new L2 block to create will have a time stamp equal or newer than our L1 origin block we derive from.
......@@ -186,14 +194,16 @@ func FuzzRejectCreateBlockBadTimestamp(f *testing.F) {
outputProvider := TestDummyOutputImpl{willError: forceOutputFail}
// Create our state
s := state{
s := Driver{
l1State: &L1State{
l1Head: l1HeadRef,
l2Head: l2HeadRef,
l2SafeHead: l2HeadRef,
l2Finalized: l2HeadRef,
Config: &cfg,
log: log.New(),
output: outputProvider,
metrics: &metrics.Metrics{TransactionsSequencedTotal: prometheus.NewCounter(prometheus.CounterOpts{})},
},
log: log.New(),
l1OriginSelector: TestDummyL1OriginSelector{retval: l1HeadRef},
config: &cfg,
sequencer: outputProvider,
derivation: TestDummyDerivationPipeline{},
metrics: &metrics.Metrics{TransactionsSequencedTotal: prometheus.NewCounter(prometheus.CounterOpts{})},
}
......@@ -204,7 +214,7 @@ func FuzzRejectCreateBlockBadTimestamp(f *testing.F) {
err := s.createNewL2Block(ctx)
// 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)
return
}
......@@ -213,10 +223,10 @@ func FuzzRejectCreateBlockBadTimestamp(f *testing.F) {
if l2HeadRef.Time+cfg.BlockTime < l2l1OriginBlock.Time {
// If not, we expect a specific error.
// TODO: This isn't the cleanest, we should construct + compare the whole error message.
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "cannot build L2 block on top")
assert.Contains(t, err.Error(), "for time")
assert.Contains(t, err.Error(), "before L1 origin")
require.NotNil(t, err)
require.Contains(t, err.Error(), "cannot build L2 block on top")
require.Contains(t, err.Error(), "for time")
require.Contains(t, err.Error(), "before L1 origin")
return
}
......@@ -230,6 +240,6 @@ func FuzzRejectCreateBlockBadTimestamp(f *testing.F) {
}
// 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