lnwallet: convert channel state machine accounting to milli-satoshi

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

@ -149,8 +149,8 @@ type PaymentDescriptor struct {
// expires. // expires.
Timeout uint32 Timeout uint32
// Amount is the HTLC amount in satoshis. // Amount is the HTLC amount in milli-satoshis.
Amount btcutil.Amount Amount lnwire.MilliSatoshi
// Index is the log entry number that his HTLC update has within the // Index is the log entry number that his HTLC update has within the
// log. Depending on if IsIncoming is true, this is either an entry the // log. Depending on if IsIncoming is true, this is either an entry the
@ -260,8 +260,8 @@ type commitment struct {
// within the commitment chain. This balance is computed by properly // within the commitment chain. This balance is computed by properly
// evaluating all the add/remove/settle log entries before the listed // evaluating all the add/remove/settle log entries before the listed
// indexes. // indexes.
ourBalance btcutil.Amount ourBalance lnwire.MilliSatoshi
theirBalance btcutil.Amount theirBalance lnwire.MilliSatoshi
// fee is the amount that will be paid as fees for this commitment // fee is the amount that will be paid as fees for this commitment
// transaction. The fee is recorded here so that it can be added // transaction. The fee is recorded here so that it can be added
@ -360,7 +360,7 @@ func (c *commitment) populateHtlcIndexes(ourCommitTx bool,
// indexes within the commitment view for a particular HTLC. // indexes within the commitment view for a particular HTLC.
populateIndex := func(htlc *PaymentDescriptor, incoming bool) error { populateIndex := func(htlc *PaymentDescriptor, incoming bool) error {
isDust := htlcIsDust(incoming, ourCommitTx, c.feePerKw, isDust := htlcIsDust(incoming, ourCommitTx, c.feePerKw,
htlc.Amount, dustLimit) htlc.Amount.ToSatoshis(), dustLimit)
var err error var err error
switch { switch {
@ -822,7 +822,7 @@ type LightningChannel struct {
// availableLocalBalance represent the amount of available money which // availableLocalBalance represent the amount of available money which
// might be processed by this channel at the specific point of time. // might be processed by this channel at the specific point of time.
availableLocalBalance btcutil.Amount availableLocalBalance lnwire.MilliSatoshi
sync.RWMutex sync.RWMutex
@ -1198,7 +1198,7 @@ func newBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64,
DoubleTweak: commitmentSecret, DoubleTweak: commitmentSecret,
WitnessScript: htlcScript, WitnessScript: htlcScript,
Output: &wire.TxOut{ Output: &wire.TxOut{
Value: int64(htlc.Amt), Value: int64(htlc.Amt.ToSatoshis()),
}, },
HashType: txscript.SigHashAll, HashType: txscript.SigHashAll,
}, },
@ -1228,7 +1228,7 @@ func newBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64,
WitnessScript: localPkScript, WitnessScript: localPkScript,
Output: &wire.TxOut{ Output: &wire.TxOut{
PkScript: localWitnessHash, PkScript: localWitnessHash,
Value: int64(revokedSnapshot.LocalBalance), Value: int64(revokedSnapshot.LocalBalance.ToSatoshis()),
}, },
HashType: txscript.SigHashAll, HashType: txscript.SigHashAll,
}, },
@ -1239,7 +1239,7 @@ func newBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64,
WitnessScript: remotePkScript, WitnessScript: remotePkScript,
Output: &wire.TxOut{ Output: &wire.TxOut{
PkScript: remoteWitnessHash, PkScript: remoteWitnessHash,
Value: int64(revokedSnapshot.RemoteBalance), Value: int64(revokedSnapshot.RemoteBalance.ToSatoshis()),
}, },
HashType: txscript.SigHashAll, HashType: txscript.SigHashAll,
}, },
@ -1350,7 +1350,7 @@ func (lc *LightningChannel) closeObserver(channelCloseNtfn *chainntnfs.SpendEven
ClosingTXID: *commitSpend.SpenderTxHash, ClosingTXID: *commitSpend.SpenderTxHash,
RemotePub: lc.channelState.IdentityPub, RemotePub: lc.channelState.IdentityPub,
Capacity: lc.Capacity, Capacity: lc.Capacity,
SettledBalance: lc.channelState.LocalBalance, SettledBalance: lc.channelState.LocalBalance.ToSatoshis(),
CloseType: channeldb.ForceClose, CloseType: channeldb.ForceClose,
IsPending: true, IsPending: true,
} }
@ -1412,7 +1412,7 @@ func (lc *LightningChannel) closeObserver(channelCloseNtfn *chainntnfs.SpendEven
SingleTweak: SingleTweakBytes(commitPoint, localPayBase), SingleTweak: SingleTweakBytes(commitPoint, localPayBase),
WitnessScript: selfP2WKH, WitnessScript: selfP2WKH,
Output: &wire.TxOut{ Output: &wire.TxOut{
Value: int64(lc.channelState.LocalBalance), Value: int64(lc.channelState.LocalBalance.ToSatoshis()),
PkScript: selfP2WKH, PkScript: selfP2WKH,
}, },
HashType: txscript.SigHashAll, HashType: txscript.SigHashAll,
@ -1599,9 +1599,9 @@ func (lc *LightningChannel) restoreStateLogs() error {
// dust with a special output index in the on-disk state // dust with a special output index in the on-disk state
// snapshot. // snapshot.
isDustLocal := htlcIsDust(htlc.Incoming, true, feeRate, isDustLocal := htlcIsDust(htlc.Incoming, true, feeRate,
htlc.Amt, localChanCfg.DustLimit) htlc.Amt.ToSatoshis(), localChanCfg.DustLimit)
isDustRemote := htlcIsDust(htlc.Incoming, false, feeRate, isDustRemote := htlcIsDust(htlc.Incoming, false, feeRate,
htlc.Amt, remoteChanCfg.DustLimit) htlc.Amt.ToSatoshis(), remoteChanCfg.DustLimit)
if !isDustLocal { if !isDustLocal {
ourP2WSH, ourWitnessScript, err = lc.genHtlcScript( ourP2WSH, ourWitnessScript, err = lc.genHtlcScript(
htlc.Incoming, true, htlc.RefundTimeout, htlc.RHash, htlc.Incoming, true, htlc.RefundTimeout, htlc.RHash,
@ -1723,9 +1723,9 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool,
// re-applied in case fee estimation parameters have changed or the // re-applied in case fee estimation parameters have changed or the
// number of outstanding HTLCs has changed. // number of outstanding HTLCs has changed.
if lc.channelState.IsInitiator { if lc.channelState.IsInitiator {
ourBalance = ourBalance + commitChain.tip().fee ourBalance += lnwire.NewMSatFromSatoshis(commitChain.tip().fee)
} else if !lc.channelState.IsInitiator { } else if !lc.channelState.IsInitiator {
theirBalance = theirBalance + commitChain.tip().fee theirBalance += lnwire.NewMSatFromSatoshis(commitChain.tip().fee)
} }
nextHeight := commitChain.tip().height + 1 nextHeight := commitChain.tip().height + 1
@ -1783,8 +1783,8 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool,
} }
numHTLCs := int64(0) numHTLCs := int64(0)
for _, htlc := range filteredHTLCView.ourUpdates { for _, htlc := range filteredHTLCView.ourUpdates {
if htlcIsDust(false, ourCommitTx, feePerKw, htlc.Amount, if htlcIsDust(false, ourCommitTx, feePerKw,
dustLimit) { htlc.Amount.ToSatoshis(), dustLimit) {
continue continue
} }
@ -1792,8 +1792,8 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool,
numHTLCs++ numHTLCs++
} }
for _, htlc := range filteredHTLCView.theirUpdates { for _, htlc := range filteredHTLCView.theirUpdates {
if htlcIsDust(true, ourCommitTx, feePerKw, htlc.Amount, if htlcIsDust(true, ourCommitTx, feePerKw,
dustLimit) { htlc.Amount.ToSatoshis(), dustLimit) {
continue continue
} }
@ -1815,9 +1815,9 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool,
// 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
// initiator. // initiator.
if lc.channelState.IsInitiator { if lc.channelState.IsInitiator {
ourBalance -= commitFee ourBalance -= lnwire.NewMSatFromSatoshis(commitFee)
} else if !lc.channelState.IsInitiator { } else if !lc.channelState.IsInitiator {
theirBalance -= commitFee theirBalance -= lnwire.NewMSatFromSatoshis(commitFee)
} }
var ( var (
@ -1842,8 +1842,8 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool,
) )
delay = uint32(lc.remoteChanCfg.CsvDelay) delay = uint32(lc.remoteChanCfg.CsvDelay)
delayBalance = theirBalance delayBalance = theirBalance.ToSatoshis()
p2wkhBalance = ourBalance p2wkhBalance = ourBalance.ToSatoshis()
} else { } else {
delayKey = TweakPubKey(lc.localChanCfg.DelayBasePoint, delayKey = TweakPubKey(lc.localChanCfg.DelayBasePoint,
commitPoint) commitPoint)
@ -1855,8 +1855,8 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool,
) )
delay = uint32(lc.localChanCfg.CsvDelay) delay = uint32(lc.localChanCfg.CsvDelay)
delayBalance = ourBalance delayBalance = ourBalance.ToSatoshis()
p2wkhBalance = theirBalance p2wkhBalance = theirBalance.ToSatoshis()
} }
// TODO(roasbeef); create all keys unconditionally within commitment // TODO(roasbeef); create all keys unconditionally within commitment
@ -1878,8 +1878,8 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool,
localKey := TweakPubKey(lc.localChanCfg.PaymentBasePoint, commitPoint) localKey := TweakPubKey(lc.localChanCfg.PaymentBasePoint, commitPoint)
remoteKey := TweakPubKey(lc.remoteChanCfg.PaymentBasePoint, commitPoint) remoteKey := TweakPubKey(lc.remoteChanCfg.PaymentBasePoint, commitPoint)
for _, htlc := range filteredHTLCView.ourUpdates { for _, htlc := range filteredHTLCView.ourUpdates {
if htlcIsDust(false, !remoteChain, feePerKw, htlc.Amount, if htlcIsDust(false, !remoteChain, feePerKw,
dustLimit) { htlc.Amount.ToSatoshis(), dustLimit) {
continue continue
} }
@ -1890,8 +1890,8 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool,
} }
} }
for _, htlc := range filteredHTLCView.theirUpdates { for _, htlc := range filteredHTLCView.theirUpdates {
if htlcIsDust(true, !remoteChain, feePerKw, htlc.Amount, if htlcIsDust(true, !remoteChain, feePerKw,
dustLimit) { htlc.Amount.ToSatoshis(), dustLimit) {
continue continue
} }
@ -1953,7 +1953,7 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool,
// reflects the current state of HTLCs within the remote or local commitment // reflects the current state of HTLCs within the remote or local commitment
// chain. // chain.
func (lc *LightningChannel) evaluateHTLCView(view *htlcView, ourBalance, func (lc *LightningChannel) evaluateHTLCView(view *htlcView, ourBalance,
theirBalance *btcutil.Amount, nextHeight uint64, remoteChain bool) *htlcView { theirBalance *lnwire.MilliSatoshi, nextHeight uint64, remoteChain bool) *htlcView {
newView := &htlcView{} newView := &htlcView{}
@ -1977,7 +1977,7 @@ func (lc *LightningChannel) evaluateHTLCView(view *htlcView, ourBalance,
// number of satoshis we've received within the channel. // number of satoshis we've received within the channel.
if entry.EntryType == Settle && !remoteChain && if entry.EntryType == Settle && !remoteChain &&
entry.removeCommitHeightLocal == 0 { entry.removeCommitHeightLocal == 0 {
lc.channelState.TotalSatoshisReceived += uint64(entry.Amount) lc.channelState.TotalMSatReceived += entry.Amount
} }
addEntry := lc.remoteUpdateLog.lookup(entry.ParentIndex) addEntry := lc.remoteUpdateLog.lookup(entry.ParentIndex)
@ -1997,7 +1997,7 @@ func (lc *LightningChannel) evaluateHTLCView(view *htlcView, ourBalance,
// channel. // channel.
if entry.EntryType == Settle && !remoteChain && if entry.EntryType == Settle && !remoteChain &&
entry.removeCommitHeightLocal == 0 { entry.removeCommitHeightLocal == 0 {
lc.channelState.TotalSatoshisSent += uint64(entry.Amount) lc.channelState.TotalMSatSent += entry.Amount
} }
addEntry := lc.localUpdateLog.lookup(entry.ParentIndex) addEntry := lc.localUpdateLog.lookup(entry.ParentIndex)
@ -2038,7 +2038,7 @@ func (lc *LightningChannel) evaluateHTLCView(view *htlcView, ourBalance,
// If the HTLC hasn't yet been committed in either chain, then the height it // If the HTLC hasn't yet been committed in either chain, then the height it
// was committed is updated. Keeping track of this inclusion height allows us to // was committed is updated. Keeping track of this inclusion height allows us to
// later compact the log once the change is fully committed in both chains. // later compact the log once the change is fully committed in both chains.
func processAddEntry(htlc *PaymentDescriptor, ourBalance, theirBalance *btcutil.Amount, func processAddEntry(htlc *PaymentDescriptor, ourBalance, theirBalance *lnwire.MilliSatoshi,
nextHeight uint64, remoteChain bool, isIncoming bool) { nextHeight uint64, remoteChain bool, isIncoming bool) {
// If we're evaluating this entry for the remote chain (to create/view // If we're evaluating this entry for the remote chain (to create/view
@ -2074,7 +2074,7 @@ func processAddEntry(htlc *PaymentDescriptor, ourBalance, theirBalance *btcutil.
// previously added HTLC. If the removal entry has already been processed, it // previously added HTLC. If the removal entry has already been processed, it
// is skipped. // is skipped.
func processRemoveEntry(htlc *PaymentDescriptor, ourBalance, func processRemoveEntry(htlc *PaymentDescriptor, ourBalance,
theirBalance *btcutil.Amount, nextHeight uint64, theirBalance *lnwire.MilliSatoshi, nextHeight uint64,
remoteChain bool, isIncoming bool) { remoteChain bool, isIncoming bool) {
var removeHeight *uint64 var removeHeight *uint64
@ -2162,7 +2162,8 @@ func genRemoteHtlcSigJobs(commitPoint *btcec.PublicKey,
// dust output after taking into account second-level HTLC fees, then a // dust output after taking into account second-level HTLC fees, then a
// sigJob will be generated and appended to the current batch. // sigJob will be generated and appended to the current batch.
for _, htlc := range remoteCommitView.incomingHTLCs { for _, htlc := range remoteCommitView.incomingHTLCs {
if htlcIsDust(true, false, feePerKw, htlc.Amount, dustLimit) { if htlcIsDust(true, false, feePerKw, htlc.Amount.ToSatoshis(),
dustLimit) {
continue continue
} }
@ -2178,7 +2179,7 @@ func genRemoteHtlcSigJobs(commitPoint *btcec.PublicKey,
// transaction needs to account for fees, so we'll compute the // transaction needs to account for fees, so we'll compute the
// required fee and output now. // required fee and output now.
htlcFee := htlcTimeoutFee(feePerKw) htlcFee := htlcTimeoutFee(feePerKw)
outputAmt := htlc.Amount - htlcFee outputAmt := htlc.Amount.ToSatoshis() - htlcFee
// With the fee calculate, we can properly create the HTLC // With the fee calculate, we can properly create the HTLC
// timeout transaction using the HTLC amount minus the fee. // timeout transaction using the HTLC amount minus the fee.
@ -2201,7 +2202,7 @@ func genRemoteHtlcSigJobs(commitPoint *btcec.PublicKey,
SingleTweak: commitTweak, SingleTweak: commitTweak,
WitnessScript: htlc.theirWitnessScript, WitnessScript: htlc.theirWitnessScript,
Output: &wire.TxOut{ Output: &wire.TxOut{
Value: int64(htlc.Amount), Value: int64(htlc.Amount.ToSatoshis()),
}, },
HashType: txscript.SigHashAll, HashType: txscript.SigHashAll,
SigHashes: txscript.NewTxSigHashes(sigJob.tx), SigHashes: txscript.NewTxSigHashes(sigJob.tx),
@ -2212,7 +2213,8 @@ func genRemoteHtlcSigJobs(commitPoint *btcec.PublicKey,
sigBatch = append(sigBatch, sigJob) sigBatch = append(sigBatch, sigJob)
} }
for _, htlc := range remoteCommitView.outgoingHTLCs { for _, htlc := range remoteCommitView.outgoingHTLCs {
if htlcIsDust(false, false, feePerKw, htlc.Amount, dustLimit) { if htlcIsDust(false, false, feePerKw, htlc.Amount.ToSatoshis(),
dustLimit) {
continue continue
} }
@ -2226,7 +2228,7 @@ func genRemoteHtlcSigJobs(commitPoint *btcec.PublicKey,
// transaction needs to account for fees, so we'll compute the // transaction needs to account for fees, so we'll compute the
// required fee and output now. // required fee and output now.
htlcFee := htlcSuccessFee(feePerKw) htlcFee := htlcSuccessFee(feePerKw)
outputAmt := htlc.Amount - htlcFee outputAmt := htlc.Amount.ToSatoshis() - htlcFee
// With the proper output amount calculated, we can now // With the proper output amount calculated, we can now
// generate the success transaction using the remote party's // generate the success transaction using the remote party's
@ -2250,7 +2252,7 @@ func genRemoteHtlcSigJobs(commitPoint *btcec.PublicKey,
SingleTweak: commitTweak, SingleTweak: commitTweak,
WitnessScript: htlc.theirWitnessScript, WitnessScript: htlc.theirWitnessScript,
Output: &wire.TxOut{ Output: &wire.TxOut{
Value: int64(htlc.Amount), Value: int64(htlc.Amount.ToSatoshis()),
}, },
HashType: txscript.SigHashAll, HashType: txscript.SigHashAll,
SigHashes: txscript.NewTxSigHashes(sigJob.tx), SigHashes: txscript.NewTxSigHashes(sigJob.tx),
@ -2526,7 +2528,7 @@ func genHtlcSigValidationJobs(localCommitmentView *commitment,
} }
htlcFee := htlcSuccessFee(feePerKw) htlcFee := htlcSuccessFee(feePerKw)
outputAmt := htlc.Amount - htlcFee outputAmt := htlc.Amount.ToSatoshis() - htlcFee
successTx, err := createHtlcSuccessTx(op, successTx, err := createHtlcSuccessTx(op,
outputAmt, uint32(localChanCfg.CsvDelay), outputAmt, uint32(localChanCfg.CsvDelay),
@ -2539,7 +2541,8 @@ func genHtlcSigValidationJobs(localCommitmentView *commitment,
sigHash, err := txscript.CalcWitnessSigHash( sigHash, err := txscript.CalcWitnessSigHash(
htlc.ourWitnessScript, hashCache, htlc.ourWitnessScript, hashCache,
txscript.SigHashAll, successTx, 0, txscript.SigHashAll, successTx, 0,
int64(htlc.Amount)) int64(htlc.Amount.ToSatoshis()),
)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -2565,7 +2568,7 @@ func genHtlcSigValidationJobs(localCommitmentView *commitment,
} }
htlcFee := htlcTimeoutFee(feePerKw) htlcFee := htlcTimeoutFee(feePerKw)
outputAmt := htlc.Amount - htlcFee outputAmt := htlc.Amount.ToSatoshis() - htlcFee
timeoutTx, err := createHtlcTimeoutTx(op, timeoutTx, err := createHtlcTimeoutTx(op,
outputAmt, htlc.Timeout, outputAmt, htlc.Timeout,
@ -2580,7 +2583,7 @@ func genHtlcSigValidationJobs(localCommitmentView *commitment,
sigHash, err := txscript.CalcWitnessSigHash( sigHash, err := txscript.CalcWitnessSigHash(
htlc.ourWitnessScript, hashCache, htlc.ourWitnessScript, hashCache,
txscript.SigHashAll, timeoutTx, 0, txscript.SigHashAll, timeoutTx, 0,
int64(htlc.Amount), int64(htlc.Amount.ToSatoshis()),
) )
if err != nil { if err != nil {
return nil, err return nil, err
@ -2838,7 +2841,7 @@ func (lc *LightningChannel) RevokeCurrentCommitment() (*lnwire.RevokeAndAck, err
// LocalAvailableBalance returns the amount of available money which might be // LocalAvailableBalance returns the amount of available money which might be
// proceed by this channel at the specific point of time. // proceed by this channel at the specific point of time.
func (lc *LightningChannel) LocalAvailableBalance() btcutil.Amount { func (lc *LightningChannel) LocalAvailableBalance() lnwire.MilliSatoshi {
lc.Lock() lc.Lock()
defer lc.Unlock() defer lc.Unlock()
@ -3388,7 +3391,7 @@ func newHtlcResolution(signer Signer, localChanCfg *channeldb.ChannelConfig,
// re-calculate the fee required at this state, so we can add the // re-calculate the fee required at this state, so we can add the
// correct output value amount to the transaction. // correct output value amount to the transaction.
htlcFee := htlcTimeoutFee(feePewKw) htlcFee := htlcTimeoutFee(feePewKw)
secondLevelOutputAmt := htlc.Amt - htlcFee secondLevelOutputAmt := htlc.Amt.ToSatoshis() - htlcFee
// With the fee calculated, re-construct the second level timeout // With the fee calculated, re-construct the second level timeout
// transaction. // transaction.
@ -3413,7 +3416,7 @@ func newHtlcResolution(signer Signer, localChanCfg *channeldb.ChannelConfig,
SingleTweak: commitTweak, SingleTweak: commitTweak,
WitnessScript: htlcCreationScript, WitnessScript: htlcCreationScript,
Output: &wire.TxOut{ Output: &wire.TxOut{
Value: int64(htlc.Amt), Value: int64(htlc.Amt.ToSatoshis()),
}, },
HashType: txscript.SigHashAll, HashType: txscript.SigHashAll,
SigHashes: txscript.NewTxSigHashes(timeoutTx), SigHashes: txscript.NewTxSigHashes(timeoutTx),
@ -3488,8 +3491,8 @@ func extractHtlcResolutions(feePerKw btcutil.Amount, ourCommit bool,
// We'll also skip any HTLC's which were dust on the commitment // We'll also skip any HTLC's which were dust on the commitment
// transaction, as these don't have a corresponding output // transaction, as these don't have a corresponding output
// within the commitment transaction. // within the commitment transaction.
if htlcIsDust(htlc.Incoming, ourCommit, feePerKw, htlc.Amt, if htlcIsDust(htlc.Incoming, ourCommit, feePerKw,
dustLimit) { htlc.Amt.ToSatoshis(), dustLimit) {
continue continue
} }
@ -3625,7 +3628,7 @@ func (lc *LightningChannel) ForceClose() (*ForceCloseSummary, error) {
WitnessScript: selfScript, WitnessScript: selfScript,
Output: &wire.TxOut{ Output: &wire.TxOut{
PkScript: delayScript, PkScript: delayScript,
Value: int64(lc.channelState.LocalBalance), Value: int64(lc.channelState.LocalBalance.ToSatoshis()),
}, },
HashType: txscript.SigHashAll, HashType: txscript.SigHashAll,
} }
@ -3686,8 +3689,8 @@ func (lc *LightningChannel) CreateCloseProposal(proposedFee uint64,
// Subtract the proposed fee from the appropriate balance, taking care // Subtract the proposed fee from the appropriate balance, taking care
// not to persist the adjusted balance, as the feeRate may change // not to persist the adjusted balance, as the feeRate may change
// during the channel closing process. // during the channel closing process.
ourBalance := lc.channelState.LocalBalance ourBalance := lc.channelState.LocalBalance.ToSatoshis()
theirBalance := lc.channelState.RemoteBalance theirBalance := lc.channelState.RemoteBalance.ToSatoshis()
if lc.channelState.IsInitiator { if lc.channelState.IsInitiator {
ourBalance = ourBalance - btcutil.Amount(proposedFee) ourBalance = ourBalance - btcutil.Amount(proposedFee)
@ -3747,8 +3750,8 @@ func (lc *LightningChannel) CompleteCooperativeClose(localSig, remoteSig,
// Subtract the proposed fee from the appropriate balance, taking care // Subtract the proposed fee from the appropriate balance, taking care
// not to persist the adjusted balance, as the feeRate may change // not to persist the adjusted balance, as the feeRate may change
// during the channel closing process. // during the channel closing process.
ourBalance := lc.channelState.LocalBalance ourBalance := lc.channelState.LocalBalance.ToSatoshis()
theirBalance := lc.channelState.RemoteBalance theirBalance := lc.channelState.RemoteBalance.ToSatoshis()
if lc.channelState.IsInitiator { if lc.channelState.IsInitiator {
ourBalance = ourBalance - btcutil.Amount(proposedFee) ourBalance = ourBalance - btcutil.Amount(proposedFee)

@ -224,9 +224,9 @@ func createTestChannels(revocationWindow int) (*LightningChannel, *LightningChan
aliceCfg := channeldb.ChannelConfig{ aliceCfg := channeldb.ChannelConfig{
ChannelConstraints: channeldb.ChannelConstraints{ ChannelConstraints: channeldb.ChannelConstraints{
DustLimit: aliceDustLimit, DustLimit: aliceDustLimit,
MaxPendingAmount: btcutil.Amount(rand.Int63()), MaxPendingAmount: lnwire.MilliSatoshi(rand.Int63()),
ChanReserve: btcutil.Amount(rand.Int63()), ChanReserve: btcutil.Amount(rand.Int63()),
MinHTLC: btcutil.Amount(rand.Int63()), MinHTLC: lnwire.MilliSatoshi(rand.Int63()),
MaxAcceptedHtlcs: uint16(rand.Int31()), MaxAcceptedHtlcs: uint16(rand.Int31()),
}, },
CsvDelay: uint16(csvTimeoutAlice), CsvDelay: uint16(csvTimeoutAlice),
@ -238,9 +238,9 @@ func createTestChannels(revocationWindow int) (*LightningChannel, *LightningChan
bobCfg := channeldb.ChannelConfig{ bobCfg := channeldb.ChannelConfig{
ChannelConstraints: channeldb.ChannelConstraints{ ChannelConstraints: channeldb.ChannelConstraints{
DustLimit: bobDustLimit, DustLimit: bobDustLimit,
MaxPendingAmount: btcutil.Amount(rand.Int63()), MaxPendingAmount: lnwire.MilliSatoshi(rand.Int63()),
ChanReserve: btcutil.Amount(rand.Int63()), ChanReserve: btcutil.Amount(rand.Int63()),
MinHTLC: btcutil.Amount(rand.Int63()), MinHTLC: lnwire.MilliSatoshi(rand.Int63()),
MaxAcceptedHtlcs: uint16(rand.Int31()), MaxAcceptedHtlcs: uint16(rand.Int31()),
}, },
CsvDelay: uint16(csvTimeoutBob), CsvDelay: uint16(csvTimeoutBob),
@ -301,8 +301,8 @@ func createTestChannels(revocationWindow int) (*LightningChannel, *LightningChan
FeePerKw: feePerKw, FeePerKw: feePerKw,
IsInitiator: true, IsInitiator: true,
Capacity: channelCapacity, Capacity: channelCapacity,
LocalBalance: channelBal - commitFee, LocalBalance: lnwire.NewMSatFromSatoshis(channelBal - commitFee),
RemoteBalance: channelBal, RemoteBalance: lnwire.NewMSatFromSatoshis(channelBal),
CommitTx: *aliceCommitTx, CommitTx: *aliceCommitTx,
CommitSig: bytes.Repeat([]byte{1}, 71), CommitSig: bytes.Repeat([]byte{1}, 71),
RemoteCurrentRevocation: bobCommitPoint, RemoteCurrentRevocation: bobCommitPoint,
@ -320,8 +320,8 @@ func createTestChannels(revocationWindow int) (*LightningChannel, *LightningChan
ChanType: channeldb.SingleFunder, ChanType: channeldb.SingleFunder,
IsInitiator: false, IsInitiator: false,
Capacity: channelCapacity, Capacity: channelCapacity,
LocalBalance: channelBal, LocalBalance: lnwire.NewMSatFromSatoshis(channelBal),
RemoteBalance: channelBal - commitFee, RemoteBalance: lnwire.NewMSatFromSatoshis(channelBal - commitFee),
CommitTx: *bobCommitTx, CommitTx: *bobCommitTx,
CommitSig: bytes.Repeat([]byte{1}, 71), CommitSig: bytes.Repeat([]byte{1}, 71),
RemoteCurrentRevocation: aliceCommitPoint, RemoteCurrentRevocation: aliceCommitPoint,
@ -378,7 +378,7 @@ func calcStaticFee(numHTLCs int) btcutil.Amount {
// createHTLC is a utility function for generating an HTLC with a given // createHTLC is a utility function for generating an HTLC with a given
// preimage and a given amount. // preimage and a given amount.
func createHTLC(data int, amount btcutil.Amount) (*lnwire.UpdateAddHTLC, [32]byte) { func createHTLC(data int, amount lnwire.MilliSatoshi) (*lnwire.UpdateAddHTLC, [32]byte) {
preimage := bytes.Repeat([]byte{byte(data)}, 32) preimage := bytes.Repeat([]byte{byte(data)}, 32)
paymentHash := sha256.Sum256(preimage) paymentHash := sha256.Sum256(preimage)
@ -417,7 +417,7 @@ func TestSimpleAddSettleWorkflow(t *testing.T) {
paymentHash := sha256.Sum256(paymentPreimage) paymentHash := sha256.Sum256(paymentPreimage)
htlc := &lnwire.UpdateAddHTLC{ htlc := &lnwire.UpdateAddHTLC{
PaymentHash: paymentHash, PaymentHash: paymentHash,
Amount: btcutil.SatoshiPerBitcoin, Amount: lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin),
Expiry: uint32(5), Expiry: uint32(5),
} }
@ -501,24 +501,24 @@ func TestSimpleAddSettleWorkflow(t *testing.T) {
// At this point, both sides should have the proper number of satoshis // At this point, both sides should have the proper number of satoshis
// sent, and commitment height updated within their local channel // sent, and commitment height updated within their local channel
// state. // state.
aliceSent := uint64(0) aliceSent := lnwire.MilliSatoshi(0)
bobSent := uint64(0) bobSent := lnwire.MilliSatoshi(0)
if aliceChannel.channelState.TotalSatoshisSent != aliceSent { if aliceChannel.channelState.TotalMSatSent != aliceSent {
t.Fatalf("alice has incorrect satoshis sent: %v vs %v", t.Fatalf("alice has incorrect milli-satoshis sent: %v vs %v",
aliceChannel.channelState.TotalSatoshisSent, aliceSent) aliceChannel.channelState.TotalMSatSent, aliceSent)
} }
if aliceChannel.channelState.TotalSatoshisReceived != bobSent { if aliceChannel.channelState.TotalMSatReceived != bobSent {
t.Fatalf("alice has incorrect satoshis received %v vs %v", t.Fatalf("alice has incorrect milli-satoshis received %v vs %v",
aliceChannel.channelState.TotalSatoshisReceived, bobSent) aliceChannel.channelState.TotalMSatReceived, bobSent)
} }
if bobChannel.channelState.TotalSatoshisSent != bobSent { if bobChannel.channelState.TotalMSatSent != bobSent {
t.Fatalf("bob has incorrect satoshis sent %v vs %v", t.Fatalf("bob has incorrect milli-satoshis sent %v vs %v",
bobChannel.channelState.TotalSatoshisSent, bobSent) bobChannel.channelState.TotalMSatSent, bobSent)
} }
if bobChannel.channelState.TotalSatoshisReceived != aliceSent { if bobChannel.channelState.TotalMSatReceived != aliceSent {
t.Fatalf("bob has incorrect satoshis received %v vs %v", t.Fatalf("bob has incorrect milli-satoshis received %v vs %v",
bobChannel.channelState.TotalSatoshisReceived, aliceSent) bobChannel.channelState.TotalMSatReceived, aliceSent)
} }
if bobChannel.currentHeight != 1 { if bobChannel.currentHeight != 1 {
t.Fatalf("bob has incorrect commitment height, %v vs %v", t.Fatalf("bob has incorrect commitment height, %v vs %v",
@ -585,27 +585,27 @@ func TestSimpleAddSettleWorkflow(t *testing.T) {
} }
// At this point, Bob should have 6 BTC settled, with Alice still having // At this point, Bob should have 6 BTC settled, with Alice still having
// 4 BTC. Alice's channel should show 1 BTC sent and Bob's channel should // 4 BTC. Alice's channel should show 1 BTC sent and Bob's channel
// show 1 BTC received. They should also be at commitment height two, // should show 1 BTC received. They should also be at commitment height
// with the revocation window extended by by 1 (5). // two, with the revocation window extended by by 1 (5).
satoshisTransferred := uint64(100000000) mSatTransferred := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
if aliceChannel.channelState.TotalSatoshisSent != satoshisTransferred { if aliceChannel.channelState.TotalMSatSent != mSatTransferred {
t.Fatalf("alice satoshis sent incorrect %v vs %v expected", t.Fatalf("alice satoshis sent incorrect %v vs %v expected",
aliceChannel.channelState.TotalSatoshisSent, aliceChannel.channelState.TotalMSatSent,
satoshisTransferred) mSatTransferred)
} }
if aliceChannel.channelState.TotalSatoshisReceived != 0 { if aliceChannel.channelState.TotalMSatReceived != 0 {
t.Fatalf("alice satoshis received incorrect %v vs %v expected", t.Fatalf("alice satoshis received incorrect %v vs %v expected",
aliceChannel.channelState.TotalSatoshisSent, 0) aliceChannel.channelState.TotalMSatReceived, 0)
} }
if bobChannel.channelState.TotalSatoshisReceived != satoshisTransferred { if bobChannel.channelState.TotalMSatReceived != mSatTransferred {
t.Fatalf("bob satoshis received incorrect %v vs %v expected", t.Fatalf("bob satoshis received incorrect %v vs %v expected",
bobChannel.channelState.TotalSatoshisReceived, bobChannel.channelState.TotalMSatReceived,
satoshisTransferred) mSatTransferred)
} }
if bobChannel.channelState.TotalSatoshisSent != 0 { if bobChannel.channelState.TotalMSatSent != 0 {
t.Fatalf("bob satoshis sent incorrect %v vs %v expected", t.Fatalf("bob satoshis sent incorrect %v vs %v expected",
bobChannel.channelState.TotalSatoshisReceived, 0) bobChannel.channelState.TotalMSatSent, 0)
} }
if bobChannel.currentHeight != 2 { if bobChannel.currentHeight != 2 {
t.Fatalf("bob has incorrect commitment height, %v vs %v", t.Fatalf("bob has incorrect commitment height, %v vs %v",
@ -680,7 +680,7 @@ func TestCheckCommitTxSize(t *testing.T) {
// Adding HTLCs and check that size stays in allowable estimation // Adding HTLCs and check that size stays in allowable estimation
// error window. // error window.
for i := 1; i <= 10; i++ { for i := 1; i <= 10; i++ {
htlc, _ := createHTLC(i, btcutil.Amount(1e7)) htlc, _ := createHTLC(i, lnwire.MilliSatoshi(1e7))
if _, err := aliceChannel.AddHTLC(htlc); err != nil { if _, err := aliceChannel.AddHTLC(htlc); err != nil {
t.Fatalf("alice unable to add htlc: %v", err) t.Fatalf("alice unable to add htlc: %v", err)
@ -699,7 +699,7 @@ func TestCheckCommitTxSize(t *testing.T) {
// Settle HTLCs and check that estimation is counting cost of settle // Settle HTLCs and check that estimation is counting cost of settle
// HTLCs properly. // HTLCs properly.
for i := 10; i >= 1; i-- { for i := 10; i >= 1; i-- {
_, preimage := createHTLC(i, btcutil.Amount(1e7)) _, preimage := createHTLC(i, lnwire.MilliSatoshi(1e7))
settleIndex, err := bobChannel.SettleHTLC(preimage) settleIndex, err := bobChannel.SettleHTLC(preimage)
if err != nil { if err != nil {
@ -797,7 +797,7 @@ func TestForceClose(t *testing.T) {
} }
defer cleanUp() defer cleanUp()
htlcAmount := btcutil.Amount(500) htlcAmount := lnwire.NewMSatFromSatoshis(500)
aliceAmount := aliceChannel.channelState.LocalBalance aliceAmount := aliceChannel.channelState.LocalBalance
bobAmount := bobChannel.channelState.LocalBalance bobAmount := bobChannel.channelState.LocalBalance
@ -818,10 +818,12 @@ func TestForceClose(t *testing.T) {
if !closeSummary.SelfOutputSignDesc.PubKey.IsEqual(aliceDelayPoint) { if !closeSummary.SelfOutputSignDesc.PubKey.IsEqual(aliceDelayPoint) {
t.Fatalf("alice incorrect pubkey in SelfOutputSignDesc") t.Fatalf("alice incorrect pubkey in SelfOutputSignDesc")
} }
if closeSummary.SelfOutputSignDesc.Output.Value != int64(aliceAmount) { if closeSummary.SelfOutputSignDesc.Output.Value !=
int64(aliceAmount.ToSatoshis()) {
t.Fatalf("alice incorrect output value in SelfOutputSignDesc, "+ t.Fatalf("alice incorrect output value in SelfOutputSignDesc, "+
"expected %v, got %v", "expected %v, got %v",
aliceChannel.channelState.LocalBalance, aliceChannel.channelState.LocalBalance.ToSatoshis(),
closeSummary.SelfOutputSignDesc.Output.Value) closeSummary.SelfOutputSignDesc.Output.Value)
} }
if closeSummary.SelfOutputMaturity != if closeSummary.SelfOutputMaturity !=
@ -850,10 +852,12 @@ func TestForceClose(t *testing.T) {
if !closeSummary.SelfOutputSignDesc.PubKey.IsEqual(bobDelayPoint) { if !closeSummary.SelfOutputSignDesc.PubKey.IsEqual(bobDelayPoint) {
t.Fatalf("bob incorrect pubkey in SelfOutputSignDesc") t.Fatalf("bob incorrect pubkey in SelfOutputSignDesc")
} }
if closeSummary.SelfOutputSignDesc.Output.Value != int64(bobAmount) { if closeSummary.SelfOutputSignDesc.Output.Value !=
int64(bobAmount.ToSatoshis()) {
t.Fatalf("bob incorrect output value in SelfOutputSignDesc, "+ t.Fatalf("bob incorrect output value in SelfOutputSignDesc, "+
"expected %v, got %v", "expected %v, got %v",
int64(bobAmount), bobAmount.ToSatoshis(),
int64(closeSummary.SelfOutputSignDesc.Output.Value)) int64(closeSummary.SelfOutputSignDesc.Output.Value))
} }
if closeSummary.SelfOutputMaturity != if closeSummary.SelfOutputMaturity !=
@ -922,10 +926,10 @@ func TestForceClose(t *testing.T) {
t.Fatalf("alice incorrect pubkey in SelfOutputSignDesc") t.Fatalf("alice incorrect pubkey in SelfOutputSignDesc")
} }
if closeSummary.SelfOutputSignDesc.Output.Value != if closeSummary.SelfOutputSignDesc.Output.Value !=
int64(aliceAmount) { int64(aliceAmount.ToSatoshis()) {
t.Fatalf("alice incorrect output value in SelfOutputSignDesc, "+ t.Fatalf("alice incorrect output value in SelfOutputSignDesc, "+
"expected %v, got %v", "expected %v, got %v",
aliceChannel.channelState.LocalBalance, aliceChannel.channelState.LocalBalance.ToSatoshis(),
closeSummary.SelfOutputSignDesc.Output.Value) closeSummary.SelfOutputSignDesc.Output.Value)
} }
@ -978,7 +982,7 @@ func TestDustHTLCFees(t *testing.T) {
aliceStartingBalance := aliceChannel.channelState.LocalBalance aliceStartingBalance := aliceChannel.channelState.LocalBalance
// This HTLC amount should be lower than the dust limits of both nodes. // This HTLC amount should be lower than the dust limits of both nodes.
htlcAmount := btcutil.Amount(100) htlcAmount := lnwire.NewMSatFromSatoshis(100)
htlc, _ := createHTLC(0, htlcAmount) htlc, _ := createHTLC(0, htlcAmount)
if _, err := aliceChannel.AddHTLC(htlc); err != nil { if _, err := aliceChannel.AddHTLC(htlc); err != nil {
t.Fatalf("alice unable to add htlc: %v", err) t.Fatalf("alice unable to add htlc: %v", err)
@ -996,16 +1000,16 @@ func TestDustHTLCFees(t *testing.T) {
// sides. // sides.
totalSatoshisAlice := (aliceChannel.channelState.LocalBalance + totalSatoshisAlice := (aliceChannel.channelState.LocalBalance +
aliceChannel.channelState.RemoteBalance + aliceChannel.channelState.RemoteBalance +
aliceChannel.channelState.CommitFee) lnwire.NewMSatFromSatoshis(aliceChannel.channelState.CommitFee))
if totalSatoshisAlice+htlcAmount != aliceChannel.Capacity { if totalSatoshisAlice+htlcAmount != lnwire.NewMSatFromSatoshis(aliceChannel.Capacity) {
t.Fatalf("alice's funds leaked: total satoshis are %v, but channel "+ t.Fatalf("alice's funds leaked: total satoshis are %v, but channel "+
"capacity is %v", int64(totalSatoshisAlice), "capacity is %v", int64(totalSatoshisAlice),
int64(aliceChannel.Capacity)) int64(aliceChannel.Capacity))
} }
totalSatoshisBob := (bobChannel.channelState.LocalBalance + totalSatoshisBob := (bobChannel.channelState.LocalBalance +
bobChannel.channelState.RemoteBalance + bobChannel.channelState.RemoteBalance +
bobChannel.channelState.CommitFee) lnwire.NewMSatFromSatoshis(bobChannel.channelState.CommitFee))
if totalSatoshisBob+htlcAmount != bobChannel.Capacity { if totalSatoshisBob+htlcAmount != lnwire.NewMSatFromSatoshis(bobChannel.Capacity) {
t.Fatalf("bob's funds leaked: total satoshis are %v, but channel "+ t.Fatalf("bob's funds leaked: total satoshis are %v, but channel "+
"capacity is %v", int64(totalSatoshisBob), "capacity is %v", int64(totalSatoshisBob),
int64(bobChannel.Capacity)) int64(bobChannel.Capacity))
@ -1054,8 +1058,9 @@ func TestHTLCDustLimit(t *testing.T) {
// The amount of the HTLC should be above Alice's dust limit and below // The amount of the HTLC should be above Alice's dust limit and below
// Bob's dust limit. // Bob's dust limit.
htlcAmount := (btcutil.Amount(500) + htlcSat := (btcutil.Amount(500) +
htlcTimeoutFee(aliceChannel.channelState.FeePerKw)) htlcTimeoutFee(aliceChannel.channelState.FeePerKw))
htlcAmount := lnwire.NewMSatFromSatoshis(htlcSat)
htlc, preimage := createHTLC(0, htlcAmount) htlc, preimage := createHTLC(0, htlcAmount)
if _, err := aliceChannel.AddHTLC(htlc); err != nil { if _, err := aliceChannel.AddHTLC(htlc); err != nil {
@ -1109,9 +1114,9 @@ func TestHTLCDustLimit(t *testing.T) {
t.Fatalf("incorrect # of outputs: expected %v, got %v", t.Fatalf("incorrect # of outputs: expected %v, got %v",
2, len(commitment.txn.TxOut)) 2, len(commitment.txn.TxOut))
} }
if aliceChannel.channelState.TotalSatoshisSent != uint64(htlcAmount) { if aliceChannel.channelState.TotalMSatSent != htlcAmount {
t.Fatalf("alice satoshis sent incorrect: expected %v, got %v", t.Fatalf("alice satoshis sent incorrect: expected %v, got %v",
htlcAmount, aliceChannel.channelState.TotalSatoshisSent) htlcAmount, aliceChannel.channelState.TotalMSatSent)
} }
} }
@ -1136,9 +1141,11 @@ func TestChannelBalanceDustLimit(t *testing.T) {
// once fees have been subtracted, but smaller than Bob's dust limit. // once fees have been subtracted, but smaller than Bob's dust limit.
defaultFee := calcStaticFee(0) defaultFee := calcStaticFee(0)
dustLimit := aliceChannel.channelState.LocalChanCfg.DustLimit dustLimit := aliceChannel.channelState.LocalChanCfg.DustLimit
aliceBalance := aliceChannel.channelState.LocalBalance aliceBalance := aliceChannel.channelState.LocalBalance.ToSatoshis()
htlcAmount := aliceBalance - (defaultFee + dustLimit + 100) htlcSat := aliceBalance - (defaultFee + dustLimit + 100)
htlcAmount += htlcSuccessFee(aliceChannel.channelState.FeePerKw) htlcSat += htlcSuccessFee(aliceChannel.channelState.FeePerKw)
htlcAmount := lnwire.NewMSatFromSatoshis(htlcSat)
htlc, preimage := createHTLC(0, htlcAmount) htlc, preimage := createHTLC(0, htlcAmount)
if _, err := aliceChannel.AddHTLC(htlc); err != nil { if _, err := aliceChannel.AddHTLC(htlc); err != nil {
@ -1171,9 +1178,9 @@ func TestChannelBalanceDustLimit(t *testing.T) {
t.Fatalf("incorrect # of outputs: expected %v, got %v", t.Fatalf("incorrect # of outputs: expected %v, got %v",
1, len(commitment.txn.TxOut)) 1, len(commitment.txn.TxOut))
} }
if aliceChannel.channelState.TotalSatoshisSent != uint64(htlcAmount) { if aliceChannel.channelState.TotalMSatSent != htlcAmount {
t.Fatalf("alice satoshis sent incorrect: expected %v, got %v", t.Fatalf("alice satoshis sent incorrect: expected %v, got %v",
htlcAmount, aliceChannel.channelState.TotalSatoshisSent) htlcAmount, aliceChannel.channelState.TotalMSatSent)
} }
} }
@ -1197,7 +1204,7 @@ func TestStateUpdatePersistence(t *testing.T) {
} }
const numHtlcs = 4 const numHtlcs = 4
const htlcAmt = 20000 htlcAmt := lnwire.NewMSatFromSatoshis(20000)
// Alice adds 3 HTLCs to the update log, while Bob adds a single HTLC. // Alice adds 3 HTLCs to the update log, while Bob adds a single HTLC.
var alicePreimage [32]byte var alicePreimage [32]byte
@ -1429,21 +1436,21 @@ func TestStateUpdatePersistence(t *testing.T) {
// The amounts transferred should been updated as per the amounts in // The amounts transferred should been updated as per the amounts in
// the HTLCs // the HTLCs
if aliceChannelNew.channelState.TotalSatoshisSent != htlcAmt*3 { if aliceChannelNew.channelState.TotalMSatSent != htlcAmt*3 {
t.Fatalf("expected %v alice satoshis sent, got %v", t.Fatalf("expected %v alice satoshis sent, got %v",
htlcAmt*3, aliceChannelNew.channelState.TotalSatoshisSent) htlcAmt*3, aliceChannelNew.channelState.TotalMSatSent)
} }
if aliceChannelNew.channelState.TotalSatoshisReceived != htlcAmt { if aliceChannelNew.channelState.TotalMSatReceived != htlcAmt {
t.Fatalf("expected %v alice satoshis received, got %v", t.Fatalf("expected %v alice satoshis received, got %v",
htlcAmt, aliceChannelNew.channelState.TotalSatoshisReceived) htlcAmt, aliceChannelNew.channelState.TotalMSatReceived)
} }
if bobChannelNew.channelState.TotalSatoshisSent != htlcAmt { if bobChannelNew.channelState.TotalMSatSent != htlcAmt {
t.Fatalf("expected %v bob satoshis sent, got %v", t.Fatalf("expected %v bob satoshis sent, got %v",
htlcAmt, bobChannel.channelState.TotalSatoshisSent) htlcAmt, bobChannel.channelState.TotalMSatSent)
} }
if bobChannelNew.channelState.TotalSatoshisReceived != htlcAmt*3 { if bobChannelNew.channelState.TotalMSatReceived != htlcAmt*3 {
t.Fatalf("expected %v bob satoshis sent, got %v", t.Fatalf("expected %v bob satoshis sent, got %v",
htlcAmt*3, bobChannel.channelState.TotalSatoshisSent) htlcAmt*3, bobChannel.channelState.TotalMSatReceived)
} }
} }
@ -1461,7 +1468,7 @@ func TestCancelHTLC(t *testing.T) {
// Add a new HTLC from Alice to Bob, then trigger a new state // Add a new HTLC from Alice to Bob, then trigger a new state
// transition in order to include it in the latest state. // transition in order to include it in the latest state.
const htlcAmt = btcutil.SatoshiPerBitcoin htlcAmt := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
var preImage [32]byte var preImage [32]byte
copy(preImage[:], bytes.Repeat([]byte{0xaa}, 32)) copy(preImage[:], bytes.Repeat([]byte{0xaa}, 32))
@ -1486,9 +1493,10 @@ func TestCancelHTLC(t *testing.T) {
// of the new HTLC. // of the new HTLC.
aliceExpectedBalance := btcutil.Amount(btcutil.SatoshiPerBitcoin*4) - aliceExpectedBalance := btcutil.Amount(btcutil.SatoshiPerBitcoin*4) -
calcStaticFee(1) calcStaticFee(1)
if aliceChannel.channelState.LocalBalance != aliceExpectedBalance { if aliceChannel.channelState.LocalBalance.ToSatoshis() != aliceExpectedBalance {
t.Fatalf("Alice's balance is wrong: expected %v, got %v", t.Fatalf("Alice's balance is wrong: expected %v, got %v",
aliceExpectedBalance, aliceChannel.channelState.LocalBalance) aliceExpectedBalance,
aliceChannel.channelState.LocalBalance.ToSatoshis())
} }
// Now, with the HTLC committed on both sides, trigger a cancellation // Now, with the HTLC committed on both sides, trigger a cancellation
@ -1527,22 +1535,28 @@ func TestCancelHTLC(t *testing.T) {
} }
expectedBalance := btcutil.Amount(btcutil.SatoshiPerBitcoin * 5) expectedBalance := btcutil.Amount(btcutil.SatoshiPerBitcoin * 5)
if aliceChannel.channelState.LocalBalance != expectedBalance-calcStaticFee(0) { if aliceChannel.channelState.LocalBalance.ToSatoshis() !=
expectedBalance-calcStaticFee(0) {
t.Fatalf("balance is wrong: expected %v, got %v", t.Fatalf("balance is wrong: expected %v, got %v",
aliceChannel.channelState.LocalBalance, expectedBalance- aliceChannel.channelState.LocalBalance.ToSatoshis(),
calcStaticFee(0)) expectedBalance-calcStaticFee(0))
} }
if aliceChannel.channelState.RemoteBalance != expectedBalance { if aliceChannel.channelState.RemoteBalance.ToSatoshis() != expectedBalance {
t.Fatalf("balance is wrong: expected %v, got %v", t.Fatalf("balance is wrong: expected %v, got %v",
aliceChannel.channelState.RemoteBalance, expectedBalance) aliceChannel.channelState.RemoteBalance.ToSatoshis(),
expectedBalance)
} }
if bobChannel.channelState.LocalBalance != expectedBalance { if bobChannel.channelState.LocalBalance.ToSatoshis() != expectedBalance {
t.Fatalf("balance is wrong: expected %v, got %v", t.Fatalf("balance is wrong: expected %v, got %v",
bobChannel.channelState.LocalBalance, expectedBalance) bobChannel.channelState.LocalBalance.ToSatoshis(),
expectedBalance)
} }
if bobChannel.channelState.RemoteBalance != expectedBalance-calcStaticFee(0) { if bobChannel.channelState.RemoteBalance.ToSatoshis() !=
expectedBalance-calcStaticFee(0) {
t.Fatalf("balance is wrong: expected %v, got %v", t.Fatalf("balance is wrong: expected %v, got %v",
bobChannel.channelState.RemoteBalance, bobChannel.channelState.RemoteBalance.ToSatoshis(),
expectedBalance-calcStaticFee(0)) expectedBalance-calcStaticFee(0))
} }
} }
@ -1574,7 +1588,7 @@ func TestCooperativeCloseDustAdherence(t *testing.T) {
bobChannel.status = channelOpen bobChannel.status = channelOpen
} }
setBalances := func(aliceBalance, bobBalance btcutil.Amount) { setBalances := func(aliceBalance, bobBalance lnwire.MilliSatoshi) {
aliceChannel.channelState.LocalBalance = aliceBalance aliceChannel.channelState.LocalBalance = aliceBalance
aliceChannel.channelState.RemoteBalance = bobBalance aliceChannel.channelState.RemoteBalance = bobBalance
bobChannel.channelState.LocalBalance = bobBalance bobChannel.channelState.LocalBalance = bobBalance
@ -1626,8 +1640,8 @@ func TestCooperativeCloseDustAdherence(t *testing.T) {
// Next we'll modify the current balances and dust limits such that // Next we'll modify the current balances and dust limits such that
// Bob's current balance is above _below_ his dust limit. // Bob's current balance is above _below_ his dust limit.
aliceBal := btcutil.Amount(btcutil.SatoshiPerBitcoin) aliceBal := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin)
bobBal := btcutil.Amount(250) bobBal := lnwire.NewMSatFromSatoshis(250)
setBalances(aliceBal, bobBal) setBalances(aliceBal, bobBal)
// Attempt another cooperative channel closure. It should succeed // Attempt another cooperative channel closure. It should succeed
@ -1659,9 +1673,10 @@ func TestCooperativeCloseDustAdherence(t *testing.T) {
t.Fatalf("close tx has wrong number of outputs: expected %v "+ t.Fatalf("close tx has wrong number of outputs: expected %v "+
"got %v", 1, len(closeTx.TxOut)) "got %v", 1, len(closeTx.TxOut))
} }
if closeTx.TxOut[0].Value != int64(aliceBal-calcStaticFee(0)) { if closeTx.TxOut[0].Value != int64(aliceBal.ToSatoshis()-calcStaticFee(0)) {
t.Fatalf("alice's balance is incorrect: expected %v, got %v", t.Fatalf("alice's balance is incorrect: expected %v, got %v",
int64(aliceBal-calcStaticFee(0)), closeTx.TxOut[0].Value) int64(aliceBal.ToSatoshis()-calcStaticFee(0)),
closeTx.TxOut[0].Value)
} }
// Finally, we'll modify the current balances and dust limits such that // Finally, we'll modify the current balances and dust limits such that
@ -1698,9 +1713,9 @@ func TestCooperativeCloseDustAdherence(t *testing.T) {
t.Fatalf("close tx has wrong number of outputs: expected %v "+ t.Fatalf("close tx has wrong number of outputs: expected %v "+
"got %v", 1, len(closeTx.TxOut)) "got %v", 1, len(closeTx.TxOut))
} }
if closeTx.TxOut[0].Value != int64(aliceBal) { if closeTx.TxOut[0].Value != int64(aliceBal.ToSatoshis()) {
t.Fatalf("bob's balance is incorrect: expected %v, got %v", t.Fatalf("bob's balance is incorrect: expected %v, got %v",
aliceBal, closeTx.TxOut[0].Value) aliceBal.ToSatoshis(), closeTx.TxOut[0].Value)
} }
} }