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.

70 lines
2.1 KiB

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