network/retrieve: add bzz-retrieve protocol (#1589)

* network/retrieve: initial commit

* network: import dependencies from stream package branch

* network/retrieve: address pr comments

* network/retrieve: fix pr comments

* network/retrieve: create logger for peer

* network: address pr comments

* network/retrieve: lint

* network/retrieve: prevent forever loop

* network/retrieve: address pr comments

* network/retrieve: fix linter

* network/retrieve: pr comments

* network/retrieval: pr comments
This commit is contained in:
acud
2019-07-25 14:23:46 +05:30
committed by Viktor Trón
parent 388d8ccd9f
commit 74b12e35bf
7 changed files with 997 additions and 1 deletions

View File

@@ -16,7 +16,11 @@
package simulation
import "github.com/ethereum/go-ethereum/p2p/enode"
import (
"fmt"
"github.com/ethereum/go-ethereum/p2p/enode"
)
// BucketKey is the type that should be used for keys in simulation buckets.
type BucketKey string
@@ -32,6 +36,24 @@ func (s *Simulation) NodeItem(id enode.ID, key interface{}) (value interface{},
return s.buckets[id].Load(key)
}
// MustNodeItem returns the item set in ServiceFunc for a particular node or panics in case
// the item is not found
func (s *Simulation) MustNodeItem(id enode.ID, key interface{}) (value interface{}) {
s.mu.Lock()
defer s.mu.Unlock()
if _, ok := s.buckets[id]; !ok {
e := fmt.Errorf("cannot find node id %s in bucket", id.String())
panic(e)
}
if v, ok := s.buckets[id].Load(key); ok {
return v
} else {
e := fmt.Errorf("cannot find key %v on node bucket", key)
panic(e)
}
}
// SetNodeItem sets a new item associated with the node with provided NodeID.
// Buckets should be used to avoid managing separate simulation global state.
func (s *Simulation) SetNodeItem(id enode.ID, key interface{}, value interface{}) {

View File

@@ -99,6 +99,34 @@ func NewInProc(services map[string]ServiceFunc) (s *Simulation) {
return s
}
// NewBzzInProc is the same as NewInProc but injects bzz as a default protocol
func NewBzzInProc(services map[string]ServiceFunc) (s *Simulation) {
services["bzz"] = func(ctx *adapters.ServiceContext, bucket *sync.Map) (node.Service, func(), error) {
addr := network.NewAddr(ctx.Config.Node())
hp := network.NewHiveParams()
hp.KeepAliveInterval = time.Duration(200) * time.Millisecond
hp.Discovery = false
var kad *network.Kademlia
// check if another kademlia already exists and load it if necessary - we dont want two independent copies of it
if kv, ok := bucket.Load(BucketKeyKademlia); ok {
kad = kv.(*network.Kademlia)
} else {
kad = network.NewKademlia(addr.Over(), network.NewKadParams())
bucket.Store(BucketKeyKademlia, kad)
}
config := &network.BzzConfig{
OverlayAddr: addr.Over(),
UnderlayAddr: addr.Under(),
HiveParams: hp,
}
return network.NewBzz(config, kad, nil, nil, nil), nil, nil
}
return NewInProc(services)
}
// NewExec does the same as New but lets the caller specify the adapter to use
func NewExec(services map[string]ServiceFunc) (s *Simulation, err error) {
s = &Simulation{