Skip to content

Commit 9d1926b

Browse files
Roasbeefguggero
authored andcommitted
channeldb: convert RevocationLog to use RecordT
1 parent d9709b8 commit 9d1926b

File tree

6 files changed

+155
-113
lines changed

6 files changed

+155
-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.
@@ -804,10 +806,10 @@ func TestChannelStateTransition(t *testing.T) {
804806

805807
// Check the output indexes are saved as expected.
806808
require.EqualValues(
807-
t, dummyLocalOutputIndex, diskPrevCommit.OurOutputIndex,
809+
t, dummyLocalOutputIndex, diskPrevCommit.OurOutputIndex.Val,
808810
)
809811
require.EqualValues(
810-
t, dummyRemoteOutIndex, diskPrevCommit.TheirOutputIndex,
812+
t, dummyRemoteOutIndex, diskPrevCommit.TheirOutputIndex.Val,
811813
)
812814

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

850852
// Check the output indexes are saved as expected.
851853
require.EqualValues(
852-
t, dummyLocalOutputIndex, diskPrevCommit.OurOutputIndex,
854+
t, dummyLocalOutputIndex, diskPrevCommit.OurOutputIndex.Val,
853855
)
854856
require.EqualValues(
855-
t, dummyRemoteOutIndex, diskPrevCommit.TheirOutputIndex,
857+
t, dummyRemoteOutIndex, diskPrevCommit.TheirOutputIndex.Val,
856858
)
857859

858860
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 (
@@ -211,15 +211,15 @@ func NewHTLCEntryFromHTLC(htlc HTLC) *HTLCEntry {
211211
type RevocationLog struct {
212212
// OurOutputIndex specifies our output index in this commitment. In a
213213
// remote commitment transaction, this is the to remote output index.
214-
OurOutputIndex uint16
214+
OurOutputIndex tlv.RecordT[tlv.TlvType0, uint16]
215215

216216
// TheirOutputIndex specifies their output index in this commitment. In
217217
// a remote commitment transaction, this is the to local output index.
218-
TheirOutputIndex uint16
218+
TheirOutputIndex tlv.RecordT[tlv.TlvType1, uint16]
219219

220220
// CommitTxHash is the hash of the latest version of the commitment
221221
// state, broadcast able by us.
222-
CommitTxHash [32]byte
222+
CommitTxHash tlv.RecordT[tlv.TlvType2, [32]byte]
223223

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

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

249281
// putRevocationLog uses the fields `CommitTx` and `Htlcs` from a
@@ -262,15 +294,26 @@ func putRevocationLog(bucket kvdb.RwBucket, commit *ChannelCommitment,
262294
}
263295

264296
rl := &RevocationLog{
265-
OurOutputIndex: uint16(ourOutputIndex),
266-
TheirOutputIndex: uint16(theirOutputIndex),
267-
CommitTxHash: commit.CommitTx.TxHash(),
268-
HTLCEntries: make([]*HTLCEntry, 0, len(commit.Htlcs)),
297+
OurOutputIndex: tlv.NewPrimitiveRecord[tlv.TlvType0](
298+
uint16(ourOutputIndex),
299+
),
300+
TheirOutputIndex: tlv.NewPrimitiveRecord[tlv.TlvType1](
301+
uint16(theirOutputIndex),
302+
),
303+
CommitTxHash: tlv.NewPrimitiveRecord[tlv.TlvType2, [32]byte](
304+
commit.CommitTx.TxHash(),
305+
),
306+
HTLCEntries: make([]*HTLCEntry, 0, len(commit.Htlcs)),
269307
}
270308

271309
if !noAmtData {
272-
rl.OurBalance = &commit.LocalBalance
273-
rl.TheirBalance = &commit.RemoteBalance
310+
rl.OurBalance = tlv.SomeRecordT(tlv.NewRecordT[tlv.TlvType3](
311+
tlv.NewBigSizeT(commit.LocalBalance),
312+
))
313+
314+
rl.TheirBalance = tlv.SomeRecordT(tlv.NewRecordT[tlv.TlvType4](
315+
tlv.NewBigSizeT(commit.RemoteBalance),
316+
))
274317
}
275318

276319
for _, htlc := range commit.Htlcs {
@@ -320,31 +363,23 @@ func fetchRevocationLog(log kvdb.RBucket,
320363
func serializeRevocationLog(w io.Writer, rl *RevocationLog) error {
321364
// Add the tlv records for all non-optional fields.
322365
records := []tlv.Record{
323-
tlv.MakePrimitiveRecord(
324-
revLogOurOutputIndexType, &rl.OurOutputIndex,
325-
),
326-
tlv.MakePrimitiveRecord(
327-
revLogTheirOutputIndexType, &rl.TheirOutputIndex,
328-
),
329-
tlv.MakePrimitiveRecord(
330-
revLogCommitTxHashType, &rl.CommitTxHash,
331-
),
366+
rl.OurOutputIndex.Record(),
367+
rl.TheirOutputIndex.Record(),
368+
rl.CommitTxHash.Record(),
332369
}
333370

334371
// Now we add any optional fields that are non-nil.
335-
if rl.OurBalance != nil {
336-
lb := uint64(*rl.OurBalance)
337-
records = append(records, tlv.MakeBigSizeRecord(
338-
revLogOurBalanceType, &lb,
339-
))
340-
}
372+
rl.OurBalance.WhenSome(
373+
func(r tlv.RecordT[tlv.TlvType3, BigSizeMilliSatoshi]) {
374+
records = append(records, r.Record())
375+
},
376+
)
341377

342-
if rl.TheirBalance != nil {
343-
rb := uint64(*rl.TheirBalance)
344-
records = append(records, tlv.MakeBigSizeRecord(
345-
revLogTheirBalanceType, &rb,
346-
))
347-
}
378+
rl.TheirBalance.WhenSome(
379+
func(r tlv.RecordT[tlv.TlvType4, BigSizeMilliSatoshi]) {
380+
records = append(records, r.Record())
381+
},
382+
)
348383

349384
// Create the tlv stream.
350385
tlvStream, err := tlv.NewStream(records...)
@@ -382,27 +417,18 @@ func serializeHTLCEntries(w io.Writer, htlcs []*HTLCEntry) error {
382417

383418
// deserializeRevocationLog deserializes a RevocationLog based on tlv format.
384419
func deserializeRevocationLog(r io.Reader) (RevocationLog, error) {
385-
var (
386-
rl RevocationLog
387-
ourBalance uint64
388-
theirBalance uint64
389-
)
420+
var rl RevocationLog
421+
422+
ourBalance := rl.OurBalance.Zero()
423+
theirBalance := rl.TheirBalance.Zero()
390424

391425
// Create the tlv stream.
392426
tlvStream, err := tlv.NewStream(
393-
tlv.MakePrimitiveRecord(
394-
revLogOurOutputIndexType, &rl.OurOutputIndex,
395-
),
396-
tlv.MakePrimitiveRecord(
397-
revLogTheirOutputIndexType, &rl.TheirOutputIndex,
398-
),
399-
tlv.MakePrimitiveRecord(
400-
revLogCommitTxHashType, &rl.CommitTxHash,
401-
),
402-
tlv.MakeBigSizeRecord(revLogOurBalanceType, &ourBalance),
403-
tlv.MakeBigSizeRecord(
404-
revLogTheirBalanceType, &theirBalance,
405-
),
427+
rl.OurOutputIndex.Record(),
428+
rl.TheirOutputIndex.Record(),
429+
rl.CommitTxHash.Record(),
430+
ourBalance.Record(),
431+
theirBalance.Record(),
406432
)
407433
if err != nil {
408434
return rl, err
@@ -414,14 +440,12 @@ func deserializeRevocationLog(r io.Reader) (RevocationLog, error) {
414440
return rl, err
415441
}
416442

417-
if t, ok := parsedTypes[revLogOurBalanceType]; ok && t == nil {
418-
lb := lnwire.MilliSatoshi(ourBalance)
419-
rl.OurBalance = &lb
443+
if t, ok := parsedTypes[ourBalance.TlvType()]; ok && t == nil {
444+
rl.OurBalance = tlv.SomeRecordT(ourBalance)
420445
}
421446

422-
if t, ok := parsedTypes[revLogTheirBalanceType]; ok && t == nil {
423-
rb := lnwire.MilliSatoshi(theirBalance)
424-
rl.TheirBalance = &rb
447+
if t, ok := parsedTypes[theirBalance.TlvType()]; ok && t == nil {
448+
rl.TheirBalance = tlv.SomeRecordT(theirBalance)
425449
}
426450

427451
// 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"
@@ -81,12 +82,11 @@ var (
8182
}},
8283
}
8384

84-
testRevocationLogNoAmts = RevocationLog{
85-
OurOutputIndex: 0,
86-
TheirOutputIndex: 1,
87-
CommitTxHash: testChannelCommit.CommitTx.TxHash(),
88-
HTLCEntries: []*HTLCEntry{&testHTLCEntry},
89-
}
85+
testRevocationLogNoAmts = NewRevocationLog(
86+
0, 1, testChannelCommit.CommitTx.TxHash(),
87+
fn.None[lnwire.MilliSatoshi](), fn.None[lnwire.MilliSatoshi](),
88+
[]*HTLCEntry{&testHTLCEntry},
89+
)
9090
testRevocationLogNoAmtsBytes = []byte{
9191
// Body length 42.
9292
0x2a,
@@ -102,14 +102,11 @@ var (
102102
0xc8, 0x22, 0x51, 0xb1, 0x5b, 0xa0, 0xbf, 0xd,
103103
}
104104

105-
testRevocationLogWithAmts = RevocationLog{
106-
OurOutputIndex: 0,
107-
TheirOutputIndex: 1,
108-
CommitTxHash: testChannelCommit.CommitTx.TxHash(),
109-
HTLCEntries: []*HTLCEntry{&testHTLCEntry},
110-
OurBalance: &localBalance,
111-
TheirBalance: &remoteBalance,
112-
}
105+
testRevocationLogWithAmts = NewRevocationLog(
106+
0, 1, testChannelCommit.CommitTx.TxHash(),
107+
fn.Some(localBalance), fn.Some(remoteBalance),
108+
[]*HTLCEntry{&testHTLCEntry},
109+
)
113110
testRevocationLogWithAmtsBytes = []byte{
114111
// Body length 52.
115112
0x34,

0 commit comments

Comments
 (0)