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. // Commands used in bitcoin message headers which describe the type of message.
const ( const (
cmdVersion = "version" CmdVersion = "version"
cmdVerAck = "verack" CmdVerAck = "verack"
cmdGetAddr = "getaddr" CmdGetAddr = "getaddr"
cmdAddr = "addr" CmdAddr = "addr"
cmdGetBlocks = "getblocks" CmdGetBlocks = "getblocks"
cmdInv = "inv" CmdInv = "inv"
cmdGetData = "getdata" CmdGetData = "getdata"
cmdNotFound = "notfound" CmdNotFound = "notfound"
cmdBlock = "block" CmdBlock = "block"
cmdTx = "tx" CmdTx = "tx"
cmdGetHeaders = "getheaders" CmdGetHeaders = "getheaders"
cmdHeaders = "headers" CmdHeaders = "headers"
cmdPing = "ping" CmdPing = "ping"
cmdPong = "pong" CmdPong = "pong"
cmdAlert = "alert" CmdAlert = "alert"
cmdMemPool = "mempool" CmdMemPool = "mempool"
cmdFilterAdd = "filteradd" CmdFilterAdd = "filteradd"
cmdFilterClear = "filterclear" CmdFilterClear = "filterclear"
cmdFilterLoad = "filterload" CmdFilterLoad = "filterload"
cmdMerkleBlock = "merkleblock" CmdMerkleBlock = "merkleblock"
cmdReject = "reject" CmdReject = "reject"
) )
// Message is an interface that describes a bitcoin message. A type that // 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) { func makeEmptyMessage(command string) (Message, error) {
var msg Message var msg Message
switch command { switch command {
case cmdVersion: case CmdVersion:
msg = &MsgVersion{} msg = &MsgVersion{}
case cmdVerAck: case CmdVerAck:
msg = &MsgVerAck{} msg = &MsgVerAck{}
case cmdGetAddr: case CmdGetAddr:
msg = &MsgGetAddr{} msg = &MsgGetAddr{}
case cmdAddr: case CmdAddr:
msg = &MsgAddr{} msg = &MsgAddr{}
case cmdGetBlocks: case CmdGetBlocks:
msg = &MsgGetBlocks{} msg = &MsgGetBlocks{}
case cmdBlock: case CmdBlock:
msg = &MsgBlock{} msg = &MsgBlock{}
case cmdInv: case CmdInv:
msg = &MsgInv{} msg = &MsgInv{}
case cmdGetData: case CmdGetData:
msg = &MsgGetData{} msg = &MsgGetData{}
case cmdNotFound: case CmdNotFound:
msg = &MsgNotFound{} msg = &MsgNotFound{}
case cmdTx: case CmdTx:
msg = &MsgTx{} msg = &MsgTx{}
case cmdPing: case CmdPing:
msg = &MsgPing{} msg = &MsgPing{}
case cmdPong: case CmdPong:
msg = &MsgPong{} msg = &MsgPong{}
case cmdGetHeaders: case CmdGetHeaders:
msg = &MsgGetHeaders{} msg = &MsgGetHeaders{}
case cmdHeaders: case CmdHeaders:
msg = &MsgHeaders{} msg = &MsgHeaders{}
case cmdAlert: case CmdAlert:
msg = &MsgAlert{} msg = &MsgAlert{}
case cmdMemPool: case CmdMemPool:
msg = &MsgMemPool{} msg = &MsgMemPool{}
case cmdFilterAdd: case CmdFilterAdd:
msg = &MsgFilterAdd{} msg = &MsgFilterAdd{}
case cmdFilterClear: case CmdFilterClear:
msg = &MsgFilterClear{} msg = &MsgFilterClear{}
case cmdFilterLoad: case CmdFilterLoad:
msg = &MsgFilterLoad{} msg = &MsgFilterLoad{}
case cmdMerkleBlock: case CmdMerkleBlock:
msg = &MsgMerkleBlock{} msg = &MsgMerkleBlock{}
case cmdReject: case CmdReject:
msg = &MsgReject{} msg = &MsgReject{}
default: 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 // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.
func (msg *MsgAddr) Command() string { func (msg *MsgAddr) Command() string {
return cmdAddr return CmdAddr
} }
// MaxPayloadLength returns the maximum length the payload can be for the // 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 // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.
func (msg *MsgAlert) Command() string { func (msg *MsgAlert) Command() string {
return cmdAlert return CmdAlert
} }
// MaxPayloadLength returns the maximum length the payload can be for the // 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 // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.
func (msg *MsgBlock) Command() string { func (msg *MsgBlock) Command() string {
return cmdBlock return CmdBlock
} }
// MaxPayloadLength returns the maximum length the payload can be for the // 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 // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.
func (msg *MsgFilterAdd) Command() string { func (msg *MsgFilterAdd) Command() string {
return cmdFilterAdd return CmdFilterAdd
} }
// MaxPayloadLength returns the maximum length the payload can be for the // 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 // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.
func (msg *MsgFilterClear) Command() string { func (msg *MsgFilterClear) Command() string {
return cmdFilterClear return CmdFilterClear
} }
// MaxPayloadLength returns the maximum length the payload can be for the // 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 // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.
func (msg *MsgFilterLoad) Command() string { func (msg *MsgFilterLoad) Command() string {
return cmdFilterLoad return CmdFilterLoad
} }
// MaxPayloadLength returns the maximum length the payload can be for the // 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 // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.
func (msg *MsgGetAddr) Command() string { func (msg *MsgGetAddr) Command() string {
return cmdGetAddr return CmdGetAddr
} }
// MaxPayloadLength returns the maximum length the payload can be for the // 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 // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.
func (msg *MsgGetBlocks) Command() string { func (msg *MsgGetBlocks) Command() string {
return cmdGetBlocks return CmdGetBlocks
} }
// MaxPayloadLength returns the maximum length the payload can be for the // 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 // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.
func (msg *MsgGetData) Command() string { func (msg *MsgGetData) Command() string {
return cmdGetData return CmdGetData
} }
// MaxPayloadLength returns the maximum length the payload can be for the // 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 // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.
func (msg *MsgGetHeaders) Command() string { func (msg *MsgGetHeaders) Command() string {
return cmdGetHeaders return CmdGetHeaders
} }
// MaxPayloadLength returns the maximum length the payload can be for the // 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 // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.
func (msg *MsgHeaders) Command() string { func (msg *MsgHeaders) Command() string {
return cmdHeaders return CmdHeaders
} }
// MaxPayloadLength returns the maximum length the payload can be for the // 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 // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.
func (msg *MsgInv) Command() string { func (msg *MsgInv) Command() string {
return cmdInv return CmdInv
} }
// MaxPayloadLength returns the maximum length the payload can be for the // 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 // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.
func (msg *MsgMemPool) Command() string { func (msg *MsgMemPool) Command() string {
return cmdMemPool return CmdMemPool
} }
// MaxPayloadLength returns the maximum length the payload can be for the // 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 // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.
func (msg *MsgMerkleBlock) Command() string { func (msg *MsgMerkleBlock) Command() string {
return cmdMerkleBlock return CmdMerkleBlock
} }
// MaxPayloadLength returns the maximum length the payload can be for the // 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 // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.
func (msg *MsgNotFound) Command() string { func (msg *MsgNotFound) Command() string {
return cmdNotFound return CmdNotFound
} }
// MaxPayloadLength returns the maximum length the payload can be for the // 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 // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.
func (msg *MsgPing) Command() string { func (msg *MsgPing) Command() string {
return cmdPing return CmdPing
} }
// MaxPayloadLength returns the maximum length the payload can be for the // 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 // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.
func (msg *MsgPong) Command() string { func (msg *MsgPong) Command() string {
return cmdPong return CmdPong
} }
// MaxPayloadLength returns the maximum length the payload can be for the // 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. // This message was not added until protocol version RejectVersion.
type MsgReject struct { type MsgReject struct {
// Cmd is the command for the message which was rejected such as // 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. // of a Message.
Cmd string Cmd string
@ -99,9 +99,9 @@ func (msg *MsgReject) BtcDecode(r io.Reader, pver uint32) error {
} }
msg.Reason = reason 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. // 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) err := readElement(r, &msg.Hash)
if err != nil { if err != nil {
return err return err
@ -139,9 +139,9 @@ func (msg *MsgReject) BtcEncode(w io.Writer, pver uint32) error {
return err 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. // 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) err := writeElement(w, &msg.Hash)
if err != nil { if err != nil {
return err 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 // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.
func (msg *MsgReject) Command() string { func (msg *MsgReject) Command() string {
return cmdReject return CmdReject
} }
// MaxPayloadLength returns the maximum length the payload can be for the // 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 // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.
func (msg *MsgTx) Command() string { func (msg *MsgTx) Command() string {
return cmdTx return CmdTx
} }
// MaxPayloadLength returns the maximum length the payload can be for the // 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 // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.
func (msg *MsgVerAck) Command() string { func (msg *MsgVerAck) Command() string {
return cmdVerAck return CmdVerAck
} }
// MaxPayloadLength returns the maximum length the payload can be for the // 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 // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.
func (msg *MsgVersion) Command() string { func (msg *MsgVersion) Command() string {
return cmdVersion return CmdVersion
} }
// MaxPayloadLength returns the maximum length the payload can be for the // MaxPayloadLength returns the maximum length the payload can be for the