mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
p2p/simulation: small simplifications, renames
This commit is contained in:
parent
8a15a4405d
commit
deb054e186
3 changed files with 25 additions and 35 deletions
|
|
@ -50,22 +50,21 @@ func (net *Network) ConnectToLastNode(id enode.ID) (err error) {
|
|||
if l < 2 {
|
||||
return nil
|
||||
}
|
||||
lid := ids[l-1]
|
||||
if lid == id {
|
||||
lid = ids[l-2]
|
||||
last := ids[l-1]
|
||||
if last == id {
|
||||
last = ids[l-2]
|
||||
}
|
||||
return net.connect(lid, id)
|
||||
return net.connect(last, id)
|
||||
}
|
||||
|
||||
// ConnectToRandomNode connects the node with provided NodeID
|
||||
// to a random node that is up.
|
||||
func (net *Network) ConnectToRandomNode(id enode.ID) (err error) {
|
||||
n := net.GetRandomUpNode(id)
|
||||
|
||||
if n == nil {
|
||||
selected := net.GetRandomUpNode(id)
|
||||
if selected == nil {
|
||||
return ErrNodeNotFound
|
||||
}
|
||||
return net.connect(n.ID(), id)
|
||||
return net.connect(selected.ID(), id)
|
||||
}
|
||||
|
||||
// ConnectNodesFull connects all nodes one to another.
|
||||
|
|
@ -75,11 +74,9 @@ func (net *Network) ConnectNodesFull(ids []enode.ID) (err error) {
|
|||
if ids == nil {
|
||||
ids = net.getUpNodeIDs()
|
||||
}
|
||||
l := len(ids)
|
||||
for i := 0; i < l; i++ {
|
||||
for j := i + 1; j < l; j++ {
|
||||
err = net.connect(ids[i], ids[j])
|
||||
if err != nil {
|
||||
for i, lid := range ids {
|
||||
for _, rid := range ids[i+1:] {
|
||||
if err = net.connect(lid, rid); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
@ -95,8 +92,7 @@ func (net *Network) ConnectNodesChain(ids []enode.ID) (err error) {
|
|||
}
|
||||
l := len(ids)
|
||||
for i := 0; i < l-1; i++ {
|
||||
err = net.connect(ids[i], ids[i+1])
|
||||
if err != nil {
|
||||
if err := net.connect(ids[i], ids[i+1]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
@ -113,11 +109,8 @@ func (net *Network) ConnectNodesRing(ids []enode.ID) (err error) {
|
|||
if l < 2 {
|
||||
return nil
|
||||
}
|
||||
for i := 0; i < l-1; i++ {
|
||||
err = net.connect(ids[i], ids[i+1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := net.ConnectNodesChain(ids); err != nil {
|
||||
return err
|
||||
}
|
||||
return net.connect(ids[l-1], ids[0])
|
||||
}
|
||||
|
|
@ -125,17 +118,15 @@ func (net *Network) ConnectNodesRing(ids []enode.ID) (err error) {
|
|||
// ConnectNodesStar connects all nodes in a star topology
|
||||
// with the center at provided NodeID.
|
||||
// If ids argument is nil, all nodes that are up will be connected.
|
||||
func (net *Network) ConnectNodesStar(id enode.ID, ids []enode.ID) (err error) {
|
||||
func (net *Network) ConnectNodesStar(pivot enode.ID, ids []enode.ID) (err error) {
|
||||
if ids == nil {
|
||||
ids = net.getUpNodeIDs()
|
||||
}
|
||||
l := len(ids)
|
||||
for i := 0; i < l; i++ {
|
||||
if id == ids[i] {
|
||||
for _, id := range ids {
|
||||
if pivot == id {
|
||||
continue
|
||||
}
|
||||
err = net.connect(id, ids[i])
|
||||
if err != nil {
|
||||
if err := net.connect(pivot, id); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -91,7 +91,7 @@ func TestConnectToLastNode(t *testing.T) {
|
|||
}
|
||||
|
||||
if net.GetConn(first, id) != nil {
|
||||
t.Errorf("connection must not exits with node(ind: %v, id: %v)", i, id)
|
||||
t.Errorf("connection must not exist with node(ind: %v, id: %v)", i, id)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -99,19 +99,18 @@ func VerifyChain(t *testing.T, net *Network, ids []enode.ID) {
|
|||
func VerifyFull(t *testing.T, net *Network, ids []enode.ID) {
|
||||
t.Helper()
|
||||
n := len(ids)
|
||||
var cc int
|
||||
for i := 0; i < n; i++ {
|
||||
for j := i + 1; j < n; j++ {
|
||||
if net.GetConn(ids[i], ids[j]) != nil {
|
||||
cc++
|
||||
var connections int
|
||||
for i, lid := range ids {
|
||||
for _, rid := range ids[i+1:] {
|
||||
if net.GetConn(lid, rid) != nil {
|
||||
connections++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
want := n * (n - 1) / 2
|
||||
|
||||
if cc != want {
|
||||
t.Errorf("expected %v connection, got %v", want, cc)
|
||||
if connections != want {
|
||||
t.Errorf("wrong number of connections, got: %v, want: %v", connections, want)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue