lnwallet+funding: rename Capacity and FundingAmt

Instead use LocalFundingAmt and RemoteFundingAmt to make it clear who is
contributing funds.
This commit is contained in:
Johan T. Halseth 2019-07-11 13:14:36 +02:00
parent e716251805
commit fcf74debe6
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26
4 changed files with 113 additions and 107 deletions

@ -1084,16 +1084,16 @@ func (f *fundingManager) handleFundingOpen(fmsg *fundingOpenMsg) {
// funds to the channel ourselves. // funds to the channel ourselves.
chainHash := chainhash.Hash(msg.ChainHash) chainHash := chainhash.Hash(msg.ChainHash)
req := &lnwallet.InitFundingReserveMsg{ req := &lnwallet.InitFundingReserveMsg{
ChainHash: &chainHash, ChainHash: &chainHash,
NodeID: fmsg.peer.IdentityKey(), NodeID: fmsg.peer.IdentityKey(),
NodeAddr: fmsg.peer.Address(), NodeAddr: fmsg.peer.Address(),
FundingAmount: 0, LocalFundingAmt: 0,
Capacity: amt, RemoteFundingAmt: amt,
CommitFeePerKw: lnwallet.SatPerKWeight(msg.FeePerKiloWeight), CommitFeePerKw: lnwallet.SatPerKWeight(msg.FeePerKiloWeight),
FundingFeePerKw: 0, FundingFeePerKw: 0,
PushMSat: msg.PushAmount, PushMSat: msg.PushAmount,
Flags: msg.ChannelFlags, Flags: msg.ChannelFlags,
MinConfs: 1, MinConfs: 1,
} }
reservation, err := f.cfg.Wallet.InitChannelReservation(req) reservation, err := f.cfg.Wallet.InitChannelReservation(req)
@ -2783,16 +2783,16 @@ func (f *fundingManager) handleInitFundingMsg(msg *initFundingMsg) {
// 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.
req := &lnwallet.InitFundingReserveMsg{ req := &lnwallet.InitFundingReserveMsg{
ChainHash: &msg.chainHash, ChainHash: &msg.chainHash,
NodeID: peerKey, NodeID: peerKey,
NodeAddr: msg.peer.Address(), NodeAddr: msg.peer.Address(),
FundingAmount: localAmt, LocalFundingAmt: localAmt,
Capacity: capacity, RemoteFundingAmt: 0,
CommitFeePerKw: commitFeePerKw, CommitFeePerKw: commitFeePerKw,
FundingFeePerKw: msg.fundingFeePerKw, FundingFeePerKw: msg.fundingFeePerKw,
PushMSat: msg.pushAmt, PushMSat: msg.pushAmt,
Flags: channelFlags, Flags: channelFlags,
MinConfs: msg.minConfs, MinConfs: msg.minConfs,
} }
reservation, err := f.cfg.Wallet.InitChannelReservation(req) reservation, err := f.cfg.Wallet.InitChannelReservation(req)

@ -415,15 +415,15 @@ func testDualFundingReservationWorkflow(miner *rpctest.Harness,
t.Fatalf("unable to query fee estimator: %v", err) t.Fatalf("unable to query fee estimator: %v", err)
} }
aliceReq := &lnwallet.InitFundingReserveMsg{ aliceReq := &lnwallet.InitFundingReserveMsg{
ChainHash: chainHash, ChainHash: chainHash,
NodeID: bobPub, NodeID: bobPub,
NodeAddr: bobAddr, NodeAddr: bobAddr,
FundingAmount: fundingAmount, LocalFundingAmt: fundingAmount,
Capacity: fundingAmount * 2, RemoteFundingAmt: fundingAmount,
CommitFeePerKw: feePerKw, CommitFeePerKw: feePerKw,
FundingFeePerKw: feePerKw, FundingFeePerKw: feePerKw,
PushMSat: 0, PushMSat: 0,
Flags: lnwire.FFAnnounceChannel, Flags: lnwire.FFAnnounceChannel,
} }
aliceChanReservation, err := alice.InitChannelReservation(aliceReq) aliceChanReservation, err := alice.InitChannelReservation(aliceReq)
if err != nil { if err != nil {
@ -458,15 +458,15 @@ func testDualFundingReservationWorkflow(miner *rpctest.Harness,
// 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.
bobReq := &lnwallet.InitFundingReserveMsg{ bobReq := &lnwallet.InitFundingReserveMsg{
ChainHash: chainHash, ChainHash: chainHash,
NodeID: alicePub, NodeID: alicePub,
NodeAddr: aliceAddr, NodeAddr: aliceAddr,
FundingAmount: fundingAmount, LocalFundingAmt: fundingAmount,
Capacity: fundingAmount * 2, RemoteFundingAmt: fundingAmount,
CommitFeePerKw: feePerKw, CommitFeePerKw: feePerKw,
FundingFeePerKw: feePerKw, FundingFeePerKw: feePerKw,
PushMSat: 0, PushMSat: 0,
Flags: lnwire.FFAnnounceChannel, Flags: lnwire.FFAnnounceChannel,
} }
bobChanReservation, err := bob.InitChannelReservation(bobReq) bobChanReservation, err := bob.InitChannelReservation(bobReq)
if err != nil { if err != nil {
@ -618,15 +618,15 @@ func testFundingTransactionLockedOutputs(miner *rpctest.Harness,
t.Fatalf("unable to query fee estimator: %v", err) t.Fatalf("unable to query fee estimator: %v", err)
} }
req := &lnwallet.InitFundingReserveMsg{ req := &lnwallet.InitFundingReserveMsg{
ChainHash: chainHash, ChainHash: chainHash,
NodeID: bobPub, NodeID: bobPub,
NodeAddr: bobAddr, NodeAddr: bobAddr,
FundingAmount: fundingAmount, LocalFundingAmt: fundingAmount,
Capacity: fundingAmount, RemoteFundingAmt: 0,
CommitFeePerKw: feePerKw, CommitFeePerKw: feePerKw,
FundingFeePerKw: feePerKw, FundingFeePerKw: feePerKw,
PushMSat: 0, PushMSat: 0,
Flags: lnwire.FFAnnounceChannel, Flags: lnwire.FFAnnounceChannel,
} }
if _, err := alice.InitChannelReservation(req); err != nil { 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)
@ -640,15 +640,15 @@ func testFundingTransactionLockedOutputs(miner *rpctest.Harness,
t.Fatalf("unable to create amt: %v", err) t.Fatalf("unable to create amt: %v", err)
} }
failedReq := &lnwallet.InitFundingReserveMsg{ failedReq := &lnwallet.InitFundingReserveMsg{
ChainHash: chainHash, ChainHash: chainHash,
NodeID: bobPub, NodeID: bobPub,
NodeAddr: bobAddr, NodeAddr: bobAddr,
FundingAmount: amt, LocalFundingAmt: amt,
Capacity: amt, RemoteFundingAmt: 0,
CommitFeePerKw: feePerKw, CommitFeePerKw: feePerKw,
FundingFeePerKw: feePerKw, FundingFeePerKw: feePerKw,
PushMSat: 0, PushMSat: 0,
Flags: lnwire.FFAnnounceChannel, Flags: lnwire.FFAnnounceChannel,
} }
failedReservation, err := alice.InitChannelReservation(failedReq) failedReservation, err := alice.InitChannelReservation(failedReq)
if err == nil { if err == nil {
@ -676,15 +676,15 @@ func testFundingCancellationNotEnoughFunds(miner *rpctest.Harness,
t.Fatalf("unable to create amt: %v", err) t.Fatalf("unable to create amt: %v", err)
} }
req := &lnwallet.InitFundingReserveMsg{ req := &lnwallet.InitFundingReserveMsg{
ChainHash: chainHash, ChainHash: chainHash,
NodeID: bobPub, NodeID: bobPub,
NodeAddr: bobAddr, NodeAddr: bobAddr,
FundingAmount: fundingAmount, LocalFundingAmt: fundingAmount,
Capacity: fundingAmount, RemoteFundingAmt: 0,
CommitFeePerKw: feePerKw, CommitFeePerKw: feePerKw,
FundingFeePerKw: feePerKw, FundingFeePerKw: feePerKw,
PushMSat: 0, PushMSat: 0,
Flags: lnwire.FFAnnounceChannel, Flags: lnwire.FFAnnounceChannel,
} }
chanReservation, err := alice.InitChannelReservation(req) chanReservation, err := alice.InitChannelReservation(req)
if err != nil { if err != nil {
@ -766,15 +766,15 @@ func testReservationInitiatorBalanceBelowDustCancel(miner *rpctest.Harness,
numBTC * numBTC * btcutil.SatoshiPerBitcoin, numBTC * numBTC * btcutil.SatoshiPerBitcoin,
) )
req := &lnwallet.InitFundingReserveMsg{ req := &lnwallet.InitFundingReserveMsg{
ChainHash: chainHash, ChainHash: chainHash,
NodeID: bobPub, NodeID: bobPub,
NodeAddr: bobAddr, NodeAddr: bobAddr,
FundingAmount: fundingAmount, LocalFundingAmt: fundingAmount,
Capacity: fundingAmount, RemoteFundingAmt: 0,
CommitFeePerKw: feePerKw, CommitFeePerKw: feePerKw,
FundingFeePerKw: feePerKw, FundingFeePerKw: feePerKw,
PushMSat: 0, PushMSat: 0,
Flags: lnwire.FFAnnounceChannel, Flags: lnwire.FFAnnounceChannel,
} }
_, err = alice.InitChannelReservation(req) _, err = alice.InitChannelReservation(req)
switch { switch {
@ -847,15 +847,15 @@ func testSingleFunderReservationWorkflow(miner *rpctest.Harness,
t.Fatalf("unable to query fee estimator: %v", err) t.Fatalf("unable to query fee estimator: %v", err)
} }
aliceReq := &lnwallet.InitFundingReserveMsg{ aliceReq := &lnwallet.InitFundingReserveMsg{
ChainHash: chainHash, ChainHash: chainHash,
NodeID: bobPub, NodeID: bobPub,
NodeAddr: bobAddr, NodeAddr: bobAddr,
FundingAmount: fundingAmt, LocalFundingAmt: fundingAmt,
Capacity: fundingAmt, RemoteFundingAmt: 0,
CommitFeePerKw: feePerKw, CommitFeePerKw: feePerKw,
FundingFeePerKw: feePerKw, FundingFeePerKw: feePerKw,
PushMSat: pushAmt, PushMSat: pushAmt,
Flags: lnwire.FFAnnounceChannel, Flags: lnwire.FFAnnounceChannel,
} }
aliceChanReservation, err := alice.InitChannelReservation(aliceReq) aliceChanReservation, err := alice.InitChannelReservation(aliceReq)
if err != nil { if err != nil {
@ -890,15 +890,15 @@ 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.
bobReq := &lnwallet.InitFundingReserveMsg{ bobReq := &lnwallet.InitFundingReserveMsg{
ChainHash: chainHash, ChainHash: chainHash,
NodeID: alicePub, NodeID: alicePub,
NodeAddr: aliceAddr, NodeAddr: aliceAddr,
FundingAmount: 0, LocalFundingAmt: 0,
Capacity: fundingAmt, RemoteFundingAmt: fundingAmt,
CommitFeePerKw: feePerKw, CommitFeePerKw: feePerKw,
FundingFeePerKw: feePerKw, FundingFeePerKw: feePerKw,
PushMSat: pushAmt, PushMSat: pushAmt,
Flags: lnwire.FFAnnounceChannel, Flags: lnwire.FFAnnounceChannel,
} }
bobChanReservation, err := bob.InitChannelReservation(bobReq) bobChanReservation, err := bob.InitChannelReservation(bobReq)
if err != nil { if err != nil {

@ -127,7 +127,7 @@ type ChannelReservation struct {
// used only internally by lnwallet. In order to concurrent safety, the // used only internally by lnwallet. In order to concurrent safety, the
// creation of all channel reservations should be carried out via the // creation of all channel reservations should be carried out via the
// lnwallet.InitChannelReservation interface. // lnwallet.InitChannelReservation interface.
func NewChannelReservation(capacity, fundingAmt btcutil.Amount, func NewChannelReservation(capacity, localFundingAmt btcutil.Amount,
commitFeePerKw SatPerKWeight, wallet *LightningWallet, commitFeePerKw SatPerKWeight, wallet *LightningWallet,
id uint64, pushMSat lnwire.MilliSatoshi, chainHash *chainhash.Hash, id uint64, pushMSat lnwire.MilliSatoshi, chainHash *chainhash.Hash,
flags lnwire.FundingFlag) (*ChannelReservation, error) { flags lnwire.FundingFlag) (*ChannelReservation, error) {
@ -139,14 +139,16 @@ func NewChannelReservation(capacity, fundingAmt btcutil.Amount,
) )
commitFee := commitFeePerKw.FeeForWeight(input.CommitWeight) commitFee := commitFeePerKw.FeeForWeight(input.CommitWeight)
fundingMSat := lnwire.NewMSatFromSatoshis(fundingAmt) localFundingMSat := lnwire.NewMSatFromSatoshis(localFundingAmt)
// TODO(halseth): make method take remote funding amount direcly
// instead of inferring it from capacity and local amt.
capacityMSat := lnwire.NewMSatFromSatoshis(capacity) capacityMSat := lnwire.NewMSatFromSatoshis(capacity)
feeMSat := lnwire.NewMSatFromSatoshis(commitFee) feeMSat := lnwire.NewMSatFromSatoshis(commitFee)
// If we're the responder to a single-funder reservation, then we have // If we're the responder to a single-funder reservation, then we have
// no initial balance in the channel unless the remote party is pushing // no initial balance in the channel unless the remote party is pushing
// some funds to us within the first commitment state. // some funds to us within the first commitment state.
if fundingAmt == 0 { if localFundingAmt == 0 {
ourBalance = pushMSat ourBalance = pushMSat
theirBalance = capacityMSat - feeMSat - pushMSat theirBalance = capacityMSat - feeMSat - pushMSat
initiator = false initiator = false
@ -163,7 +165,7 @@ func NewChannelReservation(capacity, fundingAmt btcutil.Amount,
// TODO(roasbeef): need to rework fee structure in general and // TODO(roasbeef): need to rework fee structure in general and
// also when we "unlock" dual funder within the daemon // also when we "unlock" dual funder within the daemon
if capacity == fundingAmt { if capacity == localFundingAmt {
// If we're initiating a single funder workflow, then // If we're initiating a single funder workflow, then
// we pay all the initial fees within the commitment // we pay all the initial fees within the commitment
// transaction. We also deduct our balance by the // transaction. We also deduct our balance by the
@ -174,8 +176,8 @@ func NewChannelReservation(capacity, fundingAmt btcutil.Amount,
// Otherwise, this is a dual funder workflow where both // Otherwise, this is a dual funder workflow where both
// slides split the amount funded and the commitment // slides split the amount funded and the commitment
// fee. // fee.
ourBalance = fundingMSat - (feeMSat / 2) ourBalance = localFundingMSat - (feeMSat / 2)
theirBalance = capacityMSat - fundingMSat - (feeMSat / 2) + pushMSat theirBalance = capacityMSat - localFundingMSat - (feeMSat / 2) + pushMSat
} }
initiator = true initiator = true

@ -69,12 +69,13 @@ type InitFundingReserveMsg struct {
// workflow. // workflow.
NodeAddr net.Addr NodeAddr net.Addr
// FundingAmount is the amount of funds requested for this channel. // LocalFundingAmt is the amount of funds requested from us for this
FundingAmount btcutil.Amount // channel.
LocalFundingAmt btcutil.Amount
// Capacity is the total capacity of the channel which includes the // RemoteFundingAmnt is the amount of funds the remote will contribute
// amount of funds the remote party contributes (if any). // to this channel.
Capacity btcutil.Amount RemoteFundingAmt btcutil.Amount
// CommitFeePerKw is the starting accepted satoshis/Kw fee for the set // CommitFeePerKw is the starting accepted satoshis/Kw fee for the set
// of initial commitment transactions. In order to ensure timely // of initial commitment transactions. In order to ensure timely
@ -431,7 +432,7 @@ func (l *LightningWallet) InitChannelReservation(
// validate a funding reservation request. // validate a funding reservation request.
func (l *LightningWallet) handleFundingReserveRequest(req *InitFundingReserveMsg) { func (l *LightningWallet) handleFundingReserveRequest(req *InitFundingReserveMsg) {
// It isn't possible to create a channel with zero funds committed. // It isn't possible to create a channel with zero funds committed.
if req.FundingAmount+req.Capacity == 0 { if req.LocalFundingAmt+req.RemoteFundingAmt == 0 {
err := ErrZeroCapacity() err := ErrZeroCapacity()
req.err <- err req.err <- err
req.resp <- nil req.resp <- nil
@ -449,9 +450,12 @@ func (l *LightningWallet) handleFundingReserveRequest(req *InitFundingReserveMsg
return return
} }
capacity := req.LocalFundingAmt + req.RemoteFundingAmt
localFundingAmt := req.LocalFundingAmt
id := atomic.AddUint64(&l.nextFundingID, 1) id := atomic.AddUint64(&l.nextFundingID, 1)
reservation, err := NewChannelReservation( reservation, err := NewChannelReservation(
req.Capacity, req.FundingAmount, req.CommitFeePerKw, l, id, capacity, localFundingAmt, req.CommitFeePerKw, l, id,
req.PushMSat, l.Cfg.NetParams.GenesisHash, req.Flags, req.PushMSat, l.Cfg.NetParams.GenesisHash, req.Flags,
) )
if err != nil { if err != nil {
@ -470,11 +474,11 @@ func (l *LightningWallet) handleFundingReserveRequest(req *InitFundingReserveMsg
// If we're on the receiving end of a single funder channel then we // If we're on the receiving end of a single funder channel then we
// don't need to perform any coin selection. Otherwise, attempt to // don't need to perform any coin selection. Otherwise, attempt to
// obtain enough coins to meet the required funding amount. // obtain enough coins to meet the required funding amount.
if req.FundingAmount != 0 { if req.LocalFundingAmt != 0 {
// Coin selection is done on the basis of sat/kw, so we'll use // Coin selection is done on the basis of sat/kw, so we'll use
// the fee rate passed in to perform coin selection. // the fee rate passed in to perform coin selection.
err := l.selectCoinsAndChange( err := l.selectCoinsAndChange(
req.FundingFeePerKw, req.FundingAmount, req.MinConfs, req.FundingFeePerKw, req.LocalFundingAmt, req.MinConfs,
reservation.ourContribution, reservation.ourContribution,
) )
if err != nil { if err != nil {