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)
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
@@ -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)
|
||||
}
|
||||
|
@@ -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)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user