github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/network/simulation/connect_test.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 //版权所有2018 Go Ethereum作者 10 //此文件是Go以太坊库的一部分。 11 // 12 //Go-Ethereum库是免费软件:您可以重新分发它和/或修改 13 //根据GNU发布的较低通用公共许可证的条款 14 //自由软件基金会,或者许可证的第3版,或者 15 //(由您选择)任何更高版本。 16 // 17 //Go以太坊图书馆的发行目的是希望它会有用, 18 //但没有任何保证;甚至没有 19 //适销性或特定用途的适用性。见 20 //GNU较低的通用公共许可证,了解更多详细信息。 21 // 22 //你应该收到一份GNU较低级别的公共许可证副本 23 //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。 24 25 package simulation 26 27 import ( 28 "testing" 29 30 "github.com/ethereum/go-ethereum/p2p/discover" 31 ) 32 33 func TestConnectToPivotNode(t *testing.T) { 34 sim := New(noopServiceFuncMap) 35 defer sim.Close() 36 37 pid, err := sim.AddNode() 38 if err != nil { 39 t.Fatal(err) 40 } 41 42 sim.SetPivotNode(pid) 43 44 id, err := sim.AddNode() 45 if err != nil { 46 t.Fatal(err) 47 } 48 49 if len(sim.Net.Conns) > 0 { 50 t.Fatal("no connections should exist after just adding nodes") 51 } 52 53 err = sim.ConnectToPivotNode(id) 54 if err != nil { 55 t.Fatal(err) 56 } 57 58 if sim.Net.GetConn(id, pid) == nil { 59 t.Error("node did not connect to pivot node") 60 } 61 } 62 63 func TestConnectToLastNode(t *testing.T) { 64 sim := New(noopServiceFuncMap) 65 defer sim.Close() 66 67 n := 10 68 69 ids, err := sim.AddNodes(n) 70 if err != nil { 71 t.Fatal(err) 72 } 73 74 id, err := sim.AddNode() 75 if err != nil { 76 t.Fatal(err) 77 } 78 79 if len(sim.Net.Conns) > 0 { 80 t.Fatal("no connections should exist after just adding nodes") 81 } 82 83 err = sim.ConnectToLastNode(id) 84 if err != nil { 85 t.Fatal(err) 86 } 87 88 for _, i := range ids[:n-2] { 89 if sim.Net.GetConn(id, i) != nil { 90 t.Error("node connected to the node that is not the last") 91 } 92 } 93 94 if sim.Net.GetConn(id, ids[n-1]) == nil { 95 t.Error("node did not connect to the last node") 96 } 97 } 98 99 func TestConnectToRandomNode(t *testing.T) { 100 sim := New(noopServiceFuncMap) 101 defer sim.Close() 102 103 n := 10 104 105 ids, err := sim.AddNodes(n) 106 if err != nil { 107 t.Fatal(err) 108 } 109 110 if len(sim.Net.Conns) > 0 { 111 t.Fatal("no connections should exist after just adding nodes") 112 } 113 114 err = sim.ConnectToRandomNode(ids[0]) 115 if err != nil { 116 t.Fatal(err) 117 } 118 119 var cc int 120 for i := 0; i < n; i++ { 121 for j := i + 1; j < n; j++ { 122 if sim.Net.GetConn(ids[i], ids[j]) != nil { 123 cc++ 124 } 125 } 126 } 127 128 if cc != 1 { 129 t.Errorf("expected one connection, got %v", cc) 130 } 131 } 132 133 func TestConnectNodesFull(t *testing.T) { 134 sim := New(noopServiceFuncMap) 135 defer sim.Close() 136 137 ids, err := sim.AddNodes(12) 138 if err != nil { 139 t.Fatal(err) 140 } 141 142 if len(sim.Net.Conns) > 0 { 143 t.Fatal("no connections should exist after just adding nodes") 144 } 145 146 err = sim.ConnectNodesFull(ids) 147 if err != nil { 148 t.Fatal(err) 149 } 150 151 testFull(t, sim, ids) 152 } 153 154 func testFull(t *testing.T, sim *Simulation, ids []discover.NodeID) { 155 n := len(ids) 156 var cc int 157 for i := 0; i < n; i++ { 158 for j := i + 1; j < n; j++ { 159 if sim.Net.GetConn(ids[i], ids[j]) != nil { 160 cc++ 161 } 162 } 163 } 164 165 want := n * (n - 1) / 2 166 167 if cc != want { 168 t.Errorf("expected %v connection, got %v", want, cc) 169 } 170 } 171 172 func TestConnectNodesChain(t *testing.T) { 173 sim := New(noopServiceFuncMap) 174 defer sim.Close() 175 176 ids, err := sim.AddNodes(10) 177 if err != nil { 178 t.Fatal(err) 179 } 180 181 if len(sim.Net.Conns) > 0 { 182 t.Fatal("no connections should exist after just adding nodes") 183 } 184 185 err = sim.ConnectNodesChain(ids) 186 if err != nil { 187 t.Fatal(err) 188 } 189 190 testChain(t, sim, ids) 191 } 192 193 func testChain(t *testing.T, sim *Simulation, ids []discover.NodeID) { 194 n := len(ids) 195 for i := 0; i < n; i++ { 196 for j := i + 1; j < n; j++ { 197 c := sim.Net.GetConn(ids[i], ids[j]) 198 if i == j-1 { 199 if c == nil { 200 t.Errorf("nodes %v and %v are not connected, but they should be", i, j) 201 } 202 } else { 203 if c != nil { 204 t.Errorf("nodes %v and %v are connected, but they should not be", i, j) 205 } 206 } 207 } 208 } 209 } 210 211 func TestConnectNodesRing(t *testing.T) { 212 sim := New(noopServiceFuncMap) 213 defer sim.Close() 214 215 ids, err := sim.AddNodes(10) 216 if err != nil { 217 t.Fatal(err) 218 } 219 220 if len(sim.Net.Conns) > 0 { 221 t.Fatal("no connections should exist after just adding nodes") 222 } 223 224 err = sim.ConnectNodesRing(ids) 225 if err != nil { 226 t.Fatal(err) 227 } 228 229 testRing(t, sim, ids) 230 } 231 232 func testRing(t *testing.T, sim *Simulation, ids []discover.NodeID) { 233 n := len(ids) 234 for i := 0; i < n; i++ { 235 for j := i + 1; j < n; j++ { 236 c := sim.Net.GetConn(ids[i], ids[j]) 237 if i == j-1 || (i == 0 && j == n-1) { 238 if c == nil { 239 t.Errorf("nodes %v and %v are not connected, but they should be", i, j) 240 } 241 } else { 242 if c != nil { 243 t.Errorf("nodes %v and %v are connected, but they should not be", i, j) 244 } 245 } 246 } 247 } 248 } 249 250 func TestConnectToNodesStar(t *testing.T) { 251 sim := New(noopServiceFuncMap) 252 defer sim.Close() 253 254 ids, err := sim.AddNodes(10) 255 if err != nil { 256 t.Fatal(err) 257 } 258 259 if len(sim.Net.Conns) > 0 { 260 t.Fatal("no connections should exist after just adding nodes") 261 } 262 263 centerIndex := 2 264 265 err = sim.ConnectNodesStar(ids[centerIndex], ids) 266 if err != nil { 267 t.Fatal(err) 268 } 269 270 testStar(t, sim, ids, centerIndex) 271 } 272 273 func testStar(t *testing.T, sim *Simulation, ids []discover.NodeID, centerIndex int) { 274 n := len(ids) 275 for i := 0; i < n; i++ { 276 for j := i + 1; j < n; j++ { 277 c := sim.Net.GetConn(ids[i], ids[j]) 278 if i == centerIndex || j == centerIndex { 279 if c == nil { 280 t.Errorf("nodes %v and %v are not connected, but they should be", i, j) 281 } 282 } else { 283 if c != nil { 284 t.Errorf("nodes %v and %v are connected, but they should not be", i, j) 285 } 286 } 287 } 288 } 289 } 290 291 func TestConnectToNodesStarPivot(t *testing.T) { 292 sim := New(noopServiceFuncMap) 293 defer sim.Close() 294 295 ids, err := sim.AddNodes(10) 296 if err != nil { 297 t.Fatal(err) 298 } 299 300 if len(sim.Net.Conns) > 0 { 301 t.Fatal("no connections should exist after just adding nodes") 302 } 303 304 pivotIndex := 4 305 306 sim.SetPivotNode(ids[pivotIndex]) 307 308 err = sim.ConnectNodesStarPivot(ids) 309 if err != nil { 310 t.Fatal(err) 311 } 312 313 testStar(t, sim, ids, pivotIndex) 314 }