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

lightningnetwork / lnd / 15838907453

24 Jun 2025 01:26AM UTC coverage: 57.079% (-11.1%) from 68.172%
15838907453

Pull #9982

github

web-flow
Merge e42780be2 into 45c15646c
Pull Request #9982: lnwire+lnwallet: add LocalNonces field for splice nonce coordination w/ taproot channels

103 of 167 new or added lines in 5 files covered. (61.68%)

30191 existing lines in 463 files now uncovered.

96331 of 168768 relevant lines covered (57.08%)

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

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

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

45
        return customRecords, nil
1✔
46
}
47

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

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

60
        return NewCustomRecords(typeMap)
1✔
61
}
62

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

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

76
        return nil
1✔
77
}
78

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1✔
189
        return records
1✔
190
}
191

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

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

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

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

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

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

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

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

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

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

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