Skip to content

Commit 42f97ec

Browse files
committed
fix current block num set and init return
1 parent 8332876 commit 42f97ec

2 files changed

Lines changed: 30 additions & 20 deletions

File tree

tx-submitter/batch/batch_cache.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,13 @@ func (bc *BatchCache) InitAndSyncFromDatabase() error {
251251
}
252252
lastPackedBlockHeight = store.BlockNumber.Uint64()
253253
}
254+
bc.lastPackedBlockHeight = lastPackedBlockHeight
254255
bc.sealedBatches = batches
255256
bc.sealedBatchHeaders = headers
256257
bc.parentBatchHeader = latestHeaderBytes
257258
bc.currentBlockNumber = bc.lastPackedBlockHeight
258259
bc.prevStateRoot = prevStateRoot
259260
bc.totalL1MessagePopped = totalL1MessagePopped
260-
bc.lastPackedBlockHeight = lastPackedBlockHeight
261261

262262
bc.initDone = true
263263
log.Info("Sync sealed batch from database success", "count", len(batches))
@@ -426,18 +426,13 @@ func (bc *BatchCache) GetSealedBatchHeader(batchIndex uint64) (*BatchHeaderBytes
426426
defer bc.mu.RUnlock()
427427
header, ok := bc.sealedBatchHeaders[batchIndex]
428428
if !ok {
429-
// Try to load from storage
430-
bc.mu.RUnlock()
431-
bc.mu.Lock()
432-
defer bc.mu.Unlock()
433429
// Check again after acquiring write lock
434430
header, ok = bc.sealedBatchHeaders[batchIndex]
435431
if !ok {
436432
loadedHeader, err := bc.batchStorage.LoadSealedBatchHeader(batchIndex)
437433
if err != nil {
438434
return nil, false
439435
}
440-
bc.sealedBatchHeaders[batchIndex] = loadedHeader
441436
return loadedHeader, true
442437
}
443438
return header, true
@@ -1135,8 +1130,7 @@ func (bc *BatchCache) logSealedBatch(batchHeader BatchHeaderBytes, batchHash com
11351130

11361131
func (bc *BatchCache) AssembleCurrentBatchHeader() error {
11371132
if !bc.initDone {
1138-
log.Warn("batch has not been initialized, should wait")
1139-
return nil
1133+
return errors.New("batch has not been initialized, should wait")
11401134
}
11411135
callOpts := &bind.CallOpts{
11421136
Context: bc.ctx,
@@ -1148,9 +1142,26 @@ func (bc *BatchCache) AssembleCurrentBatchHeader() error {
11481142
if endBlockNum < bc.currentBlockNumber {
11491143
return fmt.Errorf("has reorg, should check block status current %v, now %v", bc.currentBlockNumber, endBlockNum)
11501144
}
1151-
startBlockNum, err := bc.parentBatchHeader.LastBlockNumber()
1152-
if err != nil {
1153-
return err
1145+
startBlockNum := uint64(0)
1146+
version, _ := bc.parentBatchHeader.Version()
1147+
if version < 1 {
1148+
parentIndex, err := bc.parentBatchHeader.BatchIndex()
1149+
if err != nil {
1150+
log.Error("failed to get block index", "err", err)
1151+
return err
1152+
}
1153+
store, err := bc.rollupContract.BatchDataStore(nil, new(big.Int).SetUint64(parentIndex))
1154+
if err != nil {
1155+
log.Error("failed to get batch store", "err", err)
1156+
return err
1157+
}
1158+
startBlockNum = store.BlockNumber.Uint64()
1159+
} else {
1160+
startBlockNum, err = bc.parentBatchHeader.LastBlockNumber()
1161+
if err != nil {
1162+
log.Error("failed to get block number", "err", err)
1163+
return err
1164+
}
11541165
}
11551166
startBlockNum++
11561167
// Get start block once to avoid repeated queries

tx-submitter/services/rollup.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ func (r *Rollup) Start() error {
266266
time.Sleep(5 * time.Second)
267267
continue
268268
}
269+
break
269270
}
270271
}()
271272

@@ -881,22 +882,20 @@ func (r *Rollup) finalize() error {
881882
return nil
882883
}
883884
// get next batch
884-
nextBatchIndex := target.Uint64() + 1
885-
886-
rollupBatch, err := GetRollupBatchByIndex(nextBatchIndex, r.L2Clients)
887-
if err != nil {
888-
log.Warn("get next rollupBatch by index failed, rollupBatch not found",
889-
"batch_index", nextBatchIndex,
885+
rollupBatchHeader, exist := r.batchCache.GetSealedBatchHeader(target.Uint64())
886+
if !exist {
887+
log.Warn("get rollupBatch by index failed, rollupBatch not found",
888+
"batch_index", target.Uint64(),
890889
)
891890
return nil
892891
}
893-
if rollupBatch == nil {
894-
log.Info("next rollupBatch is nil,wait next rollupBatch header to finalize", "next_batch_index", nextBatchIndex)
892+
if rollupBatchHeader == nil {
893+
log.Info("next rollupBatch is nil,wait rollupBatch header to finalize", "batch_index", target.Uint64())
895894
return nil
896895
}
897896

898897
// calldata
899-
calldata, err := r.abi.Pack("finalizeBatch", []byte(rollupBatch.ParentBatchHeader))
898+
calldata, err := r.abi.Pack("finalizeBatch", rollupBatchHeader.Bytes())
900899
if err != nil {
901900
return fmt.Errorf("pack finalizeBatch error:%v", err)
902901
}

0 commit comments

Comments
 (0)