Skip to content

Commit

Permalink
Delete rolled back L1 sequence on sequence rollback event (#1022)
Browse files Browse the repository at this point in the history
* Delete rolled back L1 sequence info on sequence rollback event

* Address CR comments
  • Loading branch information
cffls authored and hexoscott committed Sep 4, 2024
1 parent bde1b5e commit 88df27f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
seqAndVerifTopics := [][]libcommon.Hash{{
contracts.SequencedBatchTopicPreEtrog,
contracts.SequencedBatchTopicEtrog,
contracts.RollbackBatchesTopic,
contracts.VerificationTopicPreEtrog,
contracts.VerificationTopicEtrog,
contracts.VerificationValidiumTopicEtrog,
Expand Down
1 change: 1 addition & 0 deletions zk/contracts/l1_contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ var (
AddNewRollupTypeTopic = common.HexToHash("0xa2970448b3bd66ba7e524e7b2a5b9cf94fa29e32488fb942afdfe70dd4b77b52")
CreateNewRollupTopic = common.HexToHash("0x194c983456df6701c6a50830b90fe80e72b823411d0d524970c9590dc277a641")
UpdateRollupTopic = common.HexToHash("0xf585e04c05d396901170247783d3e5f0ee9c1df23072985b50af089f5e48b19d")
RollbackBatchesTopic = common.HexToHash("0x1125aaf62d132d8e2d02005114f8fc360ff204c3105e4f1a700a1340dc55d5b1")
)
21 changes: 21 additions & 0 deletions zk/hermez_db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,27 @@ func (db *HermezDb) WriteSequence(l1BlockNo, batchNo uint64, l1TxHash, stateRoot
return db.tx.Put(L1SEQUENCES, ConcatKey(l1BlockNo, batchNo), val)
}

// RollbackSequences deletes the sequences up to the given batch number
func (db *HermezDb) RollbackSequences(batchNo uint64) error {
for {
latestSequence, err := db.GetLatestSequence()
if err != nil {
return err
}

if latestSequence == nil || latestSequence.BatchNo <= batchNo {
break
}

err = db.tx.Delete(L1SEQUENCES, ConcatKey(latestSequence.L1BlockNo, latestSequence.BatchNo))
if err != nil {
return err
}
}

return nil
}

func (db *HermezDb) TruncateSequences(l2BlockNo uint64) error {
batchNo, err := db.GetBatchNoByL2Block(l2BlockNo)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions zk/stages/stage_l1syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ Loop:
highestWrittenL1BlockNo = info.L1BlockNo
}
newSequencesCount++
case logRollbackBatches:
if err := hermezDb.RollbackSequences(info.BatchNo); err != nil {
return fmt.Errorf("failed to write rollback sequence, %w", err)
}
if info.L1BlockNo > highestWrittenL1BlockNo {
highestWrittenL1BlockNo = info.L1BlockNo
}
case logVerify:
if info.BatchNo > highestVerification.BatchNo {
highestVerification = info
Expand Down Expand Up @@ -214,6 +221,7 @@ var (
logSequence BatchLogType = 1
logVerify BatchLogType = 2
logL1InfoTreeUpdate BatchLogType = 4
logRollbackBatches BatchLogType = 5

logIncompatible BatchLogType = 100
)
Expand Down Expand Up @@ -257,6 +265,9 @@ func parseLogType(l1RollupId uint64, log *ethTypes.Log) (l1BatchInfo types.L1Bat
}
case contracts.UpdateL1InfoTreeTopic:
batchLogType = logL1InfoTreeUpdate
case contracts.RollbackBatchesTopic:
batchLogType = logRollbackBatches
batchNum = new(big.Int).SetBytes(log.Topics[1].Bytes()).Uint64()
default:
batchLogType = logUnknown
batchNum = 0
Expand Down

0 comments on commit 88df27f

Please sign in to comment.