neoneo-go/pkg/network/message.go

273 lines
5.9 KiB
Go
Raw Normal View History

2018-01-26 18:04:13 +00:00
package network
import (
"encoding/binary"
"errors"
2018-01-27 15:00:28 +00:00
"github.com/CityOfZion/neo-go/config"
2018-02-01 18:54:23 +00:00
"github.com/CityOfZion/neo-go/pkg/core"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
"github.com/CityOfZion/neo-go/pkg/io"
2018-02-01 18:54:23 +00:00
"github.com/CityOfZion/neo-go/pkg/network/payload"
2018-01-26 18:04:13 +00:00
)
2018-01-26 20:39:34 +00:00
const (
// The minimum size of a valid message.
minMessageSize = 24
2018-01-28 15:06:41 +00:00
cmdSize = 12
2018-01-26 20:39:34 +00:00
)
var (
errChecksumMismatch = errors.New("checksum mismatch")
)
2018-01-26 18:04:13 +00:00
// Message is the complete message send between nodes.
type Message struct {
// NetMode of the node that sends this message.
Magic config.NetMode
2018-01-26 18:04:13 +00:00
// Command is utf8 code, of which the length is 12 bytes,
// the extra part is filled with 0.
2018-01-28 15:06:41 +00:00
Command [cmdSize]byte
2018-01-26 18:04:13 +00:00
// Length of the payload
Length uint32
2018-01-26 18:04:13 +00:00
// Checksum is the first 4 bytes of the value that two times SHA256
// hash of the payload
Checksum uint32
2018-01-26 18:04:13 +00:00
// Payload send with the message.
2018-01-28 15:06:41 +00:00
Payload payload.Payload
2018-01-26 18:04:13 +00:00
}
// CommandType represents the type of a message command.
type CommandType string
2018-01-26 18:04:13 +00:00
// Valid protocol commands used to send between nodes.
2018-01-26 18:04:13 +00:00
const (
CMDAddr CommandType = "addr"
CMDBlock CommandType = "block"
CMDConsensus CommandType = "consensus"
CMDFilterAdd CommandType = "filteradd"
CMDFilterClear CommandType = "filterclear"
CMDFilterLoad CommandType = "filterload"
CMDGetAddr CommandType = "getaddr"
CMDGetBlocks CommandType = "getblocks"
CMDGetData CommandType = "getdata"
CMDGetHeaders CommandType = "getheaders"
CMDHeaders CommandType = "headers"
CMDInv CommandType = "inv"
CMDMempool CommandType = "mempool"
CMDMerkleBlock CommandType = "merkleblock"
CMDPing CommandType = "ping"
CMDPong CommandType = "pong"
CMDTX CommandType = "tx"
CMDUnknown CommandType = "unknown"
CMDVerack CommandType = "verack"
CMDVersion CommandType = "version"
2018-01-26 18:04:13 +00:00
)
// NewMessage returns a new message with the given payload.
func NewMessage(magic config.NetMode, cmd CommandType, p payload.Payload) *Message {
2018-01-28 07:03:18 +00:00
var (
size uint32
checksum []byte
)
2018-01-27 15:00:28 +00:00
if p != nil {
buf := io.NewBufBinWriter()
if err := p.EncodeBinary(buf.BinWriter); err != nil {
2018-01-28 15:06:41 +00:00
panic(err)
}
b := buf.Bytes()
size = uint32(len(b))
checksum = hash.Checksum(b)
2018-01-28 07:03:18 +00:00
} else {
checksum = hash.Checksum([]byte{})
2018-01-27 15:00:28 +00:00
}
2018-01-28 07:03:18 +00:00
2018-01-26 18:04:13 +00:00
return &Message{
2018-01-28 07:03:18 +00:00
Magic: magic,
2018-01-28 15:06:41 +00:00
Command: cmdToByteArray(cmd),
2018-01-28 07:03:18 +00:00
Length: size,
Payload: p,
Checksum: binary.LittleEndian.Uint32(checksum[:4]),
2018-01-27 15:00:28 +00:00
}
}
// CommandType converts the 12 byte command slice to a CommandType.
func (m *Message) CommandType() CommandType {
2018-01-28 15:06:41 +00:00
cmd := cmdByteArrayToString(m.Command)
2018-01-26 18:04:13 +00:00
switch cmd {
case "addr":
return CMDAddr
2018-01-26 18:04:13 +00:00
case "block":
return CMDBlock
2018-01-30 10:56:36 +00:00
case "consensus":
return CMDConsensus
case "filteradd":
return CMDFilterAdd
case "filterclear":
return CMDFilterClear
case "filterload":
return CMDFilterLoad
case "getaddr":
return CMDGetAddr
case "getblocks":
return CMDGetBlocks
case "getdata":
return CMDGetData
case "getheaders":
return CMDGetHeaders
case "headers":
return CMDHeaders
case "inv":
return CMDInv
case "mempool":
return CMDMempool
case "merkleblock":
return CMDMerkleBlock
case "ping":
return CMDPing
case "pong":
return CMDPong
case "tx":
return CMDTX
case "verack":
return CMDVerack
case "version":
return CMDVersion
2018-01-26 18:04:13 +00:00
default:
return CMDUnknown
2018-01-26 18:04:13 +00:00
}
}
// Decode a Message from the given reader.
func (m *Message) Decode(br *io.BinReader) error {
br.ReadLE(&m.Magic)
br.ReadLE(&m.Command)
br.ReadLE(&m.Length)
br.ReadLE(&m.Checksum)
if br.Err != nil {
return br.Err
}
2018-01-28 07:03:18 +00:00
// return if their is no payload.
if m.Length == 0 {
2018-01-27 15:00:28 +00:00
return nil
}
return m.decodePayload(br)
2018-01-27 15:47:43 +00:00
}
func (m *Message) decodePayload(br *io.BinReader) error {
buf := make([]byte, m.Length)
br.ReadLE(buf)
if br.Err != nil {
return br.Err
2018-01-28 10:12:05 +00:00
}
2018-01-28 07:03:18 +00:00
// Compare the checksum of the payload.
if !compareChecksum(m.Checksum, buf) {
return errChecksumMismatch
2018-01-28 07:03:18 +00:00
}
2018-01-27 15:00:28 +00:00
r := io.NewBinReaderFromBuf(buf)
2018-01-28 15:06:41 +00:00
var p payload.Payload
switch m.CommandType() {
case CMDVersion:
2018-01-27 15:00:28 +00:00
p = &payload.Version{}
if err := p.DecodeBinary(r); err != nil {
2018-01-28 17:42:22 +00:00
return err
}
case CMDInv, CMDGetData:
2018-01-28 17:42:22 +00:00
p = &payload.Inventory{}
if err := p.DecodeBinary(r); err != nil {
2018-01-28 10:12:05 +00:00
return err
}
case CMDAddr:
2018-01-28 15:18:48 +00:00
p = &payload.AddressList{}
if err := p.DecodeBinary(r); err != nil {
2018-01-28 15:18:48 +00:00
return err
}
case CMDBlock:
p = &core.Block{}
if err := p.DecodeBinary(r); err != nil {
return err
}
case CMDGetBlocks:
fallthrough
case CMDGetHeaders:
p = &payload.GetBlocks{}
if err := p.DecodeBinary(r); err != nil {
return err
}
case CMDHeaders:
p = &payload.Headers{}
if err := p.DecodeBinary(r); err != nil {
return err
}
case CMDTX:
p = &transaction.Transaction{}
if err := p.DecodeBinary(r); err != nil {
return err
}
case CMDMerkleBlock:
p = &payload.MerkleBlock{}
if err := p.DecodeBinary(r); err != nil {
return err
}
2018-01-26 18:04:13 +00:00
}
2018-01-27 15:00:28 +00:00
m.Payload = p
2018-01-26 18:04:13 +00:00
return nil
}
// Encode a Message to any given BinWriter.
func (m *Message) Encode(br *io.BinWriter) error {
br.WriteLE(m.Magic)
br.WriteLE(m.Command)
br.WriteLE(m.Length)
br.WriteLE(m.Checksum)
if br.Err != nil {
return br.Err
}
2018-01-28 07:03:18 +00:00
if m.Payload != nil {
return m.Payload.EncodeBinary(br)
2018-01-28 10:12:05 +00:00
}
2018-01-27 15:00:28 +00:00
return nil
2018-01-26 18:04:13 +00:00
}
// convert a command (string) to a byte slice filled with 0 bytes till
// size 12.
func cmdToByteArray(cmd CommandType) [cmdSize]byte {
2018-01-26 18:04:13 +00:00
cmdLen := len(cmd)
2018-01-28 15:06:41 +00:00
if cmdLen > cmdSize {
2018-01-26 18:04:13 +00:00
panic("exceeded command max length of size 12")
}
// The command can have max 12 bytes, rest is filled with 0.
2018-01-28 15:06:41 +00:00
b := [cmdSize]byte{}
for i := 0; i < cmdLen; i++ {
b[i] = cmd[i]
2018-01-26 18:04:13 +00:00
}
return b
}
2018-01-28 15:06:41 +00:00
func cmdByteArrayToString(cmd [cmdSize]byte) string {
buf := make([]byte, 0, cmdSize)
2018-01-28 15:06:41 +00:00
for i := 0; i < cmdSize; i++ {
if cmd[i] != 0 {
buf = append(buf, cmd[i])
}
}
return string(buf)
}
2018-01-26 18:04:13 +00:00
func compareChecksum(have uint32, b []byte) bool {
sum := hash.Checksum(b)
2018-01-26 18:04:13 +00:00
want := binary.LittleEndian.Uint32(sum)
return have == want
}