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

lightningnetwork / lnd / 14885280210

07 May 2025 01:59PM UTC coverage: 58.038% (-11.0%) from 68.992%
14885280210

Pull #9789

github

web-flow
Merge b72813120 into 67a40c90a
Pull Request #9789: multi: use updated TLV SizeFunc signature

3 of 6 new or added lines in 2 files covered. (50.0%)

29137 existing lines in 453 files now uncovered.

96491 of 166256 relevant lines covered (58.04%)

1.22 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) {
2✔
27
        // Make comparisons in unit tests easy by returning nil if the map is
2✔
28
        // empty.
2✔
29
        if len(tlvMap) == 0 {
4✔
30
                return nil, nil
2✔
31
        }
2✔
32

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

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

45
        return customRecords, nil
2✔
46
}
47

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

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

60
        return NewCustomRecords(typeMap)
2✔
61
}
62

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

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

76
        return nil
2✔
77
}
78

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

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

90
        return customRecords
2✔
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 {
2✔
97
        copiedRecords := make(CustomRecords, len(c))
2✔
98
        maps.Copy(copiedRecords, c)
2✔
99
        maps.Copy(copiedRecords, other)
2✔
100

2✔
101
        return copiedRecords
2✔
102
}
2✔
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) {
2✔
109

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

115
        // Validate the custom records.
116
        err := c.Validate()
2✔
117
        if err != nil {
2✔
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 {
4✔
125
                record := rp.Record()
2✔
126
                recordTlvType := uint64(record.Type())
2✔
127

2✔
128
                _, foundDuplicateTlvType := c[recordTlvType]
2✔
129
                if foundDuplicateTlvType {
2✔
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))
2✔
139
        producers = append(producers, customRecordProducers...)
2✔
140

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

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

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

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

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

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

169
// SerializeTo serializes the custom records into the given writer.
170
func (c CustomRecords) SerializeTo(w io.Writer) error {
2✔
171
        records := tlv.MapToRecords(c)
2✔
172
        return EncodeRecordsTo(w, records)
2✔
173
}
2✔
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 {
2✔
178
        records := fn.Map(
2✔
179
                recordProducers,
2✔
180
                func(producer tlv.RecordProducer) tlv.Record {
4✔
181
                        return producer.Record()
2✔
182
                },
2✔
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)
2✔
188

2✔
189
        return records
2✔
190
}
191

192
// SortProducers sorts the given record producers by their type.
193
func SortProducers(producers []tlv.RecordProducer) {
2✔
194
        sort.Slice(producers, func(i, j int) bool {
4✔
195
                recordI := producers[i].Record()
2✔
196
                recordJ := producers[j].Record()
2✔
197
                return recordI.Type() < recordJ.Type()
2✔
198
        })
2✔
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 {
2✔
214
        return fn.Map(records, func(record tlv.Record) tlv.RecordProducer {
4✔
215
                return &record
2✔
216
        })
2✔
217
}
218

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

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

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

236
        return tlvStream.Encode(w)
2✔
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) {
2✔
243

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

249
        return tlvStream.DecodeWithParsedTypes(r)
2✔
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) {
2✔
257

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

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

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

277
        return nil
2✔
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