diff --git a/swarm/network/simulation/kademlia.go b/swarm/network/simulation/kademlia.go index f52d3544e7..d11af654c5 100644 --- a/swarm/network/simulation/kademlia.go +++ b/swarm/network/simulation/kademlia.go @@ -19,7 +19,6 @@ package simulation import ( "context" "encoding/hex" - "errors" "time" "github.com/ethereum/go-ethereum/common" @@ -41,17 +40,10 @@ func (s *Simulation) WaitTillHealthy(ctx context.Context) (ill map[enode.ID]*net kademlias := s.kademlias() addrs := make([][]byte, 0, len(kademlias)) // TODO verify that all kademlias have same params - var minProxBinSize int for _, k := range kademlias { - if minProxBinSize == 0 { - minProxBinSize = k.MinProxBinSize - } addrs = append(addrs, k.BaseAddr()) } - if minProxBinSize == 0 { - return nil, errors.New("no kademlias in simulation") - } - ppmap = network.NewPeerPotMap(minProxBinSize, addrs) + ppmap = network.NewPeerPotMap(s.minProxBinSize, addrs) // Wait for healthy Kademlia on every node before checking files ticker := time.NewTicker(200 * time.Millisecond) diff --git a/swarm/network/simulation/simulation.go b/swarm/network/simulation/simulation.go index 747faf5d77..81769df88e 100644 --- a/swarm/network/simulation/simulation.go +++ b/swarm/network/simulation/simulation.go @@ -28,6 +28,7 @@ import ( "github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/simulations" "github.com/ethereum/go-ethereum/p2p/simulations/adapters" + "github.com/ethereum/go-ethereum/swarm/network" ) // Common errors that are returned by functions in this package. @@ -42,13 +43,14 @@ type Simulation struct { // of p2p/simulations.Network. Net *simulations.Network - serviceNames []string - cleanupFuncs []func() - buckets map[enode.ID]*sync.Map - pivotNodeID *enode.ID - shutdownWG sync.WaitGroup - done chan struct{} - mu sync.RWMutex + serviceNames []string + cleanupFuncs []func() + buckets map[enode.ID]*sync.Map + pivotNodeID *enode.ID + shutdownWG sync.WaitGroup + done chan struct{} + mu sync.RWMutex + minProxBinSize int httpSrv *http.Server //attach a HTTP server via SimulationOptions handler *simulations.Server //HTTP handler for the server @@ -65,16 +67,16 @@ type Simulation struct { // after network shutdown. type ServiceFunc func(ctx *adapters.ServiceContext, bucket *sync.Map) (s node.Service, cleanup func(), err error) -// New creates a new Simulation instance with new -// simulations.Network initialized with provided services. +// New creates a new simulation instance // 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), - done: make(chan struct{}), + buckets: make(map[enode.ID]*sync.Map), + done: make(chan struct{}), + minProxBinSize: network.NewKadParams().MinProxBinSize, } adapterServices := make(map[string]adapters.ServiceFunc, len(services)) diff --git a/swarm/network/simulations/discovery/discovery_test.go b/swarm/network/simulations/discovery/discovery_test.go index 73e36c2eb8..c0fcd0d083 100644 --- a/swarm/network/simulations/discovery/discovery_test.go +++ b/swarm/network/simulations/discovery/discovery_test.go @@ -266,8 +266,7 @@ func discoverySimulation(nodes, conns int, adapter adapters.NodeAdapter) (*simul wg.Wait() log.Debug(fmt.Sprintf("nodes: %v", len(addrs))) // construct the peer pot, so that kademlia health can be checked - k := network.NewKademlia(addrs[0], network.NewKadParams()) - ppmap := network.NewPeerPotMap(k, addrs) + ppmap := network.NewPeerPotMap(network.NewKadParams().MinProxBinSize, addrs) check := func(ctx context.Context, id enode.ID) (bool, error) { select { case <-ctx.Done(): @@ -403,8 +402,7 @@ func discoveryPersistenceSimulation(nodes, conns int, adapter adapters.NodeAdapt } healthy := &network.Health{} addr := id.String() - k := network.NewKademlia(common.Hex2Bytes(addr), network.NewKadParams()) - ppmap := network.NewPeerPotMap(k, addrs) + ppmap := network.NewPeerPotMap(network.NewKadParams().MinProxBinSize, addrs) if err := client.Call(&healthy, "hive_healthy", ppmap); err != nil { return fmt.Errorf("error getting node health: %s", err) } @@ -492,8 +490,7 @@ func discoveryPersistenceSimulation(nodes, conns int, adapter adapters.NodeAdapt return false, fmt.Errorf("error getting node client: %s", err) } healthy := &network.Health{} - k := network.NewKademlia(addrs[0], network.NewKadParams()) - ppmap := network.NewPeerPotMap(k, addrs) + ppmap := network.NewPeerPotMap(network.NewKadParams().MinProxBinSize, addrs) if err := client.Call(&healthy, "hive_healthy", ppmap); err != nil { return false, fmt.Errorf("error getting node health: %s", err) diff --git a/swarm/network/stream/common_test.go b/swarm/network/stream/common_test.go index 29b917d39c..a30cfc0533 100644 --- a/swarm/network/stream/common_test.go +++ b/swarm/network/stream/common_test.go @@ -55,8 +55,9 @@ var ( bucketKeyDelivery = simulation.BucketKey("delivery") bucketKeyRegistry = simulation.BucketKey("registry") - chunkSize = 4096 - pof = network.Pof + chunkSize = 4096 + pof = network.Pof + minProxBinSize = network.NewKadParams().MinProxBinSize ) func init() { diff --git a/swarm/network/stream/delivery_test.go b/swarm/network/stream/delivery_test.go index 16be99f354..2912c569a4 100644 --- a/swarm/network/stream/delivery_test.go +++ b/swarm/network/stream/delivery_test.go @@ -453,7 +453,6 @@ func TestDeliveryFromNodes(t *testing.T) { } func testDeliveryFromNodes(t *testing.T, nodes, conns, chunkCount int, skipCheck bool) { - sim := simulation.New(map[string]simulation.ServiceFunc{ "streamer": func(ctx *adapters.ServiceContext, bucket *sync.Map) (s node.Service, cleanup func(), err error) { node := ctx.Config.Node() diff --git a/swarm/network/stream/snapshot_retrieval_test.go b/swarm/network/stream/snapshot_retrieval_test.go index 942e431e13..d345ac8d02 100644 --- a/swarm/network/stream/snapshot_retrieval_test.go +++ b/swarm/network/stream/snapshot_retrieval_test.go @@ -246,7 +246,6 @@ simulation's `action` function. The snapshot should have 'streamer' in its service list. */ func runRetrievalTest(chunkCount int, nodeCount int) error { - sim := simulation.New(retrievalSimServiceMap) defer sim.Close() diff --git a/swarm/network/stream/snapshot_sync_test.go b/swarm/network/stream/snapshot_sync_test.go index 7079617e65..41d2ee314a 100644 --- a/swarm/network/stream/snapshot_sync_test.go +++ b/swarm/network/stream/snapshot_sync_test.go @@ -182,7 +182,6 @@ func streamerFunc(ctx *adapters.ServiceContext, bucket *sync.Map) (s node.Servic } func testSyncingViaGlobalSync(t *testing.T, chunkCount int, nodeCount int) { - sim := simulation.New(simServiceMap) defer sim.Close()