mirror of
https://github.com/aljazceru/breez-lnd.git
synced 2025-12-18 22:54:26 +01:00
fundingmanager+lnwallet: make InitChannelReservation take in req rather than many args
This commit is contained in:
@@ -1042,12 +1042,19 @@ func (f *fundingManager) handleFundingOpen(fmsg *fundingOpenMsg) {
|
|||||||
// responding side of a single funder workflow, we don't commit any
|
// responding side of a single funder workflow, we don't commit any
|
||||||
// funds to the channel ourselves.
|
// funds to the channel ourselves.
|
||||||
chainHash := chainhash.Hash(msg.ChainHash)
|
chainHash := chainhash.Hash(msg.ChainHash)
|
||||||
reservation, err := f.cfg.Wallet.InitChannelReservation(
|
req := &lnwallet.InitFundingReserveMsg{
|
||||||
amt, 0, msg.PushAmount,
|
ChainHash: &chainHash,
|
||||||
lnwallet.SatPerKWeight(msg.FeePerKiloWeight), 0,
|
NodeID: fmsg.peer.IdentityKey(),
|
||||||
fmsg.peer.IdentityKey(), fmsg.peer.Address(), &chainHash,
|
NodeAddr: fmsg.peer.Address(),
|
||||||
msg.ChannelFlags,
|
FundingAmount: 0,
|
||||||
)
|
Capacity: amt,
|
||||||
|
CommitFeePerKw: lnwallet.SatPerKWeight(msg.FeePerKiloWeight),
|
||||||
|
FundingFeePerKw: 0,
|
||||||
|
PushMSat: msg.PushAmount,
|
||||||
|
Flags: msg.ChannelFlags,
|
||||||
|
}
|
||||||
|
|
||||||
|
reservation, err := f.cfg.Wallet.InitChannelReservation(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fndgLog.Errorf("Unable to initialize reservation: %v", err)
|
fndgLog.Errorf("Unable to initialize reservation: %v", err)
|
||||||
f.failFundingFlow(fmsg.peer, msg.PendingChannelID, err)
|
f.failFundingFlow(fmsg.peer, msg.PendingChannelID, err)
|
||||||
@@ -2598,11 +2605,19 @@ func (f *fundingManager) handleInitFundingMsg(msg *initFundingMsg) {
|
|||||||
// Initialize a funding reservation with the local wallet. If the
|
// Initialize a funding reservation with the local wallet. If the
|
||||||
// wallet doesn't have enough funds to commit to this channel, then the
|
// wallet doesn't have enough funds to commit to this channel, then the
|
||||||
// request will fail, and be aborted.
|
// request will fail, and be aborted.
|
||||||
reservation, err := f.cfg.Wallet.InitChannelReservation(
|
req := &lnwallet.InitFundingReserveMsg{
|
||||||
capacity, localAmt, msg.pushAmt, commitFeePerKw,
|
ChainHash: &msg.chainHash,
|
||||||
msg.fundingFeePerKw, peerKey, msg.peer.Address(),
|
NodeID: peerKey,
|
||||||
&msg.chainHash, channelFlags,
|
NodeAddr: msg.peer.Address(),
|
||||||
)
|
FundingAmount: localAmt,
|
||||||
|
Capacity: capacity,
|
||||||
|
CommitFeePerKw: commitFeePerKw,
|
||||||
|
FundingFeePerKw: msg.fundingFeePerKw,
|
||||||
|
PushMSat: msg.pushAmt,
|
||||||
|
Flags: channelFlags,
|
||||||
|
}
|
||||||
|
|
||||||
|
reservation, err := f.cfg.Wallet.InitChannelReservation(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg.err <- err
|
msg.err <- err
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -294,10 +294,18 @@ func testDualFundingReservationWorkflow(miner *rpctest.Harness,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to query fee estimator: %v", err)
|
t.Fatalf("unable to query fee estimator: %v", err)
|
||||||
}
|
}
|
||||||
aliceChanReservation, err := alice.InitChannelReservation(
|
aliceReq := &lnwallet.InitFundingReserveMsg{
|
||||||
fundingAmount*2, fundingAmount, 0, feePerKw, feePerKw, bobPub,
|
ChainHash: chainHash,
|
||||||
bobAddr, chainHash, lnwire.FFAnnounceChannel,
|
NodeID: bobPub,
|
||||||
)
|
NodeAddr: bobAddr,
|
||||||
|
FundingAmount: fundingAmount,
|
||||||
|
Capacity: fundingAmount * 2,
|
||||||
|
CommitFeePerKw: feePerKw,
|
||||||
|
FundingFeePerKw: feePerKw,
|
||||||
|
PushMSat: 0,
|
||||||
|
Flags: lnwire.FFAnnounceChannel,
|
||||||
|
}
|
||||||
|
aliceChanReservation, err := alice.InitChannelReservation(aliceReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to initialize funding reservation: %v", err)
|
t.Fatalf("unable to initialize funding reservation: %v", err)
|
||||||
}
|
}
|
||||||
@@ -325,10 +333,18 @@ func testDualFundingReservationWorkflow(miner *rpctest.Harness,
|
|||||||
// Bob does the same, generating his own contribution. He then also
|
// Bob does the same, generating his own contribution. He then also
|
||||||
// receives' Alice's contribution, and consumes that so we can continue
|
// receives' Alice's contribution, and consumes that so we can continue
|
||||||
// the funding process.
|
// the funding process.
|
||||||
bobChanReservation, err := bob.InitChannelReservation(
|
bobReq := &lnwallet.InitFundingReserveMsg{
|
||||||
fundingAmount*2, fundingAmount, 0, feePerKw, feePerKw, alicePub,
|
ChainHash: chainHash,
|
||||||
aliceAddr, chainHash, lnwire.FFAnnounceChannel,
|
NodeID: alicePub,
|
||||||
)
|
NodeAddr: aliceAddr,
|
||||||
|
FundingAmount: fundingAmount,
|
||||||
|
Capacity: fundingAmount * 2,
|
||||||
|
CommitFeePerKw: feePerKw,
|
||||||
|
FundingFeePerKw: feePerKw,
|
||||||
|
PushMSat: 0,
|
||||||
|
Flags: lnwire.FFAnnounceChannel,
|
||||||
|
}
|
||||||
|
bobChanReservation, err := bob.InitChannelReservation(bobReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("bob unable to init channel reservation: %v", err)
|
t.Fatalf("bob unable to init channel reservation: %v", err)
|
||||||
}
|
}
|
||||||
@@ -481,11 +497,18 @@ func testFundingTransactionLockedOutputs(miner *rpctest.Harness,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to query fee estimator: %v", err)
|
t.Fatalf("unable to query fee estimator: %v", err)
|
||||||
}
|
}
|
||||||
_, err = alice.InitChannelReservation(
|
req := &lnwallet.InitFundingReserveMsg{
|
||||||
fundingAmount, fundingAmount, 0, feePerKw, feePerKw, bobPub,
|
ChainHash: chainHash,
|
||||||
bobAddr, chainHash, lnwire.FFAnnounceChannel,
|
NodeID: bobPub,
|
||||||
)
|
NodeAddr: bobAddr,
|
||||||
if err != nil {
|
FundingAmount: fundingAmount,
|
||||||
|
Capacity: fundingAmount,
|
||||||
|
CommitFeePerKw: feePerKw,
|
||||||
|
FundingFeePerKw: feePerKw,
|
||||||
|
PushMSat: 0,
|
||||||
|
Flags: lnwire.FFAnnounceChannel,
|
||||||
|
}
|
||||||
|
if _, err := alice.InitChannelReservation(req); err != nil {
|
||||||
t.Fatalf("unable to initialize funding reservation 1: %v", err)
|
t.Fatalf("unable to initialize funding reservation 1: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -496,10 +519,18 @@ func testFundingTransactionLockedOutputs(miner *rpctest.Harness,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to create amt: %v", err)
|
t.Fatalf("unable to create amt: %v", err)
|
||||||
}
|
}
|
||||||
failedReservation, err := alice.InitChannelReservation(
|
failedReq := &lnwallet.InitFundingReserveMsg{
|
||||||
amt, amt, 0, feePerKw, feePerKw, bobPub, bobAddr, chainHash,
|
ChainHash: chainHash,
|
||||||
lnwire.FFAnnounceChannel,
|
NodeID: bobPub,
|
||||||
)
|
NodeAddr: bobAddr,
|
||||||
|
FundingAmount: amt,
|
||||||
|
Capacity: amt,
|
||||||
|
CommitFeePerKw: feePerKw,
|
||||||
|
FundingFeePerKw: feePerKw,
|
||||||
|
PushMSat: 0,
|
||||||
|
Flags: lnwire.FFAnnounceChannel,
|
||||||
|
}
|
||||||
|
failedReservation, err := alice.InitChannelReservation(failedReq)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("not error returned, should fail on coin selection")
|
t.Fatalf("not error returned, should fail on coin selection")
|
||||||
}
|
}
|
||||||
@@ -524,19 +555,24 @@ func testFundingCancellationNotEnoughFunds(miner *rpctest.Harness,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to create amt: %v", err)
|
t.Fatalf("unable to create amt: %v", err)
|
||||||
}
|
}
|
||||||
chanReservation, err := alice.InitChannelReservation(
|
req := &lnwallet.InitFundingReserveMsg{
|
||||||
fundingAmount, fundingAmount, 0, feePerKw, feePerKw, bobPub,
|
ChainHash: chainHash,
|
||||||
bobAddr, chainHash, lnwire.FFAnnounceChannel,
|
NodeID: bobPub,
|
||||||
)
|
NodeAddr: bobAddr,
|
||||||
|
FundingAmount: fundingAmount,
|
||||||
|
Capacity: fundingAmount,
|
||||||
|
CommitFeePerKw: feePerKw,
|
||||||
|
FundingFeePerKw: feePerKw,
|
||||||
|
PushMSat: 0,
|
||||||
|
Flags: lnwire.FFAnnounceChannel,
|
||||||
|
}
|
||||||
|
chanReservation, err := alice.InitChannelReservation(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to initialize funding reservation: %v", err)
|
t.Fatalf("unable to initialize funding reservation: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempt to create another channel with 44 BTC, this should fail.
|
// Attempt to create another channel with 44 BTC, this should fail.
|
||||||
_, err = alice.InitChannelReservation(
|
_, err = alice.InitChannelReservation(req)
|
||||||
fundingAmount, fundingAmount, 0, feePerKw, feePerKw, bobPub,
|
|
||||||
bobAddr, chainHash, lnwire.FFAnnounceChannel,
|
|
||||||
)
|
|
||||||
if _, ok := err.(*lnwallet.ErrInsufficientFunds); !ok {
|
if _, ok := err.(*lnwallet.ErrInsufficientFunds); !ok {
|
||||||
t.Fatalf("coin selection succeeded should have insufficient funds: %v",
|
t.Fatalf("coin selection succeeded should have insufficient funds: %v",
|
||||||
err)
|
err)
|
||||||
@@ -565,10 +601,7 @@ func testFundingCancellationNotEnoughFunds(miner *rpctest.Harness,
|
|||||||
// attempting coin selection.
|
// attempting coin selection.
|
||||||
|
|
||||||
// Request to fund a new channel should now succeed.
|
// Request to fund a new channel should now succeed.
|
||||||
_, err = alice.InitChannelReservation(fundingAmount, fundingAmount,
|
if _, err := alice.InitChannelReservation(req); err != nil {
|
||||||
0, feePerKw, feePerKw, bobPub, bobAddr, chainHash,
|
|
||||||
lnwire.FFAnnounceChannel)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to initialize funding reservation: %v", err)
|
t.Fatalf("unable to initialize funding reservation: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -612,10 +645,18 @@ func testReservationInitiatorBalanceBelowDustCancel(miner *rpctest.Harness,
|
|||||||
feePerKw := lnwallet.SatPerKWeight(
|
feePerKw := lnwallet.SatPerKWeight(
|
||||||
numBTC * numBTC * btcutil.SatoshiPerBitcoin,
|
numBTC * numBTC * btcutil.SatoshiPerBitcoin,
|
||||||
)
|
)
|
||||||
_, err = alice.InitChannelReservation(
|
req := &lnwallet.InitFundingReserveMsg{
|
||||||
fundingAmount, fundingAmount, 0, feePerKw, feePerKw, bobPub,
|
ChainHash: chainHash,
|
||||||
bobAddr, chainHash, lnwire.FFAnnounceChannel,
|
NodeID: bobPub,
|
||||||
)
|
NodeAddr: bobAddr,
|
||||||
|
FundingAmount: fundingAmount,
|
||||||
|
Capacity: fundingAmount,
|
||||||
|
CommitFeePerKw: feePerKw,
|
||||||
|
FundingFeePerKw: feePerKw,
|
||||||
|
PushMSat: 0,
|
||||||
|
Flags: lnwire.FFAnnounceChannel,
|
||||||
|
}
|
||||||
|
_, err = alice.InitChannelReservation(req)
|
||||||
switch {
|
switch {
|
||||||
case err == nil:
|
case err == nil:
|
||||||
t.Fatalf("initialization should have failed due to " +
|
t.Fatalf("initialization should have failed due to " +
|
||||||
@@ -685,10 +726,18 @@ func testSingleFunderReservationWorkflow(miner *rpctest.Harness,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to query fee estimator: %v", err)
|
t.Fatalf("unable to query fee estimator: %v", err)
|
||||||
}
|
}
|
||||||
aliceChanReservation, err := alice.InitChannelReservation(
|
aliceReq := &lnwallet.InitFundingReserveMsg{
|
||||||
fundingAmt, fundingAmt, pushAmt, feePerKw, feePerKw, bobPub,
|
ChainHash: chainHash,
|
||||||
bobAddr, chainHash, lnwire.FFAnnounceChannel,
|
NodeID: bobPub,
|
||||||
)
|
NodeAddr: bobAddr,
|
||||||
|
FundingAmount: fundingAmt,
|
||||||
|
Capacity: fundingAmt,
|
||||||
|
CommitFeePerKw: feePerKw,
|
||||||
|
FundingFeePerKw: feePerKw,
|
||||||
|
PushMSat: pushAmt,
|
||||||
|
Flags: lnwire.FFAnnounceChannel,
|
||||||
|
}
|
||||||
|
aliceChanReservation, err := alice.InitChannelReservation(aliceReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to init channel reservation: %v", err)
|
t.Fatalf("unable to init channel reservation: %v", err)
|
||||||
}
|
}
|
||||||
@@ -716,10 +765,18 @@ func testSingleFunderReservationWorkflow(miner *rpctest.Harness,
|
|||||||
|
|
||||||
// Next, Bob receives the initial request, generates a corresponding
|
// Next, Bob receives the initial request, generates a corresponding
|
||||||
// reservation initiation, then consume Alice's contribution.
|
// reservation initiation, then consume Alice's contribution.
|
||||||
bobChanReservation, err := bob.InitChannelReservation(
|
bobReq := &lnwallet.InitFundingReserveMsg{
|
||||||
fundingAmt, 0, pushAmt, feePerKw, feePerKw, alicePub, aliceAddr,
|
ChainHash: chainHash,
|
||||||
chainHash, lnwire.FFAnnounceChannel,
|
NodeID: alicePub,
|
||||||
)
|
NodeAddr: aliceAddr,
|
||||||
|
FundingAmount: 0,
|
||||||
|
Capacity: fundingAmt,
|
||||||
|
CommitFeePerKw: feePerKw,
|
||||||
|
FundingFeePerKw: feePerKw,
|
||||||
|
PushMSat: pushAmt,
|
||||||
|
Flags: lnwire.FFAnnounceChannel,
|
||||||
|
}
|
||||||
|
bobChanReservation, err := bob.InitChannelReservation(bobReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to create bob reservation: %v", err)
|
t.Fatalf("unable to create bob reservation: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,24 +3,24 @@ package lnwallet
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/blockchain"
|
"github.com/btcsuite/btcd/blockchain"
|
||||||
|
"github.com/btcsuite/btcd/btcec"
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||||
|
"github.com/btcsuite/btcd/txscript"
|
||||||
|
"github.com/btcsuite/btcd/wire"
|
||||||
|
"github.com/btcsuite/btcutil"
|
||||||
"github.com/btcsuite/btcutil/hdkeychain"
|
"github.com/btcsuite/btcutil/hdkeychain"
|
||||||
|
"github.com/btcsuite/btcutil/txsort"
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
"github.com/lightningnetwork/lnd/channeldb"
|
"github.com/lightningnetwork/lnd/channeldb"
|
||||||
"github.com/lightningnetwork/lnd/keychain"
|
"github.com/lightningnetwork/lnd/keychain"
|
||||||
"github.com/lightningnetwork/lnd/lnwire"
|
"github.com/lightningnetwork/lnd/lnwire"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/btcec"
|
|
||||||
"github.com/btcsuite/btcd/txscript"
|
|
||||||
"github.com/btcsuite/btcd/wire"
|
|
||||||
"github.com/btcsuite/btcutil"
|
|
||||||
"github.com/btcsuite/btcutil/txsort"
|
|
||||||
"github.com/lightningnetwork/lnd/shachain"
|
"github.com/lightningnetwork/lnd/shachain"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -412,29 +412,18 @@ out:
|
|||||||
// transaction, and that the signature we record for our version of the
|
// transaction, and that the signature we record for our version of the
|
||||||
// commitment transaction is valid.
|
// commitment transaction is valid.
|
||||||
func (l *LightningWallet) InitChannelReservation(
|
func (l *LightningWallet) InitChannelReservation(
|
||||||
capacity, ourFundAmt btcutil.Amount, pushMSat lnwire.MilliSatoshi,
|
req *InitFundingReserveMsg) (*ChannelReservation, error) {
|
||||||
commitFeePerKw SatPerKWeight, fundingFeePerKw SatPerKWeight,
|
|
||||||
theirID *btcec.PublicKey, theirAddr net.Addr,
|
|
||||||
chainHash *chainhash.Hash, flags lnwire.FundingFlag) (*ChannelReservation, error) {
|
|
||||||
|
|
||||||
errChan := make(chan error, 1)
|
req.resp = make(chan *ChannelReservation, 1)
|
||||||
respChan := make(chan *ChannelReservation, 1)
|
req.err = make(chan error, 1)
|
||||||
|
|
||||||
l.msgChan <- &initFundingReserveMsg{
|
select {
|
||||||
chainHash: chainHash,
|
case l.msgChan <- req:
|
||||||
nodeID: theirID,
|
case <-l.quit:
|
||||||
nodeAddr: theirAddr,
|
return nil, errors.New("wallet shutting down")
|
||||||
fundingAmount: ourFundAmt,
|
|
||||||
capacity: capacity,
|
|
||||||
commitFeePerKw: commitFeePerKw,
|
|
||||||
fundingFeePerKw: fundingFeePerKw,
|
|
||||||
pushMSat: pushMSat,
|
|
||||||
flags: flags,
|
|
||||||
err: errChan,
|
|
||||||
resp: respChan,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return <-respChan, <-errChan
|
return <-req.resp, <-req.err
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleFundingReserveRequest processes a message intending to create, and
|
// handleFundingReserveRequest processes a message intending to create, and
|
||||||
|
|||||||
Reference in New Issue
Block a user