mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
p2p/simulation: WIP minimal snapshot test
This commit is contained in:
parent
de4265fa02
commit
89245fbdae
3 changed files with 83 additions and 27 deletions
|
|
@ -22,10 +22,83 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/log"
|
||||||
|
"github.com/ethereum/go-ethereum/node"
|
||||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
|
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestSnapshot(t *testing.T) {
|
||||||
|
protoCMap := make(map[enode.ID]map[enode.ID]chan struct{})
|
||||||
|
adapter := adapters.NewSimAdapter(adapters.Services{
|
||||||
|
"noopwoop": func(ctx *adapters.ServiceContext) (node.Service, error) {
|
||||||
|
svc := NewNoopService()
|
||||||
|
protoCMap[ctx.Config.ID] = svc.C
|
||||||
|
return svc, nil
|
||||||
|
},
|
||||||
|
})
|
||||||
|
network := NewNetwork(adapter, &NetworkConfig{
|
||||||
|
DefaultService: "noopwoop",
|
||||||
|
})
|
||||||
|
defer network.Shutdown()
|
||||||
|
|
||||||
|
nodeCount := 20
|
||||||
|
ids := make([]enode.ID, nodeCount)
|
||||||
|
for i := 0; i < nodeCount; i++ {
|
||||||
|
conf := adapters.RandomNodeConfig()
|
||||||
|
node, err := network.NewNodeWithConfig(conf)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error creating node: %s", err)
|
||||||
|
}
|
||||||
|
if err := network.Start(node.ID()); err != nil {
|
||||||
|
t.Fatalf("error starting node: %s", err)
|
||||||
|
}
|
||||||
|
ids[i] = node.ID()
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for i, id := range ids {
|
||||||
|
peerID := ids[(i+1)%len(ids)]
|
||||||
|
if err := network.Connect(id, peerID); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
evC := make(chan *Event)
|
||||||
|
sub := network.Events().Subscribe(evC)
|
||||||
|
defer sub.Unsubscribe()
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
connEventCount := nodeCount
|
||||||
|
OUTER:
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
t.Fatal(ctx.Err())
|
||||||
|
case ev := <-evC:
|
||||||
|
if ev.Type == EventTypeConn && !ev.Control {
|
||||||
|
connEventCount--
|
||||||
|
log.Debug("ev", "count", connEventCount)
|
||||||
|
if connEventCount == 0 {
|
||||||
|
break OUTER
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for nodid, peers := range protoCMap {
|
||||||
|
for peerid, peerC := range peers {
|
||||||
|
log.Debug("getting ", "node", nodid, "peer", peerid)
|
||||||
|
<-peerC
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("ok")
|
||||||
|
}
|
||||||
|
|
||||||
// TestNetworkSimulation creates a multi-node simulation network with each node
|
// TestNetworkSimulation creates a multi-node simulation network with each node
|
||||||
// connected in a ring topology, checks that all nodes successfully handshake
|
// connected in a ring topology, checks that all nodes successfully handshake
|
||||||
// with each other and that a snapshot fully represents the desired topology
|
// with each other and that a snapshot fully represents the desired topology
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ func NewNoopService(ackC map[enode.ID]chan struct{}) *NoopService {
|
||||||
|
|
||||||
func (t *NoopService) Protocols() []p2p.Protocol {
|
func (t *NoopService) Protocols() []p2p.Protocol {
|
||||||
return []p2p.Protocol{
|
return []p2p.Protocol{
|
||||||
{
|
p2p.Protocol{
|
||||||
Name: "noop",
|
Name: "noop",
|
||||||
Version: 666,
|
Version: 666,
|
||||||
Length: 0,
|
Length: 0,
|
||||||
|
|
|
||||||
|
|
@ -26,9 +26,8 @@ import (
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/ethereum/go-ethereum/node"
|
"github.com/ethereum/go-ethereum/node"
|
||||||
"github.com/ethereum/go-ethereum/p2p"
|
"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"
|
|
||||||
colorable "github.com/mattn/go-colorable"
|
colorable "github.com/mattn/go-colorable"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -182,39 +181,23 @@ func noopServiceFunc(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, f
|
||||||
return newNoopService(), nil, nil
|
return newNoopService(), nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// noopService is the service that does not do anything
|
|
||||||
// but implements node.Service interface.
|
|
||||||
type noopService struct{}
|
|
||||||
|
|
||||||
func newNoopService() node.Service {
|
func newNoopService() node.Service {
|
||||||
return &noopService{}
|
return &noopService{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *noopService) Protocols() []p2p.Protocol {
|
// a helper function for most basic Noop service
|
||||||
return []p2p.Protocol{}
|
// of a different type then NoopService to test
|
||||||
}
|
|
||||||
|
|
||||||
func (t *noopService) APIs() []rpc.API {
|
|
||||||
return []rpc.API{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *noopService) Start(server *p2p.Server) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *noopService) Stop() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// a helper function for most basic noop service
|
|
||||||
// of a different type then noopService to test
|
|
||||||
// multiple services on one node.
|
// multiple services on one node.
|
||||||
func noopService2Func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) {
|
func noopService2Func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) {
|
||||||
return new(noopService2), nil, nil
|
return new(noopService2), nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// noopService2 is the service that does not do anything
|
// NoopService2 is the service that does not do anything
|
||||||
// but implements node.Service interface.
|
// but implements node.Service interface.
|
||||||
type noopService2 struct {
|
type noopService2 struct {
|
||||||
noopService
|
simulations.NoopService
|
||||||
|
}
|
||||||
|
|
||||||
|
type noopService struct {
|
||||||
|
simulations.NoopService
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue