Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Merge PR #48: Implement the provider interface from the go-relayer
Browse files Browse the repository at this point in the history
* fix go.mod

* implement KeyProvider

* implement QueryProvider

* WIP: implementing ChainProvider

* add relayer packets needed for implementing provider

* finish implementing provider interface

* add trusting period to config

* small tweak

* omitempty on trusting-period
  • Loading branch information
jtieri authored Jan 6, 2022
1 parent 2b13131 commit 65fa5fc
Show file tree
Hide file tree
Showing 11 changed files with 2,771 additions and 52 deletions.
53 changes: 30 additions & 23 deletions client/chain_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,44 +34,51 @@ type ChainClient struct {
}

func NewChainClient(ccc *ChainClientConfig, input io.Reader, output io.Writer, kro ...keyring.Option) (*ChainClient, error) {
if err := ccc.Validate(); err != nil {
return nil, err
}

// TODO: test key directory and return error if not created
keybase, err := keyring.New(ccc.ChainID, ccc.KeyringBackend, ccc.KeyDirectory, input, kro...)
if err != nil {
return nil, err
}

// TODO: figure out how to deal with input or maybe just make all keyring backends test?

timeout, _ := time.ParseDuration(ccc.Timeout)
rpcClient, err := NewRPCClient(ccc.RPCAddr, timeout)
if err != nil {
return nil, err
}

lightprovider, err := prov.New(ccc.ChainID, ccc.RPCAddr)
if err != nil {
return nil, err
}

return &ChainClient{
cc := &ChainClient{
Keybase: keybase,
RPCClient: rpcClient,
LightProvider: lightprovider,
KeyringOptions: kro,
Config: ccc,
Input: input,
Output: output,
Codec: MakeCodec(ccc.Modules),
Logger: log.NewTMLogger(log.NewSyncWriter(output)),
}, nil
}
if err := cc.Init(); err != nil {
return nil, err
}
return cc, nil
}

func (cc *ChainClient) Init() error {
timeout, _ := time.ParseDuration(cc.Config.Timeout)
rpcClient, err := NewRPCClient(cc.Config.RPCAddr, timeout)
if err != nil {
return err
}

lightprovider, err := prov.New(cc.Config.ChainID, cc.Config.RPCAddr)
if err != nil {
return err
}

cc.RPCClient = rpcClient
cc.LightProvider = lightprovider

return nil
}

// Log takes a string and logs the data
func (cc *ChainClient) Log(s string) {
cc.Logger.Info(s)
}

// TODO: actually do something different here have a couple of levels of
// verbosity
// TODO: actually do something different here have a couple of levels of verbosity
func (cc *ChainClient) PrintTxResponse(res *sdk.TxResponse) error {
return cc.PrintObject(res)
}
Expand Down
28 changes: 24 additions & 4 deletions client/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package client

import (
"github.com/cosmos/relayer/v2/relayer/provider"
"os"
"time"

"github.com/cosmos/cosmos-sdk/types/module"
)

var (
_ provider.ProviderConfig = &ChainClientConfig{}
)

