github.com/codingfuture/orig-energi3@v0.8.4/p2p/simulations/test.go (about)

     1  // Copyright 2018 The Energi Core Authors
     2  // Copyright 2018 The go-ethereum Authors
     3  // This file is part of the Energi Core library.
     4  //
     5  // The Energi Core library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The Energi Core library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package simulations
    19  
    20  import (
    21  	"testing"
    22  
    23  	"github.com/ethereum/go-ethereum/p2p"
    24  	"github.com/ethereum/go-ethereum/p2p/enode"
    25  	"github.com/ethereum/go-ethereum/p2p/enr"
    26  	"github.com/ethereum/go-ethereum/rpc"
    27  )
    28  
    29  // NoopService is the service that does not do anything
    30  // but implements node.Service interface.
    31  type NoopService struct {
    32  	c map[enode.ID]chan struct{}
    33  }
    34  
    35  func NewNoopService(ackC map[enode.ID]chan struct{}) *NoopService {
    36  	return &NoopService{
    37  		c: ackC,
    38  	}
    39  }
    40  
    41  func (t *NoopService) Protocols() []p2p.Protocol {
    42  	return []p2p.Protocol{
    43  		{
    44  			Name:    "noop",
    45  			Version: 666,
    46  			Length:  0,
    47  			Run: func(peer *p2p.Peer, rw p2p.MsgReadWriter) error {
    48  				if t.c != nil {
    49  					t.c[peer.ID()] = make(chan struct{})
    50  					close(t.c[peer.ID()])
    51  				}
    52  				rw.ReadMsg()
    53  				return nil
    54  			},
    55  			NodeInfo: func() interface{} {
    56  				return struct{}{}
    57  			},
    58  			PeerInfo: func(id enode.ID) interface{} {
    59  				return struct{}{}
    60  			},
    61  			Attributes: []enr.Entry{},
    62  		},
    63  	}
    64  }
    65  
    66  func (t *NoopService) APIs() []rpc.API {
    67  	return []rpc.API{}
    68  }
    69  
    70  func (t *NoopService) Start(server *p2p.Server) error {
    71  	return nil
    72  }
    73  
    74  func (t *NoopService) Stop() error {
    75  	return nil
    76  }
    77  
    78  func VerifyRing(t *testing.T, net *Network, ids []enode.ID) {
    79  	t.Helper()
    80  	n := len(ids)
    81  	for i := 0; i < n; i++ {
    82  		for j := i + 1; j < n; j++ {
    83  			c := net.GetConn(ids[i], ids[j])
    84  			if i == j-1 || (i == 0 && j == n-1) {
    85  				if c == nil {
    86  					t.Errorf("nodes %v and %v are not connected, but they should be", i, j)
    87  				}
    88  			} else {
    89  				if c != nil {
    90  					t.Errorf("nodes %v and %v are connected, but they should not be", i, j)
    91  				}
    92  			}
    93  		}
    94  	}
    95  }
    96  
    97  func VerifyChain(t *testing.T, net *Network, ids []enode.ID) {
    98  	t.Helper()
    99  	n := len(ids)
   100  	for i := 0; i < n; i++ {
   101  		for j := i + 1; j < n; j++ {
   102  			c := net.GetConn(ids[i], ids[j])
   103  			if i == j-1 {
   104  				if c == nil {
   105  					t.Errorf("nodes %v and %v are not connected, but they should be", i, j)
   106  				}
   107  			} else {
   108  				if c != nil {
   109  					t.Errorf("nodes %v and %v are connected, but they should not be", i, j)
   110  				}
   111  			}
   112  		}
   113  	}
   114  }
   115  
   116  func VerifyFull(t *testing.T, net *Network, ids []enode.ID) {
   117  	t.Helper()
   118  	n := len(ids)
   119  	var connections int
   120  	for i, lid := range ids {
   121  		for _, rid := range ids[i+1:] {
   122  			if net.GetConn(lid, rid) != nil {
   123  				connections++
   124  			}
   125  		}
   126  	}
   127  
   128  	want := n * (n - 1) / 2
   129  	if connections != want {
   130  		t.Errorf("wrong number of connections, got: %v, want: %v", connections, want)
   131  	}
   132  }
   133  
   134  func VerifyStar(t *testing.T, net *Network, ids []enode.ID, centerIndex int) {
   135  	t.Helper()
   136  	n := len(ids)
   137  	for i := 0; i < n; i++ {
   138  		for j := i + 1; j < n; j++ {
   139  			c := net.GetConn(ids[i], ids[j])
   140  			if i == centerIndex || j == centerIndex {
   141  				if c == nil {
   142  					t.Errorf("nodes %v and %v are not connected, but they should be", i, j)
   143  				}
   144  			} else {
   145  				if c != nil {
   146  					t.Errorf("nodes %v and %v are connected, but they should not be", i, j)
   147  				}
   148  			}
   149  		}
   150  	}
   151  }