github.com/phillinzzz/newBsc@v1.1.6/p2p/simulations/test.go (about)

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