From 1b2cb14254acdd81975cabb7d2b2082eb1c1de3d Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Mon, 22 Jul 2024 14:34:18 -0700 Subject: [PATCH] lnwallet: change bool isIncoming to new lntypes.ChannelParty This commit removes another raw boolean value and replaces it with a more clear type/name. This will also assist us when we later try and consolidate the logic of evaluateHTLCView into a single coherent computation. --- lnwallet/channel.go | 27 ++++++++++++++++----------- lnwallet/channel_test.go | 32 ++++++++++++++++---------------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 5e6fbc5b5..f09d84813 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -2954,7 +2954,8 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance, ) if rmvHeight == 0 { processRemoveEntry( - entry, ourBalance, theirBalance, true, + entry, ourBalance, theirBalance, + lntypes.Remote, ) } } @@ -2998,7 +2999,7 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance, ) if rmvHeight == 0 { processRemoveEntry( - entry, ourBalance, theirBalance, false, + entry, ourBalance, theirBalance, lntypes.Local, ) } } @@ -3019,7 +3020,7 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance, ) if addHeight == 0 { processAddEntry( - entry, ourBalance, theirBalance, false, + entry, ourBalance, theirBalance, lntypes.Local, ) } @@ -3040,7 +3041,7 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance, ) if addHeight == 0 { processAddEntry( - entry, ourBalance, theirBalance, true, + entry, ourBalance, theirBalance, lntypes.Remote, ) } @@ -3131,9 +3132,9 @@ func (lc *LightningChannel) fetchParent(entry *paymentDescriptor, // 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. func processAddEntry(htlc *paymentDescriptor, ourBalance, - theirBalance *lnwire.MilliSatoshi, isIncoming bool) { + theirBalance *lnwire.MilliSatoshi, originator lntypes.ChannelParty) { - if isIncoming { + if originator == lntypes.Remote { // If this is a new incoming (un-committed) HTLC, then we need // to update their balance accordingly by subtracting the // amount of the HTLC that are funds pending. @@ -3149,31 +3150,35 @@ func processAddEntry(htlc *paymentDescriptor, ourBalance, // previously added HTLC. If the removal entry has already been processed, it // is skipped. func processRemoveEntry(htlc *paymentDescriptor, ourBalance, - theirBalance *lnwire.MilliSatoshi, isIncoming bool) { + theirBalance *lnwire.MilliSatoshi, originator lntypes.ChannelParty) { switch { // If an incoming HTLC is being settled, then this means that we've // received the preimage either from another subsystem, or the // upstream peer in the route. Therefore, we increase our balance by // the HTLC amount. - case isIncoming && htlc.EntryType == Settle: + case originator == lntypes.Remote && htlc.EntryType == Settle: *ourBalance += htlc.Amount // Otherwise, this HTLC is being failed out, therefore the value of the // HTLC should return to the remote party. - case isIncoming && (htlc.EntryType == Fail || htlc.EntryType == MalformedFail): + case originator == lntypes.Remote && + (htlc.EntryType == Fail || htlc.EntryType == MalformedFail): + *theirBalance += htlc.Amount // If an outgoing HTLC is being settled, then this means that the // downstream party resented the preimage or learned of it via a // downstream peer. In either case, we credit their settled value with // the value of the HTLC. - case !isIncoming && htlc.EntryType == Settle: + case originator == lntypes.Local && htlc.EntryType == Settle: *theirBalance += htlc.Amount // Otherwise, one of our outgoing HTLC's has timed out, so the value of // the HTLC should be returned to our settled balance. - case !isIncoming && (htlc.EntryType == Fail || htlc.EntryType == MalformedFail): + case originator == lntypes.Local && + (htlc.EntryType == Fail || htlc.EntryType == MalformedFail): + *ourBalance += htlc.Amount } } diff --git a/lnwallet/channel_test.go b/lnwallet/channel_test.go index 0372e4d16..0bac824a2 100644 --- a/lnwallet/channel_test.go +++ b/lnwallet/channel_test.go @@ -9089,7 +9089,7 @@ func TestProcessAddRemoveEntry(t *testing.T) { name string startHeights heights whoseCommitChain lntypes.ChannelParty - isIncoming bool + originator lntypes.ChannelParty mutateState bool ourExpectedBalance lnwire.MilliSatoshi theirExpectedBalance lnwire.MilliSatoshi @@ -9105,7 +9105,7 @@ func TestProcessAddRemoveEntry(t *testing.T) { remoteRemove: 0, }, whoseCommitChain: lntypes.Remote, - isIncoming: false, + originator: lntypes.Local, mutateState: false, ourExpectedBalance: startBalance, theirExpectedBalance: startBalance, @@ -9126,7 +9126,7 @@ func TestProcessAddRemoveEntry(t *testing.T) { remoteRemove: 0, }, whoseCommitChain: lntypes.Local, - isIncoming: false, + originator: lntypes.Local, mutateState: false, ourExpectedBalance: startBalance, theirExpectedBalance: startBalance, @@ -9147,7 +9147,7 @@ func TestProcessAddRemoveEntry(t *testing.T) { remoteRemove: 0, }, whoseCommitChain: lntypes.Local, - isIncoming: true, + originator: lntypes.Remote, mutateState: false, ourExpectedBalance: startBalance, theirExpectedBalance: startBalance - updateAmount, @@ -9168,7 +9168,7 @@ func TestProcessAddRemoveEntry(t *testing.T) { remoteRemove: 0, }, whoseCommitChain: lntypes.Local, - isIncoming: true, + originator: lntypes.Remote, mutateState: true, ourExpectedBalance: startBalance, theirExpectedBalance: startBalance - updateAmount, @@ -9190,7 +9190,7 @@ func TestProcessAddRemoveEntry(t *testing.T) { remoteRemove: 0, }, whoseCommitChain: lntypes.Remote, - isIncoming: false, + originator: lntypes.Local, mutateState: false, ourExpectedBalance: startBalance - updateAmount, theirExpectedBalance: startBalance, @@ -9211,7 +9211,7 @@ func TestProcessAddRemoveEntry(t *testing.T) { remoteRemove: 0, }, whoseCommitChain: lntypes.Remote, - isIncoming: false, + originator: lntypes.Local, mutateState: true, ourExpectedBalance: startBalance - updateAmount, theirExpectedBalance: startBalance, @@ -9232,7 +9232,7 @@ func TestProcessAddRemoveEntry(t *testing.T) { remoteRemove: removeHeight, }, whoseCommitChain: lntypes.Remote, - isIncoming: false, + originator: lntypes.Local, mutateState: false, ourExpectedBalance: startBalance, theirExpectedBalance: startBalance, @@ -9253,7 +9253,7 @@ func TestProcessAddRemoveEntry(t *testing.T) { remoteRemove: 0, }, whoseCommitChain: lntypes.Local, - isIncoming: false, + originator: lntypes.Local, mutateState: false, ourExpectedBalance: startBalance, theirExpectedBalance: startBalance, @@ -9276,7 +9276,7 @@ func TestProcessAddRemoveEntry(t *testing.T) { remoteRemove: 0, }, whoseCommitChain: lntypes.Remote, - isIncoming: true, + originator: lntypes.Remote, mutateState: false, ourExpectedBalance: startBalance + updateAmount, theirExpectedBalance: startBalance, @@ -9299,7 +9299,7 @@ func TestProcessAddRemoveEntry(t *testing.T) { remoteRemove: 0, }, whoseCommitChain: lntypes.Remote, - isIncoming: false, + originator: lntypes.Local, mutateState: false, ourExpectedBalance: startBalance, theirExpectedBalance: startBalance + updateAmount, @@ -9322,7 +9322,7 @@ func TestProcessAddRemoveEntry(t *testing.T) { remoteRemove: 0, }, whoseCommitChain: lntypes.Remote, - isIncoming: true, + originator: lntypes.Remote, mutateState: false, ourExpectedBalance: startBalance, theirExpectedBalance: startBalance + updateAmount, @@ -9345,7 +9345,7 @@ func TestProcessAddRemoveEntry(t *testing.T) { remoteRemove: 0, }, whoseCommitChain: lntypes.Remote, - isIncoming: false, + originator: lntypes.Local, mutateState: false, ourExpectedBalance: startBalance + updateAmount, theirExpectedBalance: startBalance, @@ -9370,7 +9370,7 @@ func TestProcessAddRemoveEntry(t *testing.T) { remoteRemove: 0, }, whoseCommitChain: lntypes.Local, - isIncoming: true, + originator: lntypes.Remote, mutateState: true, ourExpectedBalance: startBalance + updateAmount, theirExpectedBalance: startBalance, @@ -9395,7 +9395,7 @@ func TestProcessAddRemoveEntry(t *testing.T) { remoteRemove: 0, }, whoseCommitChain: lntypes.Remote, - isIncoming: true, + originator: lntypes.Remote, mutateState: true, ourExpectedBalance: startBalance + updateAmount, theirExpectedBalance: startBalance, @@ -9450,7 +9450,7 @@ func TestProcessAddRemoveEntry(t *testing.T) { if heightDual.GetForParty(test.whoseCommitChain) == 0 { process( update, &ourBalance, &theirBalance, - test.isIncoming, + test.originator, ) if test.mutateState {