diff --git a/testutil/network/network.go b/testutil/network/network.go index f2930671a635..30e151518037 100644 --- a/testutil/network/network.go +++ b/testutil/network/network.go @@ -12,6 +12,7 @@ import ( "path/filepath" "strings" "sync" + "sync/atomic" "syscall" "testing" "time" @@ -647,29 +648,29 @@ func (n *Network) LatestHeight() (int64, error) { timeout := time.NewTimer(time.Second * 5) defer timeout.Stop() - var latestHeight int64 + var latestHeight atomic.Int64 val := n.Validators[0] queryClient := cmtservice.NewServiceClient(val.clientCtx) for { select { case <-timeout.C: - return latestHeight, errors.New("timeout exceeded waiting for block") + return latestHeight.Load(), errors.New("timeout exceeded waiting for block") case <-ticker.C: done := make(chan struct{}) go func() { res, err := queryClient.GetLatestBlock(context.Background(), &cmtservice.GetLatestBlockRequest{}) if err == nil && res != nil { - latestHeight = res.SdkBlock.Header.Height + latestHeight.Store(res.SdkBlock.Header.Height) } done <- struct{}{} }() select { case <-timeout.C: - return latestHeight, errors.New("timeout exceeded waiting for block") + return latestHeight.Load(), errors.New("timeout exceeded waiting for block") case <-done: - if latestHeight != 0 { - return latestHeight, nil + if latestHeight.Load() != 0 { + return latestHeight.Load(), nil } } }