lnwallet: use the ChainNotifier interface throughout instead of BtcdNotifier
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.
This commit is contained in:
4 files changed
+28
-46
No files matched your search
+1
-1
@@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/btcsuite/fastsha256"
|
"github.com/btcsuite/fastsha256"
|
||||||
"github.com/davecgh/go-spew/spew"
|
"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/channeldb"
|
||||||
"github.com/lightningnetwork/lnd/lnwire"
|
"github.com/lightningnetwork/lnd/lnwire"
|
||||||
|
|
||||||
|
|||||||
@@ -13,9 +13,9 @@ import (
|
|||||||
// such as: uspv, btcwallet, Bitcoin Core, Electrum, etc. This interface then
|
// such as: uspv, btcwallet, Bitcoin Core, Electrum, etc. This interface then
|
||||||
// serves as a "base wallet", with Lightning Network awareness taking place at
|
// serves as a "base wallet", with Lightning Network awareness taking place at
|
||||||
// a "higher" level of abstraction. Essentially, an overlay wallet.
|
// a "higher" level of abstraction. Essentially, an overlay wallet.
|
||||||
// Implementors of this interface must closely adhere to the documented behavior
|
// Implementors of this interface must closely adhere to the documented
|
||||||
// of all interface methods in order to ensure identical behavior accross all
|
// behavior of all interface methods in order to ensure identical behavior
|
||||||
// concrete implementations.
|
// across all concrete implementations.
|
||||||
type WalletController interface {
|
type WalletController interface {
|
||||||
// ConfirmedBalance returns the sum of all the wallet's unspent outputs
|
// ConfirmedBalance returns the sum of all the wallet's unspent outputs
|
||||||
// that have at least confs confirmations. If confs is set to zero,
|
// that have at least confs confirmations. If confs is set to zero,
|
||||||
|
|||||||
+14
-41
@@ -9,8 +9,7 @@ import (
|
|||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
"github.com/lightningnetwork/lnd/chainntfs"
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
||||||
"github.com/lightningnetwork/lnd/chainntfs/btcdnotify"
|
|
||||||
"github.com/lightningnetwork/lnd/channeldb"
|
"github.com/lightningnetwork/lnd/channeldb"
|
||||||
"github.com/lightningnetwork/lnd/elkrem"
|
"github.com/lightningnetwork/lnd/elkrem"
|
||||||
"github.com/roasbeef/btcd/btcjson"
|
"github.com/roasbeef/btcd/btcjson"
|
||||||
@@ -18,7 +17,6 @@ import (
|
|||||||
"github.com/roasbeef/btcd/btcec"
|
"github.com/roasbeef/btcd/btcec"
|
||||||
"github.com/roasbeef/btcd/txscript"
|
"github.com/roasbeef/btcd/txscript"
|
||||||
"github.com/roasbeef/btcd/wire"
|
"github.com/roasbeef/btcd/wire"
|
||||||
"github.com/roasbeef/btcrpcclient"
|
|
||||||
"github.com/roasbeef/btcutil"
|
"github.com/roasbeef/btcutil"
|
||||||
"github.com/roasbeef/btcutil/coinset"
|
"github.com/roasbeef/btcutil/coinset"
|
||||||
"github.com/roasbeef/btcutil/txsort"
|
"github.com/roasbeef/btcutil/txsort"
|
||||||
@@ -234,8 +232,7 @@ type LightningWallet struct {
|
|||||||
// Used by in order to obtain notifications about funding transaction
|
// Used by in order to obtain notifications about funding transaction
|
||||||
// reaching a specified confirmation depth, and to catch
|
// reaching a specified confirmation depth, and to catch
|
||||||
// counterparty's broadcasting revoked commitment states.
|
// 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
|
// The core wallet, all non Lightning Network specific interaction is
|
||||||
// proxied to the internal wallet.
|
// proxied to the internal wallet.
|
||||||
@@ -279,7 +276,12 @@ type LightningWallet struct {
|
|||||||
// NewLightningWallet creates/opens and initializes a LightningWallet instance.
|
// NewLightningWallet creates/opens and initializes a LightningWallet instance.
|
||||||
// If the wallet has never been created (according to the passed dataDir), first-time
|
// If the wallet has never been created (according to the passed dataDir), first-time
|
||||||
// setup is executed.
|
// 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.
|
// Ensure the wallet exists or create it when the create flag is set.
|
||||||
netDir := networkDir(config.DataDir, config.NetParams)
|
netDir := networkDir(config.DataDir, config.NetParams)
|
||||||
|
|
||||||
@@ -344,26 +346,8 @@ func NewLightningWallet(config *Config, cdb *channeldb.DB) (*LightningWallet, er
|
|||||||
return nil, err
|
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{
|
return &LightningWallet{
|
||||||
ChainNotifier: chainNotifier,
|
chainNotifier: notifier,
|
||||||
rpc: rpcc,
|
rpc: rpcc,
|
||||||
Wallet: wallet,
|
Wallet: wallet,
|
||||||
channelDB: cdb,
|
channelDB: cdb,
|
||||||
@@ -393,17 +377,8 @@ func (l *LightningWallet) Startup() error {
|
|||||||
}
|
}
|
||||||
l.Start()
|
l.Start()
|
||||||
|
|
||||||
// Start the notification server. This is used so channel managment
|
// Pass the rpc client into the wallet so it can sync up to the
|
||||||
// goroutines can be notified when a funding transaction reaches a
|
// current main chain.
|
||||||
// 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.
|
|
||||||
l.SynchronizeRPC(l.rpc)
|
l.SynchronizeRPC(l.rpc)
|
||||||
|
|
||||||
l.wg.Add(1)
|
l.wg.Add(1)
|
||||||
@@ -426,8 +401,6 @@ func (l *LightningWallet) Shutdown() error {
|
|||||||
|
|
||||||
l.rpc.Shutdown()
|
l.rpc.Shutdown()
|
||||||
|
|
||||||
l.ChainNotifier.Stop()
|
|
||||||
|
|
||||||
close(l.quit)
|
close(l.quit)
|
||||||
l.wg.Wait()
|
l.wg.Wait()
|
||||||
return nil
|
return nil
|
||||||
@@ -1251,7 +1224,7 @@ func (l *LightningWallet) handleChannelOpen(req *channelOpenMsg) {
|
|||||||
|
|
||||||
// Finally, create and officially open the payment channel!
|
// Finally, create and officially open the payment channel!
|
||||||
// TODO(roasbeef): CreationTime once tx is 'open'
|
// TODO(roasbeef): CreationTime once tx is 'open'
|
||||||
channel, _ := NewLightningChannel(l, l.ChainNotifier, l.channelDB,
|
channel, _ := NewLightningChannel(l, l.chainNotifier, l.channelDB,
|
||||||
res.partialState)
|
res.partialState)
|
||||||
|
|
||||||
res.chanOpen <- channel
|
res.chanOpen <- channel
|
||||||
@@ -1266,7 +1239,7 @@ func (l *LightningWallet) openChannelAfterConfirmations(res *ChannelReservation)
|
|||||||
// transaction reaches `numConfs` confirmations.
|
// transaction reaches `numConfs` confirmations.
|
||||||
txid := res.fundingTx.TxSha()
|
txid := res.fundingTx.TxSha()
|
||||||
numConfs := uint32(res.numConfsToOpen)
|
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",
|
walletLog.Infof("Waiting for funding tx (txid: %v) to reach %v confirmations",
|
||||||
txid, numConfs)
|
txid, numConfs)
|
||||||
@@ -1293,7 +1266,7 @@ out:
|
|||||||
|
|
||||||
// Finally, create and officially open the payment channel!
|
// Finally, create and officially open the payment channel!
|
||||||
// TODO(roasbeef): CreationTime once tx is 'open'
|
// TODO(roasbeef): CreationTime once tx is 'open'
|
||||||
channel, _ := NewLightningChannel(l, l.ChainNotifier, l.channelDB,
|
channel, _ := NewLightningChannel(l, l.chainNotifier, l.channelDB,
|
||||||
res.partialState)
|
res.partialState)
|
||||||
res.chanOpen <- channel
|
res.chanOpen <- channel
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-1
@@ -11,6 +11,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/boltdb/bolt"
|
"github.com/boltdb/bolt"
|
||||||
|
"github.com/lightningnetwork/lnd/chainntnfs/btcdnotify"
|
||||||
"github.com/lightningnetwork/lnd/channeldb"
|
"github.com/lightningnetwork/lnd/channeldb"
|
||||||
"github.com/roasbeef/btcd/chaincfg"
|
"github.com/roasbeef/btcd/chaincfg"
|
||||||
"github.com/roasbeef/btcutil/txsort"
|
"github.com/roasbeef/btcutil/txsort"
|
||||||
@@ -338,7 +339,15 @@ func createTestWallet(miningNode *rpctest.Harness, netParams *chaincfg.Params) (
|
|||||||
return "", nil, err
|
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 {
|
if err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user