Skip to content

Commit 2eb838e

Browse files
frncmxfjl
authored andcommitted
p2p/simulations: eliminate concept of pivot (#18426)
1 parent 38cce9a commit 2eb838e

File tree

7 files changed

+28
-166
lines changed

7 files changed

+28
-166
lines changed

p2p/simulations/adapters/inproc.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -351,17 +351,3 @@ func (sn *SimNode) NodeInfo() *p2p.NodeInfo {
351351
}
352352
return server.NodeInfo()
353353
}
354-
355-
func setSocketBuffer(conn net.Conn, socketReadBuffer int, socketWriteBuffer int) error {
356-
if v, ok := conn.(*net.UnixConn); ok {
357-
err := v.SetReadBuffer(socketReadBuffer)
358-
if err != nil {
359-
return err
360-
}
361-
err = v.SetWriteBuffer(socketWriteBuffer)
362-
if err != nil {
363-
return err
364-
}
365-
}
366-
return nil
367-
}

p2p/simulations/connect.go

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,8 @@ import (
2525

2626
var (
2727
ErrNodeNotFound = errors.New("node not found")
28-
ErrNoPivotNode = errors.New("no pivot node set")
2928
)
3029

31-
// ConnectToPivotNode connects the node with provided NodeID
32-
// to the pivot node, already set by Network.SetPivotNode method.
33-
// It is useful when constructing a star network topology
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 {
38-
return ErrNoPivotNode
39-
}
40-
return net.connect(pivot.ID(), id)
41-
}
42-
4330
// ConnectToLastNode connects the node with provided NodeID
4431
// to the last node that is up, and avoiding connection to self.
4532
// It is useful when constructing a chain network topology
@@ -115,35 +102,23 @@ func (net *Network) ConnectNodesRing(ids []enode.ID) (err error) {
115102
return net.connect(ids[l-1], ids[0])
116103
}
117104

