Skip to content

multi: unify RPC connection creations for chain backends #9435

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions chainntnfs/btcdnotify/btcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
active int32 // To be used atomically.
stopped int32 // To be used atomically.

chainConn *chain.RPCClient
chainConn *chain.BtcdClient

Check failure on line 63 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / basic itests (btcd, backend=btcd cover=1)

undefined: chain.BtcdClient

Check failure on line 63 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / basic itests (bitcoind-notxindex, backend="bitcoind notxindex")

undefined: chain.BtcdClient

Check failure on line 63 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / basic itests (neutrino, backend=neutrino cover=1)

undefined: chain.BtcdClient

Check failure on line 63 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / basic itests (bitcoind, backend=bitcoind cover=1)

undefined: chain.BtcdClient

Check failure on line 63 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / check commits

undefined: chain.BtcdClient

Check failure on line 63 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / macOS itest

undefined: chain.BtcdClient

Check failure on line 63 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / itests (bitcoind-postgres, backend=bitcoind dbbackend=postgres)

undefined: chain.BtcdClient

Check failure on line 63 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / itests (bitcoind-postgres-nativesql, backend=bitcoind dbbackend=postgres nativesql=true)

undefined: chain.BtcdClient

Check failure on line 63 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / itests (bitcoind-sqlite-nativesql, backend=bitcoind dbbackend=sqlite nativesql=true)

undefined: chain.BtcdClient

Check failure on line 63 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / itests (bitcoind-sqlite, backend=bitcoind dbbackend=sqlite)

undefined: chain.BtcdClient

Check failure on line 63 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / itests (bitcoind-rpcpolling, backend="bitcoind rpcpolling")

undefined: chain.BtcdClient

Check failure on line 63 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / itests (bitcoind-etcd, backend=bitcoind dbbackend=etcd)

undefined: chain.BtcdClient

Check failure on line 63 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / windows itest

undefined: chain.BtcdClient
chainParams *chaincfg.Params

notificationCancels chan interface{}
Expand Down Expand Up @@ -135,14 +135,14 @@
OnRedeemingTx: notifier.onRedeemingTx,
}

