Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(btcclient): use notifier interface #20

Merged
merged 14 commits into from
Aug 27, 2024
22 changes: 2 additions & 20 deletions btcclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
"go.uber.org/zap"

"github.com/babylonlabs-io/vigilante/config"
"github.com/babylonlabs-io/vigilante/types"
"github.com/babylonlabs-io/vigilante/zmq"
)

var _ BTCClient = &Client{}
Expand All @@ -25,18 +23,14 @@ var _ BTCClient = &Client{}
// for information regarding the current best block chain.
type Client struct {
*rpcclient.Client
zmqClient *zmq.Client

Params *chaincfg.Params
Cfg *config.BTCConfig
params *chaincfg.Params
cfg *config.BTCConfig
logger *zap.SugaredLogger

// retry attributes
retrySleepTime time.Duration
maxRetrySleepTime time.Duration

// channel for notifying new BTC blocks to reporter
blockEventChan chan *types.BlockEvent
}

func (c *Client) GetTipBlockVerbose() (*btcjson.GetBlockVerboseResult, error) {
Expand All @@ -54,16 +48,4 @@ func (c *Client) GetTipBlockVerbose() (*btcjson.GetBlockVerboseResult, error) {

func (c *Client) Stop() {
c.Shutdown()
// NewWallet will create a client with nil blockEventChan,
// while NewWithBlockSubscriber will have a non-nil one, so
// we need to check here
if c.blockEventChan != nil {
close(c.blockEventChan)
}

if c.zmqClient != nil {
if err := c.zmqClient.Close(); err != nil {
c.logger.Debug(err)
}
}
}
139 changes: 0 additions & 139 deletions btcclient/client_block_subscriber.go

This file was deleted.

12 changes: 6 additions & 6 deletions btcclient/client_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func NewWallet(cfg *config.BTCConfig, parentLogger *zap.Logger) (*Client, error)
return nil, err
}
wallet := &Client{}
wallet.Cfg = cfg
wallet.Params = params
wallet.cfg = cfg
wallet.params = params
wallet.logger = parentLogger.With(zap.String("module", "btcclient_wallet")).Sugar()

connCfg := &rpcclient.ConnConfig{}
Expand Down Expand Up @@ -69,23 +69,23 @@ func NewWallet(cfg *config.BTCConfig, parentLogger *zap.Logger) (*Client, error)
}

func (c *Client) GetWalletPass() string {
return c.Cfg.WalletPassword
return c.cfg.WalletPassword
}

func (c *Client) GetWalletLockTime() int64 {
return c.Cfg.WalletLockTime
return c.cfg.WalletLockTime
}

func (c *Client) GetNetParams() *chaincfg.Params {
net, err := netparams.GetBTCParams(c.Cfg.NetParams)
net, err := netparams.GetBTCParams(c.cfg.NetParams)
if err != nil {
panic(fmt.Errorf("failed to get BTC network params: %w", err))
}
return net
}

func (c *Client) GetBTCConfig() *config.BTCConfig {
return c.Cfg
return c.cfg
}

