Skip to content

Commit b68195a

Browse files
Roasbeefguggero
authored andcommitted
lnwallet: export the HtlcView struct
We'll need this later on to ensure we can always interact with the new aux blobs at all stages of commitment transaction construction.
1 parent 2a63502 commit b68195a

File tree

3 files changed

+92
-67
lines changed

3 files changed

+92
-67
lines changed

lnwallet/channel.go

Lines changed: 76 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2934,18 +2934,25 @@ func HtlcIsDust(chanType channeldb.ChannelType,
29342934
return (htlcAmt - htlcFee) < dustLimit
29352935
}
29362936

2937-
// htlcView represents the "active" HTLCs at a particular point within the
2937+
// HtlcView represents the "active" HTLCs at a particular point within the
29382938
// history of the HTLC update log.
2939-
type htlcView struct {
2940-
ourUpdates []*PaymentDescriptor
2941-
theirUpdates []*PaymentDescriptor
2942-
feePerKw chainfee.SatPerKWeight
2939+
type HtlcView struct {
2940+
// OurUpdates are our outgoing HTLCs.
2941+
OurUpdates []*PaymentDescriptor
2942+
2943+
// TheirUpdates are their incoming HTLCs.
2944+
TheirUpdates []*PaymentDescriptor
2945+
2946+
// FeePerKw is the fee rate in sat/kw of the commitment transaction.
2947+
FeePerKw chainfee.SatPerKWeight
29432948
}
29442949

29452950
// fetchHTLCView returns all the candidate HTLC updates which should be
29462951
// considered for inclusion within a commitment based on the passed HTLC log
29472952
// indexes.
2948-
func (lc *LightningChannel) fetchHTLCView(theirLogIndex, ourLogIndex uint64) *htlcView {
2953+
func (lc *LightningChannel) fetchHTLCView(theirLogIndex,
2954+
ourLogIndex uint64) *HtlcView {
2955+
29492956
var ourHTLCs []*PaymentDescriptor
29502957
for e := lc.localUpdateLog.Front(); e != nil; e = e.Next() {
29512958
htlc := e.Value.(*PaymentDescriptor)
@@ -2970,9 +2977,9 @@ func (lc *LightningChannel) fetchHTLCView(theirLogIndex, ourLogIndex uint64) *ht
29702977
}
29712978
}
29722979

2973-
return &htlcView{
2974-
ourUpdates: ourHTLCs,
2975-
theirUpdates: theirHTLCs,
2980+
return &HtlcView{
2981+
OurUpdates: ourHTLCs,
2982+
TheirUpdates: theirHTLCs,
29762983
}
29772984
}
29782985

@@ -3007,7 +3014,7 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool,
30073014
if err != nil {
30083015
return nil, err
30093016
}
3010-
feePerKw := filteredHTLCView.feePerKw
3017+
feePerKw := filteredHTLCView.FeePerKw
30113018

30123019
// Actually generate unsigned commitment transaction for this view.
30133020
commitTx, err := lc.commitBuilder.createUnsignedCommitmentTx(
@@ -3067,12 +3074,16 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool,
30673074
// In order to ensure _none_ of the HTLC's associated with this new
30683075
// commitment are mutated, we'll manually copy over each HTLC to its
30693076
// respective slice.
3070-
c.outgoingHTLCs = make([]PaymentDescriptor, len(filteredHTLCView.ourUpdates))
3071-
for i, htlc := range filteredHTLCView.ourUpdates {
3077+
c.outgoingHTLCs = make(
3078+
[]PaymentDescriptor, len(filteredHTLCView.OurUpdates),
3079+
)
3080+
for i, htlc := range filteredHTLCView.OurUpdates {
30723081
c.outgoingHTLCs[i] = *htlc
30733082
}
3074-
c.incomingHTLCs = make([]PaymentDescriptor, len(filteredHTLCView.theirUpdates))
3075-
for i, htlc := range filteredHTLCView.theirUpdates {
3083+
c.incomingHTLCs = make(
3084+
[]PaymentDescriptor, len(filteredHTLCView.TheirUpdates),
3085+
)
3086+
for i, htlc := range filteredHTLCView.TheirUpdates {
30763087
c.incomingHTLCs[i] = *htlc
30773088
}
30783089

@@ -3107,15 +3118,15 @@ func fundingTxIn(chanState *channeldb.OpenChannel) wire.TxIn {
31073118
// once for each height, and only in concert with signing a new commitment.
31083119
// TODO(halseth): return htlcs to mutate instead of mutating inside
31093120
// method.
3110-
func (lc *LightningChannel) evaluateHTLCView(view *htlcView, ourBalance,
3121+
func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance,
31113122
theirBalance *lnwire.MilliSatoshi, nextHeight uint64,
3112-
remoteChain, mutateState bool) (*htlcView, error) {
3123+
remoteChain, mutateState bool) (*HtlcView, error) {
31133124

31143125
// We initialize the view's fee rate to the fee rate of the unfiltered
31153126
// view. If any fee updates are found when evaluating the view, it will
31163127
// be updated.
3117-
newView := &htlcView{
3118-
feePerKw: view.feePerKw,
3128+
newView := &HtlcView{
3129+
FeePerKw: view.FeePerKw,
31193130
}
31203131

31213132
// We use two maps, one for the local log and one for the remote log to
@@ -3128,7 +3139,7 @@ func (lc *LightningChannel) evaluateHTLCView(view *htlcView, ourBalance,
31283139
// First we run through non-add entries in both logs, populating the
31293140
// skip sets and mutating the current chain state (crediting balances,
31303141
// etc) to reflect the settle/timeout entry encountered.
3131-
for _, entry := range view.ourUpdates {
3142+
for _, entry := range view.OurUpdates {
31323143
switch entry.EntryType {
31333144
// Skip adds for now. They will be processed below.
31343145
case Add:
@@ -3157,10 +3168,13 @@ func (lc *LightningChannel) evaluateHTLCView(view *htlcView, ourBalance,
31573168
}
31583169

31593170
skipThem[addEntry.HtlcIndex] = struct{}{}
3160-
processRemoveEntry(entry, ourBalance, theirBalance,
3161-
nextHeight, remoteChain, true, mutateState)
3171+
3172+
processRemoveEntry(
3173+
entry, ourBalance, theirBalance, nextHeight,
3174+
remoteChain, true, mutateState,
3175+
)
31623176
}
3163-
for _, entry := range view.theirUpdates {
3177+
for _, entry := range view.TheirUpdates {
31643178
switch entry.EntryType {
31653179
// Skip adds for now. They will be processed below.
31663180
case Add:
@@ -3190,32 +3204,41 @@ func (lc *LightningChannel) evaluateHTLCView(view *htlcView, ourBalance,
31903204
}
31913205

31923206
skipUs[addEntry.HtlcIndex] = struct{}{}
3193-
processRemoveEntry(entry, ourBalance, theirBalance,
3194-
nextHeight, remoteChain, false, mutateState)
3207+
3208+
processRemoveEntry(
3209+
entry, ourBalance, theirBalance, nextHeight,
3210+
remoteChain, false, mutateState,
3211+
)
31953212
}
31963213

31973214
// Next we take a second pass through all the log entries, skipping any
31983215
// settled HTLCs, and debiting the chain state balance due to any newly
31993216
// added HTLCs.
3200-
for _, entry := range view.ourUpdates {
3217+
for _, entry := range view.OurUpdates {
32013218
isAdd := entry.EntryType == Add
32023219
if _, ok := skipUs[entry.HtlcIndex]; !isAdd || ok {
32033220
continue
32043221
}
32053222

3206-
processAddEntry(entry, ourBalance, theirBalance, nextHeight,
3207-
remoteChain, false, mutateState)
3208-
newView.ourUpdates = append(newView.ourUpdates, entry)
3223+
processAddEntry(
3224+
entry, ourBalance, theirBalance, nextHeight,
3225+
remoteChain, false, mutateState,
3226+
)
3227+
3228+
newView.OurUpdates = append(newView.OurUpdates, entry)
32093229
}
3210-
for _, entry := range view.theirUpdates {
3230+
for _, entry := range view.TheirUpdates {
32113231
isAdd := entry.EntryType == Add
32123232
if _, ok := skipThem[entry.HtlcIndex]; !isAdd || ok {
32133233
continue
32143234
}
32153235

3216-
processAddEntry(entry, ourBalance, theirBalance, nextHeight,
3217-
remoteChain, true, mutateState)
3218-
newView.theirUpdates = append(newView.theirUpdates, entry)
3236+
processAddEntry(
3237+
entry, ourBalance, theirBalance, nextHeight,
3238+
remoteChain, true, mutateState,
3239+
)
3240+
3241+
newView.TheirUpdates = append(newView.TheirUpdates, entry)
32193242
}
32203243

32213244
return newView, nil
@@ -3363,7 +3386,7 @@ func processRemoveEntry(htlc *PaymentDescriptor, ourBalance,
33633386
// processFeeUpdate processes a log update that updates the current commitment
33643387
// fee.
33653388
func processFeeUpdate(feeUpdate *PaymentDescriptor, nextHeight uint64,
3366-
remoteChain bool, mutateState bool, view *htlcView) {
3389+
remoteChain bool, mutateState bool, view *HtlcView) {
33673390

33683391
// Fee updates are applied for all commitments after they are
33693392
// sent/received, so we consider them being added and removed at the
@@ -3384,7 +3407,7 @@ func processFeeUpdate(feeUpdate *PaymentDescriptor, nextHeight uint64,
33843407

33853408
// If the update wasn't already locked in, update the current fee rate
33863409
// to reflect this update.
3387-
view.feePerKw = chainfee.SatPerKWeight(feeUpdate.Amount.ToSatoshis())
3410+
view.FeePerKw = chainfee.SatPerKWeight(feeUpdate.Amount.ToSatoshis())
33883411

33893412
if mutateState {
33903413
*addHeight = nextHeight
@@ -3959,10 +3982,10 @@ func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter,
39593982
// appropriate update log, in order to validate the sanity of the
39603983
// commitment resulting from _actually adding_ this HTLC to the state.
39613984
if predictOurAdd != nil {
3962-
view.ourUpdates = append(view.ourUpdates, predictOurAdd)
3985+
view.OurUpdates = append(view.OurUpdates, predictOurAdd)
39633986
}
39643987
if predictTheirAdd != nil {
3965-
view.theirUpdates = append(view.theirUpdates, predictTheirAdd)
3988+
view.TheirUpdates = append(view.TheirUpdates, predictTheirAdd)
39663989
}
39673990

39683991
ourBalance, theirBalance, commitWeight, filteredView, err := lc.computeView(
@@ -3972,7 +3995,7 @@ func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter,
39723995
return err
39733996
}
39743997

3975-
feePerKw := filteredView.feePerKw
3998+
feePerKw := filteredView.FeePerKw
39763999

39774000
// Ensure that the fee being applied is enough to be relayed across the
39784001
// network in a reasonable time frame.
@@ -4116,7 +4139,7 @@ func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter,
41164139
// First check that the remote updates won't violate it's channel
41174140
// constraints.
41184141
err = validateUpdates(
4119-
filteredView.theirUpdates, &lc.channelState.RemoteChanCfg,
4142+
filteredView.TheirUpdates, &lc.channelState.RemoteChanCfg,
41204143
)
41214144
if err != nil {
41224145
return err
@@ -4125,7 +4148,7 @@ func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter,
41254148
// Secondly check that our updates won't violate our channel
41264149
// constraints.
41274150
err = validateUpdates(
4128-
filteredView.ourUpdates, &lc.channelState.LocalChanCfg,
4151+
filteredView.OurUpdates, &lc.channelState.LocalChanCfg,
41294152
)
41304153
if err != nil {
41314154
return err
@@ -4727,17 +4750,17 @@ func (lc *LightningChannel) ProcessChanSyncMsg(
47274750
return updates, openedCircuits, closedCircuits, nil
47284751
}
47294752

4730-
// computeView takes the given htlcView, and calculates the balances, filtered
4753+
// computeView takes the given HtlcView, and calculates the balances, filtered
47314754
// view (settling unsettled HTLCs), commitment weight and feePerKw, after
47324755
// applying the HTLCs to the latest commitment. The returned balances are the
47334756
// balances *before* subtracting the commitment fee from the initiator's
47344757
// balance.
47354758
//
47364759
// If the updateState boolean is set true, the add and remove heights of the
47374760
// HTLCs will be set to the next commitment height.
4738-
func (lc *LightningChannel) computeView(view *htlcView, remoteChain bool,
4761+
func (lc *LightningChannel) computeView(view *HtlcView, remoteChain bool,
47394762
updateState bool) (lnwire.MilliSatoshi, lnwire.MilliSatoshi, int64,
4740-
*htlcView, error) {
4763+
*HtlcView, error) {
47414764

47424765
commitChain := lc.localCommitChain
47434766
dustLimit := lc.channelState.LocalChanCfg.DustLimit
@@ -4768,20 +4791,22 @@ func (lc *LightningChannel) computeView(view *htlcView, remoteChain bool,
47684791
// Initiate feePerKw to the last committed fee for this chain as we'll
47694792
// need this to determine which HTLCs are dust, and also the final fee
47704793
// rate.
4771-
view.feePerKw = commitChain.tip().feePerKw
4794+
view.FeePerKw = commitChain.tip().feePerKw
47724795

47734796
// We evaluate the view at this stage, meaning settled and failed HTLCs
47744797
// will remove their corresponding added HTLCs. The resulting filtered
47754798
// view will only have Add entries left, making it easy to compare the
47764799
// channel constraints to the final commitment state. If any fee
47774800
// updates are found in the logs, the commitment fee rate should be
47784801
// changed, so we'll also set the feePerKw to this new value.
4779-
filteredHTLCView, err := lc.evaluateHTLCView(view, &ourBalance,
4780-
&theirBalance, nextHeight, remoteChain, updateState)
4802+
filteredHTLCView, err := lc.evaluateHTLCView(
4803+
view, &ourBalance, &theirBalance, nextHeight, remoteChain,
4804+
updateState,
4805+
)
47814806
if err != nil {
47824807
return 0, 0, 0, nil, err
47834808
}
4784-
feePerKw := filteredHTLCView.feePerKw
4809+
feePerKw := filteredHTLCView.FeePerKw
47854810

47864811
// We need to first check ourBalance and theirBalance to be negative
47874812
// because MilliSathoshi is a unsigned type and can underflow in
@@ -4799,7 +4824,7 @@ func (lc *LightningChannel) computeView(view *htlcView, remoteChain bool,
47994824
// Now go through all HTLCs at this stage, to calculate the total
48004825
// weight, needed to calculate the transaction fee.
48014826
var totalHtlcWeight int64
4802-
for _, htlc := range filteredHTLCView.ourUpdates {
4827+
for _, htlc := range filteredHTLCView.OurUpdates {
48034828
if HtlcIsDust(
48044829
lc.channelState.ChanType, false, !remoteChain,
48054830
feePerKw, htlc.Amount.ToSatoshis(), dustLimit,
@@ -4810,7 +4835,7 @@ func (lc *LightningChannel) computeView(view *htlcView, remoteChain bool,
48104835

48114836
totalHtlcWeight += input.HTLCWeight
48124837
}
4813-
for _, htlc := range filteredHTLCView.theirUpdates {
4838+
for _, htlc := range filteredHTLCView.TheirUpdates {
48144839
if HtlcIsDust(
48154840
lc.channelState.ChanType, true, !remoteChain,
48164841
feePerKw, htlc.Amount.ToSatoshis(), dustLimit,
@@ -8271,13 +8296,13 @@ func (lc *LightningChannel) availableBalance(
82718296
}
82728297

82738298
// availableCommitmentBalance attempts to calculate the balance we have
8274-
// available for HTLCs on the local/remote commitment given the htlcView. To
8299+
// available for HTLCs on the local/remote commitment given the HtlcView. To
82758300
// account for sending HTLCs of different sizes, it will report the balance
82768301
// available for sending non-dust HTLCs, which will be manifested on the
82778302
// commitment, increasing the commitment fee we must pay as an initiator,
82788303
// eating into our balance. It will make sure we won't violate the channel
82798304
// reserve constraints for this amount.
8280-
func (lc *LightningChannel) availableCommitmentBalance(view *htlcView,
8305+
func (lc *LightningChannel) availableCommitmentBalance(view *HtlcView,
82818306
remoteChain bool, buffer BufferType) (lnwire.MilliSatoshi, int64) {
82828307

82838308
// Compute the current balances for this commitment. This will take
@@ -8305,7 +8330,7 @@ func (lc *LightningChannel) availableCommitmentBalance(view *htlcView,
83058330
// Calculate the commitment fee in the case where we would add another
83068331
// HTLC to the commitment, as only the balance remaining after this fee
83078332
// has been paid is actually available for sending.
8308-
feePerKw := filteredView.feePerKw
8333+
feePerKw := filteredView.FeePerKw
83098334
additionalHtlcFee := lnwire.NewMSatFromSatoshis(
83108335
feePerKw.FeeForWeight(input.HTLCWeight),
83118336
)

lnwallet/channel_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8540,10 +8540,10 @@ func TestEvaluateView(t *testing.T) {
85408540
}
85418541
}
85428542

8543-
view := &htlcView{
8544-
ourUpdates: test.ourHtlcs,
8545-
theirUpdates: test.theirHtlcs,
8546-
feePerKw: feePerKw,
8543+
view := &HtlcView{
8544+
OurUpdates: test.ourHtlcs,
8545+
TheirUpdates: test.theirHtlcs,
8546+
FeePerKw: feePerKw,
85478547
}
85488548

85498549
var (
@@ -8564,17 +8564,17 @@ func TestEvaluateView(t *testing.T) {
85648564
t.Fatalf("unexpected error: %v", err)
85658565
}
85668566

8567-
if result.feePerKw != test.expectedFee {
8567+
if result.FeePerKw != test.expectedFee {
85688568
t.Fatalf("expected fee: %v, got: %v",
8569-
test.expectedFee, result.feePerKw)
8569+
test.expectedFee, result.FeePerKw)
85708570
}
85718571

85728572
checkExpectedHtlcs(
8573-
t, result.ourUpdates, test.ourExpectedHtlcs,
8573+
t, result.OurUpdates, test.ourExpectedHtlcs,
85748574
)
85758575

85768576
checkExpectedHtlcs(
8577-
t, result.theirUpdates, test.theirExpectedHtlcs,
8577+
t, result.TheirUpdates, test.theirExpectedHtlcs,
85788578
)
85798579

85808580
if lc.channelState.TotalMSatSent != test.expectSent {
@@ -8797,15 +8797,15 @@ func TestProcessFeeUpdate(t *testing.T) {
87978797
EntryType: FeeUpdate,
87988798
}
87998799

8800-
view := &htlcView{
8801-
feePerKw: chainfee.SatPerKWeight(feePerKw),
8800+
view := &HtlcView{
8801+
FeePerKw: chainfee.SatPerKWeight(feePerKw),
88028802
}
88038803
processFeeUpdate(
88048804
update, nextHeight, test.remoteChain,
88058805
test.mutate, view,
88068806
)
88078807

8808-
if view.feePerKw != test.expectedFee {
8808+
if view.FeePerKw != test.expectedFee {
88098809
t.Fatalf("expected fee: %v, got: %v",
88108810
test.expectedFee, feePerKw)
88118811
}

0 commit comments

Comments
 (0)