@@ -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+
277360func TestStreamerDownstreamChunkDeliveryMsgExchange (t * testing.T ) {
278361 tester , streamer , localStore , teardown , err := newStreamerTester (t , & RegistryOptions {
279362 Retrieval : RetrievalDisabled ,
0 commit comments