Skip to content
This repository was archived by the owner on Aug 2, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
fa62fad
network: Cherrypick changes to kademlia for capabilities db
nolash Aug 30, 2019
97e4547
network: WIP rehabilitate tests
nolash Aug 30, 2019
8e95061
network, pss: Add capabilities to discovery message (WIP)
nolash Sep 2, 2019
59f84ae
network: Add capabilities array to discovery messages tests
nolash Sep 3, 2019
1142690
network: Clean up code and enforce constructors for BzzAddr
nolash Sep 3, 2019
f36d1bd
network: Gofmt
nolash Sep 3, 2019
df7be16
network: Speling
nolash Sep 3, 2019
213921c
network, pss, cmd: Finish renaming NewAddr
nolash Sep 4, 2019
38d170c
network, pot, pss: Rename RandomAddr to explicit RandomBzzAddr
nolash Sep 4, 2019
75e7091
pss: Gofmt
nolash Sep 4, 2019
7ef9503
network: Custom deserialize in BzzAddr for Capabilities
nolash Sep 8, 2019
5723f9f
network: Add serialization tests
nolash Sep 8, 2019
84222da
network: Temporary workaround for contingent list in BzzAddr deserialize
nolash Sep 9, 2019
70cb0cd
network: BzzAddr deserialize fail on empty object
nolash Sep 10, 2019
afc70b0
network, cmd: WIP debugging
nolash Sep 11, 2019
b209db4
network: Add deserialize in peersMsg to elimiate list error
nolash Sep 11, 2019
b8149bb
network: Re-instate serialize capabilites to bytes in BzzAddr
nolash Sep 11, 2019
bd59236
pot: Revert inadvertent rename of RandomAddress*
nolash Sep 11, 2019
060331b
network/stream: Use new BzzAddr constructor in stream tests
nolash Sep 12, 2019
3d631e9
pot: Remove wrong rename for random address gen in pot test
nolash Sep 12, 2019
3d32505
network: Remove comments, reinstate skipped hive test
nolash Sep 12, 2019
83cca5e
network: Add test for kademlia capability indices
nolash Sep 13, 2019
4cc3da1
network: Add test for connections capabilities index filtering
nolash Sep 13, 2019
bedced0
network: Add comments to capabilities index test
nolash Sep 13, 2019
0355266
newtork: Add comments, stub for capabilities index removal
nolash Sep 13, 2019
297eae3
network: Complete remove from index method and corresponding test
nolash Sep 13, 2019
a9b5fe8
network: Speling
nolash Sep 13, 2019
a876276
network: Same structure of detect conn peer in add to index as remove
nolash Sep 13, 2019
82b065d
network: Bump protocol versions
nolash Sep 13, 2019
2790fdf
network: Update test protocol version in handshake/bzz protocol tests
nolash Sep 13, 2019
7d05df6
network: Remove lightnode from BzzPeer and cryptic nil in eachAddr
nolash Sep 16, 2019
61bf42c
network: Speling
nolash Sep 16, 2019
7f24059
network: Remove retrieval light node test
nolash Sep 16, 2019
cb71212
network: Split up capability indices test
nolash Sep 16, 2019
f3cdb0d
network: Add test for deserialization result for bzzaddr
nolash Sep 17, 2019
44795f2
Travis travis travis
nolash Sep 18, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/swarm-snapshot/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func createSnapshot(filename string, nodes int, services []string) (err error) {

sim := simulation.NewInProc(map[string]simulation.ServiceFunc{
"bzz": func(ctx *adapters.ServiceContext, bucket *sync.Map) (node.Service, func(), error) {
addr := network.NewAddr(ctx.Config.Node())
addr := network.NewBzzAddrFromEnode(ctx.Config.Node())
kad := network.NewKademlia(addr.Over(), network.NewKadParams())
hp := network.NewHiveParams()
hp.KeepAliveInterval = time.Duration(200) * time.Millisecond
Expand Down
5 changes: 4 additions & 1 deletion cmd/swarm/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ func newTestCluster(t *testing.T, size int) *testCluster {
}

// connect the nodes together
for _, node := range cluster.Nodes {
for i, node := range cluster.Nodes {
if i == 0 {
continue
}
if err := node.Client.Call(nil, "admin_addPeer", cluster.Nodes[0].Enode); err != nil {
t.Fatal(err)
}
Expand Down
30 changes: 30 additions & 0 deletions network/capability/capability.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,33 @@ func (c *Capabilities) DecodeRLP(s *rlp.Stream) error {

return nil
}

// Match returns true if all bits set in the argument is also set in the receiver
func (c *Capability) Match(capCompare *Capability) bool {
if capCompare == nil || len(c.Cap) != len(capCompare.Cap) {
return false
}
// on the first occurrence of false where query has true we can fail
for i, flag := range capCompare.Cap {
if flag && !c.Cap[i] {
return false
}
}
return true
}

// Match returns true if all bits set in all capability arguments are also set in the receiver's capabilities
func (c *Capabilities) Match(capsCompare *Capabilities) bool {
for _, capCompare := range capsCompare.Caps {

// if queryied id doesn't exist in object we can nay right away
cap := c.Get(capCompare.Id)
if cap == nil {
return false
}
if !cap.Match(capCompare) {
return false
}
}
return true
}
56 changes: 56 additions & 0 deletions network/capability/capability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,59 @@ func TestCapabilitiesRLP(t *testing.T) {
t.Fatalf("cap 1 caps not correct, expected %v, got %v", cap2.Cap, cap2Restored.Cap)
}
}

// TestCapabilitiesQuery tests methods for quering capability states
func TestCapabilitiesQuery(t *testing.T) {

// Initialize capability
caps := NewCapabilities()

// Register module. Should succeed
c1 := NewCapability(1, 3)
c1.Set(1)
err := caps.Add(c1)
if err != nil {
t.Fatalf("RegisterCapabilityModule fail: %v", err)
}

c2 := NewCapability(42, 9)
c2.Set(2)
c2.Set(8)
err = caps.Add(c2)
if err != nil {
t.Fatalf("RegisterCapabilityModule fail: %v", err)
}

capsCompare := NewCapabilities()
capCompare := NewCapability(42, 10)
capCompare.Set(2)
capCompare.Set(8)
capsCompare.Add(capCompare)
if caps.Match(capsCompare) {
t.Fatalf("Expected cCompare with mismatch length to fail; %s != %s", capsCompare, caps)
}
capsCompare = NewCapabilities()
capCompare = NewCapability(42, 9)
capCompare.Set(2)
capsCompare.Add(capCompare)
if !caps.Match(capsCompare) {
t.Fatalf("Expected %s to match %s", capsCompare, caps)
}

capCompare = NewCapability(1, 3)
capsCompare.Add(capCompare)
if !caps.Match(capsCompare) {
t.Fatalf("Expected %s to match %s", capsCompare, caps)
}

capCompare.Set(1)
if !caps.Match(capsCompare) {
t.Fatalf("Expected %s to match %s", capsCompare, caps)
}

capCompare.Set(2)
if caps.Match(capsCompare) {
t.Fatalf("Expected %s not to match %s", capsCompare, caps)
}

}
25 changes: 24 additions & 1 deletion network/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"fmt"
"sync"

"github.com/ethereum/go-ethereum/rlp"
"github.com/ethersphere/swarm/log"
"github.com/ethersphere/swarm/pot"
)

Expand Down Expand Up @@ -95,6 +97,7 @@ func (d *Peer) NotifyPeer(a *BzzAddr, po uint8) {
resp := &peersMsg{
Peers: []*BzzAddr{a},
}
log.Warn("notifypeer", "notify", resp)
go d.Send(context.TODO(), resp)
}

Expand Down Expand Up @@ -127,6 +130,27 @@ type peersMsg struct {
Peers []*BzzAddr
}

// DecodeRLP implements rlp.Decoder interface
Comment thread
nolash marked this conversation as resolved.
func (p *peersMsg) DecodeRLP(s *rlp.Stream) error {
_, err := s.List()
if err != nil {
return err
}
_, err = s.List()
if err != nil {
return err
}
for {
var addr BzzAddr
err = s.Decode(&addr)
if err != nil {
break
}
p.Peers = append(p.Peers, &addr)
}
return nil
}

// String pretty prints a peersMsg
func (msg peersMsg) String() string {
return fmt.Sprintf("%T: %v", msg, msg.Peers)
Expand All @@ -140,7 +164,6 @@ func (d *Peer) handlePeersMsg(msg *peersMsg) error {
if len(msg.Peers) == 0 {
return nil
}

for _, a := range msg.Peers {
d.seen(a)
NotifyPeer(a, d.kad)
Expand Down
28 changes: 13 additions & 15 deletions network/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package network

import (
"bytes"
"crypto/ecdsa"
crand "crypto/rand"
"encoding/binary"
"fmt"
"math/rand"
Expand Down Expand Up @@ -48,7 +48,7 @@ func TestSubPeersMsg(t *testing.T) {
}

node := s.Nodes[0]
raddr := NewAddr(node)
raddr := NewBzzAddrFromEnode(node)
pp.Register(raddr)

// start the hive and wait for the connection
Expand Down Expand Up @@ -114,18 +114,15 @@ func testInitialPeersMsg(t *testing.T, peerPO, peerDepth int) {
connect := func(a pot.Address, po int) (addrs []*BzzAddr) {
n := rand.Intn(maxPeersPerPO)
for i := 0; i < n; i++ {
peer, err := newDiscPeer(pot.RandomAddressAt(a, po))
if err != nil {
t.Fatal(err)
}
peer := newDiscPeer(pot.RandomAddressAt(a, po))
hive.On(peer)
addrs = append(addrs, peer.BzzAddr)
}
return addrs
}
register := func(a pot.Address, po int) {
addr := pot.RandomAddressAt(a, po)
hive.Register(&BzzAddr{OAddr: addr[:]})
discPeer := newDiscPeer(a)
hive.Register(discPeer.BzzAddr)
}

// generate connected and just registered peers
Expand Down Expand Up @@ -238,20 +235,21 @@ func testSortPeers(peers []*BzzAddr) []*BzzAddr {
// as we are not creating a real node via the protocol,
// we need to create the discovery peer objects for the additional kademlia
// nodes manually
func newDiscPeer(addr pot.Address) (*Peer, error) {
pKey, err := ecdsa.GenerateKey(crypto.S256(), crand.Reader)
if err != nil {
return nil, err
}
func newDiscPeer(addr pot.Address) *Peer {

// deterministically create enode id
// Input to the non-random input buffer is 2xaddress since it munches 256 bits
addrSeed := append(addr.Bytes(), addr.Bytes()...)
pKey, _ := ecdsa.GenerateKey(crypto.S256(), bytes.NewBuffer(addrSeed))
pubKey := pKey.PublicKey
nod := enode.NewV4(&pubKey, net.IPv4(127, 0, 0, 1), 0, 0)
bzzAddr := &BzzAddr{OAddr: addr[:], UAddr: []byte(nod.String())}
bzzAddr := NewBzzAddr(addr[:], []byte(nod.String()))
id := nod.ID()
p2pPeer := p2p.NewPeer(id, id.String(), nil)
return NewPeer(&BzzPeer{
Peer: protocols.NewPeer(p2pPeer, &dummyMsgRW{}, DiscoverySpec),
BzzAddr: bzzAddr,
}, nil), nil
}, nil)
}

type dummyMsgRW struct{}
Expand Down
20 changes: 4 additions & 16 deletions network/enr.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,26 @@ func (b *ENRAddrEntry) DecodeRLP(s *rlp.Stream) error {
return nil
}

type ENRLightNodeEntry bool

func (b ENRLightNodeEntry) ENRKey() string {
return "bzzlightnode"
}

type ENRBootNodeEntry bool

func (b ENRBootNodeEntry) ENRKey() string {
return "bzzbootnode"
}

func getENRBzzPeer(p *p2p.Peer, rw p2p.MsgReadWriter, spec *protocols.Spec) *BzzPeer {
var lightnode ENRLightNodeEntry
var bootnode ENRBootNodeEntry

// retrieve the ENR Record data
record := p.Node().Record()
record.Load(&lightnode)
record.Load(&bootnode)

// get the address; separate function as long as we need swarm/network:NewAddr() to call it
// get the address; separate function as long as we need swarm/network:NewBzzAddrFromEnode() to call it
addr := getENRBzzAddr(p.Node())

// build the peer using the retrieved data
return &BzzPeer{
Peer: protocols.NewPeer(p, rw, spec),
LightNode: bool(lightnode),
BzzAddr: addr,
Peer: protocols.NewPeer(p, rw, spec),
BzzAddr: addr,
}
}

Expand All @@ -86,8 +77,5 @@ func getENRBzzAddr(nod *enode.Node) *BzzAddr {
record := nod.Record()
record.Load(&addr)

return &BzzAddr{
OAddr: addr.data,
UAddr: []byte(nod.String()),
}
return NewBzzAddr(addr.data, []byte(nod.String()))
}
3 changes: 2 additions & 1 deletion network/hive.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ func (h *Hive) PeerInfo(id enode.ID) interface{} {
if p == nil {
return nil
}
addr := NewAddr(p.Node())
// TODO this is bogus, the overlay address will not be correct
addr := NewBzzAddrFromEnode(p.Node())
return struct {
OAddr hexutil.Bytes
UAddr hexutil.Bytes
Expand Down
8 changes: 4 additions & 4 deletions network/hive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestRegisterAndConnect(t *testing.T) {
}

node := s.Nodes[0]
raddr := NewAddr(node)
raddr := NewBzzAddrFromEnode(node)
pp.Register(raddr)

// start the hive
Expand Down Expand Up @@ -100,15 +100,15 @@ func TestRegisterAndConnect(t *testing.T) {
}
}

// TestHiveStatePersistance creates a protocol simulation with n peers for a node
// TestHiveStatePersistence creates a protocol simulation with n peers for a node
// After protocols complete, the node is shut down and the state is stored.
// Another simulation is created, where 0 nodes are created, but where the stored state is passed
// The test succeeds if all the peers from the stored state are known after the protocols of the
// second simulation have completed
//
// Actual connectivity is not in scope for this test, as the peers loaded from state are not known to
// the simulation; the test only verifies that the peers are known to the node
func TestHiveStatePersistance(t *testing.T) {
func TestHiveStatePersistence(t *testing.T) {
dir, err := ioutil.TempDir("", "hive_test_store")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestHiveStatePersistance(t *testing.T) {
h1, cleanup1 := startHive(t, dir)
peers := make(map[string]bool)
for i := 0; i < peersCount; i++ {
raddr := RandomAddr()
raddr := RandomBzzAddr()
h1.Register(raddr)
peers[raddr.String()] = true
}
Expand Down
Loading