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
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)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
panic(err)
panic(fmt.Errorf("failed to create btc chain notifier: %w", err))

Copy link
Member Author

Choose a reason for hiding this comment

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

errors are already wrapped in the NewNodeBackendWithParams

}

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
Loading