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 (
@@ -203,15 +203,15 @@ func NewHTLCEntryFromHTLC(htlc HTLC) *HTLCEntry {
203203type 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,
312355func 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.
376411func 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.
0 commit comments