From 9680c4a1e94d840ec4981d146267ea949db293f3 Mon Sep 17 00:00:00 2001 From: nolash Date: Fri, 30 Aug 2019 15:58:06 +0200 Subject: [PATCH 01/27] network: Cherrypick changes to kademlia for capabilities db --- network/discovery_test.go | 3 ++- network/kademlia.go | 5 +++-- network/kademlia_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/network/discovery_test.go b/network/discovery_test.go index 6e191aa56b..2550487048 100644 --- a/network/discovery_test.go +++ b/network/discovery_test.go @@ -121,7 +121,8 @@ func testInitialPeersMsg(t *testing.T, peerPO, peerDepth int) { return addrs } register := func(a pot.Address, po int) { - discPeer := newDiscPeer(a) + addr := pot.RandomAddressAt(a, po) + discPeer := newDiscPeer(addr) hive.Register(discPeer.BzzAddr) } diff --git a/network/kademlia.go b/network/kademlia.go index d16eac967a..fc9c3149ec 100644 --- a/network/kademlia.go +++ b/network/kademlia.go @@ -524,8 +524,9 @@ func (k *Kademlia) Off(p *Peer) { k.setNeighbourhoodDepth() } -// EachConnFiltered performs the same action as EachConn -// with the difference that it will only return peers that matches the specified capability index filter +// EachConn is an iterator with args (base, po, f) applies f to each live peer +// that has proximity order po or less as measured from the base +// if base is nil, kademlia base address is used func (k *Kademlia) EachConnFiltered(base []byte, capKey string, o int, f func(*Peer, int) bool) error { k.lock.RLock() defer k.lock.RUnlock() diff --git a/network/kademlia_test.go b/network/kademlia_test.go index b751b61552..33c965964f 100644 --- a/network/kademlia_test.go +++ b/network/kademlia_test.go @@ -23,6 +23,7 @@ import ( "time" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/enode" @@ -943,3 +944,26 @@ func testCapabilityIndexRemove(t *testing.T) { t.Fatalf("EachAddrFiltered '42:101' expected 1 peer, got %d", c) } } + +func NewAdaptivePeer(k *Kademlia, capabilities ...*capability.Capability) (*Peer, error) { + // create the peer that fits the kademlia record + // it's quite a bit of work + peerPrivKey, err := crypto.GenerateKey() + if err != nil { + return nil, err + } + peerEnodeId := enode.PubkeyToIDV4(&peerPrivKey.PublicKey) + peerP2p := p2p.NewPeer(peerEnodeId, "foo", []p2p.Cap{}) + peerProto := protocols.NewPeer(peerP2p, nil, nil) + peerBzz := NewBzzPeer(peerProto) + caps := capability.NewCapabilities() + for _, cap := range capabilities { + caps.Add(cap) + } + peerBzz.WithCapabilities(caps) + err = k.Register(peerBzz.BzzAddr) + if err != nil { + return nil, err + } + return NewPeer(peerBzz, k), nil +} From b8326958ea8a603b14133f950ea591f32e3eca5c Mon Sep 17 00:00:00 2001 From: nolash Date: Fri, 30 Aug 2019 20:32:26 +0200 Subject: [PATCH 02/27] network: WIP rehabilitate tests --- network/networkid_test.go | 3 +-- pss/forwarding_test.go | 2 ++ pss/pss_test.go | 2 ++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/network/networkid_test.go b/network/networkid_test.go index 95ace13f5d..614092f634 100644 --- a/network/networkid_test.go +++ b/network/networkid_test.go @@ -32,7 +32,6 @@ import ( "github.com/ethereum/go-ethereum/p2p/simulations" "github.com/ethereum/go-ethereum/p2p/simulations/adapters" "github.com/ethereum/go-ethereum/rpc" - "github.com/ethersphere/swarm/network/capability" "github.com/ethersphere/swarm/testutil" ) @@ -199,7 +198,7 @@ func newServices() adapters.Services { } return adapters.Services{ "bzz": func(ctx *adapters.ServiceContext) (node.Service, error) { - addr := NewBzzAddrFromEnode(ctx.Config.Node()).WithCapabilities(capability.NewCapabilities()) + addr := NewBzzAddrFromEnode(ctx.Config.Node()) hp := NewHiveParams() hp.Discovery = false cnt++ diff --git a/pss/forwarding_test.go b/pss/forwarding_test.go index ea56c489c4..eefbfce415 100644 --- a/pss/forwarding_test.go +++ b/pss/forwarding_test.go @@ -10,6 +10,7 @@ import ( "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethersphere/swarm/network" + "github.com/ethersphere/swarm/network/capability" "github.com/ethersphere/swarm/p2p/protocols" "github.com/ethersphere/swarm/pot" "github.com/ethersphere/swarm/pss/message" @@ -319,6 +320,7 @@ func testForwardMsg(t *testing.T, ps *Pss, c *testCase) { func addPeers(kad *network.Kademlia, addresses []pot.Address) { for _, a := range addresses { p := newTestDiscoveryPeer(a, kad) + p.BzzAddr.Capabilities = capability.NewCapabilities() kad.On(p) } } diff --git a/pss/pss_test.go b/pss/pss_test.go index e6624a3f16..3998ff8bd7 100644 --- a/pss/pss_test.go +++ b/pss/pss_test.go @@ -40,6 +40,7 @@ import ( "github.com/ethereum/go-ethereum/p2p/simulations/adapters" "github.com/ethereum/go-ethereum/rpc" "github.com/ethersphere/swarm/network" + "github.com/ethersphere/swarm/network/capability" "github.com/ethersphere/swarm/network/simulation" "github.com/ethersphere/swarm/p2p/protocols" "github.com/ethersphere/swarm/pot" @@ -195,6 +196,7 @@ func TestAddressMatchProx(t *testing.T) { BzzAddr: network.NewBzzAddr(peerAddr.Bytes(), []byte(fmt.Sprintf("%x", peerAddr[:]))), } peer := network.NewPeer(bzzPeer, kad) + peer.Capabilities = capability.NewCapabilities() kad.On(peer) peers = append(peers, peer) } From 336efda09d88b755bd673ee240950dd3778dcf07 Mon Sep 17 00:00:00 2001 From: nolash Date: Mon, 2 Sep 2019 15:59:32 +0200 Subject: [PATCH 03/27] network, pss: Add capabilities to discovery message (WIP) --- network/discovery.go | 19 ++++++++++++++++--- network/hive_test.go | 2 -- network/kademlia.go | 3 +++ network/network.go | 16 ++++++++++++++-- network/protocol.go | 6 ++++-- pss/forwarding_test.go | 3 +-- pss/pss_test.go | 2 -- 7 files changed, 38 insertions(+), 13 deletions(-) diff --git a/network/discovery.go b/network/discovery.go index 87a55e3eb6..10f139f6d8 100644 --- a/network/discovery.go +++ b/network/discovery.go @@ -23,6 +23,7 @@ import ( "github.com/ethereum/go-ethereum/rlp" "github.com/ethersphere/swarm/log" + "github.com/ethersphere/swarm/network/capability" "github.com/ethersphere/swarm/pot" ) @@ -95,7 +96,11 @@ func (d *Peer) NotifyPeer(a *BzzAddr, po uint8) { return } resp := &peersMsg{ - Peers: []*BzzAddr{a}, + Peers: []*BzzAddr{a}, + Capabilities: []*capability.Capabilities{a.capabilities}, + } + if a.capabilities == nil { + log.Crit("tried to send capabilities empty", "msg", resp) } log.Warn("notifypeer", "notify", resp) go d.Send(context.TODO(), resp) @@ -127,7 +132,8 @@ disconnected // used for communicating about known peers // relevant for bootstrapping connectivity and updating peersets type peersMsg struct { - Peers []*BzzAddr + Peers []*BzzAddr + Capabilities []*capability.Capabilities } // DecodeRLP implements rlp.Decoder interface @@ -206,7 +212,14 @@ func (d *Peer) handleSubPeersMsg(msg *subPeersMsg) error { }) // if useful peers are found, send them over if len(peers) > 0 { - go d.Send(context.TODO(), &peersMsg{Peers: sortPeers(peers)}) + outMsg := &peersMsg{Peers: sortPeers(peers), Capabilities: []*capability.Capabilities{}} + for _, p := range peers { + if p.capabilities == nil { + log.Crit("attempting to send handlesub with cap nil", outMsg) + } + outMsg.Capabilities = append(outMsg.Capabilities, p.capabilities) + } + go d.Send(context.TODO(), outMsg) } } d.sentPeers = true diff --git a/network/hive_test.go b/network/hive_test.go index 670d1727f0..2ce97d1e51 100644 --- a/network/hive_test.go +++ b/network/hive_test.go @@ -17,7 +17,6 @@ package network import ( - "io/ioutil" "os" "testing" "time" @@ -109,7 +108,6 @@ func TestRegisterAndConnect(t *testing.T) { // 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 TestHiveStatePersistence(t *testing.T) { - dir, err := ioutil.TempDir("", "hive_test_store") if err != nil { t.Fatal(err) } diff --git a/network/kademlia.go b/network/kademlia.go index fc9c3149ec..ada8358d81 100644 --- a/network/kademlia.go +++ b/network/kademlia.go @@ -255,6 +255,9 @@ func (k *Kademlia) Register(peers ...*BzzAddr) error { var size int for _, p := range peers { + if p.capabilities == nil { + panic("missing caps for peer") + } log.Trace("kademlia trying to register", "addr", p) // error if self received, peer should know better // and should be punished for this diff --git a/network/network.go b/network/network.go index a0aae05141..4254cbbf23 100644 --- a/network/network.go +++ b/network/network.go @@ -17,7 +17,15 @@ import ( type BzzAddr struct { OAddr []byte UAddr []byte - Capabilities *capability.Capabilities + capabilities *capability.Capabilities +} + +func NewBzzAddr(oaddr []byte, uaddr []byte) *BzzAddr { + return &BzzAddr{ + OAddr: oaddr, + UAddr: uaddr, + capabilities: capability.NewCapabilities(), + } } // EncodeRLP implements rlp.Encoder @@ -90,6 +98,10 @@ func (a *BzzAddr) Under() []byte { return a.UAddr } +func (a *BzzAddr) Capabilities() *capability.Capabilities { + return a.capabilities +} + // ID returns the node identifier in the underlay. func (a *BzzAddr) ID() enode.ID { n, err := enode.ParseV4(string(a.UAddr)) @@ -129,7 +141,7 @@ func NewBzzAddrFromEnode(enod *enode.Node) *BzzAddr { // WithCapabilities is a chained constructor method to set the capabilities array for a BzzAddr func (b *BzzAddr) WithCapabilities(c *capability.Capabilities) *BzzAddr { - b.Capabilities = c + b.capabilities = c return b } diff --git a/network/protocol.go b/network/protocol.go index 6721c6249f..49df4d6165 100644 --- a/network/protocol.go +++ b/network/protocol.go @@ -155,9 +155,9 @@ func NewBzz(config *BzzConfig, kad *Kademlia, store state.Store, streamerSpec, r // temporary soon-to-be-legacy light/full, as above if config.LightNode { - bzz.localAddr.Capabilities.Add(newLightCapability()) + bzz.localAddr.capabilities.Add(newLightCapability()) } else { - bzz.localAddr.Capabilities.Add(newFullCapability()) + bzz.localAddr.capabilities.Add(newFullCapability()) } return bzz @@ -285,6 +285,7 @@ func (b *Bzz) performHandshake(p *protocols.Peer, handshake *HandshakeMsg) error handshake.err = err return err } + rsh.(*HandshakeMsg).Addr.capabilities = rsh.(*HandshakeMsg).Capabilities handshake.peerAddr = rsh.(*HandshakeMsg).Addr return nil } @@ -373,6 +374,7 @@ func (b *Bzz) checkHandshake(hs interface{}) error { if !isFullCapability(rhs.Addr.Capabilities.Get(0)) && !isLightCapability(rhs.Addr.Capabilities.Get(0)) { return fmt.Errorf("invalid capabilities setting: %s", rhs.Addr.Capabilities) } + rhs.Addr.capabilities = hs.(*HandshakeMsg).Capabilities return nil } diff --git a/pss/forwarding_test.go b/pss/forwarding_test.go index eefbfce415..12dd7ea814 100644 --- a/pss/forwarding_test.go +++ b/pss/forwarding_test.go @@ -10,7 +10,6 @@ import ( "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethersphere/swarm/network" - "github.com/ethersphere/swarm/network/capability" "github.com/ethersphere/swarm/p2p/protocols" "github.com/ethersphere/swarm/pot" "github.com/ethersphere/swarm/pss/message" @@ -320,7 +319,7 @@ func testForwardMsg(t *testing.T, ps *Pss, c *testCase) { func addPeers(kad *network.Kademlia, addresses []pot.Address) { for _, a := range addresses { p := newTestDiscoveryPeer(a, kad) - p.BzzAddr.Capabilities = capability.NewCapabilities() + //p.Addr.SetCapabilities = capability.NewCapabilities() kad.On(p) } } diff --git a/pss/pss_test.go b/pss/pss_test.go index 3998ff8bd7..e6624a3f16 100644 --- a/pss/pss_test.go +++ b/pss/pss_test.go @@ -40,7 +40,6 @@ import ( "github.com/ethereum/go-ethereum/p2p/simulations/adapters" "github.com/ethereum/go-ethereum/rpc" "github.com/ethersphere/swarm/network" - "github.com/ethersphere/swarm/network/capability" "github.com/ethersphere/swarm/network/simulation" "github.com/ethersphere/swarm/p2p/protocols" "github.com/ethersphere/swarm/pot" @@ -196,7 +195,6 @@ func TestAddressMatchProx(t *testing.T) { BzzAddr: network.NewBzzAddr(peerAddr.Bytes(), []byte(fmt.Sprintf("%x", peerAddr[:]))), } peer := network.NewPeer(bzzPeer, kad) - peer.Capabilities = capability.NewCapabilities() kad.On(peer) peers = append(peers, peer) } From 1266df677dad2ab5f32afe18939224cf1687595a Mon Sep 17 00:00:00 2001 From: nolash Date: Tue, 3 Sep 2019 11:52:42 +0200 Subject: [PATCH 04/27] network: Add capabilities array to discovery messages tests --- network/discovery_test.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/network/discovery_test.go b/network/discovery_test.go index 2550487048..30930f04ee 100644 --- a/network/discovery_test.go +++ b/network/discovery_test.go @@ -30,6 +30,7 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/enode" + "github.com/ethersphere/swarm/network/capability" "github.com/ethersphere/swarm/p2p/protocols" p2ptest "github.com/ethersphere/swarm/p2p/testing" "github.com/ethersphere/swarm/pot" @@ -120,9 +121,9 @@ func testInitialPeersMsg(t *testing.T, peerPO, peerDepth int) { } return addrs } + //register := func(a pot.Address, po int) { register := func(a pot.Address, po int) { - addr := pot.RandomAddressAt(a, po) - discPeer := newDiscPeer(addr) + discPeer := newDiscPeer(a) hive.Register(discPeer.BzzAddr) } @@ -176,6 +177,11 @@ func testInitialPeersMsg(t *testing.T, peerPO, peerDepth int) { // 1. pivot sends to the control peer a `subPeersMsg` advertising its depth (ignored) // 2. peer sends to pivot a `subPeersMsg` advertising its own depth (arbitrarily chosen) // 3. pivot responds with `peersMsg` with the set of expected peers + var cps []*capability.Capabilities + for _, p := range expBzzAddrs { + cps = append(cps, p.capabilities) + } + err = s.TestExchanges( p2ptest.Exchange{ Label: "outgoing subPeersMsg", @@ -199,7 +205,7 @@ func testInitialPeersMsg(t *testing.T, peerPO, peerDepth int) { Expects: []p2ptest.Expect{ { Code: 0, - Msg: &peersMsg{Peers: testSortPeers(expBzzAddrs)}, + Msg: &peersMsg{Peers: testSortPeers(expBzzAddrs), Capabilities: cps}, Peer: peerID, Timeout: 100 * time.Millisecond, }, @@ -237,7 +243,6 @@ func testSortPeers(peers []*BzzAddr) []*BzzAddr { // we need to create the discovery peer objects for the additional kademlia // nodes manually 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()...) From e355d4cce33347fda27d0386ba6775d517ba6c7c Mon Sep 17 00:00:00 2001 From: nolash Date: Tue, 3 Sep 2019 13:41:48 +0200 Subject: [PATCH 05/27] network: Clean up code and enforce constructors for BzzAddr --- network/discovery.go | 8 -------- network/discovery_test.go | 1 - network/kademlia.go | 3 --- 3 files changed, 12 deletions(-) diff --git a/network/discovery.go b/network/discovery.go index 10f139f6d8..28a734d795 100644 --- a/network/discovery.go +++ b/network/discovery.go @@ -22,7 +22,6 @@ import ( "sync" "github.com/ethereum/go-ethereum/rlp" - "github.com/ethersphere/swarm/log" "github.com/ethersphere/swarm/network/capability" "github.com/ethersphere/swarm/pot" ) @@ -99,10 +98,6 @@ func (d *Peer) NotifyPeer(a *BzzAddr, po uint8) { Peers: []*BzzAddr{a}, Capabilities: []*capability.Capabilities{a.capabilities}, } - if a.capabilities == nil { - log.Crit("tried to send capabilities empty", "msg", resp) - } - log.Warn("notifypeer", "notify", resp) go d.Send(context.TODO(), resp) } @@ -214,9 +209,6 @@ func (d *Peer) handleSubPeersMsg(msg *subPeersMsg) error { if len(peers) > 0 { outMsg := &peersMsg{Peers: sortPeers(peers), Capabilities: []*capability.Capabilities{}} for _, p := range peers { - if p.capabilities == nil { - log.Crit("attempting to send handlesub with cap nil", outMsg) - } outMsg.Capabilities = append(outMsg.Capabilities, p.capabilities) } go d.Send(context.TODO(), outMsg) diff --git a/network/discovery_test.go b/network/discovery_test.go index 30930f04ee..cc92546a76 100644 --- a/network/discovery_test.go +++ b/network/discovery_test.go @@ -121,7 +121,6 @@ func testInitialPeersMsg(t *testing.T, peerPO, peerDepth int) { } return addrs } - //register := func(a pot.Address, po int) { register := func(a pot.Address, po int) { discPeer := newDiscPeer(a) hive.Register(discPeer.BzzAddr) diff --git a/network/kademlia.go b/network/kademlia.go index ada8358d81..fc9c3149ec 100644 --- a/network/kademlia.go +++ b/network/kademlia.go @@ -255,9 +255,6 @@ func (k *Kademlia) Register(peers ...*BzzAddr) error { var size int for _, p := range peers { - if p.capabilities == nil { - panic("missing caps for peer") - } log.Trace("kademlia trying to register", "addr", p) // error if self received, peer should know better // and should be punished for this From b30b40dba43c40c759dea362f60ba41c838ffb43 Mon Sep 17 00:00:00 2001 From: nolash Date: Wed, 4 Sep 2019 09:29:47 +0200 Subject: [PATCH 06/27] network, pot, pss: Rename RandomAddr to explicit RandomBzzAddr --- network/discovery_test.go | 4 ++-- network/kademlia_test.go | 4 ++-- network/protocol_test.go | 2 +- pot/address.go | 10 +++++----- pot/pot_test.go | 4 ++-- pss/forwarding_test.go | 18 +++++++++--------- pss/prox_test.go | 2 +- pss/pss_test.go | 8 ++++---- 8 files changed, 26 insertions(+), 26 deletions(-) diff --git a/network/discovery_test.go b/network/discovery_test.go index cc92546a76..fb66ddae4c 100644 --- a/network/discovery_test.go +++ b/network/discovery_test.go @@ -105,7 +105,7 @@ func testInitialPeersMsg(t *testing.T, peerPO, peerDepth int) { sortPeers = testSortPeers pivotAddr := pot.NewAddressFromBytes(PrivateKeyToBzzKey(prvkey)) // generate control peers address at peerPO wrt pivot - peerAddr := pot.RandomAddressAt(pivotAddr, peerPO) + peerAddr := pot.RandomBzzAddressAt(pivotAddr, peerPO) // construct kademlia and hive to := NewKademlia(pivotAddr[:], NewKadParams()) hive := NewHive(NewHiveParams(), to, nil) @@ -115,7 +115,7 @@ 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 := newDiscPeer(pot.RandomAddressAt(a, po)) + peer := newDiscPeer(pot.RandomBzzAddressAt(a, po)) hive.On(peer) addrs = append(addrs, peer.BzzAddr) } diff --git a/network/kademlia_test.go b/network/kademlia_test.go index 33c965964f..371f29d66f 100644 --- a/network/kademlia_test.go +++ b/network/kademlia_test.go @@ -105,12 +105,12 @@ func TestNeighbourhoodDepth(t *testing.T) { // generate the peers var peers []*Peer for i := 0; i < 7; i++ { - addr := pot.RandomAddressAt(baseAddress, i) + addr := pot.RandomBzzAddressAt(baseAddress, i) peers = append(peers, newTestDiscoveryPeer(addr, kad)) } var sevenPeers []*Peer for i := 0; i < 2; i++ { - addr := pot.RandomAddressAt(baseAddress, 7) + addr := pot.RandomBzzAddressAt(baseAddress, 7) sevenPeers = append(sevenPeers, newTestDiscoveryPeer(addr, kad)) } diff --git a/network/protocol_test.go b/network/protocol_test.go index f91e1725ca..33f697ce09 100644 --- a/network/protocol_test.go +++ b/network/protocol_test.go @@ -91,7 +91,7 @@ func newBzzHandshakeMsg(version uint64, networkId uint64, addr *BzzAddr, lightNo func newBzzBaseTester(n int, prvkey *ecdsa.PrivateKey, spec *protocols.Spec, run func(*BzzPeer) error) (*bzzTester, error) { var addrs [][]byte for i := 0; i < n; i++ { - addr := pot.RandomAddress() + addr := pot.RandomBzzAddress() addrs = append(addrs, addr[:]) } pt, _, err := newBzzBaseTesterWithAddrs(prvkey, addrs, spec, run) diff --git a/pot/address.go b/pot/address.go index d7bed6c46e..00e21eec71 100644 --- a/pot/address.go +++ b/pot/address.go @@ -134,10 +134,10 @@ func ProxCmp(a, x, y []byte) int { return 0 } -// RandomAddressAt (address, prox) generates a random address +// RandomBzzAddressAt (address, prox) generates a random address // at proximity order prox relative to address // if prox is negative a random address is generated -func RandomAddressAt(self Address, prox int) (addr Address) { +func RandomBzzAddressAt(self Address, prox int) (addr Address) { addr = self pos := -1 if prox >= 0 { @@ -159,9 +159,9 @@ func RandomAddressAt(self Address, prox int) (addr Address) { return } -// RandomAddress generates a random address -func RandomAddress() Address { - return RandomAddressAt(Address{}, -1) +// RandomBzzAddress generates a random address +func RandomBzzAddress() Address { + return RandomBzzAddressAt(Address{}, -1) } // NewAddressFromString creates a byte slice from a string in binary representation diff --git a/pot/pot_test.go b/pot/pot_test.go index 6189c7607a..39c6e0dc6a 100644 --- a/pot/pot_test.go +++ b/pot/pot_test.go @@ -56,12 +56,12 @@ func (a *testAddr) String() string { } func randomTestAddr(n int, i int) *testAddr { - v := RandomAddress().Bin()[:n] + v := RandomBzzAddress().Bin()[:n] return newTestAddr(v, i) } func randomtestAddr(n int, i int) *testAddr { - v := RandomAddress().Bin()[:n] + v := RandomBzzAddress().Bin()[:n] return newTestAddr(v, i) } diff --git a/pss/forwarding_test.go b/pss/forwarding_test.go index 12dd7ea814..738f7f4969 100644 --- a/pss/forwarding_test.go +++ b/pss/forwarding_test.go @@ -41,15 +41,15 @@ func TestForwardBasic(t *testing.T) { const depth = 10 for i := 0; i <= depth; i++ { // add two peers for each proximity order - a := pot.RandomAddressAt(base, i) + a := pot.RandomBzzAddressAt(base, i) peerAddresses = append(peerAddresses, a) - a = pot.RandomAddressAt(base, i) + a = pot.RandomBzzAddressAt(base, i) peerAddresses = append(peerAddresses, a) } // skip one level, add one peer at one level deeper. // as a result, we will have an edge case of three peers in nearest neighbours' bin. - peerAddresses = append(peerAddresses, pot.RandomAddressAt(base, depth+2)) + peerAddresses = append(peerAddresses, pot.RandomBzzAddressAt(base, depth+2)) kad := network.NewKademlia(base[:], network.NewKadParams()) ps := createPss(t, kad) @@ -78,7 +78,7 @@ func TestForwardBasic(t *testing.T) { for i := 0; i < firstNearest; i++ { // send random messages with proximity orders, corresponding to PO of each bin, // with one peer being closer to the recipient address - a := pot.RandomAddressAt(peerAddresses[i], 64) + a := pot.RandomBzzAddressAt(peerAddresses[i], 64) c = testCase{ name: fmt.Sprintf("Send random to each PO, id: [%d]", i), recipient: a[:], @@ -93,7 +93,7 @@ func TestForwardBasic(t *testing.T) { // send random messages with proximity orders, corresponding to PO of each bin, // with random proximity relative to the recipient address po := i / 2 - a := pot.RandomAddressAt(base, po) + a := pot.RandomBzzAddressAt(base, po) c = testCase{ name: fmt.Sprintf("Send direct to known, id: [%d]", i), recipient: a[:], @@ -106,7 +106,7 @@ func TestForwardBasic(t *testing.T) { for i := firstNearest; i < len(peerAddresses); i++ { // recipient address falls into the nearest neighbours' bin - a := pot.RandomAddressAt(base, i) + a := pot.RandomBzzAddressAt(base, i) c = testCase{ name: fmt.Sprintf("recipient address falls into the nearest neighbours' bin, id: [%d]", i), recipient: a[:], @@ -118,7 +118,7 @@ func TestForwardBasic(t *testing.T) { } // send msg with proximity order much deeper than the deepest nearest neighbour - a2 := pot.RandomAddressAt(base, 77) + a2 := pot.RandomBzzAddressAt(base, 77) c = testCase{ name: "proximity order much deeper than the deepest nearest neighbour", recipient: a2[:], @@ -167,7 +167,7 @@ func TestForwardBasic(t *testing.T) { } // partial address with proximity order deeper than any of the nearest neighbour - a3 := pot.RandomAddressAt(base, part) + a3 := pot.RandomBzzAddressAt(base, part) c = testCase{ name: "partial address with proximity order deeper than any of the nearest neighbour", recipient: a3[:part], @@ -218,7 +218,7 @@ func TestForwardBasic(t *testing.T) { // send random messages with proximity orders, corresponding to PO of each bin, // with different numbers of failed attempts. // msg should be received by only one of the deeper peers. - a := pot.RandomAddressAt(base, po) + a := pot.RandomBzzAddressAt(base, po) c = testCase{ name: fmt.Sprintf("Send direct to known, id: [%d]", i), recipient: a[:], diff --git a/pss/prox_test.go b/pss/prox_test.go index f427559a8e..19421b16f4 100644 --- a/pss/prox_test.go +++ b/pss/prox_test.go @@ -136,7 +136,7 @@ func (td *testData) init(msgCount int) error { } for i := 0; i < msgCount; i++ { - msgAddr := pot.RandomAddress() // we choose message addresses randomly + msgAddr := pot.RandomBzzAddress() // we choose message addresses randomly td.recipientAddresses = append(td.recipientAddresses, msgAddr.Bytes()) smallestPo := 256 var target enode.ID diff --git a/pss/pss_test.go b/pss/pss_test.go index e6624a3f16..f84df04039 100644 --- a/pss/pss_test.go +++ b/pss/pss_test.go @@ -189,7 +189,7 @@ func TestAddressMatchProx(t *testing.T) { rw := &p2p.MsgPipeRW{} ptpPeer := p2p.NewPeer(enode.ID{}, "362436 call me anytime", []p2p.Cap{}) protoPeer := protocols.NewPeer(ptpPeer, rw, &protocols.Spec{}) - peerAddr := pot.RandomAddressAt(localPotAddr, i) + peerAddr := pot.RandomBzzAddressAt(localPotAddr, i) bzzPeer := &network.BzzPeer{ Peer: protoPeer, BzzAddr: network.NewBzzAddr(peerAddr.Bytes(), []byte(fmt.Sprintf("%x", peerAddr[:]))), @@ -270,7 +270,7 @@ func TestAddressMatchProx(t *testing.T) { // test the distances var prevReceive int for i, distance := range remoteDistances { - remotePotAddr := pot.RandomAddressAt(localPotAddr, distance) + remotePotAddr := pot.RandomBzzAddressAt(localPotAddr, distance) remoteAddr := remotePotAddr.Bytes() var data [32]byte @@ -299,7 +299,7 @@ func TestAddressMatchProx(t *testing.T) { receives = 0 prevReceive = 0 for i, distance := range remoteDistances { - remotePotAddr := pot.RandomAddressAt(localPotAddr, distance) + remotePotAddr := pot.RandomBzzAddressAt(localPotAddr, distance) remoteAddr := remotePotAddr.Bytes() var data [32]byte @@ -323,7 +323,7 @@ func TestAddressMatchProx(t *testing.T) { receives = 0 for _, distance := range remoteDistances { - remotePotAddr := pot.RandomAddressAt(localPotAddr, distance) + remotePotAddr := pot.RandomBzzAddressAt(localPotAddr, distance) remoteAddr := remotePotAddr.Bytes() pssMsg := message.New(message.Flags{Raw: true}) From 7f85493c725fd91154f94af31a2d16c32ea79c5a Mon Sep 17 00:00:00 2001 From: nolash Date: Sun, 8 Sep 2019 14:40:37 +0200 Subject: [PATCH 07/27] network: Custom deserialize in BzzAddr for Capabilities --- network/discovery.go | 12 +++--------- network/discovery_test.go | 8 +------- network/network.go | 30 +++++++++++++++++++++++------- network/protocol.go | 8 ++++---- network/protocol_test.go | 2 +- 5 files changed, 32 insertions(+), 28 deletions(-) diff --git a/network/discovery.go b/network/discovery.go index 28a734d795..262d6d2c12 100644 --- a/network/discovery.go +++ b/network/discovery.go @@ -22,7 +22,6 @@ import ( "sync" "github.com/ethereum/go-ethereum/rlp" - "github.com/ethersphere/swarm/network/capability" "github.com/ethersphere/swarm/pot" ) @@ -95,8 +94,7 @@ func (d *Peer) NotifyPeer(a *BzzAddr, po uint8) { return } resp := &peersMsg{ - Peers: []*BzzAddr{a}, - Capabilities: []*capability.Capabilities{a.capabilities}, + Peers: []*BzzAddr{a}, } go d.Send(context.TODO(), resp) } @@ -127,8 +125,7 @@ disconnected // used for communicating about known peers // relevant for bootstrapping connectivity and updating peersets type peersMsg struct { - Peers []*BzzAddr - Capabilities []*capability.Capabilities + Peers []*BzzAddr } // DecodeRLP implements rlp.Decoder interface @@ -207,10 +204,7 @@ func (d *Peer) handleSubPeersMsg(msg *subPeersMsg) error { }) // if useful peers are found, send them over if len(peers) > 0 { - outMsg := &peersMsg{Peers: sortPeers(peers), Capabilities: []*capability.Capabilities{}} - for _, p := range peers { - outMsg.Capabilities = append(outMsg.Capabilities, p.capabilities) - } + outMsg := &peersMsg{Peers: sortPeers(peers)} go d.Send(context.TODO(), outMsg) } } diff --git a/network/discovery_test.go b/network/discovery_test.go index fb66ddae4c..0279a81124 100644 --- a/network/discovery_test.go +++ b/network/discovery_test.go @@ -30,7 +30,6 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/enode" - "github.com/ethersphere/swarm/network/capability" "github.com/ethersphere/swarm/p2p/protocols" p2ptest "github.com/ethersphere/swarm/p2p/testing" "github.com/ethersphere/swarm/pot" @@ -176,11 +175,6 @@ func testInitialPeersMsg(t *testing.T, peerPO, peerDepth int) { // 1. pivot sends to the control peer a `subPeersMsg` advertising its depth (ignored) // 2. peer sends to pivot a `subPeersMsg` advertising its own depth (arbitrarily chosen) // 3. pivot responds with `peersMsg` with the set of expected peers - var cps []*capability.Capabilities - for _, p := range expBzzAddrs { - cps = append(cps, p.capabilities) - } - err = s.TestExchanges( p2ptest.Exchange{ Label: "outgoing subPeersMsg", @@ -204,7 +198,7 @@ func testInitialPeersMsg(t *testing.T, peerPO, peerDepth int) { Expects: []p2ptest.Expect{ { Code: 0, - Msg: &peersMsg{Peers: testSortPeers(expBzzAddrs), Capabilities: cps}, + Msg: &peersMsg{Peers: testSortPeers(expBzzAddrs)}, Peer: peerID, Timeout: 100 * time.Millisecond, }, diff --git a/network/network.go b/network/network.go index 4254cbbf23..a529de1324 100644 --- a/network/network.go +++ b/network/network.go @@ -17,14 +17,34 @@ import ( type BzzAddr struct { OAddr []byte UAddr []byte - capabilities *capability.Capabilities + Capabilities *capability.Capabilities +} + +func (b *BzzAddr) DecodeRLP(s *rlp.Stream) error { + _, err := s.List() + if err != nil { + return fmt.Errorf("list %v", err) + } + err = s.Decode(&b.OAddr) + if err != nil { + return fmt.Errorf("oaddr %v", err) + } + err = s.Decode(&b.UAddr) + if err != nil { + return fmt.Errorf("uaddr %v", err) + } + err = s.Decode(&b.Capabilities) + if err != nil { + return fmt.Errorf("capz %v", err) + } + return nil } func NewBzzAddr(oaddr []byte, uaddr []byte) *BzzAddr { return &BzzAddr{ OAddr: oaddr, UAddr: uaddr, - capabilities: capability.NewCapabilities(), + Capabilities: capability.NewCapabilities(), } } @@ -98,10 +118,6 @@ func (a *BzzAddr) Under() []byte { return a.UAddr } -func (a *BzzAddr) Capabilities() *capability.Capabilities { - return a.capabilities -} - // ID returns the node identifier in the underlay. func (a *BzzAddr) ID() enode.ID { n, err := enode.ParseV4(string(a.UAddr)) @@ -141,7 +157,7 @@ func NewBzzAddrFromEnode(enod *enode.Node) *BzzAddr { // WithCapabilities is a chained constructor method to set the capabilities array for a BzzAddr func (b *BzzAddr) WithCapabilities(c *capability.Capabilities) *BzzAddr { - b.capabilities = c + b.Capabilities = c return b } diff --git a/network/protocol.go b/network/protocol.go index 49df4d6165..2f02d45331 100644 --- a/network/protocol.go +++ b/network/protocol.go @@ -155,9 +155,9 @@ func NewBzz(config *BzzConfig, kad *Kademlia, store state.Store, streamerSpec, r // temporary soon-to-be-legacy light/full, as above if config.LightNode { - bzz.localAddr.capabilities.Add(newLightCapability()) + bzz.localAddr.Capabilities.Add(newLightCapability()) } else { - bzz.localAddr.capabilities.Add(newFullCapability()) + bzz.localAddr.Capabilities.Add(newFullCapability()) } return bzz @@ -285,7 +285,7 @@ func (b *Bzz) performHandshake(p *protocols.Peer, handshake *HandshakeMsg) error handshake.err = err return err } - rsh.(*HandshakeMsg).Addr.capabilities = rsh.(*HandshakeMsg).Capabilities + rsh.(*HandshakeMsg).Addr.Capabilities = rsh.(*HandshakeMsg).Addr.Capabilities handshake.peerAddr = rsh.(*HandshakeMsg).Addr return nil } @@ -374,7 +374,7 @@ func (b *Bzz) checkHandshake(hs interface{}) error { if !isFullCapability(rhs.Addr.Capabilities.Get(0)) && !isLightCapability(rhs.Addr.Capabilities.Get(0)) { return fmt.Errorf("invalid capabilities setting: %s", rhs.Addr.Capabilities) } - rhs.Addr.capabilities = hs.(*HandshakeMsg).Capabilities + rhs.Addr.Capabilities = hs.(*HandshakeMsg).Addr.Capabilities return nil } diff --git a/network/protocol_test.go b/network/protocol_test.go index 33f697ce09..4a07765093 100644 --- a/network/protocol_test.go +++ b/network/protocol_test.go @@ -404,7 +404,7 @@ func TestBzzHandshakeLightNode(t *testing.T) { select { case <-pt.bzz.handshakes[node.ID()].done: - for _, cp := range pt.bzz.handshakes[node.ID()].peerAddr.Capabilities.Caps { + for _, cp := range pt.bzz.handshakes[node.ID()].Addr.Capabilities.Caps { if cp.String() != nodeCapability.String() { t.Fatalf("peer LightNode flag is %v, should be %v", cp.String(), nodeCapability.String()) } From 152ec4ca8099bc3bef505a714d226a6cbc494649 Mon Sep 17 00:00:00 2001 From: nolash Date: Sun, 8 Sep 2019 15:24:53 +0200 Subject: [PATCH 08/27] network: Add serialization tests --- network/network.go | 8 ++++---- network/protocol.go | 24 ++++++++++++++++++++++-- network/protocol_test.go | 2 +- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/network/network.go b/network/network.go index a529de1324..c6f1cea4db 100644 --- a/network/network.go +++ b/network/network.go @@ -23,19 +23,19 @@ type BzzAddr struct { func (b *BzzAddr) DecodeRLP(s *rlp.Stream) error { _, err := s.List() if err != nil { - return fmt.Errorf("list %v", err) + return fmt.Errorf("list --- %v", err) } err = s.Decode(&b.OAddr) if err != nil { - return fmt.Errorf("oaddr %v", err) + return fmt.Errorf("oaddr --- %v", err) } err = s.Decode(&b.UAddr) if err != nil { - return fmt.Errorf("uaddr %v", err) + return fmt.Errorf("uaddr --- %v", err) } err = s.Decode(&b.Capabilities) if err != nil { - return fmt.Errorf("capz %v", err) + return fmt.Errorf("capz --- %v", err) } return nil } diff --git a/network/protocol.go b/network/protocol.go index 2f02d45331..800f3d4d31 100644 --- a/network/protocol.go +++ b/network/protocol.go @@ -26,6 +26,7 @@ import ( "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/enode" + "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc" "github.com/ethersphere/swarm/log" "github.com/ethersphere/swarm/network/capability" @@ -285,7 +286,6 @@ func (b *Bzz) performHandshake(p *protocols.Peer, handshake *HandshakeMsg) error handshake.err = err return err } - rsh.(*HandshakeMsg).Addr.Capabilities = rsh.(*HandshakeMsg).Addr.Capabilities handshake.peerAddr = rsh.(*HandshakeMsg).Addr return nil } @@ -356,6 +356,26 @@ type HandshakeMsg struct { err error } +func (bh *HandshakeMsg) DecodeRLP(s *rlp.Stream) error { + _, err := s.List() + if err != nil { + return fmt.Errorf("list --- %v", err) + } + err = s.Decode(&bh.Version) + if err != nil { + return fmt.Errorf("version --- %v", err) + } + err = s.Decode(&bh.NetworkID) + if err != nil { + return fmt.Errorf("networkid --- %v", err) + } + err = s.Decode(&bh.Addr) + if err != nil { + return fmt.Errorf("addr --- %v", err) + } + return nil +} + // String pretty prints the handshake func (bh *HandshakeMsg) String() string { return fmt.Sprintf("Handshake: Version: %v, NetworkID: %v, Addr: %v, peerAddr: %v", bh.Version, bh.NetworkID, bh.Addr, bh.peerAddr) @@ -374,7 +394,7 @@ func (b *Bzz) checkHandshake(hs interface{}) error { if !isFullCapability(rhs.Addr.Capabilities.Get(0)) && !isLightCapability(rhs.Addr.Capabilities.Get(0)) { return fmt.Errorf("invalid capabilities setting: %s", rhs.Addr.Capabilities) } - rhs.Addr.Capabilities = hs.(*HandshakeMsg).Addr.Capabilities + //rhs.Addr.Capabilities = hs.(*HandshakeMsg).Addr.Capabilities return nil } diff --git a/network/protocol_test.go b/network/protocol_test.go index 4a07765093..33f697ce09 100644 --- a/network/protocol_test.go +++ b/network/protocol_test.go @@ -404,7 +404,7 @@ func TestBzzHandshakeLightNode(t *testing.T) { select { case <-pt.bzz.handshakes[node.ID()].done: - for _, cp := range pt.bzz.handshakes[node.ID()].Addr.Capabilities.Caps { + for _, cp := range pt.bzz.handshakes[node.ID()].peerAddr.Capabilities.Caps { if cp.String() != nodeCapability.String() { t.Fatalf("peer LightNode flag is %v, should be %v", cp.String(), nodeCapability.String()) } From 6de5a42325e9ecf7e89e354ac391887d12715680 Mon Sep 17 00:00:00 2001 From: nolash Date: Mon, 9 Sep 2019 15:51:02 +0200 Subject: [PATCH 09/27] network: Temporary workaround for contingent list in BzzAddr deserialize --- network/network.go | 37 +++++++++++++++++++++++++++++++++---- network/protocol.go | 21 --------------------- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/network/network.go b/network/network.go index c6f1cea4db..7b98ecd3b1 100644 --- a/network/network.go +++ b/network/network.go @@ -20,10 +20,33 @@ type BzzAddr struct { Capabilities *capability.Capabilities } +func (b *BzzAddr) EncodeRLP(w io.Writer) error { + err := rlp.Encode(w, b.OAddr) + if err != nil { + return err + } + err = rlp.Encode(w, b.UAddr) + if err != nil { + return err + } + y, err := rlp.EncodeToBytes(b.Capabilities) + if err != nil { + return err + } + err = rlp.Encode(w, y) + if err != nil { + return err + } + return nil +} + func (b *BzzAddr) DecodeRLP(s *rlp.Stream) error { - _, err := s.List() + knd, _, err := s.Kind() if err != nil { - return fmt.Errorf("list --- %v", err) + return err + } + if knd == rlp.List { + s.List() } err = s.Decode(&b.OAddr) if err != nil { @@ -33,9 +56,15 @@ func (b *BzzAddr) DecodeRLP(s *rlp.Stream) error { if err != nil { return fmt.Errorf("uaddr --- %v", err) } - err = s.Decode(&b.Capabilities) + + var y []byte + err = s.Decode(&y) + if err != nil { + return fmt.Errorf("capzbytes --- %v", err) + } + err = rlp.DecodeBytes(y, &b.Capabilities) if err != nil { - return fmt.Errorf("capz --- %v", err) + return fmt.Errorf("capzdecode --- %v", err) } return nil } diff --git a/network/protocol.go b/network/protocol.go index 800f3d4d31..70f8146364 100644 --- a/network/protocol.go +++ b/network/protocol.go @@ -26,7 +26,6 @@ import ( "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/enode" - "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc" "github.com/ethersphere/swarm/log" "github.com/ethersphere/swarm/network/capability" @@ -356,26 +355,6 @@ type HandshakeMsg struct { err error } -func (bh *HandshakeMsg) DecodeRLP(s *rlp.Stream) error { - _, err := s.List() - if err != nil { - return fmt.Errorf("list --- %v", err) - } - err = s.Decode(&bh.Version) - if err != nil { - return fmt.Errorf("version --- %v", err) - } - err = s.Decode(&bh.NetworkID) - if err != nil { - return fmt.Errorf("networkid --- %v", err) - } - err = s.Decode(&bh.Addr) - if err != nil { - return fmt.Errorf("addr --- %v", err) - } - return nil -} - // String pretty prints the handshake func (bh *HandshakeMsg) String() string { return fmt.Sprintf("Handshake: Version: %v, NetworkID: %v, Addr: %v, peerAddr: %v", bh.Version, bh.NetworkID, bh.Addr, bh.peerAddr) From 633583e4d4198d1e110716cf5b68e508fbbeb970 Mon Sep 17 00:00:00 2001 From: nolash Date: Tue, 10 Sep 2019 16:28:57 +0200 Subject: [PATCH 10/27] network: BzzAddr deserialize fail on empty object --- network/network.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/network/network.go b/network/network.go index 7b98ecd3b1..208adbd785 100644 --- a/network/network.go +++ b/network/network.go @@ -41,13 +41,13 @@ func (b *BzzAddr) EncodeRLP(w io.Writer) error { } func (b *BzzAddr) DecodeRLP(s *rlp.Stream) error { - knd, _, err := s.Kind() + _, _, err := s.Kind() if err != nil { return err } - if knd == rlp.List { - s.List() - } + // if knd == rlp.List { + // s.List() + // } err = s.Decode(&b.OAddr) if err != nil { return fmt.Errorf("oaddr --- %v", err) From a74293ab7747e40c70118d278b79713f15cb06c8 Mon Sep 17 00:00:00 2001 From: nolash Date: Wed, 11 Sep 2019 09:25:22 +0200 Subject: [PATCH 11/27] network, cmd: WIP debugging --- network/discovery.go | 2 ++ network/network.go | 3 +++ 2 files changed, 5 insertions(+) diff --git a/network/discovery.go b/network/discovery.go index 262d6d2c12..941e1b859f 100644 --- a/network/discovery.go +++ b/network/discovery.go @@ -22,6 +22,7 @@ import ( "sync" "github.com/ethereum/go-ethereum/rlp" + "github.com/ethersphere/swarm/log" "github.com/ethersphere/swarm/pot" ) @@ -96,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) } diff --git a/network/network.go b/network/network.go index 208adbd785..4d849a20dd 100644 --- a/network/network.go +++ b/network/network.go @@ -7,6 +7,7 @@ import ( "net" "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enr" "github.com/ethereum/go-ethereum/rlp" @@ -43,6 +44,8 @@ func (b *BzzAddr) EncodeRLP(w io.Writer) error { func (b *BzzAddr) DecodeRLP(s *rlp.Stream) error { _, _, err := s.Kind() if err != nil { + log.Error("decoderlp bzzaddr err", "err", err) + panic(err) return err } // if knd == rlp.List { From 0dd7aab2c5e83d1fd967bb823948b6cf53048afe Mon Sep 17 00:00:00 2001 From: nolash Date: Wed, 11 Sep 2019 17:16:58 +0200 Subject: [PATCH 12/27] network: Add deserialize in peersMsg to elimiate list error --- network/network.go | 32 +++++++------------------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/network/network.go b/network/network.go index 4d849a20dd..f74504a1aa 100644 --- a/network/network.go +++ b/network/network.go @@ -7,7 +7,6 @@ import ( "net" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enr" "github.com/ethereum/go-ethereum/rlp" @@ -30,11 +29,7 @@ func (b *BzzAddr) EncodeRLP(w io.Writer) error { if err != nil { return err } - y, err := rlp.EncodeToBytes(b.Capabilities) - if err != nil { - return err - } - err = rlp.Encode(w, y) + err = rlp.Encode(w, b.Capabilities) if err != nil { return err } @@ -42,32 +37,19 @@ func (b *BzzAddr) EncodeRLP(w io.Writer) error { } func (b *BzzAddr) DecodeRLP(s *rlp.Stream) error { - _, _, err := s.Kind() - if err != nil { - log.Error("decoderlp bzzaddr err", "err", err) - panic(err) - return err - } - // if knd == rlp.List { - // s.List() - // } - err = s.Decode(&b.OAddr) + var err error + + b.OAddr, err = s.Bytes() if err != nil { return fmt.Errorf("oaddr --- %v", err) } - err = s.Decode(&b.UAddr) + b.UAddr, err = s.Bytes() if err != nil { return fmt.Errorf("uaddr --- %v", err) } - - var y []byte - err = s.Decode(&y) - if err != nil { - return fmt.Errorf("capzbytes --- %v", err) - } - err = rlp.DecodeBytes(y, &b.Capabilities) + err = s.Decode(&b.Capabilities) if err != nil { - return fmt.Errorf("capzdecode --- %v", err) + return fmt.Errorf("caps --- %v", err) } return nil } From a9ef8fd242588ee661483d7cbeaa7859ce4ba7f4 Mon Sep 17 00:00:00 2001 From: nolash Date: Wed, 11 Sep 2019 17:25:37 +0200 Subject: [PATCH 13/27] network: Re-instate serialize capabilites to bytes in BzzAddr --- network/network.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/network/network.go b/network/network.go index f74504a1aa..32cf4c9645 100644 --- a/network/network.go +++ b/network/network.go @@ -29,7 +29,11 @@ func (b *BzzAddr) EncodeRLP(w io.Writer) error { if err != nil { return err } - err = rlp.Encode(w, b.Capabilities) + y, err := rlp.EncodeToBytes(b.Capabilities) + if err != nil { + return err + } + err = rlp.Encode(w, y) if err != nil { return err } @@ -47,7 +51,12 @@ func (b *BzzAddr) DecodeRLP(s *rlp.Stream) error { if err != nil { return fmt.Errorf("uaddr --- %v", err) } - err = s.Decode(&b.Capabilities) + + y, err := s.Bytes() + if err != nil { + return fmt.Errorf("capsbytes --- %v", err) + } + err = rlp.DecodeBytes(y, &b.Capabilities) if err != nil { return fmt.Errorf("caps --- %v", err) } From 31b500e23a7fef264baa31283ece6b62c7e4e9cf Mon Sep 17 00:00:00 2001 From: nolash Date: Wed, 11 Sep 2019 19:22:49 +0200 Subject: [PATCH 14/27] pot: Revert inadvertent rename of RandomAddress* --- network/discovery_test.go | 4 ++-- network/kademlia_test.go | 4 ++-- network/protocol_test.go | 2 +- pot/address.go | 6 +++--- pss/forwarding_test.go | 18 +++++++++--------- pss/prox_test.go | 2 +- pss/pss_test.go | 8 ++++---- 7 files changed, 22 insertions(+), 22 deletions(-) diff --git a/network/discovery_test.go b/network/discovery_test.go index 0279a81124..0bfa94b71b 100644 --- a/network/discovery_test.go +++ b/network/discovery_test.go @@ -104,7 +104,7 @@ func testInitialPeersMsg(t *testing.T, peerPO, peerDepth int) { sortPeers = testSortPeers pivotAddr := pot.NewAddressFromBytes(PrivateKeyToBzzKey(prvkey)) // generate control peers address at peerPO wrt pivot - peerAddr := pot.RandomBzzAddressAt(pivotAddr, peerPO) + peerAddr := pot.RandomAddressAt(pivotAddr, peerPO) // construct kademlia and hive to := NewKademlia(pivotAddr[:], NewKadParams()) hive := NewHive(NewHiveParams(), to, nil) @@ -114,7 +114,7 @@ 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 := newDiscPeer(pot.RandomBzzAddressAt(a, po)) + peer := newDiscPeer(pot.RandomAddressAt(a, po)) hive.On(peer) addrs = append(addrs, peer.BzzAddr) } diff --git a/network/kademlia_test.go b/network/kademlia_test.go index 371f29d66f..33c965964f 100644 --- a/network/kademlia_test.go +++ b/network/kademlia_test.go @@ -105,12 +105,12 @@ func TestNeighbourhoodDepth(t *testing.T) { // generate the peers var peers []*Peer for i := 0; i < 7; i++ { - addr := pot.RandomBzzAddressAt(baseAddress, i) + addr := pot.RandomAddressAt(baseAddress, i) peers = append(peers, newTestDiscoveryPeer(addr, kad)) } var sevenPeers []*Peer for i := 0; i < 2; i++ { - addr := pot.RandomBzzAddressAt(baseAddress, 7) + addr := pot.RandomAddressAt(baseAddress, 7) sevenPeers = append(sevenPeers, newTestDiscoveryPeer(addr, kad)) } diff --git a/network/protocol_test.go b/network/protocol_test.go index 33f697ce09..f91e1725ca 100644 --- a/network/protocol_test.go +++ b/network/protocol_test.go @@ -91,7 +91,7 @@ func newBzzHandshakeMsg(version uint64, networkId uint64, addr *BzzAddr, lightNo func newBzzBaseTester(n int, prvkey *ecdsa.PrivateKey, spec *protocols.Spec, run func(*BzzPeer) error) (*bzzTester, error) { var addrs [][]byte for i := 0; i < n; i++ { - addr := pot.RandomBzzAddress() + addr := pot.RandomAddress() addrs = append(addrs, addr[:]) } pt, _, err := newBzzBaseTesterWithAddrs(prvkey, addrs, spec, run) diff --git a/pot/address.go b/pot/address.go index 00e21eec71..7bb9f7c0d1 100644 --- a/pot/address.go +++ b/pot/address.go @@ -137,7 +137,7 @@ func ProxCmp(a, x, y []byte) int { // RandomBzzAddressAt (address, prox) generates a random address // at proximity order prox relative to address // if prox is negative a random address is generated -func RandomBzzAddressAt(self Address, prox int) (addr Address) { +func RandomAddressAt(self Address, prox int) (addr Address) { addr = self pos := -1 if prox >= 0 { @@ -160,8 +160,8 @@ func RandomBzzAddressAt(self Address, prox int) (addr Address) { } // RandomBzzAddress generates a random address -func RandomBzzAddress() Address { - return RandomBzzAddressAt(Address{}, -1) +func RandomAddress() Address { + return RandomAddressAt(Address{}, -1) } // NewAddressFromString creates a byte slice from a string in binary representation diff --git a/pss/forwarding_test.go b/pss/forwarding_test.go index 738f7f4969..12dd7ea814 100644 --- a/pss/forwarding_test.go +++ b/pss/forwarding_test.go @@ -41,15 +41,15 @@ func TestForwardBasic(t *testing.T) { const depth = 10 for i := 0; i <= depth; i++ { // add two peers for each proximity order - a := pot.RandomBzzAddressAt(base, i) + a := pot.RandomAddressAt(base, i) peerAddresses = append(peerAddresses, a) - a = pot.RandomBzzAddressAt(base, i) + a = pot.RandomAddressAt(base, i) peerAddresses = append(peerAddresses, a) } // skip one level, add one peer at one level deeper. // as a result, we will have an edge case of three peers in nearest neighbours' bin. - peerAddresses = append(peerAddresses, pot.RandomBzzAddressAt(base, depth+2)) + peerAddresses = append(peerAddresses, pot.RandomAddressAt(base, depth+2)) kad := network.NewKademlia(base[:], network.NewKadParams()) ps := createPss(t, kad) @@ -78,7 +78,7 @@ func TestForwardBasic(t *testing.T) { for i := 0; i < firstNearest; i++ { // send random messages with proximity orders, corresponding to PO of each bin, // with one peer being closer to the recipient address - a := pot.RandomBzzAddressAt(peerAddresses[i], 64) + a := pot.RandomAddressAt(peerAddresses[i], 64) c = testCase{ name: fmt.Sprintf("Send random to each PO, id: [%d]", i), recipient: a[:], @@ -93,7 +93,7 @@ func TestForwardBasic(t *testing.T) { // send random messages with proximity orders, corresponding to PO of each bin, // with random proximity relative to the recipient address po := i / 2 - a := pot.RandomBzzAddressAt(base, po) + a := pot.RandomAddressAt(base, po) c = testCase{ name: fmt.Sprintf("Send direct to known, id: [%d]", i), recipient: a[:], @@ -106,7 +106,7 @@ func TestForwardBasic(t *testing.T) { for i := firstNearest; i < len(peerAddresses); i++ { // recipient address falls into the nearest neighbours' bin - a := pot.RandomBzzAddressAt(base, i) + a := pot.RandomAddressAt(base, i) c = testCase{ name: fmt.Sprintf("recipient address falls into the nearest neighbours' bin, id: [%d]", i), recipient: a[:], @@ -118,7 +118,7 @@ func TestForwardBasic(t *testing.T) { } // send msg with proximity order much deeper than the deepest nearest neighbour - a2 := pot.RandomBzzAddressAt(base, 77) + a2 := pot.RandomAddressAt(base, 77) c = testCase{ name: "proximity order much deeper than the deepest nearest neighbour", recipient: a2[:], @@ -167,7 +167,7 @@ func TestForwardBasic(t *testing.T) { } // partial address with proximity order deeper than any of the nearest neighbour - a3 := pot.RandomBzzAddressAt(base, part) + a3 := pot.RandomAddressAt(base, part) c = testCase{ name: "partial address with proximity order deeper than any of the nearest neighbour", recipient: a3[:part], @@ -218,7 +218,7 @@ func TestForwardBasic(t *testing.T) { // send random messages with proximity orders, corresponding to PO of each bin, // with different numbers of failed attempts. // msg should be received by only one of the deeper peers. - a := pot.RandomBzzAddressAt(base, po) + a := pot.RandomAddressAt(base, po) c = testCase{ name: fmt.Sprintf("Send direct to known, id: [%d]", i), recipient: a[:], diff --git a/pss/prox_test.go b/pss/prox_test.go index 19421b16f4..f427559a8e 100644 --- a/pss/prox_test.go +++ b/pss/prox_test.go @@ -136,7 +136,7 @@ func (td *testData) init(msgCount int) error { } for i := 0; i < msgCount; i++ { - msgAddr := pot.RandomBzzAddress() // we choose message addresses randomly + msgAddr := pot.RandomAddress() // we choose message addresses randomly td.recipientAddresses = append(td.recipientAddresses, msgAddr.Bytes()) smallestPo := 256 var target enode.ID diff --git a/pss/pss_test.go b/pss/pss_test.go index f84df04039..e6624a3f16 100644 --- a/pss/pss_test.go +++ b/pss/pss_test.go @@ -189,7 +189,7 @@ func TestAddressMatchProx(t *testing.T) { rw := &p2p.MsgPipeRW{} ptpPeer := p2p.NewPeer(enode.ID{}, "362436 call me anytime", []p2p.Cap{}) protoPeer := protocols.NewPeer(ptpPeer, rw, &protocols.Spec{}) - peerAddr := pot.RandomBzzAddressAt(localPotAddr, i) + peerAddr := pot.RandomAddressAt(localPotAddr, i) bzzPeer := &network.BzzPeer{ Peer: protoPeer, BzzAddr: network.NewBzzAddr(peerAddr.Bytes(), []byte(fmt.Sprintf("%x", peerAddr[:]))), @@ -270,7 +270,7 @@ func TestAddressMatchProx(t *testing.T) { // test the distances var prevReceive int for i, distance := range remoteDistances { - remotePotAddr := pot.RandomBzzAddressAt(localPotAddr, distance) + remotePotAddr := pot.RandomAddressAt(localPotAddr, distance) remoteAddr := remotePotAddr.Bytes() var data [32]byte @@ -299,7 +299,7 @@ func TestAddressMatchProx(t *testing.T) { receives = 0 prevReceive = 0 for i, distance := range remoteDistances { - remotePotAddr := pot.RandomBzzAddressAt(localPotAddr, distance) + remotePotAddr := pot.RandomAddressAt(localPotAddr, distance) remoteAddr := remotePotAddr.Bytes() var data [32]byte @@ -323,7 +323,7 @@ func TestAddressMatchProx(t *testing.T) { receives = 0 for _, distance := range remoteDistances { - remotePotAddr := pot.RandomBzzAddressAt(localPotAddr, distance) + remotePotAddr := pot.RandomAddressAt(localPotAddr, distance) remoteAddr := remotePotAddr.Bytes() pssMsg := message.New(message.Flags{Raw: true}) From d486f48ab0aa0d079a799d99410e8d672fa52ca5 Mon Sep 17 00:00:00 2001 From: nolash Date: Thu, 12 Sep 2019 11:01:22 +0200 Subject: [PATCH 15/27] pot: Remove wrong rename for random address gen in pot test --- pot/pot_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pot/pot_test.go b/pot/pot_test.go index 39c6e0dc6a..6189c7607a 100644 --- a/pot/pot_test.go +++ b/pot/pot_test.go @@ -56,12 +56,12 @@ func (a *testAddr) String() string { } func randomTestAddr(n int, i int) *testAddr { - v := RandomBzzAddress().Bin()[:n] + v := RandomAddress().Bin()[:n] return newTestAddr(v, i) } func randomtestAddr(n int, i int) *testAddr { - v := RandomBzzAddress().Bin()[:n] + v := RandomAddress().Bin()[:n] return newTestAddr(v, i) } From 6af8d12c2226d5a4332902baed9ffe2186b42e3d Mon Sep 17 00:00:00 2001 From: nolash Date: Thu, 12 Sep 2019 13:48:22 +0200 Subject: [PATCH 16/27] network: Remove comments, reinstate skipped hive test --- network/hive_test.go | 2 ++ network/protocol.go | 1 - pss/forwarding_test.go | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/network/hive_test.go b/network/hive_test.go index 2ce97d1e51..670d1727f0 100644 --- a/network/hive_test.go +++ b/network/hive_test.go @@ -17,6 +17,7 @@ package network import ( + "io/ioutil" "os" "testing" "time" @@ -108,6 +109,7 @@ func TestRegisterAndConnect(t *testing.T) { // 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 TestHiveStatePersistence(t *testing.T) { + dir, err := ioutil.TempDir("", "hive_test_store") if err != nil { t.Fatal(err) } diff --git a/network/protocol.go b/network/protocol.go index 70f8146364..6721c6249f 100644 --- a/network/protocol.go +++ b/network/protocol.go @@ -373,7 +373,6 @@ func (b *Bzz) checkHandshake(hs interface{}) error { if !isFullCapability(rhs.Addr.Capabilities.Get(0)) && !isLightCapability(rhs.Addr.Capabilities.Get(0)) { return fmt.Errorf("invalid capabilities setting: %s", rhs.Addr.Capabilities) } - //rhs.Addr.Capabilities = hs.(*HandshakeMsg).Addr.Capabilities return nil } diff --git a/pss/forwarding_test.go b/pss/forwarding_test.go index 12dd7ea814..ea56c489c4 100644 --- a/pss/forwarding_test.go +++ b/pss/forwarding_test.go @@ -319,7 +319,6 @@ func testForwardMsg(t *testing.T, ps *Pss, c *testCase) { func addPeers(kad *network.Kademlia, addresses []pot.Address) { for _, a := range addresses { p := newTestDiscoveryPeer(a, kad) - //p.Addr.SetCapabilities = capability.NewCapabilities() kad.On(p) } } From 5e228bf723443e7d19ca2aa856fe387f90b67d52 Mon Sep 17 00:00:00 2001 From: nolash Date: Fri, 13 Sep 2019 11:44:28 +0200 Subject: [PATCH 17/27] network: Add test for kademlia capability indices --- network/kademlia_test.go | 126 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 122 insertions(+), 4 deletions(-) diff --git a/network/kademlia_test.go b/network/kademlia_test.go index 33c965964f..829e6aa7d2 100644 --- a/network/kademlia_test.go +++ b/network/kademlia_test.go @@ -37,9 +37,14 @@ func init() { log.Root().SetHandler(h) } -func testKadPeerAddr(s string) *BzzAddr { +func testKadPeerAddr(s string, lightNode bool) *BzzAddr { a := pot.NewAddressFromString(s) - return NewBzzAddr(a, a) + bzzAddr := NewBzzAddr(a, a) + if lightNode { + bzzAddr.Capabilities.Add(lightCapability) + } + bzzAddr.Capabilities.Add(fullCapability) + return bzzAddr } func newTestKademliaParams() *KadParams { @@ -54,6 +59,11 @@ type testKademlia struct { t *testing.T } +type testKadAddrSpec struct { + bits string + lightNode bool +} + func newTestKademlia(t *testing.T, b string) *testKademlia { base := pot.NewAddressFromString(b) return &testKademlia{ @@ -79,9 +89,20 @@ func (tk *testKademlia) Off(offs ...string) { } func (tk *testKademlia) Register(regs ...string) { + var specs []testKadAddrSpec + for _, r := range regs { + specs = append(specs, testKadAddrSpec{ + bits: r, + lightNode: false, + }) + } + tk.RegisterWithCapability(specs...) +} + +func (tk *testKademlia) RegisterWithCapability(regs ...testKadAddrSpec) { var as []*BzzAddr - for _, s := range regs { - as = append(as, testKadPeerAddr(s)) + for _, r := range regs { + as = append(as, testKadPeerAddr(r.bits, r.lightNode)) } err := tk.Kademlia.Register(as...) if err != nil { @@ -210,6 +231,103 @@ func TestHighMinBinSize(t *testing.T) { } } +func TestCapabilitiesIndex(t *testing.T) { + kp := NewKadParams() + addr := RandomBzzAddr() + base := addr.OAddr + k := NewKademlia(base, kp) + + testMoreCapability := capability.NewCapability(42, 3) + testMoreCapability.Set(0) + testMoreCapability.Set(2) + k.RegisterCapabilityIndex("more", *testMoreCapability) + + testLessCapability := capability.NewCapability(42, 3) + testLessCapability.Set(2) + k.RegisterCapabilityIndex("less", *testLessCapability) + + testNoneCapability := capability.NewCapability(42, 3) + testNoneCapability.Set(1) + k.RegisterCapabilityIndex("none", *testNoneCapability) + + testOtherCapability := capability.NewCapability(666, 3) + testOtherCapability.Set(0) + testOtherCapability.Set(2) + k.RegisterCapabilityIndex("other", *testOtherCapability) + + moreAddr := RandomBzzAddr() + moreAddr.Capabilities.Add(testMoreCapability) + + lessAddr := RandomBzzAddr() + lessAddr.Capabilities.Add(testLessCapability) + + otherAddr := RandomBzzAddr() + otherAddr.Capabilities.Add(testOtherCapability) + + allAddr := RandomBzzAddr() + allAddr.Capabilities.Add(testOtherCapability) + allAddr.Capabilities.Add(testMoreCapability) + + k.Register(moreAddr, lessAddr, otherAddr, allAddr) + + var addrs []*BzzAddr + k.EachAddr(base, 255, func(a *BzzAddr, _ int) bool { + addrs = append(addrs, a) + return true + }) + if len(addrs) != 4 { + t.Fatalf("EachAddr expected 3 peers, got %d", len(addrs)) + } + + var c int + k.EachAddrFiltered(base, "more", 255, func(a *BzzAddr, _ int) bool { + c++ + cp := a.Capabilities.Get(42) + if !cp.Match(testMoreCapability) { + t.Fatalf("EachAddrFiltered 'more' capability mismatch, expected %v, got %v", testMoreCapability, cp) + } + return true + }) + if c != 2 { + t.Fatalf("EachAddrFiltered 'full' expected 2 peer, got %d", c) + } + + c = 0 + k.EachAddrFiltered(base, "less", 255, func(a *BzzAddr, _ int) bool { + c++ + return true + }) + if c != 3 { + t.Fatalf("EachAddrFiltered 'less' expected 2 peers, got %d", c) + } + + c = 0 + k.EachAddrFiltered(base, "none", 255, func(a *BzzAddr, _ int) bool { + c++ + return true + }) + if c != 0 { + t.Fatalf("EachAddrFiltered 'none' expected 0 peers, got %d", c) + } + + c = 0 + k.EachAddrFiltered(base, "other", 255, func(a *BzzAddr, _ int) bool { + c++ + cp := a.Capabilities.Get(666) + if !cp.Match(testOtherCapability) { + t.Fatalf("EachAddrFiltered 'other' capability mismatch, expected %v, got %v", testOtherCapability, cp) + } + cp = a.Capabilities.Get(42) + if cp != nil { + c++ + } + return true + }) + if c != 3 { + t.Fatalf("EachAddrFiltered 'other' expected 3 capability matches, got %d", c) + } +} + // TestHealthStrict tests the simplest definition of health // Which means whether we are connected to all neighbors we know of func TestHealthStrict(t *testing.T) { From a94933bbd38c289860d19313d1ede8a956342eff Mon Sep 17 00:00:00 2001 From: nolash Date: Fri, 13 Sep 2019 11:56:00 +0200 Subject: [PATCH 18/27] network: Add test for connections capabilities index filtering --- network/kademlia_test.go | 76 ++++++++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 23 deletions(-) diff --git a/network/kademlia_test.go b/network/kademlia_test.go index 829e6aa7d2..e4f8b31dba 100644 --- a/network/kademlia_test.go +++ b/network/kademlia_test.go @@ -37,13 +37,9 @@ func init() { log.Root().SetHandler(h) } -func testKadPeerAddr(s string, lightNode bool) *BzzAddr { +func testKadPeerAddr(s string) *BzzAddr { a := pot.NewAddressFromString(s) bzzAddr := NewBzzAddr(a, a) - if lightNode { - bzzAddr.Capabilities.Add(lightCapability) - } - bzzAddr.Capabilities.Add(fullCapability) return bzzAddr } @@ -59,11 +55,6 @@ type testKademlia struct { t *testing.T } -type testKadAddrSpec struct { - bits string - lightNode bool -} - func newTestKademlia(t *testing.T, b string) *testKademlia { base := pot.NewAddressFromString(b) return &testKademlia{ @@ -89,20 +80,9 @@ func (tk *testKademlia) Off(offs ...string) { } func (tk *testKademlia) Register(regs ...string) { - var specs []testKadAddrSpec - for _, r := range regs { - specs = append(specs, testKadAddrSpec{ - bits: r, - lightNode: false, - }) - } - tk.RegisterWithCapability(specs...) -} - -func (tk *testKademlia) RegisterWithCapability(regs ...testKadAddrSpec) { var as []*BzzAddr - for _, r := range regs { - as = append(as, testKadPeerAddr(r.bits, r.lightNode)) + for _, s := range regs { + as = append(as, testKadPeerAddr(s)) } err := tk.Kademlia.Register(as...) if err != nil { @@ -231,6 +211,7 @@ func TestHighMinBinSize(t *testing.T) { } } +// TestCapabilitiesIndex checks that capability indices contains only the peers that have the filters' capability bits set func TestCapabilitiesIndex(t *testing.T) { kp := NewKadParams() addr := RandomBzzAddr() @@ -326,6 +307,55 @@ func TestCapabilitiesIndex(t *testing.T) { if c != 3 { t.Fatalf("EachAddrFiltered 'other' expected 3 capability matches, got %d", c) } + + allBzzPeer := &BzzPeer{ + BzzAddr: allAddr, + } + allPeer := NewPeer(allBzzPeer, k) + k.On(allPeer) + + c = 0 + k.EachConn(base, 255, func(_ *Peer, _ int) bool { + c++ + return true + }) + if c != 1 { + t.Fatalf("EachConn expected 1 peer, got %d", c) + } + + c = 0 + k.EachConnFiltered(base, "other", 255, func(p *Peer, _ int) bool { + c++ + cp := p.Capabilities.Get(666) + if !cp.Match(testOtherCapability) { + t.Fatalf("EachConnFiltered 'other' missing capability %v", testOtherCapability) + } + cp = p.Capabilities.Get(42) + if !cp.Match(testMoreCapability) { + t.Fatalf("EachConnFiltered 'other' missing capability %v", testMoreCapability) + } + return true + }) + if c != 1 { + t.Fatalf("EachConnFiltered 'other' expected 1 peer, got %d", c) + } + + c = 0 + k.EachConnFiltered(base, "more", 255, func(p *Peer, _ int) bool { + c++ + cp := p.Capabilities.Get(666) + if !cp.Match(testOtherCapability) { + t.Fatalf("EachConnFiltered 'more' missing capability %v", testOtherCapability) + } + cp = p.Capabilities.Get(42) + if !cp.Match(testMoreCapability) { + t.Fatalf("EachConnFiltered 'more' missing capability %v", testMoreCapability) + } + return true + }) + if c != 1 { + t.Fatalf("EachConnFiltered 'more' expected 1 peer, got %d", c) + } } // TestHealthStrict tests the simplest definition of health From f6be3f339e2394aed37c8eafbfaeab5c7bb77e6d Mon Sep 17 00:00:00 2001 From: nolash Date: Fri, 13 Sep 2019 12:19:50 +0200 Subject: [PATCH 19/27] network: Add comments to capabilities index test --- network/kademlia_test.go | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/network/kademlia_test.go b/network/kademlia_test.go index e4f8b31dba..6f2a0d56f6 100644 --- a/network/kademlia_test.go +++ b/network/kademlia_test.go @@ -39,8 +39,7 @@ func init() { func testKadPeerAddr(s string) *BzzAddr { a := pot.NewAddressFromString(s) - bzzAddr := NewBzzAddr(a, a) - return bzzAddr + return NewBzzAddr(a, a) } func newTestKademliaParams() *KadParams { @@ -218,19 +217,23 @@ func TestCapabilitiesIndex(t *testing.T) { base := addr.OAddr k := NewKademlia(base, kp) + // "more" matches "more" only testMoreCapability := capability.NewCapability(42, 3) testMoreCapability.Set(0) testMoreCapability.Set(2) k.RegisterCapabilityIndex("more", *testMoreCapability) + // "less" matches "more" and "less" testLessCapability := capability.NewCapability(42, 3) testLessCapability.Set(2) k.RegisterCapabilityIndex("less", *testLessCapability) + // "none" matches neither "more" nor "less" testNoneCapability := capability.NewCapability(42, 3) testNoneCapability.Set(1) k.RegisterCapabilityIndex("none", *testNoneCapability) + // "other" is a different capability array testOtherCapability := capability.NewCapability(666, 3) testOtherCapability.Set(0) testOtherCapability.Set(2) @@ -245,22 +248,27 @@ func TestCapabilitiesIndex(t *testing.T) { otherAddr := RandomBzzAddr() otherAddr.Capabilities.Add(testOtherCapability) + // all includes two different capability arrays allAddr := RandomBzzAddr() allAddr.Capabilities.Add(testOtherCapability) allAddr.Capabilities.Add(testMoreCapability) + // proceed to check the matches, first for the "known peers" pot + // first adding the different peer configurations to the kademli k.Register(moreAddr, lessAddr, otherAddr, allAddr) - var addrs []*BzzAddr - k.EachAddr(base, 255, func(a *BzzAddr, _ int) bool { - addrs = append(addrs, a) + // Call without filter should still return all peers + var c int + k.EachAddr(base, 255, func(_ *BzzAddr, _ int) bool { + c++ return true }) - if len(addrs) != 4 { - t.Fatalf("EachAddr expected 3 peers, got %d", len(addrs)) + if c != 4 { + t.Fatalf("EachAddr expected 3 peers, got %d", c) } - var c int + // Matches "more", "all" + c = 0 k.EachAddrFiltered(base, "more", 255, func(a *BzzAddr, _ int) bool { c++ cp := a.Capabilities.Get(42) @@ -273,6 +281,7 @@ func TestCapabilitiesIndex(t *testing.T) { t.Fatalf("EachAddrFiltered 'full' expected 2 peer, got %d", c) } + // Matches "more", "less", "all" c = 0 k.EachAddrFiltered(base, "less", 255, func(a *BzzAddr, _ int) bool { c++ @@ -282,6 +291,7 @@ func TestCapabilitiesIndex(t *testing.T) { t.Fatalf("EachAddrFiltered 'less' expected 2 peers, got %d", c) } + // No matches c = 0 k.EachAddrFiltered(base, "none", 255, func(a *BzzAddr, _ int) bool { c++ @@ -291,6 +301,8 @@ func TestCapabilitiesIndex(t *testing.T) { t.Fatalf("EachAddrFiltered 'none' expected 0 peers, got %d", c) } + // Matches "other", "all" + // Also checks that "all" has both capabilities c = 0 k.EachAddrFiltered(base, "other", 255, func(a *BzzAddr, _ int) bool { c++ @@ -308,12 +320,15 @@ func TestCapabilitiesIndex(t *testing.T) { t.Fatalf("EachAddrFiltered 'other' expected 3 capability matches, got %d", c) } + // Now check the connection pot index + // We add the "all" peer allBzzPeer := &BzzPeer{ BzzAddr: allAddr, } allPeer := NewPeer(allBzzPeer, k) k.On(allPeer) + // Call without filter should return the single connected peer c = 0 k.EachConn(base, 255, func(_ *Peer, _ int) bool { c++ @@ -323,6 +338,7 @@ func TestCapabilitiesIndex(t *testing.T) { t.Fatalf("EachConn expected 1 peer, got %d", c) } + // Check that the peer exists in the indices for both capability arrays c = 0 k.EachConnFiltered(base, "other", 255, func(p *Peer, _ int) bool { c++ From a85f9e8dd7f1a1f59e1b6a65d2fef1da9257f14c Mon Sep 17 00:00:00 2001 From: nolash Date: Fri, 13 Sep 2019 12:59:30 +0200 Subject: [PATCH 20/27] newtork: Add comments, stub for capabilities index removal --- network/discovery.go | 3 +-- network/kademlia.go | 5 ++--- network/network.go | 4 ++++ pot/address.go | 4 ++-- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/network/discovery.go b/network/discovery.go index 941e1b859f..87a55e3eb6 100644 --- a/network/discovery.go +++ b/network/discovery.go @@ -206,8 +206,7 @@ func (d *Peer) handleSubPeersMsg(msg *subPeersMsg) error { }) // if useful peers are found, send them over if len(peers) > 0 { - outMsg := &peersMsg{Peers: sortPeers(peers)} - go d.Send(context.TODO(), outMsg) + go d.Send(context.TODO(), &peersMsg{Peers: sortPeers(peers)}) } } d.sentPeers = true diff --git a/network/kademlia.go b/network/kademlia.go index fc9c3149ec..d16eac967a 100644 --- a/network/kademlia.go +++ b/network/kademlia.go @@ -524,9 +524,8 @@ func (k *Kademlia) Off(p *Peer) { k.setNeighbourhoodDepth() } -// EachConn is an iterator with args (base, po, f) applies f to each live peer -// that has proximity order po or less as measured from the base -// if base is nil, kademlia base address is used +// EachConnFiltered performs the same action as EachConn +// with the difference that it will only return peers that matches the specified capability index filter func (k *Kademlia) EachConnFiltered(base []byte, capKey string, o int, f func(*Peer, int) bool) error { k.lock.RLock() defer k.lock.RUnlock() diff --git a/network/network.go b/network/network.go index 32cf4c9645..a7ae791130 100644 --- a/network/network.go +++ b/network/network.go @@ -20,6 +20,7 @@ type BzzAddr struct { Capabilities *capability.Capabilities } +// EncodeRLP implements rlp.Encoder func (b *BzzAddr) EncodeRLP(w io.Writer) error { err := rlp.Encode(w, b.OAddr) if err != nil { @@ -40,6 +41,7 @@ func (b *BzzAddr) EncodeRLP(w io.Writer) error { return nil } +// EncodeRLP implements rlp.Decoder func (b *BzzAddr) DecodeRLP(s *rlp.Stream) error { var err error @@ -63,6 +65,8 @@ func (b *BzzAddr) DecodeRLP(s *rlp.Stream) error { return nil } +// NewBzzAddr creates a new BzzAddr with the specified byte values for over- and underlayaddresses +// It will contain an empty capabilities object func NewBzzAddr(oaddr []byte, uaddr []byte) *BzzAddr { return &BzzAddr{ OAddr: oaddr, diff --git a/pot/address.go b/pot/address.go index 7bb9f7c0d1..d7bed6c46e 100644 --- a/pot/address.go +++ b/pot/address.go @@ -134,7 +134,7 @@ func ProxCmp(a, x, y []byte) int { return 0 } -// RandomBzzAddressAt (address, prox) generates a random address +// RandomAddressAt (address, prox) generates a random address // at proximity order prox relative to address // if prox is negative a random address is generated func RandomAddressAt(self Address, prox int) (addr Address) { @@ -159,7 +159,7 @@ func RandomAddressAt(self Address, prox int) (addr Address) { return } -// RandomBzzAddress generates a random address +// RandomAddress generates a random address func RandomAddress() Address { return RandomAddressAt(Address{}, -1) } From 42763dde5dc46b9ccafee96ee706b7edcbffb5eb Mon Sep 17 00:00:00 2001 From: nolash Date: Fri, 13 Sep 2019 14:34:37 +0200 Subject: [PATCH 21/27] network: Complete remove from index method and corresponding test --- network/kademlia_test.go | 164 --------------------------------------- 1 file changed, 164 deletions(-) diff --git a/network/kademlia_test.go b/network/kademlia_test.go index 6f2a0d56f6..33c965964f 100644 --- a/network/kademlia_test.go +++ b/network/kademlia_test.go @@ -210,170 +210,6 @@ func TestHighMinBinSize(t *testing.T) { } } -// TestCapabilitiesIndex checks that capability indices contains only the peers that have the filters' capability bits set -func TestCapabilitiesIndex(t *testing.T) { - kp := NewKadParams() - addr := RandomBzzAddr() - base := addr.OAddr - k := NewKademlia(base, kp) - - // "more" matches "more" only - testMoreCapability := capability.NewCapability(42, 3) - testMoreCapability.Set(0) - testMoreCapability.Set(2) - k.RegisterCapabilityIndex("more", *testMoreCapability) - - // "less" matches "more" and "less" - testLessCapability := capability.NewCapability(42, 3) - testLessCapability.Set(2) - k.RegisterCapabilityIndex("less", *testLessCapability) - - // "none" matches neither "more" nor "less" - testNoneCapability := capability.NewCapability(42, 3) - testNoneCapability.Set(1) - k.RegisterCapabilityIndex("none", *testNoneCapability) - - // "other" is a different capability array - testOtherCapability := capability.NewCapability(666, 3) - testOtherCapability.Set(0) - testOtherCapability.Set(2) - k.RegisterCapabilityIndex("other", *testOtherCapability) - - moreAddr := RandomBzzAddr() - moreAddr.Capabilities.Add(testMoreCapability) - - lessAddr := RandomBzzAddr() - lessAddr.Capabilities.Add(testLessCapability) - - otherAddr := RandomBzzAddr() - otherAddr.Capabilities.Add(testOtherCapability) - - // all includes two different capability arrays - allAddr := RandomBzzAddr() - allAddr.Capabilities.Add(testOtherCapability) - allAddr.Capabilities.Add(testMoreCapability) - - // proceed to check the matches, first for the "known peers" pot - // first adding the different peer configurations to the kademli - k.Register(moreAddr, lessAddr, otherAddr, allAddr) - - // Call without filter should still return all peers - var c int - k.EachAddr(base, 255, func(_ *BzzAddr, _ int) bool { - c++ - return true - }) - if c != 4 { - t.Fatalf("EachAddr expected 3 peers, got %d", c) - } - - // Matches "more", "all" - c = 0 - k.EachAddrFiltered(base, "more", 255, func(a *BzzAddr, _ int) bool { - c++ - cp := a.Capabilities.Get(42) - if !cp.Match(testMoreCapability) { - t.Fatalf("EachAddrFiltered 'more' capability mismatch, expected %v, got %v", testMoreCapability, cp) - } - return true - }) - if c != 2 { - t.Fatalf("EachAddrFiltered 'full' expected 2 peer, got %d", c) - } - - // Matches "more", "less", "all" - c = 0 - k.EachAddrFiltered(base, "less", 255, func(a *BzzAddr, _ int) bool { - c++ - return true - }) - if c != 3 { - t.Fatalf("EachAddrFiltered 'less' expected 2 peers, got %d", c) - } - - // No matches - c = 0 - k.EachAddrFiltered(base, "none", 255, func(a *BzzAddr, _ int) bool { - c++ - return true - }) - if c != 0 { - t.Fatalf("EachAddrFiltered 'none' expected 0 peers, got %d", c) - } - - // Matches "other", "all" - // Also checks that "all" has both capabilities - c = 0 - k.EachAddrFiltered(base, "other", 255, func(a *BzzAddr, _ int) bool { - c++ - cp := a.Capabilities.Get(666) - if !cp.Match(testOtherCapability) { - t.Fatalf("EachAddrFiltered 'other' capability mismatch, expected %v, got %v", testOtherCapability, cp) - } - cp = a.Capabilities.Get(42) - if cp != nil { - c++ - } - return true - }) - if c != 3 { - t.Fatalf("EachAddrFiltered 'other' expected 3 capability matches, got %d", c) - } - - // Now check the connection pot index - // We add the "all" peer - allBzzPeer := &BzzPeer{ - BzzAddr: allAddr, - } - allPeer := NewPeer(allBzzPeer, k) - k.On(allPeer) - - // Call without filter should return the single connected peer - c = 0 - k.EachConn(base, 255, func(_ *Peer, _ int) bool { - c++ - return true - }) - if c != 1 { - t.Fatalf("EachConn expected 1 peer, got %d", c) - } - - // Check that the peer exists in the indices for both capability arrays - c = 0 - k.EachConnFiltered(base, "other", 255, func(p *Peer, _ int) bool { - c++ - cp := p.Capabilities.Get(666) - if !cp.Match(testOtherCapability) { - t.Fatalf("EachConnFiltered 'other' missing capability %v", testOtherCapability) - } - cp = p.Capabilities.Get(42) - if !cp.Match(testMoreCapability) { - t.Fatalf("EachConnFiltered 'other' missing capability %v", testMoreCapability) - } - return true - }) - if c != 1 { - t.Fatalf("EachConnFiltered 'other' expected 1 peer, got %d", c) - } - - c = 0 - k.EachConnFiltered(base, "more", 255, func(p *Peer, _ int) bool { - c++ - cp := p.Capabilities.Get(666) - if !cp.Match(testOtherCapability) { - t.Fatalf("EachConnFiltered 'more' missing capability %v", testOtherCapability) - } - cp = p.Capabilities.Get(42) - if !cp.Match(testMoreCapability) { - t.Fatalf("EachConnFiltered 'more' missing capability %v", testMoreCapability) - } - return true - }) - if c != 1 { - t.Fatalf("EachConnFiltered 'more' expected 1 peer, got %d", c) - } -} - // TestHealthStrict tests the simplest definition of health // Which means whether we are connected to all neighbors we know of func TestHealthStrict(t *testing.T) { From b09418a8fd7045eb2edf1fc13f1cd415eab16ba9 Mon Sep 17 00:00:00 2001 From: nolash Date: Mon, 16 Sep 2019 22:35:25 +0200 Subject: [PATCH 22/27] network: Split up capability indices test --- network/kademlia_test.go | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/network/kademlia_test.go b/network/kademlia_test.go index 33c965964f..8ad32e196e 100644 --- a/network/kademlia_test.go +++ b/network/kademlia_test.go @@ -23,7 +23,6 @@ import ( "time" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/enode" @@ -943,27 +942,5 @@ func testCapabilityIndexRemove(t *testing.T) { if c != 1 { t.Fatalf("EachAddrFiltered '42:101' expected 1 peer, got %d", c) } -} - -func NewAdaptivePeer(k *Kademlia, capabilities ...*capability.Capability) (*Peer, error) { - // create the peer that fits the kademlia record - // it's quite a bit of work - peerPrivKey, err := crypto.GenerateKey() - if err != nil { - return nil, err - } - peerEnodeId := enode.PubkeyToIDV4(&peerPrivKey.PublicKey) - peerP2p := p2p.NewPeer(peerEnodeId, "foo", []p2p.Cap{}) - peerProto := protocols.NewPeer(peerP2p, nil, nil) - peerBzz := NewBzzPeer(peerProto) - caps := capability.NewCapabilities() - for _, cap := range capabilities { - caps.Add(cap) - } - peerBzz.WithCapabilities(caps) - err = k.Register(peerBzz.BzzAddr) - if err != nil { - return nil, err - } return NewPeer(peerBzz, k), nil } From 07f4d86fbd00937cf863f57ac3ff5254271bebb4 Mon Sep 17 00:00:00 2001 From: nolash Date: Tue, 17 Sep 2019 10:34:50 +0200 Subject: [PATCH 23/27] network, pss: Implement capabilities in pss --- network/kademlia.go | 7 ++++++- network/protocol.go | 18 +++++++++++++----- pss/pss.go | 17 +++++++++++++++++ swarm.go | 4 ++-- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/network/kademlia.go b/network/kademlia.go index d16eac967a..72d67afc1d 100644 --- a/network/kademlia.go +++ b/network/kademlia.go @@ -68,7 +68,8 @@ type KadParams struct { RetryExponent int // exponent to multiply retry intervals with MaxRetries int // maximum number of redial attempts // function to sanction or prevent suggesting a peer - Reachable func(*BzzAddr) bool `json:"-"` + Reachable func(*BzzAddr) bool `json:"-"` + Capabilities *capability.Capabilities `json:"-"` } // NewKadParams returns a params struct with default values @@ -81,6 +82,7 @@ func NewKadParams() *KadParams { RetryInterval: 4200000000, // 4.2 sec MaxRetries: 42, RetryExponent: 2, + Capabilities: capability.NewCapabilities(), } } @@ -114,6 +116,9 @@ func NewKademlia(addr []byte, params *KadParams) *Kademlia { if params == nil { params = NewKadParams() } + if params.Capabilities == nil { + params.Capabilities = capability.NewCapabilities() + } k := &Kademlia{ base: addr, KadParams: params, diff --git a/network/protocol.go b/network/protocol.go index 6721c6249f..f0e5f27de6 100644 --- a/network/protocol.go +++ b/network/protocol.go @@ -153,6 +153,7 @@ func NewBzz(config *BzzConfig, kad *Kademlia, store state.Store, streamerSpec, r bzz.retrievalSpec = nil } + bzz.localAddr.Capabilities = kad.Capabilities // temporary soon-to-be-legacy light/full, as above if config.LightNode { bzz.localAddr.Capabilities.Add(newLightCapability()) @@ -229,11 +230,18 @@ func (b *Bzz) Protocols() []p2p.Protocol { // * hive // Bzz implements the node.Service interface func (b *Bzz) APIs() []rpc.API { - return []rpc.API{{ - Namespace: "hive", - Version: "3.0", - Service: b.Hive, - }} + return []rpc.API{ + { + Namespace: "hive", + Version: "3.0", + Service: b.Hive, + }, + { + Namespace: "bzz", + Version: "4.0", + Service: capability.NewAPI(b.Kademlia.Capabilities), + }, + } } // RunProtocol is a wrapper for swarm subprotocols diff --git a/pss/pss.go b/pss/pss.go index a895be495d..72ab60d699 100644 --- a/pss/pss.go +++ b/pss/pss.go @@ -33,6 +33,7 @@ import ( "github.com/ethereum/go-ethereum/rpc" "github.com/ethersphere/swarm/log" "github.com/ethersphere/swarm/network" + "github.com/ethersphere/swarm/network/capability" "github.com/ethersphere/swarm/p2p/protocols" "github.com/ethersphere/swarm/pot" "github.com/ethersphere/swarm/pss/crypto" @@ -51,6 +52,11 @@ const ( defaultOutboxCapacity = 100000 protocolName = "pss" protocolVersion = 2 + capabilitiesSend = 0 // node sends pss messages + capabilitiesReceive = 1 // node processes pss messages + capabilitiesForward = 4 // node forwards pss messages on behalf of network + capabilitiesPartial = 5 // node accepts partially addressed messages + capabilitiesEmpty = 6 // node accepts messages with empty address ) var ( @@ -89,6 +95,7 @@ type Params struct { privateKey *ecdsa.PrivateKey SymKeyCacheCapacity int AllowRaw bool // If true, enables sending and receiving messages without builtin pss encryption + AllowForward bool } // Sane defaults for Pss @@ -265,6 +272,16 @@ func New(k *network.Kademlia, params *Params) (*Pss, error) { }) ps.outbox = newOutbox(defaultOutboxCapacity, ps.quitC, ps.forward) + cp := capability.NewCapability(1, 8) + cp.Set(capabilitiesSend) + cp.Set(capabilitiesReceive) + cp.Set(capabilitiesPartial) + cp.Set(capabilitiesEmpty) + if params.AllowForward { + cp.Set(capabilitiesForward) + } + k.Capabilities.Add(cp) + return ps, nil } diff --git a/swarm.go b/swarm.go index ebc29aab0d..4a9164c4d5 100644 --- a/swarm.go +++ b/swarm.go @@ -519,14 +519,14 @@ func (s *Swarm) APIs() []rpc.API { // public APIs { Namespace: "bzz", - Version: "3.0", + Version: "4.0", Service: &Info{s.config}, Public: true, }, // admin APIs { Namespace: "bzz", - Version: "3.0", + Version: "4.0", Service: api.NewInspector(s.api, s.bzz.Hive, s.netStore, s.streamer), Public: false, }, From 904b8c0dd8858459d6fb29e737050f07e723d3d4 Mon Sep 17 00:00:00 2001 From: nolash Date: Thu, 19 Sep 2019 11:10:13 +0200 Subject: [PATCH 24/27] network: Rebase cleanup --- network/kademlia_test.go | 1 - network/network.go | 55 ---------------------------------------- 2 files changed, 56 deletions(-) diff --git a/network/kademlia_test.go b/network/kademlia_test.go index 8ad32e196e..b751b61552 100644 --- a/network/kademlia_test.go +++ b/network/kademlia_test.go @@ -942,5 +942,4 @@ func testCapabilityIndexRemove(t *testing.T) { if c != 1 { t.Fatalf("EachAddrFiltered '42:101' expected 1 peer, got %d", c) } - return NewPeer(peerBzz, k), nil } diff --git a/network/network.go b/network/network.go index a7ae791130..541d05b836 100644 --- a/network/network.go +++ b/network/network.go @@ -75,61 +75,6 @@ func NewBzzAddr(oaddr []byte, uaddr []byte) *BzzAddr { } } -// EncodeRLP implements rlp.Encoder -func (b *BzzAddr) EncodeRLP(w io.Writer) error { - err := rlp.Encode(w, b.OAddr) - if err != nil { - return err - } - err = rlp.Encode(w, b.UAddr) - if err != nil { - return err - } - y, err := rlp.EncodeToBytes(b.Capabilities) - if err != nil { - return err - } - err = rlp.Encode(w, y) - if err != nil { - return err - } - return nil -} - -// DecodeRLP implements rlp.Decoder -func (b *BzzAddr) DecodeRLP(s *rlp.Stream) error { - var err error - - b.OAddr, err = s.Bytes() - if err != nil { - return fmt.Errorf("oaddr --- %v", err) - } - b.UAddr, err = s.Bytes() - if err != nil { - return fmt.Errorf("uaddr --- %v", err) - } - - y, err := s.Bytes() - if err != nil { - return fmt.Errorf("capsbytes --- %v", err) - } - err = rlp.DecodeBytes(y, &b.Capabilities) - if err != nil { - return fmt.Errorf("caps --- %v", err) - } - return nil -} - -// NewBzzAddr creates a new BzzAddr with the specified byte values for over- and underlayaddresses -// It will contain an empty capabilities object -func NewBzzAddr(oaddr []byte, uaddr []byte) *BzzAddr { - return &BzzAddr{ - OAddr: oaddr, - UAddr: uaddr, - Capabilities: capability.NewCapabilities(), - } -} - // Address implements OverlayPeer interface to be used in Overlay. func (a *BzzAddr) Address() []byte { return a.OAddr From 970577095719a62874fbdd3bbb8dd166d7b9f34a Mon Sep 17 00:00:00 2001 From: nolash Date: Thu, 19 Sep 2019 14:54:49 +0200 Subject: [PATCH 25/27] network: Revert regression of comment --- network/network.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/network/network.go b/network/network.go index 541d05b836..a0aae05141 100644 --- a/network/network.go +++ b/network/network.go @@ -41,7 +41,7 @@ func (b *BzzAddr) EncodeRLP(w io.Writer) error { return nil } -// EncodeRLP implements rlp.Decoder +// DecodeRLP implements rlp.Decoder func (b *BzzAddr) DecodeRLP(s *rlp.Stream) error { var err error From 544c1a2533b51a526b52b30eb09e1fc0e1f4a190 Mon Sep 17 00:00:00 2001 From: nolash Date: Thu, 19 Sep 2019 22:09:19 +0200 Subject: [PATCH 26/27] Re-trigger ci (unrelated stream test fail) From 6363f2c61aa1588e0dcfae61c65688b24adee261 Mon Sep 17 00:00:00 2001 From: nolash Date: Fri, 20 Sep 2019 10:15:04 +0200 Subject: [PATCH 27/27] network, pss: Use constants for capability IDs --- network/protocol.go | 5 +++-- pss/pss.go | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/network/protocol.go b/network/protocol.go index f0e5f27de6..863e843f1c 100644 --- a/network/protocol.go +++ b/network/protocol.go @@ -34,6 +34,7 @@ import ( ) var ( + CapabilityID = capability.CapabilityID(0) capabilitiesRetrieve = 0 capabilitiesPush = 1 capabilitiesRelayRetrieve = 4 @@ -82,7 +83,7 @@ func init() { // temporary convenience functions for legacy "LightNode" func newLightCapability() *capability.Capability { - c := capability.NewCapability(0, 16) + c := capability.NewCapability(CapabilityID, 16) c.Set(capabilitiesRetrieve) c.Set(capabilitiesPush) return c @@ -93,7 +94,7 @@ func isLightCapability(c *capability.Capability) bool { // temporary convenience functions for legacy "full node" func newFullCapability() *capability.Capability { - c := capability.NewCapability(0, 16) + c := capability.NewCapability(CapabilityID, 16) c.Set(capabilitiesRetrieve) c.Set(capabilitiesPush) c.Set(capabilitiesRelayRetrieve) diff --git a/pss/pss.go b/pss/pss.go index 72ab60d699..4c53389ebc 100644 --- a/pss/pss.go +++ b/pss/pss.go @@ -52,6 +52,7 @@ const ( defaultOutboxCapacity = 100000 protocolName = "pss" protocolVersion = 2 + CapabilityID = capability.CapabilityID(1) capabilitiesSend = 0 // node sends pss messages capabilitiesReceive = 1 // node processes pss messages capabilitiesForward = 4 // node forwards pss messages on behalf of network @@ -272,7 +273,7 @@ func New(k *network.Kademlia, params *Params) (*Pss, error) { }) ps.outbox = newOutbox(defaultOutboxCapacity, ps.quitC, ps.forward) - cp := capability.NewCapability(1, 8) + cp := capability.NewCapability(CapabilityID, 8) cp.Set(capabilitiesSend) cp.Set(capabilitiesReceive) cp.Set(capabilitiesPartial)