Skip to content

Commit

Permalink
fix(vald): use a new context for each query (#1463) (#1464)
Browse files Browse the repository at this point in the history
* fix: cancel rpc context after query

* reduce duplication
  • Loading branch information
milapsheth authored May 4, 2022
1 parent ea563f9 commit 1546d70
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions cmd/axelard/cmd/vald/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,26 +331,22 @@ func createJob(sub tmEvents.FilteredSubscriber, processor func(event tmEvents.Ev

// Wait until the node has synced with the network and return the node height
func waitTillNetworkSync(cfg config.ValdConfig, tmClient tmEvents.SyncInfoClient, logger log.Logger) (int64, error) {
rpcCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
for {
rpcCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
syncInfo, err := tmClient.LatestSyncInfo(rpcCtx)
cancel()
if err != nil {
return 0, err
}

syncInfo, err := tmClient.LatestSyncInfo(rpcCtx)
if err != nil {
return 0, err
}
// If the block height is older than the allowed time, then wait for the node to sync
if syncInfo.LatestBlockTime.Add(cfg.MaxLatestBlockAge).After(time.Now()) {
return syncInfo.LatestBlockHeight, nil
}

// If the block height is older than the allowed time, then wait for the node to sync
for syncInfo.LatestBlockTime.Add(cfg.MaxLatestBlockAge).Before(time.Now()) {
logger.Info(fmt.Sprintf("node height %d is old, waiting for a recent block", syncInfo.LatestBlockHeight))
time.Sleep(cfg.MaxLatestBlockAge)

syncInfo, err = tmClient.LatestSyncInfo(rpcCtx)
if err != nil {
return 0, err
}
}

return syncInfo.LatestBlockHeight, nil
}

// Return the block height to start listening to TM events from
Expand Down

0 comments on commit 1546d70

Please sign in to comment.