Correct const names for message category and type size

pull/50/head
Rongjian Lan 6 years ago
parent 6a408a2e97
commit a54d4487b7
  1. 31
      common/message.go

@ -24,18 +24,13 @@ n - 2 bytes - actual message payload
*/ */
// NODE_TYPE_BYTES is # of bytes message category // MESSAGE_CATEGORY_BYTES is the number of bytes message category takes
const NODE_TYPE_BYTES = 1 const MESSAGE_CATEGORY_BYTES = 1
// ACTION_TYPE_BYTES is # of bytes for message type which can be // MESSAGE_TYPE_BYTES is the number of bytes message type takes
// - for CONSENSUS category const MESSAGE_TYPE_BYTES = 1
// 0x00: consensus
// 0x01: sharding ...
// - for NODE category
// 0x00: transaction ...
const ACTION_TYPE_BYTES = 1
// The CATEGORY of messages // The message category enum
type MessageCategory byte type MessageCategory byte
const ( const (
@ -47,24 +42,24 @@ const (
// Get the message category from the p2p message content // Get the message category from the p2p message content
func GetMessageCategory(message []byte) (MessageCategory, error) { func GetMessageCategory(message []byte) (MessageCategory, error) {
if len(message) < NODE_TYPE_BYTES { if len(message) < MESSAGE_CATEGORY_BYTES {
return 0, errors.New("Failed to get node type: no data available.") return 0, errors.New("Failed to get message category: no data available.")
} }
return MessageCategory(message[NODE_TYPE_BYTES-1]), nil return MessageCategory(message[MESSAGE_CATEGORY_BYTES-1]), nil
} }
// Get the message type from the p2p message content // Get the message type from the p2p message content
func GetMessageType(message []byte) (byte, error) { func GetMessageType(message []byte) (byte, error) {
if len(message) < NODE_TYPE_BYTES+ACTION_TYPE_BYTES { if len(message) < MESSAGE_CATEGORY_BYTES+MESSAGE_TYPE_BYTES {
return 0, errors.New("Failed to get action type: no data available.") return 0, errors.New("Failed to get message type: no data available.")
} }
return byte(message[NODE_TYPE_BYTES+ACTION_TYPE_BYTES-1]), nil return byte(message[MESSAGE_CATEGORY_BYTES+MESSAGE_TYPE_BYTES-1]), nil
} }
// Get the node message payload from the p2p message content // Get the node message payload from the p2p message content
func GetMessagePayload(message []byte) ([]byte, error) { func GetMessagePayload(message []byte) ([]byte, error) {
if len(message) < NODE_TYPE_BYTES+ACTION_TYPE_BYTES { if len(message) < MESSAGE_CATEGORY_BYTES+MESSAGE_TYPE_BYTES {
return []byte{}, errors.New("Failed to get message payload: no data available.") return []byte{}, errors.New("Failed to get message payload: no data available.")
} }
return message[NODE_TYPE_BYTES+ACTION_TYPE_BYTES:], nil return message[MESSAGE_CATEGORY_BYTES+MESSAGE_TYPE_BYTES:], nil
} }

Loading…
Cancel
Save