Export command constants.

Although it is possible to get the command name for each msg type by
creating an instances of the type and calling the Command method against
it, it's slightly more efficient to simply allows callers to have direct
access to the exported constants.

This is currently really useful for the reject message since callers need
to be able to examine the command type to determine whether or not the
hash field needs to be included.
This commit is contained in:
Dave Collins 2014-07-14 10:38:29 -05:00
parent 7ee3a2220b
commit 4deb922c9d
22 changed files with 68 additions and 68 deletions

View file

@ -26,27 +26,27 @@ const MaxMessagePayload = (1024 * 1024 * 32) // 32MB
// Commands used in bitcoin message headers which describe the type of message.
const (
cmdVersion = "version"
cmdVerAck = "verack"
cmdGetAddr = "getaddr"
cmdAddr = "addr"
cmdGetBlocks = "getblocks"
cmdInv = "inv"
cmdGetData = "getdata"
cmdNotFound = "notfound"
cmdBlock = "block"
cmdTx = "tx"
cmdGetHeaders = "getheaders"
cmdHeaders = "headers"
cmdPing = "ping"
cmdPong = "pong"
cmdAlert = "alert"
cmdMemPool = "mempool"
cmdFilterAdd = "filteradd"
cmdFilterClear = "filterclear"
cmdFilterLoad = "filterload"
cmdMerkleBlock = "merkleblock"
cmdReject = "reject"
CmdVersion = "version"
CmdVerAck = "verack"
CmdGetAddr = "getaddr"
CmdAddr = "addr"
CmdGetBlocks = "getblocks"
CmdInv = "inv"
CmdGetData = "getdata"
CmdNotFound = "notfound"
CmdBlock = "block"
CmdTx = "tx"
CmdGetHeaders = "getheaders"
CmdHeaders = "headers"
CmdPing = "ping"
CmdPong = "pong"
CmdAlert = "alert"
CmdMemPool = "mempool"
CmdFilterAdd = "filteradd"
CmdFilterClear = "filterclear"
CmdFilterLoad = "filterload"
CmdMerkleBlock = "merkleblock"
CmdReject = "reject"
)
// Message is an interface that describes a bitcoin message. A type that
@ -65,67 +65,67 @@ type Message interface {
func makeEmptyMessage(command string) (Message, error) {
var msg Message
switch command {
case cmdVersion:
case CmdVersion:
msg = &MsgVersion{}
case cmdVerAck:
case CmdVerAck:
msg = &MsgVerAck{}
case cmdGetAddr:
case CmdGetAddr:
msg = &MsgGetAddr{}
case cmdAddr:
case CmdAddr:
msg = &MsgAddr{}
case cmdGetBlocks:
case CmdGetBlocks:
msg = &MsgGetBlocks{}
case cmdBlock:
case CmdBlock:
msg = &MsgBlock{}
case cmdInv:
case CmdInv:
msg = &MsgInv{}
case cmdGetData:
case CmdGetData:
msg = &MsgGetData{}
case cmdNotFound:
case CmdNotFound:
msg = &MsgNotFound{}
case cmdTx:
case CmdTx:
msg = &MsgTx{}
case cmdPing:
case CmdPing:
msg = &MsgPing{}
case cmdPong:
case CmdPong:
msg = &MsgPong{}
case cmdGetHeaders:
case CmdGetHeaders:
msg = &MsgGetHeaders{}
case cmdHeaders:
case CmdHeaders:
msg = &MsgHeaders{}
case cmdAlert:
case CmdAlert:
msg = &MsgAlert{}
case cmdMemPool:
case CmdMemPool:
msg = &MsgMemPool{}
case cmdFilterAdd:
case CmdFilterAdd:
msg = &MsgFilterAdd{}
case cmdFilterClear:
case CmdFilterClear:
msg = &MsgFilterClear{}
case cmdFilterLoad:
case CmdFilterLoad:
msg = &MsgFilterLoad{}
case cmdMerkleBlock:
case CmdMerkleBlock:
msg = &MsgMerkleBlock{}
case cmdReject:
case CmdReject:
msg = &MsgReject{}
default:

View file

@ -118,7 +118,7 @@ func (msg *MsgAddr) BtcEncode(w io.Writer, pver uint32) error {
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgAddr) Command() string {
return cmdAddr
return CmdAddr
}
// MaxPayloadLength returns the maximum length the payload can be for the

View file

@ -400,7 +400,7 @@ func (msg *MsgAlert) BtcEncode(w io.Writer, pver uint32) error {
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgAlert) Command() string {
return cmdAlert
return CmdAlert
}
// MaxPayloadLength returns the maximum length the payload can be for the

View file

@ -211,7 +211,7 @@ func (msg *MsgBlock) SerializeSize() int {
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgBlock) Command() string {
return cmdBlock
return CmdBlock
}
// MaxPayloadLength returns the maximum length the payload can be for the

View file

@ -71,7 +71,7 @@ func (msg *MsgFilterAdd) BtcEncode(w io.Writer, pver uint32) error {
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgFilterAdd) Command() string {
return cmdFilterAdd
return CmdFilterAdd
}
// MaxPayloadLength returns the maximum length the payload can be for the

View file

@ -43,7 +43,7 @@ func (msg *MsgFilterClear) BtcEncode(w io.Writer, pver uint32) error {
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgFilterClear) Command() string {
return cmdFilterClear
return CmdFilterClear
}
// MaxPayloadLength returns the maximum length the payload can be for the

View file

@ -117,7 +117,7 @@ func (msg *MsgFilterLoad) BtcEncode(w io.Writer, pver uint32) error {
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgFilterLoad) Command() string {
return cmdFilterLoad
return CmdFilterLoad
}
// MaxPayloadLength returns the maximum length the payload can be for the

View file

@ -31,7 +31,7 @@ func (msg *MsgGetAddr) BtcEncode(w io.Writer, pver uint32) error {
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgGetAddr) Command() string {
return cmdGetAddr
return CmdGetAddr
}
// MaxPayloadLength returns the maximum length the payload can be for the

View file

@ -121,7 +121,7 @@ func (msg *MsgGetBlocks) BtcEncode(w io.Writer, pver uint32) error {
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgGetBlocks) Command() string {
return cmdGetBlocks
return CmdGetBlocks
}
// MaxPayloadLength returns the maximum length the payload can be for the

View file

@ -90,7 +90,7 @@ func (msg *MsgGetData) BtcEncode(w io.Writer, pver uint32) error {
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgGetData) Command() string {
return cmdGetData
return CmdGetData
}
// MaxPayloadLength returns the maximum length the payload can be for the

View file

@ -119,7 +119,7 @@ func (msg *MsgGetHeaders) BtcEncode(w io.Writer, pver uint32) error {
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgGetHeaders) Command() string {
return cmdGetHeaders
return CmdGetHeaders
}
// MaxPayloadLength returns the maximum length the payload can be for the

View file

@ -113,7 +113,7 @@ func (msg *MsgHeaders) BtcEncode(w io.Writer, pver uint32) error {
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgHeaders) Command() string {
return cmdHeaders
return CmdHeaders
}
// MaxPayloadLength returns the maximum length the payload can be for the

View file

@ -98,7 +98,7 @@ func (msg *MsgInv) BtcEncode(w io.Writer, pver uint32) error {
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgInv) Command() string {
return cmdInv
return CmdInv
}
// MaxPayloadLength returns the maximum length the payload can be for the

View file

@ -44,7 +44,7 @@ func (msg *MsgMemPool) BtcEncode(w io.Writer, pver uint32) error {
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgMemPool) Command() string {
return cmdMemPool
return CmdMemPool
}
// MaxPayloadLength returns the maximum length the payload can be for the

View file

@ -146,7 +146,7 @@ func (msg *MsgMerkleBlock) BtcEncode(w io.Writer, pver uint32) error {
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgMerkleBlock) Command() string {
return cmdMerkleBlock
return CmdMerkleBlock
}
// MaxPayloadLength returns the maximum length the payload can be for the

View file

@ -87,7 +87,7 @@ func (msg *MsgNotFound) BtcEncode(w io.Writer, pver uint32) error {
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgNotFound) Command() string {
return cmdNotFound
return CmdNotFound
}
// MaxPayloadLength returns the maximum length the payload can be for the

View file

@ -60,7 +60,7 @@ func (msg *MsgPing) BtcEncode(w io.Writer, pver uint32) error {
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgPing) Command() string {
return cmdPing
return CmdPing
}
// MaxPayloadLength returns the maximum length the payload can be for the

View file

@ -61,7 +61,7 @@ func (msg *MsgPong) BtcEncode(w io.Writer, pver uint32) error {
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgPong) Command() string {
return cmdPong
return CmdPong
}
// MaxPayloadLength returns the maximum length the payload can be for the

View file

@ -52,7 +52,7 @@ func (code RejectCode) String() string {
// This message was not added until protocol version RejectVersion.
type MsgReject struct {
// Cmd is the command for the message which was rejected such as
// as cmdBlock or cmdTx. This can be obtained from the Command function
// as CmdBlock or CmdTx. This can be obtained from the Command function
// of a Message.
Cmd string
@ -99,9 +99,9 @@ func (msg *MsgReject) BtcDecode(r io.Reader, pver uint32) error {
}
msg.Reason = reason
// cmdBlock and cmdTx messages have an additional hash field that
// CmdBlock and CmdTx messages have an additional hash field that
// identifies the specific block or transaction.
if msg.Cmd == cmdBlock || msg.Cmd == cmdTx {
if msg.Cmd == CmdBlock || msg.Cmd == CmdTx {
err := readElement(r, &msg.Hash)
if err != nil {
return err
@ -139,9 +139,9 @@ func (msg *MsgReject) BtcEncode(w io.Writer, pver uint32) error {
return err
}
// cmdBlock and cmdTx messages have an additional hash field that
// CmdBlock and CmdTx messages have an additional hash field that
// identifies the specific block or transaction.
if msg.Cmd == cmdBlock || msg.Cmd == cmdTx {
if msg.Cmd == CmdBlock || msg.Cmd == CmdTx {
err := writeElement(w, &msg.Hash)
if err != nil {
return err
@ -154,7 +154,7 @@ func (msg *MsgReject) BtcEncode(w io.Writer, pver uint32) error {
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgReject) Command() string {
return cmdReject
return CmdReject
}
// MaxPayloadLength returns the maximum length the payload can be for the

View file

@ -405,7 +405,7 @@ func (msg *MsgTx) SerializeSize() int {
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgTx) Command() string {
return cmdTx
return CmdTx
}
// MaxPayloadLength returns the maximum length the payload can be for the

View file

@ -30,7 +30,7 @@ func (msg *MsgVerAck) BtcEncode(w io.Writer, pver uint32) error {
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgVerAck) Command() string {
return cmdVerAck
return CmdVerAck
}
// MaxPayloadLength returns the maximum length the payload can be for the

View file

@ -206,7 +206,7 @@ func (msg *MsgVersion) BtcEncode(w io.Writer, pver uint32) error {
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgVersion) Command() string {
return cmdVersion
return CmdVersion
}
// MaxPayloadLength returns the maximum length the payload can be for the