github.com/MagHErmit/tendermint@v0.282.1/p2p/mock/peer.go (about) 1 package mock 2 3 import ( 4 "net" 5 6 "github.com/MagHErmit/tendermint/crypto/ed25519" 7 "github.com/MagHErmit/tendermint/libs/service" 8 "github.com/MagHErmit/tendermint/p2p" 9 "github.com/MagHErmit/tendermint/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) TrySend(chID byte, msgBytes []byte) bool { return true } 47 func (mp *Peer) Send(chID byte, msgBytes []byte) bool { return true } 48 func (mp *Peer) NodeInfo() p2p.NodeInfo { 49 return p2p.DefaultNodeInfo{ 50 DefaultNodeID: mp.addr.ID, 51 ListenAddr: mp.addr.DialString(), 52 } 53 } 54 func (mp *Peer) Status() conn.ConnectionStatus { return conn.ConnectionStatus{} } 55 func (mp *Peer) ID() p2p.ID { return mp.id } 56 func (mp *Peer) IsOutbound() bool { return mp.Outbound } 57 func (mp *Peer) IsPersistent() bool { return mp.Persistent } 58 func (mp *Peer) Get(key string) interface{} { 59 if value, ok := mp.kv[key]; ok { 60 return value 61 } 62 return nil 63 } 64 func (mp *Peer) Set(key string, value interface{}) { 65 mp.kv[key] = value 66 } 67 func (mp *Peer) RemoteIP() net.IP { return mp.ip } 68 func (mp *Peer) SocketAddr() *p2p.NetAddress { return mp.addr } 69 func (mp *Peer) RemoteAddr() net.Addr { return &net.TCPAddr{IP: mp.ip, Port: 8800} } 70 func (mp *Peer) CloseConn() error { return nil } 71 func (mp *Peer) SetRemovalFailed() {} 72 func (mp *Peer) GetRemovalFailed() bool { return false }