connect_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. package simulations
  17. import (
  18. "testing"
  19. "github.com/ethereum/go-ethereum/node"
  20. "github.com/ethereum/go-ethereum/p2p/enode"
  21. "github.com/ethereum/go-ethereum/p2p/simulations/adapters"
  22. )
  23. func newTestNetwork(t *testing.T, nodeCount int) (*Network, []enode.ID) {
  24. t.Helper()
  25. adapter := adapters.NewSimAdapter(adapters.LifecycleConstructors{
  26. "noopwoop": func(ctx *adapters.ServiceContext, stack *node.Node) (node.Lifecycle, error) {
  27. return NewNoopService(nil), nil
  28. },
  29. })
  30. // create network
  31. network := NewNetwork(adapter, &NetworkConfig{
  32. DefaultService: "noopwoop",
  33. })
  34. // create and start nodes
  35. ids := make([]enode.ID, nodeCount)
  36. for i := range ids {
  37. conf := adapters.RandomNodeConfig()
  38. node, err := network.NewNodeWithConfig(conf)
  39. if err != nil {
  40. t.Fatalf("error creating node: %s", err)
  41. }
  42. if err := network.Start(node.ID()); err != nil {
  43. t.Fatalf("error starting node: %s", err)
  44. }
  45. ids[i] = node.ID()
  46. }
  47. if len(network.Conns) > 0 {
  48. t.Fatal("no connections should exist after just adding nodes")
  49. }
  50. return network, ids
  51. }
  52. func TestConnectToLastNode(t *testing.T) {
  53. net, ids := newTestNetwork(t, 10)
  54. defer net.Shutdown()
  55. first := ids[0]
  56. if err := net.ConnectToLastNode(first); err != nil {
  57. t.Fatal(err)
  58. }
  59. last := ids[len(ids)-1]
  60. for i, id := range ids {
  61. if id == first || id == last {
  62. continue
  63. }
  64. if net.GetConn(first, id) != nil {
  65. t.Errorf("connection must not exist with node(ind: %v, id: %v)", i, id)
  66. }
  67. }
  68. if net.GetConn(first, last) == nil {
  69. t.Error("first and last node must be connected")
  70. }
  71. }
  72. func TestConnectToRandomNode(t *testing.T) {
  73. net, ids := newTestNetwork(t, 10)
  74. defer net.Shutdown()
  75. err := net.ConnectToRandomNode(ids[0])
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. var cc int
  80. for i, a := range ids {
  81. for _, b := range ids[i:] {
  82. if net.GetConn(a, b) != nil {
  83. cc++
  84. }
  85. }
  86. }
  87. if cc != 1 {
  88. t.Errorf("expected one connection, got %v", cc)
  89. }
  90. }
  91. func TestConnectNodesFull(t *testing.T) {
  92. tests := []struct {
  93. name string
  94. nodeCount int
  95. }{
  96. {name: "no node", nodeCount: 0},
  97. {name: "single node", nodeCount: 1},
  98. {name: "2 nodes", nodeCount: 2},
  99. {name: "3 nodes", nodeCount: 3},
  100. {name: "even number of nodes", nodeCount: 12},
  101. {name: "odd number of nodes", nodeCount: 13},
  102. }
  103. for _, test := range tests {
  104. t.Run(test.name, func(t *testing.T) {
  105. net, ids := newTestNetwork(t, test.nodeCount)
  106. defer net.Shutdown()
  107. err := net.ConnectNodesFull(ids)
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. VerifyFull(t, net, ids)
  112. })
  113. }
  114. }
  115. func TestConnectNodesChain(t *testing.T) {
  116. net, ids := newTestNetwork(t, 10)
  117. defer net.Shutdown()
  118. err := net.ConnectNodesChain(ids)
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. VerifyChain(t, net, ids)
  123. }
  124. func TestConnectNodesRing(t *testing.T) {
  125. net, ids := newTestNetwork(t, 10)
  126. defer net.Shutdown()
  127. err := net.ConnectNodesRing(ids)
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. VerifyRing(t, net, ids)
  132. }
  133. func TestConnectNodesStar(t *testing.T) {
  134. net, ids := newTestNetwork(t, 10)
  135. defer net.Shutdown()
  136. pivotIndex := 2
  137. err := net.ConnectNodesStar(ids, ids[pivotIndex])
  138. if err != nil {
  139. t.Fatal(err)
  140. }
  141. VerifyStar(t, net, ids, pivotIndex)
  142. }