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"
|
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
|
|
|
)
|
|
|
|
|
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
|
|
|
|
// timestamp
|
|
|
|
Timestamp uint32
|
|
|
|
// 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
|
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-05-22 09:17:17 +00:00
|
|
|
func NewVersion(magic config.NetMode, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.Timestamp = br.ReadU32LE()
|
|
|
|
p.Nonce = br.ReadU32LE()
|
2019-12-06 15:37:37 +00:00
|
|
|
p.UserAgent = br.ReadVarBytes()
|
2020-05-22 09:17:17 +00:00
|
|
|
p.Capabilities.DecodeBinary(br)
|
2018-01-27 15:00:28 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
// EncodeBinary implements 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
|
|
|
}
|