@@ -164,22 +164,32 @@ type HTLCEntry struct {
164164 //
165165 // NOTE: this field is the memory representation of the field amtUint.
166166 Amt tlv.RecordT [tlv.TlvType4 , tlv.BigSizeT [btcutil.Amount ]]
167+
168+ // CustomBlob is an optional blob that can be used to store information
169+ // specific to revocation handling for a custom channel type.
170+ CustomBlob tlv.OptionalRecordT [tlv.TlvType5 , tlv.Blob ]
167171}
168172
169173// toTlvStream converts an HTLCEntry record into a tlv representation.
170174func (h * HTLCEntry ) toTlvStream () (* tlv.Stream , error ) {
171- return tlv .NewStream (
175+ records := [] tlv.Record {
172176 h .RHash .Record (),
173177 h .RefundTimeout .Record (),
174178 h .OutputIndex .Record (),
175179 h .Incoming .Record (),
176180 h .Amt .Record (),
177- )
181+ }
182+
183+ h .CustomBlob .WhenSome (func (r tlv.RecordT [tlv.TlvType5 , tlv.Blob ]) {
184+ records = append (records , r .Record ())
185+ })
186+
187+ return tlv .NewStream (records ... )
178188}
179189
180190// NewHTLCEntryFromHTLC creates a new HTLCEntry from an HTLC.
181191func NewHTLCEntryFromHTLC (htlc HTLC ) * HTLCEntry {
182- return & HTLCEntry {
192+ h := & HTLCEntry {
183193 RHash : tlv.NewRecordT [tlv.TlvType0 ](
184194 NewSparsePayHash (htlc .RHash ),
185195 ),
@@ -194,6 +204,16 @@ func NewHTLCEntryFromHTLC(htlc HTLC) *HTLCEntry {
194204 tlv .NewBigSizeT (htlc .Amt .ToSatoshis ()),
195205 ),
196206 }
207+
208+ if len (htlc .ExtraData ) != 0 {
209+ h .CustomBlob = tlv .SomeRecordT (
210+ tlv.NewPrimitiveRecord [tlv.TlvType5 , tlv.Blob ](
211+ htlc .ExtraData ,
212+ ),
213+ )
214+ }
215+
216+ return h
197217}
198218
199219// RevocationLog stores the info needed to construct a breach retribution. Its
@@ -236,13 +256,19 @@ type RevocationLog struct {
236256 // this field, it could be the case that the field is not present for
237257 // all revocation logs.
238258 TheirBalance tlv.OptionalRecordT [tlv.TlvType4 , BigSizeMilliSatoshi ]
259+
260+ // CustomBlob is an optional blob that can be used to store information
261+ // specific to a custom channel type. This information is only created
262+ // at channel funding time, and after wards is to be considered
263+ // immutable.
264+ CustomBlob tlv.OptionalRecordT [tlv.TlvType5 , tlv.Blob ]
239265}
240266
241267// NewRevocationLog creates a new RevocationLog from the given parameters.
242268func NewRevocationLog (ourOutputIndex uint16 , theirOutputIndex uint16 ,
243269 commitHash [32 ]byte , ourBalance ,
244- theirBalance fn.Option [lnwire.MilliSatoshi ],
245- htlcs [] * HTLCEntry ) RevocationLog {
270+ theirBalance fn.Option [lnwire.MilliSatoshi ], htlcs [] * HTLCEntry ,
271+ customBlob fn. Option [tlv. Blob ] ) RevocationLog {
246272
247273 rl := RevocationLog {
248274 OurOutputIndex : tlv.NewPrimitiveRecord [tlv.TlvType0 ](
@@ -267,6 +293,12 @@ func NewRevocationLog(ourOutputIndex uint16, theirOutputIndex uint16,
267293 ))
268294 })
269295
296+ customBlob .WhenSome (func (blob tlv.Blob ) {
297+ rl .CustomBlob = tlv .SomeRecordT (
298+ tlv.NewPrimitiveRecord [tlv.TlvType5 , tlv.Blob ](blob ),
299+ )
300+ })
301+
270302 return rl
271303}
272304
@@ -298,6 +330,12 @@ func putRevocationLog(bucket kvdb.RwBucket, commit *ChannelCommitment,
298330 HTLCEntries : make ([]* HTLCEntry , 0 , len (commit .Htlcs )),
299331 }
300332
333+ commit .CustomBlob .WhenSome (func (blob tlv.Blob ) {
334+ rl .CustomBlob = tlv .SomeRecordT (
335+ tlv.NewPrimitiveRecord [tlv.TlvType5 , tlv.Blob ](blob ),
336+ )
337+ })
338+
301339 if ! noAmtData {
302340 rl .OurBalance = tlv .SomeRecordT (tlv.NewRecordT [tlv.TlvType3 ](
303341 tlv .NewBigSizeT (commit .LocalBalance ),
@@ -373,6 +411,10 @@ func serializeRevocationLog(w io.Writer, rl *RevocationLog) error {
373411 },
374412 )
375413
414+ rl .CustomBlob .WhenSome (func (r tlv.RecordT [tlv.TlvType5 , tlv.Blob ]) {
415+ records = append (records , r .Record ())
416+ })
417+
376418 // Create the tlv stream.
377419 tlvStream , err := tlv .NewStream (records ... )
378420 if err != nil {
@@ -413,6 +455,7 @@ func deserializeRevocationLog(r io.Reader) (RevocationLog, error) {
413455
414456 ourBalance := rl .OurBalance .Zero ()
415457 theirBalance := rl .TheirBalance .Zero ()
458+ customBlob := rl .CustomBlob .Zero ()
416459
417460 // Create the tlv stream.
418461 tlvStream , err := tlv .NewStream (
@@ -421,6 +464,7 @@ func deserializeRevocationLog(r io.Reader) (RevocationLog, error) {
421464 rl .CommitTxHash .Record (),
422465 ourBalance .Record (),
423466 theirBalance .Record (),
467+ customBlob .Record (),
424468 )
425469 if err != nil {
426470 return rl , err
@@ -440,6 +484,10 @@ func deserializeRevocationLog(r io.Reader) (RevocationLog, error) {
440484 rl .TheirBalance = tlv .SomeRecordT (theirBalance )
441485 }
442486
487+ if t , ok := parsedTypes [customBlob .TlvType ()]; ok && t == nil {
488+ rl .CustomBlob = tlv .SomeRecordT (customBlob )
489+ }
490+
443491 // Read the HTLC entries.
444492 rl .HTLCEntries , err = deserializeHTLCEntries (r )
445493
@@ -454,21 +502,37 @@ func deserializeHTLCEntries(r io.Reader) ([]*HTLCEntry, error) {
454502 for {
455503 var htlc HTLCEntry
456504
505+ customBlob := htlc .CustomBlob .Zero ()
506+
457507 // Create the tlv stream.
458- tlvStream , err := htlc .toTlvStream ()
508+ records := []tlv.Record {
509+ htlc .RHash .Record (),
510+ htlc .RefundTimeout .Record (),
511+ htlc .OutputIndex .Record (),
512+ htlc .Incoming .Record (),
513+ htlc .Amt .Record (),
514+ customBlob .Record (),
515+ }
516+
517+ tlvStream , err := tlv .NewStream (records ... )
459518 if err != nil {
460519 return nil , err
461520 }
462521
463522 // Read the HTLC entry.
464- if _ , err := readTlvStream (r , tlvStream ); err != nil {
523+ parsedTypes , err := readTlvStream (r , tlvStream )
524+ if err != nil {
465525 // We've reached the end when hitting an EOF.
466526 if err == io .ErrUnexpectedEOF {
467527 break
468528 }
469529 return nil , err
470530 }
471531
532+ if t , ok := parsedTypes [customBlob .TlvType ()]; ok && t == nil {
533+ htlc .CustomBlob = tlv .SomeRecordT (customBlob )
534+ }
535+
472536 // Append the entry.
473537 htlcs = append (htlcs , & htlc )
474538 }
0 commit comments