lnwallet: embed btwallet within LightningWallet, exposes all methods publicly
* As a result in order to not over-shadow the Start/Stop methods of btcwallet.Wallet, the Start/Stop methods on LightningWallet have been renamed to Startup/Shutdown.
This commit is contained in:
parent
6a9011654a
commit
f1717b9620
@ -205,7 +205,7 @@ type LightningWallet struct {
|
|||||||
// 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.
|
||||||
// TODO(roasbeef): Why isn't this just embedded again?
|
// TODO(roasbeef): Why isn't this just embedded again?
|
||||||
wallet *btcwallet.Wallet
|
*btcwallet.Wallet
|
||||||
|
|
||||||
// An active RPC connection to a full-node. In the case of a btcd node,
|
// An active RPC connection to a full-node. In the case of a btcd node,
|
||||||
// websockets are used for notifications. If using Bitcoin Core,
|
// websockets are used for notifications. If using Bitcoin Core,
|
||||||
@ -287,7 +287,7 @@ func NewLightningWallet(privWalletPass, pubWalletPass, hdSeed []byte, dataDir st
|
|||||||
|
|
||||||
return &LightningWallet{
|
return &LightningWallet{
|
||||||
db: db,
|
db: db,
|
||||||
wallet: wallet,
|
Wallet: wallet,
|
||||||
ChannelDB: channeldb.New(wallet.Manager, lnNamespace),
|
ChannelDB: channeldb.New(wallet.Manager, lnNamespace),
|
||||||
msgChan: make(chan interface{}, msgBufferSize),
|
msgChan: make(chan interface{}, msgBufferSize),
|
||||||
// TODO(roasbeef): make this atomic.Uint32 instead? Which is
|
// TODO(roasbeef): make this atomic.Uint32 instead? Which is
|
||||||
@ -299,9 +299,9 @@ func NewLightningWallet(privWalletPass, pubWalletPass, hdSeed []byte, dataDir st
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start establishes a connection to the RPC source, and spins up all
|
// Startup establishes a connection to the RPC source, and spins up all
|
||||||
// goroutines required to handle incoming messages.
|
// goroutines required to handle incoming messages.
|
||||||
func (l *LightningWallet) Start() error {
|
func (l *LightningWallet) Startup() error {
|
||||||
// Already started?
|
// Already started?
|
||||||
if atomic.AddInt32(&l.started, 1) != 1 {
|
if atomic.AddInt32(&l.started, 1) != 1 {
|
||||||
return nil
|
return nil
|
||||||
@ -316,7 +316,7 @@ func (l *LightningWallet) Start() error {
|
|||||||
|
|
||||||
// Start the goroutines in the underlying wallet.
|
// Start the goroutines in the underlying wallet.
|
||||||
l.rpc = rpcc
|
l.rpc = rpcc
|
||||||
l.wallet.Start(rpcc)
|
l.Start(rpcc)
|
||||||
|
|
||||||
l.wg.Add(1)
|
l.wg.Add(1)
|
||||||
// TODO(roasbeef): multiple request handlers?
|
// TODO(roasbeef): multiple request handlers?
|
||||||
@ -325,13 +325,13 @@ func (l *LightningWallet) Start() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop gracefully shutsdown the wallet, and all active goroutines.
|
// Shutdown gracefully stops the wallet, and all active goroutines.
|
||||||
func (l *LightningWallet) Stop() error {
|
func (l *LightningWallet) Shutdown() error {
|
||||||
if atomic.AddInt32(&l.shutdown, 1) != 1 {
|
if atomic.AddInt32(&l.shutdown, 1) != 1 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
l.wallet.Stop()
|
l.Stop()
|
||||||
l.rpc.Shutdown()
|
l.rpc.Shutdown()
|
||||||
|
|
||||||
close(l.quit)
|
close(l.quit)
|
||||||
@ -431,7 +431,7 @@ func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg
|
|||||||
// Find all unlocked unspent outputs with greater than 6 confirmations.
|
// Find all unlocked unspent outputs with greater than 6 confirmations.
|
||||||
maxConfs := int32(math.MaxInt32)
|
maxConfs := int32(math.MaxInt32)
|
||||||
// TODO(roasbeef): make 6 a config paramter?
|
// TODO(roasbeef): make 6 a config paramter?
|
||||||
unspentOutputs, err := l.wallet.ListUnspent(6, maxConfs, nil)
|
unspentOutputs, err := l.ListUnspent(6, maxConfs, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.coinSelectMtx.Unlock()
|
l.coinSelectMtx.Unlock()
|
||||||
req.err <- err
|
req.err <- err
|
||||||
@ -475,7 +475,7 @@ func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg
|
|||||||
ourContribution.Inputs = make([]*wire.TxIn, len(selectedCoins.Coins()))
|
ourContribution.Inputs = make([]*wire.TxIn, len(selectedCoins.Coins()))
|
||||||
for i, coin := range selectedCoins.Coins() {
|
for i, coin := range selectedCoins.Coins() {
|
||||||
txout := wire.NewOutPoint(coin.Hash(), coin.Index())
|
txout := wire.NewOutPoint(coin.Hash(), coin.Index())
|
||||||
l.wallet.LockOutpoint(*txout)
|
l.LockOutpoint(*txout)
|
||||||
|
|
||||||
// Empty sig script, we'll actually sign if this reservation is
|
// Empty sig script, we'll actually sign if this reservation is
|
||||||
// queued up to be completed (the other side accepts).
|
// queued up to be completed (the other side accepts).
|
||||||
@ -492,7 +492,7 @@ func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg
|
|||||||
// Change is necessary. Query for an available change address to
|
// Change is necessary. Query for an available change address to
|
||||||
// send the remainder to.
|
// send the remainder to.
|
||||||
changeAmount := selectedTotalValue - req.fundingAmount
|
changeAmount := selectedTotalValue - req.fundingAmount
|
||||||
addrs, err := l.wallet.Manager.NextInternalAddresses(waddrmgr.DefaultAccountNum, 1)
|
addrs, err := l.Manager.NextInternalAddresses(waddrmgr.DefaultAccountNum, 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
req.err <- err
|
req.err <- err
|
||||||
req.resp <- nil
|
req.resp <- nil
|
||||||
@ -541,7 +541,7 @@ func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg
|
|||||||
// channel close.
|
// channel close.
|
||||||
// TODO(roasbeef): same here
|
// TODO(roasbeef): same here
|
||||||
//deliveryAddress, err := l.wallet.NewChangeAddress(waddrmgr.DefaultAccountNum)
|
//deliveryAddress, err := l.wallet.NewChangeAddress(waddrmgr.DefaultAccountNum)
|
||||||
addrs, err := l.wallet.Manager.NextInternalAddresses(waddrmgr.DefaultAccountNum, 1)
|
addrs, err := l.Manager.NextInternalAddresses(waddrmgr.DefaultAccountNum, 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO(roasbeef): make into func sendErorr()
|
// TODO(roasbeef): make into func sendErorr()
|
||||||
req.err <- err
|
req.err <- err
|
||||||
@ -595,7 +595,7 @@ func (l *LightningWallet) handleFundingCancelRequest(req *fundingReserveCancelMs
|
|||||||
// Mark all previously locked outpoints as usuable for future funding
|
// Mark all previously locked outpoints as usuable for future funding
|
||||||
// requests.
|
// requests.
|
||||||
for _, unusedInput := range pendingReservation.ourContribution.Inputs {
|
for _, unusedInput := range pendingReservation.ourContribution.Inputs {
|
||||||
l.wallet.UnlockOutpoint(unusedInput.PreviousOutPoint)
|
l.UnlockOutpoint(unusedInput.PreviousOutPoint)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(roasbeef): is it even worth it to keep track of unsed keys?
|
// TODO(roasbeef): is it even worth it to keep track of unsed keys?
|
||||||
@ -691,7 +691,7 @@ func (l *LightningWallet) handleContributionMsg(req *addContributionMsg) {
|
|||||||
pendingReservation.ourFundingSigs = make([][]byte, 0, len(ourContribution.Inputs))
|
pendingReservation.ourFundingSigs = make([][]byte, 0, len(ourContribution.Inputs))
|
||||||
for i, txIn := range fundingTx.TxIn {
|
for i, txIn := range fundingTx.TxIn {
|
||||||
// Does the wallet know about the txin?
|
// Does the wallet know about the txin?
|
||||||
txDetail, _ := l.wallet.TxStore.TxDetails(&txIn.PreviousOutPoint.Hash)
|
txDetail, _ := l.TxStore.TxDetails(&txIn.PreviousOutPoint.Hash)
|
||||||
if txDetail == nil {
|
if txDetail == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -706,7 +706,7 @@ func (l *LightningWallet) handleContributionMsg(req *addContributionMsg) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ai, err := l.wallet.Manager.Address(apkh)
|
ai, err := l.Manager.Address(apkh)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
req.err <- fmt.Errorf("cannot get address info: %v", err)
|
req.err <- fmt.Errorf("cannot get address info: %v", err)
|
||||||
return
|
return
|
||||||
@ -939,7 +939,7 @@ func (l *LightningWallet) getNextRawKey() (*btcec.PrivateKey, error) {
|
|||||||
l.keyGenMtx.Lock()
|
l.keyGenMtx.Lock()
|
||||||
defer l.keyGenMtx.Unlock()
|
defer l.keyGenMtx.Unlock()
|
||||||
|
|
||||||
nextAddr, err := l.wallet.Manager.NextExternalAddresses(waddrmgr.DefaultAccountNum, 1)
|
nextAddr, err := l.Manager.NextExternalAddresses(waddrmgr.DefaultAccountNum, 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ var (
|
|||||||
// within the wallet are *exactly* amount. If unable to retrieve the current
|
// within the wallet are *exactly* amount. If unable to retrieve the current
|
||||||
// balance, or the assertion fails, the test will halt with a fatal error.
|
// balance, or the assertion fails, the test will halt with a fatal error.
|
||||||
func assertProperBalance(t *testing.T, lw *LightningWallet, numConfirms, amount int32) {
|
func assertProperBalance(t *testing.T, lw *LightningWallet, numConfirms, amount int32) {
|
||||||
balance, err := lw.wallet.TxStore.Balance(0, 20)
|
balance, err := lw.TxStore.Balance(0, 20)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to query for balance: %v", err)
|
t.Fatalf("unable to query for balance: %v", err)
|
||||||
}
|
}
|
||||||
@ -175,7 +175,7 @@ func newBobNode() (*bobNode, error) {
|
|||||||
// addTestTx adds an output spendable by our test wallet, marked as included in
|
// addTestTx adds an output spendable by our test wallet, marked as included in
|
||||||
// 'block'.
|
// 'block'.
|
||||||
func addTestTx(w *LightningWallet, rec *wtxmgr.TxRecord, block *wtxmgr.BlockMeta) error {
|
func addTestTx(w *LightningWallet, rec *wtxmgr.TxRecord, block *wtxmgr.BlockMeta) error {
|
||||||
err := w.wallet.TxStore.InsertTx(rec, block)
|
err := w.TxStore.InsertTx(rec, block)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -190,14 +190,14 @@ func addTestTx(w *LightningWallet, rec *wtxmgr.TxRecord, block *wtxmgr.BlockMeta
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _, addr := range addrs {
|
for _, addr := range addrs {
|
||||||
ma, err := w.wallet.Manager.Address(addr)
|
ma, err := w.Manager.Address(addr)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = w.wallet.TxStore.AddCredit(rec, block, uint32(i),
|
err = w.TxStore.AddCredit(rec, block, uint32(i),
|
||||||
ma.Internal())
|
ma.Internal())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = w.wallet.Manager.MarkUsed(addr)
|
err = w.Manager.MarkUsed(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -226,7 +226,7 @@ func loadTestCredits(w *LightningWallet, numOutputs, btcPerOutput int) error {
|
|||||||
// Import the priv key (converting to WIF) above that controls all our
|
// Import the priv key (converting to WIF) above that controls all our
|
||||||
// available outputs.
|
// available outputs.
|
||||||
privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), testWalletPrivKey)
|
privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), testWalletPrivKey)
|
||||||
if err := w.wallet.Unlock(privPass, time.Duration(0)); err != nil {
|
if err := w.Unlock(privPass, time.Duration(0)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
bs := &waddrmgr.BlockStamp{Hash: *genBlockHash(1), Height: 1}
|
bs := &waddrmgr.BlockStamp{Hash: *genBlockHash(1), Height: 1}
|
||||||
@ -234,10 +234,10 @@ func loadTestCredits(w *LightningWallet, numOutputs, btcPerOutput int) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if _, err := w.wallet.ImportPrivateKey(wif, bs, false); err != nil {
|
if _, err := w.ImportPrivateKey(wif, bs, false); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if err := w.wallet.Manager.SetSyncedTo(&waddrmgr.BlockStamp{int32(1), *genBlockHash(1)}); err != nil {
|
if err := w.Manager.SetSyncedTo(&waddrmgr.BlockStamp{int32(1), *genBlockHash(1)}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -273,7 +273,7 @@ func loadTestCredits(w *LightningWallet, numOutputs, btcPerOutput int) error {
|
|||||||
if err := addTestTx(w, txCredit, &blk); err != nil {
|
if err := addTestTx(w, txCredit, &blk); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := w.wallet.Manager.SetSyncedTo(&waddrmgr.BlockStamp{int32(2), *genBlockHash(2)}); err != nil {
|
if err := w.Manager.SetSyncedTo(&waddrmgr.BlockStamp{int32(2), *genBlockHash(2)}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -282,7 +282,7 @@ func loadTestCredits(w *LightningWallet, numOutputs, btcPerOutput int) error {
|
|||||||
// (hard coded to 6 atm).
|
// (hard coded to 6 atm).
|
||||||
for i := 3; i < 10; i++ {
|
for i := 3; i < 10; i++ {
|
||||||
sha := *genBlockHash(i)
|
sha := *genBlockHash(i)
|
||||||
if err := w.wallet.Manager.SetSyncedTo(&waddrmgr.BlockStamp{int32(i), sha}); err != nil {
|
if err := w.Manager.SetSyncedTo(&waddrmgr.BlockStamp{int32(i), sha}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -304,7 +304,7 @@ func createTestWallet() (string, *LightningWallet, error) {
|
|||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
// TODO(roasbeef): check error once nodetest is finished.
|
// TODO(roasbeef): check error once nodetest is finished.
|
||||||
_ = wallet.Start()
|
_ = wallet.Startup()
|
||||||
|
|
||||||
// Load our test wallet with 5 outputs each holding 4BTC.
|
// Load our test wallet with 5 outputs each holding 4BTC.
|
||||||
if err := loadTestCredits(wallet, 5, 4); err != nil {
|
if err := loadTestCredits(wallet, 5, 4); err != nil {
|
||||||
@ -446,7 +446,7 @@ func testBasicWalletReservationWorkFlow(lnwallet *LightningWallet, t *testing.T)
|
|||||||
pkscript = bobNode.changeOutputs[0].PkScript
|
pkscript = bobNode.changeOutputs[0].PkScript
|
||||||
} else {
|
} else {
|
||||||
// Does the wallet know about the txin?
|
// Does the wallet know about the txin?
|
||||||
txDetail, err := lnwallet.wallet.TxStore.TxDetails(&input.PreviousOutPoint.Hash)
|
txDetail, err := lnwallet.TxStore.TxDetails(&input.PreviousOutPoint.Hash)
|
||||||
if txDetail == nil || err != nil {
|
if txDetail == nil || err != nil {
|
||||||
t.Fatalf("txstore can't find tx detail, err: %v", err)
|
t.Fatalf("txstore can't find tx detail, err: %v", err)
|
||||||
}
|
}
|
||||||
@ -531,7 +531,7 @@ func testFundingCancellationNotEnoughFunds(lnwallet *LightningWallet, t *testing
|
|||||||
}
|
}
|
||||||
|
|
||||||
// There should be three locked outpoints.
|
// There should be three locked outpoints.
|
||||||
lockedOutPoints := lnwallet.wallet.LockedOutpoints()
|
lockedOutPoints := lnwallet.LockedOutpoints()
|
||||||
if len(lockedOutPoints) != 3 {
|
if len(lockedOutPoints) != 3 {
|
||||||
t.Fatalf("two outpoints should now be locked, instead %v are",
|
t.Fatalf("two outpoints should now be locked, instead %v are",
|
||||||
lockedOutPoints)
|
lockedOutPoints)
|
||||||
@ -551,7 +551,7 @@ func testFundingCancellationNotEnoughFunds(lnwallet *LightningWallet, t *testing
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Those outpoints should no longer be locked.
|
// Those outpoints should no longer be locked.
|
||||||
lockedOutPoints = lnwallet.wallet.LockedOutpoints()
|
lockedOutPoints = lnwallet.LockedOutpoints()
|
||||||
if len(lockedOutPoints) != 0 {
|
if len(lockedOutPoints) != 0 {
|
||||||
t.Fatalf("outpoints still locked")
|
t.Fatalf("outpoints still locked")
|
||||||
}
|
}
|
||||||
@ -607,7 +607,7 @@ type testLnWallet struct {
|
|||||||
func clearWalletState(w *LightningWallet) error {
|
func clearWalletState(w *LightningWallet) error {
|
||||||
w.nextFundingID = 0
|
w.nextFundingID = 0
|
||||||
w.fundingLimbo = make(map[uint64]*ChannelReservation)
|
w.fundingLimbo = make(map[uint64]*ChannelReservation)
|
||||||
w.wallet.ResetLockedOutpoints()
|
w.ResetLockedOutpoints()
|
||||||
return w.ChannelDB.Wipe()
|
return w.ChannelDB.Wipe()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -621,7 +621,7 @@ func TestLightningWallet(t *testing.T) {
|
|||||||
t.Fatalf("unable to create test ln wallet: %v", err)
|
t.Fatalf("unable to create test ln wallet: %v", err)
|
||||||
}
|
}
|
||||||
defer os.RemoveAll(testDir)
|
defer os.RemoveAll(testDir)
|
||||||
defer lnwallet.Stop()
|
defer lnwallet.Shutdown()
|
||||||
|
|
||||||
// The wallet should now have 20BTC available for spending.
|
// The wallet should now have 20BTC available for spending.
|
||||||
assertProperBalance(t, lnwallet, 1, 20)
|
assertProperBalance(t, lnwallet, 1, 20)
|
||||||
|
Loading…
Reference in New Issue
Block a user