swarm/network, swarm/storage: Further refactor fixes

This commit is contained in:
Balint Gabor 2018-01-18 18:47:43 +01:00
parent 35609bec2e
commit f0f62218a3
5 changed files with 72 additions and 75 deletions

View file

@ -96,13 +96,13 @@ func newStreamerTester(t *testing.T) (*p2ptest.ProtocolTester, *Registry, *stora
delivery := NewDelivery(to, db) delivery := NewDelivery(to, db)
streamer := NewRegistry(delivery) streamer := NewRegistry(delivery)
run := func(p *p2p.Peer, rw p2p.MsgReadWriter) error { run := func(p *p2p.Peer, rw p2p.MsgReadWriter) error {
BzzPeer := &BzzPeer{ bzzPeer := &network.BzzPeer{
Peer: protocols.NewPeer(p, rw, Spec), Peer: protocols.NewPeer(p, rw, Spec),
localAddr: addr, localAddr: addr,
BzzAddr: network.NewAddrFromNodeID(p.ID()), BzzAddr: network.NewAddrFromNodeID(p.ID()),
} }
to.On(BzzPeer) to.On(bzzPeer)
return streamer.Run(BzzPeer) return streamer.Run(bzzPeer)
} }
protocolTester := p2ptest.NewProtocolTester(t, network.NewNodeIDFromAddr(addr), 1, run) protocolTester := p2ptest.NewProtocolTester(t, network.NewNodeIDFromAddr(addr), 1, run)

View file

@ -33,6 +33,7 @@ import (
"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"
p2ptest "github.com/ethereum/go-ethereum/p2p/testing" p2ptest "github.com/ethereum/go-ethereum/p2p/testing"
"github.com/ethereum/go-ethereum/swarm/network"
"github.com/ethereum/go-ethereum/swarm/storage" "github.com/ethereum/go-ethereum/swarm/storage"
) )
@ -323,10 +324,10 @@ func testDeliveryFromNodes(nodes, conns, size int, skipCheck bool) func(adapter
action := func(net *simulations.Network) func(context.Context) error { action := func(net *simulations.Network) func(context.Context) error {
// here we distribute chunks of a random file into localstores of nodes 1 to nodes // here we distribute chunks of a random file into localstores of nodes 1 to nodes
rrdpa := storage.NewDPA(newRoundRobinStore(localStores[1:]...), storage.NewChunkerParams()) rrdpa := storage.NewDPA(newRoundRobinStore(testing.LocalStores[1:]...), storage.NewChunkerParams())
rrdpa.Start() rrdpa.Start()
// create a retriever dpa for the pivot node // create a retriever dpa for the pivot node
dpacs := storage.NewNetStore(localStores[0].(*storage.LocalStore), func(chunk *storage.Chunk) error { return delivery.RequestFromPeers(chunk.Key[:], skipCheck) }) dpacs := storage.NewNetStore(testing.LocalStores[0].(*storage.LocalStore), func(chunk *storage.Chunk) error { return delivery.RequestFromPeers(chunk.Key[:], skipCheck) })
dpa := storage.NewDPA(dpacs, storage.NewChunkerParams()) dpa := storage.NewDPA(dpacs, storage.NewChunkerParams())
dpa.Start() dpa.Start()
return func(context.Context) error { return func(context.Context) error {
@ -404,41 +405,38 @@ func newDeliveryService(ctx *adapters.ServiceContext) (node.Service, error) {
id := ctx.Config.ID id := ctx.Config.ID
addr := NewAddrFromNodeID(id) addr := NewAddrFromNodeID(id)
kad := NewKademlia(addr.Over(), NewKadParams()) kad := NewKademlia(addr.Over(), NewKadParams())
localStore := localStores[nodeCount] localStore := testing.LocalStores[testing.NodeCount]
db := NewDBAPI(localStore.(*storage.LocalStore)) db := NewDBAPI(localStore.(*storage.LocalStore))
streamer := NewStreamerRegistry(NewDelivery(kad, db)) streamer := NewStreamerRegistry(NewDelivery(kad, db))
if nodeCount == 0 { if testing.NodeCount == 0 {
// the delivery service for the pivot node is assigned globally // the delivery service for the pivot node is assigned globally
// so that the simulation action call can use it for the // so that the simulation action call can use it for the
// swarm enabled dpa // swarm enabled dpa
delivery = streamer.delivery delivery = streamer.delivery
} }
self := &testStreamerService{ testing.NodeCount++
addr: addr, return testing.NewTestStreamerService(Spec, makeRunFunc(addr, streamer)), nil
streamer: streamer,
}
self.run = self.runDelivery
nodeCount++
return self, nil
} }
func (b *testStreamerService) runDelivery(p *p2p.Peer, rw p2p.MsgReadWriter) error { func makeRunFunc(addr network.Addr, streamer *Registry) (func(p *p2p.Peer, rw p2p.MsgReadWriter), error) {
BzzPeer := &BzzPeer{ return func(p *p2p.Peer, rw p2p.MsgReadWriter) error {
Peer: protocols.NewPeer(p, rw, StreamerSpec), bzzPeer := &network.BzzPeer{
localAddr: b.addr, Peer: protocols.NewPeer(p, rw, Spec),
localAddr: addr,
BzzAddr: NewAddrFromNodeID(p.ID()), BzzAddr: NewAddrFromNodeID(p.ID()),
} }
b.streamer.delivery.overlay.On(BzzPeer) streamer.delivery.overlay.On(bzzPeer)
defer b.streamer.delivery.overlay.Off(BzzPeer) defer streamer.delivery.overlay.Off(bzzPeer)
go func() { go func() {
// each node Subscribes to each other's retrieveRequestStream // each node Subscribes to each other's retrieveRequestStream
// need to wait till an aynchronous process registers the peers in streamer.peers // need to wait till an aynchronous process registers the peers in streamer.peers
// that is used by Subscribe // that is used by Subscribe
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
err := b.streamer.Subscribe(p.ID(), retrieveRequestStream, nil, 0, 0, Top, true) err := streamer.Subscribe(p.ID(), retrieveRequestStream, nil, 0, 0, Top, true)
if err != nil { if err != nil {
log.Warn("error in subscribe", "err", err) log.Warn("error in subscribe", "err", err)
} }
}() }()
return b.streamer.Run(BzzPeer) return streamer.Run(bzzPeer)
}
} }

View file

@ -58,7 +58,7 @@ func testSyncBetweenNodes(nodes, conns, size int, skipCheck bool, po uint8) func
action := func(net *simulations.Network) func(context.Context) error { action := func(net *simulations.Network) func(context.Context) error {
// here we distribute chunks of a random file into localstores of nodes 1 to nodes // here we distribute chunks of a random file into localstores of nodes 1 to nodes
rrdpa := storage.NewDPA(newRoundRobinStore(localStores[1:]...), storage.NewChunkerParams()) rrdpa := storage.NewDPA(newRoundRobinStore(testing.LocalStores[1:]...), storage.NewChunkerParams())
rrdpa.Start() rrdpa.Start()
// create a retriever dpa for the pivot node // create a retriever dpa for the pivot node
return func(context.Context) error { return func(context.Context) error {
@ -78,7 +78,7 @@ func testSyncBetweenNodes(nodes, conns, size int, skipCheck bool, po uint8) func
dbs := make([]*storage.DBAPI, nodes) dbs := make([]*storage.DBAPI, nodes)
for i := 0; i < nodes; i++ { for i := 0; i < nodes; i++ {
dbs[i] = NewDbAccess(localStores[i].(*storage.LocalStore)) dbs[i] = NewDbAccess(testing.LocalStores[i].(*storage.LocalStore))
} }
return func(ctx context.Context, id discover.NodeID) (bool, error) { return func(ctx context.Context, id discover.NodeID) (bool, error) {
if id != net.Nodes[0].ID() { if id != net.Nodes[0].ID() {
@ -128,42 +128,38 @@ func newSyncerService(ctx *adapters.ServiceContext) (node.Service, error) {
// for the test we make all peers share 8 bits so that syncing full bins make sense // for the test we make all peers share 8 bits so that syncing full bins make sense
addr.OAddr[0] = byte(0) addr.OAddr[0] = byte(0)
kad := NewKademlia(addr.Over(), NewKadParams()) kad := NewKademlia(addr.Over(), NewKadParams())
localStore := localStores[nodeCount] localStore := testing.LocalStores[testing.NodeCount]
db := NewDbAccess(localStore.(*storage.LocalStore)) db := NewDbAccess(localStore.(*storage.LocalStore))
streamer := NewRegistry(NewDelivery(kad, db)) streamer := NewRegistry(NewDelivery(kad, db))
RegisterIncomingSyncer(streamer, db) RegisterIncomingSyncer(streamer, db)
RegisterOutgoingSyncer(streamer, db) RegisterOutgoingSyncer(streamer, db)
self := &testStreamerService{ testing.NodeCount++
index: nodeCount, return testing.NewTestStreamerService(Spec, makeRunFunc(addr, streamer)), nil
addr: addr,
streamer: streamer,
}
self.run = self.runSyncer
nodeCount++
return self, nil
} }
func (b *testStreamerService) runSyncer(p *p2p.Peer, rw p2p.MsgReadWriter) error { func makeRunFunc(localAddr network.Addr, streamer *Registry) (func(p *p2p.Peer, rw p2p.MsgReadWriter), error) {
addr := network.NewAddrFromNodeID(p.ID()) return func(p *p2p.Peer, rw p2p.MsgReadWriter) error {
addr.OAddr[0] = byte(0) remoteAddr := network.NewAddrFromNodeID(p.ID())
BzzPeer := &BzzPeer{ remoteAddr.OAddr[0] = byte(0)
bzzPeer := &network.BzzPeer{
Peer: protocols.NewPeer(p, rw, Spec), Peer: protocols.NewPeer(p, rw, Spec),
localAddr: b.addr, localAddr: localAddr,
BzzAddr: addr, BzzAddr: remoteAddr,
} }
b.streamer.delivery.overlay.On(BzzPeer) streamer.delivery.overlay.On(bzzPeer)
defer b.streamer.delivery.overlay.Off(BzzPeer) defer streamer.delivery.overlay.Off(bzzPeer)
// if len(addr) > b.index+1 && bytes.Equal(addrs[b.index+1], addr) { // if len(addr) > b.index+1 && bytes.Equal(testing.Addrs[b.index+1], addr) {
go func() { go func() {
// each node Subscribes to each other's retrieveRequestStream // each node Subscribes to each other's retrieveRequestStream
// need to wait till an aynchronous process registers the peers in streamer.peers // need to wait till an aynchronous process registers the peers in streamer.peers
// that is used by Subscribe // that is used by Subscribe
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
if err := b.streamer.Subscribe(p.ID(), "SYNC", []byte{uint8(1)}, 0, 0, Top, false); err != nil { if err := streamer.Subscribe(p.ID(), "SYNC", []byte{uint8(1)}, 0, 0, Top, false); err != nil {
log.Warn("error in subscribe", "err", err) log.Warn("error in subscribe", "err", err)
} }
}() }()
// } // }
return b.streamer.Run(BzzPeer) return streamer.Run(bzzPeer)
}
} }

View file

@ -31,11 +31,11 @@ import (
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/protocols"
"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"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/swarm/network" "github.com/ethereum/go-ethereum/swarm/network"
"github.com/ethereum/go-ethereum/swarm/network/stream"
"github.com/ethereum/go-ethereum/swarm/storage" "github.com/ethereum/go-ethereum/swarm/storage"
) )
@ -223,23 +223,26 @@ func (rrs *roundRobinStore) Close() {
} }
type TestStreamerService struct { type TestStreamerService struct {
index int // index int
addr *network.BzzAddr // addr *network.BzzAddr
streamer *stream.Registry // // streamer *stream.Registry
run func(s *TestStreamerService, p *p2p.Peer, rw p2p.MsgReadWriter) error run func(p *p2p.Peer, rw p2p.MsgReadWriter) error
spec *protocols.Spec
} }
func NewTestStreamerService(run func(s *TestStreamerService, p *p2p.Peer, rw p2p.MsgReadWriter) error) TestStreamerService { func NewTestStreamerService(spec *protocols.Spec, run func(p *p2p.Peer, rw p2p.MsgReadWriter) error) *TestStreamerService {
t := &TestStreamerService{} return &TestStreamerService{
t.run = run run: run,
spec: spec,
}
} }
func (tds *TestStreamerService) Protocols() []p2p.Protocol { func (tds *TestStreamerService) Protocols() []p2p.Protocol {
return []p2p.Protocol{ return []p2p.Protocol{
{ {
Name: stream.Spec.Name, Name: tds.spec.Name,
Version: stream.Spec.Version, Version: tds.spec.Version,
Length: stream.Spec.Length(), Length: tds.spec.Length(),
Run: tds.run, Run: tds.run,
// NodeInfo: , // NodeInfo: ,
// PeerInfo: , // PeerInfo: ,