@@ -172,22 +172,32 @@ type HTLCEntry struct {
172172 //
173173 // NOTE: this field is the memory representation of the field amtUint.
174174 Amt tlv.RecordT [tlv.TlvType4 , tlv.BigSizeT [btcutil.Amount ]]
175+
176+ // CustomBlob is an optional blob that can be used to store information
177+ // specific to revocation handling for a custom channel type.
178+ CustomBlob tlv.OptionalRecordT [tlv.TlvType5 , tlv.Blob ]
175179}
176180
177181// toTlvStream converts an HTLCEntry record into a tlv representation.
178182func (h * HTLCEntry ) toTlvStream () (* tlv.Stream , error ) {
179- return tlv .NewStream (
183+ records := [] tlv.Record {
180184 h .RHash .Record (),
181185 h .RefundTimeout .Record (),
182186 h .OutputIndex .Record (),
183187 h .Incoming .Record (),
184188 h .Amt .Record (),
185- )
189+ }
190+
191+ h .CustomBlob .WhenSome (func (r tlv.RecordT [tlv.TlvType5 , tlv.Blob ]) {
192+ records = append (records , r .Record ())
193+ })
194+
195+ return tlv .NewStream (records ... )
186196}
187197
188198// NewHTLCEntryFromHTLC creates a new HTLCEntry from an HTLC.
189199func NewHTLCEntryFromHTLC (htlc HTLC ) * HTLCEntry {
190- return & HTLCEntry {
200+ h := & HTLCEntry {
191201 RHash : tlv.NewRecordT [tlv.TlvType0 ](
192202 NewSparsePayHash (htlc .RHash ),
193203 ),
@@ -202,6 +212,16 @@ func NewHTLCEntryFromHTLC(htlc HTLC) *HTLCEntry {
202212 tlv .NewBigSizeT (htlc .Amt .ToSatoshis ()),
203213 ),
204214 }
215+
216+ if len (htlc .ExtraData ) != 0 {
217+ h .CustomBlob = tlv .SomeRecordT (
218+ tlv.NewPrimitiveRecord [tlv.TlvType5 , tlv.Blob ](
219+ htlc .ExtraData ,
220+ ),
221+ )
222+ }
223+
224+ return h
205225}
206226
207227// RevocationLog stores the info needed to construct a breach retribution. Its
@@ -244,13 +264,19 @@ type RevocationLog struct {
244264 // this field, it could be the case that the field is not present for
245265 // all revocation logs.
246266 TheirBalance tlv.OptionalRecordT [tlv.TlvType4 , BigSizeMilliSatoshi ]
267+
268+ // CustomBlob is an optional blob that can be used to store information
269+ // specific to a custom channel type. This information is only created
270+ // at channel funding time, and after wards is to be considered
271+ // immutable.
272+ CustomBlob tlv.OptionalRecordT [tlv.TlvType5 , tlv.Blob ]
247273}
248274
249275// NewRevocationLog creates a new RevocationLog from the given parameters.
250276func NewRevocationLog (ourOutputIndex uint16 , theirOutputIndex uint16 ,
251277 commitHash [32 ]byte , ourBalance ,
252- theirBalance fn.Option [lnwire.MilliSatoshi ],
253- htlcs [] * HTLCEntry ) RevocationLog {
278+ theirBalance fn.Option [lnwire.MilliSatoshi ], htlcs [] * HTLCEntry ,
279+ customBlob fn. Option [tlv. Blob ] ) RevocationLog {
254280
255281 rl := RevocationLog {
256282 OurOutputIndex : tlv.NewPrimitiveRecord [tlv.TlvType0 ](
@@ -275,6 +301,12 @@ func NewRevocationLog(ourOutputIndex uint16, theirOutputIndex uint16,
275301 ))
276302 })
277303
304+ customBlob .WhenSome (func (blob tlv.Blob ) {
305+ rl .CustomBlob = tlv .SomeRecordT (
306+ tlv.NewPrimitiveRecord [tlv.TlvType5 , tlv.Blob ](blob ),
307+ )
308+ })
309+
278310 return rl
279311}
280312
@@ -306,6 +338,12 @@ func putRevocationLog(bucket kvdb.RwBucket, commit *ChannelCommitment,
306338 HTLCEntries : make ([]* HTLCEntry , 0 , len (commit .Htlcs )),
307339 }
308340
341+ commit .CustomBlob .WhenSome (func (blob tlv.Blob ) {
342+ rl .CustomBlob = tlv .SomeRecordT (
343+ tlv.NewPrimitiveRecord [tlv.TlvType5 , tlv.Blob ](blob ),
344+ )
345+ })
346+
309347 if ! noAmtData {
310348 rl .OurBalance = tlv .SomeRecordT (tlv.NewRecordT [tlv.TlvType3 ](
311349 tlv .NewBigSizeT (commit .LocalBalance ),
@@ -381,6 +419,10 @@ func serializeRevocationLog(w io.Writer, rl *RevocationLog) error {
381419 },
382420 )
383421
422+ rl .CustomBlob .WhenSome (func (r tlv.RecordT [tlv.TlvType5 , tlv.Blob ]) {
423+ records = append (records , r .Record ())
424+ })
425+
384426 // Create the tlv stream.
385427 tlvStream , err := tlv .NewStream (records ... )
386428 if err != nil {
@@ -421,6 +463,7 @@ func deserializeRevocationLog(r io.Reader) (RevocationLog, error) {
421463
422464 ourBalance := rl .OurBalance .Zero ()
423465 theirBalance := rl .TheirBalance .Zero ()
466+ customBlob := rl .CustomBlob .Zero ()
424467
425468 // Create the tlv stream.
426469 tlvStream , err := tlv .NewStream (
@@ -429,6 +472,7 @@ func deserializeRevocationLog(r io.Reader) (RevocationLog, error) {
429472 rl .CommitTxHash .Record (),
430473 ourBalance .Record (),
431474 theirBalance .Record (),
475+ customBlob .Record (),
432476 )
433477 if err != nil {
434478 return rl , err
@@ -448,6 +492,10 @@ func deserializeRevocationLog(r io.Reader) (RevocationLog, error) {
448492 rl .TheirBalance = tlv .SomeRecordT (theirBalance )
449493 }
450494
495+ if t , ok := parsedTypes [customBlob .TlvType ()]; ok && t == nil {
496+ rl .CustomBlob = tlv .SomeRecordT (customBlob )
497+ }
498+
451499 // Read the HTLC entries.
452500 rl .HTLCEntries , err = deserializeHTLCEntries (r )
453501
@@ -462,21 +510,37 @@ func deserializeHTLCEntries(r io.Reader) ([]*HTLCEntry, error) {
462510 for {
463511 var htlc HTLCEntry
464512
513+ customBlob := htlc .CustomBlob .Zero ()
514+
465515 // Create the tlv stream.
466- tlvStream , err := htlc .toTlvStream ()
516+ records := []tlv.Record {
517+ htlc .RHash .Record (),
518+ htlc .RefundTimeout .Record (),
519+ htlc .OutputIndex .Record (),
520+ htlc .Incoming .Record (),
521+ htlc .Amt .Record (),
522+ customBlob .Record (),
523+ }
524+
525+ tlvStream , err := tlv .NewStream (records ... )
467526 if err != nil {
468527 return nil , err
469528 }
470529
471530 // Read the HTLC entry.
472- if _ , err := readTlvStream (r , tlvStream ); err != nil {
531+ parsedTypes , err := readTlvStream (r , tlvStream )
532+ if err != nil {
473533 // We've reached the end when hitting an EOF.
474534 if err == io .ErrUnexpectedEOF {
475535 break
476536 }
477537 return nil , err
478538 }
479539
540+ if t , ok := parsedTypes [customBlob .TlvType ()]; ok && t == nil {
541+ htlc .CustomBlob = tlv .SomeRecordT (customBlob )
542+ }
543+
480544 // Append the entry.
481545 htlcs = append (htlcs , & htlc )
482546 }
0 commit comments