lnwallet: update channel reservation flow to use milli-satoshis

This commit is contained in:
Olaoluwa Osuntokun 2017-08-21 23:20:57 -07:00
parent 6e54aba7ac
commit ad00266451
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
2 changed files with 23 additions and 18 deletions

@ -21,6 +21,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwallet/btcwallet" "github.com/lightningnetwork/lnd/lnwallet/btcwallet"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/roasbeef/btcd/chaincfg" "github.com/roasbeef/btcd/chaincfg"
"github.com/roasbeef/btcd/chaincfg/chainhash" "github.com/roasbeef/btcd/chaincfg/chainhash"
_ "github.com/roasbeef/btcwallet/walletdb/bdb" _ "github.com/roasbeef/btcwallet/walletdb/bdb"
@ -216,7 +217,7 @@ func createTestWallet(tempTestDir string, miningNode *rpctest.Harness,
FeeEstimator: lnwallet.StaticFeeEstimator{FeeRate: 250}, FeeEstimator: lnwallet.StaticFeeEstimator{FeeRate: 250},
DefaultConstraints: channeldb.ChannelConstraints{ DefaultConstraints: channeldb.ChannelConstraints{
DustLimit: 500, DustLimit: 500,
MaxPendingAmount: btcutil.Amount(btcutil.SatoshiPerBitcoin) * 100, MaxPendingAmount: lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin) * 100,
ChanReserve: 100, ChanReserve: 100,
MinHTLC: 400, MinHTLC: 400,
MaxAcceptedHtlcs: 900, MaxAcceptedHtlcs: 900,
@ -541,7 +542,7 @@ func testSingleFunderReservationWorkflow(miner *rpctest.Harness,
// funded solely by us. We'll also initially push 1 BTC of the channel // funded solely by us. We'll also initially push 1 BTC of the channel
// towards Bob's side. // towards Bob's side.
fundingAmt := btcutil.Amount(4 * 1e8) fundingAmt := btcutil.Amount(4 * 1e8)
pushAmt := btcutil.Amount(btcutil.SatoshiPerBitcoin) pushAmt := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
feePerWeight := btcutil.Amount(alice.Cfg.FeeEstimator.EstimateFeePerWeight(1)) feePerWeight := btcutil.Amount(alice.Cfg.FeeEstimator.EstimateFeePerWeight(1))
feePerKw := feePerWeight * 1000 feePerKw := feePerWeight * 1000
aliceChanReservation, err := alice.InitChannelReservation(fundingAmt, aliceChanReservation, err := alice.InitChannelReservation(fundingAmt,

@ -5,6 +5,7 @@ import (
"sync" "sync"
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/roasbeef/btcd/btcec" "github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcd/chaincfg/chainhash" "github.com/roasbeef/btcd/chaincfg/chainhash"
"github.com/roasbeef/btcd/wire" "github.com/roasbeef/btcd/wire"
@ -114,10 +115,10 @@ type ChannelReservation struct {
// throughout its lifetime. // throughout its lifetime.
reservationID uint64 reservationID uint64
// pushSat the amount of satoshis that should be pushed to the // pushMSat the amount of milli-satoshis that should be pushed to the
// responder of a single funding channel as part of the initial // responder of a single funding channel as part of the initial
// commitment state. // commitment state.
pushSat btcutil.Amount pushMSat lnwire.MilliSatoshi
// chanOpen houses a struct containing the channel and additional // chanOpen houses a struct containing the channel and additional
// confirmation details will be sent on once the channel is considered // confirmation details will be sent on once the channel is considered
@ -134,23 +135,27 @@ type ChannelReservation struct {
// 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, feePerKw btcutil.Amount, func NewChannelReservation(capacity, fundingAmt, feePerKw btcutil.Amount,
wallet *LightningWallet, id uint64, pushSat btcutil.Amount, wallet *LightningWallet, id uint64, pushMSat lnwire.MilliSatoshi,
chainHash *chainhash.Hash) *ChannelReservation { chainHash *chainhash.Hash) *ChannelReservation {
var ( var (
ourBalance btcutil.Amount ourBalance lnwire.MilliSatoshi
theirBalance btcutil.Amount theirBalance lnwire.MilliSatoshi
initiator bool initiator bool
) )
commitFee := btcutil.Amount((int64(feePerKw) * commitWeight) / 1000) commitFee := btcutil.Amount((int64(feePerKw) * commitWeight) / 1000)
fundingMSat := lnwire.NewMSatFromSatoshis(fundingAmt)
capacityMSat := lnwire.NewMSatFromSatoshis(capacity)
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 fundingAmt == 0 {
ourBalance = pushSat ourBalance = pushMSat
theirBalance = capacity - commitFee - pushSat theirBalance = capacityMSat - feeMSat - pushMSat
initiator = false initiator = false
} else { } else {
// TODO(roasbeef): need to rework fee structure in general and // TODO(roasbeef): need to rework fee structure in general and
@ -161,15 +166,14 @@ func NewChannelReservation(capacity, fundingAmt, feePerKw btcutil.Amount,
// 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
// amount pushed as part of the initial state. // amount pushed as part of the initial state.
ourBalance = capacity - commitFee - pushSat ourBalance = capacityMSat - feeMSat - pushMSat
theirBalance = pushSat theirBalance = pushMSat
} else { } else {
// 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 = fundingAmt - (commitFee / 2) ourBalance = fundingMSat - (feeMSat / 2)
theirBalance = capacity - fundingAmt - theirBalance = capacityMSat - fundingMSat - (feeMSat / 2) + pushMSat
(commitFee / 2) + pushSat
} }
initiator = true initiator = true
@ -182,7 +186,7 @@ func NewChannelReservation(capacity, fundingAmt, feePerKw btcutil.Amount,
// If either of the balances are zero at this point, or we have a // If either of the balances are zero at this point, or we have a
// non-zero push amt (there's no pushing for dual funder), then this is // non-zero push amt (there's no pushing for dual funder), then this is
// a single-funder channel. // a single-funder channel.
if ourBalance == 0 || theirBalance == 0 || pushSat != 0 { if ourBalance == 0 || theirBalance == 0 || pushMSat != 0 {
chanType = channeldb.SingleFunder chanType = channeldb.SingleFunder
} else { } else {
// Otherwise, this is a dual funder channel, and no side is // Otherwise, this is a dual funder channel, and no side is
@ -193,11 +197,11 @@ func NewChannelReservation(capacity, fundingAmt, feePerKw btcutil.Amount,
return &ChannelReservation{ return &ChannelReservation{
ourContribution: &ChannelContribution{ ourContribution: &ChannelContribution{
FundingAmount: ourBalance, FundingAmount: ourBalance.ToSatoshis(),
ChannelConfig: &channeldb.ChannelConfig{}, ChannelConfig: &channeldb.ChannelConfig{},
}, },
theirContribution: &ChannelContribution{ theirContribution: &ChannelContribution{
FundingAmount: theirBalance, FundingAmount: theirBalance.ToSatoshis(),
ChannelConfig: &channeldb.ChannelConfig{}, ChannelConfig: &channeldb.ChannelConfig{},
}, },
partialState: &channeldb.OpenChannel{ partialState: &channeldb.OpenChannel{
@ -212,7 +216,7 @@ func NewChannelReservation(capacity, fundingAmt, feePerKw btcutil.Amount,
FeePerKw: feePerKw, FeePerKw: feePerKw,
CommitFee: commitFee, CommitFee: commitFee,
}, },
pushSat: pushSat, pushMSat: pushMSat,
reservationID: id, reservationID: id,
chanOpen: make(chan *openChanDetails, 1), chanOpen: make(chan *openChanDetails, 1),
chanOpenErr: make(chan error, 1), chanOpenErr: make(chan error, 1),