Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions op-acceptance-tests/tests/sdm/invalid_batch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package sdm

import (
"slices"
"testing"

"github.com/ethereum-optimism/optimism/op-acceptance-tests/tests/sdm/sdmtest"
optypes "github.com/ethereum-optimism/optimism/op-core/types"
"github.com/ethereum-optimism/optimism/op-devstack/devtest"
"github.com/ethereum-optimism/optimism/op-devstack/dsl"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum-optimism/optimism/op-service/eth/safety"
"github.com/ethereum/go-ethereum/common"
gethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
)

// TestSDMInvalidPostExecIsReplacedWithDepositsOnly checks that a batched block whose 0x7D fails
// verification is replaced by a deposits-only block, as the fault-proof program does.
func TestSDMInvalidPostExecIsReplacedWithDepositsOnly(gt *testing.T) {
t := devtest.ParallelT(gt)
sys := newFixedPolicySDMRethSystem(t)
sdmtest.VerifyOpReth(t, sys.L2EL)
sdmtest.VerifyOpReth(t, sys.L2ELVerifier)

block, receipt := submitFixtureProbe(t, sys)
target := uint64(block.Number)
honest := sys.L2EL.BlockRefByNumber(target)
// The verifier holds the honest block as unsafe, so derivation must reorg it out.
sys.L2ELVerifier.Reached(eth.Unsafe, target, 60)

from := min(sys.L2CL.SafeL2BlockRef().Number, sys.L2CLVerifier.SafeL2BlockRef().Number) + 1
sdmtest.PostTamperedSingularBatches(t, sys, from, target, func(txs []eth.Data) []eth.Data {
return overRefundPostExec(t, receipt, txs)
})

dsl.CheckAll(t,
sys.L2CL.ReachedFn(safety.CrossSafe, target, 120),
sys.L2CLVerifier.ReachedFn(safety.CrossSafe, target, 120),
sys.L2EL.ReachedFn(eth.Safe, target, 120),
sys.L2ELVerifier.ReachedFn(eth.Safe, target, 120),
)
replacement := sdmtest.GetBlockWithTxs(t, sys.L2ELVerifier, target)
t.Require().NotEqual(block.Hash, replacement.Hash, "the invalid block must be replaced")
t.Require().Equal(honest.ParentHash, sys.L2ELVerifier.BlockRefByNumber(target).ParentHash,
"only the tampered block may be replaced")
var wantDeposits, gotTxs []common.Hash
for _, tx := range block.Transactions {
if uint64(tx.Type) == optypes.DepositTxType {
wantDeposits = append(wantDeposits, tx.Hash)
}
}
for _, tx := range replacement.Transactions {
gotTxs = append(gotTxs, tx.Hash)
}
t.Require().Equal(wantDeposits, gotTxs, "the replacement must hold exactly the tampered block's deposits")
t.Require().Equal(replacement.Hash, sdmtest.GetBlockWithTxs(t, sys.L2EL, target).Hash,
"the sequencer must derive the same replacement")

// The batcher resumes on top of the replacement.
sys.L2Batcher.Start()
dsl.CheckAll(t,
sys.L2CL.ReachedFn(safety.CrossSafe, target+3, 120),
sys.L2CLVerifier.ReachedFn(safety.CrossSafe, target+3, 120),
)
}

// overRefundPostExec raises the probe tx's refund one gas above the gas it used before the refund.
func overRefundPostExec(t devtest.T, probe *gethtypes.Receipt, txs []eth.Data) []eth.Data {
last := len(txs) - 1
postExecTx, err := optypes.UnmarshalPostExecTx(txs[last])
t.Require().NoError(err, "the probe block must end with a 0x7D")
payload, err := optypes.DecodePostExecPayload(postExecTx.Data)
t.Require().NoError(err, "the probe 0x7D payload must decode")
entry := slices.IndexFunc(payload.GasRefundEntries, func(e optypes.SDMGasEntry) bool {
return e.Index == uint64(probe.TransactionIndex)
})
t.Require().GreaterOrEqual(entry, 0, "the 0x7D must refund the probe tx")
// The receipt reports gas after the refund.
evmGasUsed := probe.GasUsed + payload.GasRefundEntries[entry].GasRefund
payload.GasRefundEntries[entry].GasRefund = evmGasUsed + 1
postExecTx.Data, err = rlp.EncodeToBytes(payload)
t.Require().NoError(err, "the tampered 0x7D payload must encode")
txs[last], err = postExecTx.MarshalBinary()
t.Require().NoError(err, "the tampered 0x7D must encode")
return txs
}
57 changes: 57 additions & 0 deletions op-acceptance-tests/tests/sdm/sdmtest/batch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package sdmtest

import (
"bytes"
"io"
"slices"

"github.com/ethereum-optimism/optimism/op-batcher/compressor"
"github.com/ethereum-optimism/optimism/op-chain-ops/devkeys"
"github.com/ethereum-optimism/optimism/op-devstack/devtest"
"github.com/ethereum-optimism/optimism/op-devstack/dsl"
"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/params"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum-optimism/optimism/op-service/txplan"
)

// maxBatchFrameSize keeps the hand-built channel in a single calldata frame.
const maxBatchFrameSize = 120_000

// PostTamperedSingularBatches stands in for a malicious batcher: it posts the sequencer's blocks
// [from, to] to L1 as one singular-batch channel signed with the batcher key, after tamper has
// rewritten the raw transactions of block `to`. The real batcher must be stopped.
func PostTamperedSingularBatches(t devtest.T, sys *RethSystem, from, to uint64, tamper func([]eth.Data) []eth.Data) {
rollupCfg := sys.L2Network.Escape().RollupConfig()
comp, err := compressor.NewNonCompressor(compressor.Config{
TargetOutputSize: maxBatchFrameSize,
CompressionAlgo: derive.Zlib,
})
t.Require().NoError(err)
channel, err := derive.NewSingularChannelOut(comp, rollup.NewChainSpec(rollupCfg))
t.Require().NoError(err)

for number := from; number <= to; number++ {
payload := *sys.L2EL.PayloadByNumber(number).ExecutionPayload
if number == to {
// The client caches fetched payloads, so tamper with a copy.
payload.Transactions = tamper(slices.Clone(payload.Transactions))
}
_, err := channel.AddBlock(rollupCfg, &payload)
t.Require().NoError(err, "block %d must fit in the channel", number)
}
t.Require().NoError(channel.Close())

data := bytes.NewBuffer([]byte{params.DerivationVersion0})
_, err = channel.OutputFrame(data, maxBatchFrameSize)
t.Require().ErrorIs(err, io.EOF, "the channel must fit in one frame")

batcherKey := sys.L2Network.Escape().Keys().Secret(devkeys.BatcherRole.Key(rollupCfg.L2ChainID))
batcher := dsl.NewEOA(dsl.NewKey(t, batcherKey), sys.L1EL)
batcher.Transact(
batcher.Plan(),
txplan.WithTo(&rollupCfg.BatchInboxAddress),
txplan.WithData(data.Bytes()),
)
}
Loading
Loading