Skip to content

Commit

Permalink
chore(btcclient): use notifier interface (#20)
Browse files Browse the repository at this point in the history
- Removes usage of btc client with subscription to zmq
- Removes proprietary client implementation of zmq subscription as we
now rely on lnd's

References [issue](https://github.com/babylonchain/vigilante/issues/199)
  • Loading branch information
Lazar955 authored Aug 27, 2024
1 parent cf21c32 commit e992231
Show file tree
Hide file tree
Showing 26 changed files with 233 additions and 878 deletions.
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
16 changes: 16 additions & 0 deletions btcclient/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package btcclient
import (
"encoding/hex"
"fmt"
"github.com/babylonlabs-io/vigilante/netparams"
"net"
"os"
"time"
Expand Down Expand Up @@ -248,3 +249,18 @@ func NewNodeBackend(
return nil, fmt.Errorf("unknown node backend: %v", cfg.ActiveNodeBackend)
}
}

// NewNodeBackendWithParams creates a new NodeBackend by incorporating parameter retrieval and config conversion.
func NewNodeBackendWithParams(cfg config.BTCConfig, rawCert string) (*NodeBackend, error) {
btcParams, err := netparams.GetBTCParams(cfg.NetParams)
if err != nil {
return nil, fmt.Errorf("failed to get BTC net params: %w", err)
}

btcNotifier, err := NewNodeBackend(CfgToBtcNodeBackendConfig(cfg, rawCert), btcParams, &EmptyHintCache{})
if err != nil {
return nil, fmt.Errorf("failed to initialize notifier: %w", err)
}

return btcNotifier, nil
}
25 changes: 0 additions & 25 deletions btcclient/testutils.go

This file was deleted.

18 changes: 4 additions & 14 deletions cmd/vigilante/cmd/btcstaking_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
bst "github.com/babylonlabs-io/vigilante/btcstaking-tracker"
"github.com/babylonlabs-io/vigilante/config"
"github.com/babylonlabs-io/vigilante/metrics"
"github.com/babylonlabs-io/vigilante/netparams"
"github.com/babylonlabs-io/vigilante/rpcserver"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -61,26 +60,17 @@ 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))
}

// create BTC notifier
// TODO: is it possible to merge BTC client and BTC notifier?
btcParams, err := netparams.GetBTCParams(cfg.BTC.NetParams)
btcNotifier, err := btcclient.NewNodeBackendWithParams(cfg.BTC, "")
if err != nil {
panic(fmt.Errorf("failed to get BTC parameter: %w", err))
}
btcCfg := btcclient.CfgToBtcNodeBackendConfig(cfg.BTC, "") // we will read certifcates from file
btcNotifier, err := btcclient.NewNodeBackend(btcCfg, btcParams, &btcclient.EmptyHintCache{})
if err != nil {
panic(fmt.Errorf("failed to create btc chain notifier: %w", err))
panic(err)
}

bsMetrics := metrics.NewBTCStakingTrackerMetrics()
Expand Down
26 changes: 17 additions & 9 deletions cmd/vigilante/cmd/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"

bbnqccfg "github.com/babylonlabs-io/babylon/client/config"
bbnqc "github.com/babylonlabs-io/babylon/client/query"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -62,13 +61,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 +73,23 @@ func GetMonitorCmd() *cobra.Command {
// register monitor metrics
monitorMetrics := metrics.NewMonitorMetrics()

// create the chain notifier
btcNotifier, err := btcclient.NewNodeBackendWithParams(cfg.BTC, "")
if err != nil {
panic(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
Loading

0 comments on commit e992231

Please sign in to comment.