aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/network
diff options
context:
space:
mode:
Diffstat (limited to 'swarm/network')
-rw-r--r--swarm/network/simulation/node.go32
-rw-r--r--swarm/network/simulation/simulation.go9
-rw-r--r--swarm/network/simulations/discovery/discovery_test.go1
3 files changed, 34 insertions, 8 deletions
diff --git a/swarm/network/simulation/node.go b/swarm/network/simulation/node.go
index e24dab21b..46c2bb866 100644
--- a/swarm/network/simulation/node.go
+++ b/swarm/network/simulation/node.go
@@ -17,20 +17,28 @@
package simulation
import (
+ "bytes"
"context"
+ "crypto/ecdsa"
"encoding/json"
"errors"
"io/ioutil"
"math/rand"
"os"
+ "sync"
"time"
+ "github.com/ethereum/go-ethereum/crypto"
"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"
)
+var (
+ BucketKeyBzzPrivateKey BucketKey = "bzzprivkey"
+)
+
// NodeIDs returns NodeIDs for all nodes in the network.
func (s *Simulation) NodeIDs() (ids []enode.ID) {
nodes := s.Net.GetNodes()
@@ -104,13 +112,15 @@ func (s *Simulation) AddNode(opts ...AddNodeOption) (id enode.ID, err error) {
// for now we have no way of setting bootnodes or lightnodes in sims
// so we just let them be set to false
// they should perhaps be possible to override them with AddNodeOption
- enodeParams := &network.EnodeParams{
- PrivateKey: conf.PrivateKey,
- }
- record, err := network.NewEnodeRecord(enodeParams)
+ bzzPrivateKey, err := BzzPrivateKeyFromConfig(conf)
if err != nil {
return enode.ID{}, err
}
+
+ enodeParams := &network.EnodeParams{
+ PrivateKey: bzzPrivateKey,
+ }
+ record, err := network.NewEnodeRecord(enodeParams)
conf.Record = *record
// Add the bzz address to the node config
@@ -118,6 +128,8 @@ func (s *Simulation) AddNode(opts ...AddNodeOption) (id enode.ID, err error) {
if err != nil {
return id, err
}
+ s.buckets[node.ID()] = new(sync.Map)
+ s.SetNodeItem(node.ID(), BucketKeyBzzPrivateKey, bzzPrivateKey)
return node.ID(), s.Net.Start(node.ID())
}
@@ -315,3 +327,15 @@ func (s *Simulation) StopRandomNodes(count int) (ids []enode.ID, err error) {
func init() {
rand.Seed(time.Now().UnixNano())
}
+
+// derive a private key for swarm for the node key
+// returns the private key used to generate the bzz key
+func BzzPrivateKeyFromConfig(conf *adapters.NodeConfig) (*ecdsa.PrivateKey, error) {
+ // pad the seed key some arbitrary data as ecdsa.GenerateKey takes 40 bytes seed data
+ privKeyBuf := append(crypto.FromECDSA(conf.PrivateKey), []byte{0x62, 0x7a, 0x7a, 0x62, 0x7a, 0x7a, 0x62, 0x7a}...)
+ bzzPrivateKey, err := ecdsa.GenerateKey(crypto.S256(), bytes.NewReader(privKeyBuf))
+ if err != nil {
+ return nil, err
+ }
+ return bzzPrivateKey, nil
+}
diff --git a/swarm/network/simulation/simulation.go b/swarm/network/simulation/simulation.go
index e18d19a67..5787cafda 100644
--- a/swarm/network/simulation/simulation.go
+++ b/swarm/network/simulation/simulation.go
@@ -85,13 +85,16 @@ func New(services map[string]ServiceFunc) (s *Simulation) {
name, serviceFunc := name, serviceFunc
s.serviceNames = append(s.serviceNames, name)
adapterServices[name] = func(ctx *adapters.ServiceContext) (node.Service, error) {
- b := new(sync.Map)
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ b, ok := s.buckets[ctx.Config.ID]
+ if !ok {
+ b = new(sync.Map)
+ }
service, cleanup, err := serviceFunc(ctx, b)
if err != nil {
return nil, err
}
- s.mu.Lock()
- defer s.mu.Unlock()
if cleanup != nil {
s.cleanupFuncs = append(s.cleanupFuncs, cleanup)
}
diff --git a/swarm/network/simulations/discovery/discovery_test.go b/swarm/network/simulations/discovery/discovery_test.go
index 1ad9ac2e9..99a0c7722 100644
--- a/swarm/network/simulations/discovery/discovery_test.go
+++ b/swarm/network/simulations/discovery/discovery_test.go
@@ -131,7 +131,6 @@ func BenchmarkDiscovery_128_4(b *testing.B) { benchmarkDiscovery(b, 128, 4) }
func BenchmarkDiscovery_256_4(b *testing.B) { benchmarkDiscovery(b, 256, 4) }
func TestDiscoverySimulationExecAdapter(t *testing.T) {
- t.Skip("This is left broken pending ENR preparations for swarm binary. Execadapter is not currently in use, so a short pause won't hurt")
testDiscoverySimulationExecAdapter(t, *nodeCount, *initCount)
}