Skip to content

Commit

Permalink
Merge branch 'main' into dan/client-tp-metric
Browse files Browse the repository at this point in the history
  • Loading branch information
boojamya committed Jul 27, 2023
2 parents 041e64e + cdd7661 commit e015b7f
Show file tree
Hide file tree
Showing 32 changed files with 2,853 additions and 215 deletions.
19 changes: 17 additions & 2 deletions cmd/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func chainsCmd(a *appState) *cobra.Command {
chainsShowCmd(a),
chainsAddrCmd(a),
chainsAddDirCmd(a),
cmdChainsConfigure(a),
)

return cmd
Expand Down Expand Up @@ -144,6 +145,19 @@ $ %s ch d ibc-0`, appName, appName)),
return cmd
}

func cmdChainsConfigure(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "configure",
Short: "manage local chain configurations",
}

cmd.AddCommand(
feegrantConfigureBaseCmd(a),
)

return cmd
}

func chainsRegistryList(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "registry-list",
Expand Down Expand Up @@ -277,9 +291,10 @@ func chainsAddCmd(a *appState) *cobra.Command {
" the chain-registry or passing a file (-f) or url (-u)",
Args: withUsage(cobra.MinimumNArgs(0)),
Example: fmt.Sprintf(` $ %s chains add cosmoshub
$ %s chains add testnets/cosmoshubtestnet
$ %s chains add cosmoshub osmosis
$ %s chains add --file chains/ibc0.json ibc0
$ %s chains add --url https://relayer.com/ibc0.json ibc0`, appName, appName, appName, appName),
$ %s chains add --url https://relayer.com/ibc0.json ibc0`, appName, appName, appName, appName, appName),
RunE: func(cmd *cobra.Command, args []string) error {
file, url, err := getAddInputs(cmd)
if err != nil {
Expand Down Expand Up @@ -447,7 +462,7 @@ func addChainsFromRegistry(ctx context.Context, a *appState, chains []string) er
continue
}

chainConfig, err := chainInfo.GetChainConfig(ctx)
chainConfig, err := chainInfo.GetChainConfig(ctx, chain)
if err != nil {
a.log.Warn(
"Error generating chain config",
Expand Down
197 changes: 197 additions & 0 deletions cmd/feegrant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
package cmd

import (
"errors"
"fmt"

"github.com/cosmos/relayer/v2/relayer/chains/cosmos"
"github.com/spf13/cobra"
)

// feegrantConfigureCmd returns the fee grant configuration commands for this module
func feegrantConfigureBaseCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "feegrant",
Short: "Configure the client to use round-robin feegranted accounts when sending TXs",
Long: "Use round-robin feegranted accounts when sending TXs. Useful for relayers and applications where sequencing is important",
}

cmd.AddCommand(
feegrantConfigureBasicCmd(a),
)

return cmd
}

func feegrantConfigureBasicCmd(a *appState) *cobra.Command {
var numGrantees int
var update bool
var updateGrantees bool
var grantees []string

cmd := &cobra.Command{
Use: "basicallowance [chain-name] [granter] --num-grantees [int] --overwrite-granter --overwrite-grantees",
Short: "feegrants for the given chain and granter (if granter is unspecified, use the default key)",
Long: "feegrants for the given chain. 10 grantees by default, all with an unrestricted BasicAllowance.",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
chain := args[0]
cosmosChain, ok := a.config.Chains[chain]
if !ok {
return errChainNotFound(args[0])
}

prov, ok := cosmosChain.ChainProvider.(*cosmos.CosmosProvider)
if !ok {
return errors.New("only CosmosProvider can be feegranted")
}

granterKeyOrAddr := ""

if len(args) > 1 {
granterKeyOrAddr = args[1]
} else if prov.PCfg.FeeGrants != nil {
granterKeyOrAddr = prov.PCfg.FeeGrants.GranterKey
} else {
granterKeyOrAddr = prov.PCfg.Key
}

granterKey, err := prov.KeyFromKeyOrAddress(granterKeyOrAddr)
if err != nil {
return fmt.Errorf("could not get granter key from '%s'", granterKeyOrAddr)
}

if prov.PCfg.FeeGrants != nil && granterKey != prov.PCfg.FeeGrants.GranterKey && !update {
return fmt.Errorf("you specified granter '%s' which is different than configured feegranter '%s', but you did not specify the --overwrite-granter flag", granterKeyOrAddr, prov.PCfg.FeeGrants.GranterKey)
} else if prov.PCfg.FeeGrants != nil && granterKey != prov.PCfg.FeeGrants.GranterKey && update {
cfgErr := a.performConfigLockingOperation(cmd.Context(), func() error {
prov.PCfg.FeeGrants.GranterKey = granterKey
return nil
})
cobra.CheckErr(cfgErr)
}

if prov.PCfg.FeeGrants == nil || updateGrantees || len(grantees) > 0 {
var feegrantErr error

//No list of grantees was provided, so we will use the default naming convention "grantee1, ... granteeN"
if grantees == nil {
feegrantErr = prov.ConfigureFeegrants(numGrantees, granterKey)
} else {
feegrantErr = prov.ConfigureWithGrantees(grantees, granterKey)
}

if feegrantErr != nil {
return feegrantErr
}

cfgErr := a.performConfigLockingOperation(cmd.Context(), func() error {
chain := a.config.Chains[chain]
oldProv := chain.ChainProvider.(*cosmos.CosmosProvider)
oldProv.PCfg.FeeGrants = prov.PCfg.FeeGrants
return nil
})
cobra.CheckErr(cfgErr)
}

memo, err := cmd.Flags().GetString(flagMemo)
if err != nil {
return err
}

ctx := cmd.Context()
_, err = prov.EnsureBasicGrants(ctx, memo)
if err != nil {
return fmt.Errorf("error writing grants on chain: '%s'", err.Error())
}

//Get latest height from the chain, mark feegrant configuration as verified up to that height.
//This means we've verified feegranting is enabled on-chain and TXs can be sent with a feegranter.
if prov.PCfg.FeeGrants != nil {
fmt.Printf("Querying latest chain height to mark FeeGrant height... \n")
h, err := prov.QueryLatestHeight(ctx)
cobra.CheckErr(err)

cfgErr := a.performConfigLockingOperation(cmd.Context(), func() error {
chain := a.config.Chains[chain]
oldProv := chain.ChainProvider.(*cosmos.CosmosProvider)
oldProv.PCfg.FeeGrants = prov.PCfg.FeeGrants
oldProv.PCfg.FeeGrants.BlockHeightVerified = h
fmt.Printf("Feegrant chain height marked: %d\n", h)
return nil
})
cobra.CheckErr(cfgErr)
}

return nil
},
}
cmd.Flags().BoolVar(&update, "overwrite-granter", false, "allow overwriting the existing granter")
cmd.Flags().BoolVar(&updateGrantees, "overwrite-grantees", false, "allow overwriting existing grantees")
cmd.Flags().IntVar(&numGrantees, "num-grantees", 10, "number of grantees that will be feegranted with basic allowances")
cmd.Flags().StringSliceVar(&grantees, "grantees", []string{}, "comma separated list of grantee key names (keys are created if they do not exist)")
cmd.MarkFlagsMutuallyExclusive("num-grantees", "grantees")

memoFlag(a.viper, cmd)
return cmd
}

func feegrantBasicGrantsCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "basic chain-name [granter]",
Short: "query the grants for an account (if none is specified, the default account is returned)",
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
chain := args[0]
cosmosChain, ok := a.config.Chains[chain]
if !ok {
return errChainNotFound(args[0])
}

prov, ok := cosmosChain.ChainProvider.(*cosmos.CosmosProvider)
if !ok {
return errors.New("only CosmosProvider can be feegranted")
}

// TODO fix pagination
// pageReq, err := client.ReadPageRequest(cmd.Flags())
// if err != nil {
// return err
// }

//TODO fix height
// height, err := lensCmd.ReadHeight(cmd.Flags())
// if err != nil {
// return err
// }

keyNameOrAddress := ""
if len(args) == 0 {
keyNameOrAddress = prov.PCfg.Key
} else {
keyNameOrAddress = args[0]
}

granterAcc, err := prov.AccountFromKeyOrAddress(keyNameOrAddress)
if err != nil {
fmt.Printf("Error retrieving account from key '%s'\n", keyNameOrAddress)
return err
}
granterAddr := prov.MustEncodeAccAddr(granterAcc)

res, err := prov.QueryFeegrantsByGranter(granterAddr, nil)
if err != nil {
return err
}

for _, grant := range res {
allowance, e := prov.Sprint(grant.Allowance)
cobra.CheckErr(e)
fmt.Printf("Granter: %s, Grantee: %s, Allowance: %s\n", grant.Granter, grant.Grantee, allowance)
}

return nil
},
}
return paginationFlags(a.viper, cmd, "feegrant")
}
15 changes: 15 additions & 0 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ func queryCmd(a *appState) *cobra.Command {
lineBreakCommand(),
queryIBCDenoms(a),
queryBaseDenomFromIBCDenom(a),
feegrantQueryCmd(a),
)

return cmd
}

// feegrantQueryCmd returns the fee grant query commands for this module
func feegrantQueryCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "feegrant",
Short: "Querying commands for the feegrant module [currently BasicAllowance only]",
}

cmd.AddCommand(
feegrantBasicGrantsCmd(a),
)

return cmd
Expand Down
8 changes: 4 additions & 4 deletions cregistry/chain_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ func (c ChainInfo) GetRandomRPCEndpoint(ctx context.Context) (string, error) {
}

// GetAssetList returns the asset metadata from the cosmos chain registry for this particular chain.
func (c ChainInfo) GetAssetList(ctx context.Context) (AssetList, error) {
chainRegURL := fmt.Sprintf("https://raw.githubusercontent.com/cosmos/chain-registry/master/%s/assetlist.json", c.ChainName)
func (c ChainInfo) GetAssetList(ctx context.Context, name string) (AssetList, error) {
chainRegURL := fmt.Sprintf("https://raw.githubusercontent.com/cosmos/chain-registry/master/%s/assetlist.json", name)

res, err := http.Get(chainRegURL)
if err != nil {
Expand Down Expand Up @@ -236,11 +236,11 @@ func (c ChainInfo) GetAssetList(ctx context.Context) (AssetList, error) {

// GetChainConfig returns a CosmosProviderConfig composed from the details found in the cosmos chain registry for
// this particular chain.
func (c ChainInfo) GetChainConfig(ctx context.Context) (*cosmos.CosmosProviderConfig, error) {
func (c ChainInfo) GetChainConfig(ctx context.Context, name string) (*cosmos.CosmosProviderConfig, error) {
debug := viper.GetBool("debug")
home := viper.GetString("home")

assetList, err := c.GetAssetList(ctx)
assetList, err := c.GetAssetList(ctx, name)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit e015b7f

Please sign in to comment.