Commit 6b34dedd authored by clabby's avatar clabby

Draft garbage batch test

Throw on unexpected garbage kind

`l2geth` -> `geth` dep update

Go doesn't fall through in switch cases by default
parent 51fb98aa
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"encoding/hex"
"io" "io"
"math/big" "math/big"
...@@ -13,6 +14,7 @@ import ( ...@@ -13,6 +14,7 @@ import (
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/ethereum-optimism/optimism/op-node/eth" "github.com/ethereum-optimism/optimism/op-node/eth"
...@@ -195,6 +197,91 @@ func (s *L2Batcher) ActL2BatchSubmit(t Testing) { ...@@ -195,6 +197,91 @@ func (s *L2Batcher) ActL2BatchSubmit(t Testing) {
require.NoError(t, err, "need to send tx") require.NoError(t, err, "need to send tx")
} }
// TODO: Move this to a better location
type GarbageKind int64
const (
STRIP_VERSION GarbageKind = iota
MALFORM_RLP
TRUNCATE_END
DIRTY_APPEND
)
var GarbageKinds = []GarbageKind{
STRIP_VERSION,
// MALFORM_RLP,
TRUNCATE_END,
DIRTY_APPEND,
}
// ActL2BatchSubmit constructs a malformed channel frame and submits it to the
// batch inbox. This *should* cause the batch inbox to reject the blocks
// encoded within the frame, even if they are valid.
func (s *L2Batcher) ActL2BatchSubmitGarbage(t Testing, kind GarbageKind) {
// Don't run this action if there's no data to submit
if s.l2ChannelOut == nil {
t.InvalidAction("need to buffer data first, cannot batch submit with empty buffer")
return
}
// Collect the output frame
data := new(bytes.Buffer)
data.WriteByte(derive.DerivationVersion0)
// subtract one, to account for the version byte
if err := s.l2ChannelOut.OutputFrame(data, s.l2BatcherCfg.MaxL1TxSize-1); err == io.EOF {
s.l2ChannelOut = nil
s.l2Submitting = false
} else if err != nil {
s.l2Submitting = false
t.Fatalf("failed to output channel data to frame: %v", err)
}
outputFrame := data.Bytes()
// Malform the output frame
switch kind {
// Strip the derivation version byte from the output frame
case STRIP_VERSION:
outputFrame = outputFrame[1:]
case MALFORM_RLP:
// WIP
t.Log("output frame", hex.EncodeToString(outputFrame), rlp.Decode(bytes.NewReader(outputFrame[1:len(outputFrame)-47]), &derive.BatchData{}))
// Remove 4 bytes from the tail end of the output frame
case TRUNCATE_END:
outputFrame = outputFrame[:len(outputFrame)-4]
// Append 4 garbage bytes to the end of the output frame
case DIRTY_APPEND:
outputFrame = append(outputFrame, []byte{0xBA, 0xD0, 0xC0, 0xDE}...)
default:
t.Fatalf("Unexpected garbage kind: %v", kind)
}
nonce, err := s.l1.PendingNonceAt(t.Ctx(), s.batcherAddr)
require.NoError(t, err, "need batcher nonce")
gasTipCap := big.NewInt(2 * params.GWei)
pendingHeader, err := s.l1.HeaderByNumber(t.Ctx(), big.NewInt(-1))
require.NoError(t, err, "need l1 pending header for gas price estimation")
gasFeeCap := new(big.Int).Add(gasTipCap, new(big.Int).Mul(pendingHeader.BaseFee, big.NewInt(2)))
rawTx := &types.DynamicFeeTx{
ChainID: s.rollupCfg.L1ChainID,
Nonce: nonce,
To: &s.rollupCfg.BatchInboxAddress,
GasTipCap: gasTipCap,
GasFeeCap: gasFeeCap,
Data: outputFrame,
}
gas, err := core.IntrinsicGas(rawTx.Data, nil, false, true, true)
require.NoError(t, err, "need to compute intrinsic gas")
rawTx.Gas = gas
tx, err := types.SignNewTx(s.l2BatcherCfg.BatcherKey, s.l1Signer, rawTx)
require.NoError(t, err, "need to sign tx")
err = s.l1.SendTransaction(t.Ctx(), tx)
require.NoError(t, err, "need to send tx")
}
func (s *L2Batcher) ActBufferAll(t Testing) { func (s *L2Batcher) ActBufferAll(t Testing) {
stat, err := s.syncStatusAPI.SyncStatus(t.Ctx()) stat, err := s.syncStatusAPI.SyncStatus(t.Ctx())
require.NoError(t, err) require.NoError(t, err)
......
...@@ -196,6 +196,76 @@ func TestL2Finalization(gt *testing.T) { ...@@ -196,6 +196,76 @@ func TestL2Finalization(gt *testing.T) {
require.Equal(t, heightToSubmit, sequencer.SyncStatus().FinalizedL2.Number, "unknown/bad finalized L1 blocks are ignored") require.Equal(t, heightToSubmit, sequencer.SyncStatus().FinalizedL2.Number, "unknown/bad finalized L1 blocks are ignored")
} }
// Tests the behavior of an invalid/malformed output channel frame containing
// valid batches being submitted to the batch inbox.
func TestGarbageBatch(gt *testing.T) {
t := NewDefaultTesting(gt)
p := defaultRollupTestParams
dp := e2eutils.MakeDeployParams(t, p)
for _, garbageKind := range GarbageKinds {
sd := e2eutils.Setup(t, dp, defaultAlloc)
log := testlog.Logger(t, log.LvlError)
miner, engine, sequencer := setupSequencerTest(t, sd, log)
_, verifier := setupVerifier(t, sd, log, miner.L1Client(t, sd.RollupCfg))
batcher := NewL2Batcher(log, sd.RollupCfg, &BatcherCfg{
MinL1TxSize: 0,
MaxL1TxSize: 128_000,
BatcherKey: dp.Secrets.Batcher,
}, sequencer.RollupClient(), miner.EthClient(), engine.EthClient())
sequencer.ActL2PipelineFull(t)
verifier.ActL2PipelineFull(t)
syncAndBuildL2 := func() {
// Send a head signal to the sequencer and verifier
sequencer.ActL1HeadSignal(t)
verifier.ActL1HeadSignal(t)
// Run the derivation pipeline on the sequencer and verifier
sequencer.ActL2PipelineFull(t)
verifier.ActL2PipelineFull(t)
// Build the L2 chain to the L1 head
sequencer.ActBuildToL1Head(t)
}
// Build an empty block on L1 and run the derivation pipeline + build L2
// to the L1 head (block #1)
miner.ActEmptyBlock(t)
syncAndBuildL2()
// Ensure that the L2 safe head has an L1 Origin at genesis before any
// batches are submitted.
require.Equal(t, uint64(0), sequencer.L2Safe().L1Origin.Number)
require.Equal(t, uint64(1), sequencer.L2Unsafe().L1Origin.Number)
// Submit a batch containing all blocks built on L2 while catching up
// to the L1 head above. The output channel frame submitted to the batch
// inbox will be invalid- it will be malformed depending on the passed
// `garbageKind`.
batcher.ActBufferAll(t)
batcher.ActL2ChannelClose(t)
batcher.ActL2BatchSubmitGarbage(t, garbageKind)
// Include the batch on L1 in block #2
miner.ActL1StartBlock(12)(t)
miner.ActL1IncludeTx(dp.Addresses.Batcher)(t)
miner.ActL1EndBlock(t)
// Send a head signal + run the derivation pipeline on the sequencer
// and verifier.
syncAndBuildL2()
// Verify that the L2 blocks that were batch submitted were *not* marked
// as safe due to the malformed output channel frame. The safe head should
// still have an L1 Origin at genesis.
require.Equal(t, uint64(0), sequencer.L2Safe().L1Origin.Number)
require.Equal(t, uint64(2), sequencer.L2Unsafe().L1Origin.Number)
}
}
func TestExtendedTimeWithoutL1Batches(gt *testing.T) { func TestExtendedTimeWithoutL1Batches(gt *testing.T) {
t := NewDefaultTesting(gt) t := NewDefaultTesting(gt)
p := &e2eutils.TestParams{ p := &e2eutils.TestParams{
......
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