From a5f3fa17e780c321bb3a1fb6cd907b15415412af Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Tue, 8 Oct 2024 13:30:30 +0200 Subject: [PATCH] lnwire21: update Msat and TrueBoolean Add the TrueBoolean type along with its Record method. Also update the Millisatoshi type with a Record method. Both of these will be used in an upcoming commit which adjusts a mission control migration to use pure TLV types. --- channeldb/migration/lnwire21/msat.go | 38 ++++++++++++++++++++ channeldb/migration/lnwire21/true_boolean.go | 37 +++++++++++++++++++ lnwire/channel_update_2.go | 2 +- 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 channeldb/migration/lnwire21/true_boolean.go diff --git a/channeldb/migration/lnwire21/msat.go b/channeldb/migration/lnwire21/msat.go index 7473d72c8..47c676285 100644 --- a/channeldb/migration/lnwire21/msat.go +++ b/channeldb/migration/lnwire21/msat.go @@ -2,8 +2,10 @@ package lnwire import ( "fmt" + "io" "github.com/btcsuite/btcd/btcutil" + "github.com/lightningnetwork/lnd/tlv" ) const ( @@ -49,3 +51,39 @@ func (m MilliSatoshi) String() string { } // TODO(roasbeef): extend with arithmetic operations? + +// Record returns a TLV record that can be used to encode/decode a MilliSatoshi +// to/from a TLV stream. +func (m *MilliSatoshi) Record() tlv.Record { + return tlv.MakeDynamicRecord( + 0, m, tlv.SizeBigSize(m), encodeMilliSatoshis, + decodeMilliSatoshis, + ) +} +func encodeMilliSatoshis(w io.Writer, val interface{}, buf *[8]byte) error { + if v, ok := val.(*MilliSatoshi); ok { + bigSize := uint64(*v) + + return tlv.EBigSize(w, &bigSize, buf) + } + + return tlv.NewTypeForEncodingErr(val, "lnwire.MilliSatoshi") +} + +func decodeMilliSatoshis(r io.Reader, val interface{}, buf *[8]byte, + l uint64) error { + + if v, ok := val.(*MilliSatoshi); ok { + var bigSize uint64 + err := tlv.DBigSize(r, &bigSize, buf, l) + if err != nil { + return err + } + + *v = MilliSatoshi(bigSize) + + return nil + } + + return tlv.NewTypeForDecodingErr(val, "lnwire.MilliSatoshi", l, l) +} diff --git a/channeldb/migration/lnwire21/true_boolean.go b/channeldb/migration/lnwire21/true_boolean.go new file mode 100644 index 000000000..3cde34263 --- /dev/null +++ b/channeldb/migration/lnwire21/true_boolean.go @@ -0,0 +1,37 @@ +package lnwire + +import ( + "io" + + "github.com/lightningnetwork/lnd/tlv" +) + +// TrueBoolean is a record that indicates true or false using the presence of +// the record. If the record is absent, it indicates false. If it is present, +// it indicates true. +type TrueBoolean struct{} + +// Record returns the tlv record for the boolean entry. +func (b *TrueBoolean) Record() tlv.Record { + return tlv.MakeStaticRecord( + 0, b, 0, booleanEncoder, booleanDecoder, + ) +} + +func booleanEncoder(_ io.Writer, val interface{}, _ *[8]byte) error { + if _, ok := val.(*TrueBoolean); ok { + return nil + } + + return tlv.NewTypeForEncodingErr(val, "TrueBoolean") +} + +func booleanDecoder(_ io.Reader, val interface{}, _ *[8]byte, + l uint64) error { + + if _, ok := val.(*TrueBoolean); ok && (l == 0 || l == 1) { + return nil + } + + return tlv.NewTypeForEncodingErr(val, "TrueBoolean") +} diff --git a/lnwire/channel_update_2.go b/lnwire/channel_update_2.go index 79a76aad6..5c6d24095 100644 --- a/lnwire/channel_update_2.go +++ b/lnwire/channel_update_2.go @@ -411,7 +411,7 @@ func decodeDisableFlags(r io.Reader, val interface{}, buf *[8]byte, } // TrueBoolean is a record that indicates true or false using the presence of -// the record. If the record is absent, it indicates false. If it is presence, +// the record. If the record is absent, it indicates false. If it is present, // it indicates true. type TrueBoolean struct{}