p2p/simulation: small simplifications, renames

This commit is contained in:
Ferenc Szabo 2018-12-14 16:05:21 +01:00
parent 8a15a4405d
commit deb054e186
3 changed files with 25 additions and 35 deletions

View file

@ -50,22 +50,21 @@ func (net *Network) ConnectToLastNode(id enode.ID) (err error) {
if l < 2 { if l < 2 {
return nil return nil
} }
lid := ids[l-1] last := ids[l-1]
if lid == id { if last == id {
lid = ids[l-2] last = ids[l-2]
} }
return net.connect(lid, id) return net.connect(last, id)
} }
// ConnectToRandomNode connects the node with provided NodeID // ConnectToRandomNode connects the node with provided NodeID
// to a random node that is up. // to a random node that is up.
func (net *Network) ConnectToRandomNode(id enode.ID) (err error) { func (net *Network) ConnectToRandomNode(id enode.ID) (err error) {
n := net.GetRandomUpNode(id) selected := net.GetRandomUpNode(id)
if selected == nil {
if n == nil {
return ErrNodeNotFound return ErrNodeNotFound
} }
return net.connect(n.ID(), id) return net.connect(selected.ID(), id)
} }
// ConnectNodesFull connects all nodes one to another. // ConnectNodesFull connects all nodes one to another.
@ -75,11 +74,9 @@ func (net *Network) ConnectNodesFull(ids []enode.ID) (err error) {
if ids == nil { if ids == nil {
ids = net.getUpNodeIDs() ids = net.getUpNodeIDs()
} }
l := len(ids) for i, lid := range ids {
for i := 0; i < l; i++ { for _, rid := range ids[i+1:] {
for j := i + 1; j < l; j++ { if err = net.connect(lid, rid); err != nil {
err = net.connect(ids[i], ids[j])
if err != nil {
return err return err
} }
} }
@ -95,8 +92,7 @@ func (net *Network) ConnectNodesChain(ids []enode.ID) (err error) {
} }
l := len(ids) l := len(ids)
for i := 0; i < l-1; i++ { for i := 0; i < l-1; i++ {
err = net.connect(ids[i], ids[i+1]) if err := net.connect(ids[i], ids[i+1]); err != nil {
if err != nil {
return err return err
} }
} }
@ -113,29 +109,24 @@ func (net *Network) ConnectNodesRing(ids []enode.ID) (err error) {
if l < 2 { if l < 2 {
return nil return nil
} }
for i := 0; i < l-1; i++ { if err := net.ConnectNodesChain(ids); err != nil {
err = net.connect(ids[i], ids[i+1])
if err != nil {
return err return err
} }
}
return net.connect(ids[l-1], ids[0]) return net.connect(ids[l-1], ids[0])
} }
// ConnectNodesStar connects all nodes in a star topology // ConnectNodesStar connects all nodes in a star topology
// with the center at provided NodeID. // with the center at provided NodeID.
// If ids argument is nil, all nodes that are up will be connected. // 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 { if ids == nil {
ids = net.getUpNodeIDs() ids = net.getUpNodeIDs()
} }
l := len(ids) for _, id := range ids {
for i := 0; i < l; i++ { if pivot == id {
if id == ids[i] {
continue continue
} }
err = net.connect(id, ids[i]) if err := net.connect(pivot, id); err != nil {
if err != nil {
return err return err
} }
} }

View file

@ -91,7 +91,7 @@ func TestConnectToLastNode(t *testing.T) {
} }
if net.GetConn(first, id) != nil { 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)
} }
} }

View file

@ -99,19 +99,18 @@ func VerifyChain(t *testing.T, net *Network, ids []enode.ID) {
func VerifyFull(t *testing.T, net *Network, ids []enode.ID) { func VerifyFull(t *testing.T, net *Network, ids []enode.ID) {
t.Helper() t.Helper()
n := len(ids) n := len(ids)
var cc int var connections int
for i := 0; i < n; i++ { for i, lid := range ids {
for j := i + 1; j < n; j++ { for _, rid := range ids[i+1:] {
if net.GetConn(ids[i], ids[j]) != nil { if net.GetConn(lid, rid) != nil {
cc++ connections++
} }
} }
} }
want := n * (n - 1) / 2 want := n * (n - 1) / 2
if connections != want {
if cc != want { t.Errorf("wrong number of connections, got: %v, want: %v", connections, want)
t.Errorf("expected %v connection, got %v", want, cc)
} }
} }