Skip to content

Commit c79648e

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 ceaab9f commit c79648e

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

channeldb/revocation_log.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,19 +163,17 @@ type HTLCEntry struct {
163163

164164
// Incoming denotes whether we're the receiver or the sender of this
165165
// HTLC.
166-
//
167-
// NOTE: this field is the memory representation of the field
168-
// incomingUint.
169166
Incoming tlv.RecordT[tlv.TlvType3, bool]
170167

171168
// Amt is the amount of satoshis this HTLC escrows.
172-
//
173-
// NOTE: this field is the memory representation of the field amtUint.
174169
Amt tlv.RecordT[tlv.TlvType4, tlv.BigSizeT[btcutil.Amount]]
175170

176171
// CustomBlob is an optional blob that can be used to store information
177172
// specific to revocation handling for a custom channel type.
178173
CustomBlob tlv.OptionalRecordT[tlv.TlvType5, tlv.Blob]
174+
175+
// HtlcIndex is the index of the HTLC in the channel.
176+
HtlcIndex tlv.RecordT[tlv.TlvType6, uint16]
179177
}
180178

181179
// toTlvStream converts an HTLCEntry record into a tlv representation.
@@ -186,12 +184,15 @@ func (h *HTLCEntry) toTlvStream() (*tlv.Stream, error) {
186184
h.OutputIndex.Record(),
187185
h.Incoming.Record(),
188186
h.Amt.Record(),
187+
h.HtlcIndex.Record(),
189188
}
190189

191190
h.CustomBlob.WhenSome(func(r tlv.RecordT[tlv.TlvType5, tlv.Blob]) {
192191
records = append(records, r.Record())
193192
})
194193

194+
tlv.SortRecords(records)
195+
195196
return tlv.NewStream(records...)
196197
}
197198

@@ -211,6 +212,9 @@ func NewHTLCEntryFromHTLC(htlc HTLC) *HTLCEntry {
211212
Amt: tlv.NewRecordT[tlv.TlvType4](
212213
tlv.NewBigSizeT(htlc.Amt.ToSatoshis()),
213214
),
215+
HtlcIndex: tlv.NewPrimitiveRecord[tlv.TlvType6](
216+
uint16(htlc.HtlcIndex),
217+
),
214218
}
215219

216220
if len(htlc.ExtraData) != 0 {
@@ -520,6 +524,7 @@ func deserializeHTLCEntries(r io.Reader) ([]*HTLCEntry, error) {
520524
htlc.Incoming.Record(),
521525
htlc.Amt.Record(),
522526
customBlob.Record(),
527+
htlc.HtlcIndex.Record(),
523528
}
524529

525530
tlvStream, err := tlv.NewStream(records...)

channeldb/revocation_log_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ var (
5252
CustomBlob: tlv.SomeRecordT(
5353
tlv.NewPrimitiveRecord[tlv.TlvType5](blobBytes),
5454
),
55+
HtlcIndex: tlv.NewPrimitiveRecord[tlv.TlvType6, uint16](3),
5556
}
5657
testHTLCEntryBytes = []byte{
57-
// Body length 28.
58-
0x1c,
58+
// Body length 32.
59+
0x20,
5960
// Rhash tlv.
6061
0x0, 0x0,
6162
// RefundTimeout tlv.
@@ -68,6 +69,8 @@ var (
6869
0x4, 0x5, 0xfe, 0x0, 0xf, 0x42, 0x40,
6970
// Custom blob tlv.
7071
0x5, 0x4, 0x1, 0x2, 0x3, 0x4,
72+
// HLTC index tlv.
73+
0x6, 0x2, 0x0, 0x03,
7174
}
7275

7376
localBalance = lnwire.MilliSatoshi(9000)
@@ -84,6 +87,7 @@ var (
8487
Htlcs: []HTLC{{
8588
RefundTimeout: testHTLCEntry.RefundTimeout.Val,
8689
OutputIndex: int32(testHTLCEntry.OutputIndex.Val),
90+
HtlcIndex: uint64(testHTLCEntry.HtlcIndex.Val),
8791
Incoming: testHTLCEntry.Incoming.Val,
8892
Amt: lnwire.NewMSatFromSatoshis(
8993
testHTLCEntry.Amt.Val.Int(),
@@ -235,7 +239,7 @@ func TestSerializeHTLCEntries(t *testing.T) {
235239
partialBytes := testHTLCEntryBytes[3:]
236240

237241
// Write the total length and RHash tlv.
238-
expectedBytes := []byte{0x3c, 0x0, 0x20}
242+
expectedBytes := []byte{0x40, 0x0, 0x20}
239243
expectedBytes = append(expectedBytes, rHashBytes...)
240244

241245
// Append the rest.
@@ -350,7 +354,7 @@ func TestDerializeHTLCEntries(t *testing.T) {
350354
partialBytes := testHTLCEntryBytes[3:]
351355

352356
// Write the total length and RHash tlv.
353-
testBytes := append([]byte{0x3c, 0x0, 0x20}, rHashBytes...)
357+
testBytes := append([]byte{0x40, 0x0, 0x20}, rHashBytes...)
354358

355359
// Append the rest.
356360
testBytes = append(testBytes, partialBytes...)

0 commit comments

Comments
 (0)