118-
// ConnectNodesStar connects all nodes in a star topology
119-
// with the center at provided NodeID.
105+
// ConnectNodesStar connects all nodes into a star topology
120106
// If ids argument is nil, all nodes that are up will be connected.
121-
func (net *Network) ConnectNodesStar(pivot enode.ID, ids []enode.ID) (err error) {
107+
func (net *Network) ConnectNodesStar(ids []enode.ID, center enode.ID) (err error) {
122108
if ids == nil {
123109
ids = net.getUpNodeIDs()
124110
}
125111
for _, id := range ids {
126-
if pivot == id {
112+
if center == id {
127113
continue
128114
}
129-
if err := net.connect(pivot, id); err != nil {
115+
if err := net.connect(center, id); err != nil {
130116
return err
131117
}
132118
}
133119
return nil
134120
}
135121

136-
// ConnectNodesStarPivot connects all nodes in a star topology
137-
// with the center at already set pivot node.
138-
// If ids argument is nil, all nodes that are up will be connected.
139-
func (net *Network) ConnectNodesStarPivot(ids []enode.ID) (err error) {
140-
pivot := net.GetPivotNode()
141-
if pivot == nil {
142-
return ErrNoPivotNode
143-
}
144-
return net.ConnectNodesStar(pivot.ID(), ids)
145-
}
146-
147122
// connect connects two nodes but ignores already connected error.
148123
func (net *Network) connect(oneID, otherID enode.ID) error {
149124
return ignoreAlreadyConnectedErr(net.Connect(oneID, otherID))
@@ -155,22 +130,3 @@ func ignoreAlreadyConnectedErr(err error) error {
155130
}
156131
return err
157132
}
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: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,6 @@ func newTestNetwork(t *testing.T, nodeCount int) (*Network, []enode.ID) {
5858
return network, ids
5959
}
6060

61-
func TestConnectToPivotNode(t *testing.T) {
62-
net, ids := newTestNetwork(t, 2)
63-
defer net.Shutdown()
64-
65-
pivot := ids[0]
66-
net.SetPivotNode(pivot)
67-
68-
other := ids[1]
69-
err := net.ConnectToPivotNode(other)
70-
if err != nil {
71-
t.Fatal(err)
72-
}
73-
74-
if net.GetConn(pivot, other) == nil {
75-
t.Error("pivot and the other node are not connected")
76-
}
77-
}
78-
7961
func TestConnectToLastNode(t *testing.T) {
8062
net, ids := newTestNetwork(t, 10)
8163
defer net.Shutdown()
@@ -125,15 +107,30 @@ func TestConnectToRandomNode(t *testing.T) {
125107
}
126108

127109
func TestConnectNodesFull(t *testing.T) {
128-
net, ids := newTestNetwork(t, 12)
129-
defer net.Shutdown()
130-
131-
err := net.ConnectNodesFull(ids)
132-
if err != nil {
133-
t.Fatal(err)
110+
tests := []struct {
111+
name string
112+
nodeCount int
113+
}{
114+
{name: "no node", nodeCount: 0},
115+
{name: "single node", nodeCount: 1},
116+
{name: "2 nodes", nodeCount: 2},
117+
{name: "3 nodes", nodeCount: 3},
118+
{name: "even number of nodes", nodeCount: 12},
119+
{name: "odd number of nodes", nodeCount: 13},
134120
}
121+
for _, test := range tests {
122+
t.Run(test.name, func(t *testing.T) {
123+
net, ids := newTestNetwork(t, test.nodeCount)
124+
defer net.Shutdown()
125+
126+
err := net.ConnectNodesFull(ids)
127+
if err != nil {
128+
t.Fatal(err)
129+
}
135130

136-
VerifyFull(t, net, ids)
131+
VerifyFull(t, net, ids)
132+
})
133+
}
137134
}
138135

139136
func TestConnectNodesChain(t *testing.T) {
@@ -166,23 +163,7 @@ func TestConnectNodesStar(t *testing.T) {
166163

167164
pivotIndex := 2
168165

169-
err := net.ConnectNodesStar(ids[pivotIndex], ids)
170-
if err != nil {
171-
t.Fatal(err)
172-
}
173-
174-
VerifyStar(t, net, ids, pivotIndex)
175-
}
176-
177-
func TestConnectNodesStarPivot(t *testing.T) {
178-
net, ids := newTestNetwork(t, 10)
179-
defer net.Shutdown()
180-
181-
pivotIndex := 4
182-
183-
net.SetPivotNode(ids[pivotIndex])
184-
185-
err := net.ConnectNodesStarPivot(ids)
166+
err := net.ConnectNodesStar(ids, ids[pivotIndex])
186167
if err != nil {
187168
t.Fatal(err)
188169
}

p2p/simulations/network.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ type Network struct {
5858
Conns []*Conn `json:"conns"`
5959
connMap map[string]int
6060

61-
pivotNodeID enode.ID
62-
6361
nodeAdapter adapters.NodeAdapter
6462
events event.Feed
6563
lock sync.RWMutex

swarm/network/simulation/node.go

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func (s *Simulation) AddNodesAndConnectStar(count int, opts ...AddNodeOption) (i
188188
if err != nil {
189189
return nil, err
190190
}
191-
err = s.Net.ConnectNodesStar(ids[0], ids[1:])
191+
err = s.Net.ConnectNodesStar(ids[1:], ids[0])
192192
if err != nil {
193193
return nil, err
194194
}
@@ -241,25 +241,6 @@ func (s *Simulation) UploadSnapshot(snapshotFile string, opts ...AddNodeOption)
241241
return nil
242242
}
243243

244-
// SetPivotNode sets the NodeID of the network's pivot node.
245-
// Pivot node is just a specific node that should be treated
246-
// differently then other nodes in test. SetPivotNode and
247-
// PivotNodeID are just a convenient functions to set and
248-
// retrieve it.
249-
func (s *Simulation) SetPivotNode(id enode.ID) {
250-
s.mu.Lock()
251-
defer s.mu.Unlock()
252-
s.pivotNodeID = &id
253-
}
254-
255-
// PivotNodeID returns NodeID of the pivot node set by
256-
// Simulation.SetPivotNode method.
257-
func (s *Simulation) PivotNodeID() (id *enode.ID) {
258-
s.mu.Lock()
259-
defer s.mu.Unlock()
260-
return s.pivotNodeID
261-
}
262-
263244
// StartNode starts a node by NodeID.
264245
func (s *Simulation) StartNode(id enode.ID) (err error) {
265246
return s.Net.Start(id)

swarm/network/simulation/node_test.go

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -314,45 +314,6 @@ func TestUploadSnapshot(t *testing.T) {
314314
log.Debug("Done.")
315315
}
316316

317-
func TestPivotNode(t *testing.T) {
318-
sim := New(noopServiceFuncMap)
319-
defer sim.Close()
320-
321-
id, err := sim.AddNode()
322-
if err != nil {
323-
t.Fatal(err)
324-
}
325-
326-
id2, err := sim.AddNode()
327-
if err != nil {
328-
t.Fatal(err)
329-
}
330-
331-
if sim.PivotNodeID() != nil {
332-
t.Error("expected no pivot node")
333-
}
334-
335-
sim.SetPivotNode(id)
336-
337-
pid := sim.PivotNodeID()
338-
339-
if pid == nil {
340-
t.Error("pivot node not set")
341-
} else if *pid != id {
342-
t.Errorf("expected pivot node %s, got %s", id, *pid)
343-
}
344-
345-
sim.SetPivotNode(id2)
346-
347-
pid = sim.PivotNodeID()
348-
349-
if pid == nil {
350-
t.Error("pivot node not set")
351-
} else if *pid != id2 {
352-
t.Errorf("expected pivot node %s, got %s", id2, *pid)
353-
}
354-
}
355-
356317
func TestStartStopNode(t *testing.T) {
357318
sim := New(noopServiceFuncMap)
358319
defer sim.Close()

swarm/network/simulation/simulation.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ type Simulation struct {
4646
serviceNames []string
4747
cleanupFuncs []func()
4848
buckets map[enode.ID]*sync.Map
49-
pivotNodeID *enode.ID
5049
shutdownWG sync.WaitGroup
5150
done chan struct{}
5251
mu sync.RWMutex

0 commit comments

Comments
 (0)