From b0a9e98648fe1e329c4f41826912a9ade95076e1 Mon Sep 17 00:00:00 2001 From: Leopold Schabel Date: Fri, 5 Feb 2021 15:16:31 +0100 Subject: [PATCH] Asynchronously fetch Terra block height (#177) --- bridge/pkg/solana/client.go | 2 ++ bridge/pkg/terra/watcher.go | 49 +++++++++++++++++++++++-------------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/bridge/pkg/solana/client.go b/bridge/pkg/solana/client.go index 3f79e0c94..1b55c540c 100644 --- a/bridge/pkg/solana/client.go +++ b/bridge/pkg/solana/client.go @@ -92,6 +92,8 @@ func (s *SolanaWatcher) Run(ctx context.Context) error { } currentSolanaHeight.Set(float64(slot)) + logger.Info("current Solana height", zap.Uint64("slot", uint64(slot))) + // Find TransferOutProposal accounts without a VAA rCtx, cancel = context.WithTimeout(ctx, time.Second*5) defer cancel() diff --git a/bridge/pkg/terra/watcher.go b/bridge/pkg/terra/watcher.go index 00eddcd93..61b66b152 100644 --- a/bridge/pkg/terra/watcher.go +++ b/bridge/pkg/terra/watcher.go @@ -118,6 +118,37 @@ func (e *BridgeWatcher) Run(ctx context.Context) error { readiness.SetReady(common.ReadinessTerraSyncing) + go func() { + t := time.NewTicker(5 * time.Second) + client := &http.Client{ + Timeout: time.Second * 5, + } + + for { + <-t.C + + // Query and report height and set currentTerraHeight + resp, err := client.Get(fmt.Sprintf("%s/blocks/latest", e.urlLCD)) + if err != nil { + logger.Error("query latest block response error", zap.Error(err)) + continue + } + blocksBody, err := ioutil.ReadAll(resp.Body) + if err != nil { + logger.Error("query guardian set error", zap.Error(err)) + errC <- err + resp.Body.Close() + continue + } + resp.Body.Close() + + blockJSON := string(blocksBody) + latestBlock := gjson.Get(blockJSON, "block.header.height") + logger.Info("current Terra height", zap.Int64("block", latestBlock.Int())) + currentTerraHeight.Set(float64(latestBlock.Int())) + } + }() + go func() { defer close(errC) @@ -199,24 +230,6 @@ func (e *BridgeWatcher) Run(ctx context.Context) error { client := &http.Client{ Timeout: time.Second * 15, } - // Query and report height and set currentTerraHeight - blocksResp, err := client.Get(fmt.Sprintf("%s/blocks/latest", e.urlLCD)) - if err != nil { - logger.Error("query latest block response error", zap.Error(err)) - errC <- err - return - } - blocksBody, err := ioutil.ReadAll(blocksResp.Body) - if err != nil { - logger.Error("query guardian set error", zap.Error(err)) - errC <- err - blocksResp.Body.Close() - return - } - blockJSON := string(blocksBody) - latestBlock := gjson.Get(blockJSON, "block.header.height") - logger.Info("Current Terra height", zap.Int64("block", latestBlock.Int())) - currentTerraHeight.Set(float64(latestBlock.Int())) // Query and report guardian set status requestURL := fmt.Sprintf("%s/wasm/contracts/%s/store?query_msg={\"guardian_set_info\":{}}", e.urlLCD, e.bridge)