type ChainClientConfig struct {
Key string `json:"key" yaml:"key"`
ChainID string `json:"chain-id" yaml:"chain-id"`
Expand All @@ -15,6 +21,7 @@ type ChainClientConfig struct {
KeyringBackend string `json:"keyring-backend" yaml:"keyring-backend"`
GasAdjustment float64 `json:"gas-adjustment" yaml:"gas-adjustment"`
GasPrices string `json:"gas-prices" yaml:"gas-prices"`
TrustingPeriod string `json:"omitempty" yaml:"omitempty"`
KeyDirectory string `json:"key-directory" yaml:"key-directory"`
Debug bool `json:"debug" yaml:"debug"`
Timeout string `json:"timeout" yaml:"timeout"`
Expand All @@ -23,14 +30,25 @@ type ChainClientConfig struct {
Modules []module.AppModuleBasic `json:"-" yaml:"-"`
}

func (ccc *ChainClientConfig) NewProvider(homepath string, debug bool) (provider.ChainProvider, error) {
if err := ccc.Validate(); err != nil {
return nil, err
}
p, err := NewChainClient(ccc, os.Stdin, os.Stdout)
if err != nil {
return nil, err
}
return p, err
}

func (ccc *ChainClientConfig) Validate() error {
if _, err := time.ParseDuration(ccc.Timeout); err != nil {
return err
}
return nil
}

func GetCosmosHubConfig(keyHome string, debug bool) (*ChainClientConfig) {
func GetCosmosHubConfig(keyHome string, debug bool) *ChainClientConfig {
return &ChainClientConfig{
Key: "default",
ChainID: "cosmoshub-4",
Expand All @@ -40,6 +58,7 @@ func GetCosmosHubConfig(keyHome string, debug bool) (*ChainClientConfig) {
KeyringBackend: "test",
GasAdjustment: 1.2,
GasPrices: "0.01uatom",
TrustingPeriod: "336h",
KeyDirectory: keyHome,
Debug: debug,
Timeout: "20s",
Expand All @@ -48,7 +67,7 @@ func GetCosmosHubConfig(keyHome string, debug bool) (*ChainClientConfig) {
}
}

func GetOsmosisConfig(keyHome string, debug bool) (*ChainClientConfig) {
func GetOsmosisConfig(keyHome string, debug bool) *ChainClientConfig {
return &ChainClientConfig{
Key: "default",
ChainID: "osmosis-1",
Expand All @@ -58,6 +77,7 @@ func GetOsmosisConfig(keyHome string, debug bool) (*ChainClientConfig) {
KeyringBackend: "test",
GasAdjustment: 1.2,
GasPrices: "0.01uosmo",
TrustingPeriod: "300h",
KeyDirectory: keyHome,
Debug: debug,
Timeout: "20s",
Expand All @@ -66,7 +86,7 @@ func GetOsmosisConfig(keyHome string, debug bool) (*ChainClientConfig) {
}
}

func GetTestClient() (*ChainClient) {
func GetTestClient() *ChainClient {
cl, _ := NewChainClient(GetCosmosHubConfig("/tmp", true), nil, nil)
return cl
}
}
7 changes: 4 additions & 3 deletions client/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"errors"
"github.com/cosmos/relayer/v2/relayer/provider"
"os"

ckeys "github.com/cosmos/cosmos-sdk/client/keys"
Expand All @@ -28,7 +29,7 @@ func (cc *ChainClient) KeystoreCreated(path string) bool {
return true
}

func (cc *ChainClient) AddKey(name string) (output *KeyOutput, err error) {
func (cc *ChainClient) AddKey(name string) (output *provider.KeyOutput, err error) {
ko, err := cc.KeyAddOrRestore(name, 118)
if err != nil {
return nil, err
Expand Down Expand Up @@ -93,7 +94,7 @@ func (cc *ChainClient) ExportPrivKeyArmor(keyName string) (armor string, err err
return cc.Keybase.ExportPrivKeyArmor(keyName, ckeys.DefaultKeyPass)
}

func (cc *ChainClient) KeyAddOrRestore(keyName string, coinType uint32, mnemonic ...string) (*KeyOutput, error) {
func (cc *ChainClient) KeyAddOrRestore(keyName string, coinType uint32, mnemonic ...string) (*provider.KeyOutput, error) {
var mnemonicStr string
var err error

Expand All @@ -115,7 +116,7 @@ func (cc *ChainClient) KeyAddOrRestore(keyName string, coinType uint32, mnemonic
if err != nil {
return nil, err
}
return &KeyOutput{Mnemonic: mnemonicStr, Address: out}, nil
return &provider.KeyOutput{Mnemonic: mnemonicStr, Address: out}, nil
}

// KeyOutput contains mnemonic and address of key
Expand Down
Loading

0 comments on commit 65fa5fc

Please sign in to comment.