@@ -25,16 +25,12 @@ import (
25
25
"github.com/ethersphere/swarm/pot"
26
26
)
27
27
28
- // discovery bzz extension for requesting and relaying node address records
29
-
30
- var sortPeers = noSortPeers
31
-
32
28
// Peer wraps BzzPeer and embeds Kademlia overlay connectivity driver
33
29
type Peer struct {
34
30
* BzzPeer
35
31
kad * Kademlia
36
32
sentPeers bool // whether we already sent peer closer to this address
37
- mtx sync.RWMutex //
33
+ mtx sync.RWMutex // protect peers map
38
34
peers map [string ]bool // tracks node records sent to the peer
39
35
depth uint8 // the proximity order advertised by remote as depth of saturation
40
36
}
@@ -51,39 +47,6 @@ func NewPeer(p *BzzPeer, kad *Kademlia) *Peer {
51
47
return d
52
48
}
53
49
54
- // HandleMsg is the message handler that delegates incoming messages
55
- func (d * Peer ) HandleMsg (ctx context.Context , msg interface {}) error {
56
- switch msg := msg .(type ) {
57
-
58
- case * peersMsg :
59
- return d .handlePeersMsg (msg )
60
-
61
- case * subPeersMsg :
62
- return d .handleSubPeersMsg (msg )
63
-
64
- default :
65
- return fmt .Errorf ("unknown message type: %T" , msg )
66
- }
67
- }
68
-
69
- // NotifyDepth sends a message to all connections if depth of saturation is changed
70
- func NotifyDepth (depth uint8 , kad * Kademlia ) {
71
- f := func (val * Peer , po int ) bool {
72
- val .NotifyDepth (depth )
73
- return true
74
- }
75
- kad .EachConn (nil , 255 , f )
76
- }
77
-
78
- // NotifyPeer informs all peers about a newly added node
79
- func NotifyPeer (p * BzzAddr , k * Kademlia ) {
80
- f := func (val * Peer , po int ) bool {
81
- val .NotifyPeer (p , uint8 (po ))
82
- return true
83
- }
84
- k .EachConn (p .Address (), 255 , f )
85
- }
86
-
87
50
// NotifyPeer notifies the remote node (recipient) about a peer if
88
51
// the peer's PO is within the recipients advertised depth
89
52
// OR the peer is closer to the recipient than self
@@ -154,21 +117,6 @@ func (msg peersMsg) String() string {
154
117
return fmt .Sprintf ("%T: %v" , msg , msg .Peers )
155
118
}
156
119
157
- // handlePeersMsg called by the protocol when receiving peerset (for target address)
158
- // list of nodes ([]PeerAddr in peersMsg) is added to the overlay db using the
159
- // Register interface method
160
- func (d * Peer ) handlePeersMsg (msg * peersMsg ) error {
161
- // register all addresses
162
- if len (msg .Peers ) == 0 {
163
- return nil
164
- }
165
- for _ , a := range msg .Peers {
166
- d .seen (a )
167
- NotifyPeer (a , d .kad )
168
- }
169
- return d .kad .Register (msg .Peers ... )
170
- }
171
-
172
120
// subPeers msg is communicating the depth of the overlay table of a peer
173
121
type subPeersMsg struct {
174
122
Depth uint8
@@ -179,38 +127,6 @@ func (msg subPeersMsg) String() string {
179
127
return fmt .Sprintf ("%T: request peers > PO%02d. " , msg , msg .Depth )
180
128
}
181
129
182
- // handleSubPeersMsg handles incoming subPeersMsg
183
- // this message represents the saturation depth of the remote peer
184
- // saturation depth is the radius within which the peer subscribes to peers
185
- // the first time this is received we send peer info on all
186
- // our connected peers that fall within peers saturation depth
187
- // otherwise this depth is just recorded on the peer, so that
188
- // subsequent new connections are sent iff they fall within the radius
189
- func (d * Peer ) handleSubPeersMsg (msg * subPeersMsg ) error {
190
- d .setDepth (msg .Depth )
191
- // only send peers after the initial subPeersMsg
192
- if ! d .sentPeers {
193
- var peers []* BzzAddr
194
- // iterate connection in ascending order of disctance from the remote address
195
- d .kad .EachConn (d .Over (), 255 , func (p * Peer , po int ) bool {
196
- // terminate if we are beyond the radius
197
- if uint8 (po ) < msg .Depth {
198
- return false
199
- }
200
- if ! d .seen (p .BzzAddr ) { // here just records the peer sent
201
- peers = append (peers , p .BzzAddr )
202
- }
203
- return true
204
- })
205
- // if useful peers are found, send them over
206
- if len (peers ) > 0 {
207
- go d .Send (context .TODO (), & peersMsg {Peers : sortPeers (peers )})
208
- }
209
- }
210
- d .sentPeers = true
211
- return nil
212
- }
213
-
214
130
// seen takes a peer address and checks if it was sent to a peer already
215
131
// if not, marks the peer as sent
216
132
func (d * Peer ) seen (p * BzzAddr ) bool {
0 commit comments