Skip to content

Commit 2cf3854

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 d5f595e commit 2cf3854

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

channeldb/revocation_log.go

Lines changed: 10 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.RecordT[tlv.TlvType6, uint16]
171169
}
172170

173171
// toTlvStream converts an HTLCEntry record into a tlv representation.
@@ -178,12 +176,15 @@ func (h *HTLCEntry) toTlvStream() (*tlv.Stream, error) {
178176
h.OutputIndex.Record(),
179177
h.Incoming.Record(),
180178
h.Amt.Record(),
179+
h.HtlcIndex.Record(),
181180
}
182181

183182
h.CustomBlob.WhenSome(func(r tlv.RecordT[tlv.TlvType5, tlv.Blob]) {
184183
records = append(records, r.Record())
185184
})
186185

186+
tlv.SortRecords(records)
187+
187188
return tlv.NewStream(records...)
188189
}
189190

@@ -203,6 +204,9 @@ func NewHTLCEntryFromHTLC(htlc HTLC) *HTLCEntry {
203204
Amt: tlv.NewRecordT[tlv.TlvType4](
204205
tlv.NewBigSizeT(htlc.Amt.ToSatoshis()),
205206
),
207+
HtlcIndex: tlv.NewPrimitiveRecord[tlv.TlvType6](
208+
uint16(htlc.HtlcIndex),
209+
),
206210
}
207211

208212
if len(htlc.ExtraData) != 0 {
@@ -512,6 +516,7 @@ func deserializeHTLCEntries(r io.Reader) ([]*HTLCEntry, error) {
512516
htlc.Incoming.Record(),
513517
htlc.Amt.Record(),
514518
customBlob.Record(),
519+
htlc.HtlcIndex.Record(),
515520
}
516521

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

channeldb/revocation_log_test.go

Lines changed: 12 additions & 6 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
testHTLCEntryHash = HTLCEntry{
@@ -86,8 +89,8 @@ var (
8689
),
8790
}
8891
testHTLCEntryHashBytes = []byte{
89-
// Body length 54.
90-
0x36,
92+
// Body length 58.
93+
0x3a,
9194
// Rhash tlv.
9295
0x0, 0x20,
9396
0x33, 0x44, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -102,6 +105,8 @@ var (
102105
0x3, 0x1, 0x1,
103106
// Amt tlv.
104107
0x4, 0x5, 0xfe, 0x0, 0xf, 0x42, 0x40,
108+
// HLTC index tlv.
109+
0x6, 0x2, 0x0, 0x00,
105110
}
106111

107112
localBalance = lnwire.MilliSatoshi(9000)
@@ -118,6 +123,7 @@ var (
118123
Htlcs: []HTLC{{
119124
RefundTimeout: testHTLCEntry.RefundTimeout.Val,
120125
OutputIndex: int32(testHTLCEntry.OutputIndex.Val),
126+
HtlcIndex: uint64(testHTLCEntry.HtlcIndex.Val),
121127
Incoming: testHTLCEntry.Incoming.Val,
122128
Amt: lnwire.NewMSatFromSatoshis(
123129
testHTLCEntry.Amt.Val.Int(),
@@ -284,7 +290,7 @@ func TestSerializeHTLCEntries(t *testing.T) {
284290
partialBytes := testHTLCEntryBytes[3:]
285291

286292
// Write the total length and RHash tlv.
287-
expectedBytes := []byte{0x3c, 0x0, 0x20}
293+
expectedBytes := []byte{0x40, 0x0, 0x20}
288294
expectedBytes = append(expectedBytes, rHashBytes...)
289295

290296
// Append the rest.
@@ -399,7 +405,7 @@ func TestDeserializeHTLCEntries(t *testing.T) {
399405
partialBytes := testHTLCEntryBytes[3:]
400406

401407
// Write the total length and RHash tlv.
402-
testBytes := append([]byte{0x3c, 0x0, 0x20}, rHashBytes...)
408+
testBytes := append([]byte{0x40, 0x0, 0x20}, rHashBytes...)
403409

404410
// Append the rest.
405411
testBytes = append(testBytes, partialBytes...)

0 commit comments

Comments
 (0)