Node/EVM: Fix race in network stats (#3493)

This commit is contained in:
bruce-riley 2023-11-02 16:05:07 -05:00 committed by GitHub
parent 33c0adbf82
commit 4f9400ca3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 2 deletions

View File

@ -615,7 +615,7 @@ func (w *Watcher) Run(parentCtx context.Context) error {
atomic.StoreUint64(&w.latestBlockNumber, blockNumberU)
currentEthHeight.WithLabelValues(w.networkName).Set(float64(blockNumberU))
stats.Height = int64(blockNumberU)
p2p.DefaultRegistry.SetNetworkStats(w.chainID, &stats)
w.updateNetworkStats(&stats)
continue
}
@ -630,7 +630,7 @@ func (w *Watcher) Run(parentCtx context.Context) error {
currentEthFinalizedHeight.WithLabelValues(w.networkName).Set(float64(blockNumberU))
stats.FinalizedHeight = int64(blockNumberU)
}
p2p.DefaultRegistry.SetNetworkStats(w.chainID, &stats)
w.updateNetworkStats(&stats)
w.pendingMu.Lock()
for key, pLock := range w.pending {
@ -926,3 +926,12 @@ func (w *Watcher) SetWaitForConfirmations(waitForConfirmations bool) {
func (w *Watcher) SetMaxWaitConfirmations(maxWaitConfirmations uint64) {
w.maxWaitConfirmations = maxWaitConfirmations
}
func (w *Watcher) updateNetworkStats(stats *gossipv1.Heartbeat_Network) {
p2p.DefaultRegistry.SetNetworkStats(w.chainID, &gossipv1.Heartbeat_Network{
Height: stats.Height,
SafeHeight: stats.SafeHeight,
FinalizedHeight: stats.FinalizedHeight,
ContractAddress: w.contract.Hex(),
})
}