forked from cerc-io/plugeth
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
This commit is contained in:
committed by
Viktor Trón
parent
0bcff8f525
commit
81533deae5
@@ -245,7 +245,10 @@ func (d *Delivery) RequestFromPeers(ctx context.Context, req *network.Request) (
|
||||
} else {
|
||||
d.kad.EachConn(req.Addr[:], 255, func(p *network.Peer, po int, nn bool) bool {
|
||||
id := p.ID()
|
||||
// TODO: skip light nodes that do not accept retrieve requests
|
||||
if p.LightNode {
|
||||
// skip light nodes
|
||||
return true
|
||||
}
|
||||
if req.SkipPeer(id.String()) {
|
||||
log.Trace("Delivery.RequestFromPeers: skip peer", "peer id", id)
|
||||
return true
|
||||
|
||||
@@ -29,10 +29,13 @@ import (
|
||||
|
||||
"github.com/ethereum/go-ethereum/node"
|
||||
"github.com/ethereum/go-ethereum/p2p"
|
||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||
"github.com/ethereum/go-ethereum/p2p/protocols"
|
||||
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
|
||||
p2ptest "github.com/ethereum/go-ethereum/p2p/testing"
|
||||
"github.com/ethereum/go-ethereum/swarm/log"
|
||||
"github.com/ethereum/go-ethereum/swarm/network"
|
||||
pq "github.com/ethereum/go-ethereum/swarm/network/priorityqueue"
|
||||
"github.com/ethereum/go-ethereum/swarm/network/simulation"
|
||||
"github.com/ethereum/go-ethereum/swarm/state"
|
||||
"github.com/ethereum/go-ethereum/swarm/storage"
|
||||
@@ -274,6 +277,86 @@ func TestStreamerUpstreamRetrieveRequestMsgExchange(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// if there is one peer in the Kademlia, RequestFromPeers should return it
|
||||
func TestRequestFromPeers(t *testing.T) {
|
||||
dummyPeerID := enode.HexID("3431c3939e1ee2a6345e976a8234f9870152d64879f30bc272a074f6859e75e8")
|
||||
|
||||
addr := network.RandomAddr()
|
||||
to := network.NewKademlia(addr.OAddr, network.NewKadParams())
|
||||
delivery := NewDelivery(to, nil)
|
||||
protocolsPeer := protocols.NewPeer(p2p.NewPeer(dummyPeerID, "dummy", nil), nil, nil)
|
||||
peer := network.NewPeer(&network.BzzPeer{
|
||||
BzzAddr: network.RandomAddr(),
|
||||
LightNode: false,
|
||||
Peer: protocolsPeer,
|
||||
}, to)
|
||||
to.On(peer)
|
||||
r := NewRegistry(addr.ID(), delivery, nil, nil, nil)
|
||||
|
||||
// an empty priorityQueue has to be created to prevent a goroutine being called after the test has finished
|
||||
sp := &Peer{
|
||||
Peer: protocolsPeer,
|
||||
pq: pq.New(int(PriorityQueue), PriorityQueueCap),
|
||||
streamer: r,
|
||||
}
|
||||
r.setPeer(sp)
|
||||
req := network.NewRequest(
|
||||
storage.Address(hash0[:]),
|
||||
true,
|
||||
&sync.Map{},
|
||||
)
|
||||
ctx := context.Background()
|
||||
id, _, err := delivery.RequestFromPeers(ctx, req)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if *id != dummyPeerID {
|
||||
t.Fatalf("Expected an id, got %v", id)
|
||||
}
|
||||
}
|
||||
|
||||
// RequestFromPeers should not return light nodes
|
||||
func TestRequestFromPeersWithLightNode(t *testing.T) {
|
||||
dummyPeerID := enode.HexID("3431c3939e1ee2a6345e976a8234f9870152d64879f30bc272a074f6859e75e8")
|
||||
|
||||
addr := network.RandomAddr()
|
||||
to := network.NewKademlia(addr.OAddr, network.NewKadParams())
|
||||
delivery := NewDelivery(to, nil)
|
||||
|
||||
protocolsPeer := protocols.NewPeer(p2p.NewPeer(dummyPeerID, "dummy", nil), nil, nil)
|
||||
// setting up a lightnode
|
||||
peer := network.NewPeer(&network.BzzPeer{
|
||||
BzzAddr: network.RandomAddr(),
|
||||
LightNode: true,
|
||||
Peer: protocolsPeer,
|
||||
}, to)
|
||||
to.On(peer)
|
||||
r := NewRegistry(addr.ID(), delivery, nil, nil, nil)
|
||||
// an empty priorityQueue has to be created to prevent a goroutine being called after the test has finished
|
||||
sp := &Peer{
|
||||
Peer: protocolsPeer,
|
||||
pq: pq.New(int(PriorityQueue), PriorityQueueCap),
|
||||
streamer: r,
|
||||
}
|
||||
r.setPeer(sp)
|
||||
|
||||
req := network.NewRequest(
|
||||
storage.Address(hash0[:]),
|
||||
true,
|
||||
&sync.Map{},
|
||||
)
|
||||
|
||||
ctx := context.Background()
|
||||
// making a request which should return with "no peer found"
|
||||
_, _, err := delivery.RequestFromPeers(ctx, req)
|
||||
|
||||
expectedError := "no peer found"
|
||||
if err.Error() != expectedError {
|
||||
t.Fatalf("expected '%v', got %v", expectedError, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStreamerDownstreamChunkDeliveryMsgExchange(t *testing.T) {
|
||||
tester, streamer, localStore, teardown, err := newStreamerTester(t, &RegistryOptions{
|
||||
Retrieval: RetrievalDisabled,
|
||||
|
||||
Reference in New Issue
Block a user