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.
This commit is contained in:
Olaoluwa Osuntokun 2017-04-19 15:57:43 -07:00
parent bcd142bc30
commit f6b3c25f95
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
21 changed files with 119 additions and 151 deletions

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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