Skip to content

Commit 201a0bf

Browse files
nolashzelig
authored andcommitted
p2p/simulations, swarm/network: Custom services in snapshot (#17991)
* p2p/simulations: Add custom services to simnodes + remove sim down conn objs * p2p/simulation, swarm/network: Add selective services to discovery sim * p2p/simulations, swarm/network: Remove useless comments * p2p/simulations, swarm/network: Clean up mess from rebase * p2p/simulation: Add sleep to prevent connect flakiness in http test * p2p/simulations: added concurrent goroutines to prevent sleeps on simulation connect/disconnect * p2p/simulations, swarm/network/simulations: address pr comments * reinstated dummy service * fixed http snapshot test
1 parent a0876f7 commit 201a0bf

File tree

3 files changed

+118
-12
lines changed

3 files changed

+118
-12
lines changed

p2p/simulations/http_test.go

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package simulations
1818

1919
import (
2020
"context"
21+
"flag"
2122
"fmt"
2223
"math/rand"
2324
"net/http/httptest"
@@ -28,13 +29,26 @@ import (
2829
"time"
2930

3031
"github.com/ethereum/go-ethereum/event"
32+
"github.com/ethereum/go-ethereum/log"
3133
"github.com/ethereum/go-ethereum/node"
3234
"github.com/ethereum/go-ethereum/p2p"
3335
"github.com/ethereum/go-ethereum/p2p/enode"
3436
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
3537
"github.com/ethereum/go-ethereum/rpc"
38+
colorable "github.com/mattn/go-colorable"
3639
)
3740

41+
var (
42+
loglevel = flag.Int("loglevel", 2, "verbosity of logs")
43+
)
44+
45+
func init() {
46+
flag.Parse()
47+
48+
log.PrintOrigins(true)
49+
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*loglevel), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true))))
50+
}
51+
3852
// testService implements the node.Service interface and provides protocols
3953
// and APIs which are useful for testing nodes in a simulation network
4054
type testService struct {
@@ -584,9 +598,26 @@ func TestHTTPNodeRPC(t *testing.T) {
584598
// TestHTTPSnapshot tests creating and loading network snapshots
585599
func TestHTTPSnapshot(t *testing.T) {
586600
// start the server
587-
_, s := testHTTPServer(t)
601+
network, s := testHTTPServer(t)
588602
defer s.Close()
589603

604+
var eventsDone = make(chan struct{})
605+
count := 1
606+
eventsDoneChan := make(chan *Event)
607+
eventSub := network.Events().Subscribe(eventsDoneChan)
608+
go func() {
609+
defer eventSub.Unsubscribe()
610+
for event := range eventsDoneChan {
611+
if event.Type == EventTypeConn && !event.Control {
612+
count--
613+
if count == 0 {
614+
eventsDone <- struct{}{}
615+
return
616+
}
617+
}
618+
}
619+
}()
620+
590621
// create a two-node network
591622
client := NewClient(s.URL)
592623
nodeCount := 2
@@ -620,7 +651,7 @@ func TestHTTPSnapshot(t *testing.T) {
620651
}
621652
states[i] = state
622653
}
623-
654+
<-eventsDone
624655
// create a snapshot
625656
snap, err := client.CreateSnapshot()
626657
if err != nil {
@@ -634,9 +665,23 @@ func TestHTTPSnapshot(t *testing.T) {
634665
}
635666

636667
// create another network
637-
_, s = testHTTPServer(t)
668+
network2, s := testHTTPServer(t)
638669
defer s.Close()
639670
client = NewClient(s.URL)
671+
count = 1
672+
eventSub = network2.Events().Subscribe(eventsDoneChan)
673+
go func() {
674+
defer eventSub.Unsubscribe()
675+
for event := range eventsDoneChan {
676+
if event.Type == EventTypeConn && !event.Control {
677+
count--
678+
if count == 0 {
679+
eventsDone <- struct{}{}
680+
return
681+
}
682+
}
683+
}
684+
}()
640685

641686
// subscribe to events so we can check them later
642687
events := make(chan *Event, 100)
@@ -651,6 +696,7 @@ func TestHTTPSnapshot(t *testing.T) {
651696
if err := client.LoadSnapshot(snap); err != nil {
652697
t.Fatalf("error loading snapshot: %s", err)
653698
}
699+
<-eventsDone
654700

655701
// check the nodes and connection exists
656702
net, err := client.GetNetwork()
@@ -676,6 +722,9 @@ func TestHTTPSnapshot(t *testing.T) {
676722
if conn.Other.String() != nodes[1].ID {
677723
t.Fatalf("expected connection to have other=%q, got other=%q", nodes[1].ID, conn.Other)
678724
}
725+
if !conn.Up {
726+
t.Fatal("should be up")
727+
}
679728

680729
// check the node states were restored
681730
for i, node := range nodes {

p2p/simulations/network.go

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -644,11 +644,18 @@ type NodeSnapshot struct {
644644

645645
// Snapshot creates a network snapshot
646646
func (net *Network) Snapshot() (*Snapshot, error) {
647+
return net.snapshot(nil, nil)
648+
}
649+
650+
func (net *Network) SnapshotWithServices(addServices []string, removeServices []string) (*Snapshot, error) {
651+
return net.snapshot(addServices, removeServices)
652+
}
653+
654+
func (net *Network) snapshot(addServices []string, removeServices []string) (*Snapshot, error) {
647655
net.lock.Lock()
648656
defer net.lock.Unlock()
649657
snap := &Snapshot{
650658
Nodes: make([]NodeSnapshot, len(net.Nodes)),
651-
Conns: make([]Conn, len(net.Conns)),
652659
}
653660
for i, node := range net.Nodes {
654661
snap.Nodes[i] = NodeSnapshot{Node: *node}
@@ -660,9 +667,40 @@ func (net *Network) Snapshot() (*Snapshot, error) {
660667
return nil, err
661668
}
662669
snap.Nodes[i].Snapshots = snapshots
670+
for _, addSvc := range addServices {
671+
haveSvc := false
672+
for _, svc := range snap.Nodes[i].Node.Config.Services {
673+
if svc == addSvc {
674+
haveSvc = true
675+
break
676+
}
677+
}
678+
if !haveSvc {
679+
snap.Nodes[i].Node.Config.Services = append(snap.Nodes[i].Node.Config.Services, addSvc)
680+
}
681+
}
682+
if len(removeServices) > 0 {
683+
var cleanedServices []string
684+
for _, svc := range snap.Nodes[i].Node.Config.Services {
685+
haveSvc := false
686+
for _, rmSvc := range removeServices {
687+
if rmSvc == svc {
688+
haveSvc = true
689+
break
690+
}
691+
}
692+
if !haveSvc {
693+
cleanedServices = append(cleanedServices, svc)
694+
}
695+
696+
}
697+
snap.Nodes[i].Node.Config.Services = cleanedServices
698+
}
663699
}
664-
for i, conn := range net.Conns {
665-
snap.Conns[i] = *conn
700+
for _, conn := range net.Conns {
701+
if conn.Up {
702+
snap.Conns = append(snap.Conns, *conn)
703+
}
666704
}
667705
return snap, nil
668706
}

swarm/network/simulations/discovery/discovery_test.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,12 @@ func getDbStore(nodeID string) (*state.DBStore, error) {
8585
}
8686

8787
var (
88-
nodeCount = flag.Int("nodes", 10, "number of nodes to create (default 10)")
89-
initCount = flag.Int("conns", 1, "number of originally connected peers (default 1)")
90-
snapshotFile = flag.String("snapshot", "", "create snapshot")
91-
loglevel = flag.Int("loglevel", 3, "verbosity of logs")
92-
rawlog = flag.Bool("rawlog", false, "remove terminal formatting from logs")
88+
nodeCount = flag.Int("nodes", 10, "number of nodes to create (default 10)")
89+
initCount = flag.Int("conns", 1, "number of originally connected peers (default 1)")
90+
snapshotFile = flag.String("snapshot", "", "path to create snapshot file in")
91+
loglevel = flag.Int("loglevel", 3, "verbosity of logs")
92+
rawlog = flag.Bool("rawlog", false, "remove terminal formatting from logs")
93+
serviceOverride = flag.String("services", "", "remove or add services to the node snapshot; prefix with \"+\" to add, \"-\" to remove; example: +pss,-discovery")
9394
)
9495

9596
func init() {
@@ -306,7 +307,25 @@ func discoverySimulation(nodes, conns int, adapter adapters.NodeAdapter) (*simul
306307
}
307308

308309
if *snapshotFile != "" {
309-
snap, err := net.Snapshot()
310+
var err error
311+
var snap *simulations.Snapshot
312+
if len(*serviceOverride) > 0 {
313+
var addServices []string
314+
var removeServices []string
315+
for _, osvc := range strings.Split(*serviceOverride, ",") {
316+
if strings.Index(osvc, "+") == 0 {
317+
addServices = append(addServices, osvc[1:])
318+
} else if strings.Index(osvc, "-") == 0 {
319+
removeServices = append(removeServices, osvc[1:])
320+
} else {
321+
panic("stick to the rules, you know what they are")
322+
}
323+
}
324+
snap, err = net.SnapshotWithServices(addServices, removeServices)
325+
} else {
326+
snap, err = net.Snapshot()
327+
}
328+
310329
if err != nil {
311330
return nil, errors.New("no shapshot dude")
312331
}

0 commit comments

Comments
 (0)