github.com/pure-x-eth/consensus_tm@v0.0.0-20230502163723-e3c2ff987250/p2p/mock/peer.go (about)

     1  package mock
     2  
     3  import (
     4  	"net"
     5  
     6  	"github.com/pure-x-eth/consensus_tm/crypto/ed25519"
     7  	"github.com/pure-x-eth/consensus_tm/libs/service"
     8  	"github.com/pure-x-eth/consensus_tm/p2p"
     9  	"github.com/pure-x-eth/consensus_tm/p2p/conn"
    10  )
    11  
    12  type Peer struct {
    13  	*service.BaseService
    14  	ip                   net.IP
    15  	id                   p2p.ID
    16  	addr                 *p2p.NetAddress
    17  	kv                   map[string]interface{}
    18  	Outbound, Persistent bool
    19  }
    20  
    21  // NewPeer creates and starts a new mock peer. If the ip
    22  // is nil, random routable address is used.
    23  func NewPeer(ip net.IP) *Peer {
    24  	var netAddr *p2p.NetAddress
    25  	if ip == nil {
    26  		_, netAddr = p2p.CreateRoutableAddr()
    27  	} else {
    28  		netAddr = p2p.NewNetAddressIPPort(ip, 26656)
    29  	}
    30  	nodeKey := p2p.NodeKey{PrivKey: ed25519.GenPrivKey()}
    31  	netAddr.ID = nodeKey.ID()
    32  	mp := &Peer{
    33  		ip:   ip,
    34  		id:   nodeKey.ID(),
    35  		addr: netAddr,
    36  		kv:   make(map[string]interface{}),
    37  	}
    38  	mp.BaseService = service.NewBaseService(nil, "MockPeer", mp)
    39  	if err := mp.Start(); err != nil {
    40  		panic(err)
    41  	}
    42  	return mp
    43  }
    44  
    45  func (mp *Peer) FlushStop()                          { mp.Stop() } //nolint:errcheck //ignore error
    46  func (mp *Peer) TrySendEnvelope(e p2p.Envelope) bool { return true }
    47  func (mp *Peer) SendEnvelope(e p2p.Envelope) bool    { return true }
    48  func (mp *Peer) TrySend(_ byte, _ []byte) bool       { return true }
    49  func (mp *Peer) Send(_ byte, _ []byte) bool          { return true }
    50  func (mp *Peer) NodeInfo() p2p.NodeInfo {
    51  	return p2p.DefaultNodeInfo{
    52  		DefaultNodeID: mp.addr.ID,
    53  		ListenAddr:    mp.addr.DialString(),
    54  	}
    55  }
    56  func (mp *Peer) Status() conn.ConnectionStatus { return conn.ConnectionStatus{} }
    57  func (mp *Peer) ID() p2p.ID                    { return mp.id }
    58  func (mp *Peer) IsOutbound() bool              { return mp.Outbound }
    59  func (mp *Peer) IsPersistent() bool            { return mp.Persistent }
    60  func (mp *Peer) Get(key string) interface{} {
    61  	if value, ok := mp.kv[key]; ok {
    62  		return value
    63  	}
    64  	return nil
    65  }
    66  func (mp *Peer) Set(key string, value interface{}) {
    67  	mp.kv[key] = value
    68  }
    69  func (mp *Peer) RemoteIP() net.IP            { return mp.ip }
    70  func (mp *Peer) SocketAddr() *p2p.NetAddress { return mp.addr }
    71  func (mp *Peer) RemoteAddr() net.Addr        { return &net.TCPAddr{IP: mp.ip, Port: 8800} }
    72  func (mp *Peer) CloseConn() error            { return nil }
    73  func (mp *Peer) SetRemovalFailed()           {}
    74  func (mp *Peer) GetRemovalFailed() bool      { return false }