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-06-14 07:34:50 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-05-22 09:17:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/capability"
|
2019-08-27 16:56:12 +00:00
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MaxUserAgentLength is the limit for the user agent field.
|
2020-10-07 15:27:24 +00:00
|
|
|
const MaxUserAgentLength = 1024
|
|
|
|
|
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
|
2020-06-14 07:34:50 +00:00
|
|
|
Magic netmode.Magic
|
2018-01-27 15:00:28 +00:00
|
|
|
// currently the version of the protocol is 0
|
|
|
|
Version uint32
|
|
|
|
// timestamp
|
|
|
|
Timestamp uint32
|
2022-04-20 18:30:09 +00:00
|
|
|
// it's used to distinguish several nodes using the same public IP (or different ones)
|
2018-01-27 15:00:28 +00:00
|
|
|
Nonce uint32
|
2018-01-28 10:12:05 +00:00
|
|
|
// client id
|
2018-01-31 08:27:08 +00:00
|
|
|
UserAgent []byte
|
2020-05-22 09:17:17 +00:00
|
|
|
// List of available network services
|
|
|
|
Capabilities capability.Capabilities
|
2018-01-27 15:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewVersion returns a pointer to a Version payload.
|
2020-06-14 07:34:50 +00:00
|
|
|
func NewVersion(magic netmode.Magic, id uint32, ua string, c []capability.Capability) *Version {
|
2018-01-27 15:00:28 +00:00
|
|
|
return &Version{
|
2020-05-22 09:17:17 +00:00
|
|
|
Magic: magic,
|
|
|
|
Version: 0,
|
|
|
|
Timestamp: uint32(time.Now().UTC().Unix()),
|
|
|
|
Nonce: id,
|
|
|
|
UserAgent: []byte(ua),
|
|
|
|
Capabilities: c,
|
2018-01-27 15:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// DecodeBinary implements the Serializable interface.
|
2019-09-16 16:31:49 +00:00
|
|
|
func (p *Version) DecodeBinary(br *io.BinReader) {
|
2020-06-14 07:34:50 +00:00
|
|
|
p.Magic = netmode.Magic(br.ReadU32LE())
|
2019-12-12 15:52:23 +00:00
|
|
|
p.Version = br.ReadU32LE()
|
|
|
|
p.Timestamp = br.ReadU32LE()
|
|
|
|
p.Nonce = br.ReadU32LE()
|
2020-10-07 15:27:24 +00:00
|
|
|
p.UserAgent = br.ReadVarBytes(MaxUserAgentLength)
|
2020-05-22 09:17:17 +00:00
|
|
|
p.Capabilities.DecodeBinary(br)
|
2018-01-27 15:00:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// EncodeBinary implements the Serializable interface.
|
2020-05-22 09:17:17 +00:00
|
|
|
func (p *Version) EncodeBinary(bw *io.BinWriter) {
|
|
|
|
bw.WriteU32LE(uint32(p.Magic))
|
|
|
|
bw.WriteU32LE(p.Version)
|
|
|
|
bw.WriteU32LE(p.Timestamp)
|
|
|
|
bw.WriteU32LE(p.Nonce)
|
|
|
|
bw.WriteVarBytes(p.UserAgent)
|
|
|
|
p.Capabilities.EncodeBinary(bw)
|
2018-01-28 07:03:18 +00:00
|
|
|
}
|