github.com/decred/dcrlnd@v0.7.6/lnwire/typed_lease_expiry.go (about)

     1  package lnwire
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/decred/dcrlnd/tlv"
     7  )
     8  
     9  const (
    10  	// LeaseExpiryType is the type of the experimental record used to
    11  	// communicate the expiration of a channel lease throughout the channel
    12  	// funding process.
    13  	//
    14  	// TODO: Decide on actual TLV type. Custom records start at 2^16.
    15  	LeaseExpiryRecordType tlv.Type = 1 << 16
    16  )
    17  
    18  // LeaseExpiry represents the absolute expiration height of a channel lease. All
    19  // outputs that pay directly to the channel initiator are locked until this
    20  // height is reached.
    21  type LeaseExpiry uint32
    22  
    23  // Record returns a TLV record that can be used to encode/decode the LeaseExpiry
    24  // type from a given TLV stream.
    25  func (l *LeaseExpiry) Record() tlv.Record {
    26  	return tlv.MakeStaticRecord(
    27  		LeaseExpiryRecordType, l, 4, leaseExpiryEncoder, leaseExpiryDecoder,
    28  	)
    29  }
    30  
    31  // leaseExpiryEncoder is a custom TLV encoder for the LeaseExpiry record.
    32  func leaseExpiryEncoder(w io.Writer, val interface{}, buf *[8]byte) error {
    33  	if v, ok := val.(*LeaseExpiry); ok {
    34  		return tlv.EUint32T(w, uint32(*v), buf)
    35  	}
    36  
    37  	return tlv.NewTypeForEncodingErr(val, "lnwire.LeaseExpiry")
    38  }
    39  
    40  // leaseExpiryDecoder is a custom TLV decoder for the LeaseExpiry record.
    41  func leaseExpiryDecoder(r io.Reader, val interface{}, buf *[8]byte, l uint64) error {
    42  	if v, ok := val.(*LeaseExpiry); ok {
    43  		var leaseExpiry uint32
    44  		if err := tlv.DUint32(r, &leaseExpiry, buf, l); err != nil {
    45  			return err
    46  		}
    47  		*v = LeaseExpiry(leaseExpiry)
    48  		return nil
    49  	}
    50  
    51  	return tlv.NewTypeForEncodingErr(val, "lnwire.LeaseExpiry")
    52  }