Skip to content

Commit ed34710

Browse files
committed
multi: use updated TLV SizeFunc signature
In this commit, we update our `tlv` package dep to the updated package code which returns an explicit error from any `SizeFunc` call-backs. This allows us to return explicit errors instead of silently logging and returning or panicking in various places.
1 parent 67a40c9 commit ed34710

21 files changed

+104
-85
lines changed

channeldb/invoices.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,7 @@ func putInvoice(invoices, invoiceIndex, payAddrIndex, addIndex kvdb.RwBucket,
12361236

12371237
// recordSize returns the amount of bytes this TLV record will occupy when
12381238
// encoded.
1239-
func ampRecordSize(a *invpkg.AMPInvoiceState) func() uint64 {
1239+
func ampRecordSize(a *invpkg.AMPInvoiceState) func() (uint64, error) {
12401240
var (
12411241
b bytes.Buffer
12421242
buf [8]byte
@@ -1246,13 +1246,13 @@ func ampRecordSize(a *invpkg.AMPInvoiceState) func() uint64 {
12461246
// file is checked into, so we'll simplify things and simply encode it
12471247
// ourselves then report the total amount of bytes used.
12481248
if err := ampStateEncoder(&b, a, &buf); err != nil {
1249-
// This should never error out, but we log it just in case it
1250-
// does.
1251-
log.Errorf("encoding the amp invoice state failed: %v", err)
1249+
return func() (uint64, error) {
1250+
return 0, err
1251+
}
12521252
}
12531253

1254-
return func() uint64 {
1255-
return uint64(len(b.Bytes()))
1254+
return func() (uint64, error) {
1255+
return uint64(len(b.Bytes())), nil
12561256
}
12571257
}
12581258

@@ -1389,7 +1389,10 @@ func serializeHtlcs(w io.Writer,
13891389
if htlc.AMP != nil {
13901390
setIDRecord := tlv.MakeDynamicRecord(
13911391
htlcAMPType, &htlc.AMP.Record,
1392-
htlc.AMP.Record.PayloadSize,
1392+
func() (uint64, error) {
1393+
return htlc.AMP.Record.PayloadSize(),
1394+
nil
1395+
},
13931396
record.AMPEncoder, record.AMPDecoder,
13941397
)
13951398
records = append(records, setIDRecord)
@@ -1892,7 +1895,7 @@ func ampStateEncoder(w io.Writer, val interface{}, buf *[8]byte) error {
18921895
tlv.MakeDynamicRecord(
18931896
ampStateCircuitKeysType,
18941897
&ampState.InvoiceKeys,
1895-
func() uint64 {
1898+
func() (uint64, error) {
18961899
// The record takes 8 bytes to
18971900
// encode the set of circuits,
18981901
// 8 bytes for the scid for the
@@ -1903,7 +1906,7 @@ func ampStateEncoder(w io.Writer, val interface{}, buf *[8]byte) error {
19031906
size := tlv.VarIntSize(numKeys)
19041907
dataSize := (numKeys * 16)
19051908

1906-
return size + dataSize
1909+
return size + dataSize, nil
19071910
},
19081911
encodeCircuitKeys, decodeCircuitKeys,
19091912
),
@@ -2080,7 +2083,9 @@ func deserializeHtlcs(r io.Reader) (map[models.CircuitKey]*invpkg.InvoiceHTLC,
20802083
tlv.MakePrimitiveRecord(htlcStateType, &state),
20812084
tlv.MakePrimitiveRecord(mppTotalAmtType, &mppTotalAmt),
20822085
tlv.MakeDynamicRecord(
2083-
htlcAMPType, amp, amp.PayloadSize,
2086+
htlcAMPType, amp, func() (uint64, error) {
2087+
return amp.PayloadSize(), nil
2088+
},
20842089
record.AMPEncoder, record.AMPDecoder,
20852090
),
20862091
tlv.MakePrimitiveRecord(htlcHashType, hash32),

channeldb/migration30/revocation_log.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ func (h *HTLCEntry) toTlvStream() (*tlv.Stream, error) {
180180

181181
return tlv.NewStream(
182182
tlv.MakeDynamicRecord(
183-
rHashType, &h.RHash, h.RHashLen,
183+
rHashType, &h.RHash, func() (uint64, error) {
184+
return h.RHashLen(), nil
185+
},
184186
RHashEncoder, RHashDecoder,
185187
),
186188
tlv.MakePrimitiveRecord(

channeldb/migration32/hop.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ func NewBlindingPointRecord(point **btcec.PublicKey) tlv.Record {
4040
func NewMetadataRecord(metadata *[]byte) tlv.Record {
4141
return tlv.MakeDynamicRecord(
4242
MetadataOnionType, metadata,
43-
func() uint64 {
44-
return uint64(len(*metadata))
43+
func() (uint64, error) {
44+
return uint64(len(*metadata)), nil
4545
},
4646
tlv.EVarBytes, tlv.DVarBytes,
4747
)
@@ -51,8 +51,8 @@ func NewMetadataRecord(metadata *[]byte) tlv.Record {
5151
// total_amount_msat for the final an onion payload within a blinded route.
5252
func NewTotalAmtMsatBlinded(amt *uint64) tlv.Record {
5353
return tlv.MakeDynamicRecord(
54-
TotalAmtMsatBlindedType, amt, func() uint64 {
55-
return tlv.SizeTUint64(*amt)
54+
TotalAmtMsatBlindedType, amt, func() (uint64, error) {
55+
return tlv.SizeTUint64(*amt), nil
5656
},
5757
tlv.ETUint64, tlv.DTUint64,
5858
)

channeldb/migration32/mission_control_store.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,16 @@ type paymentFailure struct {
193193
// Record returns a TLV record that can be used to encode/decode a
194194
// paymentFailure to/from a TLV stream.
195195
func (r *paymentFailure) Record() tlv.Record {
196-
recordSize := func() uint64 {
196+
recordSize := func() (uint64, error) {
197197
var (
198198
b bytes.Buffer
199199
buf [8]byte
200200
)
201201
if err := encodePaymentFailure(&b, r, &buf); err != nil {
202-
panic(err)
202+
return 0, err
203203
}
204204

205-
return uint64(len(b.Bytes()))
205+
return uint64(len(b.Bytes())), nil
206206
}
207207

208208
return tlv.MakeDynamicRecord(
@@ -282,16 +282,16 @@ type failureMessage struct {
282282
// Record returns a TLV record that can be used to encode/decode a list of
283283
// failureMessage to/from a TLV stream.
284284
func (r *failureMessage) Record() tlv.Record {
285-
recordSize := func() uint64 {
285+
recordSize := func() (uint64, error) {
286286
var (
287287
b bytes.Buffer
288288
buf [8]byte
289289
)
290290
if err := encodeFailureMessage(&b, r, &buf); err != nil {
291-
panic(err)
291+
return 0, err
292292
}
293293

294-
return uint64(len(b.Bytes()))
294+
return uint64(len(b.Bytes())), nil
295295
}
296296

297297
return tlv.MakeDynamicRecord(
@@ -396,16 +396,16 @@ type mcRoute struct {
396396
// Record returns a TLV record that can be used to encode/decode an mcRoute
397397
// to/from a TLV stream.
398398
func (r *mcRoute) Record() tlv.Record {
399-
recordSize := func() uint64 {
399+
recordSize := func() (uint64, error) {
400400
var (
401401
b bytes.Buffer
402402
buf [8]byte
403403
)
404404
if err := encodeMCRoute(&b, r, &buf); err != nil {
405-
panic(err)
405+
return 0, err
406406
}
407407

408-
return uint64(len(b.Bytes()))
408+
return uint64(len(b.Bytes())), nil
409409
}
410410

411411
return tlv.MakeDynamicRecord(
@@ -442,16 +442,16 @@ type mcHops []*mcHop
442442
// Record returns a TLV record that can be used to encode/decode a list of
443443
// mcHop to/from a TLV stream.
444444
func (h *mcHops) Record() tlv.Record {
445-
recordSize := func() uint64 {
445+
recordSize := func() (uint64, error) {
446446
var (
447447
b bytes.Buffer
448448
buf [8]byte
449449
)
450450
if err := encodeMCHops(&b, h, &buf); err != nil {
451-
panic(err)
451+
return 0, err
452452
}
453453

454-
return uint64(len(b.Bytes()))
454+
return uint64(len(b.Bytes())), nil
455455
}
456456

457457
return tlv.MakeDynamicRecord(

channeldb/migration32/route.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ type MPP struct {
186186
func (r *MPP) Record() tlv.Record {
187187
// Fixed-size, 32 byte payment address followed by truncated 64-bit
188188
// total msat.
189-
size := func() uint64 {
190-
return 32 + tlv.SizeTUint64(uint64(r.totalMsat))
189+
size := func() (uint64, error) {
190+
return 32 + tlv.SizeTUint64(uint64(r.totalMsat)), nil
191191
}
192192

193193
return tlv.MakeDynamicRecord(
@@ -297,7 +297,9 @@ func AMPDecoder(r io.Reader, val interface{}, buf *[8]byte, l uint64) error {
297297
// Record returns a tlv.Record that can be used to encode or decode this record.
298298
func (a *AMP) Record() tlv.Record {
299299
return tlv.MakeDynamicRecord(
300-
AMPOnionType, a, a.PayloadSize, AMPEncoder, AMPDecoder,
300+
AMPOnionType, a, func() (uint64, error) {
301+
return a.PayloadSize(), nil
302+
}, AMPEncoder, AMPDecoder,
301303
)
302304
}
303305

channeldb/revocation_log.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ func (s *SparsePayHash) Record() tlv.Record {
6868
// We use a zero for the type here, as this'll be used along with the
6969
// RecordT type.
7070
return tlv.MakeDynamicRecord(
71-
0, s, s.hashLen,
72-
sparseHashEncoder, sparseHashDecoder,
71+
0, s, func() (uint64, error) {
72+
return s.hashLen(), nil
73+
}, sparseHashEncoder, sparseHashDecoder,
7374
)
7475
}
7576

contractcourt/taproot_briefcase.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func newResolverCtrlBlocks() resolverCtrlBlocks {
140140
}
141141

142142
// recordSize returns the size of the record in bytes.
143-
func (r *resolverCtrlBlocks) recordSize() uint64 {
143+
func (r *resolverCtrlBlocks) recordSize() (uint64, error) {
144144
// Each record will be serialized as: <num_total_records> || <record>,
145145
// where <record> is serialized as: <resolver_key> || <length> ||
146146
// <ctrl_block>.
@@ -154,7 +154,7 @@ func (r *resolverCtrlBlocks) recordSize() uint64 {
154154
recordSize += uint64(len(ctrlBlock))
155155
}
156156

157-
return recordSize
157+
return recordSize, nil
158158
}
159159

160160
// Encode encodes the control blocks into the target writer.
@@ -411,16 +411,16 @@ func (c *ctrlBlocks) DecodeRecords() []tlv.Record {
411411
// Record returns a TLV record that can be used to encode/decode the control
412412
// blocks. type from a given TLV stream.
413413
func (c *ctrlBlocks) Record() tlv.Record {
414-
recordSize := func() uint64 {
414+
recordSize := func() (uint64, error) {
415415
var (
416416
b bytes.Buffer
417417
buf [8]byte
418418
)
419419
if err := ctrlBlockEncoder(&b, c, &buf); err != nil {
420-
panic(err)
420+
return 0, err
421421
}
422422

423-
return uint64(len(b.Bytes()))
423+
return uint64(len(b.Bytes())), nil
424424
}
425425

426426
return tlv.MakeDynamicRecord(
@@ -459,7 +459,7 @@ func newHtlcTapTweaks() htlcTapTweaks {
459459
}
460460

461461
// recordSize returns the size of the record in bytes.
462-
func (h *htlcTapTweaks) recordSize() uint64 {
462+
func (h *htlcTapTweaks) recordSize() (uint64, error) {
463463
// Each record will be serialized as: <num_tweaks> || <tweak>, where
464464
// <tweak> is serialized as: <resolver_key> || <tweak>.
465465
numTweaks := uint64(len(*h))
@@ -473,7 +473,7 @@ func (h *htlcTapTweaks) recordSize() uint64 {
473473
recordSize += 32
474474
}
475475

476-
return recordSize
476+
return recordSize, nil
477477
}
478478

479479
// Encode encodes the tap tweaks into the target writer.
@@ -659,16 +659,16 @@ func (t *tapTweaks) DecodeRecords() []tlv.Record {
659659
// Record returns a TLV record that can be used to encode/decode the tap
660660
// tweaks.
661661
func (t *tapTweaks) Record() tlv.Record {
662-
recordSize := func() uint64 {
662+
recordSize := func() (uint64, error) {
663663
var (
664664
b bytes.Buffer
665665
buf [8]byte
666666
)
667667
if err := tapTweaksEncoder(&b, t, &buf); err != nil {
668-
panic(err)
668+
return 0, err
669669
}
670670

671-
return uint64(len(b.Bytes()))
671+
return uint64(len(b.Bytes())), nil
672672
}
673673

674674
return tlv.MakeDynamicRecord(
@@ -786,16 +786,16 @@ func htlcAuxBlobsDecoder(r io.Reader, val any, _ *[8]byte,
786786

787787
// Record returns a tlv.Record for the htlcAuxBlobs struct.
788788
func (h *htlcAuxBlobs) Record() tlv.Record {
789-
recordSize := func() uint64 {
789+
recordSize := func() (uint64, error) {
790790
var (
791791
b bytes.Buffer
792792
buf [8]byte
793793
)
794794
if err := htlcAuxBlobsEncoder(&b, h, &buf); err != nil {
795-
panic(err)
795+
return 0, err
796796
}
797797

798-
return uint64(len(b.Bytes()))
798+
return uint64(len(b.Bytes())), nil
799799
}
800800

801801
return tlv.MakeDynamicRecord(

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ replace github.com/gogo/protobuf => github.com/gogo/protobuf v1.3.2
210210
// allows us to specify that as an option.
211211
replace google.golang.org/protobuf => github.com/lightninglabs/protobuf-go-hex-display v1.30.0-hex-display
212212

213+
replace (
214+
github.com/btcsuite/btcwallet => github.com/ellemouton/btcwallet v0.11.1-0.20250507091139-3e08ed577fae
215+
github.com/lightningnetwork/lnd/tlv => github.com/ellemouton/lnd/tlv v0.0.0-20250507085853-2c656b4cabf1
216+
)
217+
213218
// If you change this please also update docs/INSTALL.md and GO_VERSION in
214219
// Makefile (then run `make lint` to see where else it needs to be updated as
215220
// well).

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ github.com/btcsuite/btclog v0.0.0-20241003133417-09c4e92e319c/go.mod h1:w7xnGOhw
6262
github.com/btcsuite/btclog/v2 v2.0.1-0.20250110154127-3ae4bf1cb318 h1:oCjIcinPt7XQ644MP/22JcjYEC84qRc3bRBH0d7Hhd4=
6363
github.com/btcsuite/btclog/v2 v2.0.1-0.20250110154127-3ae4bf1cb318/go.mod h1:XItGUfVOxotJL8kkuk2Hj3EVow5KCugXl3wWfQ6K0AE=
6464
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
65-
github.com/btcsuite/btcwallet v0.16.13 h1:JGu+wrihQ0I00ODb3w92JtBPbrHxZhbcvU01O+e+lKw=
66-
github.com/btcsuite/btcwallet v0.16.13/go.mod h1:H6dfoZcWPonM2wbVsR2ZBY0PKNZKdQyLAmnX8vL9JFA=
6765
github.com/btcsuite/btcwallet/wallet/txauthor v1.3.5 h1:Rr0njWI3r341nhSPesKQ2JF+ugDSzdPoeckS75SeDZk=
6866
github.com/btcsuite/btcwallet/wallet/txauthor v1.3.5/go.mod h1:+tXJ3Ym0nlQc/iHSwW1qzjmPs3ev+UVWMbGgfV1OZqU=
6967
github.com/btcsuite/btcwallet/wallet/txrules v1.2.2 h1:YEO+Lx1ZJJAtdRrjuhXjWrYsmAk26wLTlNzxt2q0lhk=
@@ -141,6 +139,10 @@ github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4
141139
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
142140
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
143141
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
142+
github.com/ellemouton/btcwallet v0.11.1-0.20250507091139-3e08ed577fae h1:ePIIv1Ie52iljTZv8w9UcfMR4LNtPeFF4XRpMG9o7c8=
143+
github.com/ellemouton/btcwallet v0.11.1-0.20250507091139-3e08ed577fae/go.mod h1:cNhnzc9oE0KJbs5cV6Pn0RvrtSBh4HPI4kVqPPoB9gg=
144+
github.com/ellemouton/lnd/tlv v0.0.0-20250507085853-2c656b4cabf1 h1:Uze20aelFhHkl+mqLPrFWKaH1QuUvrOLy10cIpUapa4=
145+
github.com/ellemouton/lnd/tlv v0.0.0-20250507085853-2c656b4cabf1/go.mod h1:pJuiBj1ecr1WWLOtcZ+2+hu9Ey25aJWFIsjmAoPPnmc=
144146
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
145147
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
146148
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
@@ -379,8 +381,6 @@ github.com/lightningnetwork/lnd/sqldb v1.0.9 h1:7OHi+Hui823mB/U9NzCdlZTAGSVdDCbj
379381
github.com/lightningnetwork/lnd/sqldb v1.0.9/go.mod h1:OG09zL/PHPaBJefp4HsPz2YLUJ+zIQHbpgCtLnOx8I4=
380382
github.com/lightningnetwork/lnd/ticker v1.1.1 h1:J/b6N2hibFtC7JLV77ULQp++QLtCwT6ijJlbdiZFbSM=
381383
github.com/lightningnetwork/lnd/ticker v1.1.1/go.mod h1:waPTRAAcwtu7Ji3+3k+u/xH5GHovTsCoSVpho0KDvdA=
382-
github.com/lightningnetwork/lnd/tlv v1.3.0 h1:exS/KCPEgpOgviIttfiXAPaUqw2rHQrnUOpP7HPBPiY=
383-
github.com/lightningnetwork/lnd/tlv v1.3.0/go.mod h1:pJuiBj1ecr1WWLOtcZ+2+hu9Ey25aJWFIsjmAoPPnmc=
384384
github.com/lightningnetwork/lnd/tor v1.1.6 h1:WHUumk7WgU6BUFsqHuqszI9P6nfhMeIG+rjJBlVE6OE=
385385
github.com/lightningnetwork/lnd/tor v1.1.6/go.mod h1:qSRB8llhAK+a6kaTPWOLLXSZc6Hg8ZC0mq1sUQ/8JfI=
386386
github.com/ltcsuite/ltcd v0.0.0-20190101042124-f37f8bf35796 h1:sjOGyegMIhvgfq5oaue6Td+hxZuf3tDC8lAPrFldqFw=

lnwire/channel_type.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ const (
1717
type ChannelType RawFeatureVector
1818

1919
// featureBitLen returns the length in bytes of the encoded feature bits.
20-
func (c ChannelType) featureBitLen() uint64 {
20+
func (c ChannelType) featureBitLen() (uint64, error) {
2121
fv := RawFeatureVector(c)
22+
2223
return fv.sizeFunc()
2324
}
2425

0 commit comments

Comments
 (0)