Skip to content

Commit

Permalink
optimize(indexer): update l2_scanned_blocks every 10min
Browse files Browse the repository at this point in the history
  • Loading branch information
bendanzhentan committed Mar 3, 2024
1 parent 6f7fb36 commit 14bd839
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions core/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func NewIndexer(log log.Logger, db *gorm.DB, l2Client *ClientExt, cfg Config) *I
func (i *Indexer) Start(ctx context.Context, l2ScannedBlock *L2ScannedBlock) {
timer := time.NewTimer(0)
fromBlockNumber := big.NewInt(l2ScannedBlock.Number)
lastTimeL2ScannedBlock := time.Now()

for {
select {
Expand Down Expand Up @@ -140,10 +141,14 @@ func (i *Indexer) Start(ctx context.Context, l2ScannedBlock *L2ScannedBlock) {
}
}

l2ScannedBlock.Number = toBlockNumber.Int64()
result := i.db.Save(l2ScannedBlock)
if result.Error != nil {
log.Error("update l2_scanned_blocks", "error", result.Error)
if lastTimeL2ScannedBlock.Before(time.Now().Add(-10 * time.Minute)) {
l2ScannedBlock.Number = toBlockNumber.Int64()
result := i.db.Save(l2ScannedBlock)
if result.Error != nil {
log.Error("update l2_scanned_blocks", "error", result.Error)
}

lastTimeL2ScannedBlock = time.Now()
}

fromBlockNumber = new(big.Int).Add(toBlockNumber, big.NewInt(1))
Expand Down

0 comments on commit 14bd839

Please sign in to comment.