github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/p2p/test_util.go (about)

     1  package p2p
     2  
     3  import (
     4  	"net"
     5  
     6  	log "github.com/sirupsen/logrus"
     7  	cmn "github.com/tendermint/tmlibs/common"
     8  
     9  	cfg "github.com/bytom/bytom/config"
    10  	"github.com/bytom/bytom/crypto/ed25519/chainkd"
    11  	dbm "github.com/bytom/bytom/database/leveldb"
    12  	"github.com/bytom/bytom/p2p/connection"
    13  	"github.com/bytom/bytom/p2p/discover/dht"
    14  )
    15  
    16  //PanicOnAddPeerErr add peer error
    17  var PanicOnAddPeerErr = false
    18  
    19  func CreateRandomPeer(outbound bool) *Peer {
    20  	_, netAddr := CreateRoutableAddr()
    21  	p := &Peer{
    22  		peerConn: &peerConn{
    23  			outbound: outbound,
    24  		},
    25  		NodeInfo: &NodeInfo{
    26  			ListenAddr: netAddr.DialString(),
    27  		},
    28  		mconn: &connection.MConnection{},
    29  	}
    30  	return p
    31  }
    32  
    33  func CreateRoutableAddr() (addr string, netAddr *NetAddress) {
    34  	for {
    35  		var err error
    36  		addr = cmn.Fmt("%X@%v.%v.%v.%v:46656", cmn.RandBytes(20), cmn.RandInt()%256, cmn.RandInt()%256, cmn.RandInt()%256, cmn.RandInt()%256)
    37  		netAddr, err = NewNetAddressString(addr)
    38  		if err != nil {
    39  			panic(err)
    40  		}
    41  		if netAddr.Routable() {
    42  			break
    43  		}
    44  	}
    45  	return
    46  }
    47  
    48  // Connect2Switches will connect switches i and j via net.Pipe()
    49  // Blocks until a conection is established.
    50  // NOTE: caller ensures i and j are within bounds
    51  func Connect2Switches(switches []*Switch, i, j int) {
    52  	switchI := switches[i]
    53  	switchJ := switches[j]
    54  	c1, c2 := net.Pipe()
    55  	doneCh := make(chan struct{})
    56  	go func() {
    57  		err := switchI.addPeerWithConnection(c1)
    58  		if PanicOnAddPeerErr && err != nil {
    59  			panic(err)
    60  		}
    61  		doneCh <- struct{}{}
    62  	}()
    63  	go func() {
    64  		err := switchJ.addPeerWithConnection(c2)
    65  		if PanicOnAddPeerErr && err != nil {
    66  			panic(err)
    67  		}
    68  		doneCh <- struct{}{}
    69  	}()
    70  	<-doneCh
    71  	<-doneCh
    72  }
    73  
    74  func startSwitches(switches []*Switch) error {
    75  	for _, s := range switches {
    76  		err := s.Start() // start switch and reactors
    77  		if err != nil {
    78  			return err
    79  		}
    80  	}
    81  	return nil
    82  }
    83  
    84  type mockDiscv struct {
    85  }
    86  
    87  func (m *mockDiscv) ReadRandomNodes(buf []*dht.Node) (n int) {
    88  	return 0
    89  }
    90  
    91  func MakeSwitch(cfg *cfg.Config, testdb dbm.DB, privKey chainkd.XPrv, initSwitch func(*Switch) *Switch) *Switch {
    92  	// new switch, add reactors
    93  	l, listenAddr := GetListener(cfg.P2P)
    94  	cfg.P2P.LANDiscover = false
    95  	sw, err := newSwitch(cfg, new(mockDiscv), nil, l, privKey, listenAddr)
    96  	if err != nil {
    97  		log.Errorf("create switch error: %s", err)
    98  		return nil
    99  	}
   100  	s := initSwitch(sw)
   101  	return s
   102  }