github.com/vipernet-xyz/tm@v0.34.24/light/setup.go (about)

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