@@ -524,7 +524,7 @@ func (m *Monitor) monitor() error {
524524 }
525525
526526 // fetch the next block, either via the stream or via a poll
527- nextBlock , nextBlockPayload , miss , err := m .fetchNextBlock (ctx )
527+ nextBlock , _ , miss , err := m .fetchNextBlock (ctx )
528528 if err != nil {
529529 if errors .Is (err , context .DeadlineExceeded ) {
530530 m .log .Info (fmt .Sprintf ("ethmonitor: fetchNextBlock timed out: '%v', for blockNum:%v, retrying.." , err , m .nextBlockNumber ))
@@ -546,7 +546,7 @@ func (m *Monitor) monitor() error {
546546 }
547547
548548 // build deterministic set of add/remove events which construct the canonical chain
549- events , err = m .buildCanonicalChain (ctx , nextBlock , nextBlockPayload , events )
549+ events , err = m .buildCanonicalChain (ctx , nextBlock , events )
550550 if err != nil {
551551 m .log .Warn (fmt .Sprintf ("ethmonitor: error reported '%v', failed to build chain for next blockNum:%d blockHash:%s, retrying.." ,
552552 err , nextBlock .NumberU64 (), nextBlock .Hash ().Hex ()))
@@ -582,7 +582,7 @@ func (m *Monitor) monitor() error {
582582 }
583583}
584584
585- func (m * Monitor ) buildCanonicalChain (ctx context.Context , nextBlock * types.Block , nextBlockPayload [] byte , events Blocks ) (Blocks , error ) {
585+ func (m * Monitor ) buildCanonicalChain (ctx context.Context , nextBlock * types.Block , events Blocks ) (Blocks , error ) {
586586 select {
587587 case <- ctx .Done ():
588588 return nil , ctx .Err ()
@@ -629,13 +629,13 @@ func (m *Monitor) buildCanonicalChain(ctx context.Context, nextBlock *types.Bloc
629629 time .Sleep (pause )
630630
631631 // Fetch/connect the broken chain backwards by traversing recursively via parent hashes
632- nextParentBlock , nextParentBlockPayload , err := m .fetchBlockByHash (ctx , nextBlock .ParentHash ())
632+ nextParentBlock , _ , err := m .fetchBlockByHash (ctx , nextBlock .ParentHash ())
633633 if err != nil {
634634 // NOTE: this is okay, it will auto-retry
635635 return events , err
636636 }
637637
638- events , err = m .buildCanonicalChain (ctx , nextParentBlock , nextParentBlockPayload , events )
638+ events , err = m .buildCanonicalChain (ctx , nextParentBlock , events )
639639 if err != nil {
640640 // NOTE: this is okay, it will auto-retry
641641 return events , err
@@ -796,7 +796,10 @@ func (m *Monitor) fetchNextBlock(ctx context.Context) (*types.Block, []byte, boo
796796
797797 getter := func (ctx context.Context , _ string ) ([]byte , error ) {
798798 if m .options .DebugLogging {
799- m .log .Debug (fmt .Sprintf ("ethmonitor: fetchNextBlock is calling origin for number %s" , m .nextBlockNumber ))
799+ m .nextBlockNumberMu .Lock ()
800+ nextNumForLog := m .nextBlockNumber
801+ m .nextBlockNumberMu .Unlock ()
802+ m .log .Debug (fmt .Sprintf ("ethmonitor: fetchNextBlock is calling origin for number %v" , nextNumForLog ))
800803 }
801804 for {
802805 select {
@@ -805,9 +808,15 @@ func (m *Monitor) fetchNextBlock(ctx context.Context) (*types.Block, []byte, boo
805808 default :
806809 }
807810
808- nextBlockPayload , err := m .fetchRawBlockByNumber (ctx , m .nextBlockNumber )
811+ m .nextBlockNumberMu .Lock ()
812+ nextNum := m .nextBlockNumber
813+ m .nextBlockNumberMu .Unlock ()
814+ nextBlockPayload , err := m .fetchRawBlockByNumber (ctx , nextNum )
809815 if err != nil {
810- m .log .Debug (fmt .Sprintf ("ethmonitor: [retrying] failed to fetch next block # %d, due to: %v" , m .nextBlockNumber , err ))
816+ m .nextBlockNumberMu .Lock ()
817+ logNum := m .nextBlockNumber
818+ m .nextBlockNumberMu .Unlock ()
819+ m .log .Debug (fmt .Sprintf ("ethmonitor: [retrying] failed to fetch next block # %v, due to: %v" , logNum , err ))
811820 miss = true
812821 if m .IsStreamingMode () {
813822 // in streaming mode, we'll use a shorter time to pause before we refetch
@@ -996,7 +1005,16 @@ func (m *Monitor) publish(ctx context.Context, events Blocks) error {
9961005 // Check for trail-behind-head mode and set maxBlockNum if applicable
9971006 maxBlockNum := uint64 (0 )
9981007 if m .options .TrailNumBlocksBehindHead > 0 {
999- maxBlockNum = m .LatestBlock ().NumberU64 () - uint64 (m .options .TrailNumBlocksBehindHead )
1008+ latest := m .LatestBlock ()
1009+ if latest != nil {
1010+ lb := latest .NumberU64 ()
1011+ trail := uint64 (m .options .TrailNumBlocksBehindHead )
1012+ if lb > trail {
1013+ maxBlockNum = lb - trail
1014+ } else {
1015+ maxBlockNum = 0
1016+ }
1017+ }
10001018 }
10011019
10021020 // Enqueue
0 commit comments