Skip to content

Commit

Permalink
add new RevertBatch event
Browse files Browse the repository at this point in the history
  • Loading branch information
jonastheis committed Feb 25, 2025
1 parent 4d100c1 commit ee53754
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 54 deletions.
49 changes: 43 additions & 6 deletions rollup/da_syncer/batch_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/scroll-tech/go-ethereum/ethdb"
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/rollup/da_syncer/da"
"github.com/scroll-tech/go-ethereum/rollup/l1"
)

// BatchQueue is a pipeline stage that reads all batch events from DAQueue and provides only finalized batches to the next stage.
Expand Down Expand Up @@ -54,7 +55,9 @@ func (bq *BatchQueue) NextBatch(ctx context.Context) (da.EntryWithBlocks, error)
case da.CommitBatchV0Type, da.CommitBatchWithBlobType:
bq.addBatch(daEntry)
case da.RevertBatchType:
bq.processAndDeleteBatch(daEntry)
if err = bq.handleRevertEvent(daEntry.Event()); err != nil {
return nil, fmt.Errorf("failed to handle revert event: %w", err)
}
case da.FinalizeBatchType:
if daEntry.BatchIndex() > bq.lastFinalizedBatchIndex {
bq.lastFinalizedBatchIndex = daEntry.BatchIndex()
Expand Down Expand Up @@ -90,17 +93,51 @@ func (bq *BatchQueue) addBatch(batch da.Entry) {
bq.batchesMap.Set(batch.BatchIndex(), heapElement)
}

func (bq *BatchQueue) handleRevertEvent(event l1.RollupEvent) error {
switch event.Type() {
case l1.RevertEventV0Type:
revertBatch, ok := event.(*l1.RevertBatchEventV0)
if !ok {
return fmt.Errorf("unexpected type of revert event: %T, expected RevertEventV0Type", event)
}

bq.deleteBatch(revertBatch.BatchIndex().Uint64())
case l1.RevertEventV7Type:
revertBatch, ok := event.(*l1.RevertBatchEventV7)
if !ok {
return fmt.Errorf("unexpected type of revert event: %T, expected RevertEventV7Type", event)
}

// delete all batches from revertBatch.StartBatchIndex (inclusive) to revertBatch.FinishBatchIndex (inclusive)
for i := revertBatch.StartBatchIndex().Uint64(); i <= revertBatch.FinishBatchIndex().Uint64(); i++ {
bq.deleteBatch(i)
}
default:
return fmt.Errorf("unexpected type of revert event: %T", event)
}

return nil
}

func (bq *BatchQueue) deleteBatch(batchIndex uint64) (deleted bool) {
batchHeapElement, exists := bq.batchesMap.Get(batchIndex)
if !exists {
return false
}

bq.batchesMap.Delete(batchIndex)
bq.batches.Remove(batchHeapElement)

return true
}

// processAndDeleteBatch deletes data committed in the batch from map, because this batch is reverted or finalized
// updates DASyncedL1BlockNumber
func (bq *BatchQueue) processAndDeleteBatch(batch da.Entry) da.EntryWithBlocks {
batchHeapElement, exists := bq.batchesMap.Get(batch.BatchIndex())
if !exists {
if !bq.deleteBatch(batch.BatchIndex()) {
return nil
}

bq.batchesMap.Delete(batch.BatchIndex())
bq.batches.Remove(batchHeapElement)

entryWithBlocks, ok := batch.(da.EntryWithBlocks)
if !ok {
// this should only happen if we delete a reverted batch
Expand Down
10 changes: 2 additions & 8 deletions rollup/da_syncer/da/calldata_blob_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,15 @@ func (ds *CalldataBlobSource) processRollupEventsToDA(rollupEvents l1.RollupEven
// add commit event to the list of previous commit events, so we can process events created in the same tx together
lastCommitTransactionHash = commitEvent.TxHash()
lastCommitEvents = append(lastCommitEvents, commitEvent)
case l1.RevertEventType:
case l1.RevertEventV0Type, l1.RevertEventV7Type:
// if we have any previous commit events, we need to create a new DA before processing the revert event
if len(lastCommitEvents) > 0 {
if err = getAndAppendCommitBatchDA(); err != nil {
return nil, fmt.Errorf("failed to get and append commit batch DA: %w", err)
}
}

revertEvent, ok := rollupEvent.(*l1.RevertBatchEvent)
// this should never happen because we just check event type
if !ok {
return nil, fmt.Errorf("unexpected type of rollup event: %T", rollupEvent)
}

entry = NewRevertBatch(revertEvent)
entry = NewRevertBatch(rollupEvent)
entries = append(entries, entry)
case l1.FinalizeEventType:
// if we have any previous commit events, we need to create a new DA before processing the finalized event
Expand Down
5 changes: 3 additions & 2 deletions rollup/da_syncer/da/revert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
)

type RevertBatch struct {
event *l1.RevertBatchEvent
event l1.RollupEvent
}

func NewRevertBatch(event *l1.RevertBatchEvent) *RevertBatch {
func NewRevertBatch(event l1.RollupEvent) *RevertBatch {
return &RevertBatch{
event: event,
}
Expand All @@ -21,6 +21,7 @@ func (r *RevertBatch) Type() Type {
func (r *RevertBatch) L1BlockNumber() uint64 {
return r.event.BlockNumber()
}

func (r *RevertBatch) BatchIndex() uint64 {
return r.event.BatchIndex().Uint64()
}
Expand Down
80 changes: 67 additions & 13 deletions rollup/l1/abi.go

Large diffs are not rendered by default.

40 changes: 33 additions & 7 deletions rollup/l1/abi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import (
)

func TestEventSignatures(t *testing.T) {
assert.Equal(t, crypto.Keccak256Hash([]byte("CommitBatch(uint256,bytes32)")), ScrollChainABI.Events["CommitBatch"].ID)
assert.Equal(t, crypto.Keccak256Hash([]byte("RevertBatch(uint256,bytes32)")), ScrollChainABI.Events["RevertBatch"].ID)
assert.Equal(t, crypto.Keccak256Hash([]byte("FinalizeBatch(uint256,bytes32,bytes32,bytes32)")), ScrollChainABI.Events["FinalizeBatch"].ID)
assert.Equal(t, crypto.Keccak256Hash([]byte("CommitBatch(uint256,bytes32)")), ScrollChainABI.Events[commitBatchEventName].ID)
assert.Equal(t, crypto.Keccak256Hash([]byte("RevertBatch(uint256,bytes32)")), ScrollChainABI.Events[revertBatchV0EventName].ID)
assert.Equal(t, crypto.Keccak256Hash([]byte("RevertBatch(uint256,uint256)")), ScrollChainABI.Events[revertBatchV7EventName].ID)
assert.Equal(t, crypto.Keccak256Hash([]byte("FinalizeBatch(uint256,bytes32,bytes32,bytes32)")), ScrollChainABI.Events[finalizeBatchEventName].ID)
}

func TestUnpackLog(t *testing.T) {
mockBatchIndex := big.NewInt(123)
finishMockBatchIndex := big.NewInt(125)
mockBatchHash := crypto.Keccak256Hash([]byte("mockBatch"))
mockStateRoot := crypto.Keccak256Hash([]byte("mockStateRoot"))
mockWithdrawRoot := crypto.Keccak256Hash([]byte("mockWithdrawRoot"))
Expand All @@ -42,16 +44,40 @@ func TestUnpackLog(t *testing.T) {
&CommitBatchEventUnpacked{},
},
{
revertBatchEventName,
revertBatchV0EventName,
types.Log{
Data: nil,
Topics: []common.Hash{ScrollChainABI.Events[revertBatchEventName].ID, common.BigToHash(mockBatchIndex), mockBatchHash},
Topics: []common.Hash{ScrollChainABI.Events[revertBatchV0EventName].ID, common.BigToHash(mockBatchIndex), mockBatchHash},
},
&RevertBatchEventUnpacked{
&RevertBatchEventV0Unpacked{
BatchIndex: mockBatchIndex,
BatchHash: mockBatchHash,
},
&RevertBatchEventUnpacked{},
&RevertBatchEventV0Unpacked{},
},
{
revertBatchV7EventName,
types.Log{
Data: nil,
Topics: []common.Hash{ScrollChainABI.Events[revertBatchV7EventName].ID, common.BigToHash(mockBatchIndex), common.BigToHash(mockBatchIndex)},
},
&RevertBatchEventV7Unpacked{
StartBatchIndex: mockBatchIndex,
FinishBatchIndex: mockBatchIndex,
},
&RevertBatchEventV7Unpacked{},
},
{
revertBatchV7EventName,
types.Log{
Data: nil,
Topics: []common.Hash{ScrollChainABI.Events[revertBatchV7EventName].ID, common.BigToHash(mockBatchIndex), common.BigToHash(finishMockBatchIndex)},
},
&RevertBatchEventV7Unpacked{
StartBatchIndex: mockBatchIndex,
FinishBatchIndex: finishMockBatchIndex,
},
&RevertBatchEventV7Unpacked{},
},
{
finalizeBatchEventName,
Expand Down
52 changes: 35 additions & 17 deletions rollup/l1/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (

const (
commitBatchEventName = "CommitBatch"
revertBatchEventName = "RevertBatch"
revertBatchV0EventName = "RevertBatch"
revertBatchV7EventName = "RevertBatch0"
finalizeBatchEventName = "FinalizeBatch"
nextUnfinalizedQueueIndex = "nextUnfinalizedQueueIndex"
lastFinalizedBatchIndex = "lastFinalizedBatchIndex"
Expand All @@ -32,7 +33,8 @@ type Reader struct {
scrollChainABI *abi.ABI
l1MessageQueueABI *abi.ABI
l1CommitBatchEventSignature common.Hash
l1RevertBatchEventSignature common.Hash
l1RevertBatchEventV0Signature common.Hash
l1RevertBatchEventV7Signature common.Hash
l1FinalizeBatchEventSignature common.Hash
}

Expand Down Expand Up @@ -60,7 +62,8 @@ func NewReader(ctx context.Context, config Config, l1Client Client) (*Reader, er
scrollChainABI: ScrollChainABI,
l1MessageQueueABI: L1MessageQueueABIManual,
l1CommitBatchEventSignature: ScrollChainABI.Events[commitBatchEventName].ID,
l1RevertBatchEventSignature: ScrollChainABI.Events[revertBatchEventName].ID,
l1RevertBatchEventV0Signature: ScrollChainABI.Events[revertBatchV0EventName].ID,
l1RevertBatchEventV7Signature: ScrollChainABI.Events[revertBatchV7EventName].ID,
l1FinalizeBatchEventSignature: ScrollChainABI.Events[finalizeBatchEventName].ID,
}

Expand Down Expand Up @@ -172,10 +175,11 @@ func (r *Reader) FetchRollupEventsInRange(from, to uint64) (RollupEvents, error)
},
Topics: make([][]common.Hash, 1),
}
query.Topics[0] = make([]common.Hash, 3)
query.Topics[0] = make([]common.Hash, 4)
query.Topics[0][0] = r.l1CommitBatchEventSignature
query.Topics[0][1] = r.l1RevertBatchEventSignature
query.Topics[0][2] = r.l1FinalizeBatchEventSignature
query.Topics[0][1] = r.l1RevertBatchEventV0Signature
query.Topics[0][2] = r.l1RevertBatchEventV7Signature
query.Topics[0][3] = r.l1FinalizeBatchEventSignature

logsBatch, err := r.client.FilterLogs(r.ctx, query)
if err != nil {
Expand Down Expand Up @@ -203,10 +207,11 @@ func (r *Reader) FetchRollupEventsInRangeWithCallback(from, to uint64, callback
},
Topics: make([][]common.Hash, 1),
}
query.Topics[0] = make([]common.Hash, 3)
query.Topics[0] = make([]common.Hash, 4)
query.Topics[0][0] = r.l1CommitBatchEventSignature
query.Topics[0][1] = r.l1RevertBatchEventSignature
query.Topics[0][2] = r.l1FinalizeBatchEventSignature
query.Topics[0][1] = r.l1RevertBatchEventV0Signature
query.Topics[0][2] = r.l1RevertBatchEventV7Signature
query.Topics[0][3] = r.l1FinalizeBatchEventSignature

logsBatch, err := r.client.FilterLogs(r.ctx, query)
if err != nil {
Expand Down Expand Up @@ -245,7 +250,7 @@ func (r *Reader) processLogsToRollupEvents(logs []types.Log) (RollupEvents, erro
if err = UnpackLog(r.scrollChainABI, event, commitBatchEventName, vLog); err != nil {
return nil, fmt.Errorf("failed to unpack commit rollup event log, err: %w", err)
}
log.Trace("found new CommitBatch event", "batch index", event.BatchIndex.Uint64())
log.Trace("found new CommitBatch event", "batch index", event.BatchIndex.Uint64(), "batch hash", event.BatchHash.Hex())
rollupEvent = &CommitBatchEvent{
batchIndex: event.BatchIndex,
batchHash: event.BatchHash,
Expand All @@ -254,26 +259,39 @@ func (r *Reader) processLogsToRollupEvents(logs []types.Log) (RollupEvents, erro
blockNumber: vLog.BlockNumber,
}

case r.l1RevertBatchEventSignature:
event := &RevertBatchEventUnpacked{}
if err = UnpackLog(r.scrollChainABI, event, revertBatchEventName, vLog); err != nil {
return nil, fmt.Errorf("failed to unpack revert rollup event log, err: %w", err)
case r.l1RevertBatchEventV0Signature:
event := &RevertBatchEventV0Unpacked{}
if err = UnpackLog(r.scrollChainABI, event, revertBatchV0EventName, vLog); err != nil {
return nil, fmt.Errorf("failed to unpack revert V0 rollup event log, err: %w", err)
}
log.Trace("found new RevertBatchType event", "batch index", event.BatchIndex.Uint64())
rollupEvent = &RevertBatchEvent{
log.Trace("found new RevertBatchV0Type event", "batch index", event.BatchIndex.Uint64(), "batch hash", event.BatchHash.Hex())
rollupEvent = &RevertBatchEventV0{
batchIndex: event.BatchIndex,
batchHash: event.BatchHash,
txHash: vLog.TxHash,
blockHash: vLog.BlockHash,
blockNumber: vLog.BlockNumber,
}
case r.l1RevertBatchEventV7Signature:
event := &RevertBatchEventV7Unpacked{}
if err = UnpackLog(r.scrollChainABI, event, revertBatchV7EventName, vLog); err != nil {
return nil, fmt.Errorf("failed to unpack revert V7 rollup event log, err: %w", err)
}

log.Trace("found new RevertBatchV7Type event", "start batch index", event.StartBatchIndex.Uint64(), "finish batch index", event.FinishBatchIndex.Uint64())
rollupEvent = &RevertBatchEventV7{
startBatchIndex: event.StartBatchIndex,
finishBatchIndex: event.FinishBatchIndex,
txHash: vLog.TxHash,
blockHash: vLog.BlockHash,
blockNumber: vLog.BlockNumber,
}
case r.l1FinalizeBatchEventSignature:
event := &FinalizeBatchEventUnpacked{}
if err = UnpackLog(r.scrollChainABI, event, finalizeBatchEventName, vLog); err != nil {
return nil, fmt.Errorf("failed to unpack finalized rollup event log, err: %w", err)
}
log.Trace("found new FinalizeBatchType event", "batch index", event.BatchIndex.Uint64())
log.Trace("found new FinalizeBatchType event", "batch index", event.BatchIndex.Uint64(), "batch hash", event.BatchHash.Hex())
rollupEvent = &FinalizeBatchEvent{
batchIndex: event.BatchIndex,
batchHash: event.BatchHash,
Expand Down
31 changes: 30 additions & 1 deletion rollup/rollup_sync_service/rollup_sync_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@ func (s *RollupSyncService) updateRollupEvents(daEntries da.Entries) error {

case da.RevertBatchType:
log.Trace("found new RevertBatch event", "batch index", entry.BatchIndex())
rawdb.DeleteCommittedBatchMeta(s.db, entry.BatchIndex())
if err := s.handleRevertEvent(entry.Event()); err != nil {
return fmt.Errorf("failed to handle revert event, batch index: %v, err: %w", entry.BatchIndex(), err)
}

case da.FinalizeBatchType:
event, ok := entry.Event().(*l1.FinalizeBatchEvent)
Expand Down Expand Up @@ -321,6 +323,33 @@ func (s *RollupSyncService) updateRollupEvents(daEntries da.Entries) error {
return nil
}

func (s *RollupSyncService) handleRevertEvent(event l1.RollupEvent) error {
switch event.Type() {
case l1.RevertEventV0Type:
revertBatch, ok := event.(*l1.RevertBatchEventV0)
if !ok {
return fmt.Errorf("unexpected type of revert event: %T, expected RevertEventV0Type", event)
}

rawdb.DeleteCommittedBatchMeta(s.db, revertBatch.BatchIndex().Uint64())

case l1.RevertEventV7Type:
revertBatch, ok := event.(*l1.RevertBatchEventV7)
if !ok {
return fmt.Errorf("unexpected type of revert event: %T, expected RevertEventV7Type", event)
}

// delete all batches from revertBatch.StartBatchIndex (inclusive) to revertBatch.FinishBatchIndex (inclusive)
for i := revertBatch.StartBatchIndex().Uint64(); i <= revertBatch.FinishBatchIndex().Uint64(); i++ {
rawdb.DeleteCommittedBatchMeta(s.db, i)
}
default:
return fmt.Errorf("unexpected type of revert event: %T", event)
}

return nil
}

func (s *RollupSyncService) getLocalChunksForBatch(chunkBlockRanges []*rawdb.ChunkBlockRange) ([]*encoding.Chunk, error) {
if len(chunkBlockRanges) == 0 {
return nil, fmt.Errorf("chunkBlockRanges is empty")
Expand Down

0 comments on commit ee53754

Please sign in to comment.