mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
swarm/network, swarm/pss: Dervice bzzkey
This commit is contained in:
parent
6d7141ab51
commit
eea82bedc2
2 changed files with 44 additions and 9 deletions
|
|
@ -17,7 +17,9 @@
|
|||
package simulation
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/ecdsa"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
|
|
@ -25,6 +27,7 @@ import (
|
|||
"os"
|
||||
"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"
|
||||
|
|
@ -315,3 +318,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
|
||||
func BzzKeyFromConfig(conf *adapters.NodeConfig) ([]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
|
||||
}
|
||||
bzzKey := network.PrivateKeyToBzzKey(bzzPrivateKey)
|
||||
return bzzKey, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -117,11 +117,19 @@ func newTestData() *testData {
|
|||
}
|
||||
}
|
||||
|
||||
func (d *testData) init(msgCount int) {
|
||||
func (d *testData) init(msgCount int) error {
|
||||
log.Debug("TestProxNetwork start")
|
||||
|
||||
for _, nodeId := range d.sim.NodeIDs() {
|
||||
d.nodeAddrs[nodeId] = nodeIDToAddr(nodeId)
|
||||
kadif, ok := d.sim.NodeItem(nodeId, simulation.BucketKeyKademlia)
|
||||
if !ok {
|
||||
return fmt.Errorf("no kademlia entry for %v", nodeId)
|
||||
}
|
||||
kad, ok := kadif.(*network.Kademlia)
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid kademlia entry for %v", nodeId)
|
||||
}
|
||||
d.nodeAddrs[nodeId] = kad.BaseAddr()
|
||||
}
|
||||
|
||||
for i := 0; i < int(msgCount); i++ {
|
||||
|
|
@ -169,6 +177,7 @@ func (d *testData) init(msgCount int) {
|
|||
log.Debug("nn for msg", "targets", len(d.recipients[i]), "msgidx", i, "msg", common.Bytes2Hex(msgAddr[:8]), "sender", d.senders[i], "senderpo", smallestPo)
|
||||
}
|
||||
log.Debug("msgs to receive", "count", d.requiredMessages)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Here we test specific functionality of the pss, setting the prox property of
|
||||
|
|
@ -220,7 +229,10 @@ func testProxNetwork(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tstdata.init(msgCount) // initialize the test data
|
||||
err = tstdata.init(msgCount) // initialize the test data
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
wrapper := func(c context.Context, _ *simulation.Simulation) error {
|
||||
return testRoutine(tstdata, c)
|
||||
}
|
||||
|
|
@ -230,7 +242,7 @@ func testProxNetwork(t *testing.T) {
|
|||
// however, it might just mean that not all possible messages are received
|
||||
// now we must check if all required messages are received
|
||||
cnt := tstdata.getMsgCount()
|
||||
log.Debug("TestProxNetwork finnished", "rcv", cnt)
|
||||
log.Debug("TestProxNetwork finished", "rcv", cnt)
|
||||
if cnt < tstdata.requiredMessages {
|
||||
t.Fatal(result.Error)
|
||||
}
|
||||
|
|
@ -354,7 +366,7 @@ func nodeMsgHandler(tstdata *testData, config *adapters.NodeConfig) *handler {
|
|||
// replaces pss_test.go when those tests are rewritten to the new swarm/network/simulation package
|
||||
func newProxServices(tstdata *testData, allowRaw bool, handlerContextFuncs map[Topic]handlerContextFunc, kademlias map[enode.ID]*network.Kademlia) map[string]simulation.ServiceFunc {
|
||||
stateStore := state.NewInmemoryStore()
|
||||
kademlia := func(id enode.ID) *network.Kademlia {
|
||||
kademlia := func(id enode.ID, bzzkey []byte) *network.Kademlia {
|
||||
if k, ok := kademlias[id]; ok {
|
||||
return k
|
||||
}
|
||||
|
|
@ -364,17 +376,21 @@ func newProxServices(tstdata *testData, allowRaw bool, handlerContextFuncs map[T
|
|||
params.MaxRetries = 1000
|
||||
params.RetryExponent = 2
|
||||
params.RetryInterval = 1000000
|
||||
kademlias[id] = network.NewKademlia(id[:], params)
|
||||
kademlias[id] = network.NewKademlia(bzzkey, params)
|
||||
return kademlias[id]
|
||||
}
|
||||
return map[string]simulation.ServiceFunc{
|
||||
"bzz": func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) {
|
||||
var err error
|
||||
// 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 = nodeIDToAddr(ctx.Config.Node().ID())
|
||||
addr.OAddr, err = simulation.BzzKeyFromConfig(ctx.Config)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
hp := network.NewHiveParams()
|
||||
hp.Discovery = false
|
||||
config := &network.BzzConfig{
|
||||
|
|
@ -382,7 +398,7 @@ func newProxServices(tstdata *testData, allowRaw bool, handlerContextFuncs map[T
|
|||
UnderlayAddr: addr.Under(),
|
||||
HiveParams: hp,
|
||||
}
|
||||
return network.NewBzz(config, kademlia(ctx.Config.ID), stateStore, nil, nil), nil, nil
|
||||
return network.NewBzz(config, kademlia(ctx.Config.ID, addr.OAddr), stateStore, nil, nil), nil, nil
|
||||
},
|
||||
"pss": func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) {
|
||||
// execadapter does not exec init()
|
||||
|
|
@ -395,7 +411,11 @@ func newProxServices(tstdata *testData, allowRaw bool, handlerContextFuncs map[T
|
|||
privkey, err := w.GetPrivateKey(keys)
|
||||
pssp := NewPssParams().WithPrivateKey(privkey)
|
||||
pssp.AllowRaw = allowRaw
|
||||
pskad := kademlia(ctx.Config.ID)
|
||||
bzzKey, err := simulation.BzzKeyFromConfig(ctx.Config)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
pskad := kademlia(ctx.Config.ID, bzzKey)
|
||||
ps, err := NewPss(pskad, pssp)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
|
|
|
|||
Loading…
Reference in a new issue