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