From 2cf38540e00e266e5b5122d7ba02df434b051f73 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 1 Apr 2024 17:44:14 -0700 Subject: [PATCH] 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). --- channeldb/revocation_log.go | 15 ++++++++++----- channeldb/revocation_log_test.go | 18 ++++++++++++------ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/channeldb/revocation_log.go b/channeldb/revocation_log.go index 0b51400c4..4334ec64d 100644 --- a/channeldb/revocation_log.go +++ b/channeldb/revocation_log.go @@ -155,19 +155,17 @@ type HTLCEntry struct { // Incoming denotes whether we're the receiver or the sender of this // HTLC. - // - // NOTE: this field is the memory representation of the field - // incomingUint. Incoming tlv.RecordT[tlv.TlvType3, bool] // 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]] // CustomBlob is an optional blob that can be used to store information // specific to revocation handling for a custom channel type. 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. @@ -178,12 +176,15 @@ func (h *HTLCEntry) toTlvStream() (*tlv.Stream, error) { h.OutputIndex.Record(), h.Incoming.Record(), h.Amt.Record(), + h.HtlcIndex.Record(), } h.CustomBlob.WhenSome(func(r tlv.RecordT[tlv.TlvType5, tlv.Blob]) { records = append(records, r.Record()) }) + tlv.SortRecords(records) + return tlv.NewStream(records...) } @@ -203,6 +204,9 @@ func NewHTLCEntryFromHTLC(htlc HTLC) *HTLCEntry { Amt: tlv.NewRecordT[tlv.TlvType4]( tlv.NewBigSizeT(htlc.Amt.ToSatoshis()), ), + HtlcIndex: tlv.NewPrimitiveRecord[tlv.TlvType6]( + uint16(htlc.HtlcIndex), + ), } if len(htlc.ExtraData) != 0 { @@ -512,6 +516,7 @@ func deserializeHTLCEntries(r io.Reader) ([]*HTLCEntry, error) { htlc.Incoming.Record(), htlc.Amt.Record(), customBlob.Record(), + htlc.HtlcIndex.Record(), } tlvStream, err := tlv.NewStream(records...) diff --git a/channeldb/revocation_log_test.go b/channeldb/revocation_log_test.go index 76b5d8b78..d21c41fbf 100644 --- a/channeldb/revocation_log_test.go +++ b/channeldb/revocation_log_test.go @@ -52,10 +52,11 @@ var ( CustomBlob: tlv.SomeRecordT( tlv.NewPrimitiveRecord[tlv.TlvType5](blobBytes), ), + HtlcIndex: tlv.NewPrimitiveRecord[tlv.TlvType6, uint16](3), } testHTLCEntryBytes = []byte{ - // Body length 28. - 0x1c, + // Body length 32. + 0x20, // Rhash tlv. 0x0, 0x0, // RefundTimeout tlv. @@ -68,6 +69,8 @@ var ( 0x4, 0x5, 0xfe, 0x0, 0xf, 0x42, 0x40, // Custom blob tlv. 0x5, 0x4, 0x1, 0x2, 0x3, 0x4, + // HLTC index tlv. + 0x6, 0x2, 0x0, 0x03, } testHTLCEntryHash = HTLCEntry{ @@ -86,8 +89,8 @@ var ( ), } testHTLCEntryHashBytes = []byte{ - // Body length 54. - 0x36, + // Body length 58. + 0x3a, // Rhash tlv. 0x0, 0x20, 0x33, 0x44, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -102,6 +105,8 @@ var ( 0x3, 0x1, 0x1, // Amt tlv. 0x4, 0x5, 0xfe, 0x0, 0xf, 0x42, 0x40, + // HLTC index tlv. + 0x6, 0x2, 0x0, 0x00, } localBalance = lnwire.MilliSatoshi(9000) @@ -118,6 +123,7 @@ var ( Htlcs: []HTLC{{ RefundTimeout: testHTLCEntry.RefundTimeout.Val, OutputIndex: int32(testHTLCEntry.OutputIndex.Val), + HtlcIndex: uint64(testHTLCEntry.HtlcIndex.Val), Incoming: testHTLCEntry.Incoming.Val, Amt: lnwire.NewMSatFromSatoshis( testHTLCEntry.Amt.Val.Int(), @@ -284,7 +290,7 @@ func TestSerializeHTLCEntries(t *testing.T) { partialBytes := testHTLCEntryBytes[3:] // Write the total length and RHash tlv. - expectedBytes := []byte{0x3c, 0x0, 0x20} + expectedBytes := []byte{0x40, 0x0, 0x20} expectedBytes = append(expectedBytes, rHashBytes...) // Append the rest. @@ -399,7 +405,7 @@ func TestDeserializeHTLCEntries(t *testing.T) { partialBytes := testHTLCEntryBytes[3:] // Write the total length and RHash tlv. - testBytes := append([]byte{0x3c, 0x0, 0x20}, rHashBytes...) + testBytes := append([]byte{0x40, 0x0, 0x20}, rHashBytes...) // Append the rest. testBytes = append(testBytes, partialBytes...)