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