Merge pull request #5073 from Crypt-iQ/writemessage_03042021

lnwire: remove MaxPayloadLength from lnwire.Message interface
This commit is contained in:
Eugene 2021-03-09 04:50:40 -08:00 committed by GitHub
commit 6dca38c561
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
30 changed files with 7 additions and 246 deletions

@ -254,11 +254,3 @@ func parseShutdownScript(tlvRecords ExtraOpaqueData) (DeliveryAddress,
func (a *AcceptChannel) MsgType() MessageType { func (a *AcceptChannel) MsgType() MessageType {
return MsgAcceptChannel return MsgAcceptChannel
} }
// MaxPayloadLength returns the maximum allowed payload length for a
// AcceptChannel message.
//
// This is part of the lnwire.Message interface.
func (a *AcceptChannel) MaxPayloadLength(uint32) uint32 {
return MaxMsgBody
}

@ -81,11 +81,3 @@ func (a *AnnounceSignatures) Encode(w io.Writer, pver uint32) error {
func (a *AnnounceSignatures) MsgType() MessageType { func (a *AnnounceSignatures) MsgType() MessageType {
return MsgAnnounceSignatures return MsgAnnounceSignatures
} }
// MaxPayloadLength returns the maximum allowed payload size for this message
// observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (a *AnnounceSignatures) MaxPayloadLength(pver uint32) uint32 {
return MaxMsgBody
}

@ -112,14 +112,6 @@ func (a *ChannelAnnouncement) MsgType() MessageType {
return MsgChannelAnnouncement return MsgChannelAnnouncement
} }
// MaxPayloadLength returns the maximum allowed payload size for this message
// observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (a *ChannelAnnouncement) MaxPayloadLength(pver uint32) uint32 {
return MaxMsgBody
}
// 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 *ChannelAnnouncement) DataToSign() ([]byte, error) { func (a *ChannelAnnouncement) DataToSign() ([]byte, error) {

@ -159,11 +159,3 @@ func (a *ChannelReestablish) Decode(r io.Reader, pver uint32) error {
func (a *ChannelReestablish) MsgType() MessageType { func (a *ChannelReestablish) MsgType() MessageType {
return MsgChannelReestablish return MsgChannelReestablish
} }
// MaxPayloadLength returns the maximum allowed payload size for this message
// observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (a *ChannelReestablish) MaxPayloadLength(pver uint32) uint32 {
return MaxMsgBody
}

@ -196,14 +196,6 @@ func (a *ChannelUpdate) MsgType() MessageType {
return MsgChannelUpdate return MsgChannelUpdate
} }
// MaxPayloadLength returns the maximum allowed payload size for this message
// observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (a *ChannelUpdate) MaxPayloadLength(pver uint32) uint32 {
return MaxMsgBody
}
// 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 *ChannelUpdate) DataToSign() ([]byte, error) { func (a *ChannelUpdate) DataToSign() ([]byte, error) {

@ -76,11 +76,3 @@ func (c *ClosingSigned) Encode(w io.Writer, pver uint32) error {
func (c *ClosingSigned) MsgType() MessageType { func (c *ClosingSigned) MsgType() MessageType {
return MsgClosingSigned return MsgClosingSigned
} }
// MaxPayloadLength returns the maximum allowed payload size for a
// ClosingSigned complete message observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (c *ClosingSigned) MaxPayloadLength(uint32) uint32 {
return MaxMsgBody
}

@ -86,14 +86,6 @@ func (c *CommitSig) MsgType() MessageType {
return MsgCommitSig return MsgCommitSig
} }
// MaxPayloadLength returns the maximum allowed payload size for a
// CommitSig complete message observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (c *CommitSig) MaxPayloadLength(uint32) uint32 {
return MaxMsgBody
}
// 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.
// //

@ -118,14 +118,6 @@ func (c *Error) MsgType() MessageType {
return MsgError return MsgError
} }
// MaxPayloadLength returns the maximum allowed payload size for an Error
// complete message observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (c *Error) MaxPayloadLength(uint32) uint32 {
return MaxMsgBody
}
// 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 {

