From 2f5b6cb442dd1ab20279603de938e5b60e3122fb Mon Sep 17 00:00:00 2001 From: lash Date: Fri, 22 Mar 2019 21:37:25 +0100 Subject: swarm/network: Use different privatekey for bzz overlay in sim (#19313) * cmd/swarm, p2p, swarm: Enable ENR in binary/execadapter * cmd/p2p/swarm: Remove comments + config.Enode nomarshal * p2p/simulations: Remove superfluous error check * p2p/simulation: Move init enode comment * swarm, p2p/simulations, cmd/swarm: Use nodekey in binary record sign * swarm/network, swarm/pss: Dervice bzzkey * swarm/pss: Remove unused function * swarm/network: Store swarm private key in simulation bucket * swarm/pss: Shorten TextProxNetwork shortrunning test timeout * swarm/pss: Increase prox test timeout * swarm/pss: Increase timeout slightly on shortrunning proxtest * swarm/network: Simplify bucket instantiation in servicectx func * p2p/simulations: Tcpport -> udpport * swarm/network, swarm/pss: Simplify + correct lock in servicefunc sim * swarm/network: Cleanup after rebase on extract swarm enode new * p2p/simulations, swarm/network: Make exec disc test pass * swarm/network: Prune ye olde comment * swarm/pss: Correct revised bzzkey method call * swarm/network: Clarify comment about privatekey generation data * swarm/pss: Fix syntax errors after rebase * swarm/network: Rename misleadingly named method (amend commit to trigger ci - attempt 5) --- swarm/network/simulation/node.go | 32 +++++++++++++++++++--- swarm/network/simulation/simulation.go | 9 ++++-- .../simulations/discovery/discovery_test.go | 1 - 3 files changed, 34 insertions(+), 8 deletions(-) (limited to 'swarm/network') 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) } -- cgit v1.2.3