func (c *Client) ListUnspent() ([]btcjson.ListUnspentResult, error) {
Expand Down
3 changes: 0 additions & 3 deletions btcclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
type BTCClient interface {
Stop()
WaitForShutdown()
MustSubscribeBlocks()
BlockEventChan() <-chan *types.BlockEvent
GetBestBlock() (*chainhash.Hash, uint64, error)
GetBlockByHash(blockHash *chainhash.Hash) (*types.IndexedBlock, *wire.MsgBlock, error)
FindTailBlocksByHeight(height uint64) ([]*types.IndexedBlock, error)
Expand All @@ -37,7 +35,6 @@ type BTCWallet interface {
SendRawTransaction(tx *wire.MsgTx, allowHighFees bool) (*chainhash.Hash, error)
GetRawChangeAddress(account string) (btcutil.Address, error)
WalletPassphrase(passphrase string, timeoutSecs int64) error
DumpPrivKey(address btcutil.Address) (*btcutil.WIF, error)
GetHighUTXOAndSum() (*btcjson.ListUnspentResult, float64, error)
FundRawTransaction(tx *wire.MsgTx, opts btcjson.FundRawTransactionOpts, isWitness *bool) (*btcjson.FundRawTransactionResult, error)
SignRawTransactionWithWallet(tx *wire.MsgTx) (*wire.MsgTx, bool, error)
Expand Down
25 changes: 0 additions & 25 deletions btcclient/testutils.go

This file was deleted.

8 changes: 2 additions & 6 deletions cmd/vigilante/cmd/btcstaking_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,8 @@ 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.NewWithBlockSubscriber(
&cfg.BTC,
cfg.Common.RetrySleepTime,
cfg.Common.MaxRetrySleepTime,
rootLogger,
)
btcClient, err := btcclient.NewWallet(&cfg.BTC, rootLogger)

if err != nil {
panic(fmt.Errorf("failed to open BTC client: %w", err))
}
Expand Down
31 changes: 23 additions & 8 deletions cmd/vigilante/cmd/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"github.com/babylonlabs-io/vigilante/netparams"

bbnqccfg "github.com/babylonlabs-io/babylon/client/config"
bbnqc "github.com/babylonlabs-io/babylon/client/query"
Expand Down Expand Up @@ -62,13 +63,7 @@ func GetMonitorCmd() *cobra.Command {
}

// create BTC client and connect to BTC server
// Note that monitor needs to subscribe to new BTC blocks
btcClient, err = btcclient.NewWithBlockSubscriber(
&cfg.BTC,
cfg.Common.RetrySleepTime,
cfg.Common.MaxRetrySleepTime,
rootLogger,
)
btcClient, err = btcclient.NewWallet(&cfg.BTC, rootLogger)
if err != nil {
panic(fmt.Errorf("failed to open BTC client: %w", err))
}
Expand All @@ -80,8 +75,28 @@ func GetMonitorCmd() *cobra.Command {
// register monitor metrics
monitorMetrics := metrics.NewMonitorMetrics()

// create the chain notifier
btcParams, err := netparams.GetBTCParams(cfg.BTC.NetParams)
if err != nil {
panic(fmt.Errorf("failed to get BTC net params: %w", err))
}
btcCfg := btcclient.CfgToBtcNodeBackendConfig(cfg.BTC, "")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rawCert is needed for btcd, which we wanted to remove, correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, there's a new issue to remove btcd from repo (previously merged PR regarding that was to use it in e2e)

btcNotifier, err := btcclient.NewNodeBackend(btcCfg, btcParams, &btcclient.EmptyHintCache{})
if err != nil {
panic(fmt.Errorf("failed to initialize notifier: %w", err))
}

// create monitor
vigilanteMonitor, err = monitor.New(&cfg.Monitor, &cfg.Common, rootLogger, genesisInfo, bbnQueryClient, btcClient, monitorMetrics)
vigilanteMonitor, err = monitor.New(
&cfg.Monitor,
&cfg.Common,
rootLogger,
genesisInfo,
bbnQueryClient,
btcClient,
btcNotifier,
monitorMetrics,
)
if err != nil {
panic(fmt.Errorf("failed to create vigilante monitor: %w", err))
}
Expand Down
15 changes: 14 additions & 1 deletion cmd/vigilante/cmd/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"github.com/babylonlabs-io/vigilante/netparams"

bbnclient "github.com/babylonlabs-io/babylon/client/client"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -48,7 +49,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.NewWithBlockSubscriber(&cfg.BTC, cfg.Common.RetrySleepTime, cfg.Common.MaxRetrySleepTime, rootLogger)
btcClient, err = btcclient.NewWallet(&cfg.BTC, rootLogger)
if err != nil {
panic(fmt.Errorf("failed to open BTC client: %w", err))
}
Expand All @@ -62,12 +63,24 @@ func GetReporterCmd() *cobra.Command {
// register reporter metrics
reporterMetrics := metrics.NewReporterMetrics()

// create the chain notifier
btcParams, err := netparams.GetBTCParams(cfg.BTC.NetParams)
if err != nil {
panic(fmt.Errorf("failed to get BTC net params: %w", err))
}
btcCfg := btcclient.CfgToBtcNodeBackendConfig(cfg.BTC, "")
btcNotifier, err := btcclient.NewNodeBackend(btcCfg, btcParams, &btcclient.EmptyHintCache{})
if err != nil {
panic(fmt.Errorf("failed to initialize notifier: %w", err))
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This chunk is duplicated across several programs. Maybe we can wrap it into a general constructor?


// create reporter
vigilantReporter, err = reporter.New(
&cfg.Reporter,
rootLogger,
btcClient,
babylonClient,
btcNotifier,
cfg.Common.RetrySleepTime,
cfg.Common.MaxRetrySleepTime,
reporterMetrics,
Expand Down
Loading
Loading