• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 15561477203

10 Jun 2025 01:54PM UTC coverage: 58.351% (-10.1%) from 68.487%
15561477203

Pull #9356

github

web-flow
Merge 6440b25db into c6d6d4c0b
Pull Request #9356: lnrpc: add incoming/outgoing channel ids filter to forwarding history request

33 of 36 new or added lines in 2 files covered. (91.67%)

28366 existing lines in 455 files now uncovered.

97715 of 167461 relevant lines covered (58.35%)

1.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

10.2
/lnwallet/aux_leaf_store.go
1
package lnwallet
2

3
import (
4
        "github.com/btcsuite/btcd/btcutil"
5
        "github.com/btcsuite/btcd/chaincfg/chainhash"
6
        "github.com/btcsuite/btcd/wire"
7
        "github.com/lightningnetwork/lnd/channeldb"
8
        "github.com/lightningnetwork/lnd/fn/v2"
9
        "github.com/lightningnetwork/lnd/input"
10
        "github.com/lightningnetwork/lnd/lntypes"
11
        "github.com/lightningnetwork/lnd/lnwire"
12
        "github.com/lightningnetwork/lnd/tlv"
13
)
14

15
// CommitSortFunc is a function type alias for a function that sorts the
16
// commitment transaction outputs. The second parameter is a list of CLTV
17
// timeouts that must correspond to the number of transaction outputs, with the
18
// value of 0 for non-HTLC outputs. The HTLC indexes are needed to have a
19
// deterministic sort value for HTLCs that have the identical amount, CLTV
20
// timeout and payment hash (e.g. multiple MPP shards of the same payment, where
21
// the on-chain script would be identical).
22
type CommitSortFunc func(tx *wire.MsgTx, cltvs []uint32,
23
        indexes []input.HtlcIndex) error
24

25
// DefaultCommitSort is the default commitment sort function that sorts the
26
// commitment transaction inputs and outputs according to BIP69. The second
27
// parameter is a list of CLTV timeouts that must correspond to the number of
28
// transaction outputs, with the value of 0 for non-HTLC outputs. The third
29
// parameter is unused for the default sort function.
30
func DefaultCommitSort(tx *wire.MsgTx, cltvs []uint32,
31
        _ []input.HtlcIndex) error {
3✔
32

3✔
33
        InPlaceCommitSort(tx, cltvs)
3✔
34
        return nil
3✔
35
}
3✔
36

37
// CommitAuxLeaves stores two potential auxiliary leaves for the remote and
38
// local output that may be used to augment the final tapscript trees of the
39
// commitment transaction.
40
type CommitAuxLeaves struct {
41
        // LocalAuxLeaf is the local party's auxiliary leaf.
42
        LocalAuxLeaf input.AuxTapLeaf
43

44
        // RemoteAuxLeaf is the remote party's auxiliary leaf.
45
        RemoteAuxLeaf input.AuxTapLeaf
46

47
        // OutgoingHTLCLeaves is the set of aux leaves for the outgoing HTLCs
48
        // on this commitment transaction.
49
        OutgoingHtlcLeaves input.HtlcAuxLeaves
50

51
        // IncomingHTLCLeaves is the set of aux leaves for the incoming HTLCs
52
        // on this commitment transaction.
53
        IncomingHtlcLeaves input.HtlcAuxLeaves
54
}
55

