From f6b3c25f95608086ac5891ba0868e5449033f4e0 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 19 Apr 2017 15:57:43 -0700 Subject: [PATCH] lnwire: on Message interface convert Command to MsgType This commit modifies the Message interface to convert the Command method to a MsgType method that uses a new set of message type for all the defined messages. These new messages types nearly exactly match the message types used within the current draft of the BOLT specifications. --- lnwire/announcement_signatures.go | 6 +- lnwire/channel_announcement.go | 6 +- lnwire/channel_update.go | 6 +- lnwire/close_complete.go | 10 +-- lnwire/close_request.go | 8 +- lnwire/commit_sig.go | 6 +- lnwire/error.go | 8 +- lnwire/funding_locked.go | 7 +- lnwire/init_message.go | 21 ++--- lnwire/message.go | 122 ++++++++++++-------------- lnwire/node_announcement.go | 6 +- lnwire/ping.go | 6 +- lnwire/pong.go | 6 +- lnwire/revoke_and_ack.go | 6 +- lnwire/single_funding_complete.go | 8 +- lnwire/single_funding_request.go | 5 +- lnwire/single_funding_response.go | 7 +- lnwire/single_funding_signcomplete.go | 7 +- lnwire/update_add_htlc.go | 7 +- lnwire/update_fail_htlc.go | 6 +- lnwire/update_fulfill_htlc.go | 6 +- 21 files changed, 119 insertions(+), 151 deletions(-) diff --git a/lnwire/announcement_signatures.go b/lnwire/announcement_signatures.go index a79eebcc..913ba64c 100644 --- a/lnwire/announcement_signatures.go +++ b/lnwire/announcement_signatures.go @@ -66,12 +66,12 @@ func (a *AnnounceSignatures) Encode(w io.Writer, pver uint32) error { ) } -// Command returns the integer uniquely identifying this message type on the +// MsgType returns the integer uniquely identifying this message type on the // wire. // // This is part of the lnwire.Message interface. -func (a *AnnounceSignatures) Command() uint32 { - return CmdAnnounceSignatures +func (a *AnnounceSignatures) MsgType() MessageType { + return MsgAnnounceSignatures } // MaxPayloadLength returns the maximum allowed payload size for this message diff --git a/lnwire/channel_announcement.go b/lnwire/channel_announcement.go index 8e6eb1b9..6818ca4c 100644 --- a/lnwire/channel_announcement.go +++ b/lnwire/channel_announcement.go @@ -79,12 +79,12 @@ func (a *ChannelAnnouncement) Encode(w io.Writer, pver uint32) error { ) } -// Command returns the integer uniquely identifying this message type on the +// MsgType returns the integer uniquely identifying this message type on the // wire. // // This is part of the lnwire.Message interface. -func (a *ChannelAnnouncement) Command() uint32 { - return CmdChannelAnnouncement +func (a *ChannelAnnouncement) MsgType() MessageType { + return MsgChannelAnnouncement } // MaxPayloadLength returns the maximum allowed payload size for this message diff --git a/lnwire/channel_update.go b/lnwire/channel_update.go index 38991218..18605235 100644 --- a/lnwire/channel_update.go +++ b/lnwire/channel_update.go @@ -84,12 +84,12 @@ func (a *ChannelUpdate) Encode(w io.Writer, pver uint32) error { ) } -// Command returns the integer uniquely identifying this message type on the +// MsgType returns the integer uniquely identifying this message type on the // wire. // // This is part of the lnwire.Message interface. -func (a *ChannelUpdate) Command() uint32 { - return CmdChannelUpdate +func (a *ChannelUpdate) MsgType() MessageType { + return MsgChannelUpdate } // MaxPayloadLength returns the maximum allowed payload size for this message diff --git a/lnwire/close_complete.go b/lnwire/close_complete.go index 5b673a42..653180ae 100644 --- a/lnwire/close_complete.go +++ b/lnwire/close_complete.go @@ -41,8 +41,6 @@ var _ Message = (*CloseComplete)(nil) // // This is part of the lnwire.Message interface. func (c *CloseComplete) Decode(r io.Reader, pver uint32) error { - // ChannelPoint (8) - // ResponderCloseSig (73) return readElements(r, &c.ChannelPoint, &c.ResponderCloseSig) @@ -53,19 +51,17 @@ func (c *CloseComplete) Decode(r io.Reader, pver uint32) error { // // This is part of the lnwire.Message interface. func (c *CloseComplete) Encode(w io.Writer, pver uint32) error { - // ChannelPoint (8) - // ResponderCloseSig (73) return writeElements(w, c.ChannelPoint, c.ResponderCloseSig) } -// Command returns the integer uniquely identifying this message type on the +// MsgType returns the integer uniquely identifying this message type on the // wire. // // This is part of the lnwire.Message interface. -func (c *CloseComplete) Command() uint32 { - return CmdCloseComplete +func (c *CloseComplete) MsgType() MessageType { + return MsgCloseComplete } // MaxPayloadLength returns the maximum allowed payload size for a CloseComplete diff --git a/lnwire/close_request.go b/lnwire/close_request.go index eb6a6bd0..6510ff42 100644 --- a/lnwire/close_request.go +++ b/lnwire/close_request.go @@ -1,8 +1,6 @@ package lnwire import ( - "fmt" - "github.com/roasbeef/btcd/btcec" "github.com/roasbeef/btcutil" @@ -69,12 +67,12 @@ func (c *CloseRequest) Encode(w io.Writer, pver uint32) error { c.Fee) } -// Command returns the integer uniquely identifying this message type on the +// MsgType returns the integer uniquely identifying this message type on the // wire. // // This is part of the lnwire.Message interface. -func (c *CloseRequest) Command() uint32 { - return CmdCloseRequest +func (c *CloseRequest) MsgType() MessageType { + return MsgCloseRequest } // MaxPayloadLength returns the maximum allowed payload size for this message diff --git a/lnwire/commit_sig.go b/lnwire/commit_sig.go index e9f0acb3..8da7833c 100644 --- a/lnwire/commit_sig.go +++ b/lnwire/commit_sig.go @@ -60,12 +60,12 @@ func (c *CommitSig) Encode(w io.Writer, pver uint32) error { ) } -// Command returns the integer uniquely identifying this message type on the +// MsgType returns the integer uniquely identifying this message type on the // wire. // // This is part of the lnwire.Message interface. -func (c *CommitSig) Command() uint32 { - return CmdCommitSig +func (c *CommitSig) MsgType() MessageType { + return MsgCommitSig } // MaxPayloadLength returns the maximum allowed payload size for a diff --git a/lnwire/error.go b/lnwire/error.go index 4c296f1e..cf28a491 100644 --- a/lnwire/error.go +++ b/lnwire/error.go @@ -1,9 +1,7 @@ package lnwire import ( - "fmt" "io" - "math" "google.golang.org/grpc/codes" ) @@ -92,12 +90,12 @@ func (c *Error) Encode(w io.Writer, pver uint32) error { ) } -// Command returns the integer uniquely identifying an Error message on the +// MsgType returns the integer uniquely identifying an Error message on the // wire. // // This is part of the lnwire.Message interface. -func (c *Error) Command() uint32 { - return CmdError +func (c *Error) MsgType() MessageType { + return MsgError } // MaxPayloadLength returns the maximum allowed payload size for a Error diff --git a/lnwire/funding_locked.go b/lnwire/funding_locked.go index a4a0202e..ed22c5ee 100644 --- a/lnwire/funding_locked.go +++ b/lnwire/funding_locked.go @@ -1,7 +1,6 @@ package lnwire import ( - "fmt" "io" "github.com/roasbeef/btcd/btcec" @@ -57,12 +56,12 @@ func (c *FundingLocked) Encode(w io.Writer, pver uint32) error { c.NextPerCommitmentPoint) } -// Command returns the uint32 code which uniquely identifies this message as a +// MsgType returns the uint32 code which uniquely identifies this message as a // FundingLocked message on the wire. // // This is part of the lnwire.Message interface. -func (c *FundingLocked) Command() uint32 { - return CmdFundingLocked +func (c *FundingLocked) MsgType() MessageType { + return MsgFundingLocked } // MaxPayloadLength returns the maximum allowed payload length for a diff --git a/lnwire/init_message.go b/lnwire/init_message.go index 327c254d..471d64d9 100644 --- a/lnwire/init_message.go +++ b/lnwire/init_message.go @@ -1,9 +1,6 @@ package lnwire -import ( - "github.com/go-errors/errors" - "io" -) +import "io" // Init is the first message reveals the features supported or required by this // node. Nodes wait for receipt of the other's features to simplify error @@ -27,23 +24,21 @@ func NewInitMessage(gf, lf *FeatureVector) *Init { } } +// A compile time check to ensure Init implements the lnwire.Message +// interface. +var _ Message = (*Init)(nil) + // Decode deserializes a serialized Init message stored in the passed // io.Reader observing the specified protocol version. // // This is part of the lnwire.Message interface. func (msg *Init) Decode(r io.Reader, pver uint32) error { - // LocalFeatures(~) - // GlobalFeatures(~) return readElements(r, &msg.LocalFeatures, &msg.GlobalFeatures, ) } -// A compile time check to ensure Init implements the lnwire.Message -// interface. -var _ Message = (*Init)(nil) - // Encode serializes the target Init into the passed io.Writer observing // the protocol version specified. // @@ -55,12 +50,12 @@ func (msg *Init) Encode(w io.Writer, pver uint32) error { ) } -// Command returns the integer uniquely identifying this message type on the +// MsgType returns the integer uniquely identifying this message type on the // wire. // // This is part of the lnwire.Message interface. -func (msg *Init) Command() uint32 { - return CmdInit +func (msg *Init) MsgType() MessageType { + return MsgInit } // MaxPayloadLength returns the maximum allowed payload size for a Init diff --git a/lnwire/message.go b/lnwire/message.go index 9c538152..fbb80924 100644 --- a/lnwire/message.go +++ b/lnwire/message.go @@ -4,10 +4,9 @@ package lnwire import ( "bytes" + "encoding/binary" "fmt" "io" - - "github.com/roasbeef/btcd/wire" ) // MessageHeaderSize is the number of bytes in a lightning message header. @@ -19,53 +18,42 @@ const MessageHeaderSize = 12 // MaxMessagePayload is the maximum bytes a message can be regardless of other // individual limits imposed by messages themselves. - -// Commands used in lightning message headers which detail the type of message. -// TODO(roasbeef): update with latest type numbering from spec -const ( - CmdInit = uint32(1) - - // Commands for opening a channel funded by one party (single funder). - CmdSingleFundingRequest = uint32(100) - CmdSingleFundingResponse = uint32(110) - CmdSingleFundingComplete = uint32(120) - CmdSingleFundingSignComplete = uint32(130) - - // Command for locking a funded channel - CmdFundingLocked = uint32(200) const MaxMessagePayload = 65535 // 65KB - // Commands for the workflow of cooperatively closing an active channel. - CmdCloseRequest = uint32(300) - CmdCloseComplete = uint32(310) +// 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 +// consists simply of 2-byte message type. We omit a length field, and checksum +// as the Lighting Protocol is intended to be encapsulated within a +// confidential+authenticated cryptographic messaging protocol. +type MessageType uint16 - // Commands for negotiating HTLCs. - CmdUpdateAddHTLC = uint32(1000) - CmdUpdateFufillHTLC = uint32(1010) - CmdUpdateFailHTLC = uint32(1020) - - // Commands for modifying commitment transactions. - CmdCommitSig = uint32(2000) - CmdRevokeAndAck = uint32(2010) - - // Commands for reporting protocol errors. - CmdError = uint32(4000) - - // Commands for discovery service. - CmdChannelAnnouncement = uint32(5000) - CmdChannelUpdateAnnouncement = uint32(5010) - CmdNodeAnnouncement = uint32(5020) - CmdAnnounceSignatures = uint32(5030) - - // Commands for connection keep-alive. - CmdPing = uint32(6000) - CmdPong = uint32(6010) +const ( + MsgInit MessageType = 16 + MsgError = 17 + MsgPing = 18 + MsgPong = 19 + MsgSingleFundingRequest = 32 + MsgSingleFundingResponse = 33 + MsgSingleFundingComplete = 34 + MsgSingleFundingSignComplete = 35 + MsgFundingLocked = 36 + MsgCloseRequest = 39 + MsgCloseComplete = 40 + MsgUpdateAddHTLC = 128 + MsgUpdateFufillHTLC = 130 + MsgUpdateFailHTLC = 131 + MsgCommitSig = 132 + MsgRevokeAndAck = 133 + MsgChannelAnnouncement = 256 + MsgNodeAnnouncement = 257 + MsgChannelUpdate = 258 + MsgAnnounceSignatures = 259 ) // UnknownMessage is an implementation of the error interface that allows the // creation of an error in response to an unknown message. type UnknownMessage struct { - messageType uint32 + messageType MessageType } // Error returns a human readable string describing the error. @@ -82,58 +70,58 @@ func (u *UnknownMessage) Error() string { type Message interface { Decode(io.Reader, uint32) error Encode(io.Writer, uint32) error - Command() uint32 + MsgType() MessageType MaxPayloadLength(uint32) uint32 } // makeEmptyMessage creates a new empty message of the proper concrete type -// based on the command ID. -func makeEmptyMessage(command uint32) (Message, error) { +// based on the passed message type. +func makeEmptyMessage(msgType MessageType) (Message, error) { var msg Message - switch command { - case CmdInit: + switch msgType { + case MsgInit: msg = &Init{} - case CmdSingleFundingRequest: + case MsgSingleFundingRequest: msg = &SingleFundingRequest{} - case CmdSingleFundingResponse: + case MsgSingleFundingResponse: msg = &SingleFundingResponse{} - case CmdSingleFundingComplete: + case MsgSingleFundingComplete: msg = &SingleFundingComplete{} - case CmdSingleFundingSignComplete: + case MsgSingleFundingSignComplete: msg = &SingleFundingSignComplete{} - case CmdFundingLocked: + case MsgFundingLocked: msg = &FundingLocked{} - case CmdCloseRequest: + case MsgCloseRequest: msg = &CloseRequest{} - case CmdCloseComplete: + case MsgCloseComplete: msg = &CloseComplete{} - case CmdUpdateAddHTLC: + case MsgUpdateAddHTLC: msg = &UpdateAddHTLC{} - case CmdUpdateFailHTLC: + case MsgUpdateFailHTLC: msg = &UpdateFailHTLC{} - case CmdUpdateFufillHTLC: + case MsgUpdateFufillHTLC: msg = &UpdateFufillHTLC{} - case CmdCommitSig: + case MsgCommitSig: msg = &CommitSig{} - case CmdRevokeAndAck: + case MsgRevokeAndAck: msg = &RevokeAndAck{} - case CmdError: + case MsgError: msg = &Error{} - case CmdChannelAnnouncement: + case MsgChannelAnnouncement: msg = &ChannelAnnouncement{} - case CmdChannelUpdateAnnouncement: - msg = &ChannelUpdateAnnouncement{} - case CmdNodeAnnouncement: + case MsgChannelUpdate: + msg = &ChannelUpdate{} + case MsgNodeAnnouncement: msg = &NodeAnnouncement{} - case CmdPing: + case MsgPing: msg = &Ping{} - case CmdAnnounceSignatures: + case MsgAnnounceSignatures: msg = &AnnounceSignatures{} - case CmdPong: + case MsgPong: msg = &Pong{} default: - return nil, fmt.Errorf("unhandled command [%d]", command) + return nil, fmt.Errorf("unknown message type [%d]", msgType) } return msg, nil diff --git a/lnwire/node_announcement.go b/lnwire/node_announcement.go index a7e12107..82a21b01 100644 --- a/lnwire/node_announcement.go +++ b/lnwire/node_announcement.go @@ -140,12 +140,12 @@ func (a *NodeAnnouncement) Encode(w io.Writer, pver uint32) error { ) } -// Command returns the integer uniquely identifying this message type on the +// MsgType returns the integer uniquely identifying this message type on the // wire. // // This is part of the lnwire.Message interface. -func (a *NodeAnnouncement) Command() uint32 { - return CmdNodeAnnouncement +func (a *NodeAnnouncement) MsgType() MessageType { + return MsgNodeAnnouncement } // MaxPayloadLength returns the maximum allowed payload size for this message diff --git a/lnwire/ping.go b/lnwire/ping.go index 18635346..49e0c06c 100644 --- a/lnwire/ping.go +++ b/lnwire/ping.go @@ -50,12 +50,12 @@ func (p *Ping) Encode(w io.Writer, pver uint32) error { p.PaddingBytes) } -// Command returns the integer uniquely identifying this message type on the +// MsgType returns the integer uniquely identifying this message type on the // wire. // // This is part of the lnwire.Message interface. -func (p *Ping) Command() uint32 { - return CmdPing +func (p *Ping) MsgType() MessageType { + return MsgPing } // MaxPayloadLength returns the maximum allowed payload size for a Ping diff --git a/lnwire/pong.go b/lnwire/pong.go index e503b71b..378ecb16 100644 --- a/lnwire/pong.go +++ b/lnwire/pong.go @@ -46,12 +46,12 @@ func (p *Pong) Encode(w io.Writer, pver uint32) error { ) } -// Command returns the integer uniquely identifying this message type on the +// MsgType returns the integer uniquely identifying this message type on the // wire. // // This is part of the lnwire.Message interface. -func (p *Pong) Command() uint32 { - return CmdPong +func (p *Pong) MsgType() MessageType { + return MsgPong } // MaxPayloadLength returns the maximum allowed payload size for a Pong diff --git a/lnwire/revoke_and_ack.go b/lnwire/revoke_and_ack.go index 87681e42..0f189e11 100644 --- a/lnwire/revoke_and_ack.go +++ b/lnwire/revoke_and_ack.go @@ -78,12 +78,12 @@ func (c *RevokeAndAck) Encode(w io.Writer, pver uint32) error { ) } -// Command returns the integer uniquely identifying this message type on the +// MsgType returns the integer uniquely identifying this message type on the // wire. // // This is part of the lnwire.Message interface. -func (c *RevokeAndAck) Command() uint32 { - return CmdRevokeAndAck +func (c *RevokeAndAck) MsgType() MessageType { + return MsgRevokeAndAck } // MaxPayloadLength returns the maximum allowed payload size for a diff --git a/lnwire/single_funding_complete.go b/lnwire/single_funding_complete.go index 1e4b90d6..93fd81e2 100644 --- a/lnwire/single_funding_complete.go +++ b/lnwire/single_funding_complete.go @@ -1,8 +1,6 @@ package lnwire import ( - "bytes" - "fmt" "io" "github.com/roasbeef/btcd/btcec" @@ -86,12 +84,12 @@ func (s *SingleFundingComplete) Encode(w io.Writer, pver uint32) error { s.StateHintObsfucator[:]) } -// Command returns the uint32 code which uniquely identifies this message as a +// MsgType returns the uint32 code which uniquely identifies this message as a // SingleFundingComplete on the wire. // // This is part of the lnwire.Message interface. -func (s *SingleFundingComplete) Command() uint32 { - return CmdSingleFundingComplete +func (s *SingleFundingComplete) MsgType() MessageType { + return MsgSingleFundingComplete } // MaxPayloadLength returns the maximum allowed payload length for a diff --git a/lnwire/single_funding_request.go b/lnwire/single_funding_request.go index 2966362e..82e54de9 100644 --- a/lnwire/single_funding_request.go +++ b/lnwire/single_funding_request.go @@ -1,7 +1,6 @@ package lnwire import ( - "fmt" "io" "github.com/roasbeef/btcd/btcec" @@ -152,8 +151,8 @@ func (c *SingleFundingRequest) Encode(w io.Writer, pver uint32) error { // SingleFundingRequest on the wire. // // This is part of the lnwire.Message interface. -func (c *SingleFundingRequest) Command() uint32 { - return CmdSingleFundingRequest +func (c *SingleFundingRequest) MsgType() MessageType { + return MsgSingleFundingRequest } // MaxPayloadLength returns the maximum allowed payload length for a diff --git a/lnwire/single_funding_response.go b/lnwire/single_funding_response.go index 6d2da774..0f91f835 100644 --- a/lnwire/single_funding_response.go +++ b/lnwire/single_funding_response.go @@ -1,7 +1,6 @@ package lnwire import ( - "fmt" "io" "github.com/roasbeef/btcd/btcec" @@ -114,12 +113,12 @@ func (c *SingleFundingResponse) Encode(w io.Writer, pver uint32) error { c.ConfirmationDepth) } -// Command returns the uint32 code which uniquely identifies this message as a +// MsgType returns the uint32 code which uniquely identifies this message as a // SingleFundingResponse on the wire. // // This is part of the lnwire.Message interface. -func (c *SingleFundingResponse) Command() uint32 { - return CmdSingleFundingResponse +func (c *SingleFundingResponse) MsgType() MessageType { + return MsgSingleFundingResponse } // MaxPayloadLength returns the maximum allowed payload length for a diff --git a/lnwire/single_funding_signcomplete.go b/lnwire/single_funding_signcomplete.go index 24645887..e99d23e3 100644 --- a/lnwire/single_funding_signcomplete.go +++ b/lnwire/single_funding_signcomplete.go @@ -1,7 +1,6 @@ package lnwire import ( - "fmt" "io" "github.com/roasbeef/btcd/btcec" @@ -54,12 +53,12 @@ func (c *SingleFundingSignComplete) Encode(w io.Writer, pver uint32) error { c.CommitSignature) } -// Command returns the uint32 code which uniquely identifies this message as a +// MsgType returns the uint32 code which uniquely identifies this message as a // SingleFundingSignComplete on the wire. // // This is part of the lnwire.Message interface. -func (c *SingleFundingSignComplete) Command() uint32 { - return CmdSingleFundingSignComplete +func (c *SingleFundingSignComplete) MsgType() MessageType { + return MsgSingleFundingSignComplete } // MaxPayloadLength returns the maximum allowed payload length for a diff --git a/lnwire/update_add_htlc.go b/lnwire/update_add_htlc.go index 44b11cac..fedbc2f3 100644 --- a/lnwire/update_add_htlc.go +++ b/lnwire/update_add_htlc.go @@ -1,7 +1,6 @@ package lnwire import ( - "fmt" "io" "github.com/roasbeef/btcutil" @@ -94,12 +93,12 @@ func (c *UpdateAddHTLC) Encode(w io.Writer, pver uint32) error { ) } -// Command returns the integer uniquely identifying this message type on the +// MsgType returns the integer uniquely identifying this message type on the // wire. // // This is part of the lnwire.Message interface. -func (c *UpdateAddHTLC) Command() uint32 { - return CmdUpdateAddHTLC +func (c *UpdateAddHTLC) MsgType() MessageType { + return MsgUpdateAddHTLC } // MaxPayloadLength returns the maximum allowed payload size for a UpdateAddHTLC diff --git a/lnwire/update_fail_htlc.go b/lnwire/update_fail_htlc.go index 5e0f8547..37a80bf4 100644 --- a/lnwire/update_fail_htlc.go +++ b/lnwire/update_fail_htlc.go @@ -120,12 +120,12 @@ func (c *UpdateFailHTLC) Encode(w io.Writer, pver uint32) error { ) } -// Command returns the integer uniquely identifying this message type on the +// MsgType returns the integer uniquely identifying this message type on the // wire. // // This is part of the lnwire.Message interface. -func (c *UpdateFailHTLC) Command() uint32 { - return CmdUpdateFailHTLC +func (c *UpdateFailHTLC) MsgType() MessageType { + return MsgUpdateFailHTLC } // MaxPayloadLength returns the maximum allowed payload size for a UpdateFailHTLC diff --git a/lnwire/update_fulfill_htlc.go b/lnwire/update_fulfill_htlc.go index e5ae0c89..30f5f882 100644 --- a/lnwire/update_fulfill_htlc.go +++ b/lnwire/update_fulfill_htlc.go @@ -60,12 +60,12 @@ func (c *UpdateFufillHTLC) Encode(w io.Writer, pver uint32) error { ) } -// Command returns the integer uniquely identifying this message type on the +// MsgType returns the integer uniquely identifying this message type on the // wire. // // This is part of the lnwire.Message interface. -func (c *UpdateFufillHTLC) Command() uint32 { - return CmdUpdateFufillHTLC +func (c *UpdateFufillHTLC) MsgType() MessageType { + return MsgUpdateFufillHTLC } // MaxPayloadLength returns the maximum allowed payload size for a UpdateFufillHTLC