You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
1.9 KiB
80 lines
1.9 KiB
7 years ago
|
package common
|
||
7 years ago
|
|
||
|
import (
|
||
|
"errors"
|
||
|
)
|
||
|
|
||
|
/*
|
||
|
|
||
|
Node will process the content of the p2p message
|
||
|
|
||
|
---- content start -----
|
||
|
1 byte - message category
|
||
7 years ago
|
0x00: COMMITTEE
|
||
|
0x01: NODE...
|
||
|
1 byte - message type
|
||
|
- for COMMITTEE category
|
||
7 years ago
|
0x00: consensus
|
||
7 years ago
|
0x01: sharding ...
|
||
|
- for NODE category
|
||
|
0x00: transaction ...
|
||
7 years ago
|
n - 2 bytes - actual message payload
|
||
|
---- content end -----
|
||
|
|
||
|
*/
|
||
|
|
||
|
const NODE_TYPE_BYTES = 1
|
||
|
const ACTION_TYPE_BYTES = 1
|
||
|
|
||
7 years ago
|
// The CATEGORY of messages
|
||
7 years ago
|
type MessageCategory byte
|
||
7 years ago
|
|
||
7 years ago
|
const (
|
||
|
COMMITTEE MessageCategory = iota
|
||
|
NODE
|
||
|
// TODO: add more types
|
||
|
)
|
||
|
|
||
7 years ago
|
|
||
|
// The specific types of message under COMMITTEE category
|
||
7 years ago
|
type CommitteeMessageType byte
|
||
7 years ago
|
|
||
7 years ago
|
const (
|
||
|
CONSENSUS CommitteeMessageType = iota
|
||
|
// TODO: add more types
|
||
|
)
|
||
|
|
||
7 years ago
|
// The specific types of message under NODE category
|
||
7 years ago
|
type NodeMessageType byte
|
||
7 years ago
|
|
||
7 years ago
|
const (
|
||
|
TRANSACTION NodeMessageType = iota
|
||
7 years ago
|
CONTROL
|
||
7 years ago
|
// TODO: add more types
|
||
|
)
|
||
|
|
||
7 years ago
|
|
||
7 years ago
|
// Get the message category from the p2p message content
|
||
|
func GetMessageCategory(message []byte) (MessageCategory, error) {
|
||
|
if len(message) < NODE_TYPE_BYTES {
|
||
|
return 0, errors.New("Failed to get node type: no data available.")
|
||
|
}
|
||
|
return MessageCategory(message[NODE_TYPE_BYTES-1]), nil
|
||
|
}
|
||
|
|
||
|
// Get the action type from the p2p message content
|
||
|
func GetMessageType(message []byte) (byte, error) {
|
||
7 years ago
|
if len(message) < NODE_TYPE_BYTES+ACTION_TYPE_BYTES {
|
||
7 years ago
|
return 0, errors.New("Failed to get action type: no data available.")
|
||
|
}
|
||
|
return byte(message[NODE_TYPE_BYTES+ACTION_TYPE_BYTES-1]), nil
|
||
|
}
|
||
|
|
||
|
// Get the node message payload from the p2p message content
|
||
|
func GetMessagePayload(message []byte) ([]byte, error) {
|
||
|
if len(message) < NODE_TYPE_BYTES+ACTION_TYPE_BYTES {
|
||
|
return []byte{}, errors.New("Failed to get message payload: no data available.")
|
||
|
}
|
||
|
return message[NODE_TYPE_BYTES+ACTION_TYPE_BYTES:], nil
|
||
|
}
|