From 8bbd010f745d185703e140f78df484921c60e3ca Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 3 Aug 2016 22:31:20 -0700 Subject: [PATCH] lnwallet: use the ChainNotifier interface throughout instead of BtcdNotifier MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit refactors the code within lnwallet interacting with the ChainNotifier to accept, and call against the implementation rather than a single concrete implementation. LightningWallet no longer creates it’s own BtcdNotifier implementation doing construction, now instead accepting a pre-started `ChainNotifier` interface. All imports have been updated to reflect the new naming scheme. --- lnwallet/channel.go | 2 +- lnwallet/interface.go | 6 ++--- lnwallet/wallet.go | 55 +++++++++++------------------------------ lnwallet/wallet_test.go | 11 ++++++++- 4 files changed, 28 insertions(+), 46 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 6b796172..982c5029 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -8,7 +8,7 @@ import ( "github.com/btcsuite/fastsha256" "github.com/davecgh/go-spew/spew" - "github.com/lightningnetwork/lnd/chainntfs" + "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/lnwire" diff --git a/lnwallet/interface.go b/lnwallet/interface.go index b1ab4a94..087556a7 100644 --- a/lnwallet/interface.go +++ b/lnwallet/interface.go @@ -13,9 +13,9 @@ import ( // such as: uspv, btcwallet, Bitcoin Core, Electrum, etc. This interface then // serves as a "base wallet", with Lightning Network awareness taking place at // a "higher" level of abstraction. Essentially, an overlay wallet. -// Implementors of this interface must closely adhere to the documented behavior -// of all interface methods in order to ensure identical behavior accross all -// concrete implementations. +// Implementors of this interface must closely adhere to the documented +// behavior of all interface methods in order to ensure identical behavior +// across all concrete implementations. type WalletController interface { // ConfirmedBalance returns the sum of all the wallet's unspent outputs // that have at least confs confirmations. If confs is set to zero, diff --git a/lnwallet/wallet.go b/lnwallet/wallet.go index 356d193b..c5b69a35 100644 --- a/lnwallet/wallet.go +++ b/lnwallet/wallet.go @@ -9,8 +9,7 @@ import ( "sync/atomic" "github.com/davecgh/go-spew/spew" - "github.com/lightningnetwork/lnd/chainntfs" - "github.com/lightningnetwork/lnd/chainntfs/btcdnotify" + "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/elkrem" "github.com/roasbeef/btcd/btcjson" @@ -18,7 +17,6 @@ import ( "github.com/roasbeef/btcd/btcec" "github.com/roasbeef/btcd/txscript" "github.com/roasbeef/btcd/wire" - "github.com/roasbeef/btcrpcclient" "github.com/roasbeef/btcutil" "github.com/roasbeef/btcutil/coinset" "github.com/roasbeef/btcutil/txsort" @@ -234,8 +232,7 @@ type LightningWallet struct { // Used by in order to obtain notifications about funding transaction // reaching a specified confirmation depth, and to catch // counterparty's broadcasting revoked commitment states. - // TODO(roasbeef): needs to be stripped out from wallet - ChainNotifier chainntnfs.ChainNotifier + chainNotifier chainntnfs.ChainNotifier // The core wallet, all non Lightning Network specific interaction is // proxied to the internal wallet. @@ -279,7 +276,12 @@ type LightningWallet struct { // NewLightningWallet creates/opens and initializes a LightningWallet instance. // If the wallet has never been created (according to the passed dataDir), first-time // setup is executed. -func NewLightningWallet(config *Config, cdb *channeldb.DB) (*LightningWallet, error) { +// +// NOTE: The passed channeldb, and ChainNotifier should already be fully +// initialized/started before being passed as a function arugment. +func NewLightningWallet(config *Config, cdb *channeldb.DB, + notifier chainntnfs.ChainNotifier) (*LightningWallet, error) { + // Ensure the wallet exists or create it when the create flag is set. netDir := networkDir(config.DataDir, config.NetParams) @@ -344,26 +346,8 @@ func NewLightningWallet(config *Config, cdb *channeldb.DB) (*LightningWallet, er return nil, err } - // Using the same authentication info, create a config for a second - // rpcclient which will be used by the current default chain - // notifier implemenation. - rpcConfig := &btcrpcclient.ConnConfig{ - Host: config.RpcHost, - Endpoint: "ws", - User: config.RpcUser, - Pass: config.RpcPass, - Certificates: config.CACert, - DisableTLS: false, - DisableConnectOnNew: true, - DisableAutoReconnect: false, - } - chainNotifier, err := btcdnotify.NewBtcdNotifier(rpcConfig) - if err != nil { - return nil, err - } - return &LightningWallet{ - ChainNotifier: chainNotifier, + chainNotifier: notifier, rpc: rpcc, Wallet: wallet, channelDB: cdb, @@ -393,17 +377,8 @@ func (l *LightningWallet) Startup() error { } l.Start() - // Start the notification server. This is used so channel managment - // goroutines can be notified when a funding transaction reaches a - // sufficient number of confirmations, or when the input for the funding - // transaction is spent in an attempt at an uncooperative close by the - // counter party. - if err := l.ChainNotifier.Start(); err != nil { - return err - } - - // Pass the rpc client into the wallet so it can sync up to the current - // main chain. + // Pass the rpc client into the wallet so it can sync up to the + // current main chain. l.SynchronizeRPC(l.rpc) l.wg.Add(1) @@ -426,8 +401,6 @@ func (l *LightningWallet) Shutdown() error { l.rpc.Shutdown() - l.ChainNotifier.Stop() - close(l.quit) l.wg.Wait() return nil @@ -1251,7 +1224,7 @@ func (l *LightningWallet) handleChannelOpen(req *channelOpenMsg) { // Finally, create and officially open the payment channel! // TODO(roasbeef): CreationTime once tx is 'open' - channel, _ := NewLightningChannel(l, l.ChainNotifier, l.channelDB, + channel, _ := NewLightningChannel(l, l.chainNotifier, l.channelDB, res.partialState) res.chanOpen <- channel @@ -1266,7 +1239,7 @@ func (l *LightningWallet) openChannelAfterConfirmations(res *ChannelReservation) // transaction reaches `numConfs` confirmations. txid := res.fundingTx.TxSha() numConfs := uint32(res.numConfsToOpen) - confNtfn, _ := l.ChainNotifier.RegisterConfirmationsNtfn(&txid, numConfs) + confNtfn, _ := l.chainNotifier.RegisterConfirmationsNtfn(&txid, numConfs) walletLog.Infof("Waiting for funding tx (txid: %v) to reach %v confirmations", txid, numConfs) @@ -1293,7 +1266,7 @@ out: // Finally, create and officially open the payment channel! // TODO(roasbeef): CreationTime once tx is 'open' - channel, _ := NewLightningChannel(l, l.ChainNotifier, l.channelDB, + channel, _ := NewLightningChannel(l, l.chainNotifier, l.channelDB, res.partialState) res.chanOpen <- channel } diff --git a/lnwallet/wallet_test.go b/lnwallet/wallet_test.go index 7cb8846c..330401c9 100644 --- a/lnwallet/wallet_test.go +++ b/lnwallet/wallet_test.go @@ -11,6 +11,7 @@ import ( "time" "github.com/boltdb/bolt" + "github.com/lightningnetwork/lnd/chainntnfs/btcdnotify" "github.com/lightningnetwork/lnd/channeldb" "github.com/roasbeef/btcd/chaincfg" "github.com/roasbeef/btcutil/txsort" @@ -338,7 +339,15 @@ func createTestWallet(miningNode *rpctest.Harness, netParams *chaincfg.Params) ( return "", nil, err } - wallet, err := NewLightningWallet(config, cdb) + chainNotifier, err := btcdnotify.New(&rpcConfig) + if err != nil { + return "", nil, err + } + if err := chainNotifier.Start(); err != nil { + return "", nil, err + } + + wallet, err := NewLightningWallet(config, cdb, chainNotifier) if err != nil { return "", nil, err }