network_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2017 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. "context"
  19. "fmt"
  20. "testing"
  21. "time"
  22. "github.com/ethereum/go-ethereum/p2p/enode"
  23. "github.com/ethereum/go-ethereum/p2p/simulations/adapters"
  24. )
  25. // TestNetworkSimulation creates a multi-node simulation network with each node
  26. // connected in a ring topology, checks that all nodes successfully handshake
  27. // with each other and that a snapshot fully represents the desired topology
  28. func TestNetworkSimulation(t *testing.T) {
  29. // create simulation network with 20 testService nodes
  30. adapter := adapters.NewSimAdapter(adapters.Services{
  31. "test": newTestService,
  32. })
  33. network := NewNetwork(adapter, &NetworkConfig{
  34. DefaultService: "test",
  35. })
  36. defer network.Shutdown()
  37. nodeCount := 20
  38. ids := make([]enode.ID, nodeCount)
  39. for i := 0; i < nodeCount; i++ {
  40. conf := adapters.RandomNodeConfig()
  41. node, err := network.NewNodeWithConfig(conf)
  42. if err != nil {
  43. t.Fatalf("error creating node: %s", err)
  44. }
  45. if err := network.Start(node.ID()); err != nil {
  46. t.Fatalf("error starting node: %s", err)
  47. }
  48. ids[i] = node.ID()
  49. }
  50. // perform a check which connects the nodes in a ring (so each node is
  51. // connected to exactly two peers) and then checks that all nodes
  52. // performed two handshakes by checking their peerCount
  53. action := func(_ context.Context) error {
  54. for i, id := range ids {
  55. peerID := ids[(i+1)%len(ids)]
  56. if err := network.Connect(id, peerID); err != nil {
  57. return err
  58. }
  59. }
  60. return nil
  61. }
  62. check := func(ctx context.Context, id enode.ID) (bool, error) {
  63. // check we haven't run out of time
  64. select {
  65. case <-ctx.Done():
  66. return false, ctx.Err()
  67. default:
  68. }
  69. // get the node
  70. node := network.GetNode(id)
  71. if node == nil {
  72. return false, fmt.Errorf("unknown node: %s", id)
  73. }
  74. // check it has exactly two peers
  75. client, err := node.Client()
  76. if err != nil {
  77. return false, err
  78. }
  79. var peerCount int64
  80. if err := client.CallContext(ctx, &peerCount, "test_peerCount"); err != nil {
  81. return false, err
  82. }
  83. switch {
  84. case peerCount < 2:
  85. return false, nil
  86. case peerCount == 2:
  87. return true, nil
  88. default:
  89. return false, fmt.Errorf("unexpected peerCount: %d", peerCount)
  90. }
  91. }
  92. timeout := 30 * time.Second
  93. ctx, cancel := context.WithTimeout(context.Background(), timeout)
  94. defer cancel()
  95. // trigger a check every 100ms
  96. trigger := make(chan enode.ID)
  97. go triggerChecks(ctx, ids, trigger, 100*time.Millisecond)
  98. result := NewSimulation(network).Run(ctx, &Step{
  99. Action: action,
  100. Trigger: trigger,
  101. Expect: &Expectation{
  102. Nodes: ids,
  103. Check: check,
  104. },
  105. })
  106. if result.Error != nil {
  107. t.Fatalf("simulation failed: %s", result.Error)
  108. }
  109. // take a network snapshot and check it contains the correct topology
  110. snap, err := network.Snapshot()
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. if len(snap.Nodes) != nodeCount {
  115. t.Fatalf("expected snapshot to contain %d nodes, got %d", nodeCount, len(snap.Nodes))
  116. }
  117. if len(snap.Conns) != nodeCount {
  118. t.Fatalf("expected snapshot to contain %d connections, got %d", nodeCount, len(snap.Conns))
  119. }
  120. for i, id := range ids {
  121. conn := snap.Conns[i]
  122. if conn.One != id {
  123. t.Fatalf("expected conn[%d].One to be %s, got %s", i, id, conn.One)
  124. }
  125. peerID := ids[(i+1)%len(ids)]
  126. if conn.Other != peerID {
  127. t.Fatalf("expected conn[%d].Other to be %s, got %s", i, peerID, conn.Other)
  128. }
  129. }
  130. }
  131. func triggerChecks(ctx context.Context, ids []enode.ID, trigger chan enode.ID, interval time.Duration) {
  132. tick := time.NewTicker(interval)
  133. defer tick.Stop()
  134. for {
  135. select {
  136. case <-tick.C:
  137. for _, id := range ids {
  138. select {
  139. case trigger <- id:
  140. case <-ctx.Done():
  141. return
  142. }
  143. }
  144. case <-ctx.Done():
  145. return
  146. }
  147. }
  148. }