Skip to content

Commit 2510c19

Browse files
Roasbeefguggero
authored andcommitted
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).
1 parent 669740c commit 2510c19

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

channeldb/revocation_log.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,17 @@ type HTLCEntry struct {
155155

156156
// Incoming denotes whether we're the receiver or the sender of this
157157
// HTLC.
158-
//
159-
// NOTE: this field is the memory representation of the field
160-
// incomingUint.
161158
Incoming tlv.RecordT[tlv.TlvType3, bool]
162159

163160
// Amt is the amount of satoshis this HTLC escrows.
164-
//
165-
// NOTE: this field is the memory representation of the field amtUint.
166161
Amt tlv.RecordT[tlv.TlvType4, tlv.BigSizeT[btcutil.Amount]]
167162

168163
// CustomBlob is an optional blob that can be used to store information
169164
// specific to revocation handling for a custom channel type.
170165
CustomBlob tlv.OptionalRecordT[tlv.TlvType5, tlv.Blob]
166+
167+
// HtlcIndex is the index of the HTLC in the channel.
168+
HtlcIndex tlv.OptionalRecordT[tlv.TlvType6, uint16]
171169
}
172170

173171
// toTlvStream converts an HTLCEntry record into a tlv representation.
@@ -184,6 +182,12 @@ func (h *HTLCEntry) toTlvStream() (*tlv.Stream, error) {
184182
records = append(records, r.Record())
185183
})
186184

185+
h.HtlcIndex.WhenSome(func(r tlv.RecordT[tlv.TlvType6, uint16]) {
186+
records = append(records, r.Record())
187+
})
188+
189+
tlv.SortRecords(records)
190+
187191
return tlv.NewStream(records...)
188192
}
189193

@@ -203,6 +207,9 @@ func NewHTLCEntryFromHTLC(htlc HTLC) (*HTLCEntry, error) {
203207
Amt: tlv.NewRecordT[tlv.TlvType4](
204208
tlv.NewBigSizeT(htlc.Amt.ToSatoshis()),
205209
),
210+
HtlcIndex: tlv.SomeRecordT(tlv.NewPrimitiveRecord[tlv.TlvType6](
211+
uint16(htlc.HtlcIndex),
212+
)),
206213
}
207214

208215
if len(htlc.CustomRecords) != 0 {
@@ -509,6 +516,7 @@ func deserializeHTLCEntries(r io.Reader) ([]*HTLCEntry, error) {
509516
var htlc HTLCEntry
510517

511518
customBlob := htlc.CustomBlob.Zero()
519+
htlcIndex := htlc.HtlcIndex.Zero()
512520

513521
// Create the tlv stream.
514522
records := []tlv.Record{
@@ -518,6 +526,7 @@ func deserializeHTLCEntries(r io.Reader) ([]*HTLCEntry, error) {
518526
htlc.Incoming.Record(),
519527
htlc.Amt.Record(),
520528
customBlob.Record(),
529+
htlcIndex.Record(),
521530
}
522531

523532
tlvStream, err := tlv.NewStream(records...)
@@ -539,6 +548,10 @@ func deserializeHTLCEntries(r io.Reader) ([]*HTLCEntry, error) {
539548
htlc.CustomBlob = tlv.SomeRecordT(customBlob)
540549
}
541550

551+
if t, ok := parsedTypes[htlcIndex.TlvType()]; ok && t == nil {
552+
htlc.HtlcIndex = tlv.SomeRecordT(htlcIndex)
553+
}
554+
542555
// Append the entry.
543556
htlcs = append(htlcs, &htlc)
544557
}

channeldb/revocation_log_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@ var (
5959
CustomBlob: tlv.SomeRecordT(
6060
tlv.NewPrimitiveRecord[tlv.TlvType5](blobBytes),
6161
),
62+
HtlcIndex: tlv.SomeRecordT(
63+
tlv.NewPrimitiveRecord[tlv.TlvType6, uint16](0x33),
64+
),
6265
}
6366
testHTLCEntryBytes = []byte{
64-
// Body length 41.
65-
0x29,
67+
// Body length 45.
68+
0x2d,
6669
// Rhash tlv.
6770
0x0, 0x0,
6871
// RefundTimeout tlv.
@@ -76,6 +79,8 @@ var (
7679
// Custom blob tlv.
7780
0x5, 0x11, 0xfe, 0x00, 0x01, 0x00, 0x01, 0x0b, 0x63, 0x75, 0x73,
7881
0x74, 0x6f, 0x6d, 0x20, 0x64, 0x61, 0x74, 0x61,
82+
// HLTC index tlv.
83+
0x6, 0x2, 0x0, 0x33,
7984
}
8085

8186
testHTLCEntryHash = HTLCEntry{
@@ -126,7 +131,11 @@ var (
126131
Htlcs: []HTLC{{
127132
RefundTimeout: testHTLCEntry.RefundTimeout.Val,
128133
OutputIndex: int32(testHTLCEntry.OutputIndex.Val),
129-
Incoming: testHTLCEntry.Incoming.Val,
134+
HtlcIndex: uint64(
135+
testHTLCEntry.HtlcIndex.ValOpt().
136+
UnsafeFromSome(),
137+
),
138+
Incoming: testHTLCEntry.Incoming.Val,
130139
Amt: lnwire.NewMSatFromSatoshis(
131140
testHTLCEntry.Amt.Val.Int(),
132141
),
@@ -294,7 +303,7 @@ func TestSerializeHTLCEntries(t *testing.T) {
294303
partialBytes := testHTLCEntryBytes[3:]
295304

296305
// Write the total length and RHash tlv.
297-
expectedBytes := []byte{0x49, 0x0, 0x20}
306+
expectedBytes := []byte{0x4d, 0x0, 0x20}
298307
expectedBytes = append(expectedBytes, rHashBytes...)
299308

300309
// Append the rest.

0 commit comments

Comments
 (0)