• 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

80.89
/lnwire/custom_records.go
1
package lnwire
2

3
import (
4
        "bytes"
5
        "fmt"
6
        "io"
7
        "maps"
8
        "sort"
9

10
        "github.com/lightningnetwork/lnd/fn/v2"
11
        "github.com/lightningnetwork/lnd/tlv"
12
)
13

14
const (
15
        // MinCustomRecordsTlvType is the minimum custom records TLV type as
16
        // defined in BOLT 01.
17
        MinCustomRecordsTlvType = 65536
18
)
19

20
// CustomRecords stores a set of custom key/value pairs. Map keys are TLV types
21
// which must be greater than or equal to MinCustomRecordsTlvType.
22
type CustomRecords map[uint64][]byte
23

24
// NewCustomRecords creates a new CustomRecords instance from a
25
// tlv.TypeMap.
26
func NewCustomRecords(tlvMap tlv.TypeMap) (CustomRecords, error) {
3✔
27
        // Make comparisons in unit tests easy by returning nil if the map is
3✔
28
        // empty.
3✔
29
        if len(tlvMap) == 0 {
6✔
30
                return nil, nil
3✔
31
        }
3✔
32

33
        customRecords := make(CustomRecords, len(tlvMap))
3✔
34
        for k, v := range tlvMap {
6✔
35
                customRecords[uint64(k)] = v
3✔
36
        }
3✔
37

38
        // Validate the custom records.
39
        err := customRecords.Validate()
3✔
40
        if err != nil {
3✔
UNCOV
41
                return nil, fmt.Errorf("custom records from tlv map "+
×
UNCOV
42
                        "validation error: %w", err)
×
UNCOV
43
        }
×
44

45
        return customRecords, nil
3✔
46
}
47

48
// ParseCustomRecords creates a new CustomRecords instance from a tlv.Blob.
49
func ParseCustomRecords(b tlv.Blob) (CustomRecords, error) {
3✔
50
        return ParseCustomRecordsFrom(bytes.NewReader(b))
3✔
51
}
3✔
52

53
// ParseCustomRecordsFrom creates a new CustomRecords instance from a reader.
54
func ParseCustomRecordsFrom(r io.Reader) (CustomRecords, error) {
3✔
55
        typeMap, err := DecodeRecords(r)
3✔
56
        if err != nil {
3✔
57
                return nil, fmt.Errorf("error decoding HTLC record: %w", err)
×
58
        }
×
59

60
        return NewCustomRecords(typeMap)
3✔
61
}
62

63
// Validate checks that all custom records are in the custom type range.
64
func (c CustomRecords) Validate() error {
3✔
65
        if c == nil {
6✔
66
                return nil
3✔
67
        }
3✔
68

69
        for key := range c {
6✔
70
                if key < MinCustomRecordsTlvType {
3✔
UNCOV
71
                        return fmt.Errorf("custom records entry with TLV "+
×
UNCOV
72
                                "type below min: %d", MinCustomRecordsTlvType)
×
UNCOV
73
                }
×
74
        }
75

76
        return nil
3✔
77
}
78

79
// Copy returns a copy of the custom records.
80
func (c CustomRecords) Copy() CustomRecords {
3✔
81
        if c == nil {
6✔
82
                return nil
3✔
83
        }
3✔
84

85
        customRecords := make(CustomRecords, len(c))
3✔
86
        for k, v := range c {
6✔
87
                customRecords[k] = v
3✔
88
        }
3✔
89

90
        return customRecords
3✔
91
}
92

93
// MergedCopy creates a copy of the records and merges them with the given
94
// records. If the same key is present in both sets, the value from the other
95
// records will be used.
96
func (c CustomRecords) MergedCopy(other CustomRecords) CustomRecords {
3✔
97
        copiedRecords := make(CustomRecords, len(c))
3✔
98
        maps.Copy(copiedRecords, c)
3✔
99
        maps.Copy(copiedRecords, other)
3✔
100

3✔
101
        return copiedRecords
3✔
102
}
3✔
103

104
// ExtendRecordProducers extends the given records slice with the custom
105
// records. The resultant records slice will be sorted if the given records
106
// slice contains TLV types greater than or equal to MinCustomRecordsTlvType.
107
func (c CustomRecords) ExtendRecordProducers(
108
        producers []tlv.RecordProducer) ([]tlv.RecordProducer, error) {
3✔
109

3✔
110
        // If the custom records are nil or empty, there is nothing to do.
3✔
111
        if len(c) == 0 {
6✔
112
                return producers, nil
3✔
113
        }
3✔
114

115
        // Validate the custom records.
116
        err := c.Validate()
3✔
117
        if err != nil {
3✔
UNCOV
118
                return nil, err
×
UNCOV
119
        }
×
120

121
        // Ensure that the existing records slice TLV types are not also present
122
        // in the custom records. If they are, the resultant extended records
123
        // slice would erroneously contain duplicate TLV types.
124
        for _, rp := range producers {
6✔
125
                record := rp.Record()
3✔
126
                recordTlvType := uint64(record.Type())
3✔
127

3✔
128
                _, foundDuplicateTlvType := c[recordTlvType]
3✔
129
                if foundDuplicateTlvType {
3✔
UNCOV
130
                        return nil, fmt.Errorf("custom records contains a TLV "+
×
UNCOV
131
                                "type that is already present in the "+
×
UNCOV
132
                                "existing records: %d", recordTlvType)
×
UNCOV
133
                }
×
134
        }
135

136
        // Convert the custom records map to a TLV record producer slice and
137
        // append them to the exiting records slice.
138
        customRecordProducers := RecordsAsProducers(tlv.MapToRecords(c))
3✔
139
        producers = append(producers, customRecordProducers...)
3✔
140

3✔
141
        // If the records slice which was given as an argument included TLV
3✔
142
        // values greater than or equal to the minimum custom records TLV type
3✔
143
        // we will sort the extended records slice to ensure that it is ordered
3✔
144
        // correctly.
3✔
145
        SortProducers(producers)
3✔
146

3✔
147
        return producers, nil
3✔
148
}
149

