From 686e734e2278923ef2811838653273ec4d9b30c6 Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Thu, 6 Dec 2018 21:14:04 -0800 Subject: [PATCH] chainntnfs/bitcoindnotify: add chainParams parameter to bitcoindnotify.New In this commit, we add the current chain parameters to the BitcoindNotifier. This will be used in a future commit in order to convert outputs scripts into addresses. This is needed since the bitcoind backend uses these addresses to detect whether the script encoded within it was spent by a transaction in the chain. --- chainntnfs/bitcoindnotify/bitcoind.go | 11 ++++++++--- chainntnfs/bitcoindnotify/bitcoind_test.go | 5 ++++- chainntnfs/bitcoindnotify/driver.go | 19 +++++++++++++------ 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/chainntnfs/bitcoindnotify/bitcoind.go b/chainntnfs/bitcoindnotify/bitcoind.go index 5b0df594..6981ae63 100644 --- a/chainntnfs/bitcoindnotify/bitcoind.go +++ b/chainntnfs/bitcoindnotify/bitcoind.go @@ -8,6 +8,7 @@ import ( "sync/atomic" "github.com/btcsuite/btcd/btcjson" + "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" @@ -58,7 +59,8 @@ type BitcoindNotifier struct { started int32 // To be used atomically. stopped int32 // To be used atomically. - chainConn *chain.BitcoindClient + chainConn *chain.BitcoindClient + chainParams *chaincfg.Params notificationCancels chan interface{} notificationRegistry chan interface{} @@ -88,12 +90,15 @@ type BitcoindNotifier struct { 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 +// bitcoind node detailed in the passed configuration is already running, and // willing to accept RPC requests and new zmq clients. -func New(chainConn *chain.BitcoindConn, spendHintCache chainntnfs.SpendHintCache, +func New(chainConn *chain.BitcoindConn, chainParams *chaincfg.Params, + spendHintCache chainntnfs.SpendHintCache, confirmHintCache chainntnfs.ConfirmHintCache) *BitcoindNotifier { notifier := &BitcoindNotifier{ + chainParams: chainParams, + notificationCancels: make(chan interface{}), notificationRegistry: make(chan interface{}), diff --git a/chainntnfs/bitcoindnotify/bitcoind_test.go b/chainntnfs/bitcoindnotify/bitcoind_test.go index 63357ae6..27b7dff4 100644 --- a/chainntnfs/bitcoindnotify/bitcoind_test.go +++ b/chainntnfs/bitcoindnotify/bitcoind_test.go @@ -41,7 +41,10 @@ func setUpNotifier(t *testing.T, bitcoindConn *chain.BitcoindConn, t.Helper() - notifier := New(bitcoindConn, spendHintCache, confirmHintCache) + notifier := New( + bitcoindConn, chainntnfs.NetParams, spendHintCache, + confirmHintCache, + ) if err := notifier.Start(); err != nil { t.Fatalf("unable to start notifier: %v", err) } diff --git a/chainntnfs/bitcoindnotify/driver.go b/chainntnfs/bitcoindnotify/driver.go index d6ef3705..6054f0de 100644 --- a/chainntnfs/bitcoindnotify/driver.go +++ b/chainntnfs/bitcoindnotify/driver.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" + "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcwallet/chain" "github.com/lightningnetwork/lnd/chainntnfs" ) @@ -11,9 +12,9 @@ import ( // 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) != 4 { return nil, fmt.Errorf("incorrect number of arguments to "+ - ".New(...), expected 2, instead passed %v", len(args)) + ".New(...), expected 4, instead passed %v", len(args)) } chainConn, ok := args[0].(*chain.BitcoindConn) @@ -22,19 +23,25 @@ func createNewNotifier(args ...interface{}) (chainntnfs.ChainNotifier, error) { "is incorrect, expected a *chain.BitcoindConn") } - spendHintCache, ok := args[1].(chainntnfs.SpendHintCache) + chainParams, ok := args[1].(*chaincfg.Params) if !ok { return nil, errors.New("second argument to bitcoindnotify.New " + + "is incorrect, expected a *chaincfg.Params") + } + + spendHintCache, ok := args[2].(chainntnfs.SpendHintCache) + if !ok { + return nil, errors.New("third argument to bitcoindnotify.New " + "is incorrect, expected a chainntnfs.SpendHintCache") } - confirmHintCache, ok := args[2].(chainntnfs.ConfirmHintCache) + confirmHintCache, ok := args[3].(chainntnfs.ConfirmHintCache) if !ok { - return nil, errors.New("third argument to bitcoindnotify.New " + + return nil, errors.New("fourth argument to bitcoindnotify.New " + "is incorrect, expected a chainntnfs.ConfirmHintCache") } - return New(chainConn, spendHintCache, confirmHintCache), nil + return New(chainConn, chainParams, spendHintCache, confirmHintCache), nil } // init registers a driver for the BtcdNotifier concrete implementation of the