lnwire: use BigSize for encoding btc amount

This commit is contained in:
yyforyongyu
2025-07-01 21:28:48 +08:00
parent 04a2be29d2
commit 0f1cb54eb2
4 changed files with 34 additions and 38 deletions

View File

@@ -75,10 +75,10 @@ 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 dustLimit tlv.RecordT[tlv.TlvType0, uint64] var dustLimit tlv.RecordT[tlv.TlvType0, tlv.BigSizeT[btcutil.Amount]]
var maxValue tlv.RecordT[tlv.TlvType2, uint64] var maxValue tlv.RecordT[tlv.TlvType2, uint64]
var htlcMin tlv.RecordT[tlv.TlvType4, uint64] var htlcMin tlv.RecordT[tlv.TlvType4, uint64]
var reserve tlv.RecordT[tlv.TlvType6, uint64] var reserve tlv.RecordT[tlv.TlvType6, tlv.BigSizeT[btcutil.Amount]]
csvDelay := dc.CsvDelay.Zero() csvDelay := dc.CsvDelay.Zero()
maxHtlcs := dc.MaxAcceptedHTLCs.Zero() maxHtlcs := dc.MaxAcceptedHTLCs.Zero()
chanType := dc.ChannelType.Zero() chanType := dc.ChannelType.Zero()
@@ -94,8 +94,8 @@ func (dc *DynCommit) Decode(r io.Reader, _ uint32) error {
// 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[dc.DustLimit.TlvType()]; ok && val == nil { if val, ok := typeMap[dc.DustLimit.TlvType()]; ok && val == nil {
var rec tlv.RecordT[tlv.TlvType0, btcutil.Amount] var rec tlv.RecordT[tlv.TlvType0, tlv.BigSizeT[btcutil.Amount]]
rec.Val = btcutil.Amount(dustLimit.Val) rec.Val = dustLimit.Val
dc.DustLimit = tlv.SomeRecordT(rec) dc.DustLimit = tlv.SomeRecordT(rec)
} }
if val, ok := typeMap[dc.MaxValueInFlight.TlvType()]; ok && val == nil { if val, ok := typeMap[dc.MaxValueInFlight.TlvType()]; ok && val == nil {
@@ -109,8 +109,8 @@ func (dc *DynCommit) Decode(r io.Reader, _ uint32) error {
dc.HtlcMinimum = tlv.SomeRecordT(rec) dc.HtlcMinimum = tlv.SomeRecordT(rec)
} }
if val, ok := typeMap[dc.ChannelReserve.TlvType()]; ok && val == nil { if val, ok := typeMap[dc.ChannelReserve.TlvType()]; ok && val == nil {
var rec tlv.RecordT[tlv.TlvType6, btcutil.Amount] var rec tlv.RecordT[tlv.TlvType6, tlv.BigSizeT[btcutil.Amount]]
rec.Val = btcutil.Amount(reserve.Val) rec.Val = reserve.Val
dc.ChannelReserve = tlv.SomeRecordT(rec) dc.ChannelReserve = tlv.SomeRecordT(rec)
} }
if val, ok := typeMap[dc.CsvDelay.TlvType()]; ok && val == nil { if val, ok := typeMap[dc.CsvDelay.TlvType()]; ok && val == nil {

View File

@@ -17,7 +17,9 @@ 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 tlv.OptionalRecordT[tlv.TlvType0, btcutil.Amount] DustLimit tlv.OptionalRecordT[
tlv.TlvType0, tlv.BigSizeT[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.
@@ -29,7 +31,9 @@ type DynPropose struct {
// 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 tlv.OptionalRecordT[tlv.TlvType6, btcutil.Amount] ChannelReserve tlv.OptionalRecordT[
tlv.TlvType6, tlv.BigSizeT[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.
@@ -98,10 +102,10 @@ 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 dustLimit tlv.RecordT[tlv.TlvType0, uint64] var dustLimit tlv.RecordT[tlv.TlvType0, tlv.BigSizeT[btcutil.Amount]]
var maxValue tlv.RecordT[tlv.TlvType2, uint64] var maxValue tlv.RecordT[tlv.TlvType2, uint64]
var htlcMin tlv.RecordT[tlv.TlvType4, uint64] var htlcMin tlv.RecordT[tlv.TlvType4, uint64]
var reserve tlv.RecordT[tlv.TlvType6, uint64] var reserve tlv.RecordT[tlv.TlvType6, tlv.BigSizeT[btcutil.Amount]]
csvDelay := dp.CsvDelay.Zero() csvDelay := dp.CsvDelay.Zero()
maxHtlcs := dp.MaxAcceptedHTLCs.Zero() maxHtlcs := dp.MaxAcceptedHTLCs.Zero()
chanType := dp.ChannelType.Zero() chanType := dp.ChannelType.Zero()
@@ -117,8 +121,8 @@ func (dp *DynPropose) Decode(r io.Reader, _ uint32) error {
// 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[dp.DustLimit.TlvType()]; ok && val == nil { if val, ok := typeMap[dp.DustLimit.TlvType()]; ok && val == nil {
var rec tlv.RecordT[tlv.TlvType0, btcutil.Amount] var rec tlv.RecordT[tlv.TlvType0, tlv.BigSizeT[btcutil.Amount]]
rec.Val = btcutil.Amount(dustLimit.Val) rec.Val = dustLimit.Val
dp.DustLimit = tlv.SomeRecordT(rec) dp.DustLimit = tlv.SomeRecordT(rec)
} }
if val, ok := typeMap[dp.MaxValueInFlight.TlvType()]; ok && val == nil { if val, ok := typeMap[dp.MaxValueInFlight.TlvType()]; ok && val == nil {
@@ -132,8 +136,8 @@ func (dp *DynPropose) Decode(r io.Reader, _ uint32) error {
dp.HtlcMinimum = tlv.SomeRecordT(rec) dp.HtlcMinimum = tlv.SomeRecordT(rec)
} }
if val, ok := typeMap[dp.ChannelReserve.TlvType()]; ok && val == nil { if val, ok := typeMap[dp.ChannelReserve.TlvType()]; ok && val == nil {
var rec tlv.RecordT[tlv.TlvType6, btcutil.Amount] var rec tlv.RecordT[tlv.TlvType6, tlv.BigSizeT[btcutil.Amount]]
rec.Val = btcutil.Amount(reserve.Val) rec.Val = reserve.Val
dp.ChannelReserve = tlv.SomeRecordT(rec) dp.ChannelReserve = tlv.SomeRecordT(rec)
} }
if val, ok := typeMap[dp.CsvDelay.TlvType()]; ok && val == nil { if val, ok := typeMap[dp.CsvDelay.TlvType()]; ok && val == nil {
@@ -187,11 +191,10 @@ func dynProposeRecords(dp *DynPropose) []tlv.RecordProducer {
recordProducers := make([]tlv.RecordProducer, 0, 7) recordProducers := make([]tlv.RecordProducer, 0, 7)
dp.DustLimit.WhenSome( dp.DustLimit.WhenSome(
func(dl tlv.RecordT[tlv.TlvType0, btcutil.Amount]) { func(dl tlv.RecordT[tlv.TlvType0,
rec := tlv.NewPrimitiveRecord[tlv.TlvType0]( tlv.BigSizeT[btcutil.Amount]]) {
uint64(dl.Val),
) recordProducers = append(recordProducers, &dl)
recordProducers = append(recordProducers, &rec)
}, },
) )
dp.MaxValueInFlight.WhenSome( dp.MaxValueInFlight.WhenSome(
@@ -211,11 +214,10 @@ func dynProposeRecords(dp *DynPropose) []tlv.RecordProducer {
}, },
) )
dp.ChannelReserve.WhenSome( dp.ChannelReserve.WhenSome(
func(reserve tlv.RecordT[tlv.TlvType6, btcutil.Amount]) { func(reserve tlv.RecordT[tlv.TlvType6,
rec := tlv.NewPrimitiveRecord[tlv.TlvType6]( tlv.BigSizeT[btcutil.Amount]]) {
uint64(reserve.Val),
) recordProducers = append(recordProducers, &reserve)
recordProducers = append(recordProducers, &rec)
}, },
) )
dp.CsvDelay.WhenSome( dp.CsvDelay.WhenSome(

View File

@@ -5,10 +5,8 @@ import (
crand "crypto/rand" crand "crypto/rand"
"encoding/hex" "encoding/hex"
"math" "math"
"math/rand"
"net" "net"
"testing" "testing"
"time"
"github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/ecdsa" "github.com/btcsuite/btcd/btcec/v2/ecdsa"
@@ -249,7 +247,3 @@ func TestLightningWireProtocol(t *testing.T) {
})) }))
} }
} }
func init() {
rand.Seed(time.Now().Unix())
}

View File

@@ -833,9 +833,9 @@ 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 {
var rec tlv.RecordT[tlv.TlvType0, btcutil.Amount] var rec tlv.RecordT[tlv.TlvType0, tlv.BigSizeT[btcutil.Amount]]
val := btcutil.Amount(rapid.Uint32().Draw(t, "dustLimit")) val := btcutil.Amount(rapid.Uint32().Draw(t, "dustLimit"))
rec.Val = val rec.Val = tlv.NewBigSizeT(val)
msg.DustLimit = tlv.SomeRecordT(rec) msg.DustLimit = tlv.SomeRecordT(rec)
} }
@@ -847,9 +847,9 @@ func (dp *DynPropose) RandTestMessage(t *rapid.T) Message {
} }
if includeChannelReserve { if includeChannelReserve {
var rec tlv.RecordT[tlv.TlvType6, btcutil.Amount] var rec tlv.RecordT[tlv.TlvType6, tlv.BigSizeT[btcutil.Amount]]
val := btcutil.Amount(rapid.Uint32().Draw(t, "channelReserve")) val := btcutil.Amount(rapid.Uint32().Draw(t, "channelReserve"))
rec.Val = val rec.Val = tlv.NewBigSizeT(val)
msg.ChannelReserve = tlv.SomeRecordT(rec) msg.ChannelReserve = tlv.SomeRecordT(rec)
} }
@@ -942,9 +942,9 @@ func (dc *DynCommit) RandTestMessage(t *rapid.T) Message {
// Generate random values for each included field // Generate random values for each included field
if includeDustLimit { if includeDustLimit {
var rec tlv.RecordT[tlv.TlvType0, btcutil.Amount] var rec tlv.RecordT[tlv.TlvType0, tlv.BigSizeT[btcutil.Amount]]
val := btcutil.Amount(rapid.Uint32().Draw(t, "dustLimit")) val := btcutil.Amount(rapid.Uint32().Draw(t, "dustLimit"))
rec.Val = val rec.Val = tlv.NewBigSizeT(val)
dp.DustLimit = tlv.SomeRecordT(rec) dp.DustLimit = tlv.SomeRecordT(rec)
} }
@@ -956,9 +956,9 @@ func (dc *DynCommit) RandTestMessage(t *rapid.T) Message {
} }
if includeChannelReserve { if includeChannelReserve {
var rec tlv.RecordT[tlv.TlvType6, btcutil.Amount] var rec tlv.RecordT[tlv.TlvType6, tlv.BigSizeT[btcutil.Amount]]
val := btcutil.Amount(rapid.Uint32().Draw(t, "channelReserve")) val := btcutil.Amount(rapid.Uint32().Draw(t, "channelReserve"))
rec.Val = val rec.Val = tlv.NewBigSizeT(val)
dp.ChannelReserve = tlv.SomeRecordT(rec) dp.ChannelReserve = tlv.SomeRecordT(rec)
} }