150
// RecordProducers returns a slice of record producers for the custom records.
151
func (c CustomRecords) RecordProducers() []tlv.RecordProducer {
3✔
152
        // If the custom records are nil or empty, return an empty slice.
3✔
153
        if len(c) == 0 {
6✔
154
                return nil
3✔
155
        }
3✔
156

157
        // Convert the custom records map to a TLV record producer slice.
158
        records := tlv.MapToRecords(c)
3✔
159

3✔
160
        return RecordsAsProducers(records)
3✔
161
}
162

163
// Serialize serializes the custom records into a byte slice.
164
func (c CustomRecords) Serialize() ([]byte, error) {
3✔
165
        records := tlv.MapToRecords(c)
3✔
166
        return EncodeRecords(records)
3✔
167
}
3✔
168

169
// SerializeTo serializes the custom records into the given writer.
170
func (c CustomRecords) SerializeTo(w io.Writer) error {
3✔
171
        records := tlv.MapToRecords(c)
3✔
172
        return EncodeRecordsTo(w, records)
3✔
173
}
3✔
174

175
// ProduceRecordsSorted converts a slice of record producers into a slice of
176
// records and then sorts it by type.
177
func ProduceRecordsSorted(recordProducers ...tlv.RecordProducer) []tlv.Record {
3✔
178
        records := fn.Map(
3✔
179
                recordProducers,
3✔
180
                func(producer tlv.RecordProducer) tlv.Record {
6✔
181
                        return producer.Record()
3✔
182
                },
3✔
183
        )
184

185
        // Ensure that the set of records are sorted before we attempt to
186
        // decode from the stream, to ensure they're canonical.
187
        tlv.SortRecords(records)
3✔
188

3✔
189
        return records
3✔
190
}
191

192
// SortProducers sorts the given record producers by their type.
193
func SortProducers(producers []tlv.RecordProducer) {
3✔
194
        sort.Slice(producers, func(i, j int) bool {
6✔
195
                recordI := producers[i].Record()
3✔
196
                recordJ := producers[j].Record()
3✔
197
                return recordI.Type() < recordJ.Type()
3✔
198
        })
3✔
199
}
200

201
// TlvMapToRecords converts a TLV map into a slice of records.
UNCOV
202
func TlvMapToRecords(tlvMap tlv.TypeMap) []tlv.Record {
×
UNCOV
203
        tlvMapGeneric := make(map[uint64][]byte)
×
UNCOV
204
        for k, v := range tlvMap {
×
UNCOV
205
                tlvMapGeneric[uint64(k)] = v
×
UNCOV
206
        }
×
207

UNCOV
208
        return tlv.MapToRecords(tlvMapGeneric)
×
209
}
210

211
// RecordsAsProducers converts a slice of records into a slice of record
212
// producers.
213
func RecordsAsProducers(records []tlv.Record) []tlv.RecordProducer {
3✔
214
        return fn.Map(records, func(record tlv.Record) tlv.RecordProducer {
6✔
215
                return &record
3✔
216
        })
3✔
217
}
218

219
// EncodeRecords encodes the given records into a byte slice.
220
func EncodeRecords(records []tlv.Record) ([]byte, error) {
3✔
221
        var buf bytes.Buffer
3✔
222
        if err := EncodeRecordsTo(&buf, records); err != nil {
3✔
223
                return nil, err
×
224
        }
×
225

226
        return buf.Bytes(), nil
3✔
227
}
228

229
// EncodeRecordsTo encodes the given records into the given writer.
230
func EncodeRecordsTo(w io.Writer, records []tlv.Record) error {
3✔
231
        tlvStream, err := tlv.NewStream(records...)
3✔
232
        if err != nil {
3✔
233
                return err
×
234
        }
×
235

236
        return tlvStream.Encode(w)
3✔
237
}
238

239
// DecodeRecords decodes the given byte slice into the given records and returns
240
// the rest as a TLV type map.
241
func DecodeRecords(r io.Reader,
242
        records ...tlv.Record) (tlv.TypeMap, error) {
3✔
243

3✔
244
        tlvStream, err := tlv.NewStream(records...)
3✔
245
        if err != nil {
3✔
246
                return nil, err
×
247
        }
×
248

249
        return tlvStream.DecodeWithParsedTypes(r)
3✔
250
}
251

252
// DecodeRecordsP2P decodes the given byte slice into the given records and
253
// returns the rest as a TLV type map. This function is identical to
254
// DecodeRecords except that the record size is capped at 65535.
255
func DecodeRecordsP2P(r *bytes.Reader,
256
        records ...tlv.Record) (tlv.TypeMap, error) {
3✔
257

3✔
258
        tlvStream, err := tlv.NewStream(records...)
3✔
259
        if err != nil {
3✔
260
                return nil, err
×
261
        }
×
262

263
        return tlvStream.DecodeWithParsedTypesP2P(r)
3✔
264
}
265

266
// AssertUniqueTypes asserts that the given records have unique types.
267
func AssertUniqueTypes(r []tlv.Record) error {
3✔
268
        seen := make(fn.Set[tlv.Type], len(r))
3✔
269
        for _, record := range r {
6✔
270
                t := record.Type()
3✔
271
                if seen.Contains(t) {
3✔
UNCOV
272
                        return fmt.Errorf("duplicate record type: %d", t)
×
UNCOV
273
                }
×
274
                seen.Add(t)
3✔
275
        }
276

277
        return nil
3✔
278
}
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