@@ -95,15 +95,25 @@ func (service *IndexerService) SyncRange(start, end uint64, skipNode, skipData b
9595
9696func (service * IndexerService ) SyncLive () {
9797 for ; ; time .Sleep (service .config .BlockFrequency ) {
98+ reorgDepth , err := service .reorgWatcher .LookForReorg ()
99+ if err != nil {
100+ logger .WithField ("error" , err ).Error ("reorg lookup" )
101+ continue
102+ }
103+ if reorgDepth != 0 {
104+ indexingMetrics .ReorgBlockTotal (service .stateReader .chainID , reorgDepth )
105+ logger .WithField ("depth" , reorgDepth ).Info ("reorg detected" )
106+ }
107+
98108 state , err := service .stateReader .state ()
99109 if err != nil {
100110 logger .WithField ("error" , err ).Error ("cannot get state" )
101111 continue
102112 }
103113 logger .WithFields (state .Fields ()).Info ("last blocks" )
114+ indexingMetrics .BlockDifference (state .chainID , state .node - state .LastProcessed ())
104115
105- if err := service .reorgWatcher .LookForReorg (); err != nil {
106- logger .WithField ("error" , err ).Error ("reorg lookup" )
116+ if state .node == state .LastProcessed () {
107117 continue
108118 }
109119
@@ -184,6 +194,11 @@ type syncState struct {
184194 data uint64
185195}
186196
197+ // LastProcessed returns the real last block processed by taking the smallest block between state.data and state.blocks
198+ func (state syncState ) LastProcessed () uint64 {
199+ return min (state .data , state .blocks )
200+ }
201+
187202func (state syncState ) Fields () map [string ]interface {} {
188203 return map [string ]interface {}{
189204 "chainID" : state .chainID ,
@@ -236,8 +251,7 @@ func (service *Indexer) FromHead(state syncState) error {
236251 // clear balance cache
237252 defer service .balanceCache .Clear (state .chainID )
238253
239- // get the real last block processed by taking the smallest block between state.data and state.blocks
240- startBlock := max (min (state .data , state .blocks )+ 1 , 0 )
254+ startBlock := max (state .LastProcessed ()+ 1 , 0 )
241255 bulk := min (service .config .Bulk , state .node - startBlock + 1 )
242256
243257 for ; startBlock <= state .node ; startBlock += bulk {
@@ -257,9 +271,11 @@ func (service *Indexer) FromHead(state syncState) error {
257271 "elapsed" : time .Since (start ),
258272 }).Info ("indexed blocks" )
259273 }
274+ end := time .Since (start ) // save end to unify log and metric value
275+ indexingMetrics .IndexingTime (state .chainID , end )
260276
261277 logger .WithFields (logrus.Fields {
262- "duration" : time . Since ( start ) ,
278+ "duration" : end ,
263279 }).Info ("indexed head" )
264280 return nil
265281}
@@ -278,6 +294,7 @@ func (service *Indexer) Balances(state syncState) {
278294 continue
279295 }
280296 logger = logger .WithField ("pending" , total )
297+ indexingMetrics .PendingBalanceUpdate (state .chainID , total )
281298 if total == 0 {
282299 logger .Info ("finished updating balances" )
283300 return
@@ -312,6 +329,7 @@ func (service *Indexer) ENS(state syncState) {
312329 continue
313330 }
314331 logger = logger .WithField ("pending" , total )
332+ indexingMetrics .PendingENSUpdate (state .chainID , total )
315333 if total == 0 {
316334 logger .Info ("finished importing ens" )
317335 return
0 commit comments