From 1546d70aaa9041f9087cb36ac2140ff0f278f59a Mon Sep 17 00:00:00 2001 From: Milap Sheth Date: Wed, 4 May 2022 18:10:30 +0000 Subject: [PATCH] fix(vald): use a new context for each query (#1463) (#1464) * fix: cancel rpc context after query * reduce duplication --- cmd/axelard/cmd/vald/start.go | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/cmd/axelard/cmd/vald/start.go b/cmd/axelard/cmd/vald/start.go index b02378e23..9d2b7fb00 100644 --- a/cmd/axelard/cmd/vald/start.go +++ b/cmd/axelard/cmd/vald/start.go @@ -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