mirror of
https://github.com/LBRYFoundation/lbcwallet.git
synced 2025-08-23 17:47:29 +00:00
chain: refactor BitcoindConn init params into config struct
This commit is contained in:
parent
178d124b76
commit
3fed46822c
2 changed files with 46 additions and 27 deletions
|
@ -10,7 +10,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/btcjson"
|
"github.com/btcsuite/btcd/btcjson"
|
||||||
"github.com/btcsuite/btcd/chaincfg"
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||||
"github.com/btcsuite/btcd/txscript"
|
"github.com/btcsuite/btcd/txscript"
|
||||||
"github.com/btcsuite/btcd/wire"
|
"github.com/btcsuite/btcd/wire"
|
||||||
|
@ -40,10 +39,6 @@ type BitcoindClient struct {
|
||||||
// chain.
|
// chain.
|
||||||
birthday time.Time
|
birthday time.Time
|
||||||
|
|
||||||
// chainParams are the parameters of the current chain this client is
|
|
||||||
// active under.
|
|
||||||
chainParams *chaincfg.Params
|
|
||||||
|
|
||||||
// id is the unique ID of this client assigned by the backing bitcoind
|
// id is the unique ID of this client assigned by the backing bitcoind
|
||||||
// connection.
|
// connection.
|
||||||
id uint64
|
id uint64
|
||||||
|
@ -920,7 +915,7 @@ func (c *BitcoindClient) reorg(currentBlock waddrmgr.BlockStamp,
|
||||||
func (c *BitcoindClient) FilterBlocks(
|
func (c *BitcoindClient) FilterBlocks(
|
||||||
req *FilterBlocksRequest) (*FilterBlocksResponse, error) {
|
req *FilterBlocksRequest) (*FilterBlocksResponse, error) {
|
||||||
|
|
||||||
blockFilterer := NewBlockFilterer(c.chainParams, req)
|
blockFilterer := NewBlockFilterer(c.chainConn.cfg.ChainParams, req)
|
||||||
|
|
||||||
// Iterate over the requested blocks, fetching each from the rpc client.
|
// Iterate over the requested blocks, fetching each from the rpc client.
|
||||||
// Each block will scanned using the reverse addresses indexes generated
|
// Each block will scanned using the reverse addresses indexes generated
|
||||||
|
@ -1282,7 +1277,7 @@ func (c *BitcoindClient) filterTx(tx *wire.MsgTx,
|
||||||
// Non-standard outputs can be safely skipped.
|
// Non-standard outputs can be safely skipped.
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
addr, err := pkScript.Address(c.chainParams)
|
addr, err := pkScript.Address(c.chainConn.cfg.ChainParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Non-standard outputs can be safely skipped.
|
// Non-standard outputs can be safely skipped.
|
||||||
continue
|
continue
|
||||||
|
@ -1298,7 +1293,7 @@ func (c *BitcoindClient) filterTx(tx *wire.MsgTx,
|
||||||
// add it to our watch list.
|
// add it to our watch list.
|
||||||
for i, txOut := range tx.TxOut {
|
for i, txOut := range tx.TxOut {
|
||||||
_, addrs, _, err := txscript.ExtractPkScriptAddrs(
|
_, addrs, _, err := txscript.ExtractPkScriptAddrs(
|
||||||
txOut.PkScript, c.chainParams,
|
txOut.PkScript, c.chainConn.cfg.ChainParams,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Non-standard outputs can be safely skipped.
|
// Non-standard outputs can be safely skipped.
|
||||||
|
|
|
@ -38,21 +38,49 @@ const (
|
||||||
seqNumLen = 4
|
seqNumLen = 4
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// BitcoindConfig contains all of the parameters required to establish a
|
||||||
|
// connection to a bitcoind's RPC.
|
||||||
|
type BitcoindConfig struct {
|
||||||
|
// ChainParams are the chain parameters the bitcoind server is running
|
||||||
|
// on.
|
||||||
|
ChainParams *chaincfg.Params
|
||||||
|
|
||||||
|
// Host is the IP address and port of the bitcoind's RPC server.
|
||||||
|
Host string
|
||||||
|
|
||||||
|
// User is the username to use to authenticate to bitcoind's RPC server.
|
||||||
|
User string
|
||||||
|
|
||||||
|
// Pass is the passphrase to use to authenticate to bitcoind's RPC
|
||||||
|
// server.
|
||||||
|
Pass string
|
||||||
|
|
||||||
|
// ZMQBlockHost is the IP address and port of the bitcoind's rawblock
|
||||||
|
// listener.
|
||||||
|
ZMQBlockHost string
|
||||||
|
|
||||||
|
// ZMQTxHost is the IP address and port of the bitcoind's rawtx
|
||||||
|
// listener.
|
||||||
|
ZMQTxHost string
|
||||||
|
|
||||||
|
// ZMQReadDeadline represents the read deadline we'll apply when reading
|
||||||
|
// ZMQ messages from either subscription.
|
||||||
|
ZMQReadDeadline time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
// BitcoindConn represents a persistent client connection to a bitcoind node
|
// BitcoindConn represents a persistent client connection to a bitcoind node
|
||||||
// that listens for events read from a ZMQ connection.
|
// that listens for events read from a ZMQ connection.
|
||||||
type BitcoindConn struct {
|
type BitcoindConn struct {
|
||||||
started int32 // To be used atomically.
|
started int32 // To be used atomically.
|
||||||
stopped int32 // To be used atomically.
|
stopped int32 // To be used atomically.
|
||||||
|
|
||||||
|
cfg BitcoindConfig
|
||||||
|
|
||||||
// rescanClientCounter is an atomic counter that assigns a unique ID to
|
// rescanClientCounter is an atomic counter that assigns a unique ID to
|
||||||
// each new bitcoind rescan client using the current bitcoind
|
// each new bitcoind rescan client using the current bitcoind
|
||||||
// connection.
|
// connection.
|
||||||
rescanClientCounter uint64
|
rescanClientCounter uint64
|
||||||
|
|
||||||
// chainParams identifies the current network the bitcoind node is
|
|
||||||
// running on.
|
|
||||||
chainParams *chaincfg.Params
|
|
||||||
|
|
||||||
// client is the RPC client to the bitcoind node.
|
// client is the RPC client to the bitcoind node.
|
||||||
client *rpcclient.Client
|
client *rpcclient.Client
|
||||||
|
|
||||||
|
@ -77,20 +105,16 @@ type BitcoindConn struct {
|
||||||
// string. The ZMQ connections are established immediately to ensure liveness.
|
// string. The ZMQ connections are established immediately to ensure liveness.
|
||||||
// If the remote node does not operate on the same bitcoin network as described
|
// If the remote node does not operate on the same bitcoin network as described
|
||||||
// by the passed chain parameters, the connection will be disconnected.
|
// by the passed chain parameters, the connection will be disconnected.
|
||||||
func NewBitcoindConn(chainParams *chaincfg.Params,
|
func NewBitcoindConn(cfg *BitcoindConfig) (*BitcoindConn, error) {
|
||||||
host, user, pass, zmqBlockHost, zmqTxHost string,
|
|
||||||
zmqPollInterval time.Duration) (*BitcoindConn, error) {
|
|
||||||
|
|
||||||
clientCfg := &rpcclient.ConnConfig{
|
clientCfg := &rpcclient.ConnConfig{
|
||||||
Host: host,
|
Host: cfg.Host,
|
||||||
User: user,
|
User: cfg.User,
|
||||||
Pass: pass,
|
Pass: cfg.Pass,
|
||||||
DisableAutoReconnect: false,
|
DisableAutoReconnect: false,
|
||||||
DisableConnectOnNew: true,
|
DisableConnectOnNew: true,
|
||||||
DisableTLS: true,
|
DisableTLS: true,
|
||||||
HTTPPostMode: true,
|
HTTPPostMode: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := rpcclient.New(clientCfg, nil)
|
client, err := rpcclient.New(clientCfg, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -101,7 +125,8 @@ func NewBitcoindConn(chainParams *chaincfg.Params,
|
||||||
// concern to ensure one type of event isn't dropped from the connection
|
// concern to ensure one type of event isn't dropped from the connection
|
||||||
// queue due to another type of event filling it up.
|
// queue due to another type of event filling it up.
|
||||||
zmqBlockConn, err := gozmq.Subscribe(
|
zmqBlockConn, err := gozmq.Subscribe(
|
||||||
zmqBlockHost, []string{rawBlockZMQCommand}, zmqPollInterval,
|
cfg.ZMQBlockHost, []string{rawBlockZMQCommand},
|
||||||
|
cfg.ZMQReadDeadline,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to subscribe for zmq block "+
|
return nil, fmt.Errorf("unable to subscribe for zmq block "+
|
||||||
|
@ -109,7 +134,7 @@ func NewBitcoindConn(chainParams *chaincfg.Params,
|
||||||
}
|
}
|
||||||
|
|
||||||
zmqTxConn, err := gozmq.Subscribe(
|
zmqTxConn, err := gozmq.Subscribe(
|
||||||
zmqTxHost, []string{rawTxZMQCommand}, zmqPollInterval,
|
cfg.ZMQTxHost, []string{rawTxZMQCommand}, cfg.ZMQReadDeadline,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
zmqBlockConn.Close()
|
zmqBlockConn.Close()
|
||||||
|
@ -118,7 +143,7 @@ func NewBitcoindConn(chainParams *chaincfg.Params,
|
||||||
}
|
}
|
||||||
|
|
||||||
conn := &BitcoindConn{
|
conn := &BitcoindConn{
|
||||||
chainParams: chainParams,
|
cfg: *cfg,
|
||||||
client: client,
|
client: client,
|
||||||
zmqBlockConn: zmqBlockConn,
|
zmqBlockConn: zmqBlockConn,
|
||||||
zmqTxConn: zmqTxConn,
|
zmqTxConn: zmqTxConn,
|
||||||
|
@ -144,9 +169,9 @@ func (c *BitcoindConn) Start() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if net != c.chainParams.Net {
|
if net != c.cfg.ChainParams.Net {
|
||||||
return fmt.Errorf("expected network %v, got %v",
|
return fmt.Errorf("expected network %v, got %v",
|
||||||
c.chainParams.Net, net)
|
c.cfg.ChainParams.Net, net)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.wg.Add(2)
|
c.wg.Add(2)
|
||||||
|
@ -407,8 +432,7 @@ func (c *BitcoindConn) NewBitcoindClient() *BitcoindClient {
|
||||||
|
|
||||||
id: atomic.AddUint64(&c.rescanClientCounter, 1),
|
id: atomic.AddUint64(&c.rescanClientCounter, 1),
|
||||||
|
|
||||||
chainParams: c.chainParams,
|
chainConn: c,
|
||||||
chainConn: c,
|
|
||||||
|
|
||||||
rescanUpdate: make(chan interface{}),
|
rescanUpdate: make(chan interface{}),
|
||||||
watchedAddresses: make(map[string]struct{}),
|
watchedAddresses: make(map[string]struct{}),
|
||||||
|
|
Loading…
Add table
Reference in a new issue