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.

83 lines
2.1 KiB

  1. package privval
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/tendermint/tendermint/crypto"
  6. "github.com/tendermint/tendermint/types"
  7. )
  8. // RetrySignerClient wraps SignerClient adding retry for each operation (except
  9. // Ping) w/ a timeout.
  10. type RetrySignerClient struct {
  11. next *SignerClient
  12. retries int
  13. timeout time.Duration
  14. }
  15. // NewRetrySignerClient returns RetrySignerClient. If +retries+ is 0, the
  16. // client will be retrying each operation indefinitely.
  17. func NewRetrySignerClient(sc *SignerClient, retries int, timeout time.Duration) *RetrySignerClient {
  18. return &RetrySignerClient{sc, retries, timeout}
  19. }
  20. var _ types.PrivValidator = (*RetrySignerClient)(nil)
  21. func (sc *RetrySignerClient) Close() error {
  22. return sc.next.Close()
  23. }
  24. func (sc *RetrySignerClient) IsConnected() bool {
  25. return sc.next.IsConnected()
  26. }
  27. func (sc *RetrySignerClient) WaitForConnection(maxWait time.Duration) error {
  28. return sc.next.WaitForConnection(maxWait)
  29. }
  30. //--------------------------------------------------------
  31. // Implement PrivValidator
  32. func (sc *RetrySignerClient) Ping() error {
  33. return sc.next.Ping()
  34. }
  35. func (sc *RetrySignerClient) GetPubKey() (crypto.PubKey, error) {
  36. var (
  37. pk crypto.PubKey
  38. err error
  39. )
  40. for i := 0; i < sc.retries || sc.retries == 0; i++ {
  41. pk, err = sc.next.GetPubKey()
  42. if err == nil {
  43. return pk, nil
  44. }
  45. time.Sleep(sc.timeout)
  46. }
  47. return nil, fmt.Errorf("exhausted all attempts to get pubkey: %w", err)
  48. }
  49. func (sc *RetrySignerClient) SignVote(chainID string, vote *types.Vote) error {
  50. var err error
  51. for i := 0; i < sc.retries || sc.retries == 0; i++ {
  52. err = sc.next.SignVote(chainID, vote)
  53. if err == nil {
  54. return nil
  55. }
  56. time.Sleep(sc.timeout)
  57. }
  58. return fmt.Errorf("exhausted all attempts to sign vote: %w", err)
  59. }
  60. func (sc *RetrySignerClient) SignProposal(chainID string, proposal *types.Proposal) error {
  61. var err error
  62. for i := 0; i < sc.retries || sc.retries == 0; i++ {
  63. err = sc.next.SignProposal(chainID, proposal)
  64. if err == nil {
  65. return nil
  66. }
  67. time.Sleep(sc.timeout)
  68. }
  69. return fmt.Errorf("exhausted all attempts to sign proposal: %w", err)
  70. }