rpcCfg := &chain.RPCClientConfig{
rpcCfg := &chain.BtcdConfig{

Check failure on line 138 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / basic itests (btcd, backend=btcd cover=1)

undefined: chain.BtcdConfig

Check failure on line 138 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / basic itests (bitcoind-notxindex, backend="bitcoind notxindex")

undefined: chain.BtcdConfig

Check failure on line 138 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / basic itests (neutrino, backend=neutrino cover=1)

undefined: chain.BtcdConfig

Check failure on line 138 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / basic itests (bitcoind, backend=bitcoind cover=1)

undefined: chain.BtcdConfig

Check failure on line 138 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / check commits

undefined: chain.BtcdConfig

Check failure on line 138 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / macOS itest

undefined: chain.BtcdConfig

Check failure on line 138 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / itests (bitcoind-postgres, backend=bitcoind dbbackend=postgres)

undefined: chain.BtcdConfig

Check failure on line 138 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / itests (bitcoind-postgres-nativesql, backend=bitcoind dbbackend=postgres nativesql=true)

undefined: chain.BtcdConfig

Check failure on line 138 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / itests (bitcoind-sqlite-nativesql, backend=bitcoind dbbackend=sqlite nativesql=true)

undefined: chain.BtcdConfig

Check failure on line 138 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / itests (bitcoind-sqlite, backend=bitcoind dbbackend=sqlite)

undefined: chain.BtcdConfig

Check failure on line 138 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / itests (bitcoind-rpcpolling, backend="bitcoind rpcpolling")

undefined: chain.BtcdConfig

Check failure on line 138 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / itests (bitcoind-etcd, backend=bitcoind dbbackend=etcd)

undefined: chain.BtcdConfig

Check failure on line 138 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / windows itest

undefined: chain.BtcdConfig
ReconnectAttempts: 20,
Conn: config,
Chain: chainParams,
NotificationHandlers: ntfnCallbacks,
}

chainRPC, err := chain.NewRPCClientWithConfig(rpcCfg)
chainRPC, err := chain.NewBtcdClientWithConfig(rpcCfg)

Check failure on line 145 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / basic itests (btcd, backend=btcd cover=1)

undefined: chain.NewBtcdClientWithConfig

Check failure on line 145 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / basic itests (bitcoind-notxindex, backend="bitcoind notxindex")

undefined: chain.NewBtcdClientWithConfig

Check failure on line 145 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / basic itests (neutrino, backend=neutrino cover=1)

undefined: chain.NewBtcdClientWithConfig

Check failure on line 145 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / basic itests (bitcoind, backend=bitcoind cover=1)

undefined: chain.NewBtcdClientWithConfig

Check failure on line 145 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / check commits

undefined: chain.NewBtcdClientWithConfig

Check failure on line 145 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / macOS itest

undefined: chain.NewBtcdClientWithConfig

Check failure on line 145 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / itests (bitcoind-postgres, backend=bitcoind dbbackend=postgres)

undefined: chain.NewBtcdClientWithConfig

Check failure on line 145 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / itests (bitcoind-postgres-nativesql, backend=bitcoind dbbackend=postgres nativesql=true)

undefined: chain.NewBtcdClientWithConfig

Check failure on line 145 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / itests (bitcoind-sqlite-nativesql, backend=bitcoind dbbackend=sqlite nativesql=true)

undefined: chain.NewBtcdClientWithConfig

Check failure on line 145 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / itests (bitcoind-sqlite, backend=bitcoind dbbackend=sqlite)

undefined: chain.NewBtcdClientWithConfig

Check failure on line 145 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / itests (bitcoind-rpcpolling, backend="bitcoind rpcpolling")

undefined: chain.NewBtcdClientWithConfig

Check failure on line 145 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / itests (bitcoind-etcd, backend=bitcoind dbbackend=etcd)

undefined: chain.NewBtcdClientWithConfig

Check failure on line 145 in chainntnfs/btcdnotify/btcd.go

View workflow job for this annotation

GitHub Actions / windows itest

undefined: chain.NewBtcdClientWithConfig
if err != nil {
return nil, err
}
Expand Down
61 changes: 27 additions & 34 deletions chainreg/chainregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,18 +383,8 @@
cc.ChainView = chainview.NewBitcoindFilteredChainView(
bitcoindConn, cfg.BlockCache,
)
cc.ChainSource = bitcoindConn.NewBitcoindClient()

// Initialize config to connect to bitcoind RPC.
rpcConfig := &rpcclient.ConnConfig{
Host: bitcoindHost,
User: bitcoindMode.RPCUser,
Pass: bitcoindMode.RPCPass,
DisableConnectOnNew: true,
DisableAutoReconnect: false,
DisableTLS: true,
HTTPPostMode: true,
}
chainRPC := bitcoindConn.NewBitcoindClient()
cc.ChainSource = chainRPC

// If feeurl is not provided, use bitcoind's fee estimator.
if cfg.Fee.URL == "" {
Expand All @@ -406,34 +396,27 @@
// use live fee estimates, rather than a statically
// coded value.
fallBackFeeRate := chainfee.SatPerKVByte(25 * 1000)

cc.FeeEstimator, err = chainfee.NewBitcoindEstimator(
*rpcConfig, bitcoindMode.EstimateMode,
chainRPC, bitcoindMode.EstimateMode,
fallBackFeeRate.FeePerKWeight(),
)
if err != nil {
return nil, nil, err
}
}

// We need to use some apis that are not exposed by btcwallet,
// for a health check function so we create an ad-hoc bitcoind
// connection.
chainConn, err := rpcclient.New(rpcConfig, nil)
if err != nil {
return nil, nil, err
}

// Before we continue any further, we'll ensure that the
// backend understands Taproot. If not, then all the default
// features can't be used.
if !backendSupportsTaproot(chainConn) {
if !backendSupportsTaproot(chainRPC) {
return nil, nil, fmt.Errorf("node backend does not " +
"support taproot")
}

// The api we will use for our health check depends on the
// bitcoind version.
cmd, ver, err := getBitcoindHealthCheckCmd(chainConn)
cmd, ver, err := getBitcoindHealthCheckCmd(chainRPC)
if err != nil {
return nil, nil, err
}
Expand All @@ -453,7 +436,9 @@
}

// Fetch all active zmq notifications from the bitcoind client.
resp, err := chainConn.RawRequest("getzmqnotifications", nil)
resp, err := chainRPC.RawRequest(

Check failure on line 439 in chainreg/chainregistry.go

View workflow job for this annotation

GitHub Actions / lint code

chainRPC.RawRequest undefined (type *chain.BitcoindClient has no field or method RawRequest) (typecheck)
"getzmqnotifications", nil,
)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -520,7 +505,7 @@
}

