diff --git a/eth/backend.go b/eth/backend.go index aad639d4c4c..6dbfd0c6506 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -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, diff --git a/zk/contracts/l1_contracts.go b/zk/contracts/l1_contracts.go index 7c4bb7f3d80..7ba052d7872 100644 --- a/zk/contracts/l1_contracts.go +++ b/zk/contracts/l1_contracts.go @@ -16,4 +16,5 @@ var ( AddNewRollupTypeTopic = common.HexToHash("0xa2970448b3bd66ba7e524e7b2a5b9cf94fa29e32488fb942afdfe70dd4b77b52") CreateNewRollupTopic = common.HexToHash("0x194c983456df6701c6a50830b90fe80e72b823411d0d524970c9590dc277a641") UpdateRollupTopic = common.HexToHash("0xf585e04c05d396901170247783d3e5f0ee9c1df23072985b50af089f5e48b19d") + RollbackBatchesTopic = common.HexToHash("0x1125aaf62d132d8e2d02005114f8fc360ff204c3105e4f1a700a1340dc55d5b1") ) diff --git a/zk/hermez_db/db.go b/zk/hermez_db/db.go index 24de3570578..b3e6319a9d4 100644 --- a/zk/hermez_db/db.go +++ b/zk/hermez_db/db.go @@ -528,6 +528,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 { diff --git a/zk/stages/stage_l1syncer.go b/zk/stages/stage_l1syncer.go index 513351e997a..8362247b8a4 100644 --- a/zk/stages/stage_l1syncer.go +++ b/zk/stages/stage_l1syncer.go @@ -149,6 +149,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 @@ -222,6 +229,7 @@ var ( logSequence BatchLogType = 1 logVerify BatchLogType = 2 logL1InfoTreeUpdate BatchLogType = 4 + logRollbackBatches BatchLogType = 5 logIncompatible BatchLogType = 100 ) @@ -265,6 +273,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