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.

88 lines
2.4 KiB

8 years ago
8 years ago
  1. package client
  2. import (
  3. "time"
  4. "github.com/pkg/errors"
  5. "github.com/tendermint/tendermint/types"
  6. cmn "github.com/tendermint/tmlibs/common"
  7. events "github.com/tendermint/tmlibs/events"
  8. )
  9. // Waiter is informed of current height, decided whether to quit early
  10. type Waiter func(delta int) (abort error)
  11. // DefaultWaitStrategy is the standard backoff algorithm,
  12. // but you can plug in another one
  13. func DefaultWaitStrategy(delta int) (abort error) {
  14. if delta > 10 {
  15. return errors.Errorf("Waiting for %d blocks... aborting", delta)
  16. } else if delta > 0 {
  17. // estimate of wait time....
  18. // wait half a second for the next block (in progress)
  19. // plus one second for every full block
  20. delay := time.Duration(delta-1)*time.Second + 500*time.Millisecond
  21. time.Sleep(delay)
  22. }
  23. return nil
  24. }
  25. // Wait for height will poll status at reasonable intervals until
  26. // the block at the given height is available.
  27. //
  28. // If waiter is nil, we use DefaultWaitStrategy, but you can also
  29. // provide your own implementation
  30. func WaitForHeight(c StatusClient, h int, waiter Waiter) error {
  31. if waiter == nil {
  32. waiter = DefaultWaitStrategy
  33. }
  34. delta := 1
  35. for delta > 0 {
  36. s, err := c.Status()
  37. if err != nil {
  38. return err
  39. }
  40. delta = h - s.LatestBlockHeight
  41. // wait for the time, or abort early
  42. if err := waiter(delta); err != nil {
  43. return err
  44. }
  45. }
  46. return nil
  47. }
  48. // WaitForOneEvent subscribes to a websocket event for the given
  49. // event time and returns upon receiving it one time, or
  50. // when the timeout duration has expired.
  51. //
  52. // This handles subscribing and unsubscribing under the hood
  53. func WaitForOneEvent(evsw types.EventSwitch,
  54. evtTyp string, timeout time.Duration) (types.TMEventData, error) {
  55. listener := cmn.RandStr(12)
  56. evts, quit := make(chan events.EventData, 10), make(chan bool, 1)
  57. // start timeout count-down
  58. go func() {
  59. time.Sleep(timeout)
  60. quit <- true
  61. }()
  62. // register for the next event of this type
  63. evsw.AddListenerForEvent(listener, evtTyp, func(data events.EventData) {
  64. evts <- data
  65. })
  66. // make sure to unregister after the test is over
  67. defer evsw.RemoveListenerForEvent(evtTyp, listener)
  68. // defer evsw.RemoveListener(listener) // this also works
  69. select {
  70. case <-quit:
  71. return types.TMEventData{}, errors.New("timed out waiting for event")
  72. case evt := <-evts:
  73. tmevt, ok := evt.(types.TMEventData)
  74. if ok {
  75. return tmevt, nil
  76. }
  77. return types.TMEventData{}, errors.Errorf("Got unexpected event type: %#v", evt)
  78. }
  79. }