2018-01-26 18:04:13 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2020-05-25 07:09:21 +00:00
|
|
|
"errors"
|
2019-09-22 17:09:55 +00:00
|
|
|
"fmt"
|
2018-01-27 15:00:28 +00:00
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/payload"
|
2018-01-26 18:04:13 +00:00
|
|
|
)
|
|
|
|
|
2020-07-08 12:25:58 +00:00
|
|
|
//go:generate stringer -type=CommandType -output=message_string.go
|
2018-01-26 20:39:34 +00:00
|
|
|
|
2020-06-15 18:13:32 +00:00
|
|
|
// CompressionMinSize is the lower bound to apply compression.
|
|
|
|
const CompressionMinSize = 1024
|
2020-05-25 07:09:21 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Message is a complete message sent between nodes.
|
2018-01-26 18:04:13 +00:00
|
|
|
type Message struct {
|
2020-05-25 07:09:21 +00:00
|
|
|
// Flags that represents whether a message is compressed.
|
|
|
|
// 0 for None, 1 for Compressed.
|
|
|
|
Flags MessageFlag
|
2022-04-20 18:30:09 +00:00
|
|
|
// Command is a byte command code.
|
2020-05-19 11:54:51 +00:00
|
|
|
Command CommandType
|
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
|
2020-05-25 07:09:21 +00:00
|
|
|
|
|
|
|
// Compressed message payload.
|
|
|
|
compressedPayload []byte
|
2020-06-18 09:00:51 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// StateRootInHeader specifies if the state root is included in the block header.
|
2020-11-17 12:57:50 +00:00
|
|
|
// This is needed for correct decoding.
|
|
|
|
StateRootInHeader bool
|
2018-01-26 18:04:13 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MessageFlag represents compression level of a message payload.
|
2020-05-25 07:09:21 +00:00
|
|
|
type MessageFlag byte
|
|
|
|
|
2021-05-12 20:17:03 +00:00
|
|
|
// Possible message flags.
|
2020-05-25 07:09:21 +00:00
|
|
|
const (
|
|
|
|
Compressed MessageFlag = 1 << iota
|
2020-06-18 08:30:07 +00:00
|
|
|
None MessageFlag = 0
|
2020-05-25 07:09:21 +00:00
|
|
|
)
|
|
|
|
|
2018-03-09 15:55:25 +00:00
|
|
|
// CommandType represents the type of a message command.
|
2020-05-19 11:54:51 +00:00
|
|
|
type CommandType byte
|
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 (
|
2021-05-12 20:17:03 +00:00
|
|
|
// Handshaking.
|
2020-05-19 11:54:51 +00:00
|
|
|
CMDVersion CommandType = 0x00
|
|
|
|
CMDVerack CommandType = 0x01
|
|
|
|
|
2021-05-12 20:17:03 +00:00
|
|
|
// Connectivity.
|
2020-05-19 11:54:51 +00:00
|
|
|
CMDGetAddr CommandType = 0x10
|
|
|
|
CMDAddr CommandType = 0x11
|
|
|
|
CMDPing CommandType = 0x18
|
|
|
|
CMDPong CommandType = 0x19
|
|
|
|
|
2021-05-12 20:17:03 +00:00
|
|
|
// Synchronization.
|
2020-11-27 10:55:48 +00:00
|
|
|
CMDGetHeaders CommandType = 0x20
|
|
|
|
CMDHeaders CommandType = 0x21
|
|
|
|
CMDGetBlocks CommandType = 0x24
|
|
|
|
CMDMempool CommandType = 0x25
|
|
|
|
CMDInv CommandType = 0x27
|
|
|
|
CMDGetData CommandType = 0x28
|
|
|
|
CMDGetBlockByIndex CommandType = 0x29
|
|
|
|
CMDNotFound CommandType = 0x2a
|
|
|
|
CMDTX = CommandType(payload.TXType)
|
|
|
|
CMDBlock = CommandType(payload.BlockType)
|
2021-01-14 13:38:40 +00:00
|
|
|
CMDExtensible = CommandType(payload.ExtensibleType)
|
2020-11-27 10:55:48 +00:00
|
|
|
CMDP2PNotaryRequest = CommandType(payload.P2PNotaryRequestType)
|
2021-07-30 13:57:42 +00:00
|
|
|
CMDGetMPTData CommandType = 0x51 // 0x5.. commands are used for extensions (P2PNotary, state exchange cmds)
|
|
|
|
CMDMPTData CommandType = 0x52
|
2020-11-27 10:55:48 +00:00
|
|
|
CMDReject CommandType = 0x2f
|
2020-05-19 11:54:51 +00:00
|
|
|
|
2021-05-12 20:17:03 +00:00
|
|
|
// SPV protocol.
|
2020-05-19 11:54:51 +00:00
|
|
|
CMDFilterLoad CommandType = 0x30
|
|
|
|
CMDFilterAdd CommandType = 0x31
|
|
|
|
CMDFilterClear CommandType = 0x32
|
|
|
|
CMDMerkleBlock CommandType = 0x38
|
|
|
|
|
2021-05-12 20:17:03 +00:00
|
|
|
// Others.
|
2020-05-19 11:54:51 +00:00
|
|
|
CMDAlert CommandType = 0x40
|
2018-01-26 18:04:13 +00:00
|
|
|
)
|
|
|
|
|
2021-03-25 19:25:30 +00:00
|
|
|
// NewMessage returns a new message with the given payload.
|
2020-05-21 10:35:44 +00:00
|
|
|
func NewMessage(cmd CommandType, p payload.Payload) *Message {
|
2018-01-26 18:04:13 +00:00
|
|
|
return &Message{
|
2020-05-19 11:54:51 +00:00
|
|
|
Command: cmd,
|
2020-04-19 20:40:31 +00:00
|
|
|
Payload: p,
|
2020-05-25 07:09:21 +00:00
|
|
|
Flags: None,
|
2018-01-27 15:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Decode decodes a Message from the given reader.
|
2019-09-16 09:18:13 +00:00
|
|
|
func (m *Message) Decode(br *io.BinReader) error {
|
2020-05-25 07:09:21 +00:00
|
|
|
m.Flags = MessageFlag(br.ReadB())
|
2020-05-19 11:54:51 +00:00
|
|
|
m.Command = CommandType(br.ReadB())
|
2020-05-25 07:09:21 +00:00
|
|
|
l := br.ReadVarUint()
|
|
|
|
// check the length first in order not to allocate memory
|
|
|
|
// for an empty compressed payload
|
|
|
|
if l == 0 {
|
2020-06-19 08:18:43 +00:00
|
|
|
switch m.Command {
|
2020-06-19 21:28:51 +00:00
|
|
|
case CMDFilterClear, CMDGetAddr, CMDMempool, CMDVerack:
|
2020-06-19 08:18:43 +00:00
|
|
|
m.Payload = payload.NewNullPayload()
|
|
|
|
default:
|
2020-11-17 12:57:50 +00:00
|
|
|
return fmt.Errorf("unexpected empty payload: %s", m.Command)
|
2020-06-19 08:18:43 +00:00
|
|
|
}
|
2020-05-25 07:09:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-06-15 18:13:32 +00:00
|
|
|
if l > payload.MaxSize {
|
2020-06-18 08:31:19 +00:00
|
|
|
return errors.New("invalid payload size")
|
|
|
|
}
|
2020-05-25 07:09:21 +00:00
|
|
|
m.compressedPayload = make([]byte, l)
|
|
|
|
br.ReadBytes(m.compressedPayload)
|
2019-08-28 12:43:56 +00:00
|
|
|
if br.Err != nil {
|
|
|
|
return br.Err
|
2018-03-03 07:16:05 +00:00
|
|
|
}
|
2020-05-25 07:09:21 +00:00
|
|
|
return m.decodePayload()
|
2018-01-27 15:47:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 07:09:21 +00:00
|
|
|
func (m *Message) decodePayload() error {
|
|
|
|
buf := m.compressedPayload
|
|
|
|
// try decompression
|
|
|
|
if m.Flags&Compressed != 0 {
|
|
|
|
d, err := decompress(m.compressedPayload)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
buf = d
|
2018-01-28 10:12:05 +00:00
|
|
|
}
|
2018-01-27 15:00:28 +00:00
|
|
|
|
2018-01-28 15:06:41 +00:00
|
|
|
var p payload.Payload
|
2020-05-19 11:54:51 +00:00
|
|
|
switch m.Command {
|
2018-03-09 15:55:25 +00:00
|
|
|
case CMDVersion:
|
2018-01-27 15:00:28 +00:00
|
|
|
p = &payload.Version{}
|
2019-08-30 10:14:12 +00:00
|
|
|
case CMDInv, CMDGetData:
|
2018-01-28 17:42:22 +00:00
|
|
|
p = &payload.Inventory{}
|
2021-07-30 13:57:42 +00:00
|
|
|
case CMDGetMPTData:
|
|
|
|
p = &payload.MPTInventory{}
|
|
|
|
case CMDMPTData:
|
|
|
|
p = &payload.MPTData{}
|
2018-03-09 15:55:25 +00:00
|
|
|
case CMDAddr:
|
2018-01-28 15:18:48 +00:00
|
|
|
p = &payload.AddressList{}
|
2018-03-09 15:55:25 +00:00
|
|
|
case CMDBlock:
|
2021-03-25 18:46:52 +00:00
|
|
|
p = block.New(m.StateRootInHeader)
|
2021-01-14 13:38:40 +00:00
|
|
|
case CMDExtensible:
|
2021-03-25 18:59:54 +00:00
|
|
|
p = payload.NewExtensible()
|
2020-11-27 10:55:48 +00:00
|
|
|
case CMDP2PNotaryRequest:
|
2021-03-25 19:18:47 +00:00
|
|
|
p = &payload.P2PNotaryRequest{}
|
2019-08-29 16:43:23 +00:00
|
|
|
case CMDGetBlocks:
|
2018-02-04 19:54:51 +00:00
|
|
|
p = &payload.GetBlocks{}
|
2020-07-31 11:47:42 +00:00
|
|
|
case CMDGetHeaders:
|
|
|
|
fallthrough
|
2020-07-31 10:58:22 +00:00
|
|
|
case CMDGetBlockByIndex:
|
|
|
|
p = &payload.GetBlockByIndex{}
|
2018-03-09 15:55:25 +00:00
|
|
|
case CMDHeaders:
|
2021-03-25 18:46:52 +00:00
|
|
|
p = &payload.Headers{StateRootInHeader: m.StateRootInHeader}
|
2018-03-10 12:04:06 +00:00
|
|
|
case CMDTX:
|
2021-08-04 20:11:53 +00:00
|
|
|
p, err := transaction.NewTransactionFromBytes(buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.Payload = p
|
|
|
|
return nil
|
2018-08-10 14:32:49 +00:00
|
|
|
case CMDMerkleBlock:
|
2021-03-25 18:46:52 +00:00
|
|
|
p = &payload.MerkleBlock{}
|
2020-01-17 10:17:19 +00:00
|
|
|
case CMDPing, CMDPong:
|
|
|
|
p = &payload.Ping{}
|
2020-08-18 11:24:27 +00:00
|
|
|
case CMDNotFound:
|
|
|
|
p = &payload.Inventory{}
|
2019-09-22 17:09:55 +00:00
|
|
|
default:
|
2020-05-19 11:54:51 +00:00
|
|
|
return fmt.Errorf("can't decode command %s", m.Command.String())
|
2019-09-16 16:31:49 +00:00
|
|
|
}
|
2021-08-04 20:11:53 +00:00
|
|
|
r := io.NewBinReaderFromBuf(buf)
|
2019-09-16 16:31:49 +00:00
|
|
|
p.DecodeBinary(r)
|
2022-09-02 11:29:47 +00:00
|
|
|
if r.Err == nil || errors.Is(r.Err, payload.ErrTooManyHeaders) {
|
2019-12-30 12:38:23 +00:00
|
|
|
m.Payload = p
|
2018-01-26 18:04:13 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 12:38:23 +00:00
|
|
|
return r.Err
|
2018-01-26 18:04:13 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Encode encodes a Message to any given BinWriter.
|
2019-09-16 09:18:13 +00:00
|
|
|
func (m *Message) Encode(br *io.BinWriter) error {
|
2020-05-25 07:09:21 +00:00
|
|
|
if err := m.tryCompressPayload(); err != nil {
|
|
|
|
return err
|
2019-09-16 16:31:49 +00:00
|
|
|
}
|
2021-08-06 08:15:50 +00:00
|
|
|
growSize := 2 + 1 // header + empty payload
|
|
|
|
if m.compressedPayload != nil {
|
|
|
|
growSize += 8 + len(m.compressedPayload) // varint + byte-slice
|
|
|
|
}
|
|
|
|
br.Grow(growSize)
|
2020-05-25 07:09:21 +00:00
|
|
|
br.WriteB(byte(m.Flags))
|
|
|
|
br.WriteB(byte(m.Command))
|
|
|
|
if m.compressedPayload != nil {
|
|
|
|
br.WriteVarBytes(m.compressedPayload)
|
|
|
|
} else {
|
|
|
|
br.WriteB(0)
|
2018-03-10 12:04:06 +00:00
|
|
|
}
|
2020-05-25 07:09:21 +00:00
|
|
|
return br.Err
|
2018-01-26 18:04:13 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 18:16:31 +00:00
|
|
|
// Bytes serializes a Message into the new allocated buffer and returns it.
|
|
|
|
func (m *Message) Bytes() ([]byte, error) {
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
if err := m.Encode(w.BinWriter); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return w.Bytes(), nil
|
|
|
|
}
|
2020-05-25 07:09:21 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// tryCompressPayload sets the message's compressed payload to a serialized
|
|
|
|
// payload and compresses it in case its size exceeds CompressionMinSize.
|
2020-05-25 07:09:21 +00:00
|
|
|
func (m *Message) tryCompressPayload() error {
|
|
|
|
if m.Payload == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
m.Payload.EncodeBinary(buf.BinWriter)
|
|
|
|
if buf.Err != nil {
|
|
|
|
return buf.Err
|
|
|
|
}
|
|
|
|
compressedPayload := buf.Bytes()
|
|
|
|
if m.Flags&Compressed == 0 {
|
|
|
|
switch m.Payload.(type) {
|
2020-12-23 12:32:16 +00:00
|
|
|
case *payload.Headers, *payload.MerkleBlock, payload.NullPayload,
|
2021-07-30 13:57:42 +00:00
|
|
|
*payload.Inventory, *payload.MPTInventory:
|
2020-05-25 07:09:21 +00:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
size := len(compressedPayload)
|
|
|
|
// try compression
|
|
|
|
if size > CompressionMinSize {
|
|
|
|
c, err := compress(compressedPayload)
|
|
|
|
if err == nil {
|
|
|
|
compressedPayload = c
|
|
|
|
m.Flags |= Compressed
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.compressedPayload = compressedPayload
|
|
|
|
return nil
|
|
|
|
}
|