swarm/network, swarm/pss: Dervice bzzkey

This commit is contained in:
lash 2019-03-19 09:54:37 +01:00
parent 6d7141ab51
commit eea82bedc2
2 changed files with 44 additions and 9 deletions

View file

@ -17,7 +17,9 @@
package simulation package simulation
import ( import (
"bytes"
"context" "context"
"crypto/ecdsa"
"encoding/json" "encoding/json"
"errors" "errors"
"io/ioutil" "io/ioutil"
@ -25,6 +27,7 @@ import (
"os" "os"
"time" "time"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/simulations" "github.com/ethereum/go-ethereum/p2p/simulations"
"github.com/ethereum/go-ethereum/p2p/simulations/adapters" "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() { func init() {
rand.Seed(time.Now().UnixNano()) 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
}

View file

@ -117,11 +117,19 @@ func newTestData() *testData {
} }
} }
func (d *testData) init(msgCount int) { func (d *testData) init(msgCount int) error {
log.Debug("TestProxNetwork start") log.Debug("TestProxNetwork start")
for _, nodeId := range d.sim.NodeIDs() { 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++ { 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("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) log.Debug("msgs to receive", "count", d.requiredMessages)
return nil
} }
// Here we test specific functionality of the pss, setting the prox property of // 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 { if err != nil {
t.Fatal(err) 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 { wrapper := func(c context.Context, _ *simulation.Simulation) error {
return testRoutine(tstdata, c) 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 // however, it might just mean that not all possible messages are received
// now we must check if all required messages are received // now we must check if all required messages are received
cnt := tstdata.getMsgCount() cnt := tstdata.getMsgCount()
log.Debug("TestProxNetwork finnished", "rcv", cnt) log.Debug("TestProxNetwork finished", "rcv", cnt)
if cnt < tstdata.requiredMessages { if cnt < tstdata.requiredMessages {
t.Fatal(result.Error) 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 // 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 { func newProxServices(tstdata *testData, allowRaw bool, handlerContextFuncs map[Topic]handlerContextFunc, kademlias map[enode.ID]*network.Kademlia) map[string]simulation.ServiceFunc {
stateStore := state.NewInmemoryStore() 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 { if k, ok := kademlias[id]; ok {
return k return k
} }
@ -364,17 +376,21 @@ func newProxServices(tstdata *testData, allowRaw bool, handlerContextFuncs map[T
params.MaxRetries = 1000 params.MaxRetries = 1000
params.RetryExponent = 2 params.RetryExponent = 2
params.RetryInterval = 1000000 params.RetryInterval = 1000000
kademlias[id] = network.NewKademlia(id[:], params) kademlias[id] = network.NewKademlia(bzzkey, params)
return kademlias[id] return kademlias[id]
} }
return map[string]simulation.ServiceFunc{ return map[string]simulation.ServiceFunc{
"bzz": func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) { "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 // 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. // 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 // 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 // therefore we keep a local copy of the translation here
addr := network.NewAddr(ctx.Config.Node()) 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 := network.NewHiveParams()
hp.Discovery = false hp.Discovery = false
config := &network.BzzConfig{ config := &network.BzzConfig{
@ -382,7 +398,7 @@ func newProxServices(tstdata *testData, allowRaw bool, handlerContextFuncs map[T
UnderlayAddr: addr.Under(), UnderlayAddr: addr.Under(),
HiveParams: hp, 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) { "pss": func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) {
// execadapter does not exec init() // execadapter does not exec init()
@ -395,7 +411,11 @@ func newProxServices(tstdata *testData, allowRaw bool, handlerContextFuncs map[T
privkey, err := w.GetPrivateKey(keys) privkey, err := w.GetPrivateKey(keys)
pssp := NewPssParams().WithPrivateKey(privkey) pssp := NewPssParams().WithPrivateKey(privkey)
pssp.AllowRaw = allowRaw 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) ps, err := NewPss(pskad, pssp)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err