diff --git a/swarm/network/simulation/node.go b/swarm/network/simulation/node.go index 5210054530..09b6b35a25 100644 --- a/swarm/network/simulation/node.go +++ b/swarm/network/simulation/node.go @@ -25,6 +25,7 @@ import ( "io/ioutil" "math/rand" "os" + "sync" "time" "github.com/ethereum/go-ethereum/crypto" @@ -34,6 +35,10 @@ import ( "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() @@ -111,6 +116,8 @@ func (s *Simulation) AddNode(opts ...AddNodeOption) (id enode.ID, err error) { PrivateKey: conf.PrivateKey, } record, err := network.NewEnodeRecord(enodeParams) + + bzzPrivateKey, bzzKey, err := BzzKeyFromConfig(conf) if err != nil { return enode.ID{}, err } @@ -121,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()) } @@ -320,13 +329,14 @@ func init() { } // derive a private key for swarm for the node key -func BzzKeyFromConfig(conf *adapters.NodeConfig) ([]byte, error) { +// returns the private key used to generate the bzz key AND the generated bzz key +func BzzKeyFromConfig(conf *adapters.NodeConfig) (*ecdsa.PrivateKey, []byte, error) { // ecdsa.GenerateKey takes 40 bytes entropy 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 nil, nil, err } bzzKey := network.PrivateKeyToBzzKey(bzzPrivateKey) - return bzzKey, nil + return bzzPrivateKey, bzzKey, nil } diff --git a/swarm/network/simulation/simulation.go b/swarm/network/simulation/simulation.go index e18d19a67c..788442a3ce 100644 --- a/swarm/network/simulation/simulation.go +++ b/swarm/network/simulation/simulation.go @@ -85,7 +85,11 @@ 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) + var b *sync.Map + var ok bool + if b, ok = s.buckets[ctx.Config.ID]; !ok { + b = new(sync.Map) + } service, cleanup, err := serviceFunc(ctx, b) if err != nil { return nil, err diff --git a/swarm/pss/prox_test.go b/swarm/pss/prox_test.go index d963f31f39..57db026c71 100644 --- a/swarm/pss/prox_test.go +++ b/swarm/pss/prox_test.go @@ -2,6 +2,7 @@ package pss import ( "context" + "crypto/ecdsa" "encoding/binary" "errors" "fmt" @@ -382,15 +383,17 @@ func newProxServices(tstdata *testData, allowRaw bool, handlerContextFuncs map[T return map[string]simulation.ServiceFunc{ "bzz": func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) { var err error + var bzzPrivateKey *ecdsa.PrivateKey // normally translation of enode id to swarm address is concealed by the network package // however, we need to keep track of it in the test driver as well. // if the translation in the network package changes, that can cause these tests to unpredictably fail // therefore we keep a local copy of the translation here addr := network.NewAddr(ctx.Config.Node()) - addr.OAddr, err = simulation.BzzKeyFromConfig(ctx.Config) + bzzPrivateKey, addr.OAddr, err = simulation.BzzKeyFromConfig(ctx.Config) if err != nil { return nil, nil, err } + b.Store(simulation.BucketKeyBzzPrivateKey, bzzPrivateKey) hp := network.NewHiveParams() hp.Discovery = false config := &network.BzzConfig{ @@ -411,7 +414,7 @@ func newProxServices(tstdata *testData, allowRaw bool, handlerContextFuncs map[T privkey, err := w.GetPrivateKey(keys) pssp := NewPssParams().WithPrivateKey(privkey) pssp.AllowRaw = allowRaw - bzzKey, err := simulation.BzzKeyFromConfig(ctx.Config) + _, bzzKey, err := simulation.BzzKeyFromConfig(ctx.Config) if err != nil { return nil, nil, err }