Skip to content

Commit

Permalink
If initial get chain id fails retry
Browse files Browse the repository at this point in the history
  • Loading branch information
marino39 committed Aug 15, 2023
1 parent f39ee54 commit 5da30cf
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions ethmonitor/ethmonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func NewMonitor(provider ethrpc.Interface, options ...Options) (*Monitor, error)
chainID := ethkit.NewLazy(func() *big.Int {
chainId, err := getChainID(provider)
if err != nil {
return big.NewInt(-1)
return nil
}
return chainId
})
Expand Down Expand Up @@ -473,7 +473,7 @@ func (m *Monitor) filterLogs(ctx context.Context, blockHash common.Hash, topics
topicsDigest.Write([]byte{'\n'})
}

key := fmt.Sprintf("ethmonitor:%s:Logs:hash=%s;topics=%d", m.chainID.Get().String(), blockHash.String(), topicsDigest.Sum64())
key := fmt.Sprintf("ethmonitor:%s:Logs:hash=%s;topics=%d", m.getChainID().String(), blockHash.String(), topicsDigest.Sum64())
return m.logCache.GetOrSetWithLockEx(ctx, key, getter, m.options.CacheExpiry)
}

Expand Down Expand Up @@ -536,7 +536,7 @@ func (m *Monitor) fetchNextBlock(ctx context.Context) (*types.Block, bool, error
}

if m.blockCache != nil {
key := fmt.Sprintf("ethmonitor:%s:BlockNum:%s", m.chainID.Get().String(), m.nextBlockNumber.String())
key := fmt.Sprintf("ethmonitor:%s:BlockNum:%s", m.getChainID().String(), m.nextBlockNumber.String())
nextBlock, err := m.blockCache.GetOrSetWithLockEx(ctx, key, getter, m.options.CacheExpiry)
return nextBlock, miss, err
}
Expand Down Expand Up @@ -625,7 +625,7 @@ func (m *Monitor) fetchBlockByHash(ctx context.Context, hash common.Hash) (*type
}

if m.blockCache != nil {
key := fmt.Sprintf("ethmonitor:%s:BlockHash:%s", m.chainID.Get().String(), hash.String())
key := fmt.Sprintf("ethmonitor:%s:BlockHash:%s", m.getChainID().String(), hash.String())
return m.blockCache.GetOrSetWithLockEx(ctx, key, getter, m.options.CacheExpiry)
}
return getter(ctx, "")
Expand Down Expand Up @@ -775,6 +775,14 @@ func (m *Monitor) PurgeHistory() {
}
}

func (m *Monitor) getChainID() *big.Int {
if val := m.chainID.Get(); val == nil {
return big.NewInt(-1)
} else {
return val
}
}

func getChainID(provider ethrpc.Interface) (*big.Int, error) {
var chainID *big.Int
err := breaker.Do(context.Background(), func() error {
Expand Down

0 comments on commit 5da30cf

Please sign in to comment.