Files
tendermint/lite2/setup.go
Callum Waters ff786515b8 lite: add helper functions for initiating the light client (#4486)
* add new net client

* hijack example test

* lint fixes

* reorganised file structure

* renamed funcs and added documentation

* implemented suggested changes

* restored example tests

* edited comments

* Update lite2/setup.go

Co-Authored-By: Bot from GolangCI <42910462+golangcibot@users.noreply.github.com>

* Update lite2/client.go

* lite cmds use HTTP client

* better naming

* Delete go.sum

* Delete go.mod

* restore go mod

* restore go.sum

* fix double import

Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
Co-authored-by: Bot from GolangCI <42910462+golangcibot@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2020-03-03 12:00:06 +00:00

78 lines
2.0 KiB
Go

package lite
import (
"time"
"github.com/tendermint/tendermint/lite2/provider"
"github.com/tendermint/tendermint/lite2/provider/http"
"github.com/tendermint/tendermint/lite2/store"
)
// NewHTTPClient initiates an instance of a lite client using HTTP addresses
// for both the primary provider and witnesses of the lite client. A trusted
// header and hash must be passed to initialize the client.
//
// See all Option(s) for the additional configuration.
// See NewClient.
func NewHTTPClient(
chainID string,
trustOptions TrustOptions,
primaryAddress string,
witnessesAddresses []string,
trustedStore store.Store,
options ...Option) (*Client, error) {
providers, err := providersFromAddresses(append(witnessesAddresses, primaryAddress), chainID)
if err != nil {
return nil, err
}
return NewClient(
chainID,
trustOptions,
providers[len(providers)-1],
providers[:len(providers)-1],
trustedStore,
options...)
}
// NewHTTPClientFromTrustedStore initiates an instance of a lite client using
// HTTP addresses for both the primary provider and witnesses and uses a
// trusted store as the root of trust.
//
// See all Option(s) for the additional configuration.
// See NewClientFromTrustedStore.
func NewHTTPClientFromTrustedStore(
chainID string,
trustingPeriod time.Duration,
primaryAddress string,
witnessesAddresses []string,
trustedStore store.Store,
options ...Option) (*Client, error) {
providers, err := providersFromAddresses(append(witnessesAddresses, primaryAddress), chainID)
if err != nil {
return nil, err
}
return NewClientFromTrustedStore(
chainID,
trustingPeriod,
providers[len(providers)-1],
providers[:len(providers)-1],
trustedStore,
options...)
}
func providersFromAddresses(addrs []string, chainID string) ([]provider.Provider, error) {
providers := make([]provider.Provider, len(addrs))
for idx, address := range addrs {
p, err := http.New(chainID, address)
if err != nil {
return nil, err
}
providers[idx] = p
}
return providers, nil
}