test.go 3.6 KB

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