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

lightningnetwork / lnd / 11170835610

03 Oct 2024 10:41PM UTC coverage: 49.188% (-9.6%) from 58.738%
11170835610

push

github

web-flow
Merge pull request #9154 from ziggie1984/master

multi: bump btcd version.

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

26110 existing lines in 428 files now uncovered.

97359 of 197934 relevant lines covered (49.19%)

1.04 hits per line

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

75.16
/lnwire/custom_records.go
1
package lnwire
2

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

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

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

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

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

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

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

44
        return customRecords, nil
2✔
45
}
46

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

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

59
        return NewCustomRecords(typeMap)
2✔
60
}
61

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

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

75
        return nil
2✔
76
}
77

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

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

89
        return customRecords
2✔
90
}
91

92
// MergedCopy creates a copy of the records and merges them with the given
93
// records. If the same key is present in both sets, the value from the other
94
// records will be used.
95
func (c CustomRecords) MergedCopy(other CustomRecords) CustomRecords {
2✔
96
        copiedRecords := make(CustomRecords, len(c))
2✔
97
        for k, v := range c {
4✔
98
                copiedRecords[k] = v
2✔
99
        }
2✔
100

101
        for k, v := range other {
4✔
102
                copiedRecords[k] = v
2✔
103
        }
2✔
104

105
        return copiedRecords
2✔
106
}
107

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

2✔
114
        // If the custom records are nil or empty, there is nothing to do.
2✔
115
        if len(c) == 0 {
4✔
116
                return producers, nil
2✔
117
        }
2✔
118

119
        // Validate the custom records.
120
        err := c.Validate()
2✔
121
        if err != nil {
2✔
UNCOV
122
                return nil, err
×
UNCOV
123
        }
×
124

125
        // Ensure that the existing records slice TLV types are not also present
126
        // in the custom records. If they are, the resultant extended records
127
        // slice would erroneously contain duplicate TLV types.
128
        for _, rp := range producers {
2✔
UNCOV
129
                record := rp.Record()
×
UNCOV
130
                recordTlvType := uint64(record.Type())
×
UNCOV
131

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

140
        // Convert the custom records map to a TLV record producer slice and
141
        // append them to the exiting records slice.
142
        customRecordProducers := RecordsAsProducers(tlv.MapToRecords(c))
2✔
143
        producers = append(producers, customRecordProducers...)
2✔
144

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

2✔
151
        return producers, nil
2✔
152
}
153

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

161
        // Convert the custom records map to a TLV record producer slice.
162
        records := tlv.MapToRecords(c)
2✔
163

2✔
164
        return RecordsAsProducers(records)
2✔
165
}
166

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

173
// SerializeTo serializes the custom records into the given writer.
174
func (c CustomRecords) SerializeTo(w io.Writer) error {
2✔
175
        records := tlv.MapToRecords(c)
2✔
176
        return EncodeRecordsTo(w, records)
2✔
177
}
2✔
178

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

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

2✔
190
        return records
2✔
191
}
192

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

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

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

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

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

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

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

237
        return tlvStream.Encode(w)
2✔
238
}
239

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

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

250
        return tlvStream.DecodeWithParsedTypes(r)
2✔
251
}
252

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

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

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

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

278
        return nil
2✔
279
}
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