@ -66,11 +66,3 @@ func (f *FundingCreated) Decode(r io.Reader, pver uint32) error {
func (f *FundingCreated) MsgType() MessageType { func (f *FundingCreated) MsgType() MessageType {
return MsgFundingCreated return MsgFundingCreated
} }
// MaxPayloadLength returns the maximum allowed payload length for a
// FundingCreated message.
//
// This is part of the lnwire.Message interface.
func (f *FundingCreated) MaxPayloadLength(uint32) uint32 {
return MaxMsgBody
}

@ -73,12 +73,3 @@ func (c *FundingLocked) Encode(w io.Writer, pver uint32) error {
func (c *FundingLocked) MsgType() MessageType { func (c *FundingLocked) MsgType() MessageType {
return MsgFundingLocked return MsgFundingLocked
} }
// MaxPayloadLength returns the maximum allowed payload length for a
// FundingLocked message. This is calculated by summing the max length of all
// the fields within a FundingLocked message.
//
// This is part of the lnwire.Message interface.
func (c *FundingLocked) MaxPayloadLength(uint32) uint32 {
return MaxMsgBody
}

@ -49,11 +49,3 @@ func (f *FundingSigned) Decode(r io.Reader, pver uint32) error {
func (f *FundingSigned) MsgType() MessageType { func (f *FundingSigned) MsgType() MessageType {
return MsgFundingSigned return MsgFundingSigned
} }
// MaxPayloadLength returns the maximum allowed payload length for a
// FundingSigned message.
//
// This is part of the lnwire.Message interface.
func (f *FundingSigned) MaxPayloadLength(uint32) uint32 {
return MaxMsgBody
}

@ -73,12 +73,3 @@ func (g *GossipTimestampRange) Encode(w io.Writer, pver uint32) error {
func (g *GossipTimestampRange) MsgType() MessageType { func (g *GossipTimestampRange) MsgType() MessageType {
return MsgGossipTimestampRange return MsgGossipTimestampRange
} }
// MaxPayloadLength returns the maximum allowed payload size for a
// GossipTimestampRange complete message observing the specified protocol
// version.
//
// This is part of the lnwire.Message interface.
func (g *GossipTimestampRange) MaxPayloadLength(uint32) uint32 {
return MaxMsgBody
}

@ -71,11 +71,3 @@ func (msg *Init) Encode(w io.Writer, pver uint32) error {
func (msg *Init) MsgType() MessageType { func (msg *Init) MsgType() MessageType {
return MsgInit return MsgInit
} }
// MaxPayloadLength returns the maximum allowed payload size for an Init
// complete message observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (msg *Init) MaxPayloadLength(uint32) uint32 {
return MaxMsgBody
}

@ -250,9 +250,6 @@ func TestEmptyMessageUnknownType(t *testing.T) {
// TestLightningWireProtocol uses the testing/quick package to create a series // TestLightningWireProtocol uses the testing/quick package to create a series
// of fuzz tests to attempt to break a primary scenario which is implemented as // of fuzz tests to attempt to break a primary scenario which is implemented as
// property based testing scenario. // property based testing scenario.
//
// Debug help: when the message payload can reach a size larger than the return
// value of MaxPayloadLength, the test can panic without a helpful message.
func TestLightningWireProtocol(t *testing.T) { func TestLightningWireProtocol(t *testing.T) {
t.Parallel() t.Parallel()
@ -274,9 +271,9 @@ func TestLightningWireProtocol(t *testing.T) {
// the 2 bytes for the message type) is _below_ the specified // the 2 bytes for the message type) is _below_ the specified
// max payload size for this message. // max payload size for this message.
payloadLen := uint32(b.Len()) - 2 payloadLen := uint32(b.Len()) - 2
if payloadLen > msg.MaxPayloadLength(0) { if payloadLen > MaxMsgBody {
t.Fatalf("msg payload constraint violated: %v > %v", t.Fatalf("msg payload constraint violated: %v > %v",
payloadLen, msg.MaxPayloadLength(0)) payloadLen, MaxMsgBody)
return false return false
} }

@ -151,7 +151,6 @@ type Serializable interface {
type Message interface { type Message interface {
Serializable Serializable
MsgType() MessageType MsgType() MessageType
MaxPayloadLength(uint32) uint32
} }
// makeEmptyMessage creates a new empty message of the proper concrete type // makeEmptyMessage creates a new empty message of the proper concrete type
@ -237,19 +236,12 @@ func WriteMessage(w io.Writer, msg Message, pver uint32) (int, error) {
payload := bw.Bytes() payload := bw.Bytes()
lenp := len(payload) lenp := len(payload)
// Enforce maximum overall message payload. // Enforce maximum message payload, which means the body cannot be
if lenp > MaxMessagePayload { // greater than MaxMsgBody.
if lenp > MaxMsgBody {
return totalBytes, fmt.Errorf("message payload is too large - "+ return totalBytes, fmt.Errorf("message payload is too large - "+
"encoded %d bytes, but maximum message payload is %d bytes", "encoded %d bytes, but maximum message body is %d bytes",
lenp, MaxMessagePayload) lenp, MaxMsgBody)
}
// Enforce maximum message payload on the message type.
mpl := msg.MaxPayloadLength(pver)
if uint32(lenp) > mpl {
return totalBytes, fmt.Errorf("message payload is too large - "+
"encoded %d bytes, but maximum message payload of "+
"type %v is %d bytes", lenp, msg.MsgType(), mpl)
} }
// With the initial sanity checks complete, we'll now write out the // With the initial sanity checks complete, we'll now write out the

@ -145,14 +145,6 @@ func (a *NodeAnnouncement) MsgType() MessageType {
return MsgNodeAnnouncement return MsgNodeAnnouncement
} }
// MaxPayloadLength returns the maximum allowed payload size for this message
// observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (a *NodeAnnouncement) MaxPayloadLength(pver uint32) uint32 {
return MaxMsgBody
}
// DataToSign returns the part of the message that should be signed. // DataToSign returns the part of the message that should be signed.
func (a *NodeAnnouncement) DataToSign() ([]byte, error) { func (a *NodeAnnouncement) DataToSign() ([]byte, error) {

@ -239,11 +239,3 @@ func (o *OpenChannel) Decode(r io.Reader, pver uint32) error {
func (o *OpenChannel) MsgType() MessageType { func (o *OpenChannel) MsgType() MessageType {
return MsgOpenChannel return MsgOpenChannel
} }
// MaxPayloadLength returns the maximum allowed payload length for a
// OpenChannel message.
//
// This is part of the lnwire.Message interface.
func (o *OpenChannel) MaxPayloadLength(uint32) uint32 {
return MaxMsgBody
}

@ -57,11 +57,3 @@ func (p *Ping) Encode(w io.Writer, pver uint32) error {
func (p *Ping) MsgType() MessageType { func (p *Ping) MsgType() MessageType {
return MsgPing return MsgPing
} }
// MaxPayloadLength returns the maximum allowed payload size for a Ping
// complete message observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (p Ping) MaxPayloadLength(uint32) uint32 {
return 65532
}

@ -53,11 +53,3 @@ func (p *Pong) Encode(w io.Writer, pver uint32) error {
func (p *Pong) MsgType() MessageType { func (p *Pong) MsgType() MessageType {
return MsgPong return MsgPong
} }
// MaxPayloadLength returns the maximum allowed payload size for a Pong
// complete message observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (p *Pong) MaxPayloadLength(uint32) uint32 {
return 65532
}

@ -75,14 +75,6 @@ func (q *QueryChannelRange) MsgType() MessageType {
return MsgQueryChannelRange return MsgQueryChannelRange
} }
// MaxPayloadLength returns the maximum allowed payload size for a
// QueryChannelRange complete message observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (q *QueryChannelRange) MaxPayloadLength(uint32) uint32 {
return MaxMsgBody
}
// 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 {

@ -432,11 +432,3 @@ func encodeShortChanIDs(w io.Writer, encodingType ShortChanIDEncoding,
func (q *QueryShortChanIDs) MsgType() MessageType { func (q *QueryShortChanIDs) MsgType() MessageType {
return MsgQueryShortChanIDs return MsgQueryShortChanIDs
} }
// MaxPayloadLength returns the maximum allowed payload size for a
// QueryShortChanIDs complete message observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (q *QueryShortChanIDs) MaxPayloadLength(uint32) uint32 {
return MaxMsgBody
}

@ -112,14 +112,6 @@ func (c *ReplyChannelRange) MsgType() MessageType {
return MsgReplyChannelRange return MsgReplyChannelRange
} }
// MaxPayloadLength returns the maximum allowed payload size for a
// ReplyChannelRange complete message observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (c *ReplyChannelRange) MaxPayloadLength(uint32) uint32 {
return MaxMsgBody
}
// 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 (c *ReplyChannelRange) LastBlockHeight() uint32 { func (c *ReplyChannelRange) LastBlockHeight() uint32 {

@ -69,12 +69,3 @@ func (c *ReplyShortChanIDsEnd) Encode(w io.Writer, pver uint32) error {
func (c *ReplyShortChanIDsEnd) MsgType() MessageType { func (c *ReplyShortChanIDsEnd) MsgType() MessageType {
return MsgReplyShortChanIDsEnd return MsgReplyShortChanIDsEnd
} }
// MaxPayloadLength returns the maximum allowed payload size for a
// ReplyShortChanIDsEnd complete message observing the specified protocol
// version.
//
// This is part of the lnwire.Message interface.
func (c *ReplyShortChanIDsEnd) MaxPayloadLength(uint32) uint32 {
return MaxMsgBody
}

@ -82,14 +82,6 @@ func (c *RevokeAndAck) MsgType() MessageType {
return MsgRevokeAndAck return MsgRevokeAndAck
} }
// MaxPayloadLength returns the maximum allowed payload size for a RevokeAndAck
// complete message observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (c *RevokeAndAck) MaxPayloadLength(uint32) uint32 {
return MaxMsgBody
}
// 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.
// //

@ -57,11 +57,3 @@ func (s *Shutdown) Encode(w io.Writer, pver uint32) error {
func (s *Shutdown) MsgType() MessageType { func (s *Shutdown) MsgType() MessageType {
return MsgShutdown return MsgShutdown
} }
// MaxPayloadLength returns the maximum allowed payload size for this message
// observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (s *Shutdown) MaxPayloadLength(pver uint32) uint32 {
return MaxMsgBody
}

@ -108,14 +108,6 @@ func (c *UpdateAddHTLC) MsgType() MessageType {
return MsgUpdateAddHTLC return MsgUpdateAddHTLC
} }
// MaxPayloadLength returns the maximum allowed payload size for an UpdateAddHTLC
// complete message observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (c *UpdateAddHTLC) MaxPayloadLength(uint32) uint32 {
return MaxMsgBody
}
// 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.
// //

@ -71,14 +71,6 @@ func (c *UpdateFailHTLC) MsgType() MessageType {
return MsgUpdateFailHTLC return MsgUpdateFailHTLC
} }
// MaxPayloadLength returns the maximum allowed payload size for an UpdateFailHTLC
// complete message observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (c *UpdateFailHTLC) MaxPayloadLength(uint32) uint32 {
return MaxMsgBody
}
// 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.
// //

@ -71,15 +71,6 @@ func (c *UpdateFailMalformedHTLC) MsgType() MessageType {
return MsgUpdateFailMalformedHTLC return MsgUpdateFailMalformedHTLC
} }
// MaxPayloadLength returns the maximum allowed payload size for a
// UpdateFailMalformedHTLC complete message observing the specified protocol
// version.
//
// This is part of the lnwire.Message interface.
func (c *UpdateFailMalformedHTLC) MaxPayloadLength(uint32) uint32 {
return MaxMsgBody
}
// 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.
// //

@ -67,14 +67,6 @@ func (c *UpdateFee) MsgType() MessageType {
return MsgUpdateFee return MsgUpdateFee
} }
// MaxPayloadLength returns the maximum allowed payload size for an UpdateFee
// complete message observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (c *UpdateFee) MaxPayloadLength(uint32) uint32 {
return MaxMsgBody
}
// 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.
// //

@ -77,14 +77,6 @@ func (c *UpdateFulfillHTLC) MsgType() MessageType {
return MsgUpdateFulfillHTLC return MsgUpdateFulfillHTLC
} }
// MaxPayloadLength returns the maximum allowed payload size for an UpdateFulfillHTLC
// complete message observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (c *UpdateFulfillHTLC) MaxPayloadLength(uint32) uint32 {
return MaxMsgBody
}
// 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.
// //