github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/p2p/simulations/test.go (about)

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