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.

80 lines
2.0 KiB

  1. package light
  2. import (
  3. "context"
  4. "time"
  5. "github.com/tendermint/tendermint/light/provider"
  6. "github.com/tendermint/tendermint/light/provider/http"
  7. "github.com/tendermint/tendermint/light/store"
  8. )
  9. // NewHTTPClient initiates an instance of a light client using HTTP addresses
  10. // for both the primary provider and witnesses of the light client. A trusted
  11. // header and hash must be passed to initialize the client.
  12. //
  13. // See all Option(s) for the additional configuration.
  14. // See NewClient.
  15. func NewHTTPClient(
  16. ctx context.Context,
  17. chainID string,
  18. trustOptions TrustOptions,
  19. primaryAddress string,
  20. witnessesAddresses []string,
  21. trustedStore store.Store,
  22. options ...Option) (*Client, error) {
  23. providers, err := providersFromAddresses(append(witnessesAddresses, primaryAddress), chainID)
  24. if err != nil {
  25. return nil, err
  26. }
  27. return NewClient(
  28. ctx,
  29. chainID,
  30. trustOptions,
  31. providers[len(providers)-1],
  32. providers[:len(providers)-1],
  33. trustedStore,
  34. options...)
  35. }
  36. // NewHTTPClientFromTrustedStore initiates an instance of a light client using
  37. // HTTP addresses for both the primary provider and witnesses and uses a
  38. // trusted store as the root of trust.
  39. //
  40. // See all Option(s) for the additional configuration.
  41. // See NewClientFromTrustedStore.
  42. func NewHTTPClientFromTrustedStore(
  43. chainID string,
  44. trustingPeriod time.Duration,
  45. primaryAddress string,
  46. witnessesAddresses []string,
  47. trustedStore store.Store,
  48. options ...Option) (*Client, error) {
  49. providers, err := providersFromAddresses(append(witnessesAddresses, primaryAddress), chainID)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return NewClientFromTrustedStore(
  54. chainID,
  55. trustingPeriod,
  56. providers[len(providers)-1],
  57. providers[:len(providers)-1],
  58. trustedStore,
  59. options...)
  60. }
  61. func providersFromAddresses(addrs []string, chainID string) ([]provider.Provider, error) {
  62. providers := make([]provider.Provider, len(addrs))
  63. for idx, address := range addrs {
  64. p, err := http.New(chainID, address)
  65. if err != nil {
  66. return nil, err
  67. }
  68. providers[idx] = p
  69. }
  70. return providers, nil
  71. }