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.

97 lines
2.5 KiB

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