lnwire: add new SerializedSize method to all wire messages

This'll be useful for the bandwidth based rate limiting we'll implement
in the next commit.
This commit is contained in:
Olaoluwa Osuntokun
2025-03-18 18:55:16 -05:00
parent e8875e06fe
commit 56a100123b
40 changed files with 453 additions and 4 deletions

View File

@@ -128,6 +128,10 @@ type AcceptChannel struct {
// interface. // interface.
var _ Message = (*AcceptChannel)(nil) var _ Message = (*AcceptChannel)(nil)
// A compile time check to ensure AcceptChannel implements the lnwire.SizeableMessage
// interface.
var _ SizeableMessage = (*AcceptChannel)(nil)
// Encode serializes the target AcceptChannel into the passed io.Writer // Encode serializes the target AcceptChannel into the passed io.Writer
// implementation. Serialization will observe the rules defined by the passed // implementation. Serialization will observe the rules defined by the passed
// protocol version. // protocol version.
@@ -281,3 +285,10 @@ func (a *AcceptChannel) Decode(r io.Reader, pver uint32) error {
func (a *AcceptChannel) MsgType() MessageType { func (a *AcceptChannel) MsgType() MessageType {
return MsgAcceptChannel return MsgAcceptChannel
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (a *AcceptChannel) SerializedSize() (uint32, error) {
return MessageSerializedSize(a)
}

View File

@@ -47,6 +47,10 @@ type AnnounceSignatures1 struct {
// lnwire.Message interface. // lnwire.Message interface.
var _ Message = (*AnnounceSignatures1)(nil) var _ Message = (*AnnounceSignatures1)(nil)
// A compile time check to ensure AnnounceSignatures1 implements the
// lnwire.SizeableMessage interface.
var _ SizeableMessage = (*AnnounceSignatures1)(nil)
// A compile time check to ensure AnnounceSignatures1 implements the // A compile time check to ensure AnnounceSignatures1 implements the
// lnwire.AnnounceSignatures interface. // lnwire.AnnounceSignatures interface.
var _ AnnounceSignatures = (*AnnounceSignatures1)(nil) var _ AnnounceSignatures = (*AnnounceSignatures1)(nil)
@@ -97,6 +101,13 @@ func (a *AnnounceSignatures1) MsgType() MessageType {
return MsgAnnounceSignatures return MsgAnnounceSignatures
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (a *AnnounceSignatures1) SerializedSize() (uint32, error) {
return MessageSerializedSize(a)
}
// SCID returns the ShortChannelID of the channel. // SCID returns the ShortChannelID of the channel.
// //
// This is part of the lnwire.AnnounceSignatures interface. // This is part of the lnwire.AnnounceSignatures interface.

View File

@@ -41,6 +41,10 @@ type AnnounceSignatures2 struct {
// lnwire.Message interface. // lnwire.Message interface.
var _ Message = (*AnnounceSignatures2)(nil) var _ Message = (*AnnounceSignatures2)(nil)
// A compile time check to ensure AnnounceSignatures2 implements the
// lnwire.SizeableMessage interface.
var _ SizeableMessage = (*AnnounceSignatures2)(nil)
// Decode deserializes a serialized AnnounceSignatures2 stored in the passed // Decode deserializes a serialized AnnounceSignatures2 stored in the passed
// io.Reader observing the specified protocol version. // io.Reader observing the specified protocol version.
// //
@@ -82,6 +86,13 @@ func (a *AnnounceSignatures2) MsgType() MessageType {
return MsgAnnounceSignatures2 return MsgAnnounceSignatures2
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (a *AnnounceSignatures2) SerializedSize() (uint32, error) {
return MessageSerializedSize(a)
}
// SCID returns the ShortChannelID of the channel. // SCID returns the ShortChannelID of the channel.
// //
// NOTE: this is part of the AnnounceSignatures interface. // NOTE: this is part of the AnnounceSignatures interface.

View File

@@ -62,6 +62,10 @@ type ChannelAnnouncement1 struct {
// lnwire.Message interface. // lnwire.Message interface.
var _ Message = (*ChannelAnnouncement1)(nil) var _ Message = (*ChannelAnnouncement1)(nil)
// A compile time check to ensure ChannelAnnouncement1 implements the
// lnwire.SizeableMessage interface.
var _ SizeableMessage = (*ChannelAnnouncement1)(nil)
// Decode deserializes a serialized ChannelAnnouncement stored in the passed // Decode deserializes a serialized ChannelAnnouncement stored in the passed
// io.Reader observing the specified protocol version. // io.Reader observing the specified protocol version.
// //
@@ -143,6 +147,13 @@ func (a *ChannelAnnouncement1) MsgType() MessageType {
return MsgChannelAnnouncement return MsgChannelAnnouncement
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (a *ChannelAnnouncement1) SerializedSize() (uint32, error) {
return MessageSerializedSize(a)
}
// DataToSign is used to retrieve part of the announcement message which should // DataToSign is used to retrieve part of the announcement message which should
// be signed. // be signed.
func (a *ChannelAnnouncement1) DataToSign() ([]byte, error) { func (a *ChannelAnnouncement1) DataToSign() ([]byte, error) {

View File

@@ -194,10 +194,21 @@ func (c *ChannelAnnouncement2) MsgType() MessageType {
return MsgChannelAnnouncement2 return MsgChannelAnnouncement2
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (c *ChannelAnnouncement2) SerializedSize() (uint32, error) {
return MessageSerializedSize(c)
}
// A compile time check to ensure ChannelAnnouncement2 implements the // A compile time check to ensure ChannelAnnouncement2 implements the
// lnwire.Message interface. // lnwire.Message interface.
var _ Message = (*ChannelAnnouncement2)(nil) var _ Message = (*ChannelAnnouncement2)(nil)
// A compile time check to ensure ChannelAnnouncement2 implements the
// lnwire.SizeableMessage interface.
var _ SizeableMessage = (*ChannelAnnouncement2)(nil)
// Node1KeyBytes returns the bytes representing the public key of node 1 in the // Node1KeyBytes returns the bytes representing the public key of node 1 in the
// channel. // channel.
// //

View File

@@ -63,6 +63,10 @@ func NewChannelReady(cid ChannelID, npcp *btcec.PublicKey) *ChannelReady {
// interface. // interface.
var _ Message = (*ChannelReady)(nil) var _ Message = (*ChannelReady)(nil)
// A compile time check to ensure ChannelReady implements the
// lnwire.SizeableMessage interface.
var _ SizeableMessage = (*ChannelReady)(nil)
// Decode deserializes the serialized ChannelReady message stored in the // Decode deserializes the serialized ChannelReady message stored in the
// passed io.Reader into the target ChannelReady using the deserialization // passed io.Reader into the target ChannelReady using the deserialization
// rules defined by the passed protocol version. // rules defined by the passed protocol version.
@@ -170,3 +174,10 @@ func (c *ChannelReady) Encode(w *bytes.Buffer, _ uint32) error {
func (c *ChannelReady) MsgType() MessageType { func (c *ChannelReady) MsgType() MessageType {
return MsgChannelReady return MsgChannelReady
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (c *ChannelReady) SerializedSize() (uint32, error) {
return MessageSerializedSize(c)
}

View File

@@ -99,6 +99,10 @@ type ChannelReestablish struct {
// lnwire.Message interface. // lnwire.Message interface.
var _ Message = (*ChannelReestablish)(nil) var _ Message = (*ChannelReestablish)(nil)
// A compile time check to ensure ChannelReestablish implements the
// lnwire.SizeableMessage interface.
var _ SizeableMessage = (*ChannelReestablish)(nil)
// Encode serializes the target ChannelReestablish into the passed io.Writer // Encode serializes the target ChannelReestablish into the passed io.Writer
// observing the protocol version specified. // observing the protocol version specified.
// //
@@ -234,3 +238,10 @@ func (a *ChannelReestablish) Decode(r io.Reader, pver uint32) error {
func (a *ChannelReestablish) MsgType() MessageType { func (a *ChannelReestablish) MsgType() MessageType {
return MsgChannelReestablish return MsgChannelReestablish
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (a *ChannelReestablish) SerializedSize() (uint32, error) {
return MessageSerializedSize(a)
}

View File

@@ -124,6 +124,9 @@ type ChannelUpdate1 struct {
// interface. // interface.
var _ Message = (*ChannelUpdate1)(nil) var _ Message = (*ChannelUpdate1)(nil)
// A compile time check to ensure ChannelUpdate1 implements the lnwire.SizeableMessage interface.
var _ SizeableMessage = (*ChannelUpdate1)(nil)
// Decode deserializes a serialized ChannelUpdate stored in the passed // Decode deserializes a serialized ChannelUpdate stored in the passed
// io.Reader observing the specified protocol version. // io.Reader observing the specified protocol version.
// //
@@ -367,3 +370,10 @@ func (a *ChannelUpdate1) SetSCID(scid ShortChannelID) {
// A compile time assertion to ensure ChannelUpdate1 implements the // A compile time assertion to ensure ChannelUpdate1 implements the
// ChannelUpdate interface. // ChannelUpdate interface.
var _ ChannelUpdate = (*ChannelUpdate1)(nil) var _ ChannelUpdate = (*ChannelUpdate1)(nil)
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (a *ChannelUpdate1) SerializedSize() (uint32, error) {
return MessageSerializedSize(a)
}

View File

@@ -241,6 +241,13 @@ func (c *ChannelUpdate2) MsgType() MessageType {
return MsgChannelUpdate2 return MsgChannelUpdate2
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (c *ChannelUpdate2) SerializedSize() (uint32, error) {
return MessageSerializedSize(c)
}
func (c *ChannelUpdate2) ExtraData() ExtraOpaqueData { func (c *ChannelUpdate2) ExtraData() ExtraOpaqueData {
return c.ExtraOpaqueData return c.ExtraOpaqueData
} }

View File

@@ -169,6 +169,16 @@ func (c *ClosingComplete) MsgType() MessageType {
return MsgClosingComplete return MsgClosingComplete
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (c *ClosingComplete) SerializedSize() (uint32, error) {
return MessageSerializedSize(c)
}
// A compile time check to ensure ClosingComplete implements the lnwire.Message // A compile time check to ensure ClosingComplete implements the lnwire.Message
// interface. // interface.
var _ Message = (*ClosingComplete)(nil) var _ Message = (*ClosingComplete)(nil)
// A compile time check to ensure ClosingComplete implements the lnwire.SizeableMessage interface.
var _ SizeableMessage = (*ClosingComplete)(nil)

View File

@@ -107,6 +107,17 @@ func (c *ClosingSig) MsgType() MessageType {
return MsgClosingSig return MsgClosingSig
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (c *ClosingSig) SerializedSize() (uint32, error) {
return MessageSerializedSize(c)
}
// A compile time check to ensure ClosingSig implements the lnwire.Message // A compile time check to ensure ClosingSig implements the lnwire.Message
// interface. // interface.
var _ Message = (*ClosingSig)(nil) var _ Message = (*ClosingSig)(nil)
// A compile time check to ensure ClosingSig implements the lnwire.SizeableMessage
// interface.
var _ SizeableMessage = (*ClosingSig)(nil)

View File

@@ -59,6 +59,9 @@ func NewClosingSigned(cid ChannelID, fs btcutil.Amount,
// interface. // interface.
var _ Message = (*ClosingSigned)(nil) var _ Message = (*ClosingSigned)(nil)
// A compile time check to ensure ClosingSigned implements the lnwire.SizeableMessage interface.
var _ SizeableMessage = (*ClosingSigned)(nil)
// Decode deserializes a serialized ClosingSigned message stored in the passed // Decode deserializes a serialized ClosingSigned message stored in the passed
// io.Reader observing the specified protocol version. // io.Reader observing the specified protocol version.
// //
@@ -130,3 +133,10 @@ func (c *ClosingSigned) Encode(w *bytes.Buffer, pver uint32) error {
func (c *ClosingSigned) MsgType() MessageType { func (c *ClosingSigned) MsgType() MessageType {
return MsgClosingSigned return MsgClosingSigned
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (c *ClosingSigned) SerializedSize() (uint32, error) {
return MessageSerializedSize(c)
}

View File

@@ -64,6 +64,9 @@ func NewCommitSig() *CommitSig {
// interface. // interface.
var _ Message = (*CommitSig)(nil) var _ Message = (*CommitSig)(nil)
// A compile time check to ensure CommitSig implements the lnwire.SizeableMessage interface.
var _ SizeableMessage = (*CommitSig)(nil)
// Decode deserializes a serialized CommitSig message stored in the // Decode deserializes a serialized CommitSig message stored in the
// passed io.Reader observing the specified protocol version. // passed io.Reader observing the specified protocol version.
// //
@@ -151,3 +154,10 @@ func (c *CommitSig) MsgType() MessageType {
func (c *CommitSig) TargetChanID() ChannelID { func (c *CommitSig) TargetChanID() ChannelID {
return c.ChanID return c.ChanID
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (c *CommitSig) SerializedSize() (uint32, error) {
return MessageSerializedSize(c)
}

View File

@@ -73,6 +73,10 @@ type Custom struct {
// interface. // interface.
var _ Message = (*Custom)(nil) var _ Message = (*Custom)(nil)
// A compile time check to ensure Custom implements the lnwire.SizeableMessage
// interface.
var _ SizeableMessage = (*Custom)(nil)
// NewCustom instantiates a new custom message. // NewCustom instantiates a new custom message.
func NewCustom(msgType MessageType, data []byte) (*Custom, error) { func NewCustom(msgType MessageType, data []byte) (*Custom, error) {
if msgType < CustomTypeStart && !IsCustomOverride(msgType) { if msgType < CustomTypeStart && !IsCustomOverride(msgType) {
@@ -117,3 +121,10 @@ func (c *Custom) Decode(r io.Reader, pver uint32) error {
func (c *Custom) MsgType() MessageType { func (c *Custom) MsgType() MessageType {
return c.Type return c.Type
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (c *Custom) SerializedSize() (uint32, error) {
return MessageSerializedSize(c)
}

View File

@@ -41,6 +41,10 @@ type DynAck struct {
// interface. // interface.
var _ Message = (*DynAck)(nil) var _ Message = (*DynAck)(nil)
// A compile time check to ensure DynAck implements the lnwire.SizeableMessage
// interface.
var _ SizeableMessage = (*DynAck)(nil)
// Encode serializes the target DynAck into the passed io.Writer. Serialization // Encode serializes the target DynAck into the passed io.Writer. Serialization
// will observe the rules defined by the passed protocol version. // will observe the rules defined by the passed protocol version.
// //
@@ -136,3 +140,10 @@ func (da *DynAck) Decode(r io.Reader, _ uint32) error {
func (da *DynAck) MsgType() MessageType { func (da *DynAck) MsgType() MessageType {
return MsgDynAck return MsgDynAck
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (da *DynAck) SerializedSize() (uint32, error) {
return MessageSerializedSize(da)
}

View File

@@ -105,6 +105,10 @@ type DynPropose struct {
// interface. // interface.
var _ Message = (*DynPropose)(nil) var _ Message = (*DynPropose)(nil)
// A compile time check to ensure DynPropose implements the
// lnwire.SizeableMessage interface.
var _ SizeableMessage = (*DynPropose)(nil)
// Encode serializes the target DynPropose into the passed io.Writer. // Encode serializes the target DynPropose into the passed io.Writer.
// Serialization will observe the rules defined by the passed protocol version. // Serialization will observe the rules defined by the passed protocol version.
// //
@@ -317,3 +321,10 @@ func (dp *DynPropose) Decode(r io.Reader, _ uint32) error {
func (dp *DynPropose) MsgType() MessageType { func (dp *DynPropose) MsgType() MessageType {
return MsgDynPropose return MsgDynPropose
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (dp *DynPropose) SerializedSize() (uint32, error) {
return MessageSerializedSize(dp)
}

View File

@@ -30,6 +30,10 @@ type DynReject struct {
// interface. // interface.
var _ Message = (*DynReject)(nil) var _ Message = (*DynReject)(nil)
// A compile time check to ensure DynReject implements the
// lnwire.SizeableMessage interface.
var _ SizeableMessage = (*DynReject)(nil)
// Encode serializes the target DynReject into the passed io.Writer. // Encode serializes the target DynReject into the passed io.Writer.
// Serialization will observe the rules defined by the passed protocol version. // Serialization will observe the rules defined by the passed protocol version.
// //
@@ -74,3 +78,10 @@ func (dr *DynReject) Decode(r io.Reader, _ uint32) error {
func (dr *DynReject) MsgType() MessageType { func (dr *DynReject) MsgType() MessageType {
return MsgDynReject return MsgDynReject
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (dr *DynReject) SerializedSize() (uint32, error) {
return MessageSerializedSize(dr)
}

View File

@@ -70,6 +70,10 @@ func NewError() *Error {
// interface. // interface.
var _ Message = (*Error)(nil) var _ Message = (*Error)(nil)
// A compile time check to ensure Error implements the lnwire.SizeableMessage
// interface.
var _ SizeableMessage = (*Error)(nil)
// Error returns the string representation to Error. // Error returns the string representation to Error.
// //
// NOTE: Satisfies the error interface. // NOTE: Satisfies the error interface.
@@ -113,6 +117,13 @@ func (c *Error) MsgType() MessageType {
return MsgError return MsgError
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (c *Error) SerializedSize() (uint32, error) {
return MessageSerializedSize(c)
}
// isASCII is a helper method that checks whether all bytes in `data` would be // isASCII is a helper method that checks whether all bytes in `data` would be
// printable ASCII characters if interpreted as a string. // printable ASCII characters if interpreted as a string.
func isASCII(data []byte) bool { func isASCII(data []byte) bool {

View File

@@ -44,6 +44,10 @@ type FundingCreated struct {
// interface. // interface.
var _ Message = (*FundingCreated)(nil) var _ Message = (*FundingCreated)(nil)
// A compile time check to ensure FundingCreated implements the
// lnwire.SizeableMessage interface.
var _ SizeableMessage = (*FundingCreated)(nil)
// Encode serializes the target FundingCreated into the passed io.Writer // Encode serializes the target FundingCreated into the passed io.Writer
// implementation. Serialization will observe the rules defined by the passed // implementation. Serialization will observe the rules defined by the passed
// protocol version. // protocol version.
@@ -117,3 +121,10 @@ func (f *FundingCreated) Decode(r io.Reader, pver uint32) error {
func (f *FundingCreated) MsgType() MessageType { func (f *FundingCreated) MsgType() MessageType {
return MsgFundingCreated return MsgFundingCreated
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (f *FundingCreated) SerializedSize() (uint32, error) {
return MessageSerializedSize(f)
}

View File

@@ -36,6 +36,10 @@ type FundingSigned struct {
// interface. // interface.
var _ Message = (*FundingSigned)(nil) var _ Message = (*FundingSigned)(nil)
// A compile time check to ensure FundingSigned implements the lnwire.SizeableMessage
// interface.
var _ SizeableMessage = (*FundingSigned)(nil)
// Encode serializes the target FundingSigned into the passed io.Writer // Encode serializes the target FundingSigned into the passed io.Writer
// implementation. Serialization will observe the rules defined by the passed // implementation. Serialization will observe the rules defined by the passed
// protocol version. // protocol version.
@@ -103,3 +107,10 @@ func (f *FundingSigned) Decode(r io.Reader, pver uint32) error {
func (f *FundingSigned) MsgType() MessageType { func (f *FundingSigned) MsgType() MessageType {
return MsgFundingSigned return MsgFundingSigned
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (f *FundingSigned) SerializedSize() (uint32, error) {
return MessageSerializedSize(f)
}

View File

@@ -58,6 +58,9 @@ func NewGossipTimestampRange() *GossipTimestampRange {
// lnwire.Message interface. // lnwire.Message interface.
var _ Message = (*GossipTimestampRange)(nil) var _ Message = (*GossipTimestampRange)(nil)
// A compile time check to ensure GossipTimestampRange implements the lnwire.SizeableMessage interface.
var _ SizeableMessage = (*GossipTimestampRange)(nil)
// Decode deserializes a serialized GossipTimestampRange message stored in the // Decode deserializes a serialized GossipTimestampRange message stored in the
// passed io.Reader observing the specified protocol version. // passed io.Reader observing the specified protocol version.
// //
@@ -143,3 +146,10 @@ func (g *GossipTimestampRange) Encode(w *bytes.Buffer, pver uint32) error {
func (g *GossipTimestampRange) MsgType() MessageType { func (g *GossipTimestampRange) MsgType() MessageType {
return MsgGossipTimestampRange return MsgGossipTimestampRange
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (g *GossipTimestampRange) SerializedSize() (uint32, error) {
return MessageSerializedSize(g)
}

View File

@@ -43,6 +43,10 @@ func NewInitMessage(gf *RawFeatureVector, f *RawFeatureVector) *Init {
// interface. // interface.
var _ Message = (*Init)(nil) var _ Message = (*Init)(nil)
// A compile time check to ensure Init implements the lnwire.SizeableMessage
// interface.
var _ SizeableMessage = (*Init)(nil)
// Decode deserializes a serialized Init message stored in the passed // Decode deserializes a serialized Init message stored in the passed
// io.Reader observing the specified protocol version. // io.Reader observing the specified protocol version.
// //
@@ -78,3 +82,10 @@ func (msg *Init) Encode(w *bytes.Buffer, pver uint32) error {
func (msg *Init) MsgType() MessageType { func (msg *Init) MsgType() MessageType {
return MsgInit return MsgInit
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (msg *Init) SerializedSize() (uint32, error) {
return MessageSerializedSize(msg)
}

View File

@@ -12,6 +12,10 @@ import (
"io" "io"
) )
// MessageTypeSize is the size in bytes of the message type field in the header
// of all messages.
const MessageTypeSize = 2
// MessageType is the unique 2 byte big-endian integer that indicates the type // MessageType is the unique 2 byte big-endian integer that indicates the type
// of message on the wire. All messages have a very simple header which // of message on the wire. All messages have a very simple header which
// consists simply of 2-byte message type. We omit a length field, and checksum // consists simply of 2-byte message type. We omit a length field, and checksum
@@ -234,6 +238,31 @@ type LinkUpdater interface {
TargetChanID() ChannelID TargetChanID() ChannelID
} }
// SizeableMessage is an interface that extends the base Message interface with
// a method to calculate the serialized size of a message.
type SizeableMessage interface {
Message
// SerializedSize returns the serialized size of the message in bytes.
// The returned size includes the message type header bytes.
SerializedSize() (uint32, error)
}
// MessageSerializedSize calculates the serialized size of a message in bytes.
// This is a helper function that can be used by all message types to implement
// the SerializedSize method.
func MessageSerializedSize(msg Message) (uint32, error) {
var buf bytes.Buffer
// Encode the message to the buffer.
if err := msg.Encode(&buf, 0); err != nil {
return 0, err
}
// Add the size of the message type.
return uint32(buf.Len()) + MessageTypeSize, nil
}
// makeEmptyMessage creates a new empty message of the proper concrete type // makeEmptyMessage creates a new empty message of the proper concrete type
// based on the passed message type. // based on the passed message type.
func makeEmptyMessage(msgType MessageType) (Message, error) { func makeEmptyMessage(msgType MessageType) (Message, error) {
@@ -337,6 +366,12 @@ func makeEmptyMessage(msgType MessageType) (Message, error) {
return msg, nil return msg, nil
} }
// MakeEmptyMessage creates a new empty message of the proper concrete type
// based on the passed message type. This is exported to be used in tests.
func MakeEmptyMessage(msgType MessageType) (Message, error) {
return makeEmptyMessage(msgType)
}
// WriteMessage writes a lightning Message to a buffer including the necessary // WriteMessage writes a lightning Message to a buffer including the necessary
// header information and returns the number of bytes written. If any error is // header information and returns the number of bytes written. If any error is
// encountered, the buffer passed will be reset to its original state since we // encountered, the buffer passed will be reset to its original state since we

View File

@@ -104,6 +104,9 @@ type NodeAnnouncement struct {
// lnwire.Message interface. // lnwire.Message interface.
var _ Message = (*NodeAnnouncement)(nil) var _ Message = (*NodeAnnouncement)(nil)
// A compile time check to ensure NodeAnnouncement implements the lnwire.SizeableMessage interface.
var _ SizeableMessage = (*NodeAnnouncement)(nil)
// Decode deserializes a serialized NodeAnnouncement stored in the passed // Decode deserializes a serialized NodeAnnouncement stored in the passed
// io.Reader observing the specified protocol version. // io.Reader observing the specified protocol version.
// //
@@ -202,3 +205,10 @@ func (a *NodeAnnouncement) DataToSign() ([]byte, error) {
return buf.Bytes(), nil return buf.Bytes(), nil
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (a *NodeAnnouncement) SerializedSize() (uint32, error) {
return MessageSerializedSize(a)
}

View File

@@ -164,6 +164,10 @@ type OpenChannel struct {
// interface. // interface.
var _ Message = (*OpenChannel)(nil) var _ Message = (*OpenChannel)(nil)
// A compile time check to ensure OpenChannel implements the lnwire.SizeableMessage
// interface.
var _ SizeableMessage = (*OpenChannel)(nil)
// Encode serializes the target OpenChannel into the passed io.Writer // Encode serializes the target OpenChannel into the passed io.Writer
// implementation. Serialization will observe the rules defined by the passed // implementation. Serialization will observe the rules defined by the passed
// protocol version. // protocol version.
@@ -335,3 +339,10 @@ func (o *OpenChannel) Decode(r io.Reader, pver uint32) error {
func (o *OpenChannel) MsgType() MessageType { func (o *OpenChannel) MsgType() MessageType {
return MsgOpenChannel return MsgOpenChannel
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (o *OpenChannel) SerializedSize() (uint32, error) {
return MessageSerializedSize(o)
}

View File

@@ -33,6 +33,10 @@ func NewPing(numBytes uint16) *Ping {
// A compile time check to ensure Ping implements the lnwire.Message interface. // A compile time check to ensure Ping implements the lnwire.Message interface.
var _ Message = (*Ping)(nil) var _ Message = (*Ping)(nil)
// A compile time check to ensure Ping implements the lnwire.SizeableMessage
// interface.
var _ SizeableMessage = (*Ping)(nil)
// Decode deserializes a serialized Ping message stored in the passed io.Reader // Decode deserializes a serialized Ping message stored in the passed io.Reader
// observing the specified protocol version. // observing the specified protocol version.
// //
@@ -69,3 +73,10 @@ func (p *Ping) Encode(w *bytes.Buffer, pver uint32) error {
func (p *Ping) MsgType() MessageType { func (p *Ping) MsgType() MessageType {
return MsgPing return MsgPing
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (p *Ping) SerializedSize() (uint32, error) {
return MessageSerializedSize(p)
}

View File

@@ -39,6 +39,9 @@ func NewPong(pongBytes []byte) *Pong {
// A compile time check to ensure Pong implements the lnwire.Message interface. // A compile time check to ensure Pong implements the lnwire.Message interface.
var _ Message = (*Pong)(nil) var _ Message = (*Pong)(nil)
// A compile time check to ensure Pong implements the lnwire.SizeableMessage interface.
var _ SizeableMessage = (*Pong)(nil)
// Decode deserializes a serialized Pong message stored in the passed io.Reader // Decode deserializes a serialized Pong message stored in the passed io.Reader
// observing the specified protocol version. // observing the specified protocol version.
// //
@@ -64,3 +67,10 @@ func (p *Pong) Encode(w *bytes.Buffer, pver uint32) error {
func (p *Pong) MsgType() MessageType { func (p *Pong) MsgType() MessageType {
return MsgPong return MsgPong
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (p *Pong) SerializedSize() (uint32, error) {
return MessageSerializedSize(p)
}

View File

@@ -49,6 +49,10 @@ func NewQueryChannelRange() *QueryChannelRange {
// lnwire.Message interface. // lnwire.Message interface.
var _ Message = (*QueryChannelRange)(nil) var _ Message = (*QueryChannelRange)(nil)
// A compile time check to ensure QueryChannelRange implements the lnwire.SizeableMessage
// interface.
var _ SizeableMessage = (*QueryChannelRange)(nil)
// Decode deserializes a serialized QueryChannelRange message stored in the // Decode deserializes a serialized QueryChannelRange message stored in the
// passed io.Reader observing the specified protocol version. // passed io.Reader observing the specified protocol version.
// //
@@ -121,6 +125,14 @@ func (q *QueryChannelRange) MsgType() MessageType {
return MsgQueryChannelRange return MsgQueryChannelRange
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (q *QueryChannelRange) SerializedSize() (uint32, error) {
msgCpy := *q
return MessageSerializedSize(&msgCpy)
}
// LastBlockHeight returns the last block height covered by the range of a // LastBlockHeight returns the last block height covered by the range of a
// QueryChannelRange message. // QueryChannelRange message.
func (q *QueryChannelRange) LastBlockHeight() uint32 { func (q *QueryChannelRange) LastBlockHeight() uint32 {

View File

@@ -91,6 +91,10 @@ func NewQueryShortChanIDs(h chainhash.Hash, e QueryEncoding,
// lnwire.Message interface. // lnwire.Message interface.
var _ Message = (*QueryShortChanIDs)(nil) var _ Message = (*QueryShortChanIDs)(nil)
// A compile time check to ensure QueryShortChanIDs implements the lnwire.SizeableMessage
// interface.
var _ SizeableMessage = (*QueryShortChanIDs)(nil)
// Decode deserializes a serialized QueryShortChanIDs message stored in the // Decode deserializes a serialized QueryShortChanIDs message stored in the
// passed io.Reader observing the specified protocol version. // passed io.Reader observing the specified protocol version.
// //
@@ -427,3 +431,10 @@ func encodeShortChanIDs(w *bytes.Buffer, encodingType QueryEncoding,
func (q *QueryShortChanIDs) MsgType() MessageType { func (q *QueryShortChanIDs) MsgType() MessageType {
return MsgQueryShortChanIDs return MsgQueryShortChanIDs
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (q *QueryShortChanIDs) SerializedSize() (uint32, error) {
return MessageSerializedSize(q)
}

View File

@@ -70,6 +70,9 @@ func NewReplyChannelRange() *ReplyChannelRange {
// lnwire.Message interface. // lnwire.Message interface.
var _ Message = (*ReplyChannelRange)(nil) var _ Message = (*ReplyChannelRange)(nil)
// A compile time check to ensure ReplyChannelRange implements the lnwire.SizeableMessage interface.
var _ SizeableMessage = (*ReplyChannelRange)(nil)
// Decode deserializes a serialized ReplyChannelRange message stored in the // Decode deserializes a serialized ReplyChannelRange message stored in the
// passed io.Reader observing the specified protocol version. // passed io.Reader observing the specified protocol version.
// //
@@ -223,3 +226,10 @@ func (c *ReplyChannelRange) LastBlockHeight() uint32 {
} }
return uint32(lastBlockHeight) return uint32(lastBlockHeight)
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (c *ReplyChannelRange) SerializedSize() (uint32, error) {
return MessageSerializedSize(c)
}

View File

@@ -39,6 +39,9 @@ func NewReplyShortChanIDsEnd() *ReplyShortChanIDsEnd {
// lnwire.Message interface. // lnwire.Message interface.
var _ Message = (*ReplyShortChanIDsEnd)(nil) var _ Message = (*ReplyShortChanIDsEnd)(nil)
// A compile time check to ensure ReplyShortChanIDsEnd implements the lnwire.SizeableMessage interface.
var _ SizeableMessage = (*ReplyShortChanIDsEnd)(nil)
// Decode deserializes a serialized ReplyShortChanIDsEnd message stored in the // Decode deserializes a serialized ReplyShortChanIDsEnd message stored in the
// passed io.Reader observing the specified protocol version. // passed io.Reader observing the specified protocol version.
// //
@@ -74,3 +77,10 @@ func (c *ReplyShortChanIDsEnd) Encode(w *bytes.Buffer, pver uint32) error {
func (c *ReplyShortChanIDsEnd) MsgType() MessageType { func (c *ReplyShortChanIDsEnd) MsgType() MessageType {
return MsgReplyShortChanIDsEnd return MsgReplyShortChanIDsEnd
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (c *ReplyShortChanIDsEnd) SerializedSize() (uint32, error) {
return MessageSerializedSize(c)
}

View File

@@ -55,6 +55,9 @@ func NewRevokeAndAck() *RevokeAndAck {
// interface. // interface.
var _ Message = (*RevokeAndAck)(nil) var _ Message = (*RevokeAndAck)(nil)
// A compile time check to ensure RevokeAndAck implements the lnwire.SizeableMessage interface.
var _ SizeableMessage = (*RevokeAndAck)(nil)
// Decode deserializes a serialized RevokeAndAck message stored in the // Decode deserializes a serialized RevokeAndAck message stored in the
// passed io.Reader observing the specified protocol version. // passed io.Reader observing the specified protocol version.
// //
@@ -136,3 +139,10 @@ func (c *RevokeAndAck) MsgType() MessageType {
func (c *RevokeAndAck) TargetChanID() ChannelID { func (c *RevokeAndAck) TargetChanID() ChannelID {
return c.ChanID return c.ChanID
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (c *RevokeAndAck) SerializedSize() (uint32, error) {
return MessageSerializedSize(c)
}

View File

@@ -61,6 +61,9 @@ func NewShutdown(cid ChannelID, addr DeliveryAddress) *Shutdown {
// interface. // interface.
var _ Message = (*Shutdown)(nil) var _ Message = (*Shutdown)(nil)
// A compile-time check to ensure Shutdown implements the lnwire.SizeableMessage interface.
var _ SizeableMessage = (*Shutdown)(nil)
// Decode deserializes a serialized Shutdown from the passed io.Reader, // Decode deserializes a serialized Shutdown from the passed io.Reader,
// observing the specified protocol version. // observing the specified protocol version.
// //
@@ -133,3 +136,10 @@ func (s *Shutdown) Encode(w *bytes.Buffer, pver uint32) error {
func (s *Shutdown) MsgType() MessageType { func (s *Shutdown) MsgType() MessageType {
return MsgShutdown return MsgShutdown
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (s *Shutdown) SerializedSize() (uint32, error) {
return MessageSerializedSize(s)
}

View File

@@ -24,6 +24,9 @@ type Stfu struct {
// A compile time check to ensure Stfu implements the lnwire.Message interface. // A compile time check to ensure Stfu implements the lnwire.Message interface.
var _ Message = (*Stfu)(nil) var _ Message = (*Stfu)(nil)
// A compile time check to ensure Stfu implements the lnwire.SizeableMessage interface.
var _ SizeableMessage = (*Stfu)(nil)
// Encode serializes the target Stfu into the passed io.Writer. // Encode serializes the target Stfu into the passed io.Writer.
// Serialization will observe the rules defined by the passed protocol version. // Serialization will observe the rules defined by the passed protocol version.
// //
@@ -68,6 +71,13 @@ func (s *Stfu) MsgType() MessageType {
return MsgStfu return MsgStfu
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (s *Stfu) SerializedSize() (uint32, error) {
return MessageSerializedSize(s)
}
// A compile time check to ensure Stfu implements the // A compile time check to ensure Stfu implements the
// lnwire.LinkUpdater interface. // lnwire.LinkUpdater interface.
var _ LinkUpdater = (*Stfu)(nil) var _ LinkUpdater = (*Stfu)(nil)

View File

@@ -110,6 +110,10 @@ func NewUpdateAddHTLC() *UpdateAddHTLC {
// interface. // interface.
var _ Message = (*UpdateAddHTLC)(nil) var _ Message = (*UpdateAddHTLC)(nil)
// A compile time check to ensure UpdateAddHTLC implements the lnwire.SizeableMessage
// interface.
var _ SizeableMessage = (*UpdateAddHTLC)(nil)
// Decode deserializes a serialized UpdateAddHTLC message stored in the passed // Decode deserializes a serialized UpdateAddHTLC message stored in the passed
// io.Reader observing the specified protocol version. // io.Reader observing the specified protocol version.
// //
@@ -212,3 +216,10 @@ func (c *UpdateAddHTLC) MsgType() MessageType {
func (c *UpdateAddHTLC) TargetChanID() ChannelID { func (c *UpdateAddHTLC) TargetChanID() ChannelID {
return c.ChanID return c.ChanID
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (c *UpdateAddHTLC) SerializedSize() (uint32, error) {
return MessageSerializedSize(c)
}

View File

@@ -38,6 +38,10 @@ type UpdateFailHTLC struct {
// interface. // interface.
var _ Message = (*UpdateFailHTLC)(nil) var _ Message = (*UpdateFailHTLC)(nil)
// A compile time check to ensure UpdateFailHTLC implements the lnwire.SizeableMessage
// interface.
var _ SizeableMessage = (*UpdateFailHTLC)(nil)
// Decode deserializes a serialized UpdateFailHTLC message stored in the passed // Decode deserializes a serialized UpdateFailHTLC message stored in the passed
// io.Reader observing the specified protocol version. // io.Reader observing the specified protocol version.
// //
@@ -79,6 +83,13 @@ func (c *UpdateFailHTLC) MsgType() MessageType {
return MsgUpdateFailHTLC return MsgUpdateFailHTLC
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (c *UpdateFailHTLC) SerializedSize() (uint32, error) {
return MessageSerializedSize(c)
}
// TargetChanID returns the channel id of the link for which this message is // TargetChanID returns the channel id of the link for which this message is
// intended. // intended.
// //

View File

@@ -36,6 +36,10 @@ type UpdateFailMalformedHTLC struct {
// lnwire.Message interface. // lnwire.Message interface.
var _ Message = (*UpdateFailMalformedHTLC)(nil) var _ Message = (*UpdateFailMalformedHTLC)(nil)
// A compile time check to ensure UpdateFailMalformedHTLC implements the
// lnwire.SizeableMessage interface.
var _ SizeableMessage = (*UpdateFailMalformedHTLC)(nil)
// Decode deserializes a serialized UpdateFailMalformedHTLC message stored in the passed // Decode deserializes a serialized UpdateFailMalformedHTLC message stored in the passed
// io.Reader observing the specified protocol version. // io.Reader observing the specified protocol version.
// //
@@ -84,6 +88,13 @@ func (c *UpdateFailMalformedHTLC) MsgType() MessageType {
return MsgUpdateFailMalformedHTLC return MsgUpdateFailMalformedHTLC
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (c *UpdateFailMalformedHTLC) SerializedSize() (uint32, error) {
return MessageSerializedSize(c)
}
// TargetChanID returns the channel id of the link for which this message is // TargetChanID returns the channel id of the link for which this message is
// intended. // intended.
// //

View File

@@ -36,6 +36,10 @@ func NewUpdateFee(chanID ChannelID, feePerKw uint32) *UpdateFee {
// interface. // interface.
var _ Message = (*UpdateFee)(nil) var _ Message = (*UpdateFee)(nil)
// A compile time check to ensure UpdateFee implements the
// lnwire.SizeableMessage interface.
var _ SizeableMessage = (*UpdateFee)(nil)
// Decode deserializes a serialized UpdateFee message stored in the passed // Decode deserializes a serialized UpdateFee message stored in the passed
// io.Reader observing the specified protocol version. // io.Reader observing the specified protocol version.
// //
@@ -72,6 +76,13 @@ func (c *UpdateFee) MsgType() MessageType {
return MsgUpdateFee return MsgUpdateFee
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (c *UpdateFee) SerializedSize() (uint32, error) {
return MessageSerializedSize(c)
}
// TargetChanID returns the channel id of the link for which this message is // TargetChanID returns the channel id of the link for which this message is
// intended. // intended.
// //

View File

@@ -44,12 +44,16 @@ func NewUpdateFulfillHTLC(chanID ChannelID, id uint64,
} }
} }
// A compile time check to ensure UpdateFulfillHTLC implements the lnwire.Message // A compile time check to ensure UpdateFulfillHTLC implements the
// interface. // lnwire.Message interface.
var _ Message = (*UpdateFulfillHTLC)(nil) var _ Message = (*UpdateFulfillHTLC)(nil)
// Decode deserializes a serialized UpdateFulfillHTLC message stored in the passed // A compile time check to ensure UpdateFulfillHTLC implements the
// io.Reader observing the specified protocol version. // lnwire.SizeableMessage interface.
var _ SizeableMessage = (*UpdateFulfillHTLC)(nil)
// Decode deserializes a serialized UpdateFulfillHTLC message stored in the
// passed io.Reader observing the specified protocol version.
// //
// This is part of the lnwire.Message interface. // This is part of the lnwire.Message interface.
func (c *UpdateFulfillHTLC) Decode(r io.Reader, pver uint32) error { func (c *UpdateFulfillHTLC) Decode(r io.Reader, pver uint32) error {
@@ -115,6 +119,13 @@ func (c *UpdateFulfillHTLC) MsgType() MessageType {
return MsgUpdateFulfillHTLC return MsgUpdateFulfillHTLC
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (c *UpdateFulfillHTLC) SerializedSize() (uint32, error) {
return MessageSerializedSize(c)
}
// TargetChanID returns the channel id of the link for which this message is // TargetChanID returns the channel id of the link for which this message is
// intended. // intended.
// //

View File

@@ -29,6 +29,10 @@ type Warning struct {
// interface. // interface.
var _ Message = (*Warning)(nil) var _ Message = (*Warning)(nil)
// A compile time check to ensure Warning implements the lnwire.SizeableMessage
// interface.
var _ SizeableMessage = (*Warning)(nil)
// NewWarning creates a new Warning message. // NewWarning creates a new Warning message.
func NewWarning() *Warning { func NewWarning() *Warning {
return &Warning{} return &Warning{}
@@ -74,3 +78,10 @@ func (c *Warning) Encode(w *bytes.Buffer, _ uint32) error {
func (c *Warning) MsgType() MessageType { func (c *Warning) MsgType() MessageType {
return MsgWarning return MsgWarning
} }
// SerializedSize returns the serialized size of the message in bytes.
//
// This is part of the lnwire.SizeableMessage interface.
func (c *Warning) SerializedSize() (uint32, error) {
return MessageSerializedSize(c)
}