swarm/network: minor changes re review; add missing lock to NeighbourhoodDepthC

This commit is contained in:
zelig 2018-09-11 14:07:24 +02:00
parent b7286a9909
commit 99cd4a66de
5 changed files with 9 additions and 39 deletions

View file

@ -26,7 +26,7 @@ import (
// discovery bzz extension for requesting and relaying node address records // discovery bzz extension for requesting and relaying node address records
// Peer wraps BzzPeer and embeds an Overlay connectivity driver // Peer wraps BzzPeer and embeds Kademlia overlay connectivity driver
type Peer struct { type Peer struct {
*BzzPeer *BzzPeer
kad *Kademlia kad *Kademlia
@ -146,7 +146,7 @@ func (d *Peer) handlePeersMsg(msg *peersMsg) error {
return d.kad.Register(msg.Peers...) return d.kad.Register(msg.Peers...)
} }
// subPeers msg is communicating the depth/sharpness/focus of the overlay table of a peer // subPeers msg is communicating the depth of the overlay table of a peer
type subPeersMsg struct { type subPeersMsg struct {
Depth uint8 Depth uint8
} }
@ -177,7 +177,7 @@ func (d *Peer) handleSubPeersMsg(msg *subPeersMsg) error {
return nil return nil
} }
// seen takes an Overlay peer and checks if it was sent to a peer already // seen takes an peer address and checks if it was sent to a peer already
// if not, marks the peer as sent // if not, marks the peer as sent
func (d *Peer) seen(p *BzzAddr) bool { func (d *Peer) seen(p *BzzAddr) bool {
d.mtx.Lock() d.mtx.Lock()

View file

@ -132,7 +132,7 @@ func (e *entry) Hex() string {
return fmt.Sprintf("%x", e.Address()) return fmt.Sprintf("%x", e.Address())
} }
// Register enters each OverlayAddr as kademlia peer record into the // Register enters each address as kademlia peer record into the
// database of known peer addresses // database of known peer addresses
func (k *Kademlia) Register(peers ...*BzzAddr) error { func (k *Kademlia) Register(peers ...*BzzAddr) error {
k.lock.Lock() k.lock.Lock()
@ -290,6 +290,8 @@ func (k *Kademlia) On(p *Peer) (uint8, bool) {
// Not receiving from the returned channel will block On function // Not receiving from the returned channel will block On function
// when the neighbourhood depth is changed. // when the neighbourhood depth is changed.
func (k *Kademlia) NeighbourhoodDepthC() <-chan int { func (k *Kademlia) NeighbourhoodDepthC() <-chan int {
k.lock.Lock()
defer k.lock.Unlock()
if k.nDepthC == nil { if k.nDepthC == nil {
k.nDepthC = make(chan int) k.nDepthC = make(chan int)
} }

View file

@ -62,33 +62,6 @@ var DiscoverySpec = &protocols.Spec{
}, },
} }
//
// // Addr interface that peerPool needs
// type Addr interface {
// OverlayPeer
// Over() []byte
// Under() []byte
// String() string
// Update(OverlayAddr) OverlayAddr
// }
//
// // Peer interface represents an live peer connection
// type Peer interface {
// Addr // the address of a peer
// Conn // the live connection (protocols.Peer)
// LastActive() time.Time // last time active
// }
//
// // Conn interface represents an live peer connection
// type Conn interface {
// ID() discover.NodeID // the key that uniquely identifies the Node for the peerPool
// Handshake(context.Context, interface{}, func(interface{}) error) (interface{}, error) // can send messages
// Send(context.Context, interface{}) error // can send messages
// Drop(error) // disconnect this peer
// Run(func(context.Context, interface{}) error) error // the run function to run a protocol
// Off() OverlayAddr
// }
// BzzConfig captures the config params used by the hive // BzzConfig captures the config params used by the hive
type BzzConfig struct { type BzzConfig struct {
OverlayAddr []byte // base address of the overlay network OverlayAddr []byte // base address of the overlay network
@ -275,7 +248,7 @@ type BzzPeer struct {
LightNode bool LightNode bool
} }
func NewBzzTestPeer(p *protocols.Peer, addr *BzzAddr) *BzzPeer { func NewBzzPeer(p *protocols.Peer, addr *BzzAddr) *BzzPeer {
return &BzzPeer{ return &BzzPeer{
Peer: p, Peer: p,
localAddr: addr, localAddr: addr,
@ -283,11 +256,6 @@ func NewBzzTestPeer(p *protocols.Peer, addr *BzzAddr) *BzzPeer {
} }
} }
// // Off returns the overlay peer record for offline persistence
// func (p *BzzPeer) Off() OverlayAddr {
// return p.BzzAddr
// }
// LastActive returns the time the peer was last active // LastActive returns the time the peer was last active
func (p *BzzPeer) LastActive() time.Time { func (p *BzzPeer) LastActive() time.Time {
return p.lastActive return p.lastActive

View file

@ -458,7 +458,7 @@ func (r *Registry) updateSyncing() {
func (r *Registry) runProtocol(p *p2p.Peer, rw p2p.MsgReadWriter) error { func (r *Registry) runProtocol(p *p2p.Peer, rw p2p.MsgReadWriter) error {
peer := protocols.NewPeer(p, rw, Spec) peer := protocols.NewPeer(p, rw, Spec)
bp := network.NewBzzTestPeer(peer, r.addr) bp := network.NewBzzPeer(peer, r.addr)
np := network.NewPeer(bp, r.delivery.kad) np := network.NewPeer(bp, r.delivery.kad)
r.delivery.kad.On(np) r.delivery.kad.On(np)
defer r.delivery.kad.Off(np) defer r.delivery.kad.Off(np)