2018-01-27 15:00:28 +00:00
|
|
|
package payload
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2018-01-28 15:06:41 +00:00
|
|
|
"io"
|
2018-02-06 06:43:32 +00:00
|
|
|
"time"
|
2018-01-27 15:00:28 +00:00
|
|
|
)
|
|
|
|
|
2018-01-31 08:27:08 +00:00
|
|
|
const minVersionSize = 27
|
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,
|
|
|
|
Services: 1,
|
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 {
|
|
|
|
err := binary.Read(r, binary.LittleEndian, &p.Version)
|
|
|
|
err = binary.Read(r, binary.LittleEndian, &p.Services)
|
|
|
|
err = binary.Read(r, binary.LittleEndian, &p.Timestamp)
|
|
|
|
err = binary.Read(r, binary.LittleEndian, &p.Port)
|
|
|
|
err = binary.Read(r, binary.LittleEndian, &p.Nonce)
|
2018-01-31 08:27:08 +00:00
|
|
|
|
|
|
|
var lenUA uint8
|
|
|
|
err = binary.Read(r, binary.LittleEndian, &lenUA)
|
|
|
|
p.UserAgent = make([]byte, lenUA)
|
2018-01-28 15:06:41 +00:00
|
|
|
err = binary.Read(r, binary.LittleEndian, &p.UserAgent)
|
2018-01-31 08:27:08 +00:00
|
|
|
|
2018-01-28 15:06:41 +00:00
|
|
|
err = binary.Read(r, binary.LittleEndian, &p.StartHeight)
|
|
|
|
err = binary.Read(r, binary.LittleEndian, &p.Relay)
|
2018-01-28 10:12:05 +00:00
|
|
|
|
2018-01-28 15:06:41 +00:00
|
|
|
return 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 {
|
|
|
|
err := binary.Write(w, binary.LittleEndian, p.Version)
|
|
|
|
err = binary.Write(w, binary.LittleEndian, p.Services)
|
|
|
|
err = binary.Write(w, binary.LittleEndian, p.Timestamp)
|
|
|
|
err = binary.Write(w, binary.LittleEndian, p.Port)
|
|
|
|
err = binary.Write(w, binary.LittleEndian, p.Nonce)
|
2018-01-31 08:27:08 +00:00
|
|
|
err = binary.Write(w, binary.LittleEndian, uint8(len(p.UserAgent)))
|
2018-01-28 15:06:41 +00:00
|
|
|
err = binary.Write(w, binary.LittleEndian, p.UserAgent)
|
|
|
|
err = binary.Write(w, binary.LittleEndian, p.StartHeight)
|
|
|
|
err = binary.Write(w, binary.LittleEndian, p.Relay)
|
2018-01-28 07:03:18 +00:00
|
|
|
|
2018-01-28 15:06:41 +00:00
|
|
|
return 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 {
|
|
|
|
return uint32(minVersionSize + len(p.UserAgent))
|
2018-01-27 15:00:28 +00:00
|
|
|
}
|