parent
01c96fbc50
commit
9d8024a9dd
3 changed files with 0 additions and 163 deletions
|
@ -1,64 +0,0 @@
|
|||
package payload
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/CityOfZion/neo-go/pkg/wire/command"
|
||||
"github.com/CityOfZion/neo-go/pkg/wire/util"
|
||||
)
|
||||
|
||||
// AddrMessage represents an address message on the neo network
|
||||
type AddrMessage struct {
|
||||
AddrList []*NetAddr
|
||||
}
|
||||
|
||||
// NewAddrMessage instantiates a new AddrMessage
|
||||
func NewAddrMessage() (*AddrMessage, error) {
|
||||
addrMess := &AddrMessage{
|
||||
nil,
|
||||
}
|
||||
return addrMess, nil
|
||||
}
|
||||
|
||||
// AddNetAddr will add a net address into the Address message
|
||||
func (a *AddrMessage) AddNetAddr(n *NetAddr) error {
|
||||
a.AddrList = append(a.AddrList, n)
|
||||
// TODO:check if max reached, if so return err. What is max?
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DecodePayload Implements Messager interface
|
||||
func (a *AddrMessage) DecodePayload(r io.Reader) error {
|
||||
|
||||
br := &util.BinReader{R: r}
|
||||
listLen := br.VarUint()
|
||||
|
||||
a.AddrList = make([]*NetAddr, listLen)
|
||||
for i := 0; i < int(listLen); i++ {
|
||||
a.AddrList[i] = &NetAddr{}
|
||||
a.AddrList[i].DecodePayload(br)
|
||||
if br.Err != nil {
|
||||
return br.Err
|
||||
}
|
||||
}
|
||||
return br.Err
|
||||
}
|
||||
|
||||
// EncodePayload Implements messager interface
|
||||
func (a *AddrMessage) EncodePayload(w io.Writer) error {
|
||||
bw := &util.BinWriter{W: w}
|
||||
|
||||
listLen := uint64(len(a.AddrList))
|
||||
bw.VarUint(listLen)
|
||||
|
||||
for _, addr := range a.AddrList {
|
||||
addr.EncodePayload(bw)
|
||||
}
|
||||
return bw.Err
|
||||
}
|
||||
|
||||
// Command Implements messager interface
|
||||
func (a *AddrMessage) Command() command.Type {
|
||||
return command.Addr
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
package payload
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/CityOfZion/neo-go/pkg/wire/util/Checksum"
|
||||
|
||||
"github.com/CityOfZion/neo-go/pkg/wire/protocol"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestAddrMessageEncodeDecode(t *testing.T) {
|
||||
|
||||
ip := []byte(net.ParseIP("127.0.0.1").To16())
|
||||
|
||||
var ipByte [16]byte
|
||||
copy(ipByte[:], ip)
|
||||
|
||||
netaddr, err := NewNetAddr(uint32(time.Now().Unix()), ipByte, 8080, protocol.NodePeerService)
|
||||
addrmsg, err := NewAddrMessage()
|
||||
addrmsg.AddNetAddr(netaddr)
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
err = addrmsg.EncodePayload(buf)
|
||||
expected := checksum.FromBuf(buf)
|
||||
|
||||
addrmsgDec, err := NewAddrMessage()
|
||||
r := bytes.NewReader(buf.Bytes())
|
||||
err = addrmsgDec.DecodePayload(r)
|
||||
|
||||
buf = new(bytes.Buffer)
|
||||
err = addrmsgDec.EncodePayload(buf)
|
||||
have := checksum.FromBuf(buf)
|
||||
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, expected, have)
|
||||
}
|
|
@ -1,59 +0,0 @@
|
|||
package payload
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/CityOfZion/neo-go/pkg/wire/protocol"
|
||||
"github.com/CityOfZion/neo-go/pkg/wire/util"
|
||||
)
|
||||
|
||||
//NetAddr is an abstraction for the IP layer
|
||||
type NetAddr struct {
|
||||
Timestamp uint32
|
||||
IP [16]byte
|
||||
Port uint16
|
||||
Service protocol.ServiceFlag
|
||||
}
|
||||
|
||||
//NewNetAddr returns a NetAddr object
|
||||
func NewNetAddr(time uint32, ip [16]byte, port uint16, service protocol.ServiceFlag) (*NetAddr, error) {
|
||||
return &NetAddr{time, ip, port, service}, nil
|
||||
}
|
||||
|
||||
//NewAddrFromVersionMessage returns a NetAddr object from a version message
|
||||
func NewAddrFromVersionMessage(version VersionMessage) (*NetAddr, error) {
|
||||
|
||||
var ip [16]byte
|
||||
|
||||
copy(ip[:], []byte(version.IP)[:16])
|
||||
|
||||
return NewNetAddr(version.Timestamp, ip, version.Port, version.Services)
|
||||
}
|
||||
|
||||
// EncodePayload Implements messager interface
|
||||
func (n *NetAddr) EncodePayload(bw *util.BinWriter) {
|
||||
|
||||
bw.Write(uint32(time.Now().Unix()))
|
||||
bw.Write(protocol.NodePeerService)
|
||||
bw.WriteBigEnd(n.IP)
|
||||
bw.WriteBigEnd(n.Port)
|
||||
}
|
||||
|
||||
// DecodePayload Implements Messager interface
|
||||
func (n *NetAddr) DecodePayload(br *util.BinReader) {
|
||||
|
||||
br.Read(&n.Timestamp)
|
||||
br.Read(&n.Service)
|
||||
br.ReadBigEnd(&n.IP)
|
||||
br.ReadBigEnd(&n.Port)
|
||||
}
|
||||
|
||||
//IPPort returns the IPPort from the NetAddr
|
||||
func (n *NetAddr) IPPort() string {
|
||||
ip := net.IP(n.IP[:]).String()
|
||||
port := strconv.Itoa(int(n.Port))
|
||||
ipport := ip + ":" + port
|
||||
return ipport
|
||||
}
|
Loading…
Reference in a new issue