56
// AuxChanState is a struct that holds certain fields of the
57
// channeldb.OpenChannel struct that are used by the aux components. The data
58
// is copied over to prevent accidental mutation of the original channel state.
59
type AuxChanState struct {
60
        // ChanType denotes which type of channel this is.
61
        ChanType channeldb.ChannelType
62

63
        // FundingOutpoint is the outpoint of the final funding transaction.
64
        // This value uniquely and globally identifies the channel within the
65
        // target blockchain as specified by the chain hash parameter.
66
        FundingOutpoint wire.OutPoint
67

68
        // ShortChannelID encodes the exact location in the chain in which the
69
        // channel was initially confirmed. This includes: the block height,
70
        // transaction index, and the output within the target transaction.
71
        //
72
        // If IsZeroConf(), then this will the "base" (very first) ALIAS scid
73
        // and the confirmed SCID will be stored in ConfirmedScid.
74
        ShortChannelID lnwire.ShortChannelID
75

76
        // IsInitiator is a bool which indicates if we were the original
77
        // initiator for the channel. This value may affect how higher levels
78
        // negotiate fees, or close the channel.
79
        IsInitiator bool
80

81
        // Capacity is the total capacity of this channel.
82
        Capacity btcutil.Amount
83

84
        // LocalChanCfg is the channel configuration for the local node.
85
        LocalChanCfg channeldb.ChannelConfig
86

87
        // RemoteChanCfg is the channel configuration for the remote node.
88
        RemoteChanCfg channeldb.ChannelConfig
89

90
        // ThawHeight is the height when a frozen channel once again becomes a
91
        // normal channel. If this is zero, then there're no restrictions on
92
        // this channel. If the value is lower than 500,000, then it's
93
        // interpreted as a relative height, or an absolute height otherwise.
94
        ThawHeight uint32
95

96
        // TapscriptRoot is an optional tapscript root used to derive the MuSig2
97
        // funding output.
98
        TapscriptRoot fn.Option[chainhash.Hash]
99

100
        // CustomBlob is an optional blob that can be used to store information
101
        // specific to a custom channel type. This information is only created
102
        // at channel funding time, and after wards is to be considered
103
        // immutable.
104
        CustomBlob fn.Option[tlv.Blob]
105
}
106

107
// NewAuxChanState creates a new AuxChanState from the given channel state.
UNCOV
108
func NewAuxChanState(chanState *channeldb.OpenChannel) AuxChanState {
×
UNCOV
109
        return AuxChanState{
×
UNCOV
110
                ChanType:        chanState.ChanType,
×
UNCOV
111
                FundingOutpoint: chanState.FundingOutpoint,
×
UNCOV
112
                ShortChannelID:  chanState.ShortChannelID,
×
UNCOV
113
                IsInitiator:     chanState.IsInitiator,
×
UNCOV
114
                Capacity:        chanState.Capacity,
×
UNCOV
115
                LocalChanCfg:    chanState.LocalChanCfg,
×
UNCOV
116
                RemoteChanCfg:   chanState.RemoteChanCfg,
×
UNCOV
117
                ThawHeight:      chanState.ThawHeight,
×
UNCOV
118
                TapscriptRoot:   chanState.TapscriptRoot,
×
UNCOV
119
                CustomBlob:      chanState.CustomBlob,
×
UNCOV
120
        }
×
UNCOV
121
}
×
122

123
// CommitDiffAuxInput is the input required to compute the diff of the auxiliary
124
// leaves for a commitment transaction.
125
type CommitDiffAuxInput struct {
126
        // ChannelState is the static channel information of the channel this
127
        // commitment transaction relates to.
128
        ChannelState AuxChanState
129

130
        // PrevBlob is the blob of the previous commitment transaction.
131
        PrevBlob tlv.Blob
132

133
        // UnfilteredView is the unfiltered, original HTLC view of the channel.
134
        // Unfiltered in this context means that the view contains all HTLCs,
135
        // including the canceled ones.
136
        UnfilteredView AuxHtlcView
137

138
        // WhoseCommit denotes whose commitment transaction we are computing the
139
        // diff for.
140
        WhoseCommit lntypes.ChannelParty
141

142
        // OurBalance is the balance of the local party.
143
        OurBalance lnwire.MilliSatoshi
144

145
        // TheirBalance is the balance of the remote party.
146
        TheirBalance lnwire.MilliSatoshi
147

148
        // KeyRing is the key ring that can be used to derive keys for the
149
        // commitment transaction.
150
        KeyRing CommitmentKeyRing
151
}
152

153
// CommitDiffAuxResult is the result of computing the diff of the auxiliary
154
// leaves for a commitment transaction.
155
type CommitDiffAuxResult struct {
156
        // AuxLeaves are the auxiliary leaves for the new commitment
157
        // transaction.
158
        AuxLeaves fn.Option[CommitAuxLeaves]
159

160
        // CommitSortFunc is an optional function that sorts the commitment
161
        // transaction inputs and outputs.
162
        CommitSortFunc fn.Option[CommitSortFunc]
163
}
164

