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 (
1617const (
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
3131var (
@@ -211,15 +211,15 @@ func NewHTLCEntryFromHTLC(htlc HTLC) *HTLCEntry {
211211type 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,
320363func 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.
384419func 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.
0 commit comments