btcdnotify: use chain.NewRPCClientWithConfig to init RPC client

So we can use the methods implemented on the chain RPC client.
This commit is contained in:
yyforyongyu
2024-02-24 12:20:48 +08:00
parent b5e4384e24
commit ebef3679ac

View File

@ -14,6 +14,7 @@ import (
"github.com/btcsuite/btcd/rpcclient" "github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcwallet/chain"
"github.com/lightningnetwork/lnd/blockcache" "github.com/lightningnetwork/lnd/blockcache"
"github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/queue" "github.com/lightningnetwork/lnd/queue"
@ -58,7 +59,7 @@ type BtcdNotifier struct {
active int32 // To be used atomically. active int32 // To be used atomically.
stopped int32 // To be used atomically. stopped int32 // To be used atomically.
chainConn *rpcclient.Client chainConn *chain.RPCClient
chainParams *chaincfg.Params chainParams *chaincfg.Params
notificationCancels chan interface{} notificationCancels chan interface{}
@ -127,21 +128,30 @@ func New(config *rpcclient.ConnConfig, chainParams *chaincfg.Params,
quit: make(chan struct{}), quit: make(chan struct{}),
} }
// Disable connecting to btcd within the rpcclient.New method. We
// defer establishing the connection to our .Start() method.
config.DisableConnectOnNew = true
config.DisableAutoReconnect = false
ntfnCallbacks := &rpcclient.NotificationHandlers{ ntfnCallbacks := &rpcclient.NotificationHandlers{
OnBlockConnected: notifier.onBlockConnected, OnBlockConnected: notifier.onBlockConnected,
OnBlockDisconnected: notifier.onBlockDisconnected, OnBlockDisconnected: notifier.onBlockDisconnected,
OnRedeemingTx: notifier.onRedeemingTx, OnRedeemingTx: notifier.onRedeemingTx,
} }
// Disable connecting to btcd within the rpcclient.New method. We rpcCfg := &chain.RPCClientConfig{
// defer establishing the connection to our .Start() method. ReconnectAttempts: 20,
config.DisableConnectOnNew = true Conn: config,
config.DisableAutoReconnect = false Chain: chainParams,
chainConn, err := rpcclient.New(config, ntfnCallbacks) NotificationHandlers: ntfnCallbacks,
}
chainRPC, err := chain.NewRPCClientWithConfig(rpcCfg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
notifier.chainConn = chainConn
notifier.chainConn = chainRPC
return notifier, nil return notifier, nil
} }