165
// AuxLeafStore is used to optionally fetch auxiliary tapscript leaves for the
166
// commitment transaction given an opaque blob. This is also used to implement
167
// a state transition function for the blobs to allow them to be refreshed with
168
// each state.
169
type AuxLeafStore interface {
170
        // FetchLeavesFromView attempts to fetch the auxiliary leaves that
171
        // correspond to the passed aux blob, and pending original (unfiltered)
172
        // HTLC view.
173
        FetchLeavesFromView(
174
                in CommitDiffAuxInput) fn.Result[CommitDiffAuxResult]
175

176
        // FetchLeavesFromCommit attempts to fetch the auxiliary leaves that
177
        // correspond to the passed aux blob, and an existing channel
178
        // commitment.
179
        FetchLeavesFromCommit(chanState AuxChanState,
180
                commit channeldb.ChannelCommitment, keyRing CommitmentKeyRing,
181
                whoseCommit lntypes.ChannelParty) fn.Result[CommitDiffAuxResult]
182

183
        // FetchLeavesFromRevocation attempts to fetch the auxiliary leaves
184
        // from a channel revocation that stores balance + blob information.
185
        FetchLeavesFromRevocation(
186
                r *channeldb.RevocationLog) fn.Result[CommitDiffAuxResult]
187

188
        // ApplyHtlcView serves as the state transition function for the custom
189
        // channel's blob. Given the old blob, and an HTLC view, then a new
190
        // blob should be returned that reflects the pending updates.
191
        ApplyHtlcView(in CommitDiffAuxInput) fn.Result[fn.Option[tlv.Blob]]
192
}
193

194
// auxLeavesFromView is used to derive the set of commit aux leaves (if any),
195
// that are needed to create a new commitment transaction using the original
196
// (unfiltered) htlc view.
197
func auxLeavesFromView(leafStore AuxLeafStore, chanState *channeldb.OpenChannel,
198
        prevBlob fn.Option[tlv.Blob], originalView *HtlcView,
199
        whoseCommit lntypes.ChannelParty, ourBalance,
200
        theirBalance lnwire.MilliSatoshi,
UNCOV
201
        keyRing CommitmentKeyRing) fn.Result[CommitDiffAuxResult] {
×
UNCOV
202

×
UNCOV
203
        return fn.MapOptionZ(
×
UNCOV
204
                prevBlob, func(blob tlv.Blob) fn.Result[CommitDiffAuxResult] {
×
205
                        return leafStore.FetchLeavesFromView(CommitDiffAuxInput{
×
206
                                ChannelState:   NewAuxChanState(chanState),
×
207
                                PrevBlob:       blob,
×
208
                                UnfilteredView: newAuxHtlcView(originalView),
×
209
                                WhoseCommit:    whoseCommit,
×
210
                                OurBalance:     ourBalance,
×
211
                                TheirBalance:   theirBalance,
×
212
                                KeyRing:        keyRing,
×
213
                        })
×
214
                },
×
215
        )
216
}
217

218
// updateAuxBlob is a helper function that attempts to update the aux blob
219
// given the prior and current state information.
220
func updateAuxBlob(leafStore AuxLeafStore, chanState *channeldb.OpenChannel,
221
        prevBlob fn.Option[tlv.Blob], nextViewUnfiltered *HtlcView,
222
        whoseCommit lntypes.ChannelParty, ourBalance,
223
        theirBalance lnwire.MilliSatoshi,
UNCOV
224
        keyRing CommitmentKeyRing) fn.Result[fn.Option[tlv.Blob]] {
×
UNCOV
225

×
UNCOV
226
        return fn.MapOptionZ(
×
UNCOV
227
                prevBlob, func(blob tlv.Blob) fn.Result[fn.Option[tlv.Blob]] {
×
228
                        return leafStore.ApplyHtlcView(CommitDiffAuxInput{
×
229
                                ChannelState: NewAuxChanState(chanState),
×
230
                                PrevBlob:     blob,
×
231
                                UnfilteredView: newAuxHtlcView(
×
232
                                        nextViewUnfiltered,
×
233
                                ),
×
234
                                WhoseCommit:  whoseCommit,
×
235
                                OurBalance:   ourBalance,
×
236
                                TheirBalance: theirBalance,
×
237
                                KeyRing:      keyRing,
×
238
                        })
×
239
                },
×
240
        )
241
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc