mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-01-18 13:27:56 +01:00
lnwire: add LeaseExpiry custom record for Open+AcceptChannel
This commit is contained in:
parent
6052a446dc
commit
6252563bc5
@ -99,6 +99,12 @@ type AcceptChannel struct {
|
||||
// open.
|
||||
ChannelType *ChannelType
|
||||
|
||||
// LeaseExpiry represents the absolute expiration height of a channel
|
||||
// lease. This is a custom TLV record that will only apply when a leased
|
||||
// channel is being opened using the script enforced lease commitment
|
||||
// type.
|
||||
LeaseExpiry *LeaseExpiry
|
||||
|
||||
// ExtraData is the set of data that was appended to this message to
|
||||
// fill out the full maximum transport message size. These fields can
|
||||
// be used to specify optional data such as custom TLV fields.
|
||||
@ -125,6 +131,9 @@ func (a *AcceptChannel) Encode(w *bytes.Buffer, pver uint32) error {
|
||||
if a.ChannelType != nil {
|
||||
recordProducers = append(recordProducers, a.ChannelType)
|
||||
}
|
||||
if a.LeaseExpiry != nil {
|
||||
recordProducers = append(recordProducers, a.LeaseExpiry)
|
||||
}
|
||||
err := EncodeMessageExtraData(&a.ExtraData, recordProducers...)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -226,9 +235,12 @@ func (a *AcceptChannel) Decode(r io.Reader, pver uint32) error {
|
||||
|
||||
// Next we'll parse out the set of known records, keeping the raw tlv
|
||||
// bytes untouched to ensure we don't drop any bytes erroneously.
|
||||
var chanType ChannelType
|
||||
var (
|
||||
chanType ChannelType
|
||||
leaseExpiry LeaseExpiry
|
||||
)
|
||||
typeMap, err := tlvRecords.ExtractRecords(
|
||||
&a.UpfrontShutdownScript, &chanType,
|
||||
&a.UpfrontShutdownScript, &chanType, &leaseExpiry,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -238,6 +250,9 @@ func (a *AcceptChannel) Decode(r io.Reader, pver uint32) error {
|
||||
if val, ok := typeMap[ChannelTypeRecordType]; ok && val == nil {
|
||||
a.ChannelType = &chanType
|
||||
}
|
||||
if val, ok := typeMap[LeaseExpiryRecordType]; ok && val == nil {
|
||||
a.LeaseExpiry = &leaseExpiry
|
||||
}
|
||||
|
||||
a.ExtraData = tlvRecords
|
||||
|
||||
|
@ -360,7 +360,7 @@ func TestLightningWireProtocol(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
// 1/2 chance empty upfront shutdown script.
|
||||
// 1/2 chance empty TLV records.
|
||||
if r.Intn(2) == 0 {
|
||||
req.UpfrontShutdownScript, err = randDeliveryAddress(r)
|
||||
if err != nil {
|
||||
@ -370,6 +370,9 @@ func TestLightningWireProtocol(t *testing.T) {
|
||||
|
||||
req.ChannelType = new(ChannelType)
|
||||
*req.ChannelType = ChannelType(*randRawFeatureVector(r))
|
||||
|
||||
req.LeaseExpiry = new(LeaseExpiry)
|
||||
*req.LeaseExpiry = LeaseExpiry(1337)
|
||||
} else {
|
||||
req.UpfrontShutdownScript = []byte{}
|
||||
}
|
||||
@ -429,7 +432,7 @@ func TestLightningWireProtocol(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
// 1/2 chance empty upfront shutdown script.
|
||||
// 1/2 chance empty TLV records.
|
||||
if r.Intn(2) == 0 {
|
||||
req.UpfrontShutdownScript, err = randDeliveryAddress(r)
|
||||
if err != nil {
|
||||
@ -439,6 +442,9 @@ func TestLightningWireProtocol(t *testing.T) {
|
||||
|
||||
req.ChannelType = new(ChannelType)
|
||||
*req.ChannelType = ChannelType(*randRawFeatureVector(r))
|
||||
|
||||
req.LeaseExpiry = new(LeaseExpiry)
|
||||
*req.LeaseExpiry = LeaseExpiry(1337)
|
||||
} else {
|
||||
req.UpfrontShutdownScript = []byte{}
|
||||
}
|
||||
|
@ -135,6 +135,12 @@ type OpenChannel struct {
|
||||
// open.
|
||||
ChannelType *ChannelType
|
||||
|
||||
// LeaseExpiry represents the absolute expiration height of a channel
|
||||
// lease. This is a custom TLV record that will only apply when a leased
|
||||
// channel is being opened using the script enforced lease commitment
|
||||
// type.
|
||||
LeaseExpiry *LeaseExpiry
|
||||
|
||||
// ExtraData is the set of data that was appended to this message to
|
||||
// fill out the full maximum transport message size. These fields can
|
||||
// be used to specify optional data such as custom TLV fields.
|
||||
@ -160,6 +166,9 @@ func (o *OpenChannel) Encode(w *bytes.Buffer, pver uint32) error {
|
||||
if o.ChannelType != nil {
|
||||
recordProducers = append(recordProducers, o.ChannelType)
|
||||
}
|
||||
if o.LeaseExpiry != nil {
|
||||
recordProducers = append(recordProducers, o.LeaseExpiry)
|
||||
}
|
||||
err := EncodeMessageExtraData(&o.ExtraData, recordProducers...)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -282,9 +291,12 @@ func (o *OpenChannel) Decode(r io.Reader, pver uint32) error {
|
||||
|
||||
// Next we'll parse out the set of known records, keeping the raw tlv
|
||||
// bytes untouched to ensure we don't drop any bytes erroneously.
|
||||
var chanType ChannelType
|
||||
var (
|
||||
chanType ChannelType
|
||||
leaseExpiry LeaseExpiry
|
||||
)
|
||||
typeMap, err := tlvRecords.ExtractRecords(
|
||||
&o.UpfrontShutdownScript, &chanType,
|
||||
&o.UpfrontShutdownScript, &chanType, &leaseExpiry,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -294,6 +306,9 @@ func (o *OpenChannel) Decode(r io.Reader, pver uint32) error {
|
||||
if val, ok := typeMap[ChannelTypeRecordType]; ok && val == nil {
|
||||
o.ChannelType = &chanType
|
||||
}
|
||||
if val, ok := typeMap[LeaseExpiryRecordType]; ok && val == nil {
|
||||
o.LeaseExpiry = &leaseExpiry
|
||||
}
|
||||
|
||||
o.ExtraData = tlvRecords
|
||||
|
||||
|
52
lnwire/typed_lease_expiry.go
Normal file
52
lnwire/typed_lease_expiry.go
Normal file
@ -0,0 +1,52 @@
|
||||
package lnwire
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/lightningnetwork/lnd/tlv"
|
||||
)
|
||||
|
||||
const (
|
||||
// LeaseExpiryType is the type of the experimental record used to
|
||||
// communicate the expiration of a channel lease throughout the channel
|
||||
// funding process.
|
||||
//
|
||||
// TODO: Decide on actual TLV type. Custom records start at 2^16.
|
||||
LeaseExpiryRecordType tlv.Type = 1 << 16
|
||||
)
|
||||
|
||||
// LeaseExpiry represents the absolute expiration height of a channel lease. All
|
||||
// outputs that pay directly to the channel initiator are locked until this
|
||||
// height is reached.
|
||||
type LeaseExpiry uint32
|
||||
|
||||
// Record returns a TLV record that can be used to encode/decode the LeaseExpiry
|
||||
// type from a given TLV stream.
|
||||
func (l *LeaseExpiry) Record() tlv.Record {
|
||||
return tlv.MakeStaticRecord(
|
||||
LeaseExpiryRecordType, l, 4, leaseExpiryEncoder, leaseExpiryDecoder,
|
||||
)
|
||||
}
|
||||
|
||||
// leaseExpiryEncoder is a custom TLV encoder for the LeaseExpiry record.
|
||||
func leaseExpiryEncoder(w io.Writer, val interface{}, buf *[8]byte) error {
|
||||
if v, ok := val.(*LeaseExpiry); ok {
|
||||
return tlv.EUint32T(w, uint32(*v), buf)
|
||||
}
|
||||
|
||||
return tlv.NewTypeForEncodingErr(val, "lnwire.LeaseExpiry")
|
||||
}
|
||||
|
||||
// leaseExpiryDecoder is a custom TLV decoder for the LeaseExpiry record.
|
||||
func leaseExpiryDecoder(r io.Reader, val interface{}, buf *[8]byte, l uint64) error {
|
||||
if v, ok := val.(*LeaseExpiry); ok {
|
||||
var leaseExpiry uint32
|
||||
if err := tlv.DUint32(r, &leaseExpiry, buf, l); err != nil {
|
||||
return err
|
||||
}
|
||||
*v = LeaseExpiry(leaseExpiry)
|
||||
return nil
|
||||
}
|
||||
|
||||
return tlv.NewTypeForEncodingErr(val, "lnwire.LeaseExpiry")
|
||||
}
|
25
lnwire/typed_lease_expiry_test.go
Normal file
25
lnwire/typed_lease_expiry_test.go
Normal file
@ -0,0 +1,25 @@
|
||||
package lnwire
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// TestChannelTypeEncodeDecode tests that we're able to properly encode and
|
||||
// decode channel types within TLV streams.
|
||||
func TestLeaseExpiryEncodeDecode(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
leaseExpiry := LeaseExpiry(1337)
|
||||
|
||||
var extraData ExtraOpaqueData
|
||||
require.NoError(t, extraData.PackRecords(&leaseExpiry))
|
||||
|
||||
var leaseExpiry2 LeaseExpiry
|
||||
tlvs, err := extraData.ExtractRecords(&leaseExpiry2)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Contains(t, tlvs, LeaseExpiryRecordType)
|
||||
require.Equal(t, leaseExpiry, leaseExpiry2)
|
||||
}
|
Loading…
Reference in New Issue
Block a user