cc.HealthCheck = func() error {
_, err := chainConn.RawRequest(cmd, nil)
_, err := chainRPC.RawRequest(cmd, nil)

Check failure on line 508 in chainreg/chainregistry.go

View workflow job for this annotation

GitHub Actions / lint code

chainRPC.RawRequest undefined (type *chain.BitcoindClient has no field or method RawRequest) (typecheck)
if err != nil {
return err
}
Expand All @@ -535,7 +520,7 @@
// Make sure the bitcoind chain backend maintains a
// healthy connection to the network by checking the
// number of outbound peers.
return checkOutboundPeers(chainConn)
return checkOutboundPeers(chainRPC)
}

case "btcd":
Expand Down Expand Up @@ -616,9 +601,12 @@

// Create a special websockets rpc client for btcd which will be
// used by the wallet for notifications, calls, etc.
chainRPC, err := chain.NewRPCClient(
cfg.ActiveNetParams.Params, btcdHost, btcdUser,
btcdPass, rpcCert, false, 20,
chainRPC, err := chain.NewBtcdClientWithConfig(

Check failure on line 604 in chainreg/chainregistry.go

View workflow job for this annotation

GitHub Actions / lint code

undefined: chain.NewBtcdClientWithConfig (typecheck)
&chain.BtcdConfig{

Check failure on line 605 in chainreg/chainregistry.go

View workflow job for this annotation

GitHub Actions / lint code

undefined: chain.BtcdConfig (typecheck)
Conn: rpcConfig,
Chain: cfg.ActiveNetParams.Params,
ReconnectAttempts: 20,
},
)
if err != nil {
return nil, nil, err
Expand All @@ -630,7 +618,12 @@
restConfCopy := *rpcConfig
restConfCopy.Endpoint = ""
restConfCopy.HTTPPostMode = true
chainConn, err := rpcclient.New(&restConfCopy, nil)
chainConn, err := chain.NewBtcdClientWithConfig(

Check failure on line 621 in chainreg/chainregistry.go

View workflow job for this annotation

GitHub Actions / lint code

undefined: chain.NewBtcdClientWithConfig (typecheck)
&chain.BtcdConfig{

Check failure on line 622 in chainreg/chainregistry.go

View workflow job for this annotation

GitHub Actions / lint code

undefined: chain.BtcdConfig (typecheck)
Conn: &restConfCopy,
Chain: cfg.ActiveNetParams.Params,
},
)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -658,7 +651,7 @@
// Make sure the btcd chain backend maintains a
// healthy connection to the network by checking the
// number of outbound peers.
return checkOutboundPeers(chainRPC.Client)
return checkOutboundPeers(chainRPC)
}

// If feeurl is not provided, use btcd's fee estimator.
Expand All @@ -671,7 +664,7 @@
// value.
fallBackFeeRate := chainfee.SatPerKVByte(25 * 1000)
cc.FeeEstimator, err = chainfee.NewBtcdEstimator(
*rpcConfig, fallBackFeeRate.FeePerKWeight(),
chainRPC, fallBackFeeRate.FeePerKWeight(),
)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -800,7 +793,7 @@
// command, because it has no locking and is an inexpensive call, which was
// added in version 0.15. If we are on an earlier version, we fallback to using
// getblockchaininfo.
func getBitcoindHealthCheckCmd(client *rpcclient.Client) (string, int64, error) {
func getBitcoindHealthCheckCmd(client chain.Interface) (string, int64, error) {
// Query bitcoind to get our current version.
resp, err := client.RawRequest("getnetworkinfo", nil)
if err != nil {
Expand Down Expand Up @@ -896,7 +889,7 @@
// provided RPC client. If the number of outbound peers is below 6, a warning
// is logged. This function is intended to ensure that the chain backend
// maintains a healthy connection to the network.
func checkOutboundPeers(client *rpcclient.Client) error {
func checkOutboundPeers(client chain.Interface) error {
peers, err := client.GetPeerInfo()
if err != nil {
return err
Expand Down
17 changes: 17 additions & 0 deletions chainreg/no_chain_backend.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package chainreg

import (
"encoding/json"
"errors"
"time"

Expand Down Expand Up @@ -147,6 +148,10 @@ func (n *NoChainSource) Stop() {
func (n *NoChainSource) WaitForShutdown() {
}

func (n *NoChainSource) GetPeerInfo() ([]btcjson.GetPeerInfoResult, error) {
return nil, errNotImplemented
}

func (n *NoChainSource) GetBestBlock() (*chainhash.Hash, int32, error) {
return noChainBackendBestHash, noChainBackendBestHeight, nil
}
Expand All @@ -172,6 +177,12 @@ func (n *NoChainSource) GetBlockHeader(*chainhash.Hash) (*wire.BlockHeader,
}, nil
}

func (n *NoChainSource) GetBlockChainInfo() (*btcjson.GetBlockChainInfoResult,
error) {

return nil, errNotImplemented
}

func (n *NoChainSource) IsCurrent() bool {
return true
}
Expand All @@ -192,6 +203,12 @@ func (n *NoChainSource) SendRawTransaction(*wire.MsgTx, bool) (*chainhash.Hash,
return nil, errNotImplemented
}

func (n *NoChainSource) RawRequest(method string,
params []json.RawMessage) (json.RawMessage, error) {

return nil, errNotImplemented
}

func (n *NoChainSource) Rescan(*chainhash.Hash, []btcutil.Address,
map[wire.OutPoint]btcutil.Address) error {

Expand Down
8 changes: 4 additions & 4 deletions chainreg/taproot_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import (
"encoding/json"

"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcwallet/chain"
)

// backendSupportsTaproot returns true if the backend understands the taproot
// soft fork.
func backendSupportsTaproot(rpc *rpcclient.Client) bool {
func backendSupportsTaproot(client chain.Interface) bool {
// First, we'll try to access the normal getblockchaininfo call.
chainInfo, err := rpc.GetBlockChainInfo()
chainInfo, err := client.GetBlockChainInfo()

Check failure on line 13 in chainreg/taproot_check.go

View workflow job for this annotation

GitHub Actions / lint code

client.GetBlockChainInfo undefined (type chain.Interface has no field or method GetBlockChainInfo) (typecheck)
if err == nil {
// If this call worked, then we'll check that the taproot
// deployment is defined.
Expand Down Expand Up @@ -41,7 +41,7 @@
// running a newer version of bitcoind that still has the
// getblockchaininfo call, but doesn't populate the data, so we'll hit
// the new getdeploymentinfo call.
resp, err := rpc.RawRequest("getdeploymentinfo", nil)
resp, err := client.RawRequest("getdeploymentinfo", nil)

Check failure on line 44 in chainreg/taproot_check.go

View workflow job for this annotation

GitHub Actions / lint code

client.RawRequest undefined (type chain.Interface has no field or method RawRequest) (typecheck)
if err != nil {
log.Warnf("unable to make getdeploymentinfo request: %v", err)
return false
Expand Down
36 changes: 36 additions & 0 deletions lnmock/chain.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package lnmock

import (
"encoding/json"

"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
Expand Down Expand Up @@ -32,6 +34,28 @@ func (m *MockChain) WaitForShutdown() {
m.Called()
}

func (m *MockChain) GetPeerInfo() ([]btcjson.GetPeerInfoResult, error) {
args := m.Called()

if args.Get(0) == nil {
return nil, args.Error(1)
}

return args.Get(0).([]btcjson.GetPeerInfoResult), args.Error(1)
}

func (m *MockChain) GetBlockChainInfo() (*btcjson.GetBlockChainInfoResult,
error) {

args := m.Called()

if args.Get(0) == nil {
return nil, args.Error(1)
}

return args.Get(0).(*btcjson.GetBlockChainInfoResult), args.Error(1)
}

func (m *MockChain) GetBestBlock() (*chainhash.Hash, int32, error) {
args := m.Called()

Expand Down Expand Up @@ -114,6 +138,18 @@ func (m *MockChain) SendRawTransaction(tx *wire.MsgTx, allowHighFees bool) (
return args.Get(0).(*chainhash.Hash), args.Error(1)
}

func (m *MockChain) RawRequest(method string,
params []json.RawMessage) (json.RawMessage, error) {

args := m.Called(method, params)

if args.Get(0) == nil {
return nil, args.Error(1)
}

return args.Get(0).(json.RawMessage), args.Error(1)
}

func (m *MockChain) Rescan(startHash *chainhash.Hash, addrs []btcutil.Address,
outPoints map[wire.OutPoint]btcutil.Address) error {

Expand Down
2 changes: 1 addition & 1 deletion lnwallet/btcwallet/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (b *BtcWallet) GetUtxo(op *wire.OutPoint, pkScript []byte,
// Otherwise, the output is assumed to be in the UTXO.
return spendReport.Output, nil

case *chain.RPCClient:
case *chain.BtcdClient:
txout, err := backend.GetTxOut(&op.Hash, op.Index, false)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion lnwallet/btcwallet/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func getChainBackend(t *testing.T, netParams *chaincfg.Params) (chain.Interface,
require.NoError(t, err)

rpcConfig := miningNode.RPCConfig()
chainClient, err := chain.NewRPCClient(
chainClient, err := chain.NewBtcdClient(
netParams, rpcConfig.Host, rpcConfig.User, rpcConfig.Pass,
rpcConfig.Certificates, false, 20,
)
Expand Down
Loading
Loading