channeldb: add HtlcIndex to HTLCEntry

This may be useful for custom channel types that base everything off the index (a global value) rather than the output index (can change with each state).
This commit is contained in:
Olaoluwa Osuntokun
2024-04-01 17:44:14 -07:00
committed by Oliver Gugger
parent d5f595e641
commit 2cf38540e0
2 changed files with 22 additions and 11 deletions

View File

@ -155,19 +155,17 @@ type HTLCEntry struct {
// Incoming denotes whether we're the receiver or the sender of this // Incoming denotes whether we're the receiver or the sender of this
// HTLC. // HTLC.
//
// NOTE: this field is the memory representation of the field
// incomingUint.
Incoming tlv.RecordT[tlv.TlvType3, bool] Incoming tlv.RecordT[tlv.TlvType3, bool]
// Amt is the amount of satoshis this HTLC escrows. // Amt is the amount of satoshis this HTLC escrows.
//
// NOTE: this field is the memory representation of the field amtUint.
Amt tlv.RecordT[tlv.TlvType4, tlv.BigSizeT[btcutil.Amount]] Amt tlv.RecordT[tlv.TlvType4, tlv.BigSizeT[btcutil.Amount]]
// CustomBlob is an optional blob that can be used to store information // CustomBlob is an optional blob that can be used to store information
// specific to revocation handling for a custom channel type. // specific to revocation handling for a custom channel type.
CustomBlob tlv.OptionalRecordT[tlv.TlvType5, tlv.Blob] CustomBlob tlv.OptionalRecordT[tlv.TlvType5, tlv.Blob]
// HtlcIndex is the index of the HTLC in the channel.
HtlcIndex tlv.RecordT[tlv.TlvType6, uint16]
} }
// toTlvStream converts an HTLCEntry record into a tlv representation. // toTlvStream converts an HTLCEntry record into a tlv representation.
@ -178,12 +176,15 @@ func (h *HTLCEntry) toTlvStream() (*tlv.Stream, error) {
h.OutputIndex.Record(), h.OutputIndex.Record(),
h.Incoming.Record(), h.Incoming.Record(),
h.Amt.Record(), h.Amt.Record(),
h.HtlcIndex.Record(),
} }
h.CustomBlob.WhenSome(func(r tlv.RecordT[tlv.TlvType5, tlv.Blob]) { h.CustomBlob.WhenSome(func(r tlv.RecordT[tlv.TlvType5, tlv.Blob]) {
records = append(records, r.Record()) records = append(records, r.Record())
}) })
tlv.SortRecords(records)
return tlv.NewStream(records...) return tlv.NewStream(records...)
} }
@ -203,6 +204,9 @@ func NewHTLCEntryFromHTLC(htlc HTLC) *HTLCEntry {
Amt: tlv.NewRecordT[tlv.TlvType4]( Amt: tlv.NewRecordT[tlv.TlvType4](
tlv.NewBigSizeT(htlc.Amt.ToSatoshis()), tlv.NewBigSizeT(htlc.Amt.ToSatoshis()),
), ),
HtlcIndex: tlv.NewPrimitiveRecord[tlv.TlvType6](
uint16(htlc.HtlcIndex),
),
} }
if len(htlc.ExtraData) != 0 { if len(htlc.ExtraData) != 0 {
@ -512,6 +516,7 @@ func deserializeHTLCEntries(r io.Reader) ([]*HTLCEntry, error) {
htlc.Incoming.Record(), htlc.Incoming.Record(),
htlc.Amt.Record(), htlc.Amt.Record(),
customBlob.Record(), customBlob.Record(),
htlc.HtlcIndex.Record(),
} }
tlvStream, err := tlv.NewStream(records...) tlvStream, err := tlv.NewStream(records...)

View File

@ -52,10 +52,11 @@ var (
CustomBlob: tlv.SomeRecordT( CustomBlob: tlv.SomeRecordT(
tlv.NewPrimitiveRecord[tlv.TlvType5](blobBytes), tlv.NewPrimitiveRecord[tlv.TlvType5](blobBytes),
), ),
HtlcIndex: tlv.NewPrimitiveRecord[tlv.TlvType6, uint16](3),
} }
testHTLCEntryBytes = []byte{ testHTLCEntryBytes = []byte{
// Body length 28. // Body length 32.
0x1c, 0x20,
// Rhash tlv. // Rhash tlv.
0x0, 0x0, 0x0, 0x0,
// RefundTimeout tlv. // RefundTimeout tlv.
@ -68,6 +69,8 @@ var (
0x4, 0x5, 0xfe, 0x0, 0xf, 0x42, 0x40, 0x4, 0x5, 0xfe, 0x0, 0xf, 0x42, 0x40,
// Custom blob tlv. // Custom blob tlv.
0x5, 0x4, 0x1, 0x2, 0x3, 0x4, 0x5, 0x4, 0x1, 0x2, 0x3, 0x4,
// HLTC index tlv.
0x6, 0x2, 0x0, 0x03,
} }
testHTLCEntryHash = HTLCEntry{ testHTLCEntryHash = HTLCEntry{
@ -86,8 +89,8 @@ var (
), ),
} }
testHTLCEntryHashBytes = []byte{ testHTLCEntryHashBytes = []byte{
// Body length 54. // Body length 58.
0x36, 0x3a,
// Rhash tlv. // Rhash tlv.
0x0, 0x20, 0x0, 0x20,
0x33, 0x44, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x44, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00,
@ -102,6 +105,8 @@ var (
0x3, 0x1, 0x1, 0x3, 0x1, 0x1,
// Amt tlv. // Amt tlv.
0x4, 0x5, 0xfe, 0x0, 0xf, 0x42, 0x40, 0x4, 0x5, 0xfe, 0x0, 0xf, 0x42, 0x40,
// HLTC index tlv.
0x6, 0x2, 0x0, 0x00,
} }
localBalance = lnwire.MilliSatoshi(9000) localBalance = lnwire.MilliSatoshi(9000)
@ -118,6 +123,7 @@ var (
Htlcs: []HTLC{{ Htlcs: []HTLC{{
RefundTimeout: testHTLCEntry.RefundTimeout.Val, RefundTimeout: testHTLCEntry.RefundTimeout.Val,
OutputIndex: int32(testHTLCEntry.OutputIndex.Val), OutputIndex: int32(testHTLCEntry.OutputIndex.Val),
HtlcIndex: uint64(testHTLCEntry.HtlcIndex.Val),
Incoming: testHTLCEntry.Incoming.Val, Incoming: testHTLCEntry.Incoming.Val,
Amt: lnwire.NewMSatFromSatoshis( Amt: lnwire.NewMSatFromSatoshis(
testHTLCEntry.Amt.Val.Int(), testHTLCEntry.Amt.Val.Int(),
@ -284,7 +290,7 @@ func TestSerializeHTLCEntries(t *testing.T) {
partialBytes := testHTLCEntryBytes[3:] partialBytes := testHTLCEntryBytes[3:]
// Write the total length and RHash tlv. // Write the total length and RHash tlv.
expectedBytes := []byte{0x3c, 0x0, 0x20} expectedBytes := []byte{0x40, 0x0, 0x20}
expectedBytes = append(expectedBytes, rHashBytes...) expectedBytes = append(expectedBytes, rHashBytes...)
// Append the rest. // Append the rest.
@ -399,7 +405,7 @@ func TestDeserializeHTLCEntries(t *testing.T) {
partialBytes := testHTLCEntryBytes[3:] partialBytes := testHTLCEntryBytes[3:]
// Write the total length and RHash tlv. // Write the total length and RHash tlv.
testBytes := append([]byte{0x3c, 0x0, 0x20}, rHashBytes...) testBytes := append([]byte{0x40, 0x0, 0x20}, rHashBytes...)
// Append the rest. // Append the rest.
testBytes = append(testBytes, partialBytes...) testBytes = append(testBytes, partialBytes...)