2018-01-26 18:04:13 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2018-01-28 10:12:05 +00:00
|
|
|
"fmt"
|
2018-01-26 18:04:13 +00:00
|
|
|
"io"
|
2018-01-27 15:00:28 +00:00
|
|
|
|
2018-03-25 10:45:54 +00:00
|
|
|
"github.com/CityOfZion/neo-go/config"
|
2018-02-01 18:54:23 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core"
|
2018-03-10 12:04:06 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
2019-08-23 15:50:45 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
2018-02-01 18:54:23 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/network/payload"
|
2019-08-28 12:43:56 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
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
|
|
|
)
|
|
|
|
|
2018-01-29 07:42:49 +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 {
|
2018-03-14 09:36:59 +00:00
|
|
|
// NetMode of the node that sends this message.
|
2018-03-25 10:45:54 +00:00
|
|
|
Magic config.NetMode
|
2018-03-14 09:36:59 +00:00
|
|
|
|
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-03-14 09:36:59 +00:00
|
|
|
|
2018-01-26 18:04:13 +00:00
|
|
|
// Length of the payload
|
|
|
|
Length uint32
|
2018-03-14 09:36:59 +00:00
|
|
|
|
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-03-14 09:36:59 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-03-09 15:55:25 +00:00
|
|
|
// CommandType represents the type of a message command.
|
|
|
|
type CommandType string
|
2018-01-26 18:04:13 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
// Valid protocol commands used to send between nodes.
|
2018-01-26 18:04:13 +00:00
|
|
|
const (
|
2018-08-10 14:32:49 +00:00
|
|
|
CMDAddr CommandType = "addr"
|
|
|
|
CMDBlock CommandType = "block"
|
|
|
|
CMDConsensus CommandType = "consensus"
|
|
|
|
CMDFilterAdd CommandType = "filteradd"
|
|
|
|
CMDFilterClear CommandType = "filterclear"
|
|
|
|
CMDFilterLoad CommandType = "filterload"
|
2019-08-27 17:12:01 +00:00
|
|
|
CMDGetAddr CommandType = "getaddr"
|
|
|
|
CMDGetBlocks CommandType = "getblocks"
|
|
|
|
CMDGetData CommandType = "getdata"
|
|
|
|
CMDGetHeaders CommandType = "getheaders"
|
|
|
|
CMDHeaders CommandType = "headers"
|
|
|
|
CMDInv CommandType = "inv"
|
2019-08-27 17:08:48 +00:00
|
|
|
CMDMempool CommandType = "mempool"
|
2019-08-27 17:12:01 +00:00
|
|
|
CMDMerkleBlock CommandType = "merkleblock"
|
2019-08-27 17:08:48 +00:00
|
|
|
CMDPing CommandType = "ping"
|
|
|
|
CMDPong CommandType = "pong"
|
2019-08-27 17:12:01 +00:00
|
|
|
CMDTX CommandType = "tx"
|
|
|
|
CMDUnknown CommandType = "unknown"
|
|
|
|
CMDVerack CommandType = "verack"
|
|
|
|
CMDVersion CommandType = "version"
|
2018-01-26 18:04:13 +00:00
|
|
|
)
|
|
|
|
|
2018-03-09 15:55:25 +00:00
|
|
|
// NewMessage returns a new message with the given payload.
|
2018-03-25 10:45:54 +00:00
|
|
|
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 {
|
2018-01-28 15:06:41 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := p.EncodeBinary(buf); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-01-31 08:27:08 +00:00
|
|
|
size = uint32(buf.Len())
|
2019-08-23 15:50:45 +00:00
|
|
|
checksum = hash.Checksum(buf.Bytes())
|
2018-01-28 07:03:18 +00:00
|
|
|
} else {
|
2019-08-23 15:50:45 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 15:55:25 +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":
|
2018-03-09 15:55:25 +00:00
|
|
|
return CMDAddr
|
2018-01-26 18:04:13 +00:00
|
|
|
case "block":
|
2018-03-09 15:55:25 +00:00
|
|
|
return CMDBlock
|
2018-01-30 10:56:36 +00:00
|
|
|
case "consensus":
|
2018-03-09 15:55:25 +00:00
|
|
|
return CMDConsensus
|
2018-08-10 14:32:49 +00:00
|
|
|
case "filteradd":
|
|
|
|
return CMDFilterAdd
|
|
|
|
case "filterclear":
|
|
|
|
return CMDFilterClear
|
2019-08-27 17:12:01 +00:00
|
|
|
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
|
2019-08-27 17:08:48 +00:00
|
|
|
case "mempool":
|
|
|
|
return CMDMempool
|
2019-08-27 17:12:01 +00:00
|
|
|
case "merkleblock":
|
|
|
|
return CMDMerkleBlock
|
2019-08-27 17:08:48 +00:00
|
|
|
case "ping":
|
|
|
|
return CMDPing
|
|
|
|
case "pong":
|
|
|
|
return CMDPong
|
2019-08-27 17:12:01 +00:00
|
|
|
case "tx":
|
|
|
|
return CMDTX
|
|
|
|
case "verack":
|
|
|
|
return CMDVerack
|
|
|
|
case "version":
|
|
|
|
return CMDVersion
|
2018-01-26 18:04:13 +00:00
|
|
|
default:
|
2018-03-09 15:55:25 +00:00
|
|
|
return CMDUnknown
|
2018-01-26 18:04:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-09 16:58:09 +00:00
|
|
|
// Decode a Message from the given reader.
|
|
|
|
func (m *Message) Decode(r io.Reader) error {
|
2019-08-28 12:43:56 +00:00
|
|
|
br := util.BinReader{R: r}
|
|
|
|
br.ReadLE(&m.Magic)
|
|
|
|
br.ReadLE(&m.Command)
|
|
|
|
br.ReadLE(&m.Length)
|
|
|
|
br.ReadLE(&m.Checksum)
|
|
|
|
if br.Err != nil {
|
|
|
|
return br.Err
|
2018-03-03 07:16:05 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
2018-01-28 15:06:41 +00:00
|
|
|
return m.decodePayload(r)
|
2018-01-27 15:47:43 +00:00
|
|
|
}
|
|
|
|
|
2018-01-28 15:06:41 +00:00
|
|
|
func (m *Message) decodePayload(r io.Reader) error {
|
2018-02-04 19:54:51 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
n, err := io.CopyN(buf, r, int64(m.Length))
|
2018-01-28 10:12:05 +00:00
|
|
|
if err != nil {
|
2018-01-28 07:03:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-28 10:12:05 +00:00
|
|
|
if uint32(n) != m.Length {
|
|
|
|
return fmt.Errorf("expected to have read exactly %d bytes got %d", m.Length, n)
|
|
|
|
}
|
|
|
|
|
2018-01-28 07:03:18 +00:00
|
|
|
// Compare the checksum of the payload.
|
2018-02-04 19:54:51 +00:00
|
|
|
if !compareChecksum(m.Checksum, buf.Bytes()) {
|
2018-01-29 07:42:49 +00:00
|
|
|
return errChecksumMismatch
|
2018-01-28 07:03:18 +00:00
|
|
|
}
|
2018-01-27 15:00:28 +00:00
|
|
|
|
2018-01-28 15:06:41 +00:00
|
|
|
var p payload.Payload
|
2018-03-09 15:55:25 +00:00
|
|
|
switch m.CommandType() {
|
|
|
|
case CMDVersion:
|
2018-01-27 15:00:28 +00:00
|
|
|
p = &payload.Version{}
|
2018-03-14 09:36:59 +00:00
|
|
|
if err := p.DecodeBinary(buf); err != nil {
|
2018-01-28 17:42:22 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-03-09 15:55:25 +00:00
|
|
|
case CMDInv:
|
2018-01-28 17:42:22 +00:00
|
|
|
p = &payload.Inventory{}
|
2018-03-14 09:36:59 +00:00
|
|
|
if err := p.DecodeBinary(buf); err != nil {
|
2018-01-28 10:12:05 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-03-09 15:55:25 +00:00
|
|
|
case CMDAddr:
|
2018-01-28 15:18:48 +00:00
|
|
|
p = &payload.AddressList{}
|
2018-03-14 09:36:59 +00:00
|
|
|
if err := p.DecodeBinary(buf); err != nil {
|
2018-01-28 15:18:48 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-03-09 15:55:25 +00:00
|
|
|
case CMDBlock:
|
2018-01-31 08:27:08 +00:00
|
|
|
p = &core.Block{}
|
2018-03-14 09:36:59 +00:00
|
|
|
if err := p.DecodeBinary(buf); err != nil {
|
2018-01-31 08:27:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-03-09 15:55:25 +00:00
|
|
|
case CMDGetHeaders:
|
2018-02-04 19:54:51 +00:00
|
|
|
p = &payload.GetBlocks{}
|
2018-03-14 09:36:59 +00:00
|
|
|
if err := p.DecodeBinary(buf); err != nil {
|
2018-02-04 19:54:51 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-03-09 15:55:25 +00:00
|
|
|
case CMDHeaders:
|
2018-02-04 19:54:51 +00:00
|
|
|
p = &payload.Headers{}
|
2018-03-14 09:36:59 +00:00
|
|
|
if err := p.DecodeBinary(buf); err != nil {
|
2018-02-04 19:54:51 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-03-10 12:04:06 +00:00
|
|
|
case CMDTX:
|
|
|
|
p = &transaction.Transaction{}
|
2018-03-14 09:36:59 +00:00
|
|
|
if err := p.DecodeBinary(buf); err != nil {
|
2018-03-10 12:04:06 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-08-10 14:32:49 +00:00
|
|
|
case CMDMerkleBlock:
|
|
|
|
p = &payload.MerkleBlock{}
|
|
|
|
if err := p.DecodeBinary(buf); 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
|
|
|
|
}
|
|
|
|
|
2018-04-09 16:58:09 +00:00
|
|
|
// Encode a Message to any given io.Writer.
|
|
|
|
func (m *Message) Encode(w io.Writer) error {
|
2019-08-28 12:43:56 +00:00
|
|
|
br := util.BinWriter{W: w}
|
|
|
|
br.WriteLE(m.Magic)
|
|
|
|
br.WriteLE(m.Command)
|
|
|
|
br.WriteLE(m.Length)
|
|
|
|
br.WriteLE(m.Checksum)
|
|
|
|
if br.Err != nil {
|
|
|
|
return br.Err
|
2018-03-10 12:04:06 +00:00
|
|
|
}
|
2018-01-28 07:03:18 +00:00
|
|
|
if m.Payload != nil {
|
2018-01-28 15:06:41 +00:00
|
|
|
return m.Payload.EncodeBinary(w)
|
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.
|
2018-03-09 15:55:25 +00:00
|
|
|
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 {
|
2019-01-25 11:20:35 +00:00
|
|
|
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 {
|
2019-08-23 15:50:45 +00:00
|
|
|
sum := hash.Checksum(b)
|
2018-01-26 18:04:13 +00:00
|
|
|
want := binary.LittleEndian.Uint32(sum)
|
|
|
|
return have == want
|
|
|
|
}
|