Kademlia refactor (#17641)

* swarm/network: simplify kademlia/hive; rid interfaces

* swarm, swarm/network/stream, swarm/netork/simulations,, swarm/pss: adapt to new Kad API

* swarm/network: minor changes re review; add missing lock to NeighbourhoodDepthC
This commit is contained in:
Viktor Trón
2018-09-12 11:24:56 +02:00
committed by Balint Gabor
parent b06ff563a1
commit bfce00385f
16 changed files with 260 additions and 421 deletions

View File

@ -62,32 +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
type BzzConfig struct {
OverlayAddr []byte // base address of the overlay network
@ -114,7 +88,7 @@ type Bzz struct {
// * bzz config
// * overlay driver
// * peer store
func NewBzz(config *BzzConfig, kad Overlay, store state.Store, streamerSpec *protocols.Spec, streamerRun func(*BzzPeer) error) *Bzz {
func NewBzz(config *BzzConfig, kad *Kademlia, store state.Store, streamerSpec *protocols.Spec, streamerRun func(*BzzPeer) error) *Bzz {
return &Bzz{
Hive: NewHive(config.HiveParams, kad, store),
NetworkID: config.NetworkID,
@ -131,7 +105,7 @@ func (b *Bzz) UpdateLocalAddr(byteaddr []byte) *BzzAddr {
b.localAddr = b.localAddr.Update(&BzzAddr{
UAddr: byteaddr,
OAddr: b.localAddr.OAddr,
}).(*BzzAddr)
})
return b.localAddr
}
@ -274,7 +248,7 @@ type BzzPeer struct {
LightNode bool
}
func NewBzzTestPeer(p *protocols.Peer, addr *BzzAddr) *BzzPeer {
func NewBzzPeer(p *protocols.Peer, addr *BzzAddr) *BzzPeer {
return &BzzPeer{
Peer: p,
localAddr: addr,
@ -282,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
func (p *BzzPeer) LastActive() time.Time {
return p.lastActive
@ -388,8 +357,8 @@ func (a *BzzAddr) ID() discover.NodeID {
}
// Update updates the underlay address of a peer record
func (a *BzzAddr) Update(na OverlayAddr) OverlayAddr {
return &BzzAddr{a.OAddr, na.(Addr).Under()}
func (a *BzzAddr) Update(na *BzzAddr) *BzzAddr {
return &BzzAddr{a.OAddr, na.UAddr}
}
// String pretty prints the address
@ -410,9 +379,9 @@ func RandomAddr() *BzzAddr {
}
// NewNodeIDFromAddr transforms the underlay address to an adapters.NodeID
func NewNodeIDFromAddr(addr Addr) discover.NodeID {
log.Info(fmt.Sprintf("uaddr=%s", string(addr.Under())))
node := discover.MustParseNode(string(addr.Under()))
func NewNodeIDFromAddr(addr *BzzAddr) discover.NodeID {
log.Info(fmt.Sprintf("uaddr=%s", string(addr.UAddr)))
node := discover.MustParseNode(string(addr.UAddr))
return node.ID
}