Skip to content

Commit

Permalink
protocol: add capabilities to address payload
Browse files Browse the repository at this point in the history
Part of #871
  • Loading branch information
AnnaShaleva committed May 25, 2020
1 parent 902cfd5 commit c0299f4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
33 changes: 21 additions & 12 deletions pkg/network/payload/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@ import (
"time"

"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/network/capability"
)

// AddressAndTime payload.
type AddressAndTime struct {
Timestamp uint32
Services uint64
IP [16]byte
Port uint16
Timestamp uint32
IP [16]byte
Capabilities []capability.Capability
}

// NewAddressAndTime creates a new AddressAndTime object.
func NewAddressAndTime(e *net.TCPAddr, t time.Time) *AddressAndTime {
aat := AddressAndTime{
Timestamp: uint32(t.UTC().Unix()),
Services: 1,
Port: uint16(e.Port),
Capabilities: []capability.Capability{
{
Type: capability.TCPServer,
Data: &capability.Server{Port: uint16(e.Port)},
},
},
}
copy(aat.IP[:], e.IP)
return &aat
Expand All @@ -30,26 +34,31 @@ func NewAddressAndTime(e *net.TCPAddr, t time.Time) *AddressAndTime {
// DecodeBinary implements Serializable interface.
func (p *AddressAndTime) DecodeBinary(br *io.BinReader) {
p.Timestamp = br.ReadU32LE()
p.Services = br.ReadU64LE()
br.ReadBytes(p.IP[:])
p.Port = br.ReadU16BE()
br.ReadArray(&p.Capabilities, capability.MaxCapabilities)
}

// EncodeBinary implements Serializable interface.
func (p *AddressAndTime) EncodeBinary(bw *io.BinWriter) {
bw.WriteU32LE(p.Timestamp)
bw.WriteU64LE(p.Services)
bw.WriteBytes(p.IP[:])
bw.WriteU16BE(p.Port)
bw.WriteArray(p.Capabilities)
}

// IPPortString makes a string from IP and port specified.
func (p *AddressAndTime) IPPortString() string {
var netip = make(net.IP, 16)

copy(netip, p.IP[:])
port := strconv.Itoa(int(p.Port))
return netip.String() + ":" + port
var tcpPort uint16
for _, cap := range p.Capabilities {
if cap.Type == capability.TCPServer {
tcpPort = cap.Data.(*capability.Server).Port
break
}
}
port := strconv.Itoa(int(tcpPort))
return net.JoinHostPort(netip.String(), port)
}

// AddressList is a list with AddrAndTime.
Expand Down
7 changes: 6 additions & 1 deletion pkg/network/payload/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/network/capability"
"github.com/stretchr/testify/assert"
)

Expand All @@ -21,7 +22,11 @@ func TestEncodeDecodeAddress(t *testing.T) {
aatip := make(net.IP, 16)
copy(aatip, addr.IP[:])
assert.Equal(t, e.IP, aatip)
assert.Equal(t, e.Port, int(addr.Port))
assert.Equal(t, 1, len(addr.Capabilities))
assert.Equal(t, capability.Capability{
Type: capability.TCPServer,
Data: &capability.Server{Port: uint16(e.Port)},
}, addr.Capabilities[0])

testserdes.EncodeDecodeBinary(t, addr, new(AddressAndTime))
}
Expand Down

0 comments on commit c0299f4

Please sign in to comment.