You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.0 KiB

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