Skip to content

Commit

Permalink
feat: add an initial CCC check to block close to HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak committed Aug 30, 2024
1 parent 268814c commit 5c367f3
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/rawdb/accessors_row_consumption.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ func ReadBlockRowConsumptionRLP(db ethdb.Reader, l2BlockHash common.Hash) rlp.Ra
}
return data
}

// DeleteBlockRowConsumption deletes a RowConsumption of the block from the database
func DeleteBlockRowConsumption(db ethdb.KeyValueWriter, l2BlockHash common.Hash) error {
return db.Delete(rowConsumptionKey(l2BlockHash))
}
27 changes: 27 additions & 0 deletions miner/scroll_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,28 @@ func (w *worker) close() {
w.wg.Wait()
}

// checkHeadRowConsumption will start some initial workers to CCC check block close to the HEAD
func (w *worker) checkHeadRowConsumption() error {
checkStart := uint64(1)
numOfBlockToCheck := uint64(w.config.CCCMaxWorkers + 1)
head := w.chain.CurrentHeader()
if head.Number.Uint64() > numOfBlockToCheck {
checkStart = head.Number.Uint64() - numOfBlockToCheck
}

for curBlockNum := checkStart; curBlockNum <= head.Number.Uint64(); curBlockNum++ {
block := w.chain.GetBlockByNumber(curBlockNum)
// only spawn CCC checkers for blocks with no row consumption data stored in DB
if rawdb.ReadBlockRowConsumption(w.chain.Database(), block.Hash()) == nil {
if err := w.asyncChecker.Check(block); err != nil {
return err
}
}
}

return nil
}

// mainLoop is a standalone goroutine to regenerate the sealing task based on the received event.
func (w *worker) mainLoop() {
defer w.wg.Done()
Expand Down Expand Up @@ -322,6 +344,11 @@ func (w *worker) mainLoop() {

select {
case <-w.startCh:
if err := w.checkHeadRowConsumption(); err != nil {
log.Error("failed to start head checkers", "err", err)
return
}

_, err = w.tryCommitNewWork(time.Now(), w.chain.CurrentHeader().Hash(), nil)
case trigger := <-w.reorgCh:
err = w.handleReorg(&trigger)
Expand Down
43 changes: 43 additions & 0 deletions miner/scroll_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1142,3 +1142,46 @@ secondReorg:
require.Equal(t, oldBlock.Transactions()[1:][i].Hash(), newBlock.Transactions()[i].Hash())
}
}

func TestRestartHeadCCC(t *testing.T) {
var (
engine consensus.Engine
chainConfig *params.ChainConfig
db = rawdb.NewMemoryDatabase()
)
chainConfig = params.AllCliqueProtocolChanges
chainConfig.Clique = &params.CliqueConfig{Period: 1, Epoch: 30000, RelaxedPeriod: true}
chainConfig.Scroll.FeeVaultAddress = &common.Address{}
engine = clique.New(chainConfig.Clique, db)

maxTxPerBlock := 2
chainConfig.Scroll.MaxTxPerBlock = &maxTxPerBlock
chainConfig.Scroll.L1Config = &params.L1Config{
NumL1MessagesPerBlock: 10,
}

chainConfig.LondonBlock = big.NewInt(0)
w, b := newTestWorker(t, chainConfig, engine, db, 0)
defer w.close()

// This test chain imports the mined blocks.
b.genesis.MustCommit(db)

// Insert local tx
for i := 0; i < 10; i++ {
b.txPool.AddLocal(b.newRandomTx(true))
}

// Start mining!
w.start()
time.Sleep(time.Second * 5)
w.stop()

headHash := w.chain.CurrentHeader().Hash()
rawdb.DeleteBlockRowConsumption(db, headHash)
require.Nil(t, rawdb.ReadBlockRowConsumption(db, headHash))
w.start()
time.Sleep(time.Second)
// head should be rechecked by CCC
require.NotNil(t, rawdb.ReadBlockRowConsumption(db, headHash))
}

0 comments on commit 5c367f3

Please sign in to comment.