Skip to content

Commit 0bf2050

Browse files
Roasbeefguggero
authored andcommitted
channeldb: convert RevocationLog to use RecordT
1 parent f3655da commit 0bf2050

File tree

5 files changed

+146
-113
lines changed

5 files changed

+146
-113
lines changed

channeldb/channel_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -570,9 +570,11 @@ func assertCommitmentEqual(t *testing.T, a, b *ChannelCommitment) {
570570
func assertRevocationLogEntryEqual(t *testing.T, c *ChannelCommitment,
571571
r *RevocationLog) {
572572

573+
t.Helper()
574+
573575
// Check the common fields.
574576
require.EqualValues(
575-
t, r.CommitTxHash, c.CommitTx.TxHash(), "CommitTx mismatch",
577+
t, r.CommitTxHash.Val, c.CommitTx.TxHash(), "CommitTx mismatch",
576578
)
577579

578580
// Now check the common fields from the HTLCs.
@@ -806,10 +808,10 @@ func TestChannelStateTransition(t *testing.T) {
806808

807809
// Check the output indexes are saved as expected.
808810
require.EqualValues(
809-
t, dummyLocalOutputIndex, diskPrevCommit.OurOutputIndex,
811+
t, dummyLocalOutputIndex, diskPrevCommit.OurOutputIndex.Val,
810812
)
811813
require.EqualValues(
812-
t, dummyRemoteOutIndex, diskPrevCommit.TheirOutputIndex,
814+
t, dummyRemoteOutIndex, diskPrevCommit.TheirOutputIndex.Val,
813815
)
814816

815817
// The two deltas (the original vs the on-disk version) should
@@ -851,10 +853,10 @@ func TestChannelStateTransition(t *testing.T) {
851853

852854
// Check the output indexes are saved as expected.
853855
require.EqualValues(
854-
t, dummyLocalOutputIndex, diskPrevCommit.OurOutputIndex,
856+
t, dummyLocalOutputIndex, diskPrevCommit.OurOutputIndex.Val,
855857
)
856858
require.EqualValues(
857-
t, dummyRemoteOutIndex, diskPrevCommit.TheirOutputIndex,
859+
t, dummyRemoteOutIndex, diskPrevCommit.TheirOutputIndex.Val,
858860
)
859861

860862
assertRevocationLogEntryEqual(t, &oldRemoteCommit, prevCommit)

channeldb/revocation_log.go

Lines changed: 91 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"math"
88

99
"github.com/btcsuite/btcd/btcutil"
10+
"github.com/lightningnetwork/lnd/fn"
1011
"github.com/lightningnetwork/lnd/kvdb"
1112
"github.com/lightningnetwork/lnd/lntypes"
1213
"github.com/lightningnetwork/lnd/lnwire"
@@ -16,16 +17,15 @@ import (
1617
const (
1718
// OutputIndexEmpty is used when the output index doesn't exist.
1819
OutputIndexEmpty = math.MaxUint16
20+
)
1921

20-
// A set of tlv type definitions used to serialize the body of
21-
// revocation logs to the database.
22-
//
23-
// NOTE: A migration should be added whenever this list changes.
24-
revLogOurOutputIndexType tlv.Type = 0
25-
revLogTheirOutputIndexType tlv.Type = 1
26-
revLogCommitTxHashType tlv.Type = 2
27-
revLogOurBalanceType tlv.Type = 3
28-
revLogTheirBalanceType tlv.Type = 4
22+
type (
23+
// BigSizeAmount is a type alias for a TLV record of a btcutil.Amount.
24+
BigSizeAmount = tlv.BigSizeT[btcutil.Amount]
25+
26+
// BigSizeMilliSatoshi is a type alias for a TLV record of a
27+
// lnwire.MilliSatoshi.
28+
BigSizeMilliSatoshi = tlv.BigSizeT[lnwire.MilliSatoshi]
2929
)
3030

3131
var (
@@ -203,15 +203,15 @@ func NewHTLCEntryFromHTLC(htlc HTLC) *HTLCEntry {
203203
type RevocationLog struct {
204204
// OurOutputIndex specifies our output index in this commitment. In a
205205
// remote commitment transaction, this is the to remote output index.
206-
OurOutputIndex uint16
206+
OurOutputIndex tlv.RecordT[tlv.TlvType0, uint16]
207207

208208
// TheirOutputIndex specifies their output index in this commitment. In
209209
// a remote commitment transaction, this is the to local output index.
210-
TheirOutputIndex uint16
210+
TheirOutputIndex tlv.RecordT[tlv.TlvType1, uint16]
211211

212212
// CommitTxHash is the hash of the latest version of the commitment
213213
// state, broadcast able by us.
214-
CommitTxHash [32]byte
214+
CommitTxHash tlv.RecordT[tlv.TlvType2, [32]byte]
215215

216216
// HTLCEntries is the set of HTLCEntry's that are pending at this
217217
// particular commitment height.
@@ -221,21 +221,53 @@ type RevocationLog struct {
221221
// directly spendable by us. In other words, it is the value of the
222222
// to_remote output on the remote parties' commitment transaction.
223223
//
224-
// NOTE: this is a pointer so that it is clear if the value is zero or
224+
// NOTE: this is an option so that it is clear if the value is zero or
225225
// nil. Since migration 30 of the channeldb initially did not include
226226
// this field, it could be the case that the field is not present for
227227
// all revocation logs.
228-
OurBalance *lnwire.MilliSatoshi
228+
OurBalance tlv.OptionalRecordT[tlv.TlvType3, BigSizeMilliSatoshi]
229229

230230
// TheirBalance is the current available balance within the channel
231231
// directly spendable by the remote node. In other words, it is the
232232
// value of the to_local output on the remote parties' commitment.
233233
//
234-
// NOTE: this is a pointer so that it is clear if the value is zero or
234+
// NOTE: this is an option so that it is clear if the value is zero or
235235
// nil. Since migration 30 of the channeldb initially did not include
236236
// this field, it could be the case that the field is not present for
237237
// all revocation logs.
238-
TheirBalance *lnwire.MilliSatoshi
238+
TheirBalance tlv.OptionalRecordT[tlv.TlvType4, BigSizeMilliSatoshi]
239+
}
240+
241+
// NewRevocationLog creates a new RevocationLog from the given parameters.
242+
func NewRevocationLog(ourOutputIndex uint16, theirOutputIndex uint16,
243+
commitHash [32]byte, ourBalance,
244+
theirBalance fn.Option[lnwire.MilliSatoshi],
245+
htlcs []*HTLCEntry) RevocationLog {
246+
247+
rl := RevocationLog{
248+
OurOutputIndex: tlv.NewPrimitiveRecord[tlv.TlvType0](
249+
ourOutputIndex,
250+
),
251+
TheirOutputIndex: tlv.NewPrimitiveRecord[tlv.TlvType1](
252+
theirOutputIndex,
253+
),
254+
CommitTxHash: tlv.NewPrimitiveRecord[tlv.TlvType2](commitHash),
255+
HTLCEntries: htlcs,
256+
}
257+
258+
ourBalance.WhenSome(func(balance lnwire.MilliSatoshi) {
259+
rl.OurBalance = tlv.SomeRecordT(tlv.NewRecordT[tlv.TlvType3](
260+
tlv.NewBigSizeT(balance),
261+
))
262+
})
263+
264+
theirBalance.WhenSome(func(balance lnwire.MilliSatoshi) {
265+
rl.TheirBalance = tlv.SomeRecordT(tlv.NewRecordT[tlv.TlvType4](
266+
tlv.NewBigSizeT(balance),
267+
))
268+
})
269+
270+
return rl
239271
}
240272

241273
// putRevocationLog uses the fields `CommitTx` and `Htlcs` from a
@@ -254,15 +286,26 @@ func putRevocationLog(bucket kvdb.RwBucket, commit *ChannelCommitment,
254286
}
255287

256288
rl := &RevocationLog{
257-
OurOutputIndex: uint16(ourOutputIndex),
258-
TheirOutputIndex: uint16(theirOutputIndex),
259-
CommitTxHash: commit.CommitTx.TxHash(),
260-
HTLCEntries: make([]*HTLCEntry, 0, len(commit.Htlcs)),
289+
OurOutputIndex: tlv.NewPrimitiveRecord[tlv.TlvType0](
290+
uint16(ourOutputIndex),
291+
),
292+
TheirOutputIndex: tlv.NewPrimitiveRecord[tlv.TlvType1](
293+
uint16(theirOutputIndex),
294+
),
295+
CommitTxHash: tlv.NewPrimitiveRecord[tlv.TlvType2, [32]byte](
296+
commit.CommitTx.TxHash(),
297+
),
298+
HTLCEntries: make([]*HTLCEntry, 0, len(commit.Htlcs)),
261299
}
262300

263301
if !noAmtData {
264-
rl.OurBalance = &commit.LocalBalance
265-
rl.TheirBalance = &commit.RemoteBalance
302+
rl.OurBalance = tlv.SomeRecordT(tlv.NewRecordT[tlv.TlvType3](
303+
tlv.NewBigSizeT(commit.LocalBalance),
304+
))
305+
306+
rl.TheirBalance = tlv.SomeRecordT(tlv.NewRecordT[tlv.TlvType4](
307+
tlv.NewBigSizeT(commit.RemoteBalance),
308+
))
266309
}
267310

268311
for _, htlc := range commit.Htlcs {
@@ -312,31 +355,23 @@ func fetchRevocationLog(log kvdb.RBucket,
312355
func serializeRevocationLog(w io.Writer, rl *RevocationLog) error {
313356
// Add the tlv records for all non-optional fields.
314357
records := []tlv.Record{
315-
tlv.MakePrimitiveRecord(
316-
revLogOurOutputIndexType, &rl.OurOutputIndex,
317-
),
318-
tlv.MakePrimitiveRecord(
319-
revLogTheirOutputIndexType, &rl.TheirOutputIndex,
320-
),
321-
tlv.MakePrimitiveRecord(
322-
revLogCommitTxHashType, &rl.CommitTxHash,
323-
),
358+
rl.OurOutputIndex.Record(),
359+
rl.TheirOutputIndex.Record(),
360+
rl.CommitTxHash.Record(),
324361
}
325362

326363
// Now we add any optional fields that are non-nil.
327-
if rl.OurBalance != nil {
328-
lb := uint64(*rl.OurBalance)
329-
records = append(records, tlv.MakeBigSizeRecord(
330-
revLogOurBalanceType, &lb,
331-
))
332-
}
364+
rl.OurBalance.WhenSome(
365+
func(r tlv.RecordT[tlv.TlvType3, BigSizeMilliSatoshi]) {
366+
records = append(records, r.Record())
367+
},
368+
)
333369

334-
if rl.TheirBalance != nil {
335-
rb := uint64(*rl.TheirBalance)
336-
records = append(records, tlv.MakeBigSizeRecord(
337-
revLogTheirBalanceType, &rb,
338-
))
339-
}
370+
rl.TheirBalance.WhenSome(
371+
func(r tlv.RecordT[tlv.TlvType4, BigSizeMilliSatoshi]) {
372+
records = append(records, r.Record())
373+
},
374+
)
340375

341376
// Create the tlv stream.
342377
tlvStream, err := tlv.NewStream(records...)
@@ -374,27 +409,18 @@ func serializeHTLCEntries(w io.Writer, htlcs []*HTLCEntry) error {
374409

375410
// deserializeRevocationLog deserializes a RevocationLog based on tlv format.
376411
func deserializeRevocationLog(r io.Reader) (RevocationLog, error) {
377-
var (
378-
rl RevocationLog
379-
ourBalance uint64
380-
theirBalance uint64
381-
)
412+
var rl RevocationLog
413+
414+
ourBalance := rl.OurBalance.Zero()
415+
theirBalance := rl.TheirBalance.Zero()
382416

383417
// Create the tlv stream.
384418
tlvStream, err := tlv.NewStream(
385-
tlv.MakePrimitiveRecord(
386-
revLogOurOutputIndexType, &rl.OurOutputIndex,
387-
),
388-
tlv.MakePrimitiveRecord(
389-
revLogTheirOutputIndexType, &rl.TheirOutputIndex,
390-
),
391-
tlv.MakePrimitiveRecord(
392-
revLogCommitTxHashType, &rl.CommitTxHash,
393-
),
394-
tlv.MakeBigSizeRecord(revLogOurBalanceType, &ourBalance),
395-
tlv.MakeBigSizeRecord(
396-
revLogTheirBalanceType, &theirBalance,
397-
),
419+
rl.OurOutputIndex.Record(),
420+
rl.TheirOutputIndex.Record(),
421+
rl.CommitTxHash.Record(),
422+
ourBalance.Record(),
423+
theirBalance.Record(),
398424
)
399425
if err != nil {
400426
return rl, err
@@ -406,14 +432,12 @@ func deserializeRevocationLog(r io.Reader) (RevocationLog, error) {
406432
return rl, err
407433
}
408434

409-
if t, ok := parsedTypes[revLogOurBalanceType]; ok && t == nil {
410-
lb := lnwire.MilliSatoshi(ourBalance)
411-
rl.OurBalance = &lb
435+
if t, ok := parsedTypes[ourBalance.TlvType()]; ok && t == nil {
436+
rl.OurBalance = tlv.SomeRecordT(ourBalance)
412437
}
413438

414-
if t, ok := parsedTypes[revLogTheirBalanceType]; ok && t == nil {
415-
rb := lnwire.MilliSatoshi(theirBalance)
416-
rl.TheirBalance = &rb
439+
if t, ok := parsedTypes[theirBalance.TlvType()]; ok && t == nil {
440+
rl.TheirBalance = tlv.SomeRecordT(theirBalance)
417441
}
418442

419443
// Read the HTLC entries.

channeldb/revocation_log_test.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
"github.com/btcsuite/btcd/btcutil"
11+
"github.com/lightningnetwork/lnd/fn"
1112
"github.com/lightningnetwork/lnd/kvdb"
1213
"github.com/lightningnetwork/lnd/lntest/channels"
1314
"github.com/lightningnetwork/lnd/lnwire"
@@ -115,12 +116,11 @@ var (
115116
}},
116117
}
117118

118-
testRevocationLogNoAmts = RevocationLog{
119-
OurOutputIndex: 0,
120-
TheirOutputIndex: 1,
121-
CommitTxHash: testChannelCommit.CommitTx.TxHash(),
122-
HTLCEntries: []*HTLCEntry{&testHTLCEntry},
123-
}
119+
testRevocationLogNoAmts = NewRevocationLog(
120+
0, 1, testChannelCommit.CommitTx.TxHash(),
121+
fn.None[lnwire.MilliSatoshi](), fn.None[lnwire.MilliSatoshi](),
122+
[]*HTLCEntry{&testHTLCEntry},
123+
)
124124
testRevocationLogNoAmtsBytes = []byte{
125125
// Body length 42.
126126
0x2a,
@@ -136,14 +136,11 @@ var (
136136
0xc8, 0x22, 0x51, 0xb1, 0x5b, 0xa0, 0xbf, 0xd,
137137
}
138138

139-
testRevocationLogWithAmts = RevocationLog{
140-
OurOutputIndex: 0,
141-
TheirOutputIndex: 1,
142-
CommitTxHash: testChannelCommit.CommitTx.TxHash(),
143-
HTLCEntries: []*HTLCEntry{&testHTLCEntry},
144-
OurBalance: &localBalance,
145-
TheirBalance: &remoteBalance,
146-
}
139+
testRevocationLogWithAmts = NewRevocationLog(
140+
0, 1, testChannelCommit.CommitTx.TxHash(),
141+
fn.Some(localBalance), fn.Some(remoteBalance),
142+
[]*HTLCEntry{&testHTLCEntry},
143+
)
147144
testRevocationLogWithAmtsBytes = []byte{
148145
// Body length 52.
149146
0x34,

0 commit comments

Comments
 (0)