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.

68 lines
2.0 KiB

  1. package mock
  2. import (
  3. "net"
  4. "github.com/tendermint/tendermint/crypto/ed25519"
  5. cmn "github.com/tendermint/tendermint/libs/common"
  6. "github.com/tendermint/tendermint/p2p"
  7. "github.com/tendermint/tendermint/p2p/conn"
  8. )
  9. type Peer struct {
  10. *cmn.BaseService
  11. ip net.IP
  12. id p2p.ID
  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 = p2p.NewNetAddressIPPort(ip, 26656)
  25. }
  26. nodeKey := p2p.NodeKey{PrivKey: ed25519.GenPrivKey()}
  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 = cmn.NewBaseService(nil, "MockPeer", mp)
  35. mp.Start()
  36. return mp
  37. }
  38. func (mp *Peer) FlushStop() { mp.Stop() }
  39. func (mp *Peer) TrySend(chID byte, msgBytes []byte) bool { return true }
  40. func (mp *Peer) Send(chID byte, msgBytes []byte) bool { return true }
  41. func (mp *Peer) NodeInfo() p2p.NodeInfo {
  42. return p2p.DefaultNodeInfo{
  43. ID_: mp.addr.ID,
  44. ListenAddr: mp.addr.DialString(),
  45. }
  46. }
  47. func (mp *Peer) Status() conn.ConnectionStatus { return conn.ConnectionStatus{} }
  48. func (mp *Peer) ID() p2p.ID { return mp.id }
  49. func (mp *Peer) IsOutbound() bool { return mp.Outbound }
  50. func (mp *Peer) IsPersistent() bool { return mp.Persistent }
  51. func (mp *Peer) Get(key string) interface{} {
  52. if value, ok := mp.kv[key]; ok {
  53. return value
  54. }
  55. return nil
  56. }
  57. func (mp *Peer) Set(key string, value interface{}) {
  58. mp.kv[key] = value
  59. }
  60. func (mp *Peer) RemoteIP() net.IP { return mp.ip }
  61. func (mp *Peer) OriginalAddr() *p2p.NetAddress { return mp.addr }
  62. func (mp *Peer) RemoteAddr() net.Addr { return &net.TCPAddr{IP: mp.ip, Port: 8800} }
  63. func (mp *Peer) CloseConn() error { return nil }