From 06e956276ceb9022c1e875d481fbadd6fe1b40a2 Mon Sep 17 00:00:00 2001 From: Anton Evangelatov Date: Wed, 21 Nov 2018 12:21:36 +0100 Subject: [PATCH 1/4] swarm/network: fix logs --- swarm/network/stream/delivery.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swarm/network/stream/delivery.go b/swarm/network/stream/delivery.go index 64d754336f93..a5c9fa7e86d7 100644 --- a/swarm/network/stream/delivery.go +++ b/swarm/network/stream/delivery.go @@ -255,7 +255,7 @@ func (d *Delivery) RequestFromPeers(ctx context.Context, req *network.Request) ( } sp = d.getPeer(id) if sp == nil { - log.Warn("Delivery.RequestFromPeers: peer not found", "id", id) + //log.Warn("Delivery.RequestFromPeers: peer not found", "id", id) return true } spID = &id From f0da48c19a82f2477f92f2b672f6c81130bd7823 Mon Sep 17 00:00:00 2001 From: Janos Guljas Date: Thu, 22 Nov 2018 10:38:08 +0100 Subject: [PATCH 2/4] swarm/network/simulation: improve New function comment --- swarm/network/simulation/simulation.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/swarm/network/simulation/simulation.go b/swarm/network/simulation/simulation.go index f6d3ce229087..46133aaddf89 100644 --- a/swarm/network/simulation/simulation.go +++ b/swarm/network/simulation/simulation.go @@ -68,6 +68,10 @@ type ServiceFunc func(ctx *adapters.ServiceContext, bucket *sync.Map) (s node.Se // New creates a new Simulation instance with new // simulations.Network initialized with provided services. +// Services map must have unique keys as service names and +// every ServiceFunc must return a node.Service of the unique type. +// This restriction is required by node.Node.Start() function +// which is used to start node.Service returned by ServiceFunc. func New(services map[string]ServiceFunc) (s *Simulation) { s = &Simulation{ buckets: make(map[enode.ID]*sync.Map), From ae922ffb62a559c3dd54db06d22ae8a13ea473cf Mon Sep 17 00:00:00 2001 From: Janos Guljas Date: Thu, 22 Nov 2018 11:15:28 +0100 Subject: [PATCH 3/4] swarm/network/simulation: fix multiple services issue --- swarm/network/simulation/node_test.go | 35 +++++++++++++++++++++ swarm/network/simulation/simulation.go | 3 ++ swarm/network/simulation/simulation_test.go | 31 ++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/swarm/network/simulation/node_test.go b/swarm/network/simulation/node_test.go index 086ab606f6c6..01346ef14df5 100644 --- a/swarm/network/simulation/node_test.go +++ b/swarm/network/simulation/node_test.go @@ -160,6 +160,41 @@ func TestAddNodeWithService(t *testing.T) { } } +func TestAddNodeMultipleServices(t *testing.T) { + sim := New(map[string]ServiceFunc{ + "noop1": noopServiceFunc, + "noop2": noopService2Func, + }) + defer sim.Close() + + id, err := sim.AddNode() + if err != nil { + t.Fatal(err) + } + + n := sim.Net.GetNode(id).Node.(*adapters.SimNode) + if n.Service("noop1") == nil { + t.Error("service noop1 not found on node") + } + if n.Service("noop2") == nil { + t.Error("service noop2 not found on node") + } +} + +func TestAddNodeDuplicateServiceError(t *testing.T) { + sim := New(map[string]ServiceFunc{ + "noop1": noopServiceFunc, + "noop2": noopServiceFunc, + }) + defer sim.Close() + + wantErr := "duplicate service: *simulation.noopService" + _, err := sim.AddNode() + if err.Error() != wantErr { + t.Errorf("got error %q, want %q", err, wantErr) + } +} + func TestAddNodes(t *testing.T) { sim := New(noopServiceFuncMap) defer sim.Close() diff --git a/swarm/network/simulation/simulation.go b/swarm/network/simulation/simulation.go index 46133aaddf89..e5435b9f0e53 100644 --- a/swarm/network/simulation/simulation.go +++ b/swarm/network/simulation/simulation.go @@ -80,6 +80,9 @@ func New(services map[string]ServiceFunc) (s *Simulation) { adapterServices := make(map[string]adapters.ServiceFunc, len(services)) for name, serviceFunc := range services { + // Scope this variables correctly + // as they will be in the adapterServices[name] function accessed later. + name, serviceFunc := name, serviceFunc s.serviceNames = append(s.serviceNames, name) adapterServices[name] = func(ctx *adapters.ServiceContext) (node.Service, error) { b := new(sync.Map) diff --git a/swarm/network/simulation/simulation_test.go b/swarm/network/simulation/simulation_test.go index eed09bf508d5..ec1df87c82a0 100644 --- a/swarm/network/simulation/simulation_test.go +++ b/swarm/network/simulation/simulation_test.go @@ -205,3 +205,34 @@ func (t *noopService) Start(server *p2p.Server) error { func (t *noopService) Stop() error { return nil } + +// a helper function for most basic noop service +// of a different type then noopService to test +// multiple services on one node. +func noopService2Func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) { + return newNoopService2(), nil, nil +} + +// noopService2 is the service that does not do anything +// but implements node.Service interface. +type noopService2 struct{} + +func newNoopService2() node.Service { + return &noopService2{} +} + +func (t *noopService2) Protocols() []p2p.Protocol { + return []p2p.Protocol{} +} + +func (t *noopService2) APIs() []rpc.API { + return []rpc.API{} +} + +func (t *noopService2) Start(server *p2p.Server) error { + return nil +} + +func (t *noopService2) Stop() error { + return nil +} From f4958c0e0618c326fe63a930ed4e171c5e5f65a8 Mon Sep 17 00:00:00 2001 From: Janos Guljas Date: Fri, 23 Nov 2018 09:55:48 +0100 Subject: [PATCH 4/4] swamr/network/simulation: simplify noopService2 in tests --- swarm/network/simulation/simulation_test.go | 24 +++------------------ 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/swarm/network/simulation/simulation_test.go b/swarm/network/simulation/simulation_test.go index ec1df87c82a0..ca8599d7c0f4 100644 --- a/swarm/network/simulation/simulation_test.go +++ b/swarm/network/simulation/simulation_test.go @@ -210,29 +210,11 @@ func (t *noopService) Stop() error { // of a different type then noopService to test // multiple services on one node. func noopService2Func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) { - return newNoopService2(), nil, nil + return new(noopService2), nil, nil } // noopService2 is the service that does not do anything // but implements node.Service interface. -type noopService2 struct{} - -func newNoopService2() node.Service { - return &noopService2{} -} - -func (t *noopService2) Protocols() []p2p.Protocol { - return []p2p.Protocol{} -} - -func (t *noopService2) APIs() []rpc.API { - return []rpc.API{} -} - -func (t *noopService2) Start(server *p2p.Server) error { - return nil -} - -func (t *noopService2) Stop() error { - return nil +type noopService2 struct { + noopService }