Skip to content

Commit

Permalink
chore(*): use proper retry (#29)
Browse files Browse the repository at this point in the history
Replaces usage of `retry.do` from babylon with `avast/retry-go`

[References issue](#8)
  • Loading branch information
Lazar955 authored Sep 4, 2024
1 parent 40695cd commit 3b8378d
Show file tree
Hide file tree
Showing 26 changed files with 288 additions and 61 deletions.
1 change: 1 addition & 0 deletions btcclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Client struct {
// retry attributes
retrySleepTime time.Duration
maxRetrySleepTime time.Duration
maxRetryTimes uint
}

func (c *Client) GetTipBlockVerbose() (*btcjson.GetBlockVerboseResult, error) {
Expand Down
15 changes: 9 additions & 6 deletions btcclient/client_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,25 @@ import (
// used by vigilant submitter
// a wallet is essentially a BTC client
// that connects to the btcWallet daemon
func NewWallet(cfg *config.BTCConfig, parentLogger *zap.Logger) (*Client, error) {
params, err := netparams.GetBTCParams(cfg.NetParams)
func NewWallet(cfg *config.Config, parentLogger *zap.Logger) (*Client, error) {
params, err := netparams.GetBTCParams(cfg.BTC.NetParams)
if err != nil {
return nil, err
}
wallet := &Client{}
wallet.cfg = cfg
wallet.cfg = &cfg.BTC
wallet.params = params
wallet.logger = parentLogger.With(zap.String("module", "btcclient_wallet")).Sugar()
wallet.retrySleepTime = cfg.Common.RetrySleepTime
wallet.maxRetryTimes = cfg.Common.MaxRetryTimes
wallet.maxRetrySleepTime = cfg.Common.MaxRetrySleepTime

connCfg := &rpcclient.ConnConfig{
// this will work with node loaded with multiple wallets
Host: cfg.Endpoint + "/wallet/" + cfg.WalletName,
Host: cfg.BTC.Endpoint + "/wallet/" + cfg.BTC.WalletName,
HTTPPostMode: true,
User: cfg.Username,
Pass: cfg.Password,
User: cfg.BTC.Username,
Pass: cfg.BTC.Password,
DisableTLS: true,
}

Expand Down
34 changes: 25 additions & 9 deletions btcclient/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -66,13 +66,17 @@ 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),
retry.Attempts(c.maxRetryTimes),
); err != nil {
c.logger.Debug(
"failed to query the best block hash", zap.Error(err))
}
Expand All @@ -86,13 +90,17 @@ 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),
retry.Attempts(c.maxRetryTimes),
); err != nil {
c.logger.Debug(
"failed to query the block hash", zap.Uint64("height", height), zap.Error(err))
}
Expand All @@ -106,13 +114,17 @@ 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),
retry.Attempts(c.maxRetryTimes),
); err != nil {
c.logger.Debug(
"failed to query the block", zap.String("hash", hash.String()), zap.Error(err))
}
Expand All @@ -126,13 +138,17 @@ 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),
retry.Attempts(c.maxRetryTimes),
); err != nil {
c.logger.Debug(
"failed to query the block verbose", zap.String("hash", hash.String()), zap.Error(err))
}
Expand Down
3 changes: 2 additions & 1 deletion btcstaking-tracker/atomicslasher/atomic_slasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ func New(
parentLogger *zap.Logger,
retrySleepTime time.Duration,
maxRetrySleepTime time.Duration,
maxRetryTimes uint,
btcClient btcclient.BTCClient,
btcNotifier notifier.ChainNotifier,
bbnClient BabylonClient,
slashedFPSKChan chan *btcec.PrivateKey,
metrics *metrics.AtomicSlasherMetrics,
) *AtomicSlasher {
logger := parentLogger.With(zap.String("module", "atomic_slasher"))
bbnAdapter := NewBabylonAdapter(logger, cfg, retrySleepTime, maxRetrySleepTime, bbnClient)
bbnAdapter := NewBabylonAdapter(logger, cfg, retrySleepTime, maxRetrySleepTime, maxRetryTimes, bbnClient)
return &AtomicSlasher{
quit: make(chan struct{}),
cfg: cfg,
Expand Down
5 changes: 5 additions & 0 deletions btcstaking-tracker/atomicslasher/babylon_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type BabylonAdapter struct {
cfg *config.BTCStakingTrackerConfig
retrySleepTime time.Duration
maxRetrySleepTime time.Duration
maxRetryTimes uint
bbnClient BabylonClient
}

Expand All @@ -28,13 +29,15 @@ func NewBabylonAdapter(
cfg *config.BTCStakingTrackerConfig,
retrySleepTime time.Duration,
maxRetrySleepTime time.Duration,
maxRetryTimes uint,
bbnClient BabylonClient,
) *BabylonAdapter {
return &BabylonAdapter{
logger: logger,
cfg: cfg,
retrySleepTime: retrySleepTime,
maxRetrySleepTime: maxRetrySleepTime,
maxRetryTimes: maxRetryTimes,
bbnClient: bbnClient,
}
}
Expand All @@ -53,6 +56,7 @@ func (ba *BabylonAdapter) BTCStakingParams(ctx context.Context, version uint32)
retry.Context(ctx),
retry.Delay(ba.retrySleepTime),
retry.MaxDelay(ba.maxRetrySleepTime),
retry.Attempts(ba.maxRetryTimes),
)

return bsParams, err
Expand All @@ -72,6 +76,7 @@ func (ba *BabylonAdapter) BTCDelegation(ctx context.Context, stakingTxHashHex st
retry.Context(ctx),
retry.Delay(ba.retrySleepTime),
retry.MaxDelay(ba.maxRetrySleepTime),
retry.Attempts(ba.maxRetryTimes),
)

return resp, err
Expand Down
12 changes: 11 additions & 1 deletion btcstaking-tracker/btcslasher/bootstrapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,17 @@ func FuzzSlasher_Bootstrapping(f *testing.F) {
logger, err := config.NewRootLogger("auto", "debug")
require.NoError(t, err)
slashedFPSKChan := make(chan *btcec.PrivateKey, 100)
btcSlasher, err := btcslasher.New(logger, mockBTCClient, mockBabylonQuerier, &chaincfg.SimNetParams, commonCfg.RetrySleepTime, commonCfg.MaxRetrySleepTime, slashedFPSKChan, metrics.NewBTCStakingTrackerMetrics().SlasherMetrics)
btcSlasher, err := btcslasher.New(
logger,
mockBTCClient,
mockBabylonQuerier,
&chaincfg.SimNetParams,
commonCfg.RetrySleepTime,
commonCfg.MaxRetrySleepTime,
commonCfg.MaxRetryTimes,
slashedFPSKChan,
metrics.NewBTCStakingTrackerMetrics().SlasherMetrics,
)
require.NoError(t, err)

// slashing address
Expand Down
4 changes: 3 additions & 1 deletion btcstaking-tracker/btcslasher/slasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type BTCSlasher struct {
btcFinalizationTimeout uint64
retrySleepTime time.Duration
maxRetrySleepTime time.Duration

maxRetryTimes uint
// channel for finality signature messages, which might include
// equivocation evidences
finalitySigChan <-chan coretypes.ResultEvent
Expand All @@ -59,6 +59,7 @@ func New(
netParams *chaincfg.Params,
retrySleepTime time.Duration,
maxRetrySleepTime time.Duration,
maxRetryTimes uint,
slashedFPSKChan chan *btcec.PrivateKey,
metrics *metrics.SlasherMetrics,
) (*BTCSlasher, error) {
Expand All @@ -71,6 +72,7 @@ func New(
netParams: netParams,
retrySleepTime: retrySleepTime,
maxRetrySleepTime: maxRetrySleepTime,
maxRetryTimes: maxRetryTimes,
slashedFPSKChan: slashedFPSKChan,
slashResultChan: make(chan *SlashResult, 1000),
quit: make(chan struct{}),
Expand Down
12 changes: 11 additions & 1 deletion btcstaking-tracker/btcslasher/slasher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,17 @@ func FuzzSlasher(f *testing.F) {
logger, err := config.NewRootLogger("auto", "debug")
require.NoError(t, err)
slashedFPSKChan := make(chan *btcec.PrivateKey, 100)
btcSlasher, err := btcslasher.New(logger, mockBTCClient, mockBabylonQuerier, &chaincfg.SimNetParams, commonCfg.RetrySleepTime, commonCfg.MaxRetrySleepTime, slashedFPSKChan, metrics.NewBTCStakingTrackerMetrics().SlasherMetrics)
btcSlasher, err := btcslasher.New(
logger,
mockBTCClient,
mockBabylonQuerier,
&chaincfg.SimNetParams,
commonCfg.RetrySleepTime,
commonCfg.MaxRetrySleepTime,
commonCfg.MaxRetryTimes,
slashedFPSKChan,
metrics.NewBTCStakingTrackerMetrics().SlasherMetrics,
)
require.NoError(t, err)
err = btcSlasher.LoadParams()
require.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions btcstaking-tracker/btcslasher/slasher_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (bs *BTCSlasher) slashBTCDelegation(
retry.Context(ctx),
retry.Delay(bs.retrySleepTime),
retry.MaxDelay(bs.maxRetrySleepTime),
retry.Attempts(bs.maxRetryTimes),
)

slashRes := &SlashResult{
Expand Down
2 changes: 2 additions & 0 deletions btcstaking-tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func NewBTCSTakingTracker(
btcParams,
commonCfg.RetrySleepTime,
commonCfg.MaxRetrySleepTime,
commonCfg.MaxRetryTimes,
slashedFPSKChan,
metrics.SlasherMetrics,
)
Expand All @@ -100,6 +101,7 @@ func NewBTCSTakingTracker(
logger,
commonCfg.RetrySleepTime,
commonCfg.MaxRetrySleepTime,
commonCfg.MaxRetryTimes,
btcClient,
btcNotifier,
bbnClient,
Expand Down
2 changes: 1 addition & 1 deletion cmd/vigilante/cmd/btcstaking_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func GetBTCStakingTracker() *cobra.Command {

// create BTC client and connect to BTC server
// Note that monitor needs to subscribe to new BTC blocks
btcClient, err := btcclient.NewWallet(&cfg.BTC, rootLogger)
btcClient, err := btcclient.NewWallet(&cfg, rootLogger)

if err != nil {
panic(fmt.Errorf("failed to open BTC client: %w", err))
Expand Down
2 changes: 1 addition & 1 deletion cmd/vigilante/cmd/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func GetMonitorCmd() *cobra.Command {
}

// create BTC client and connect to BTC server
btcClient, err = btcclient.NewWallet(&cfg.BTC, rootLogger)
btcClient, err = btcclient.NewWallet(&cfg, rootLogger)
if err != nil {
panic(fmt.Errorf("failed to open BTC client: %w", err))
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/vigilante/cmd/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func GetReporterCmd() *cobra.Command {

// create BTC client and connect to BTC server
// Note that vigilant reporter needs to subscribe to new BTC blocks
btcClient, err = btcclient.NewWallet(&cfg.BTC, rootLogger)
btcClient, err = btcclient.NewWallet(&cfg, rootLogger)
if err != nil {
panic(fmt.Errorf("failed to open BTC client: %w", err))
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/vigilante/cmd/submitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func GetSubmitterCmd() *cobra.Command {
}

// create BTC wallet and connect to BTC server
btcWallet, err := btcclient.NewWallet(&cfg.BTC, rootLogger)
btcWallet, err := btcclient.NewWallet(&cfg, rootLogger)
if err != nil {
panic(fmt.Errorf("failed to open BTC client: %w", err))
}
Expand Down Expand Up @@ -71,6 +71,7 @@ func GetSubmitterCmd() *cobra.Command {
submitterAddr,
cfg.Common.RetrySleepTime,
cfg.Common.MaxRetrySleepTime,
cfg.Common.MaxRetryTimes,
submitterMetrics,
)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
const (
defaultRetrySleepTime = 5 * time.Second
defaultMaxRetrySleepTime = 5 * time.Minute
defaultMaxRetryTimes = 5
)

// CommonConfig defines the server's basic configuration
Expand All @@ -23,6 +24,8 @@ type CommonConfig struct {
// Maximum backoff interval between retries. Exponential backoff leads to interval increase.
// This value is the cap of the interval, when exceeded the retries stop.
MaxRetrySleepTime time.Duration `mapstructure:"max-retry-sleep-time"`
// The max number of retries in case of a failure
MaxRetryTimes uint `mapstructure:"max-retry-times"`
}

func isOneOf(v string, list []string) bool {
Expand Down Expand Up @@ -60,5 +63,6 @@ func DefaultCommonConfig() CommonConfig {
LogLevel: "debug",
RetrySleepTime: defaultRetrySleepTime,
MaxRetrySleepTime: defaultMaxRetrySleepTime,
MaxRetryTimes: defaultMaxRetryTimes,
}
}
2 changes: 2 additions & 0 deletions e2etest/submitter_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func TestSubmitterSubmission(t *testing.T) {
subAddr,
tm.Config.Common.RetrySleepTime,
tm.Config.Common.MaxRetrySleepTime,
tm.Config.Common.MaxRetryTimes,
metrics.NewSubmitterMetrics(),
)

Expand Down Expand Up @@ -135,6 +136,7 @@ func TestSubmitterSubmissionReplace(t *testing.T) {
subAddr,
tm.Config.Common.RetrySleepTime,
tm.Config.Common.MaxRetrySleepTime,
tm.Config.Common.MaxRetryTimes,
metrics.NewSubmitterMetrics(),
)

Expand Down
2 changes: 1 addition & 1 deletion e2etest/test_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type TestManager struct {
}

func initBTCClientWithSubscriber(t *testing.T, cfg *config.Config) *btcclient.Client {
client, err := btcclient.NewWallet(&cfg.BTC, zap.NewNop())
client, err := btcclient.NewWallet(cfg, zap.NewNop())
require.NoError(t, err)

// let's wait until chain rpc becomes available
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
Loading

0 comments on commit 3b8378d

Please sign in to comment.