@@ -2893,10 +2893,9 @@ func fundingTxIn(chanState *channeldb.OpenChannel) wire.TxIn {
2893
2893
// 1. The new htlcView reflecting the current channel state.
2894
2894
// 2. A Dual of the updates which have not yet been committed in
2895
2895
// 'whoseCommitChain's commitment chain.
2896
- func (lc * LightningChannel ) evaluateHTLCView (view * HtlcView , ourBalance ,
2897
- theirBalance * lnwire.MilliSatoshi , nextHeight uint64 ,
2898
- whoseCommitChain lntypes.ChannelParty ) (* HtlcView ,
2899
- lntypes.Dual [[]* paymentDescriptor ], error ) {
2896
+ func (lc * LightningChannel ) evaluateHTLCView (view * HtlcView ,
2897
+ whoseCommitChain lntypes.ChannelParty , nextHeight uint64 ) (* HtlcView ,
2898
+ lntypes.Dual [[]* paymentDescriptor ], lntypes.Dual [int64 ], error ) {
2900
2899
2901
2900
// We initialize the view's fee rate to the fee rate of the unfiltered
2902
2901
// view. If any fee updates are found when evaluating the view, it will
@@ -2929,6 +2928,8 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance,
2929
2928
Remote : fn .NewSet [uint64 ](),
2930
2929
}
2931
2930
2931
+ balanceDeltas := lntypes.Dual [int64 ]{}
2932
+
2932
2933
parties := [2 ]lntypes.ChannelParty {lntypes .Local , lntypes .Remote }
2933
2934
for _ , party := range parties {
2934
2935
// First we run through non-add entries in both logs,
@@ -2949,7 +2950,8 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance,
2949
2950
entry , whoseCommitChain , party .CounterParty (),
2950
2951
)
2951
2952
if err != nil {
2952
- return nil , noUncommitted , err
2953
+ noDeltas := lntypes.Dual [int64 ]{}
2954
+ return nil , noUncommitted , noDeltas , err
2953
2955
}
2954
2956
2955
2957
skipSet := skip .GetForParty (party .CounterParty ())
@@ -2960,41 +2962,30 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance,
2960
2962
if rmvHeight == 0 {
2961
2963
switch {
2962
2964
// If an incoming HTLC is being settled, then
2963
- // this means that we've received the preimage
2964
- // either from another subsystem, or the
2965
- // upstream peer in the route. Therefore, we
2966
- // increase our balance by the HTLC amount.
2967
- case party .CounterParty () == lntypes .Remote &&
2968
- entry .EntryType == Settle :
2969
-
2970
- * ourBalance += entry .Amount
2965
+ // this means that the preimage has been
2966
+ // received by the settling party Therefore, we
2967
+ // increase the settling party's balance by the
2968
+ // HTLC amount.
2969
+ case entry .EntryType == Settle :
2970
+ delta := int64 (entry .Amount )
2971
+ balanceDeltas .ModifyForParty (
2972
+ party ,
2973
+ func (acc int64 ) int64 {
2974
+ return acc + delta
2975
+ },
2976
+ )
2971
2977
2972
2978
// Otherwise, this HTLC is being failed out,
2973
2979
// therefore the value of the HTLC should
2974
- // return to the remote party.
2975
- case party .CounterParty () == lntypes .Remote &&
2976
- entry .EntryType != Settle :
2977
-
2978
- * theirBalance += entry .Amount
2979
-
2980
- // If an outgoing HTLC is being settled, then
2981
- // this means that the downstream party
2982
- // resented the preimage or learned of it via a
2983
- // downstream peer. In either case, we credit
2984
- // their settled value with the value of the
2985
- // HTLC.
2986
- case party .CounterParty () == lntypes .Local &&
2987
- entry .EntryType == Settle :
2988
-
2989
- * theirBalance += entry .Amount
2990
-
2991
- // Otherwise, one of our outgoing HTLC's has
2992
- // timed out, so the value of the HTLC should
2993
- // be returned to our settled balance.
2994
- case party .CounterParty () == lntypes .Local &&
2995
- entry .EntryType != Settle :
2996
-
2997
- * ourBalance += entry .Amount
2980
+ // return to the failing party's counterparty.
2981
+ case entry .EntryType != Settle :
2982
+ delta := int64 (entry .Amount )
2983
+ balanceDeltas .ModifyForParty (
2984
+ party .CounterParty (),
2985
+ func (acc int64 ) int64 {
2986
+ return acc + delta
2987
+ },
2988
+ )
2998
2989
}
2999
2990
}
3000
2991
}
@@ -3015,19 +3006,19 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance,
3015
3006
addHeights := & entry .addCommitHeights
3016
3007
addHeight := addHeights .GetForParty (whoseCommitChain )
3017
3008
if addHeight == 0 {
3018
- if party == lntypes . Remote {
3019
- // If this is a new incoming
3020
- // (un-committed) HTLC, then we need
3021
- // to update their balance accordingly
3022
- // by subtracting the amount of the
3023
- // HTLC that are funds pending.
3024
- * theirBalance -= entry . Amount
3025
- } else {
3026
- // Similarly, we need to debit our
3027
- // balance if this is an out going HTLC
3028
- // to reflect the pending balance.
3029
- * ourBalance -= entry . Amount
3030
- }
3009
+ // If this is a new incoming (un-committed)
3010
+ // HTLC, then we need to update their balance
3011
+ // accordingly by subtracting the amount of
3012
+ // the HTLC that are funds pending.
3013
+ // Similarly, we need to debit our balance if
3014
+ // this is an out going HTLC to reflect the
3015
+ // pending balance.
3016
+ balanceDeltas . ModifyForParty (
3017
+ party ,
3018
+ func ( acc int64 ) int64 {
3019
+ return acc - int64 ( entry . Amount )
3020
+ },
3021
+ )
3031
3022
}
3032
3023
}
3033
3024
@@ -3068,7 +3059,7 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance,
3068
3059
},
3069
3060
)
3070
3061
3071
- return newView , uncommittedUpdates , nil
3062
+ return newView , uncommittedUpdates , balanceDeltas , nil
3072
3063
}
3073
3064
3074
3065
// fetchParent is a helper that looks up update log parent entries in the
@@ -4589,12 +4580,26 @@ func (lc *LightningChannel) computeView(view *HtlcView,
4589
4580
// channel constraints to the final commitment state. If any fee
4590
4581
// updates are found in the logs, the commitment fee rate should be
4591
4582
// changed, so we'll also set the feePerKw to this new value.
4592
- filteredHTLCView , uncommitted , err := lc .evaluateHTLCView (
4593
- view , & ourBalance , & theirBalance , nextHeight , whoseCommitChain ,
4583
+ filteredHTLCView , uncommitted , deltas , err := lc .evaluateHTLCView (
4584
+ view , whoseCommitChain , nextHeight ,
4594
4585
)
4595
4586
if err != nil {
4596
4587
return 0 , 0 , 0 , nil , err
4597
4588
}
4589
+
4590
+ // Add the balance deltas to the balances we got from the commitment
4591
+ // state.
4592
+ if deltas .Local >= 0 {
4593
+ ourBalance += lnwire .MilliSatoshi (deltas .Local )
4594
+ } else {
4595
+ ourBalance -= lnwire .MilliSatoshi (- 1 * deltas .Local )
4596
+ }
4597
+ if deltas .Remote >= 0 {
4598
+ theirBalance += lnwire .MilliSatoshi (deltas .Remote )
4599
+ } else {
4600
+ theirBalance -= lnwire .MilliSatoshi (- 1 * deltas .Remote )
4601
+ }
4602
+
4598
4603
if updateState {
4599
4604
state := lc .channelState
4600
4605
received := & state .TotalMSatReceived
0 commit comments