2018-01-27 15:00:28 +00:00
|
|
|
package payload
|
|
|
|
|
|
|
|
import (
|
2018-01-28 15:06:41 +00:00
|
|
|
"io"
|
2018-02-06 06:43:32 +00:00
|
|
|
"time"
|
2019-08-28 12:43:56 +00:00
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
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
|
|
|
|
// for zero-length string)
|
2018-01-31 08:27:08 +00:00
|
|
|
const minVersionSize = 27
|
2018-01-27 15:00:28 +00:00
|
|
|
|
2019-08-27 16:56:12 +00:00
|
|
|
// List of Services offered by the node
|
|
|
|
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 {
|
|
|
|
// 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.
|
2018-01-28 07:03:18 +00:00
|
|
|
func NewVersion(id uint32, p uint16, ua string, h uint32, r bool) *Version {
|
2018-01-27 15:00:28 +00:00
|
|
|
return &Version{
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-28 15:06:41 +00:00
|
|
|
// DecodeBinary implements the Payload interface.
|
|
|
|
func (p *Version) DecodeBinary(r io.Reader) error {
|
2019-08-28 12:43:56 +00:00
|
|
|
br := util.BinReader{R: r}
|
|
|
|
br.ReadLE(&p.Version)
|
|
|
|
br.ReadLE(&p.Services)
|
|
|
|
br.ReadLE(&p.Timestamp)
|
|
|
|
br.ReadLE(&p.Port)
|
|
|
|
br.ReadLE(&p.Nonce)
|
2019-08-29 10:42:16 +00:00
|
|
|
p.UserAgent = br.ReadBytes()
|
2019-08-28 12:43:56 +00:00
|
|
|
br.ReadLE(&p.StartHeight)
|
|
|
|
br.ReadLE(&p.Relay)
|
|
|
|
return br.Err
|
2018-01-27 15:00:28 +00:00
|
|
|
}
|
|
|
|
|
2018-01-28 15:06:41 +00:00
|
|
|
// EncodeBinary implements the Payload interface.
|
|
|
|
func (p *Version) EncodeBinary(w io.Writer) error {
|
2019-08-28 12:43:56 +00:00
|
|
|
br := util.BinWriter{W: w}
|
|
|
|
br.WriteLE(p.Version)
|
|
|
|
br.WriteLE(p.Services)
|
|
|
|
br.WriteLE(p.Timestamp)
|
|
|
|
br.WriteLE(p.Port)
|
|
|
|
br.WriteLE(p.Nonce)
|
|
|
|
|
2019-08-29 10:42:16 +00:00
|
|
|
br.WriteBytes(p.UserAgent)
|
2019-08-28 12:43:56 +00:00
|
|
|
br.WriteLE(p.StartHeight)
|
|
|
|
br.WriteLE(&p.Relay)
|
|
|
|
return br.Err
|
2018-01-28 07:03:18 +00:00
|
|
|
}
|
2018-01-27 15:00:28 +00:00
|
|
|
|
2018-01-28 10:12:05 +00:00
|
|
|
// Size implements the payloader interface.
|
2018-01-28 07:03:18 +00:00
|
|
|
func (p *Version) Size() uint32 {
|
2019-08-29 14:19:52 +00:00
|
|
|
return uint32(minVersionSize + util.GetVarSize(p.UserAgent))
|
2018-01-27 15:00:28 +00:00
|
|
|
}
|