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.

264 lines
6.0 KiB

  1. package monitor
  2. import (
  3. "encoding/json"
  4. "math"
  5. "time"
  6. "github.com/pkg/errors"
  7. "github.com/tendermint/tendermint/crypto"
  8. "github.com/tendermint/tendermint/libs/events"
  9. "github.com/tendermint/tendermint/libs/log"
  10. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  11. rpc_client "github.com/tendermint/tendermint/rpc/lib/client"
  12. em "github.com/tendermint/tendermint/tools/tm-monitor/eventmeter"
  13. tmtypes "github.com/tendermint/tendermint/types"
  14. )
  15. const maxRestarts = 25
  16. type Node struct {
  17. IsValidator bool `json:"is_validator"` // validator or non-validator?
  18. Online bool `json:"online"`
  19. Height int64 `json:"height"`
  20. rpcAddr string
  21. Name string `json:"name"`
  22. pubKey crypto.PubKey
  23. BlockLatency float64 `json:"block_latency" amino:"unsafe"` // ms, interval between block commits
  24. // em holds the ws connection. Each eventMeter callback is called in a separate go-routine.
  25. em eventMeter
  26. // rpcClient is an client for making RPC calls to TM
  27. rpcClient rpc_client.HTTPClient
  28. blockCh chan<- *tmtypes.Block
  29. blockLatencyCh chan<- float64
  30. disconnectCh chan<- bool
  31. checkIsValidatorInterval time.Duration
  32. quit chan struct{}
  33. logger log.Logger
  34. }
  35. func NewNode(rpcAddr string, options ...func(*Node)) *Node {
  36. em := em.NewEventMeter(rpcAddr, UnmarshalEvent)
  37. rpcClient := rpc_client.NewURIClient(rpcAddr) // HTTP client by default
  38. rpcClient.SetCodec(cdc)
  39. return NewNodeWithEventMeterAndRpcClient(rpcAddr, em, rpcClient, options...)
  40. }
  41. func NewNodeWithEventMeterAndRpcClient(
  42. rpcAddr string,
  43. em eventMeter,
  44. rpcClient rpc_client.HTTPClient,
  45. options ...func(*Node)) *Node {
  46. n := &Node{
  47. rpcAddr: rpcAddr,
  48. em: em,
  49. rpcClient: rpcClient,
  50. Name: rpcAddr,
  51. quit: make(chan struct{}),
  52. checkIsValidatorInterval: 5 * time.Second,
  53. logger: log.NewNopLogger(),
  54. }
  55. for _, option := range options {
  56. option(n)
  57. }
  58. return n
  59. }
  60. // SetCheckIsValidatorInterval lets you change interval for checking whenever
  61. // node is still a validator or not.
  62. func SetCheckIsValidatorInterval(d time.Duration) func(n *Node) {
  63. return func(n *Node) {
  64. n.checkIsValidatorInterval = d
  65. }
  66. }
  67. func (n *Node) SendBlocksTo(ch chan<- *tmtypes.Block) {
  68. n.blockCh = ch
  69. }
  70. func (n *Node) SendBlockLatenciesTo(ch chan<- float64) {
  71. n.blockLatencyCh = ch
  72. }
  73. func (n *Node) NotifyAboutDisconnects(ch chan<- bool) {
  74. n.disconnectCh = ch
  75. }
  76. // SetLogger lets you set your own logger
  77. func (n *Node) SetLogger(l log.Logger) {
  78. n.logger = l
  79. n.em.SetLogger(l)
  80. }
  81. func (n *Node) Start() error {
  82. if err := n.em.Start(); err != nil {
  83. return err
  84. }
  85. n.em.RegisterLatencyCallback(latencyCallback(n))
  86. err := n.em.Subscribe(tmtypes.EventQueryNewBlock.String(), newBlockCallback(n))
  87. if err != nil {
  88. return err
  89. }
  90. n.em.RegisterDisconnectCallback(disconnectCallback(n))
  91. n.Online = true
  92. n.checkIsValidator()
  93. go n.checkIsValidatorLoop()
  94. return nil
  95. }
  96. func (n *Node) Stop() {
  97. n.Online = false
  98. n.em.Stop()
  99. close(n.quit)
  100. }
  101. // implements eventmeter.EventCallbackFunc
  102. func newBlockCallback(n *Node) em.EventCallbackFunc {
  103. return func(metric *em.EventMetric, data interface{}) {
  104. block := data.(tmtypes.TMEventData).(tmtypes.EventDataNewBlock).Block
  105. n.Height = block.Height
  106. n.logger.Info("new block", "height", block.Height)
  107. if n.blockCh != nil {
  108. n.blockCh <- block
  109. }
  110. }
  111. }
  112. // implements eventmeter.EventLatencyFunc
  113. func latencyCallback(n *Node) em.LatencyCallbackFunc {
  114. return func(latency float64) {
  115. n.BlockLatency = latency / 1000000.0 // ns to ms
  116. n.logger.Info("new block latency", "latency", n.BlockLatency)
  117. if n.blockLatencyCh != nil {
  118. n.blockLatencyCh <- latency
  119. }
  120. }
  121. }
  122. // implements eventmeter.DisconnectCallbackFunc
  123. func disconnectCallback(n *Node) em.DisconnectCallbackFunc {
  124. return func() {
  125. n.Online = false
  126. n.logger.Info("status", "down")
  127. if n.disconnectCh != nil {
  128. n.disconnectCh <- true
  129. }
  130. }
  131. }
  132. func (n *Node) RestartEventMeterBackoff() error {
  133. attempt := 0
  134. for {
  135. d := time.Duration(math.Exp2(float64(attempt)))
  136. time.Sleep(d * time.Second)
  137. if err := n.em.Start(); err != nil {
  138. n.logger.Info("restart failed", "err", err)
  139. } else {
  140. // TODO: authenticate pubkey
  141. return nil
  142. }
  143. attempt++
  144. if attempt > maxRestarts {
  145. return errors.New("Reached max restarts")
  146. }
  147. }
  148. }
  149. func (n *Node) NumValidators() (height int64, num int, err error) {
  150. height, vals, err := n.validators()
  151. if err != nil {
  152. return 0, 0, err
  153. }
  154. return height, len(vals), nil
  155. }
  156. func (n *Node) validators() (height int64, validators []*tmtypes.Validator, err error) {
  157. vals := new(ctypes.ResultValidators)
  158. if _, err = n.rpcClient.Call("validators", nil, vals); err != nil {
  159. return 0, make([]*tmtypes.Validator, 0), err
  160. }
  161. return vals.BlockHeight, vals.Validators, nil
  162. }
  163. func (n *Node) checkIsValidatorLoop() {
  164. for {
  165. select {
  166. case <-n.quit:
  167. return
  168. case <-time.After(n.checkIsValidatorInterval):
  169. n.checkIsValidator()
  170. }
  171. }
  172. }
  173. func (n *Node) checkIsValidator() {
  174. _, validators, err := n.validators()
  175. if err == nil {
  176. for _, v := range validators {
  177. key, err1 := n.getPubKey()
  178. if err1 == nil && v.PubKey.Equals(key) {
  179. n.IsValidator = true
  180. }
  181. }
  182. } else {
  183. n.logger.Info("check is validator failed", "err", err)
  184. }
  185. }
  186. func (n *Node) getPubKey() (crypto.PubKey, error) {
  187. if n.pubKey != nil {
  188. return n.pubKey, nil
  189. }
  190. status := new(ctypes.ResultStatus)
  191. _, err := n.rpcClient.Call("status", nil, status)
  192. if err != nil {
  193. return nil, err
  194. }
  195. n.pubKey = status.ValidatorInfo.PubKey
  196. return n.pubKey, nil
  197. }
  198. type eventMeter interface {
  199. Start() error
  200. Stop()
  201. RegisterLatencyCallback(em.LatencyCallbackFunc)
  202. RegisterDisconnectCallback(em.DisconnectCallbackFunc)
  203. Subscribe(string, em.EventCallbackFunc) error
  204. Unsubscribe(string) error
  205. SetLogger(l log.Logger)
  206. }
  207. // UnmarshalEvent unmarshals a json event
  208. func UnmarshalEvent(b json.RawMessage) (string, events.EventData, error) {
  209. event := new(ctypes.ResultEvent)
  210. if err := cdc.UnmarshalJSON(b, event); err != nil {
  211. return "", nil, err
  212. }
  213. return event.Query, event.Data, nil
  214. }