Skip to content
This repository was archived by the owner on Aug 2, 2021. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 49 additions & 7 deletions network/simulation/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package simulation
import (
"context"
"errors"
"io/ioutil"
"net/http"
"os"
"sync"
"time"

Expand All @@ -31,6 +33,11 @@ import (
"github.com/ethersphere/swarm/network"
)

const (
SimulationTypeInproc = iota
SimulationTypeExec
)

// Common errors that are returned by functions in this package.
var (
ErrNodeNotFound = errors.New("node not found")
Expand All @@ -50,6 +57,8 @@ type Simulation struct {
done chan struct{}
mu sync.RWMutex
neighbourhoodSize int
baseDir string
typ int

httpSrv *http.Server //attach a HTTP server via SimulationOptions
handler *simulations.Server //HTTP handler for the server
Expand All @@ -76,8 +85,44 @@ func New(services map[string]ServiceFunc) (s *Simulation) {
buckets: make(map[enode.ID]*sync.Map),
done: make(chan struct{}),
neighbourhoodSize: network.NewKadParams().NeighbourhoodSize,
typ: SimulationTypeInproc,
}

adapterServices := addServices(s, services)

s.Net = simulations.NewNetwork(
adapters.NewTCPAdapter(adapterServices),
&simulations.NetworkConfig{ID: "0"},
)

return s
}

// NewExec does the same as New but lets the caller specify the adapter to use
func NewExec(services map[string]ServiceFunc) (s *Simulation, err error) {
Comment thread
nolash marked this conversation as resolved.
s = &Simulation{
buckets: make(map[enode.ID]*sync.Map),
done: make(chan struct{}),
neighbourhoodSize: network.NewKadParams().NeighbourhoodSize,
typ: SimulationTypeExec,
}

adapterServices := addServices(s, services)
adapters.RegisterServices(adapterServices)

@skylenet skylenet Jun 26, 2019

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nolash We have a problem with this RegisterServices() function. It should be called within an init() function as described in the documentation: https://github.com/ethereum/go-ethereum/tree/master/p2p/simulations#services

So basically, to make the current exec/docker adapter work. we would have to register all services upfront.
In our case it gets a bit harder, because we attached this to (s *Simulation)... so unless we define all our simulations inside an init(), this won't work.

I've been thinking now about this for a while and I don't find a decent solution.... any idea?

fyi @nonsense

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had completely forgotten about this requirement, I remember reading about it a year or so ago...

I also don't have any solutions :(

@nolash nolash Jun 27, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In our case it gets a bit harder

Why exactly do we need to put it in an init (except for the doc claiming that we do)?


s.baseDir, err = ioutil.TempDir("", "swarm-sim")
if err != nil {
return nil, err
}
s.Net = simulations.NewNetwork(
adapters.NewExecAdapter(s.baseDir),
&simulations.NetworkConfig{ID: "0"},
)

return s, nil
}

func addServices(s *Simulation, services map[string]ServiceFunc) map[string]adapters.ServiceFunc {
Comment thread
nolash marked this conversation as resolved.
Outdated
adapterServices := make(map[string]adapters.ServiceFunc, len(services))
for name, serviceFunc := range services {
// Scope this variables correctly
Expand All @@ -102,13 +147,7 @@ func New(services map[string]ServiceFunc) (s *Simulation) {
return service, nil
}
}

s.Net = simulations.NewNetwork(
adapters.NewTCPAdapter(adapterServices),
&simulations.NetworkConfig{ID: "0"},
)

return s
return adapterServices
}

// RunFunc is the function that will be called
Expand Down Expand Up @@ -208,6 +247,9 @@ func (s *Simulation) Close() {

s.shutdownWG.Wait()
s.Net.Shutdown()
if s.baseDir != "" {
os.RemoveAll(s.baseDir)
}
}

// Done returns a channel that is closed when the simulation
Expand Down