2018-01-27 15:00:28 +00:00
|
|
|
package payload
|
|
|
|
|
|
|
|
import (
|
2018-02-06 06:43:32 +00:00
|
|
|
"time"
|
2019-08-28 12:43:56 +00:00
|
|
|
|
2020-05-21 10:35:44 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2018-01-27 15:00:28 +00:00
|
|
|
)
|
|
|
|
|
2019-08-29 14:19:52 +00:00
|
|
|
// Size of the payload not counting UserAgent encoding (which is at least 1 byte
|
2019-10-22 14:56:03 +00:00
|
|
|
// for zero-length string).
|
2018-01-31 08:27:08 +00:00
|
|
|
const minVersionSize = 27
|
2018-01-27 15:00:28 +00:00
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// List of Services offered by the node.
|
2019-08-27 16:56:12 +00:00
|
|
|
const (
|
|
|
|
nodePeerService uint64 = 1
|
|
|
|
// BloomFilerService uint64 = 2 // Not implemented
|
|
|
|
// PrunedNode uint64 = 3 // Not implemented
|
|
|
|
// LightNode uint64 = 4 // Not implemented
|
|
|
|
|
|
|
|
)
|
|
|
|
|
2018-01-27 15:00:28 +00:00
|
|
|
// Version payload.
|
|
|
|
type Version struct {
|
2020-05-21 10:35:44 +00:00
|
|
|
// NetMode of the node
|
|
|
|
Magic config.NetMode
|
2018-01-27 15:00:28 +00:00
|
|
|
// currently the version of the protocol is 0
|
|
|
|
Version uint32
|
|
|
|
// currently 1
|
|
|
|
Services uint64
|
|
|
|
// timestamp
|
|
|
|
Timestamp uint32
|
|
|
|
// port this server is listening on
|
|
|
|
Port uint16
|
|
|
|
// it's used to distinguish the node from public IP
|
|
|
|
Nonce uint32
|
2018-01-28 10:12:05 +00:00
|
|
|
// client id
|
2018-01-31 08:27:08 +00:00
|
|
|
UserAgent []byte
|
2018-01-27 15:00:28 +00:00
|
|
|
// Height of the block chain
|
|
|
|
StartHeight uint32
|
|
|
|
// Whether to receive and forward
|
|
|
|
Relay bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewVersion returns a pointer to a Version payload.
|
2020-05-21 10:35:44 +00:00
|
|
|
func NewVersion(magic config.NetMode, id uint32, p uint16, ua string, h uint32, r bool) *Version {
|
2018-01-27 15:00:28 +00:00
|
|
|
return &Version{
|
2020-05-21 10:35:44 +00:00
|
|
|
Magic: magic,
|
2018-01-27 15:00:28 +00:00
|
|
|
Version: 0,
|
2019-08-27 16:56:12 +00:00
|
|
|
Services: nodePeerService,
|
2018-02-06 06:43:32 +00:00
|
|
|
Timestamp: uint32(time.Now().UTC().Unix()),
|
2018-01-27 15:00:28 +00:00
|
|
|
Port: p,
|
2018-01-28 07:03:18 +00:00
|
|
|
Nonce: id,
|
2018-01-31 08:27:08 +00:00
|
|
|
UserAgent: []byte(ua),
|
2018-02-06 06:43:32 +00:00
|
|
|
StartHeight: h,
|
2018-01-27 15:00:28 +00:00
|
|
|
Relay: r,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
// DecodeBinary implements Serializable interface.
|
|
|
|
func (p *Version) DecodeBinary(br *io.BinReader) {
|
2020-05-21 10:35:44 +00:00
|
|
|
p.Magic = config.NetMode(br.ReadU32LE())
|
2019-12-12 15:52:23 +00:00
|
|
|
p.Version = br.ReadU32LE()
|
|
|
|
p.Services = br.ReadU64LE()
|
|
|
|
p.Timestamp = br.ReadU32LE()
|
|
|
|
p.Port = br.ReadU16LE()
|
|
|
|
p.Nonce = br.ReadU32LE()
|
2019-12-06 15:37:37 +00:00
|
|
|
p.UserAgent = br.ReadVarBytes()
|
2019-12-12 15:52:23 +00:00
|
|
|
p.StartHeight = br.ReadU32LE()
|
|
|
|
p.Relay = br.ReadBool()
|
2018-01-27 15:00:28 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
// EncodeBinary implements Serializable interface.
|
|
|
|
func (p *Version) EncodeBinary(br *io.BinWriter) {
|
2020-05-21 10:35:44 +00:00
|
|
|
br.WriteU32LE(uint32(p.Magic))
|
2019-12-12 15:52:23 +00:00
|
|
|
br.WriteU32LE(p.Version)
|
|
|
|
br.WriteU64LE(p.Services)
|
|
|
|
br.WriteU32LE(p.Timestamp)
|
|
|
|
br.WriteU16LE(p.Port)
|
|
|
|
br.WriteU32LE(p.Nonce)
|
2019-08-28 12:43:56 +00:00
|
|
|
|
2019-11-22 10:34:06 +00:00
|
|
|
br.WriteVarBytes(p.UserAgent)
|
2019-12-12 15:52:23 +00:00
|
|
|
br.WriteU32LE(p.StartHeight)
|
|
|
|
br.WriteBool(p.Relay)
|
2018-01-28 07:03:18 +00:00
|
|
|
}
|