test.go 2.8 KB

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