From 7ff04d8bad70fb6463713836f5f890a7dbe6d4db Mon Sep 17 00:00:00 2001 From: eugene Date: Thu, 4 Mar 2021 16:48:10 -0500 Subject: [PATCH] lnwire: remove MaxPayloadLength from Message interface Removes the MaxPayloadLength function from the Message interface and checks that each message payload is not greater than MaxMsgBody. Since all messages are now allowed to be 65535 bytes in size, the MaxPayloadLength is no longer needed. --- lnwire/accept_channel.go | 8 -------- lnwire/announcement_signatures.go | 8 -------- lnwire/channel_announcement.go | 8 -------- lnwire/channel_reestablish.go | 8 -------- lnwire/channel_update.go | 8 -------- lnwire/closing_signed.go | 8 -------- lnwire/commit_sig.go | 8 -------- lnwire/error.go | 8 -------- lnwire/funding_created.go | 8 -------- lnwire/funding_locked.go | 9 --------- lnwire/funding_signed.go | 8 -------- lnwire/gossip_timestamp_range.go | 9 --------- lnwire/init_message.go | 8 -------- lnwire/lnwire_test.go | 7 ++----- lnwire/message.go | 18 +++++------------- lnwire/node_announcement.go | 8 -------- lnwire/open_channel.go | 8 -------- lnwire/ping.go | 8 -------- lnwire/pong.go | 8 -------- lnwire/query_channel_range.go | 8 -------- lnwire/query_short_chan_ids.go | 8 -------- lnwire/reply_channel_range.go | 8 -------- lnwire/reply_short_chan_ids_end.go | 9 --------- lnwire/revoke_and_ack.go | 8 -------- lnwire/shutdown.go | 8 -------- lnwire/update_add_htlc.go | 8 -------- lnwire/update_fail_htlc.go | 8 -------- lnwire/update_fail_malformed_htlc.go | 9 --------- lnwire/update_fee.go | 8 -------- lnwire/update_fulfill_htlc.go | 8 -------- 30 files changed, 7 insertions(+), 246 deletions(-) diff --git a/lnwire/accept_channel.go b/lnwire/accept_channel.go index 57f2ad402..f88a1a08f 100644 --- a/lnwire/accept_channel.go +++ b/lnwire/accept_channel.go @@ -254,11 +254,3 @@ func parseShutdownScript(tlvRecords ExtraOpaqueData) (DeliveryAddress, func (a *AcceptChannel) MsgType() MessageType { 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 -} diff --git a/lnwire/announcement_signatures.go b/lnwire/announcement_signatures.go index 14341392b..41372db19 100644 --- a/lnwire/announcement_signatures.go +++ b/lnwire/announcement_signatures.go @@ -81,11 +81,3 @@ func (a *AnnounceSignatures) Encode(w io.Writer, pver uint32) error { func (a *AnnounceSignatures) MsgType() MessageType { 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 -} diff --git a/lnwire/channel_announcement.go b/lnwire/channel_announcement.go index de4b72b31..865b7b52c 100644 --- a/lnwire/channel_announcement.go +++ b/lnwire/channel_announcement.go @@ -112,14 +112,6 @@ func (a *ChannelAnnouncement) MsgType() MessageType { 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 // be signed. func (a *ChannelAnnouncement) DataToSign() ([]byte, error) { diff --git a/lnwire/channel_reestablish.go b/lnwire/channel_reestablish.go index bfe7c53ae..ebf2fcc1f 100644 --- a/lnwire/channel_reestablish.go +++ b/lnwire/channel_reestablish.go @@ -159,11 +159,3 @@ func (a *ChannelReestablish) Decode(r io.Reader, pver uint32) error { func (a *ChannelReestablish) MsgType() MessageType { 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 -} diff --git a/lnwire/channel_update.go b/lnwire/channel_update.go index 800000565..87a7d30a1 100644 --- a/lnwire/channel_update.go +++ b/lnwire/channel_update.go @@ -196,14 +196,6 @@ func (a *ChannelUpdate) MsgType() MessageType { 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 // be signed. func (a *ChannelUpdate) DataToSign() ([]byte, error) { diff --git a/lnwire/closing_signed.go b/lnwire/closing_signed.go index 7732715bb..3e4dcdf26 100644 --- a/lnwire/closing_signed.go +++ b/lnwire/closing_signed.go @@ -76,11 +76,3 @@ func (c *ClosingSigned) Encode(w io.Writer, pver uint32) error { func (c *ClosingSigned) MsgType() MessageType { 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 -} diff --git a/lnwire/commit_sig.go b/lnwire/commit_sig.go index 8856389f7..3251fe8dc 100644 --- a/lnwire/commit_sig.go +++ b/lnwire/commit_sig.go @@ -86,14 +86,6 @@ func (c *CommitSig) MsgType() MessageType { 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 // intended. // diff --git a/lnwire/error.go b/lnwire/error.go index 02c07aea5..0aa2d4c31 100644 --- a/lnwire/error.go +++ b/lnwire/error.go @@ -118,14 +118,6 @@ func (c *Error) MsgType() MessageType { 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 // printable ASCII characters if interpreted as a string. func isASCII(data []byte) bool { diff --git a/lnwire/funding_created.go b/lnwire/funding_created.go index 437b1b6a8..0f10214ed 100644 --- a/lnwire/funding_created.go +++ b/lnwire/funding_created.go @@ -66,11 +66,3 @@ func (f *FundingCreated) Decode(r io.Reader, pver uint32) error { func (f *FundingCreated) MsgType() MessageType { 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 -} diff --git a/lnwire/funding_locked.go b/lnwire/funding_locked.go index 1eeddfb6c..0f8e4ca1e 100644 --- a/lnwire/funding_locked.go +++ b/lnwire/funding_locked.go @@ -73,12 +73,3 @@ func (c *FundingLocked) Encode(w io.Writer, pver uint32) error { func (c *FundingLocked) MsgType() MessageType { 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 -} diff --git a/lnwire/funding_signed.go b/lnwire/funding_signed.go index 1ef155680..15c62cf05 100644 --- a/lnwire/funding_signed.go +++ b/lnwire/funding_signed.go @@ -49,11 +49,3 @@ func (f *FundingSigned) Decode(r io.Reader, pver uint32) error { func (f *FundingSigned) MsgType() MessageType { 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 -} diff --git a/lnwire/gossip_timestamp_range.go b/lnwire/gossip_timestamp_range.go index fb62e2721..6827b9818 100644 --- a/lnwire/gossip_timestamp_range.go +++ b/lnwire/gossip_timestamp_range.go @@ -73,12 +73,3 @@ func (g *GossipTimestampRange) Encode(w io.Writer, pver uint32) error { func (g *GossipTimestampRange) MsgType() MessageType { 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 -} diff --git a/lnwire/init_message.go b/lnwire/init_message.go index 4e33fbb6e..18fa693b4 100644 --- a/lnwire/init_message.go +++ b/lnwire/init_message.go @@ -71,11 +71,3 @@ func (msg *Init) Encode(w io.Writer, pver uint32) error { func (msg *Init) MsgType() MessageType { 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 -} diff --git a/lnwire/lnwire_test.go b/lnwire/lnwire_test.go index f9c48d382..14082bfe9 100644 --- a/lnwire/lnwire_test.go +++ b/lnwire/lnwire_test.go @@ -250,9 +250,6 @@ func TestEmptyMessageUnknownType(t *testing.T) { // TestLightningWireProtocol uses the testing/quick package to create a series // of fuzz tests to attempt to break a primary scenario which is implemented as // 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) { t.Parallel() @@ -274,9 +271,9 @@ func TestLightningWireProtocol(t *testing.T) { // the 2 bytes for the message type) is _below_ the specified // max payload size for this message. payloadLen := uint32(b.Len()) - 2 - if payloadLen > msg.MaxPayloadLength(0) { + if payloadLen > MaxMsgBody { t.Fatalf("msg payload constraint violated: %v > %v", - payloadLen, msg.MaxPayloadLength(0)) + payloadLen, MaxMsgBody) return false } diff --git a/lnwire/message.go b/lnwire/message.go index b5c27339e..048122887 100644 --- a/lnwire/message.go +++ b/lnwire/message.go @@ -151,7 +151,6 @@ type Serializable interface { type Message interface { Serializable MsgType() MessageType - MaxPayloadLength(uint32) uint32 } // 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() lenp := len(payload) - // Enforce maximum overall message payload. - if lenp > MaxMessagePayload { + // Enforce maximum message payload, which means the body cannot be + // greater than MaxMsgBody. + if lenp > MaxMsgBody { return totalBytes, fmt.Errorf("message payload is too large - "+ - "encoded %d bytes, but maximum message payload is %d bytes", - lenp, MaxMessagePayload) - } - - // 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) + "encoded %d bytes, but maximum message body is %d bytes", + lenp, MaxMsgBody) } // With the initial sanity checks complete, we'll now write out the diff --git a/lnwire/node_announcement.go b/lnwire/node_announcement.go index 62414d4b5..540137aae 100644 --- a/lnwire/node_announcement.go +++ b/lnwire/node_announcement.go @@ -145,14 +145,6 @@ func (a *NodeAnnouncement) MsgType() MessageType { 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. func (a *NodeAnnouncement) DataToSign() ([]byte, error) { diff --git a/lnwire/open_channel.go b/lnwire/open_channel.go index 70dbe7905..9407d0037 100644 --- a/lnwire/open_channel.go +++ b/lnwire/open_channel.go @@ -239,11 +239,3 @@ func (o *OpenChannel) Decode(r io.Reader, pver uint32) error { func (o *OpenChannel) MsgType() MessageType { 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 -} diff --git a/lnwire/ping.go b/lnwire/ping.go index cf9a83b78..e7e160d44 100644 --- a/lnwire/ping.go +++ b/lnwire/ping.go @@ -57,11 +57,3 @@ func (p *Ping) Encode(w io.Writer, pver uint32) error { func (p *Ping) MsgType() MessageType { 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 -} diff --git a/lnwire/pong.go b/lnwire/pong.go index c3166aaf6..b0d54523b 100644 --- a/lnwire/pong.go +++ b/lnwire/pong.go @@ -53,11 +53,3 @@ func (p *Pong) Encode(w io.Writer, pver uint32) error { func (p *Pong) MsgType() MessageType { 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 -} diff --git a/lnwire/query_channel_range.go b/lnwire/query_channel_range.go index 3bdb30e5e..167a4c64e 100644 --- a/lnwire/query_channel_range.go +++ b/lnwire/query_channel_range.go @@ -75,14 +75,6 @@ func (q *QueryChannelRange) MsgType() MessageType { 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 // QueryChannelRange message. func (q *QueryChannelRange) LastBlockHeight() uint32 { diff --git a/lnwire/query_short_chan_ids.go b/lnwire/query_short_chan_ids.go index bae23d9f5..92ffaf585 100644 --- a/lnwire/query_short_chan_ids.go +++ b/lnwire/query_short_chan_ids.go @@ -432,11 +432,3 @@ func encodeShortChanIDs(w io.Writer, encodingType ShortChanIDEncoding, func (q *QueryShortChanIDs) MsgType() MessageType { 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 -} diff --git a/lnwire/reply_channel_range.go b/lnwire/reply_channel_range.go index 5167cc5a5..2e8085c65 100644 --- a/lnwire/reply_channel_range.go +++ b/lnwire/reply_channel_range.go @@ -112,14 +112,6 @@ func (c *ReplyChannelRange) MsgType() MessageType { 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 // QueryChannelRange message. func (c *ReplyChannelRange) LastBlockHeight() uint32 { diff --git a/lnwire/reply_short_chan_ids_end.go b/lnwire/reply_short_chan_ids_end.go index 92f1e8fc3..2280dee4b 100644 --- a/lnwire/reply_short_chan_ids_end.go +++ b/lnwire/reply_short_chan_ids_end.go @@ -69,12 +69,3 @@ func (c *ReplyShortChanIDsEnd) Encode(w io.Writer, pver uint32) error { func (c *ReplyShortChanIDsEnd) MsgType() MessageType { 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 -} diff --git a/lnwire/revoke_and_ack.go b/lnwire/revoke_and_ack.go index b187fae6e..6562547fe 100644 --- a/lnwire/revoke_and_ack.go +++ b/lnwire/revoke_and_ack.go @@ -82,14 +82,6 @@ func (c *RevokeAndAck) MsgType() MessageType { 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 // intended. // diff --git a/lnwire/shutdown.go b/lnwire/shutdown.go index 8def329c0..4eec13523 100644 --- a/lnwire/shutdown.go +++ b/lnwire/shutdown.go @@ -57,11 +57,3 @@ func (s *Shutdown) Encode(w io.Writer, pver uint32) error { func (s *Shutdown) MsgType() MessageType { 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 -} diff --git a/lnwire/update_add_htlc.go b/lnwire/update_add_htlc.go index 9211d39ff..35fc5fc36 100644 --- a/lnwire/update_add_htlc.go +++ b/lnwire/update_add_htlc.go @@ -108,14 +108,6 @@ func (c *UpdateAddHTLC) MsgType() MessageType { 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 // intended. // diff --git a/lnwire/update_fail_htlc.go b/lnwire/update_fail_htlc.go index 09666ac25..8706c1dc8 100644 --- a/lnwire/update_fail_htlc.go +++ b/lnwire/update_fail_htlc.go @@ -71,14 +71,6 @@ func (c *UpdateFailHTLC) MsgType() MessageType { 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 // intended. // diff --git a/lnwire/update_fail_malformed_htlc.go b/lnwire/update_fail_malformed_htlc.go index b28ec29ff..e7994fccc 100644 --- a/lnwire/update_fail_malformed_htlc.go +++ b/lnwire/update_fail_malformed_htlc.go @@ -71,15 +71,6 @@ func (c *UpdateFailMalformedHTLC) MsgType() MessageType { 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 // intended. // diff --git a/lnwire/update_fee.go b/lnwire/update_fee.go index 25ab180c2..375b5c671 100644 --- a/lnwire/update_fee.go +++ b/lnwire/update_fee.go @@ -67,14 +67,6 @@ func (c *UpdateFee) MsgType() MessageType { 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 // intended. // diff --git a/lnwire/update_fulfill_htlc.go b/lnwire/update_fulfill_htlc.go index 36977b1e9..2b7f2e494 100644 --- a/lnwire/update_fulfill_htlc.go +++ b/lnwire/update_fulfill_htlc.go @@ -77,14 +77,6 @@ func (c *UpdateFulfillHTLC) MsgType() MessageType { 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 // intended. //