Skip to content

Commit 472c23a

Browse files
acudnonsense
authored andcommitted
p2p/simulation: move connection methods from swarm/network/simulation (#18323)
1 parent d322c9d commit 472c23a

14 files changed

Lines changed: 500 additions & 439 deletions
Lines changed: 68 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,65 +14,69 @@
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

17-
package simulation
17+
package simulations
1818

1919
import (
20+
"errors"
2021
"strings"
2122

2223
"github.com/ethereum/go-ethereum/p2p/enode"
2324
)
2425

26+
var (
27+
ErrNodeNotFound = errors.New("node not found")
28+
ErrNoPivotNode = errors.New("no pivot node set")
29+
)
30+
2531
// ConnectToPivotNode connects the node with provided NodeID
26-
// to the pivot node, already set by Simulation.SetPivotNode method.
32+
// to the pivot node, already set by Network.SetPivotNode method.
2733
// It is useful when constructing a star network topology
28-
// when simulation adds and removes nodes dynamically.
29-
func (s *Simulation) ConnectToPivotNode(id enode.ID) (err error) {
30-
pid := s.PivotNodeID()
31-
if pid == nil {
34+
// when Network adds and removes nodes dynamically.
35+
func (net *Network) ConnectToPivotNode(id enode.ID) (err error) {
36+
pivot := net.GetPivotNode()
37+
if pivot == nil {
3238
return ErrNoPivotNode
3339
}
34-
return s.connect(*pid, id)
40+
return net.connect(pivot.ID(), id)
3541
}
3642

3743
// ConnectToLastNode connects the node with provided NodeID
3844
// to the last node that is up, and avoiding connection to self.
3945
// It is useful when constructing a chain network topology
40-
// when simulation adds and removes nodes dynamically.
41-
func (s *Simulation) ConnectToLastNode(id enode.ID) (err error) {
42-
ids := s.UpNodeIDs()
46+
// when Network adds and removes nodes dynamically.
47+
func (net *Network) ConnectToLastNode(id enode.ID) (err error) {
48+
ids := net.getUpNodeIDs()
4349
l := len(ids)
4450
if l < 2 {
4551
return nil
4652
}
47-
lid := ids[l-1]
48-
if lid == id {
49-
lid = ids[l-2]
53+
last := ids[l-1]
54+
if last == id {
55+
last = ids[l-2]
5056
}
51-
return s.connect(lid, id)
57+
return net.connect(last, id)
5258
}
5359

54-
// ConnectToRandomNode connects the node with provieded NodeID
60+
// ConnectToRandomNode connects the node with provided NodeID
5561
// to a random node that is up.
56-
func (s *Simulation) ConnectToRandomNode(id enode.ID) (err error) {
57-
n := s.RandomUpNode(id)
58-
if n == nil {
62+
func (net *Network) ConnectToRandomNode(id enode.ID) (err error) {
63+
selected := net.GetRandomUpNode(id)
64+
if selected == nil {
5965
return ErrNodeNotFound
6066
}
61-
return s.connect(n.ID, id)
67+
return net.connect(selected.ID(), id)
6268
}
6369

6470
// ConnectNodesFull connects all nodes one to another.
6571
// It provides a complete connectivity in the network
6672
// which should be rarely needed.
67-
func (s *Simulation) ConnectNodesFull(ids []enode.ID) (err error) {
73+
func (net *Network) ConnectNodesFull(ids []enode.ID) (err error) {
6874
if ids == nil {
69-
ids = s.UpNodeIDs()
75+
ids = net.getUpNodeIDs()
7076
}
71-
l := len(ids)
72-
for i := 0; i < l; i++ {
73-
for j := i + 1; j < l; j++ {
74-
err = s.connect(ids[i], ids[j])
75-
if err != nil {
77+
for i, lid := range ids {
78+
for _, rid := range ids[i+1:] {
79+
if err = net.connect(lid, rid); err != nil {
7680
return err
7781
}
7882
}
@@ -82,14 +86,13 @@ func (s *Simulation) ConnectNodesFull(ids []enode.ID) (err error) {
8286

8387
// ConnectNodesChain connects all nodes in a chain topology.
8488
// If ids argument is nil, all nodes that are up will be connected.
85-
func (s *Simulation) ConnectNodesChain(ids []enode.ID) (err error) {
89+
func (net *Network) ConnectNodesChain(ids []enode.ID) (err error) {
8690
if ids == nil {
87-
ids = s.UpNodeIDs()
91+
ids = net.getUpNodeIDs()
8892
}
8993
l := len(ids)
9094
for i := 0; i < l-1; i++ {
91-
err = s.connect(ids[i], ids[i+1])
92-
if err != nil {
95+
if err := net.connect(ids[i], ids[i+1]); err != nil {
9396
return err
9497
}
9598
}
@@ -98,37 +101,32 @@ func (s *Simulation) ConnectNodesChain(ids []enode.ID) (err error) {
98101

99102
// ConnectNodesRing connects all nodes in a ring topology.
100103
// If ids argument is nil, all nodes that are up will be connected.
101-
func (s *Simulation) ConnectNodesRing(ids []enode.ID) (err error) {
104+
func (net *Network) ConnectNodesRing(ids []enode.ID) (err error) {
102105
if ids == nil {
103-
ids = s.UpNodeIDs()
106+
ids = net.getUpNodeIDs()
104107
}
105108
l := len(ids)
106109
if l < 2 {
107110
return nil
108111
}
109-
for i := 0; i < l-1; i++ {
110-
err = s.connect(ids[i], ids[i+1])
111-
if err != nil {
112-
return err
113-
}
112+
if err := net.ConnectNodesChain(ids); err != nil {
113+
return err
114114
}
115-
return s.connect(ids[l-1], ids[0])
115+
return net.connect(ids[l-1], ids[0])
116116
}
117117

118118
// ConnectNodesStar connects all nodes in a star topology
119119
// with the center at provided NodeID.
120120
// If ids argument is nil, all nodes that are up will be connected.
121-
func (s *Simulation) ConnectNodesStar(id enode.ID, ids []enode.ID) (err error) {
121+
func (net *Network) ConnectNodesStar(pivot enode.ID, ids []enode.ID) (err error) {
122122
if ids == nil {
123-
ids = s.UpNodeIDs()
123+
ids = net.getUpNodeIDs()
124124
}
125-
l := len(ids)
126-
for i := 0; i < l; i++ {
127-
if id == ids[i] {
125+
for _, id := range ids {
126+
if pivot == id {
128127
continue
129128
}
130-
err = s.connect(id, ids[i])
131-
if err != nil {
129+
if err := net.connect(pivot, id); err != nil {
132130
return err
133131
}
134132
}
@@ -138,17 +136,17 @@ func (s *Simulation) ConnectNodesStar(id enode.ID, ids []enode.ID) (err error) {
138136
// ConnectNodesStarPivot connects all nodes in a star topology
139137
// with the center at already set pivot node.
140138
// If ids argument is nil, all nodes that are up will be connected.
141-
func (s *Simulation) ConnectNodesStarPivot(ids []enode.ID) (err error) {
142-
id := s.PivotNodeID()
143-
if id == nil {
139+
func (net *Network) ConnectNodesStarPivot(ids []enode.ID) (err error) {
140+
pivot := net.GetPivotNode()
141+
if pivot == nil {
144142
return ErrNoPivotNode
145143
}
146-
return s.ConnectNodesStar(*id, ids)
144+
return net.ConnectNodesStar(pivot.ID(), ids)
147145
}
148146

149147
// connect connects two nodes but ignores already connected error.
150-
func (s *Simulation) connect(oneID, otherID enode.ID) error {
151-
return ignoreAlreadyConnectedErr(s.Net.Connect(oneID, otherID))
148+
func (net *Network) connect(oneID, otherID enode.ID) error {
149+
return ignoreAlreadyConnectedErr(net.Connect(oneID, otherID))
152150
}
153151

154152
func ignoreAlreadyConnectedErr(err error) error {
@@ -157,3 +155,22 @@ func ignoreAlreadyConnectedErr(err error) error {
157155
}
158156
return err
159157
}
158+
159+
// SetPivotNode sets the NodeID of the network's pivot node.
160+
// Pivot node is just a specific node that should be treated
161+
// differently then other nodes in test. SetPivotNode and
162+
// GetPivotNode are just a convenient functions to set and
163+
// retrieve it.
164+
func (net *Network) SetPivotNode(id enode.ID) {
165+
net.lock.Lock()
166+
defer net.lock.Unlock()
167+
net.pivotNodeID = id
168+
}
169+
170+
// GetPivotNode returns NodeID of the pivot node set by
171+
// Network.SetPivotNode method.
172+
func (net *Network) GetPivotNode() (node *Node) {
173+
net.lock.RLock()
174+
defer net.lock.RUnlock()
175+
return net.getNode(net.pivotNodeID)
176+
}

‎p2p/simulations/connect_test.go‎

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
// Copyright 2018 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package simulations
18+
19+
import (
20+
"testing"
21+
22+
"github.com/ethereum/go-ethereum/node"
23+
"github.com/ethereum/go-ethereum/p2p/enode"
24+
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
25+
)
26+
27+
func newTestNetwork(t *testing.T, nodeCount int) (*Network, []enode.ID) {
28+
adapter := adapters.NewSimAdapter(adapters.Services{
29+
"noopwoop": func(ctx *adapters.ServiceContext) (node.Service, error) {
30+
return NewNoopService(nil), nil
31+
},
32+
})
33+
34+
// create network
35+
network := NewNetwork(adapter, &NetworkConfig{
36+
DefaultService: "noopwoop",
37+
})
38+
39+
// create and start nodes
40+
ids := make([]enode.ID, nodeCount)
41+
for i := range ids {
42+
conf := adapters.RandomNodeConfig()
43+
node, err := network.NewNodeWithConfig(conf)
44+
if err != nil {
45+
t.Fatalf("error creating node: %s", err)
46+
}
47+
if err := network.Start(node.ID()); err != nil {
48+
t.Fatalf("error starting node: %s", err)
49+
}
50+
ids[i] = node.ID()
51+
}
52+
53+
if len(network.Conns) > 0 {
54+
t.Fatal("no connections should exist after just adding nodes")
55+
}
56+
57+
return network, ids
58+
}
59+
60+
func TestConnectToPivotNode(t *testing.T) {
61+
net, ids := newTestNetwork(t, 2)
62+
defer net.Shutdown()
63+
64+
pivot := ids[0]
65+
net.SetPivotNode(pivot)
66+
67+
other := ids[1]
68+
err := net.ConnectToPivotNode(other)
69+
if err != nil {
70+
t.Fatal(err)
71+
}
72+
73+
if net.GetConn(pivot, other) == nil {
74+
t.Error("pivot and the other node are not connected")
75+
}
76+
}
77+
78+
func TestConnectToLastNode(t *testing.T) {
79+
net, ids := newTestNetwork(t, 10)
80+
defer net.Shutdown()
81+
82+
first := ids[0]
83+
if err := net.ConnectToLastNode(first); err != nil {
84+
t.Fatal(err)
85+
}
86+
87+
last := ids[len(ids)-1]
88+
for i, id := range ids {
89+
if id == first || id == last {
90+
continue
91+
}
92+
93+
if net.GetConn(first, id) != nil {
94+
t.Errorf("connection must not exist with node(ind: %v, id: %v)", i, id)
95+
}
96+
}
97+
98+
if net.GetConn(first, last) == nil {
99+
t.Error("first and last node must be connected")
100+
}
101+
}
102+
103+
func TestConnectToRandomNode(t *testing.T) {
104+
net, ids := newTestNetwork(t, 10)
105+
defer net.Shutdown()
106+
107+
err := net.ConnectToRandomNode(ids[0])
108+
if err != nil {
109+
t.Fatal(err)
110+
}
111+
112+
var cc int
113+
for i, a := range ids {
114+
for _, b := range ids[i:] {
115+
if net.GetConn(a, b) != nil {
116+
cc++
117+
}
118+
}
119+
}
120+
121+
if cc != 1 {
122+
t.Errorf("expected one connection, got %v", cc)
123+
}
124+
}
125+
126+
func TestConnectNodesFull(t *testing.T) {
127+
net, ids := newTestNetwork(t, 12)
128+
defer net.Shutdown()
129+
130+
err := net.ConnectNodesFull(ids)
131+
if err != nil {
132+
t.Fatal(err)
133+
}
134+
135+
VerifyFull(t, net, ids)
136+
}
137+
138+
func TestConnectNodesChain(t *testing.T) {
139+
net, ids := newTestNetwork(t, 10)
140+
defer net.Shutdown()
141+
142+
err := net.ConnectNodesChain(ids)
143+
if err != nil {
144+
t.Fatal(err)
145+
}
146+
147+
VerifyChain(t, net, ids)
148+
}
149+
150+
func TestConnectNodesRing(t *testing.T) {
151+
net, ids := newTestNetwork(t, 10)
152+
defer net.Shutdown()
153+
154+
err := net.ConnectNodesRing(ids)
155+
if err != nil {
156+
t.Fatal(err)
157+
}
158+
159+
VerifyRing(t, net, ids)
160+
}
161+
162+
func TestConnectNodesStar(t *testing.T) {
163+
net, ids := newTestNetwork(t, 10)
164+
defer net.Shutdown()
165+
166+
pivotIndex := 2
167+
168+
err := net.ConnectNodesStar(ids[pivotIndex], ids)
169+
if err != nil {
170+
t.Fatal(err)
171+
}
172+
173+
VerifyStar(t, net, ids, pivotIndex)
174+
}
175+
176+
func TestConnectNodesStarPivot(t *testing.T) {
177+
net, ids := newTestNetwork(t, 10)
178+
defer net.Shutdown()
179+
180+
pivotIndex := 4
181+
182+
net.SetPivotNode(ids[pivotIndex])
183+
184+
err := net.ConnectNodesStarPivot(ids)
185+
if err != nil {
186+
t.Fatal(err)
187+
}
188+
189+
VerifyStar(t, net, ids, pivotIndex)
190+
}

0 commit comments

Comments
 (0)