mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-23 23:25:22 +00:00
_pkg.dev: drop version from payload
Nothing new for us here.
This commit is contained in:
parent
650a1d4bcc
commit
26eee620ec
2 changed files with 0 additions and 152 deletions
|
@ -1,93 +0,0 @@
|
|||
// Copied and Modified for NEO from: https://github.com/decred/dcrd/blob/master/wire/VersionMessage.go
|
||||
|
||||
package payload
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/CityOfZion/neo-go/pkg/wire/command"
|
||||
"github.com/CityOfZion/neo-go/pkg/wire/protocol"
|
||||
"github.com/CityOfZion/neo-go/pkg/wire/util"
|
||||
)
|
||||
|
||||
const minMsgVersionSize = 28
|
||||
|
||||
var errInvalidNetAddr = errors.New("provided net.Addr is not a net.TCPAddr")
|
||||
|
||||
//VersionMessage represents a version message on the neo-network
|
||||
type VersionMessage struct {
|
||||
Version protocol.Version
|
||||
Timestamp uint32
|
||||
Services protocol.ServiceFlag
|
||||
IP net.IP
|
||||
Port uint16
|
||||
Nonce uint32
|
||||
UserAgent []byte
|
||||
StartHeight uint32
|
||||
Relay bool
|
||||
}
|
||||
|
||||
//NewVersionMessage will return a VersionMessage object
|
||||
func NewVersionMessage(addr net.Addr, startHeight uint32, relay bool, pver protocol.Version, userAgent string, nonce uint32, services protocol.ServiceFlag) (*VersionMessage, error) {
|
||||
|
||||
tcpAddr, ok := addr.(*net.TCPAddr)
|
||||
if !ok {
|
||||
return nil, errInvalidNetAddr
|
||||
}
|
||||
|
||||
version := &VersionMessage{
|
||||
pver,
|
||||
uint32(time.Now().Unix()),
|
||||
services,
|
||||
tcpAddr.IP,
|
||||
uint16(tcpAddr.Port),
|
||||
nonce,
|
||||
[]byte(userAgent),
|
||||
startHeight,
|
||||
relay,
|
||||
}
|
||||
return version, nil
|
||||
}
|
||||
|
||||
// DecodePayload Implements Messager interface
|
||||
func (v *VersionMessage) DecodePayload(r io.Reader) error {
|
||||
br := &util.BinReader{R: r}
|
||||
br.Read(&v.Version)
|
||||
br.Read(&v.Services)
|
||||
br.Read(&v.Timestamp)
|
||||
br.Read(&v.Port) // Port is not BigEndian as stated in the docs
|
||||
br.Read(&v.Nonce)
|
||||
|
||||
var lenUA uint8
|
||||
br.Read(&lenUA)
|
||||
|
||||
v.UserAgent = make([]byte, lenUA)
|
||||
br.Read(&v.UserAgent)
|
||||
br.Read(&v.StartHeight)
|
||||
br.Read(&v.Relay)
|
||||
return br.Err
|
||||
}
|
||||
|
||||
// EncodePayload Implements messager interface
|
||||
func (v *VersionMessage) EncodePayload(w io.Writer) error {
|
||||
bw := &util.BinWriter{W: w}
|
||||
|
||||
bw.Write(v.Version)
|
||||
bw.Write(v.Services)
|
||||
bw.Write(v.Timestamp)
|
||||
bw.Write(v.Port) // Not big End
|
||||
bw.Write(v.Nonce)
|
||||
bw.Write(uint8(len(v.UserAgent)))
|
||||
bw.Write(v.UserAgent)
|
||||
bw.Write(v.StartHeight)
|
||||
bw.Write(v.Relay)
|
||||
return bw.Err
|
||||
}
|
||||
|
||||
// Command Implements messager interface
|
||||
func (v *VersionMessage) Command() command.Type {
|
||||
return command.Version
|
||||
}
|
|
@ -1,59 +0,0 @@
|
|||
package payload
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"math/rand"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/CityOfZion/neo-go/pkg/wire/protocol"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValidNewVersionMessage(t *testing.T) {
|
||||
|
||||
expectedIP := "127.0.0.1"
|
||||
expectedPort := 8333
|
||||
tcpAddrMe := &net.TCPAddr{IP: net.ParseIP(expectedIP), Port: expectedPort}
|
||||
nonce := randRange(12949672, 42949672)
|
||||
message, err := NewVersionMessage(tcpAddrMe, 0, true, protocol.DefaultVersion, protocol.UserAgent, nonce, protocol.NodePeerService)
|
||||
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, expectedIP, message.IP.String())
|
||||
assert.Equal(t, uint16(expectedPort), message.Port)
|
||||
assert.Equal(t, protocol.DefaultVersion, message.Version)
|
||||
}
|
||||
func TestEncode(t *testing.T) {
|
||||
|
||||
expectedIP := "127.0.0.1"
|
||||
expectedPort := 8333
|
||||
tcpAddrMe := &net.TCPAddr{IP: net.ParseIP(expectedIP), Port: expectedPort}
|
||||
nonce := randRange(12949672, 42949672)
|
||||
message, err := NewVersionMessage(tcpAddrMe, 0, true, protocol.DefaultVersion, protocol.UserAgent, nonce, protocol.NodePeerService)
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
err = message.EncodePayload(buf)
|
||||
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, len(message.UserAgent)+minMsgVersionSize, int(buf.Len()))
|
||||
}
|
||||
func TestLenIsCorrect(t *testing.T) {
|
||||
|
||||
expectedIP := "127.0.0.1"
|
||||
expectedPort := 8333
|
||||
tcpAddrMe := &net.TCPAddr{IP: net.ParseIP(expectedIP), Port: expectedPort}
|
||||
nonce := randRange(12949672, 42949672)
|
||||
message, err := NewVersionMessage(tcpAddrMe, 0, true, protocol.DefaultVersion, protocol.UserAgent, nonce, protocol.NodePeerService)
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
err = message.EncodePayload(buf)
|
||||
assert.Equal(t, nil, err)
|
||||
|
||||
assert.Equal(t, len(message.UserAgent)+minMsgVersionSize, len(buf.Bytes()))
|
||||
}
|
||||
|
||||
func randRange(min, max int) uint32 {
|
||||
rand.Seed(time.Now().Unix() + int64(rand.Uint64()))
|
||||
return uint32(rand.Intn(max-min) + min)
|
||||
}
|
Loading…
Reference in a new issue