lnwallet+channeldb: use BigSizeT to save space

This commit is contained in:
yyforyongyu committed 2025-06-06 19:55:58 +08:00
1 parent fc906f2a65
commit f5ffe82280
3 files changed
+36 -28

No files matched your search

+6 -4
View File
@@ -165,7 +165,7 @@ type HTLCEntry struct {
CustomBlob tlv.OptionalRecordT[tlv.TlvType5, tlv.Blob]
// HtlcIndex is the index of the HTLC in the channel.
HtlcIndex tlv.OptionalRecordT[tlv.TlvType6, uint16]
HtlcIndex tlv.OptionalRecordT[tlv.TlvType6, tlv.BigSizeT[uint64]]
}
// toTlvStream converts an HTLCEntry record into a tlv representation.
@@ -182,7 +182,9 @@ func (h *HTLCEntry) toTlvStream() (*tlv.Stream, error) {
records = append(records, r.Record())
})
h.HtlcIndex.WhenSome(func(r tlv.RecordT[tlv.TlvType6, uint16]) {
h.HtlcIndex.WhenSome(func(r tlv.RecordT[tlv.TlvType6,
tlv.BigSizeT[uint64]]) {
records = append(records, r.Record())
})
@@ -207,8 +209,8 @@ func NewHTLCEntryFromHTLC(htlc HTLC) (*HTLCEntry, error) {
Amt: tlv.NewRecordT[tlv.TlvType4](
tlv.NewBigSizeT(htlc.Amt.ToSatoshis()),
),
HtlcIndex: tlv.SomeRecordT(tlv.NewPrimitiveRecord[tlv.TlvType6](
uint16(htlc.HtlcIndex),
HtlcIndex: tlv.SomeRecordT(tlv.NewRecordT[tlv.TlvType6](
tlv.NewBigSizeT(htlc.HtlcIndex),
)),
}
+8 -8
View File
@@ -59,13 +59,13 @@ var (
CustomBlob: tlv.SomeRecordT(
tlv.NewPrimitiveRecord[tlv.TlvType5](blobBytes),
),
HtlcIndex: tlv.SomeRecordT(
tlv.NewPrimitiveRecord[tlv.TlvType6, uint16](0x33),
),
HtlcIndex: tlv.SomeRecordT(tlv.NewRecordT[tlv.TlvType6](
tlv.NewBigSizeT(uint64(0x33)),
)),
}
testHTLCEntryBytes = []byte{
// Body length 45.
0x2d,
// Body length 44.
0x2c,
// Rhash tlv.
0x0, 0x0,
// RefundTimeout tlv.
@@ -80,7 +80,7 @@ var (
0x5, 0x11, 0xfe, 0x00, 0x01, 0x00, 0x01, 0x0b, 0x63, 0x75, 0x73,
0x74, 0x6f, 0x6d, 0x20, 0x64, 0x61, 0x74, 0x61,
// HLTC index tlv.
0x6, 0x2, 0x0, 0x33,
0x6, 0x1, 0x33,
}
testHTLCEntryHash = HTLCEntry{
@@ -133,7 +133,7 @@ var (
OutputIndex: int32(testHTLCEntry.OutputIndex.Val),
HtlcIndex: uint64(
testHTLCEntry.HtlcIndex.ValOpt().
UnsafeFromSome(),
UnsafeFromSome().Int(),
),
Incoming: testHTLCEntry.Incoming.Val,
Amt: lnwire.NewMSatFromSatoshis(
@@ -303,7 +303,7 @@ func TestSerializeHTLCEntries(t *testing.T) {
partialBytes := testHTLCEntryBytes[3:]
// Write the total length and RHash tlv.
expectedBytes := []byte{0x4d, 0x0, 0x20}
expectedBytes := []byte{0x4c, 0x0, 0x20}
expectedBytes = append(expectedBytes, rHashBytes...)
// Append the rest.
+22 -16
View File
@@ -2340,18 +2340,21 @@ func createHtlcRetribution(chanState *channeldb.OpenChannel,
// We'll generate the original second level witness script now, as
// we'll need it if we're revoking an HTLC output on the remote
// commitment transaction, and *they* go to the second level.
//nolint:ll
secondLevelAuxLeaf := fn.FlatMapOption(
func(l CommitAuxLeaves) fn.Option[input.AuxTapLeaf] {
return fn.MapOption(func(val uint16) input.AuxTapLeaf {
idx := input.HtlcIndex(val)
return fn.MapOption(
func(val tlv.BigSizeT[uint64]) input.AuxTapLeaf {
idx := val.Int()
if htlc.Incoming.Val {
leaves := l.IncomingHtlcLeaves[idx]
return leaves.SecondLevelLeaf
}
if htlc.Incoming.Val {
leaves := l.IncomingHtlcLeaves[idx]
return leaves.SecondLevelLeaf
}
return l.OutgoingHtlcLeaves[idx].SecondLevelLeaf
})(htlc.HtlcIndex.ValOpt())
return l.OutgoingHtlcLeaves[idx].SecondLevelLeaf
},
)(htlc.HtlcIndex.ValOpt())
},
)(auxLeaves)
secondLevelScript, err := SecondLevelHtlcScript(
@@ -2368,18 +2371,21 @@ func createHtlcRetribution(chanState *channeldb.OpenChannel,
// HTLC script. Otherwise, is this was an outgoing HTLC that we sent,
// then from the PoV of the remote commitment state, they're the
// receiver of this HTLC.
//nolint:ll
htlcLeaf := fn.FlatMapOption(
func(l CommitAuxLeaves) fn.Option[input.AuxTapLeaf] {
return fn.MapOption(func(val uint16) input.AuxTapLeaf {
idx := input.HtlcIndex(val)
return fn.MapOption(
func(val tlv.BigSizeT[uint64]) input.AuxTapLeaf {
idx := val.Int()
if htlc.Incoming.Val {
leaves := l.IncomingHtlcLeaves[idx]
return leaves.AuxTapLeaf
}
if htlc.Incoming.Val {
leaves := l.IncomingHtlcLeaves[idx]
return leaves.AuxTapLeaf
}
return l.OutgoingHtlcLeaves[idx].AuxTapLeaf
})(htlc.HtlcIndex.ValOpt())
return l.OutgoingHtlcLeaves[idx].AuxTapLeaf
},
)(htlc.HtlcIndex.ValOpt())
},
)(auxLeaves)
scriptInfo, err := genHtlcScript(