2018-01-26 18:04:13 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
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/consensus"
|
|
|
|
"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-05-19 11:54:51 +00:00
|
|
|
//go:generate stringer -type=CommandType
|
2018-01-26 20:39:34 +00:00
|
|
|
|
2018-01-26 18:04:13 +00:00
|
|
|
// Message is the complete message send between nodes.
|
|
|
|
type Message struct {
|
2020-05-19 11:54:51 +00:00
|
|
|
// Command is byte command code.
|
|
|
|
Command CommandType
|
2018-03-14 09:36:59 +00:00
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Length of the payload.
|
2018-01-26 18:04:13 +00:00
|
|
|
Length 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.
|
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 (
|
2020-05-19 11:54:51 +00:00
|
|
|
// handshaking
|
|
|
|
CMDVersion CommandType = 0x00
|
|
|
|
CMDVerack CommandType = 0x01
|
|
|
|
|
|
|
|
// connectivity
|
|
|
|
CMDGetAddr CommandType = 0x10
|
|
|
|
CMDAddr CommandType = 0x11
|
|
|
|
CMDPing CommandType = 0x18
|
|
|
|
CMDPong CommandType = 0x19
|
|
|
|
|
|
|
|
// synchronization
|
|
|
|
CMDGetHeaders CommandType = 0x20
|
|
|
|
CMDHeaders CommandType = 0x21
|
|
|
|
CMDGetBlocks CommandType = 0x24
|
|
|
|
CMDMempool CommandType = 0x25
|
|
|
|
CMDInv CommandType = 0x27
|
|
|
|
CMDGetData CommandType = 0x28
|
|
|
|
CMDUnknown CommandType = 0x2a
|
|
|
|
CMDTX CommandType = 0x2b
|
|
|
|
CMDBlock CommandType = 0x2c
|
|
|
|
CMDConsensus CommandType = 0x2d
|
|
|
|
CMDReject CommandType = 0x2f
|
|
|
|
|
|
|
|
// SPV protocol
|
|
|
|
CMDFilterLoad CommandType = 0x30
|
|
|
|
CMDFilterAdd CommandType = 0x31
|
|
|
|
CMDFilterClear CommandType = 0x32
|
|
|
|
CMDMerkleBlock CommandType = 0x38
|
|
|
|
|
|
|
|
// others
|
|
|
|
CMDAlert CommandType = 0x40
|
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.
|
2020-05-21 10:35:44 +00:00
|
|
|
func NewMessage(cmd CommandType, p payload.Payload) *Message {
|
2018-01-28 07:03:18 +00:00
|
|
|
var (
|
2020-04-19 20:40:31 +00:00
|
|
|
size uint32
|
2018-01-28 07:03:18 +00:00
|
|
|
)
|
|
|
|
|
2018-01-27 15:00:28 +00:00
|
|
|
if p != nil {
|
2019-09-16 09:18:13 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
2019-09-16 16:31:49 +00:00
|
|
|
p.EncodeBinary(buf.BinWriter)
|
|
|
|
if buf.Err != nil {
|
|
|
|
panic(buf.Err)
|
2018-01-28 15:06:41 +00:00
|
|
|
}
|
2019-09-16 09:18:13 +00:00
|
|
|
b := buf.Bytes()
|
|
|
|
size = uint32(len(b))
|
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{
|
2020-05-19 11:54:51 +00:00
|
|
|
Command: cmd,
|
2020-04-19 20:40:31 +00:00
|
|
|
Length: size,
|
|
|
|
Payload: p,
|
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-19 11:54:51 +00:00
|
|
|
m.Command = CommandType(br.ReadB())
|
2019-12-12 15:52:23 +00:00
|
|
|
m.Length = br.ReadU32LE()
|
2019-08-28 12:43:56 +00:00
|
|
|
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
|
|
|
|
}
|
2019-09-16 09:18:13 +00:00
|
|
|
return m.decodePayload(br)
|
2018-01-27 15:47:43 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 09:18:13 +00:00
|
|
|
func (m *Message) decodePayload(br *io.BinReader) error {
|
|
|
|
buf := make([]byte, m.Length)
|
2019-12-12 15:52:23 +00:00
|
|
|
br.ReadBytes(buf)
|
2019-09-16 09:18:13 +00:00
|
|
|
if br.Err != nil {
|
|
|
|
return br.Err
|
2018-01-28 10:12:05 +00:00
|
|
|
}
|
2018-01-27 15:00:28 +00:00
|
|
|
|
2019-09-16 09:18:13 +00:00
|
|
|
r := io.NewBinReaderFromBuf(buf)
|
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{}
|
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:
|
2020-01-14 12:32:07 +00:00
|
|
|
p = &block.Block{}
|
2019-10-17 08:28:58 +00:00
|
|
|
case CMDConsensus:
|
2019-11-08 15:40:21 +00:00
|
|
|
p = &consensus.Payload{}
|
2019-08-29 16:43:23 +00:00
|
|
|
case CMDGetBlocks:
|
|
|
|
fallthrough
|
2018-03-09 15:55:25 +00:00
|
|
|
case CMDGetHeaders:
|
2018-02-04 19:54:51 +00:00
|
|
|
p = &payload.GetBlocks{}
|
2018-03-09 15:55:25 +00:00
|
|
|
case CMDHeaders:
|
2018-02-04 19:54:51 +00:00
|
|
|
p = &payload.Headers{}
|
2018-03-10 12:04:06 +00:00
|
|
|
case CMDTX:
|
|
|
|
p = &transaction.Transaction{}
|
2018-08-10 14:32:49 +00:00
|
|
|
case CMDMerkleBlock:
|
|
|
|
p = &payload.MerkleBlock{}
|
2020-01-17 10:17:19 +00:00
|
|
|
case CMDPing, CMDPong:
|
|
|
|
p = &payload.Ping{}
|
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
|
|
|
}
|
|
|
|
p.DecodeBinary(r)
|
2019-12-30 12:38:23 +00:00
|
|
|
if r.Err == nil || r.Err == payload.ErrTooManyHeaders {
|
|
|
|
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-19 11:54:51 +00:00
|
|
|
br.WriteB(byte(m.Command))
|
2019-12-12 15:52:23 +00:00
|
|
|
br.WriteU32LE(m.Length)
|
2019-09-16 16:31:49 +00:00
|
|
|
if m.Payload != nil {
|
|
|
|
m.Payload.EncodeBinary(br)
|
|
|
|
|
|
|
|
}
|
2019-08-28 12:43:56 +00:00
|
|
|
if br.Err != nil {
|
|
|
|
return br.Err
|
2018-03-10 12:04:06 +00:00
|
|
|
}
|
2018-01-27 15:00:28 +00:00
|
|
|
return nil
|
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
|
|
|
|
}
|
|
|
|
if w.Err != nil {
|
|
|
|
return nil, w.Err
|
|
|
|
}
|
|
|
|
return w.Bytes(), nil
|
|
|
|
}
|