mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-20 13:04:28 +02:00
multi: update to latest BitcoindClient interface
In this commit, we introduce a nice optimization with regards to lnd's interaction with a bitcoind backend. Within lnd, we currently have three different subsystems responsible for watching the chain: chainntnfs, lnwallet, and routing/chainview. Each of these subsystems has an active RPC and ZMQ connection to the underlying bitcoind node. This would incur a toll on the underlying bitcoind node and would cause us to miss ZMQ events, which are crucial to lnd. We remedy this issue by sharing the same connection to a bitcoind node between the different clients within lnd.
This commit is contained in:
@@ -8,9 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcd/btcjson"
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||
"github.com/btcsuite/btcd/rpcclient"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/btcsuite/btcutil"
|
||||
"github.com/btcsuite/btcwallet/chain"
|
||||
@@ -87,8 +85,7 @@ var _ chainntnfs.ChainNotifier = (*BitcoindNotifier)(nil)
|
||||
// New returns a new BitcoindNotifier instance. This function assumes the
|
||||
// bitcoind node detailed in the passed configuration is already running, and
|
||||
// willing to accept RPC requests and new zmq clients.
|
||||
func New(config *rpcclient.ConnConfig, zmqConnect string,
|
||||
params chaincfg.Params) (*BitcoindNotifier, error) {
|
||||
func New(chainConn *chain.BitcoindConn) *BitcoindNotifier {
|
||||
notifier := &BitcoindNotifier{
|
||||
notificationCancels: make(chan interface{}),
|
||||
notificationRegistry: make(chan interface{}),
|
||||
@@ -100,18 +97,9 @@ func New(config *rpcclient.ConnConfig, zmqConnect string,
|
||||
quit: make(chan struct{}),
|
||||
}
|
||||
|
||||
// Disable connecting to bitcoind within the rpcclient.New method. We
|
||||
// defer establishing the connection to our .Start() method.
|
||||
config.DisableConnectOnNew = true
|
||||
config.DisableAutoReconnect = false
|
||||
chainConn, err := chain.NewBitcoindClient(¶ms, config.Host,
|
||||
config.User, config.Pass, zmqConnect, 100*time.Millisecond)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
notifier.chainConn = chainConn
|
||||
notifier.chainConn = chainConn.NewBitcoindClient(time.Unix(0, 0))
|
||||
|
||||
return notifier, nil
|
||||
return notifier
|
||||
}
|
||||
|
||||
// Start connects to the running bitcoind node over websockets, registers for
|
||||
|
@@ -3,38 +3,25 @@ package bitcoindnotify
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/btcsuite/btcd/rpcclient"
|
||||
"github.com/btcsuite/btcwallet/chain"
|
||||
"github.com/lightningnetwork/lnd/chainntnfs"
|
||||
)
|
||||
|
||||
// createNewNotifier creates a new instance of the ChainNotifier interface
|
||||
// implemented by BitcoindNotifier.
|
||||
func createNewNotifier(args ...interface{}) (chainntnfs.ChainNotifier, error) {
|
||||
if len(args) != 3 {
|
||||
if len(args) != 1 {
|
||||
return nil, fmt.Errorf("incorrect number of arguments to "+
|
||||
".New(...), expected 3, instead passed %v", len(args))
|
||||
".New(...), expected 1, instead passed %v", len(args))
|
||||
}
|
||||
|
||||
config, ok := args[0].(*rpcclient.ConnConfig)
|
||||
chainConn, ok := args[0].(*chain.BitcoindConn)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("first argument to bitcoindnotifier." +
|
||||
"New is incorrect, expected a *rpcclient.ConnConfig")
|
||||
return nil, fmt.Errorf("first argument to bitcoindnotify.New " +
|
||||
"is incorrect, expected a *chain.BitcoindConn")
|
||||
}
|
||||
|
||||
zmqConnect, ok := args[1].(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("second argument to bitcoindnotifier." +
|
||||
"New is incorrect, expected a string")
|
||||
}
|
||||
|
||||
params, ok := args[2].(chaincfg.Params)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("third argument to bitcoindnotifier." +
|
||||
"New is incorrect, expected a chaincfg.Params")
|
||||
}
|
||||
|
||||
return New(config, zmqConnect, params)
|
||||
return New(chainConn), nil
|
||||
}
|
||||
|
||||
// init registers a driver for the BtcdNotifier concrete implementation of the
|
||||
|
@@ -13,19 +13,19 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||
"github.com/btcsuite/btcwallet/walletdb"
|
||||
"github.com/lightninglabs/neutrino"
|
||||
"github.com/lightningnetwork/lnd/chainntnfs"
|
||||
"github.com/ltcsuite/ltcd/btcjson"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/btcsuite/btcd/btcjson"
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||
"github.com/btcsuite/btcd/integration/rpctest"
|
||||
"github.com/btcsuite/btcd/rpcclient"
|
||||
"github.com/btcsuite/btcd/txscript"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/btcsuite/btcutil"
|
||||
"github.com/btcsuite/btcwallet/chain"
|
||||
"github.com/btcsuite/btcwallet/walletdb"
|
||||
"github.com/lightninglabs/neutrino"
|
||||
"github.com/lightningnetwork/lnd/chainntnfs"
|
||||
|
||||
// Required to auto-register the bitcoind backed ChainNotifier
|
||||
// implementation.
|
||||
@@ -1375,7 +1375,8 @@ func TestInterfaces(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to create temp dir: %v", err)
|
||||
}
|
||||
zmqPath := "ipc:///" + tempBitcoindDir + "/weks.socket"
|
||||
zmqBlockHost := "ipc:///" + tempBitcoindDir + "/blocks.socket"
|
||||
zmqTxHost := "ipc:///" + tempBitcoindDir + "/tx.socket"
|
||||
cleanUp1 := func() {
|
||||
os.RemoveAll(tempBitcoindDir)
|
||||
}
|
||||
@@ -1392,8 +1393,8 @@ func TestInterfaces(t *testing.T) {
|
||||
"220110063096c221be9933c82d38e1",
|
||||
fmt.Sprintf("-rpcport=%d", rpcPort),
|
||||
"-disablewallet",
|
||||
"-zmqpubrawblock="+zmqPath,
|
||||
"-zmqpubrawtx="+zmqPath,
|
||||
"-zmqpubrawblock="+zmqBlockHost,
|
||||
"-zmqpubrawtx="+zmqTxHost,
|
||||
)
|
||||
err = bitcoind.Start()
|
||||
if err != nil {
|
||||
@@ -1410,20 +1411,26 @@ func TestInterfaces(t *testing.T) {
|
||||
// Wait for the bitcoind instance to start up.
|
||||
time.Sleep(time.Second)
|
||||
|
||||
// Start the FilteredChainView implementation instance.
|
||||
config := rpcclient.ConnConfig{
|
||||
Host: fmt.Sprintf(
|
||||
"127.0.0.1:%d", rpcPort),
|
||||
User: "weks",
|
||||
Pass: "weks",
|
||||
DisableAutoReconnect: false,
|
||||
DisableConnectOnNew: true,
|
||||
DisableTLS: true,
|
||||
HTTPPostMode: true,
|
||||
host := fmt.Sprintf("127.0.0.1:%d", rpcPort)
|
||||
chainConn, err := chain.NewBitcoindConn(
|
||||
netParams, host, "weks", "weks", zmqBlockHost,
|
||||
zmqTxHost, 100*time.Millisecond,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to establish connection to "+
|
||||
"bitcoind: %v", err)
|
||||
}
|
||||
if err := chainConn.Start(); err != nil {
|
||||
t.Fatalf("unable to establish connection to "+
|
||||
"bitcoind: %v", err)
|
||||
}
|
||||
cleanUp3 := func() {
|
||||
chainConn.Stop()
|
||||
cleanUp2()
|
||||
}
|
||||
cleanUp = cleanUp3
|
||||
|
||||
notifier, err = notifierDriver.New(&config, zmqPath,
|
||||
*netParams)
|
||||
notifier, err = notifierDriver.New(chainConn)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create %v notifier: %v",
|
||||
notifierType, err)
|
||||
|
Reference in New Issue
Block a user