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
1f097cb946
commit
487cffa540
2 changed files with 44 additions and 9 deletions
|
|
@ -17,6 +17,8 @@
|
||||||
package simulation
|
package simulation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/ecdsa"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
|
@ -24,6 +26,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"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"
|
||||||
|
|
@ -326,3 +329,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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -139,11 +139,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++ {
|
||||||
|
|
@ -191,6 +199,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
|
||||||
|
|
@ -246,7 +255,10 @@ func testProxNetwork(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to recreate snapshot: %s", err)
|
t.Fatalf("failed to recreate snapshot: %s", 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)
|
||||||
}
|
}
|
||||||
|
|
@ -256,7 +268,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)
|
||||||
}
|
}
|
||||||
|
|
@ -380,7 +392,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
|
||||||
}
|
}
|
||||||
|
|
@ -390,17 +402,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{
|
||||||
|
|
@ -408,7 +424,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()
|
||||||
|
|
@ -421,7 +437,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
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue