github.com/johnwhitton/go-ethereum@v1.8.23/p2p/simulations/test.go (about) 1 package simulations 2 3 import ( 4 "testing" 5 6 "github.com/ethereum/go-ethereum/p2p" 7 "github.com/ethereum/go-ethereum/p2p/enode" 8 "github.com/ethereum/go-ethereum/p2p/enr" 9 "github.com/ethereum/go-ethereum/rpc" 10 ) 11 12 // NoopService is the service that does not do anything 13 // but implements node.Service interface. 14 type NoopService struct { 15 c map[enode.ID]chan struct{} 16 } 17 18 func NewNoopService(ackC map[enode.ID]chan struct{}) *NoopService { 19 return &NoopService{ 20 c: ackC, 21 } 22 } 23 24 func (t *NoopService) Protocols() []p2p.Protocol { 25 return []p2p.Protocol{ 26 { 27 Name: "noop", 28 Version: 666, 29 Length: 0, 30 Run: func(peer *p2p.Peer, rw p2p.MsgReadWriter) error { 31 if t.c != nil { 32 t.c[peer.ID()] = make(chan struct{}) 33 close(t.c[peer.ID()]) 34 } 35 rw.ReadMsg() 36 return nil 37 }, 38 NodeInfo: func() interface{} { 39 return struct{}{} 40 }, 41 PeerInfo: func(id enode.ID) interface{} { 42 return struct{}{} 43 }, 44 Attributes: []enr.Entry{}, 45 }, 46 } 47 } 48 49 func (t *NoopService) APIs() []rpc.API { 50 return []rpc.API{} 51 } 52 53 func (t *NoopService) Start(server *p2p.Server) error { 54 return nil 55 } 56 57 func (t *NoopService) Stop() error { 58 return nil 59 } 60 61 func VerifyRing(t *testing.T, net *Network, ids []enode.ID) { 62 t.Helper() 63 n := len(ids) 64 for i := 0; i < n; i++ { 65 for j := i + 1; j < n; j++ { 66 c := net.GetConn(ids[i], ids[j]) 67 if i == j-1 || (i == 0 && j == n-1) { 68 if c == nil { 69 t.Errorf("nodes %v and %v are not connected, but they should be", i, j) 70 } 71 } else { 72 if c != nil { 73 t.Errorf("nodes %v and %v are connected, but they should not be", i, j) 74 } 75 } 76 } 77 } 78 } 79 80 func VerifyChain(t *testing.T, net *Network, ids []enode.ID) { 81 t.Helper() 82 n := len(ids) 83 for i := 0; i < n; i++ { 84 for j := i + 1; j < n; j++ { 85 c := net.GetConn(ids[i], ids[j]) 86 if i == j-1 { 87 if c == nil { 88 t.Errorf("nodes %v and %v are not connected, but they should be", i, j) 89 } 90 } else { 91 if c != nil { 92 t.Errorf("nodes %v and %v are connected, but they should not be", i, j) 93 } 94 } 95 } 96 } 97 } 98 99 func VerifyFull(t *testing.T, net *Network, ids []enode.ID) { 100 t.Helper() 101 n := len(ids) 102 var connections int 103 for i, lid := range ids { 104 for _, rid := range ids[i+1:] { 105 if net.GetConn(lid, rid) != nil { 106 connections++ 107 } 108 } 109 } 110 111 want := n * (n - 1) / 2 112 if connections != want { 113 t.Errorf("wrong number of connections, got: %v, want: %v", connections, want) 114 } 115 } 116 117 func VerifyStar(t *testing.T, net *Network, ids []enode.ID, centerIndex int) { 118 t.Helper() 119 n := len(ids) 120 for i := 0; i < n; i++ { 121 for j := i + 1; j < n; j++ { 122 c := net.GetConn(ids[i], ids[j]) 123 if i == centerIndex || j == centerIndex { 124 if c == nil { 125 t.Errorf("nodes %v and %v are not connected, but they should be", i, j) 126 } 127 } else { 128 if c != nil { 129 t.Errorf("nodes %v and %v are connected, but they should not be", i, j) 130 } 131 } 132 } 133 } 134 }