lnwire: convert DynPropose and DynCommit to use typed tlv records
This commit is contained in:
1 parent
79e4e74447
commit
a72cadf2ed
3 files changed
+163
-373
No files matched your search
+36
-128
@@ -5,7 +5,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/btcutil"
|
"github.com/btcsuite/btcd/btcutil"
|
||||||
"github.com/lightningnetwork/lnd/fn/v2"
|
|
||||||
"github.com/lightningnetwork/lnd/tlv"
|
"github.com/lightningnetwork/lnd/tlv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -43,75 +42,12 @@ func (dc *DynCommit) Encode(w *bytes.Buffer, _ uint32) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var tlvRecords []tlv.Record
|
var extra ExtraOpaqueData
|
||||||
dc.DustLimit.WhenSome(func(dl btcutil.Amount) {
|
err := extra.PackRecords(dynProposeRecords(&dc.DynPropose)...)
|
||||||
protoSats := uint64(dl)
|
|
||||||
tlvRecords = append(
|
|
||||||
tlvRecords, tlv.MakePrimitiveRecord(
|
|
||||||
DPDustLimitSatoshis, &protoSats,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
dc.MaxValueInFlight.WhenSome(func(max MilliSatoshi) {
|
|
||||||
protoSats := uint64(max)
|
|
||||||
tlvRecords = append(
|
|
||||||
tlvRecords, tlv.MakePrimitiveRecord(
|
|
||||||
DPMaxHtlcValueInFlightMsat, &protoSats,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
dc.HtlcMinimum.WhenSome(func(min MilliSatoshi) {
|
|
||||||
protoSats := uint64(min)
|
|
||||||
tlvRecords = append(
|
|
||||||
tlvRecords, tlv.MakePrimitiveRecord(
|
|
||||||
DPHtlcMinimumMsat, &protoSats,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
dc.ChannelReserve.WhenSome(func(min btcutil.Amount) {
|
|
||||||
channelReserve := uint64(min)
|
|
||||||
tlvRecords = append(
|
|
||||||
tlvRecords, tlv.MakePrimitiveRecord(
|
|
||||||
DPChannelReserveSatoshis, &channelReserve,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
dc.CsvDelay.WhenSome(func(wait uint16) {
|
|
||||||
tlvRecords = append(
|
|
||||||
tlvRecords, tlv.MakePrimitiveRecord(
|
|
||||||
DPToSelfDelay, &wait,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
dc.MaxAcceptedHTLCs.WhenSome(func(max uint16) {
|
|
||||||
tlvRecords = append(
|
|
||||||
tlvRecords, tlv.MakePrimitiveRecord(
|
|
||||||
DPMaxAcceptedHtlcs, &max,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
dc.ChannelType.WhenSome(func(ty ChannelType) {
|
|
||||||
tlvRecords = append(
|
|
||||||
tlvRecords, tlv.MakeDynamicRecord(
|
|
||||||
DPChannelType, &ty,
|
|
||||||
ty.featureBitLen,
|
|
||||||
channelTypeEncoder, channelTypeDecoder,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
tlv.SortRecords(tlvRecords)
|
|
||||||
|
|
||||||
tlvStream, err := tlv.NewStream(tlvRecords...)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
dc.ExtraData = extra
|
||||||
var extraBytesWriter bytes.Buffer
|
|
||||||
if err := tlvStream.Encode(&extraBytesWriter); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
dc.ExtraData = ExtraOpaqueData(extraBytesWriter.Bytes())
|
|
||||||
|
|
||||||
return WriteBytes(w, dc.ExtraData)
|
return WriteBytes(w, dc.ExtraData)
|
||||||
}
|
}
|
||||||
@@ -135,80 +71,52 @@ func (dc *DynCommit) Decode(r io.Reader, _ uint32) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Prepare receiving buffers to be filled by TLV extraction.
|
// Prepare receiving buffers to be filled by TLV extraction.
|
||||||
var dustLimitScratch uint64
|
var dustLimit tlv.RecordT[tlv.TlvType0, uint64]
|
||||||
dustLimit := tlv.MakePrimitiveRecord(
|
var maxValue tlv.RecordT[tlv.TlvType2, uint64]
|
||||||
DPDustLimitSatoshis, &dustLimitScratch,
|
var htlcMin tlv.RecordT[tlv.TlvType4, uint64]
|
||||||
|
var reserve tlv.RecordT[tlv.TlvType6, uint64]
|
||||||
|
csvDelay := dc.CsvDelay.Zero()
|
||||||
|
maxHtlcs := dc.MaxAcceptedHTLCs.Zero()
|
||||||
|
chanType := dc.ChannelType.Zero()
|
||||||
|
|
||||||
|
typeMap, err := tlvRecords.ExtractRecords(
|
||||||
|
&dustLimit, &maxValue, &htlcMin, &reserve, &csvDelay, &maxHtlcs,
|
||||||
|
&chanType,
|
||||||
)
|
)
|
||||||
|
|
||||||
var maxValueScratch uint64
|
|
||||||
maxValue := tlv.MakePrimitiveRecord(
|
|
||||||
DPMaxHtlcValueInFlightMsat, &maxValueScratch,
|
|
||||||
)
|
|
||||||
|
|
||||||
var htlcMinScratch uint64
|
|
||||||
htlcMin := tlv.MakePrimitiveRecord(
|
|
||||||
DPHtlcMinimumMsat, &htlcMinScratch,
|
|
||||||
)
|
|
||||||
|
|
||||||
var reserveScratch uint64
|
|
||||||
reserve := tlv.MakePrimitiveRecord(
|
|
||||||
DPChannelReserveSatoshis, &reserveScratch,
|
|
||||||
)
|
|
||||||
|
|
||||||
var csvDelayScratch uint16
|
|
||||||
csvDelay := tlv.MakePrimitiveRecord(DPToSelfDelay, &csvDelayScratch)
|
|
||||||
|
|
||||||
var maxHtlcsScratch uint16
|
|
||||||
maxHtlcs := tlv.MakePrimitiveRecord(
|
|
||||||
DPMaxAcceptedHtlcs, &maxHtlcsScratch,
|
|
||||||
)
|
|
||||||
|
|
||||||
var chanTypeScratch ChannelType
|
|
||||||
chanType := tlv.MakeDynamicRecord(
|
|
||||||
DPChannelType, &chanTypeScratch, chanTypeScratch.featureBitLen,
|
|
||||||
channelTypeEncoder, channelTypeDecoder,
|
|
||||||
)
|
|
||||||
|
|
||||||
// Create set of Records to read TLV bytestream into.
|
|
||||||
records := []tlv.Record{
|
|
||||||
dustLimit, maxValue, htlcMin, reserve, csvDelay, maxHtlcs,
|
|
||||||
chanType,
|
|
||||||
}
|
|
||||||
tlv.SortRecords(records)
|
|
||||||
|
|
||||||
// Read TLV stream into record set.
|
|
||||||
extraBytesReader := bytes.NewReader(tlvRecords)
|
|
||||||
tlvStream, err := tlv.NewStream(records...)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
typeMap, err := tlvStream.DecodeWithParsedTypesP2P(extraBytesReader)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the results of the TLV Stream decoding and appropriately set
|
// Check the results of the TLV Stream decoding and appropriately set
|
||||||
// message fields.
|
// message fields.
|
||||||
if val, ok := typeMap[DPDustLimitSatoshis]; ok && val == nil {
|
if val, ok := typeMap[dc.DustLimit.TlvType()]; ok && val == nil {
|
||||||
dc.DustLimit = fn.Some(btcutil.Amount(dustLimitScratch))
|
var rec tlv.RecordT[tlv.TlvType0, btcutil.Amount]
|
||||||
|
rec.Val = btcutil.Amount(dustLimit.Val)
|
||||||
|
dc.DustLimit = tlv.SomeRecordT(rec)
|
||||||
}
|
}
|
||||||
if val, ok := typeMap[DPMaxHtlcValueInFlightMsat]; ok && val == nil {
|
if val, ok := typeMap[dc.MaxValueInFlight.TlvType()]; ok && val == nil {
|
||||||
dc.MaxValueInFlight = fn.Some(MilliSatoshi(maxValueScratch))
|
var rec tlv.RecordT[tlv.TlvType2, MilliSatoshi]
|
||||||
|
rec.Val = MilliSatoshi(maxValue.Val)
|
||||||
|
dc.MaxValueInFlight = tlv.SomeRecordT(rec)
|
||||||
}
|
}
|
||||||
if val, ok := typeMap[DPHtlcMinimumMsat]; ok && val == nil {
|
if val, ok := typeMap[dc.HtlcMinimum.TlvType()]; ok && val == nil {
|
||||||
dc.HtlcMinimum = fn.Some(MilliSatoshi(htlcMinScratch))
|
var rec tlv.RecordT[tlv.TlvType4, MilliSatoshi]
|
||||||
|
rec.Val = MilliSatoshi(htlcMin.Val)
|
||||||
|
dc.HtlcMinimum = tlv.SomeRecordT(rec)
|
||||||
}
|
}
|
||||||
if val, ok := typeMap[DPChannelReserveSatoshis]; ok && val == nil {
|
if val, ok := typeMap[dc.ChannelReserve.TlvType()]; ok && val == nil {
|
||||||
dc.ChannelReserve = fn.Some(btcutil.Amount(reserveScratch))
|
var rec tlv.RecordT[tlv.TlvType6, btcutil.Amount]
|
||||||
|
rec.Val = btcutil.Amount(reserve.Val)
|
||||||
|
dc.ChannelReserve = tlv.SomeRecordT(rec)
|
||||||
}
|
}
|
||||||
if val, ok := typeMap[DPToSelfDelay]; ok && val == nil {
|
if val, ok := typeMap[dc.CsvDelay.TlvType()]; ok && val == nil {
|
||||||
dc.CsvDelay = fn.Some(csvDelayScratch)
|
dc.CsvDelay = tlv.SomeRecordT(csvDelay)
|
||||||
}
|
}
|
||||||
if val, ok := typeMap[DPMaxAcceptedHtlcs]; ok && val == nil {
|
if val, ok := typeMap[dc.MaxAcceptedHTLCs.TlvType()]; ok && val == nil {
|
||||||
dc.MaxAcceptedHTLCs = fn.Some(maxHtlcsScratch)
|
dc.MaxAcceptedHTLCs = tlv.SomeRecordT(maxHtlcs)
|
||||||
}
|
}
|
||||||
if val, ok := typeMap[DPChannelType]; ok && val == nil {
|
if val, ok := typeMap[dc.ChannelType.TlvType()]; ok && val == nil {
|
||||||
dc.ChannelType = fn.Some(chanTypeScratch)
|
dc.ChannelType = tlv.SomeRecordT(chanType)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(tlvRecords) != 0 {
|
if len(tlvRecords) != 0 {
|
||||||
|
|||||||
+105
-234
@@ -5,40 +5,9 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/btcutil"
|
"github.com/btcsuite/btcd/btcutil"
|
||||||
"github.com/lightningnetwork/lnd/fn/v2"
|
|
||||||
"github.com/lightningnetwork/lnd/tlv"
|
"github.com/lightningnetwork/lnd/tlv"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
// DPDustLimitSatoshis is the TLV type number that identifies the record
|
|
||||||
// for DynPropose.DustLimit.
|
|
||||||
DPDustLimitSatoshis tlv.Type = 0
|
|
||||||
|
|
||||||
// DPMaxHtlcValueInFlightMsat is the TLV type number that identifies the
|
|
||||||
// record for DynPropose.MaxValueInFlight.
|
|
||||||
DPMaxHtlcValueInFlightMsat tlv.Type = 2
|
|
||||||
|
|
||||||
// DPHtlcMinimumMsat is the TLV type number that identifies the record
|
|
||||||
// for DynPropose.HtlcMinimum.
|
|
||||||
DPHtlcMinimumMsat tlv.Type = 4
|
|
||||||
|
|
||||||
// DPChannelReserveSatoshis is the TLV type number that identifies the
|
|
||||||
// for DynPropose.ChannelReserve.
|
|
||||||
DPChannelReserveSatoshis tlv.Type = 6
|
|
||||||
|
|
||||||
// DPToSelfDelay is the TLV type number that identifies the record for
|
|
||||||
// DynPropose.CsvDelay.
|
|
||||||
DPToSelfDelay tlv.Type = 8
|
|
||||||
|
|
||||||
// DPMaxAcceptedHtlcs is the TLV type number that identifies the record
|
|
||||||
// for DynPropose.MaxAcceptedHTLCs.
|
|
||||||
DPMaxAcceptedHtlcs tlv.Type = 10
|
|
||||||
|
|
||||||
// DPChannelType is the TLV type number that identifies the record for
|
|
||||||
// DynPropose.ChannelType.
|
|
||||||
DPChannelType tlv.Type = 12
|
|
||||||
)
|
|
||||||
|
|
||||||
// DynPropose is a message that is sent during a dynamic commitments negotiation
|
// DynPropose is a message that is sent during a dynamic commitments negotiation
|
||||||
// process. It is sent by both parties to propose new channel parameters.
|
// process. It is sent by both parties to propose new channel parameters.
|
||||||
type DynPropose struct {
|
type DynPropose struct {
|
||||||
@@ -48,31 +17,31 @@ type DynPropose struct {
|
|||||||
|
|
||||||
// DustLimit, if not nil, proposes a change to the dust_limit_satoshis
|
// DustLimit, if not nil, proposes a change to the dust_limit_satoshis
|
||||||
// for the sender's commitment transaction.
|
// for the sender's commitment transaction.
|
||||||
DustLimit fn.Option[btcutil.Amount]
|
DustLimit tlv.OptionalRecordT[tlv.TlvType0, btcutil.Amount]
|
||||||
|
|
||||||
// MaxValueInFlight, if not nil, proposes a change to the
|
// MaxValueInFlight, if not nil, proposes a change to the
|
||||||
// max_htlc_value_in_flight_msat limit of the sender.
|
// max_htlc_value_in_flight_msat limit of the sender.
|
||||||
MaxValueInFlight fn.Option[MilliSatoshi]
|
MaxValueInFlight tlv.OptionalRecordT[tlv.TlvType2, MilliSatoshi]
|
||||||
|
|
||||||
// HtlcMinimum, if not nil, proposes a change to the htlc_minimum_msat
|
// HtlcMinimum, if not nil, proposes a change to the htlc_minimum_msat
|
||||||
// floor of the sender.
|
// floor of the sender.
|
||||||
HtlcMinimum fn.Option[MilliSatoshi]
|
HtlcMinimum tlv.OptionalRecordT[tlv.TlvType4, MilliSatoshi]
|
||||||
|
|
||||||
// ChannelReserve, if not nil, proposes a change to the
|
// ChannelReserve, if not nil, proposes a change to the
|
||||||
// channel_reserve_satoshis requirement of the recipient.
|
// channel_reserve_satoshis requirement of the recipient.
|
||||||
ChannelReserve fn.Option[btcutil.Amount]
|
ChannelReserve tlv.OptionalRecordT[tlv.TlvType6, btcutil.Amount]
|
||||||
|
|
||||||
// CsvDelay, if not nil, proposes a change to the to_self_delay
|
// CsvDelay, if not nil, proposes a change to the to_self_delay
|
||||||
// requirement of the recipient.
|
// requirement of the recipient.
|
||||||
CsvDelay fn.Option[uint16]
|
CsvDelay tlv.OptionalRecordT[tlv.TlvType8, uint16]
|
||||||
|
|
||||||
// MaxAcceptedHTLCs, if not nil, proposes a change to the
|
// MaxAcceptedHTLCs, if not nil, proposes a change to the
|
||||||
// max_accepted_htlcs limit of the sender.
|
// max_accepted_htlcs limit of the sender.
|
||||||
MaxAcceptedHTLCs fn.Option[uint16]
|
MaxAcceptedHTLCs tlv.OptionalRecordT[tlv.TlvType10, uint16]
|
||||||
|
|
||||||
// ChannelType, if not nil, proposes a change to the channel_type
|
// ChannelType, if not nil, proposes a change to the channel_type
|
||||||
// parameter.
|
// parameter.
|
||||||
ChannelType fn.Option[ChannelType]
|
ChannelType tlv.OptionalRecordT[tlv.TlvType12, ChannelType]
|
||||||
|
|
||||||
// ExtraData is the set of data that was appended to this message to
|
// ExtraData is the set of data that was appended to this message to
|
||||||
// fill out the full maximum transport message size. These fields can
|
// fill out the full maximum transport message size. These fields can
|
||||||
@@ -97,79 +66,17 @@ var _ SizeableMessage = (*DynPropose)(nil)
|
|||||||
//
|
//
|
||||||
// This is a part of the lnwire.Message interface.
|
// This is a part of the lnwire.Message interface.
|
||||||
func (dp *DynPropose) Encode(w *bytes.Buffer, _ uint32) error {
|
func (dp *DynPropose) Encode(w *bytes.Buffer, _ uint32) error {
|
||||||
var tlvRecords []tlv.Record
|
|
||||||
dp.DustLimit.WhenSome(func(dl btcutil.Amount) {
|
|
||||||
protoSats := uint64(dl)
|
|
||||||
tlvRecords = append(
|
|
||||||
tlvRecords, tlv.MakePrimitiveRecord(
|
|
||||||
DPDustLimitSatoshis, &protoSats,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
dp.MaxValueInFlight.WhenSome(func(max MilliSatoshi) {
|
|
||||||
protoSats := uint64(max)
|
|
||||||
tlvRecords = append(
|
|
||||||
tlvRecords, tlv.MakePrimitiveRecord(
|
|
||||||
DPMaxHtlcValueInFlightMsat, &protoSats,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
dp.HtlcMinimum.WhenSome(func(min MilliSatoshi) {
|
|
||||||
protoSats := uint64(min)
|
|
||||||
tlvRecords = append(
|
|
||||||
tlvRecords, tlv.MakePrimitiveRecord(
|
|
||||||
DPHtlcMinimumMsat, &protoSats,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
dp.ChannelReserve.WhenSome(func(min btcutil.Amount) {
|
|
||||||
channelReserve := uint64(min)
|
|
||||||
tlvRecords = append(
|
|
||||||
tlvRecords, tlv.MakePrimitiveRecord(
|
|
||||||
DPChannelReserveSatoshis, &channelReserve,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
dp.CsvDelay.WhenSome(func(wait uint16) {
|
|
||||||
tlvRecords = append(
|
|
||||||
tlvRecords, tlv.MakePrimitiveRecord(
|
|
||||||
DPToSelfDelay, &wait,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
dp.MaxAcceptedHTLCs.WhenSome(func(max uint16) {
|
|
||||||
tlvRecords = append(
|
|
||||||
tlvRecords, tlv.MakePrimitiveRecord(
|
|
||||||
DPMaxAcceptedHtlcs, &max,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
dp.ChannelType.WhenSome(func(ty ChannelType) {
|
|
||||||
tlvRecords = append(
|
|
||||||
tlvRecords, tlv.MakeDynamicRecord(
|
|
||||||
DPChannelType, &ty,
|
|
||||||
ty.featureBitLen,
|
|
||||||
channelTypeEncoder, channelTypeDecoder,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
tlv.SortRecords(tlvRecords)
|
|
||||||
|
|
||||||
tlvStream, err := tlv.NewStream(tlvRecords...)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
var extraBytesWriter bytes.Buffer
|
|
||||||
if err := tlvStream.Encode(&extraBytesWriter); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
dp.ExtraData = ExtraOpaqueData(extraBytesWriter.Bytes())
|
|
||||||
|
|
||||||
if err := WriteChannelID(w, dp.ChanID); err != nil {
|
if err := WriteChannelID(w, dp.ChanID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
producers := dynProposeRecords(dp)
|
||||||
|
|
||||||
|
err := EncodeMessageExtraData(&dp.ExtraData, producers...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return WriteBytes(w, dp.ExtraData)
|
return WriteBytes(w, dp.ExtraData)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,81 +98,52 @@ func (dp *DynPropose) Decode(r io.Reader, _ uint32) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Prepare receiving buffers to be filled by TLV extraction.
|
// Prepare receiving buffers to be filled by TLV extraction.
|
||||||
var dustLimitScratch uint64
|
var dustLimit tlv.RecordT[tlv.TlvType0, uint64]
|
||||||
dustLimit := tlv.MakePrimitiveRecord(
|
var maxValue tlv.RecordT[tlv.TlvType2, uint64]
|
||||||
DPDustLimitSatoshis, &dustLimitScratch,
|
var htlcMin tlv.RecordT[tlv.TlvType4, uint64]
|
||||||
|
var reserve tlv.RecordT[tlv.TlvType6, uint64]
|
||||||
|
csvDelay := dp.CsvDelay.Zero()
|
||||||
|
maxHtlcs := dp.MaxAcceptedHTLCs.Zero()
|
||||||
|
chanType := dp.ChannelType.Zero()
|
||||||
|
|
||||||
|
typeMap, err := tlvRecords.ExtractRecords(
|
||||||
|
&dustLimit, &maxValue, &htlcMin, &reserve, &csvDelay, &maxHtlcs,
|
||||||
|
&chanType,
|
||||||
)
|
)
|
||||||
|
|
||||||
var maxValueScratch uint64
|
|
||||||
maxValue := tlv.MakePrimitiveRecord(
|
|
||||||
DPMaxHtlcValueInFlightMsat, &maxValueScratch,
|
|
||||||
)
|
|
||||||
|
|
||||||
var htlcMinScratch uint64
|
|
||||||
htlcMin := tlv.MakePrimitiveRecord(
|
|
||||||
DPHtlcMinimumMsat, &htlcMinScratch,
|
|
||||||
)
|
|
||||||
|
|
||||||
var reserveScratch uint64
|
|
||||||
reserve := tlv.MakePrimitiveRecord(
|
|
||||||
DPChannelReserveSatoshis, &reserveScratch,
|
|
||||||
)
|
|
||||||
|
|
||||||
var csvDelayScratch uint16
|
|
||||||
csvDelay := tlv.MakePrimitiveRecord(DPToSelfDelay, &csvDelayScratch)
|
|
||||||
|
|
||||||
var maxHtlcsScratch uint16
|
|
||||||
maxHtlcs := tlv.MakePrimitiveRecord(
|
|
||||||
DPMaxAcceptedHtlcs, &maxHtlcsScratch,
|
|
||||||
)
|
|
||||||
|
|
||||||
var chanTypeScratch ChannelType
|
|
||||||
chanType := tlv.MakeDynamicRecord(
|
|
||||||
DPChannelType, &chanTypeScratch, chanTypeScratch.featureBitLen,
|
|
||||||
channelTypeEncoder, channelTypeDecoder,
|
|
||||||
)
|
|
||||||
|
|
||||||
// Create set of Records to read TLV bytestream into.
|
|
||||||
records := []tlv.Record{
|
|
||||||
dustLimit, maxValue, htlcMin, reserve, csvDelay, maxHtlcs,
|
|
||||||
chanType,
|
|
||||||
}
|
|
||||||
tlv.SortRecords(records)
|
|
||||||
|
|
||||||
// Read TLV stream into record set.
|
|
||||||
extraBytesReader := bytes.NewReader(tlvRecords)
|
|
||||||
tlvStream, err := tlv.NewStream(records...)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
typeMap, err := tlvStream.DecodeWithParsedTypesP2P(extraBytesReader)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the results of the TLV Stream decoding and appropriately set
|
// Check the results of the TLV Stream decoding and appropriately set
|
||||||
// message fields.
|
// message fields.
|
||||||
if val, ok := typeMap[DPDustLimitSatoshis]; ok && val == nil {
|
if val, ok := typeMap[dp.DustLimit.TlvType()]; ok && val == nil {
|
||||||
dp.DustLimit = fn.Some(btcutil.Amount(dustLimitScratch))
|
var rec tlv.RecordT[tlv.TlvType0, btcutil.Amount]
|
||||||
|
rec.Val = btcutil.Amount(dustLimit.Val)
|
||||||
|
dp.DustLimit = tlv.SomeRecordT(rec)
|
||||||
}
|
}
|
||||||
if val, ok := typeMap[DPMaxHtlcValueInFlightMsat]; ok && val == nil {
|
if val, ok := typeMap[dp.MaxValueInFlight.TlvType()]; ok && val == nil {
|
||||||
dp.MaxValueInFlight = fn.Some(MilliSatoshi(maxValueScratch))
|
var rec tlv.RecordT[tlv.TlvType2, MilliSatoshi]
|
||||||
|
rec.Val = MilliSatoshi(maxValue.Val)
|
||||||
|
dp.MaxValueInFlight = tlv.SomeRecordT(rec)
|
||||||
}
|
}
|
||||||
if val, ok := typeMap[DPHtlcMinimumMsat]; ok && val == nil {
|
if val, ok := typeMap[dp.HtlcMinimum.TlvType()]; ok && val == nil {
|
||||||
dp.HtlcMinimum = fn.Some(MilliSatoshi(htlcMinScratch))
|
var rec tlv.RecordT[tlv.TlvType4, MilliSatoshi]
|
||||||
|
rec.Val = MilliSatoshi(htlcMin.Val)
|
||||||
|
dp.HtlcMinimum = tlv.SomeRecordT(rec)
|
||||||
}
|
}
|
||||||
if val, ok := typeMap[DPChannelReserveSatoshis]; ok && val == nil {
|
if val, ok := typeMap[dp.ChannelReserve.TlvType()]; ok && val == nil {
|
||||||
dp.ChannelReserve = fn.Some(btcutil.Amount(reserveScratch))
|
var rec tlv.RecordT[tlv.TlvType6, btcutil.Amount]
|
||||||
|
rec.Val = btcutil.Amount(reserve.Val)
|
||||||
|
dp.ChannelReserve = tlv.SomeRecordT(rec)
|
||||||
}
|
}
|
||||||
if val, ok := typeMap[DPToSelfDelay]; ok && val == nil {
|
if val, ok := typeMap[dp.CsvDelay.TlvType()]; ok && val == nil {
|
||||||
dp.CsvDelay = fn.Some(csvDelayScratch)
|
dp.CsvDelay = tlv.SomeRecordT(csvDelay)
|
||||||
}
|
}
|
||||||
if val, ok := typeMap[DPMaxAcceptedHtlcs]; ok && val == nil {
|
if val, ok := typeMap[dp.MaxAcceptedHTLCs.TlvType()]; ok && val == nil {
|
||||||
dp.MaxAcceptedHTLCs = fn.Some(maxHtlcsScratch)
|
dp.MaxAcceptedHTLCs = tlv.SomeRecordT(maxHtlcs)
|
||||||
}
|
}
|
||||||
if val, ok := typeMap[DPChannelType]; ok && val == nil {
|
if val, ok := typeMap[dp.ChannelType.TlvType()]; ok && val == nil {
|
||||||
dp.ChannelType = fn.Some(chanTypeScratch)
|
dp.ChannelType = tlv.SomeRecordT(chanType)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(tlvRecords) != 0 {
|
if len(tlvRecords) != 0 {
|
||||||
@@ -294,74 +172,67 @@ func (dp *DynPropose) SerializedSize() (uint32, error) {
|
|||||||
// the parameters on deck for changing) and serializes just this component. The
|
// the parameters on deck for changing) and serializes just this component. The
|
||||||
// main purpose of this is to make it easier to validate the DynAck signature.
|
// main purpose of this is to make it easier to validate the DynAck signature.
|
||||||
func (dp *DynPropose) SerializeTlvData() ([]byte, error) {
|
func (dp *DynPropose) SerializeTlvData() ([]byte, error) {
|
||||||
var tlvRecords []tlv.Record
|
producers := dynProposeRecords(dp)
|
||||||
dp.DustLimit.WhenSome(func(dl btcutil.Amount) {
|
|
||||||
protoSats := uint64(dl)
|
|
||||||
tlvRecords = append(
|
|
||||||
tlvRecords, tlv.MakePrimitiveRecord(
|
|
||||||
DPDustLimitSatoshis, &protoSats,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
dp.MaxValueInFlight.WhenSome(func(max MilliSatoshi) {
|
|
||||||
protoSats := uint64(max)
|
|
||||||
tlvRecords = append(
|
|
||||||
tlvRecords, tlv.MakePrimitiveRecord(
|
|
||||||
DPMaxHtlcValueInFlightMsat, &protoSats,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
dp.HtlcMinimum.WhenSome(func(min MilliSatoshi) {
|
|
||||||
protoSats := uint64(min)
|
|
||||||
tlvRecords = append(
|
|
||||||
tlvRecords, tlv.MakePrimitiveRecord(
|
|
||||||
DPHtlcMinimumMsat, &protoSats,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
dp.ChannelReserve.WhenSome(func(min btcutil.Amount) {
|
|
||||||
channelReserve := uint64(min)
|
|
||||||
tlvRecords = append(
|
|
||||||
tlvRecords, tlv.MakePrimitiveRecord(
|
|
||||||
DPChannelReserveSatoshis, &channelReserve,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
dp.CsvDelay.WhenSome(func(wait uint16) {
|
|
||||||
tlvRecords = append(
|
|
||||||
tlvRecords, tlv.MakePrimitiveRecord(
|
|
||||||
DPToSelfDelay, &wait,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
dp.MaxAcceptedHTLCs.WhenSome(func(max uint16) {
|
|
||||||
tlvRecords = append(
|
|
||||||
tlvRecords, tlv.MakePrimitiveRecord(
|
|
||||||
DPMaxAcceptedHtlcs, &max,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
dp.ChannelType.WhenSome(func(ty ChannelType) {
|
|
||||||
tlvRecords = append(
|
|
||||||
tlvRecords, tlv.MakeDynamicRecord(
|
|
||||||
DPChannelType, &ty,
|
|
||||||
ty.featureBitLen,
|
|
||||||
channelTypeEncoder, channelTypeDecoder,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
tlv.SortRecords(tlvRecords)
|
|
||||||
|
|
||||||
tlvStream, err := tlv.NewStream(tlvRecords...)
|
var extra ExtraOpaqueData
|
||||||
|
err := extra.PackRecords(producers...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var outBuf bytes.Buffer
|
return extra, nil
|
||||||
err = tlvStream.Encode(&outBuf)
|
}
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
func dynProposeRecords(dp *DynPropose) []tlv.RecordProducer {
|
||||||
}
|
recordProducers := make([]tlv.RecordProducer, 0, 7)
|
||||||
|
|
||||||
return outBuf.Bytes(), nil
|
dp.DustLimit.WhenSome(
|
||||||
|
func(dl tlv.RecordT[tlv.TlvType0, btcutil.Amount]) {
|
||||||
|
rec := tlv.NewPrimitiveRecord[tlv.TlvType0](
|
||||||
|
uint64(dl.Val),
|
||||||
|
)
|
||||||
|
recordProducers = append(recordProducers, &rec)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
dp.MaxValueInFlight.WhenSome(
|
||||||
|
func(max tlv.RecordT[tlv.TlvType2, MilliSatoshi]) {
|
||||||
|
rec := tlv.NewPrimitiveRecord[tlv.TlvType2](
|
||||||
|
uint64(max.Val),
|
||||||
|
)
|
||||||
|
recordProducers = append(recordProducers, &rec)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
dp.HtlcMinimum.WhenSome(
|
||||||
|
func(min tlv.RecordT[tlv.TlvType4, MilliSatoshi]) {
|
||||||
|
rec := tlv.NewPrimitiveRecord[tlv.TlvType4](
|
||||||
|
uint64(min.Val),
|
||||||
|
)
|
||||||
|
recordProducers = append(recordProducers, &rec)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
dp.ChannelReserve.WhenSome(
|
||||||
|
func(reserve tlv.RecordT[tlv.TlvType6, btcutil.Amount]) {
|
||||||
|
rec := tlv.NewPrimitiveRecord[tlv.TlvType6](
|
||||||
|
uint64(reserve.Val),
|
||||||
|
)
|
||||||
|
recordProducers = append(recordProducers, &rec)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
dp.CsvDelay.WhenSome(
|
||||||
|
func(wait tlv.RecordT[tlv.TlvType8, uint16]) {
|
||||||
|
recordProducers = append(recordProducers, &wait)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
dp.MaxAcceptedHTLCs.WhenSome(
|
||||||
|
func(max tlv.RecordT[tlv.TlvType10, uint16]) {
|
||||||
|
recordProducers = append(recordProducers, &max)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
dp.ChannelType.WhenSome(
|
||||||
|
func(ty tlv.RecordT[tlv.TlvType12, ChannelType]) {
|
||||||
|
recordProducers = append(recordProducers, &ty)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
return recordProducers
|
||||||
}
|
}
|
||||||
+22
-11
@@ -833,32 +833,43 @@ func (dp *DynPropose) RandTestMessage(t *rapid.T) Message {
|
|||||||
|
|
||||||
// Generate random values for each included field
|
// Generate random values for each included field
|
||||||
if includeDustLimit {
|
if includeDustLimit {
|
||||||
dl := btcutil.Amount(rapid.Uint32().Draw(t, "dustLimit"))
|
var rec tlv.RecordT[tlv.TlvType0, btcutil.Amount]
|
||||||
msg.DustLimit = fn.Some(dl)
|
val := btcutil.Amount(rapid.Uint32().Draw(t, "dustLimit"))
|
||||||
|
rec.Val = val
|
||||||
|
msg.DustLimit = tlv.SomeRecordT(rec)
|
||||||
}
|
}
|
||||||
|
|
||||||
if includeMaxValueInFlight {
|
if includeMaxValueInFlight {
|
||||||
mvif := MilliSatoshi(rapid.Uint64().Draw(t, "maxValueInFlight"))
|
var rec tlv.RecordT[tlv.TlvType2, MilliSatoshi]
|
||||||
msg.MaxValueInFlight = fn.Some(mvif)
|
val := MilliSatoshi(rapid.Uint64().Draw(t, "maxValueInFlight"))
|
||||||
|
rec.Val = val
|
||||||
|
msg.MaxValueInFlight = tlv.SomeRecordT(rec)
|
||||||
}
|
}
|
||||||
|
|
||||||
if includeChannelReserve {
|
if includeChannelReserve {
|
||||||
cr := btcutil.Amount(rapid.Uint32().Draw(t, "channelReserve"))
|
var rec tlv.RecordT[tlv.TlvType6, btcutil.Amount]
|
||||||
msg.ChannelReserve = fn.Some(cr)
|
val := btcutil.Amount(rapid.Uint32().Draw(t, "channelReserve"))
|
||||||
|
rec.Val = val
|
||||||
|
msg.ChannelReserve = tlv.SomeRecordT(rec)
|
||||||
}
|
}
|
||||||
|
|
||||||
if includeCsvDelay {
|
if includeCsvDelay {
|
||||||
cd := rapid.Uint16().Draw(t, "csvDelay")
|
csvDelay := msg.CsvDelay.Zero()
|
||||||
msg.CsvDelay = fn.Some(cd)
|
val := rapid.Uint16().Draw(t, "csvDelay")
|
||||||
|
csvDelay.Val = val
|
||||||
|
msg.CsvDelay = tlv.SomeRecordT(csvDelay)
|
||||||
}
|
}
|
||||||
|
|
||||||
if includeMaxAcceptedHTLCs {
|
if includeMaxAcceptedHTLCs {
|
||||||
mah := rapid.Uint16().Draw(t, "maxAcceptedHTLCs")
|
maxHtlcs := msg.MaxAcceptedHTLCs.Zero()
|
||||||
msg.MaxAcceptedHTLCs = fn.Some(mah)
|
maxHtlcs.Val = rapid.Uint16().Draw(t, "maxAcceptedHTLCs")
|
||||||
|
msg.MaxAcceptedHTLCs = tlv.SomeRecordT(maxHtlcs)
|
||||||
}
|
}
|
||||||
|
|
||||||
if includeChannelType {
|
if includeChannelType {
|
||||||
msg.ChannelType = fn.Some(*RandChannelType(t))
|
chanType := msg.ChannelType.Zero()
|
||||||
|
chanType.Val = *RandChannelType(t)
|
||||||
|
msg.ChannelType = tlv.SomeRecordT(chanType)
|
||||||
}
|
}
|
||||||
|
|
||||||
return msg
|
return msg
|
||||||
|
|||||||
Reference in new issue
Block a user