Skip to content

Commit 81533de

Browse files
vmaarkzelig
authored andcommitted
swarm/network: light nodes are not dialed, saved and requested from (#17975)
* RequestFromPeers does not use peers marked as lightnode * fix warning about variable name * write tests for RequestFromPeers * lightnodes should be omitted from the addressbook * resolve pr comments regarding logging, formatting and comments * resolve pr comments regarding comments and added a missing newline * add assertions to check peers in live connections
1 parent 0bcff8f commit 81533de

4 files changed

Lines changed: 153 additions & 13 deletions

File tree

‎swarm/network/kademlia.go‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func (k *Kademlia) On(p *Peer) (uint8, bool) {
261261
// found among live peers, do nothing
262262
return v
263263
})
264-
if ins {
264+
if ins && !p.BzzPeer.LightNode {
265265
a := newEntry(p.BzzAddr)
266266
a.conn = p
267267
// insert new online peer into addrs
@@ -329,14 +329,18 @@ func (k *Kademlia) Off(p *Peer) {
329329
k.lock.Lock()
330330
defer k.lock.Unlock()
331331
var del bool
332-
k.addrs, _, _, _ = pot.Swap(k.addrs, p, pof, func(v pot.Val) pot.Val {
333-
// v cannot be nil, must check otherwise we overwrite entry
334-
if v == nil {
335-
panic(fmt.Sprintf("connected peer not found %v", p))
336-
}
332+
if !p.BzzPeer.LightNode {
333+
k.addrs, _, _, _ = pot.Swap(k.addrs, p, pof, func(v pot.Val) pot.Val {
334+
// v cannot be nil, must check otherwise we overwrite entry
335+
if v == nil {
336+
panic(fmt.Sprintf("connected peer not found %v", p))
337+
}
338+
del = true
339+
return newEntry(p.BzzAddr)
340+
})
341+
} else {
337342
del = true
338-
return newEntry(p.BzzAddr)
339-
})
343+
}
340344

341345
if del {
342346
k.conns, _, _, _ = pot.Swap(k.conns, p, pof, func(_ pot.Val) pot.Val {

‎swarm/network/kademlia_test.go‎

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ func newTestKademlia(b string) *Kademlia {
4646
return NewKademlia(base, params)
4747
}
4848

49-
func newTestKadPeer(k *Kademlia, s string) *Peer {
50-
return NewPeer(&BzzPeer{BzzAddr: testKadPeerAddr(s)}, k)
49+
func newTestKadPeer(k *Kademlia, s string, lightNode bool) *Peer {
50+
return NewPeer(&BzzPeer{BzzAddr: testKadPeerAddr(s), LightNode: lightNode}, k)
5151
}
5252

5353
func On(k *Kademlia, ons ...string) {
5454
for _, s := range ons {
55-
k.On(newTestKadPeer(k, s))
55+
k.On(newTestKadPeer(k, s, false))
5656
}
5757
}
5858

5959
func Off(k *Kademlia, offs ...string) {
6060
for _, s := range offs {
61-
k.Off(newTestKadPeer(k, s))
61+
k.Off(newTestKadPeer(k, s, false))
6262
}
6363
}
6464

@@ -254,6 +254,56 @@ func TestSuggestPeerFindPeers(t *testing.T) {
254254

255255
}
256256

257+
// a node should stay in the address book if it's removed from the kademlia
258+
func TestOffEffectingAddressBookNormalNode(t *testing.T) {
259+
k := newTestKademlia("00000000")
260+
// peer added to kademlia
261+
k.On(newTestKadPeer(k, "01000000", false))
262+
// peer should be in the address book
263+
if k.addrs.Size() != 1 {
264+
t.Fatal("known peer addresses should contain 1 entry")
265+
}
266+
// peer should be among live connections
267+
if k.conns.Size() != 1 {
268+
t.Fatal("live peers should contain 1 entry")
269+
}
270+
// remove peer from kademlia
271+
k.Off(newTestKadPeer(k, "01000000", false))
272+
// peer should be in the address book
273+
if k.addrs.Size() != 1 {
274+
t.Fatal("known peer addresses should contain 1 entry")
275+
}
276+
// peer should not be among live connections
277+
if k.conns.Size() != 0 {
278+
t.Fatal("live peers should contain 0 entry")
279+
}
280+
}
281+
282+
// a light node should not be in the address book
283+
func TestOffEffectingAddressBookLightNode(t *testing.T) {
284+
k := newTestKademlia("00000000")
285+
// light node peer added to kademlia
286+
k.On(newTestKadPeer(k, "01000000", true))
287+
// peer should not be in the address book
288+
if k.addrs.Size() != 0 {
289+
t.Fatal("known peer addresses should contain 0 entry")
290+
}
291+
// peer should be among live connections
292+
if k.conns.Size() != 1 {
293+
t.Fatal("live peers should contain 1 entry")
294+
}
295+
// remove peer from kademlia
296+
k.Off(newTestKadPeer(k, "01000000", true))
297+
// peer should not be in the address book
298+
if k.addrs.Size() != 0 {
299+
t.Fatal("known peer addresses should contain 0 entry")
300+
}
301+
// peer should not be among live connections
302+
if k.conns.Size() != 0 {
303+
t.Fatal("live peers should contain 0 entry")
304+
}
305+
}
306+
257307
func TestSuggestPeerRetries(t *testing.T) {
258308
k := newTestKademlia("00000000")
259309
k.RetryInterval = int64(300 * time.Millisecond) // cycle

‎swarm/network/stream/delivery.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,10 @@ func (d *Delivery) RequestFromPeers(ctx context.Context, req *network.Request) (
245245
} else {
246246
d.kad.EachConn(req.Addr[:], 255, func(p *network.Peer, po int, nn bool) bool {
247247
id := p.ID()
248-
// TODO: skip light nodes that do not accept retrieve requests
248+
if p.LightNode {
249+
// skip light nodes
250+
return true
251+
}
249252
if req.SkipPeer(id.String()) {
250253
log.Trace("Delivery.RequestFromPeers: skip peer", "peer id", id)
251254
return true

‎swarm/network/stream/delivery_test.go‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ import (
2929

3030
"github.com/ethereum/go-ethereum/node"
3131
"github.com/ethereum/go-ethereum/p2p"
32+
"github.com/ethereum/go-ethereum/p2p/enode"
33+
"github.com/ethereum/go-ethereum/p2p/protocols"
3234
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
3335
p2ptest "github.com/ethereum/go-ethereum/p2p/testing"
3436
"github.com/ethereum/go-ethereum/swarm/log"
3537
"github.com/ethereum/go-ethereum/swarm/network"
38+
pq "github.com/ethereum/go-ethereum/swarm/network/priorityqueue"
3639
"github.com/ethereum/go-ethereum/swarm/network/simulation"
3740
"github.com/ethereum/go-ethereum/swarm/state"
3841
"github.com/ethereum/go-ethereum/swarm/storage"
@@ -274,6 +277,86 @@ func TestStreamerUpstreamRetrieveRequestMsgExchange(t *testing.T) {
274277
}
275278
}
276279

280+
// if there is one peer in the Kademlia, RequestFromPeers should return it
281+
func TestRequestFromPeers(t *testing.T) {
282+
dummyPeerID := enode.HexID("3431c3939e1ee2a6345e976a8234f9870152d64879f30bc272a074f6859e75e8")
283+
284+
addr := network.RandomAddr()
285+
to := network.NewKademlia(addr.OAddr, network.NewKadParams())
286+
delivery := NewDelivery(to, nil)
287+
protocolsPeer := protocols.NewPeer(p2p.NewPeer(dummyPeerID, "dummy", nil), nil, nil)
288+
peer := network.NewPeer(&network.BzzPeer{
289+
BzzAddr: network.RandomAddr(),
290+
LightNode: false,
291+
Peer: protocolsPeer,
292+
}, to)
293+
to.On(peer)
294+
r := NewRegistry(addr.ID(), delivery, nil, nil, nil)
295+
296+
// an empty priorityQueue has to be created to prevent a goroutine being called after the test has finished
297+
sp := &Peer{
298+
Peer: protocolsPeer,
299+
pq: pq.New(int(PriorityQueue), PriorityQueueCap),
300+
streamer: r,
301+
}
302+
r.setPeer(sp)
303+
req := network.NewRequest(
304+
storage.Address(hash0[:]),
305+
true,
306+
&sync.Map{},
307+
)
308+
ctx := context.Background()
309+
id, _, err := delivery.RequestFromPeers(ctx, req)
310+
311+
if err != nil {
312+
t.Fatal(err)
313+
}
314+
if *id != dummyPeerID {
315+
t.Fatalf("Expected an id, got %v", id)
316+
}
317+
}
318+
319+
// RequestFromPeers should not return light nodes
320+
func TestRequestFromPeersWithLightNode(t *testing.T) {
321+
dummyPeerID := enode.HexID("3431c3939e1ee2a6345e976a8234f9870152d64879f30bc272a074f6859e75e8")
322+
323+
addr := network.RandomAddr()
324+
to := network.NewKademlia(addr.OAddr, network.NewKadParams())
325+
delivery := NewDelivery(to, nil)
326+
327+
protocolsPeer := protocols.NewPeer(p2p.NewPeer(dummyPeerID, "dummy", nil), nil, nil)
328+
// setting up a lightnode
329+
peer := network.NewPeer(&network.BzzPeer{
330+
BzzAddr: network.RandomAddr(),
331+
LightNode: true,
332+
Peer: protocolsPeer,
333+
}, to)
334+
to.On(peer)
335+
r := NewRegistry(addr.ID(), delivery, nil, nil, nil)
336+
// an empty priorityQueue has to be created to prevent a goroutine being called after the test has finished
337+
sp := &Peer{
338+
Peer: protocolsPeer,
339+
pq: pq.New(int(PriorityQueue), PriorityQueueCap),
340+
streamer: r,
341+
}
342+
r.setPeer(sp)
343+
344+
req := network.NewRequest(
345+
storage.Address(hash0[:]),
346+
true,
347+
&sync.Map{},
348+
)
349+
350+
ctx := context.Background()
351+
// making a request which should return with "no peer found"
352+
_, _, err := delivery.RequestFromPeers(ctx, req)
353+
354+
expectedError := "no peer found"
355+
if err.Error() != expectedError {
356+
t.Fatalf("expected '%v', got %v", expectedError, err)
357+
}
358+
}
359+
277360
func TestStreamerDownstreamChunkDeliveryMsgExchange(t *testing.T) {
278361
tester, streamer, localStore, teardown, err := newStreamerTester(t, &RegistryOptions{
279362
Retrieval: RetrievalDisabled,

0 commit comments

Comments
 (0)