From 939ce1335bc44431e8fdb4aacd20fd0674e49ab2 Mon Sep 17 00:00:00 2001 From: lazar Date: Mon, 2 Sep 2024 18:36:06 +0200 Subject: [PATCH] replace babylon retry do --- btcclient/query.go | 30 +++++++++++++++++++++--------- go.mod | 2 +- go.sum | 4 ++-- reporter/reporter.go | 9 ++++++--- reporter/utils.go | 18 ++++++++++++------ submitter/submitter.go | 9 ++++++--- 6 files changed, 48 insertions(+), 24 deletions(-) diff --git a/btcclient/query.go b/btcclient/query.go index c62b4cf3..1ef82076 100644 --- a/btcclient/query.go +++ b/btcclient/query.go @@ -3,7 +3,7 @@ package btcclient import ( "fmt" - "github.com/babylonlabs-io/babylon/types/retry" + "github.com/avast/retry-go/v4" "github.com/btcsuite/btcd/btcjson" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" @@ -66,13 +66,16 @@ func (c *Client) getBestBlockHashWithRetry() (*chainhash.Hash, error) { err error ) - if err := retry.Do(c.retrySleepTime, c.maxRetrySleepTime, func() error { + if err := retry.Do(func() error { blockHash, err = c.GetBestBlockHash() if err != nil { return err } return nil - }); err != nil { + }, + retry.Delay(c.retrySleepTime), + retry.MaxDelay(c.maxRetrySleepTime), + ); err != nil { c.logger.Debug( "failed to query the best block hash", zap.Error(err)) } @@ -86,13 +89,16 @@ func (c *Client) getBlockHashWithRetry(height uint64) (*chainhash.Hash, error) { err error ) - if err := retry.Do(c.retrySleepTime, c.maxRetrySleepTime, func() error { + if err := retry.Do(func() error { blockHash, err = c.GetBlockHash(int64(height)) if err != nil { return err } return nil - }); err != nil { + }, + retry.Delay(c.retrySleepTime), + retry.MaxDelay(c.maxRetrySleepTime), + ); err != nil { c.logger.Debug( "failed to query the block hash", zap.Uint64("height", height), zap.Error(err)) } @@ -106,13 +112,16 @@ func (c *Client) getBlockWithRetry(hash *chainhash.Hash) (*wire.MsgBlock, error) err error ) - if err := retry.Do(c.retrySleepTime, c.maxRetrySleepTime, func() error { + if err := retry.Do(func() error { block, err = c.GetBlock(hash) if err != nil { return err } return nil - }); err != nil { + }, + retry.Delay(c.retrySleepTime), + retry.MaxDelay(c.maxRetrySleepTime), + ); err != nil { c.logger.Debug( "failed to query the block", zap.String("hash", hash.String()), zap.Error(err)) } @@ -126,13 +135,16 @@ func (c *Client) getBlockVerboseWithRetry(hash *chainhash.Hash) (*btcjson.GetBlo err error ) - if err := retry.Do(c.retrySleepTime, c.maxRetrySleepTime, func() error { + if err := retry.Do(func() error { blockVerbose, err = c.GetBlockVerbose(hash) if err != nil { return err } return nil - }); err != nil { + }, + retry.Delay(c.retrySleepTime), + retry.MaxDelay(c.maxRetrySleepTime), + ); err != nil { c.logger.Debug( "failed to query the block verbose", zap.String("hash", hash.String()), zap.Error(err)) } diff --git a/go.mod b/go.mod index 71a53725..d5e72d6a 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( cosmossdk.io/errors v1.0.1 cosmossdk.io/log v1.3.1 cosmossdk.io/math v1.3.0 - github.com/avast/retry-go/v4 v4.5.1 + github.com/avast/retry-go/v4 v4.6.0 github.com/babylonlabs-io/babylon v0.9.1 github.com/boljen/go-bitmap v0.0.0-20151001105940-23cd2fb0ce7d github.com/btcsuite/btcd v0.24.2 diff --git a/go.sum b/go.sum index aa5d1149..7ad26eaa 100644 --- a/go.sum +++ b/go.sum @@ -275,8 +275,8 @@ github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5 github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a/go.mod h1:DAHtR1m6lCRdSC2Tm3DSWRPvIPr6xNKyeHdqDQSQT+A= -github.com/avast/retry-go/v4 v4.5.1 h1:AxIx0HGi4VZ3I02jr78j5lZ3M6x1E0Ivxa6b0pUUh7o= -github.com/avast/retry-go/v4 v4.5.1/go.mod h1:/sipNsvNB3RRuT5iNcb6h73nw3IBmXJ/H3XrCQYSOpc= +github.com/avast/retry-go/v4 v4.6.0 h1:K9xNA+KeB8HHc2aWFuLb25Offp+0iVRXEvFx8IinRJA= +github.com/avast/retry-go/v4 v4.6.0/go.mod h1:gvWlPhBVsvBbLkVGDg/KwvBv0bEkCOLRRSHKIr2PyOE= github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQwij/eHl5CU= github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= github.com/aws/aws-sdk-go v1.44.122/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo= diff --git a/reporter/reporter.go b/reporter/reporter.go index 5c2f757c..902e069f 100644 --- a/reporter/reporter.go +++ b/reporter/reporter.go @@ -7,8 +7,8 @@ import ( "sync" "time" + "github.com/avast/retry-go/v4" "github.com/babylonlabs-io/babylon/btctxformatter" - "github.com/babylonlabs-io/babylon/types/retry" btcctypes "github.com/babylonlabs-io/babylon/x/btccheckpoint/types" "github.com/babylonlabs-io/vigilante/btcclient" "github.com/babylonlabs-io/vigilante/config" @@ -57,10 +57,13 @@ func New( btccParamsRes *btcctypes.QueryParamsResponse err error ) - err = retry.Do(retrySleepTime, maxRetrySleepTime, func() error { + err = retry.Do(func() error { btccParamsRes, err = babylonClient.BTCCheckpointParams() return err - }) + }, + retry.Delay(retrySleepTime), + retry.MaxDelay(maxRetrySleepTime), + ) if err != nil { return nil, fmt.Errorf("failed to get BTC Checkpoint parameters: %w", err) } diff --git a/reporter/utils.go b/reporter/utils.go index ce1ef528..40056ba5 100644 --- a/reporter/utils.go +++ b/reporter/utils.go @@ -7,7 +7,7 @@ import ( pv "github.com/cosmos/relayer/v2/relayer/provider" - "github.com/babylonlabs-io/babylon/types/retry" + "github.com/avast/retry-go/v4" btcctypes "github.com/babylonlabs-io/babylon/x/btccheckpoint/types" btclctypes "github.com/babylonlabs-io/babylon/x/btclightclient/types" "github.com/babylonlabs-io/vigilante/types" @@ -33,10 +33,13 @@ func (r *Reporter) getHeaderMsgsToSubmit(signer string, ibs []*types.IndexedBloc for i, header := range ibs { blockHash := header.BlockHash() var res *btclctypes.QueryContainsBytesResponse - err = retry.Do(r.retrySleepTime, r.maxRetrySleepTime, func() error { + err = retry.Do(func() error { res, err = r.babylonClient.ContainsBTCBlock(&blockHash) return err - }) + }, + retry.Delay(r.retrySleepTime), + retry.MaxDelay(r.maxRetrySleepTime), + ) if err != nil { return nil, err } @@ -57,7 +60,7 @@ func (r *Reporter) getHeaderMsgsToSubmit(signer string, ibs []*types.IndexedBloc blockChunks := chunkBy(ibsToSubmit, int(r.Cfg.MaxHeadersInMsg)) - headerMsgsToSubmit := []*btclctypes.MsgInsertHeaders{} + headerMsgsToSubmit := make([]*btclctypes.MsgInsertHeaders, 0, len(blockChunks)) for _, ibChunk := range blockChunks { msgInsertHeaders := types.NewMsgInsertHeaders(signer, ibChunk) @@ -69,14 +72,17 @@ func (r *Reporter) getHeaderMsgsToSubmit(signer string, ibs []*types.IndexedBloc func (r *Reporter) submitHeaderMsgs(msg *btclctypes.MsgInsertHeaders) error { // submit the headers - err := retry.Do(r.retrySleepTime, r.maxRetrySleepTime, func() error { + err := retry.Do(func() error { res, err := r.babylonClient.InsertHeaders(context.Background(), msg) if err != nil { return err } r.logger.Infof("Successfully submitted %d headers to Babylon with response code %v", len(msg.Headers), res.Code) return nil - }) + }, + retry.Delay(r.retrySleepTime), + retry.MaxDelay(r.maxRetrySleepTime), + ) if err != nil { r.metrics.FailedHeadersCounter.Add(float64(len(msg.Headers))) return fmt.Errorf("failed to submit headers: %w", err) diff --git a/submitter/submitter.go b/submitter/submitter.go index da1fd744..591803cd 100644 --- a/submitter/submitter.go +++ b/submitter/submitter.go @@ -6,8 +6,8 @@ import ( "sync" "time" + "github.com/avast/retry-go/v4" "github.com/babylonlabs-io/babylon/btctxformatter" - "github.com/babylonlabs-io/babylon/types/retry" btcctypes "github.com/babylonlabs-io/babylon/x/btccheckpoint/types" sdk "github.com/cosmos/cosmos-sdk/types" "go.uber.org/zap" @@ -48,10 +48,13 @@ func New( btccheckpointParams *btcctypes.QueryParamsResponse err error ) - err = retry.Do(retrySleepTime, maxRetrySleepTime, func() error { + err = retry.Do(func() error { btccheckpointParams, err = queryClient.BTCCheckpointParams() return err - }) + }, + retry.Delay(retrySleepTime), + retry.MaxDelay(maxRetrySleepTime), + ) if err != nil { return nil, fmt.Errorf("failed to get checkpoint params: %w", err) }