Skip to content

Commit 93854bb

Browse files
janosnonsense
authored andcommitted
swarm/network/simulation: fix New function for-loop scope (#18161)
1 parent f051580 commit 93854bb

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

swarm/network/simulation/node_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,41 @@ func TestAddNodeWithService(t *testing.T) {
160160
}
161161
}
162162

163+
func TestAddNodeMultipleServices(t *testing.T) {
164+
sim := New(map[string]ServiceFunc{
165+
"noop1": noopServiceFunc,
166+
"noop2": noopService2Func,
167+
})
168+
defer sim.Close()
169+
170+
id, err := sim.AddNode()
171+
if err != nil {
172+
t.Fatal(err)
173+
}
174+
175+
n := sim.Net.GetNode(id).Node.(*adapters.SimNode)
176+
if n.Service("noop1") == nil {
177+
t.Error("service noop1 not found on node")
178+
}
179+
if n.Service("noop2") == nil {
180+
t.Error("service noop2 not found on node")
181+
}
182+
}
183+
184+
func TestAddNodeDuplicateServiceError(t *testing.T) {
185+
sim := New(map[string]ServiceFunc{
186+
"noop1": noopServiceFunc,
187+
"noop2": noopServiceFunc,
188+
})
189+
defer sim.Close()
190+
191+
wantErr := "duplicate service: *simulation.noopService"
192+
_, err := sim.AddNode()
193+
if err.Error() != wantErr {
194+
t.Errorf("got error %q, want %q", err, wantErr)
195+
}
196+
}
197+
163198
func TestAddNodes(t *testing.T) {
164199
sim := New(noopServiceFuncMap)
165200
defer sim.Close()

swarm/network/simulation/simulation.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ type ServiceFunc func(ctx *adapters.ServiceContext, bucket *sync.Map) (s node.Se
6868

6969
// New creates a new Simulation instance with new
7070
// simulations.Network initialized with provided services.
71+
// Services map must have unique keys as service names and
72+
// every ServiceFunc must return a node.Service of the unique type.
73+
// This restriction is required by node.Node.Start() function
74+
// which is used to start node.Service returned by ServiceFunc.
7175
func New(services map[string]ServiceFunc) (s *Simulation) {
7276
s = &Simulation{
7377
buckets: make(map[enode.ID]*sync.Map),
@@ -76,6 +80,9 @@ func New(services map[string]ServiceFunc) (s *Simulation) {
7680

7781
adapterServices := make(map[string]adapters.ServiceFunc, len(services))
7882
for name, serviceFunc := range services {
83+
// Scope this variables correctly
84+
// as they will be in the adapterServices[name] function accessed later.
85+
name, serviceFunc := name, serviceFunc
7986
s.serviceNames = append(s.serviceNames, name)
8087
adapterServices[name] = func(ctx *adapters.ServiceContext) (node.Service, error) {
8188
b := new(sync.Map)

swarm/network/simulation/simulation_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,16 @@ func (t *noopService) Start(server *p2p.Server) error {
205205
func (t *noopService) Stop() error {
206206
return nil
207207
}
208+
209+
// a helper function for most basic noop service
210+
// of a different type then noopService to test
211+
// multiple services on one node.
212+
func noopService2Func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) {
213+
return new(noopService2), nil, nil
214+
}
215+
216+
// noopService2 is the service that does not do anything
217+
// but implements node.Service interface.
218+
type noopService2 struct {
219+
noopService
220+
}

swarm/network/stream/delivery.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func (d *Delivery) RequestFromPeers(ctx context.Context, req *network.Request) (
255255
}
256256
sp = d.getPeer(id)
257257
if sp == nil {
258-
log.Warn("Delivery.RequestFromPeers: peer not found", "id", id)
258+
//log.Warn("Delivery.RequestFromPeers: peer not found", "id", id)
259259
return true
260260
}
261261
spID = &id

0 commit comments

Comments
 (0)