Skip to content

Commit

Permalink
feat(batcher): send batcher transaction right before KromaMPTTime
Browse files Browse the repository at this point in the history
  • Loading branch information
0xbenyun committed Dec 2, 2024
1 parent 3473ad1 commit 1737b2b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
6 changes: 6 additions & 0 deletions op-batcher/batcher/channel_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ var (
ErrChannelTimeoutClose = errors.New("close to channel timeout")
ErrSeqWindowClose = errors.New("close to sequencer window timeout")
ErrTerminated = errors.New("channel terminated")
// [Kroma: START]
ErrJustBeforeKromaMPTTime = errors.New("reached the block just before KromaMPTTime")
// [Kroma: END]
)

type ChannelFullError struct {
Expand Down Expand Up @@ -174,7 +177,10 @@ func (c *ChannelBuilder) AddBlock(block *types.Block) (*derive.L1BlockInfo, erro
if err = c.co.FullErr(); err != nil {
c.setFullErr(err)
// Adding this block still worked, so don't return error, just mark as full
} else /* [Kroma: START] */ if c.rollupCfg.KromaMPTTime != nil && block.Time() == *c.rollupCfg.KromaMPTTime-c.rollupCfg.BlockTime {
c.setFullErr(ErrJustBeforeKromaMPTTime)
}
// [Kroma: END]

return l1info, nil
}
Expand Down
65 changes: 65 additions & 0 deletions op-batcher/batcher/channel_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,68 @@ func TestChannelManager_ChannelCreation(t *testing.T) {
})
}
}

// [Kroma: START]
func TestChannelManager_Close_BeforeMPTBlock(t *testing.T) {
require := require.New(t)

rng := rand.New(rand.NewSource(123))
log := testlog.Logger(t, log.LevelError)

cfg := defaultTestChannelConfig()
cfg.InitNoneCompressor()
cfg.BatchType = derive.SpanBatchType
rollupConfig := defaultTestRollupConfig
rollupConfig.BlockTime = 2
kromaMPTTime := uint64(1732200100)
rollupConfig.KromaMPTTime = &kromaMPTTime
m := NewChannelManager(log, metrics.NoopMetrics, cfg, &rollupConfig)
m.Clear(eth.BlockID{})

numTx := 1
zktBlock1 := derivetest.RandomL2BlockWithChainId(rng, numTx, defaultTestRollupConfig.L2ChainID)
zktBlock2 := derivetest.RandomL2BlockWithChainId(rng, numTx, defaultTestRollupConfig.L2ChainID)
mptBlock := derivetest.RandomL2BlockWithChainId(rng, numTx, defaultTestRollupConfig.L2ChainID)

h := zktBlock1.Header()
h.Time = kromaMPTTime - rollupConfig.BlockTime*2
zktBlock1 = zktBlock1.WithSeal(h)

h = zktBlock2.Header()
h.Number = new(big.Int).Add(zktBlock1.Number(), big.NewInt(1))
h.Time = zktBlock1.Time() + rollupConfig.BlockTime
h.ParentHash = zktBlock1.Hash()
zktBlock2 = zktBlock2.WithSeal(h)

h = mptBlock.Header()
h.Number = new(big.Int).Add(zktBlock2.Number(), big.NewInt(1))
h.Time = zktBlock2.Time() + rollupConfig.BlockTime
h.ParentHash = zktBlock2.Hash()
mptBlock = mptBlock.WithSeal(h)

require.NoError(m.AddL2Block(zktBlock1), "adding 1st L2 block")
require.NoError(m.AddL2Block(zktBlock2), "adding 2nd L2 block")
require.NoError(m.AddL2Block(mptBlock), "adding 3nd L2 block")

txdata, err := m.TxData(eth.BlockID{})
require.NoError(err)
ch1 := m.txChannels[txdata.ID().String()]
require.ErrorIs(ch1.FullErr(), ErrJustBeforeKromaMPTTime)

// current channel is not full yet
_, err = m.TxData(eth.BlockID{})
require.ErrorIs(err, io.EOF)

// Close immediately marks the channel as full with an ErrTerminated
// if the channel is not already full.
m.currentChannel.Close()
err = m.outputFrames()
require.NoError(err)
txdata, err = m.TxData(eth.BlockID{})
require.NoError(err)
ch2 := m.txChannels[txdata.ID().String()]
require.ErrorIs(ch2.FullErr(), ErrTerminated)
require.NotEqual(ch1.ID(), ch2.ID())
}

// [Kroma: END]

0 comments on commit 1737b2b

Please sign in to comment.