lnwallet: precalculate fees in mSAT to avoid multiple conversions

This commit is contained in:
Olaoluwa Osuntokun 2018-02-24 19:19:24 -08:00
parent 217166fb10
commit ac90a8288e
No known key found for this signature in database
GPG Key ID: 964EA263DD637C21

@ -2139,6 +2139,7 @@ func (lc *LightningChannel) createCommitmentTx(c *commitment,
// With the weight known, we can now calculate the commitment fee, // With the weight known, we can now calculate the commitment fee,
// ensuring that we account for any dust outputs trimmed above. // ensuring that we account for any dust outputs trimmed above.
commitFee := c.feePerKw.FeeForWeight(totalCommitWeight) commitFee := c.feePerKw.FeeForWeight(totalCommitWeight)
commitFeeMSat := lnwire.NewMSatFromSatoshis(commitFee)
// Currently, within the protocol, the initiator always pays the fees. // Currently, within the protocol, the initiator always pays the fees.
// So we'll subtract the fee amount from the balance of the current // So we'll subtract the fee amount from the balance of the current
@ -2149,13 +2150,13 @@ func (lc *LightningChannel) createCommitmentTx(c *commitment,
ourBalance = 0 ourBalance = 0
case lc.channelState.IsInitiator: case lc.channelState.IsInitiator:
ourBalance -= lnwire.NewMSatFromSatoshis(commitFee) ourBalance -= commitFeeMSat
case !lc.channelState.IsInitiator && commitFee > theirBalance.ToSatoshis(): case !lc.channelState.IsInitiator && commitFee > theirBalance.ToSatoshis():
theirBalance = 0 theirBalance = 0
case !lc.channelState.IsInitiator: case !lc.channelState.IsInitiator:
theirBalance -= lnwire.NewMSatFromSatoshis(commitFee) theirBalance -= commitFeeMSat
} }
var ( var (
@ -3227,57 +3228,58 @@ func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter,
validateUpdates := func(updates []*PaymentDescriptor, validateUpdates := func(updates []*PaymentDescriptor,
constraints *channeldb.ChannelConfig) error { constraints *channeldb.ChannelConfig) error {
// We keep track of the number of HTLCs in flight for // We keep track of the number of HTLCs in flight for the
// the commitment, and the amount in flight. // commitment, and the amount in flight.
var numInFlight uint16 var numInFlight uint16
var amtInFlight lnwire.MilliSatoshi var amtInFlight lnwire.MilliSatoshi
// Go through all updates, checking that they don't // Go through all updates, checking that they don't violate the
// violate the channel constraints. // channel constraints.
for _, entry := range updates { for _, entry := range updates {
if entry.EntryType == Add { if entry.EntryType == Add {
// An HTLC is being added, this will // An HTLC is being added, this will add to the
// add to the number and amount in // number and amount in flight.
// flight.
amtInFlight += entry.Amount amtInFlight += entry.Amount
numInFlight++ numInFlight++
// Check that the value of the HTLC they // Check that the value of the HTLC they added
// added is above our minimum. // is above our minimum.
if entry.Amount < constraints.MinHTLC { if entry.Amount < constraints.MinHTLC {
return ErrBelowMinHTLC return ErrBelowMinHTLC
} }
} }
} }
// Now that we know the total value of added HTLCs, // Now that we know the total value of added HTLCs, we check
// we check that this satisfy the MaxPendingAmont // that this satisfy the MaxPendingAmont contraint.
// contraint.
if amtInFlight > constraints.MaxPendingAmount { if amtInFlight > constraints.MaxPendingAmount {
return ErrMaxPendingAmount return ErrMaxPendingAmount
} }
// In this step, we verify that the total number of // In this step, we verify that the total number of active
// active HTLCs does not exceed the constraint of the // HTLCs does not exceed the constraint of the maximum number
// maximum number of HTLCs in flight. // of HTLCs in flight.
if numInFlight > constraints.MaxAcceptedHtlcs { if numInFlight > constraints.MaxAcceptedHtlcs {
return ErrMaxHTLCNumber return ErrMaxHTLCNumber
} }
return nil return nil
} }
// First check that the remote updates won't violate it's // First check that the remote updates won't violate it's channel
// channel constraints. // constraints.
err := validateUpdates(filteredView.theirUpdates, err := validateUpdates(
lc.remoteChanCfg) filteredView.theirUpdates, lc.remoteChanCfg,
)
if err != nil { if err != nil {
return err return err
} }
// Secondly check that our updates won't violate our // Secondly check that our updates won't violate our channel
// channel constraints. // constraints.
err = validateUpdates(filteredView.ourUpdates, err = validateUpdates(
lc.localChanCfg) filteredView.ourUpdates, lc.localChanCfg,
)
if err != nil { if err != nil {
return err return err
} }
@ -3836,11 +3838,13 @@ func (lc *LightningChannel) AddHTLC(htlc *lnwire.UpdateAddHTLC) (uint64, error)
OnionBlob: htlc.OnionBlob[:], OnionBlob: htlc.OnionBlob[:],
} }
// Make sure adding this HTLC won't violate any of the constrainst // Make sure adding this HTLC won't violate any of the constraints we
// we must keep on our commitment transaction. // must keep on our commitment transaction.
remoteACKedIndex := lc.localCommitChain.tail().theirMessageIndex remoteACKedIndex := lc.localCommitChain.tail().theirMessageIndex
if err := lc.validateCommitmentSanity(remoteACKedIndex, err := lc.validateCommitmentSanity(
lc.localUpdateLog.logIndex, true, pd); err != nil { remoteACKedIndex, lc.localUpdateLog.logIndex, true, pd,
)
if err != nil {
return 0, err return 0, err
} }