Skip to content

Commit bfce003

Browse files
zeliggbalint
authored andcommitted
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
1 parent b06ff56 commit bfce003

16 files changed

Lines changed: 260 additions & 421 deletions

‎swarm/network/discovery.go‎

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,30 @@ import (
2626

2727
// discovery bzz extension for requesting and relaying node address records
2828

29-
// discPeer wraps BzzPeer and embeds an Overlay connectivity driver
30-
type discPeer struct {
29+
// Peer wraps BzzPeer and embeds Kademlia overlay connectivity driver
30+
type Peer struct {
3131
*BzzPeer
32-
overlay Overlay
33-
sentPeers bool // whether we already sent peer closer to this address
34-
mtx sync.RWMutex
32+
kad *Kademlia
33+
sentPeers bool // whether we already sent peer closer to this address
34+
mtx sync.RWMutex //
3535
peers map[string]bool // tracks node records sent to the peer
3636
depth uint8 // the proximity order advertised by remote as depth of saturation
3737
}
3838

39-
// NewDiscovery constructs a discovery peer
40-
func newDiscovery(p *BzzPeer, o Overlay) *discPeer {
41-
d := &discPeer{
42-
overlay: o,
39+
// NewPeer constructs a discovery peer
40+
func NewPeer(p *BzzPeer, kad *Kademlia) *Peer {
41+
d := &Peer{
42+
kad: kad,
4343
BzzPeer: p,
4444
peers: make(map[string]bool),
4545
}
4646
// record remote as seen so we never send a peer its own record
47-
d.seen(d)
47+
d.seen(p.BzzAddr)
4848
return d
4949
}
5050

5151
// HandleMsg is the message handler that delegates incoming messages
52-
func (d *discPeer) HandleMsg(ctx context.Context, msg interface{}) error {
52+
func (d *Peer) HandleMsg(ctx context.Context, msg interface{}) error {
5353
switch msg := msg.(type) {
5454

5555
case *peersMsg:
@@ -64,24 +64,18 @@ func (d *discPeer) HandleMsg(ctx context.Context, msg interface{}) error {
6464
}
6565

6666
// NotifyDepth sends a message to all connections if depth of saturation is changed
67-
func NotifyDepth(depth uint8, h Overlay) {
68-
f := func(val OverlayConn, po int, _ bool) bool {
69-
dp, ok := val.(*discPeer)
70-
if ok {
71-
dp.NotifyDepth(depth)
72-
}
67+
func NotifyDepth(depth uint8, kad *Kademlia) {
68+
f := func(val *Peer, po int, _ bool) bool {
69+
val.NotifyDepth(depth)
7370
return true
7471
}
75-
h.EachConn(nil, 255, f)
72+
kad.EachConn(nil, 255, f)
7673
}
7774

7875
// NotifyPeer informs all peers about a newly added node
79-
func NotifyPeer(p OverlayAddr, k Overlay) {
80-
f := func(val OverlayConn, po int, _ bool) bool {
81-
dp, ok := val.(*discPeer)
82-
if ok {
83-
dp.NotifyPeer(p, uint8(po))
84-
}
76+
func NotifyPeer(p *BzzAddr, k *Kademlia) {
77+
f := func(val *Peer, po int, _ bool) bool {
78+
val.NotifyPeer(p, uint8(po))
8579
return true
8680
}
8781
k.EachConn(p.Address(), 255, f)
@@ -91,22 +85,20 @@ func NotifyPeer(p OverlayAddr, k Overlay) {
9185
// the peer's PO is within the recipients advertised depth
9286
// OR the peer is closer to the recipient than self
9387
// unless already notified during the connection session
94-
func (d *discPeer) NotifyPeer(a OverlayAddr, po uint8) {
88+
func (d *Peer) NotifyPeer(a *BzzAddr, po uint8) {
9589
// immediately return
9690
if (po < d.getDepth() && pot.ProxCmp(d.localAddr, d, a) != 1) || d.seen(a) {
9791
return
9892
}
99-
// log.Trace(fmt.Sprintf("%08x peer %08x notified of peer %08x", d.localAddr.Over()[:4], d.Address()[:4], a.Address()[:4]))
10093
resp := &peersMsg{
101-
Peers: []*BzzAddr{ToAddr(a)},
94+
Peers: []*BzzAddr{a},
10295
}
10396
go d.Send(context.TODO(), resp)
10497
}
10598

10699
// NotifyDepth sends a subPeers Msg to the receiver notifying them about
107100
// a change in the depth of saturation
108-
func (d *discPeer) NotifyDepth(po uint8) {
109-
// log.Trace(fmt.Sprintf("%08x peer %08x notified of new depth %v", d.localAddr.Over()[:4], d.Address()[:4], po))
101+
func (d *Peer) NotifyDepth(po uint8) {
110102
go d.Send(context.TODO(), &subPeersMsg{Depth: po})
111103
}
112104

@@ -141,20 +133,20 @@ func (msg peersMsg) String() string {
141133
// handlePeersMsg called by the protocol when receiving peerset (for target address)
142134
// list of nodes ([]PeerAddr in peersMsg) is added to the overlay db using the
143135
// Register interface method
144-
func (d *discPeer) handlePeersMsg(msg *peersMsg) error {
136+
func (d *Peer) handlePeersMsg(msg *peersMsg) error {
145137
// register all addresses
146138
if len(msg.Peers) == 0 {
147139
return nil
148140
}
149141

150142
for _, a := range msg.Peers {
151143
d.seen(a)
152-
NotifyPeer(a, d.overlay)
144+
NotifyPeer(a, d.kad)
153145
}
154-
return d.overlay.Register(toOverlayAddrs(msg.Peers...))
146+
return d.kad.Register(msg.Peers...)
155147
}
156148

157-
// subPeers msg is communicating the depth/sharpness/focus of the overlay table of a peer
149+
// subPeers msg is communicating the depth of the overlay table of a peer
158150
type subPeersMsg struct {
159151
Depth uint8
160152
}
@@ -164,31 +156,30 @@ func (msg subPeersMsg) String() string {
164156
return fmt.Sprintf("%T: request peers > PO%02d. ", msg, msg.Depth)
165157
}
166158

167-
func (d *discPeer) handleSubPeersMsg(msg *subPeersMsg) error {
159+
func (d *Peer) handleSubPeersMsg(msg *subPeersMsg) error {
168160
if !d.sentPeers {
169161
d.setDepth(msg.Depth)
170162
var peers []*BzzAddr
171-
d.overlay.EachConn(d.Over(), 255, func(p OverlayConn, po int, isproxbin bool) bool {
163+
d.kad.EachConn(d.Over(), 255, func(p *Peer, po int, isproxbin bool) bool {
172164
if pob, _ := pof(d, d.localAddr, 0); pob > po {
173165
return false
174166
}
175-
if !d.seen(p) {
176-
peers = append(peers, ToAddr(p.Off()))
167+
if !d.seen(p.BzzAddr) {
168+
peers = append(peers, p.BzzAddr)
177169
}
178170
return true
179171
})
180172
if len(peers) > 0 {
181-
// log.Debug(fmt.Sprintf("%08x: %v peers sent to %v", d.overlay.BaseAddr(), len(peers), d))
182173
go d.Send(context.TODO(), &peersMsg{Peers: peers})
183174
}
184175
}
185176
d.sentPeers = true
186177
return nil
187178
}
188179

189-
// seen takes an Overlay peer and checks if it was sent to a peer already
180+
// seen takes an peer address and checks if it was sent to a peer already
190181
// if not, marks the peer as sent
191-
func (d *discPeer) seen(p OverlayPeer) bool {
182+
func (d *Peer) seen(p *BzzAddr) bool {
192183
d.mtx.Lock()
193184
defer d.mtx.Unlock()
194185
k := string(p.Address())
@@ -199,12 +190,13 @@ func (d *discPeer) seen(p OverlayPeer) bool {
199190
return false
200191
}
201192

202-
func (d *discPeer) getDepth() uint8 {
193+
func (d *Peer) getDepth() uint8 {
203194
d.mtx.RLock()
204195
defer d.mtx.RUnlock()
205196
return d.depth
206197
}
207-
func (d *discPeer) setDepth(depth uint8) {
198+
199+
func (d *Peer) setDepth(depth uint8) {
208200
d.mtx.Lock()
209201
defer d.mtx.Unlock()
210202
d.depth = depth

‎swarm/network/discovery_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestDiscovery(t *testing.T) {
3333

3434
id := s.IDs[0]
3535
raddr := NewAddrFromNodeID(id)
36-
pp.Register([]OverlayAddr{OverlayAddr(raddr)})
36+
pp.Register(raddr)
3737

3838
// start the hive and wait for the connection
3939
pp.Start(s.Server)

‎swarm/network/hive.go‎

Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,10 @@ import (
3232
Hive is the logistic manager of the swarm
3333
3434
When the hive is started, a forever loop is launched that
35-
asks the Overlay Topology driver (e.g., generic kademlia nodetable)
35+
asks the kademlia nodetable
3636
to suggest peers to bootstrap connectivity
3737
*/
3838

39-
// Overlay is the interface for kademlia (or other topology drivers)
40-
type Overlay interface {
41-
// suggest peers to connect to
42-
SuggestPeer() (OverlayAddr, int, bool)
43-
// register and deregister peer connections
44-
On(OverlayConn) (depth uint8, changed bool)
45-
Off(OverlayConn)
46-
// register peer addresses
47-
Register([]OverlayAddr) error
48-
// iterate over connected peers
49-
EachConn([]byte, int, func(OverlayConn, int, bool) bool)
50-
// iterate over known peers (address records)
51-
EachAddr([]byte, int, func(OverlayAddr, int, bool) bool)
52-
// pretty print the connectivity
53-
String() string
54-
// base Overlay address of the node itself
55-
BaseAddr() []byte
56-
// connectivity health check used for testing
57-
Healthy(*PeerPot) *Health
58-
}
59-
6039
// HiveParams holds the config options to hive
6140
type HiveParams struct {
6241
Discovery bool // if want discovery of not
@@ -78,7 +57,7 @@ func NewHiveParams() *HiveParams {
7857
// Hive manages network connections of the swarm node
7958
type Hive struct {
8059
*HiveParams // settings
81-
Overlay // the overlay connectiviy driver
60+
*Kademlia // the overlay connectiviy driver
8261
Store state.Store // storage interface to save peers across sessions
8362
addPeer func(*discover.Node) // server callback to connect to a peer
8463
// bookkeeping
@@ -88,12 +67,12 @@ type Hive struct {
8867

8968
// NewHive constructs a new hive
9069
// HiveParams: config parameters
91-
// Overlay: connectivity driver using a network topology
70+
// Kademlia: connectivity driver using a network topology
9271
// StateStore: to save peers across sessions
93-
func NewHive(params *HiveParams, overlay Overlay, store state.Store) *Hive {
72+
func NewHive(params *HiveParams, kad *Kademlia, store state.Store) *Hive {
9473
return &Hive{
9574
HiveParams: params,
96-
Overlay: overlay,
75+
Kademlia: kad,
9776
Store: store,
9877
}
9978
}
@@ -133,7 +112,7 @@ func (h *Hive) Stop() error {
133112
}
134113
}
135114
log.Info(fmt.Sprintf("%08x hive stopped, dropping peers", h.BaseAddr()[:4]))
136-
h.EachConn(nil, 255, func(p OverlayConn, _ int, _ bool) bool {
115+
h.EachConn(nil, 255, func(p *Peer, _ int, _ bool) bool {
137116
log.Info(fmt.Sprintf("%08x dropping peer %08x", h.BaseAddr()[:4], p.Address()[:4]))
138117
p.Drop(nil)
139118
return true
@@ -151,14 +130,14 @@ func (h *Hive) connect() {
151130

152131
addr, depth, changed := h.SuggestPeer()
153132
if h.Discovery && changed {
154-
NotifyDepth(uint8(depth), h)
133+
NotifyDepth(uint8(depth), h.Kademlia)
155134
}
156135
if addr == nil {
157136
continue
158137
}
159138

160139
log.Trace(fmt.Sprintf("%08x hive connect() suggested %08x", h.BaseAddr()[:4], addr.Address()[:4]))
161-
under, err := discover.ParseNode(string(addr.(Addr).Under()))
140+
under, err := discover.ParseNode(string(addr.Under()))
162141
if err != nil {
163142
log.Warn(fmt.Sprintf("%08x unable to connect to bee %08x: invalid node URL: %v", h.BaseAddr()[:4], addr.Address()[:4], err))
164143
continue
@@ -170,19 +149,19 @@ func (h *Hive) connect() {
170149

171150
// Run protocol run function
172151
func (h *Hive) Run(p *BzzPeer) error {
173-
dp := newDiscovery(p, h)
152+
dp := NewPeer(p, h.Kademlia)
174153
depth, changed := h.On(dp)
175154
// if we want discovery, advertise change of depth
176155
if h.Discovery {
177156
if changed {
178157
// if depth changed, send to all peers
179-
NotifyDepth(depth, h)
158+
NotifyDepth(depth, h.Kademlia)
180159
} else {
181160
// otherwise just send depth to new peer
182161
dp.NotifyDepth(depth)
183162
}
184163
}
185-
NotifyPeer(p.Off(), h)
164+
NotifyPeer(p.BzzAddr, h.Kademlia)
186165
defer h.Off(dp)
187166
return dp.Run(dp.HandleMsg)
188167
}
@@ -206,17 +185,6 @@ func (h *Hive) PeerInfo(id discover.NodeID) interface{} {
206185
}
207186
}
208187

209-
// ToAddr returns the serialisable version of u
210-
func ToAddr(pa OverlayPeer) *BzzAddr {
211-
if addr, ok := pa.(*BzzAddr); ok {
212-
return addr
213-
}
214-
if p, ok := pa.(*discPeer); ok {
215-
return p.BzzAddr
216-
}
217-
return pa.(*BzzPeer).BzzAddr
218-
}
219-
220188
// loadPeers, savePeer implement persistence callback/
221189
func (h *Hive) loadPeers() error {
222190
var as []*BzzAddr
@@ -230,28 +198,19 @@ func (h *Hive) loadPeers() error {
230198
}
231199
log.Info(fmt.Sprintf("hive %08x: peers loaded", h.BaseAddr()[:4]))
232200

233-
return h.Register(toOverlayAddrs(as...))
234-
}
235-
236-
// toOverlayAddrs transforms an array of BzzAddr to OverlayAddr
237-
func toOverlayAddrs(as ...*BzzAddr) (oas []OverlayAddr) {
238-
for _, a := range as {
239-
oas = append(oas, OverlayAddr(a))
240-
}
241-
return
201+
return h.Register(as...)
242202
}
243203

244204
// savePeers, savePeer implement persistence callback/
245205
func (h *Hive) savePeers() error {
246206
var peers []*BzzAddr
247-
h.Overlay.EachAddr(nil, 256, func(pa OverlayAddr, i int, _ bool) bool {
207+
h.Kademlia.EachAddr(nil, 256, func(pa *BzzAddr, i int, _ bool) bool {
248208
if pa == nil {
249209
log.Warn(fmt.Sprintf("empty addr: %v", i))
250210
return true
251211
}
252-
apa := ToAddr(pa)
253-
log.Trace("saving peer", "peer", apa)
254-
peers = append(peers, apa)
212+
log.Trace("saving peer", "peer", pa)
213+
peers = append(peers, pa)
255214
return true
256215
})
257216
if err := h.Store.Put("peers", peers); err != nil {

‎swarm/network/hive_test.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestRegisterAndConnect(t *testing.T) {
4141

4242
id := s.IDs[0]
4343
raddr := NewAddrFromNodeID(id)
44-
pp.Register([]OverlayAddr{OverlayAddr(raddr)})
44+
pp.Register(raddr)
4545

4646
// start the hive and wait for the connection
4747
err := pp.Start(s.Server)
@@ -77,7 +77,7 @@ func TestHiveStatePersistance(t *testing.T) {
7777
peers := make(map[string]bool)
7878
for _, id := range s.IDs {
7979
raddr := NewAddrFromNodeID(id)
80-
pp.Register([]OverlayAddr{OverlayAddr(raddr)})
80+
pp.Register(raddr)
8181
peers[raddr.String()] = true
8282
}
8383

@@ -97,8 +97,8 @@ func TestHiveStatePersistance(t *testing.T) {
9797

9898
pp.Start(s1.Server)
9999
i := 0
100-
pp.Overlay.EachAddr(nil, 256, func(addr OverlayAddr, po int, nn bool) bool {
101-
delete(peers, addr.(*BzzAddr).String())
100+
pp.Kademlia.EachAddr(nil, 256, func(addr *BzzAddr, po int, nn bool) bool {
101+
delete(peers, addr.String())
102102
i++
103103
return true
104104
})

0 commit comments

Comments
 (0)