Assumptions. OP Stack derivation as op-node implements it: L1 = Ethereum mainnet (12 s blocks), L2 = OP Mainnet (2 s blocks), span batches enabled (Canyon+). Span batches change only the batch encoding inside a channel; the frame and channel layers below are untouched, so I decode the legacy batch payload: smaller decoder, same channel.
Core. Derivation is a pull-based pipeline: L1 traversal → L1 retrieval → frame queue → channel bank → channel assembler → batch queue → payload attributes → engine queue. Each stage is a pure function of (canonical L1 data, rollup config) plus one cursor. That is what makes an L1 reorg cheap: rewinding the cursor re-derives everything downstream, with no side effects to roll back. Test it the same way: a fake L1 source, a golden L2 chain, and a reorg injected mid-stream.
Go
package derive
import (
"encoding/binary"
"errors"
)
const (
MaxFrameSize = 120_000
ChannelTimeout = 300
MaxSequencerDrift = 600
)
var (
ErrFrameTooShort = errors.New("frame truncated")
ErrFrameTooLarge = errors.New("frame data exceeds MaxFrameSize")
)
type Frame struct {
ID [16]byte
Number uint16
Data []byte
IsLast bool
}
func ParseFrames(data []byte) ([]Frame, error) {
var out []Frame
for len(data) > 0 {
if len(data) < 23 {
return nil, ErrFrameTooShort
}
var f Frame
copy(f.ID[:], data[:16])
f.Number = binary.BigEndian.Uint16(data[16:18])
n := binary.BigEndian.Uint32(data[18:22])
if n > MaxFrameSize {
return nil, ErrFrameTooLarge
}
if uint32(len(data)-23) < n {
return nil, ErrFrameTooShort
}
f.Data = data[22 : 22+n]
f.IsLast = data[22+n] == 1
out = append(out, f)
data = data[23+n:]
}
return out, nil
}
type Channel struct {
ID [16]byte
frames map[uint16][]byte
last uint16
closed bool
openAt uint64
}
func (c *Channel) Add(f Frame, l1Block uint64) {
if c.frames == nil {
c.frames = map[uint16][]byte{}
c.openAt = l1Block
}
if _, dup := c.frames[f.Number]; dup {
return
}
c.frames[f.Number] = f.Data
if f.IsLast {
c.last, c.closed = f.Number, true
}
}
func (c *Channel) Read() ([]byte, bool) {
if !c.closed {
return nil, false
}
var buf []byte
for i := 0; i <= int(c.last); i++ {
d, ok := c.frames[uint16(i)]
if !ok {
return nil, false
}
buf = append(buf, d...)
}
return buf, true
}
func (c *Channel) Expired(l1Block uint64) bool {
return l1Block > c.openAt+ChannelTimeout
}
Go
package derive
import (
"encoding/binary"
"errors"
"testing"
)
func encodeFrame(id [16]byte, num uint16, data []byte, last bool) []byte {
b := append([]byte{}, id[:]...)
b = binary.BigEndian.AppendUint16(b, num)
b = binary.BigEndian.AppendUint32(b, uint32(len(data)))
b = append(b, data...)
if last {
return append(b, 1)
}
return append(b, 0)
}
func TestChannelReassembly(t *testing.T) {
id := [16]byte{0xAA}
raw := append(encodeFrame(id, 1, []byte("world"), true),
encodeFrame(id, 0, []byte("hello"), false)...)
frames, err := ParseFrames(raw)
if err != nil {
t.Fatal(err)
}
if len(frames) != 2 {
t.Fatalf("frames = %d, want 2", len(frames))
}
ch := &Channel{}
for _, f := range frames {
ch.Add(f, 100)
}
got, ok := ch.Read()
if !ok || string(got) != "helloworld" {
t.Fatalf("Read() = %q, %v; want \"helloworld\", true", got, ok)
}
}
func TestFrameTooLarge(t *testing.T) {
id := [16]byte{0xBB}
b := append([]byte{}, id[:]...)
b = binary.BigEndian.AppendUint16(b, 0)
b = binary.BigEndian.AppendUint32(b, MaxFrameSize+1)
b = append(b, 0)
if _, err := ParseFrames(b); !errors.Is(err, ErrFrameTooLarge) {
t.Fatalf("err = %v, want ErrFrameTooLarge", err)
}
}
func TestChannelTimeout(t *testing.T) {
ch := &Channel{}
ch.Add(Frame{ID: [16]byte{1}, Number: 0, Data: []byte("x")}, 100)
if _, ok := ch.Read(); ok {
t.Fatal("open channel must not read")
}
if ch.Expired(400) {
t.Fatal("expired at 400, want open")
}
if !ch.Expired(401) {
t.Fatal("not expired at 401, want expired")
}
}
Trace of TestChannelReassembly. raw is 56 bytes: frame 1 (16-byte ID + 2-byte number + 4-byte length + 5 bytes world + 1 byte 1) then frame 0 (same shape, hello, trailing 0). First iteration: len(data) = 56 ≥ 23, n = 5, len(data)-23 = 33 ≥ 5, so Data = data[22:27] = "world" and IsLast = data[27] == 1 → true; the cursor advances 28 bytes. Second iteration: Number = 0, Data = "hello", IsLast = false; advances 28, leaving 0. Add sees frame 1 first, so openAt = 100, frames[1] = "world", last = 1, closed = true; frame 0 then lands in the map. Read walks i = 0, 1 and returns "helloworld", true. TestFrameTooLarge builds exactly 23 bytes with n = 120001 > 120000, so ParseFrames returns ErrFrameTooLarge before reading any frame data. TestChannelTimeout opens at L1 block 100; 400 > 400 is false, 401 > 400 is true.
Batch → payload attributes. A channel payload is a sequence of version-prefixed batches. 0x00 is a legacy batch (RLP [epoch_num, epoch_hash, timestamp, tx_list]); 0x01 is a span batch carrying the same fields for a run of L2 blocks. Two timestamp rules decide acceptance against the L1 origin block: timestamp >= epochTime and timestamp <= epochTime + MaxSequencerDrift (600 s). Derived attributes: {timestamp: batch.Timestamp, prevRandao: epoch.mixHash, parent: last L2 block, noTxPool: true, gasLimit: 30_000_000, transactions: [L1 attributes deposit] ++ [deposits from the epoch's OptimismPortal logs] ++ batch.Txs}. At 2 s L2 blocks and 12 s L1 blocks that is 6 L2 blocks per epoch, and the 600 s drift window spans 300 L2 blocks.
Trade-offs. Pull-based stages make a rewind a cursor reset; a push-based event loop needs explicit backpressure and turns reorg handling into a distributed rollback. Span batches cut per-block calldata overhead versus legacy batches but need a more complex decoder. Since Ecotone the batcher can post to EIP-4844 blobs instead of calldata: cheaper, but blobs are pruned after 4096 epochs × 32 slots × 12 s ≈ 18.2 days, so derivation must never lag the L1 head by more than that or the data is gone. Calldata is permanent, which is why it stays the fallback.
| failure | symptom | detection |
|---|
| parser divergence | two nodes derive different L2 blocks from identical L1 | hash derived payload attributes per L1 block and compare across nodes |
| skipped deposit | user funds never credited on L2 | reconcile L2 deposit count against TransactionDeposited logs from the L1 OptimismPortal per epoch |
| timestamp rule error | batch accepted past epochTime + 600 | assert in derivation; alert on the drift error |
| config mismatch | empty channel bank, chain stalls | compare --batch-inbox-address (default 0xff00000000000000000000000000000000000010) and L2 chain ID across nodes at startup |
| malformed channel | frame over 120 000 bytes or truncated | count ErrFrameTooLarge / ErrFrameTooShort; a spike means a bad batcher |
| deep reorg | safe head sits above a reorged L1 origin | track the safe head's L1 origin; rewind when L1 ancestry changes |
| inconsistent safe head | safe head not a prefix of unsafe head | assert safeHead.number <= unsafeHead.number and shared ancestry |
Channel timeout is 300 L1 blocks = 3600 s: a channel whose first frame lands at block N is dropped once the L1 head passes N+300, so a batcher that stalls mid-channel loses the whole channel, not just the tail.