Skip to content

Commit

Permalink
External feegrants (#1338)
Browse files Browse the repository at this point in the history
* Allow feegrants where granter is external (relayer does not have key)

* linting/logging

* Added feegrant command gas flag

* Added doc for external feegrant config

* Fix logging to conform to style guide

* Lock on read bech32 address map

* Lock on read bech32 address map

* Lock on read bech32 address map

---------

Co-authored-by: Kyle <[email protected]>
  • Loading branch information
KyleMoser and Kyle authored Jan 29, 2024
1 parent 5ab55c0 commit 7cb083c
Show file tree
Hide file tree
Showing 11 changed files with 779 additions and 163 deletions.
71 changes: 43 additions & 28 deletions cmd/feegrant.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"errors"
"fmt"

sdkflags "github.com/cosmos/cosmos-sdk/client/flags"

"github.com/cosmos/relayer/v2/relayer/chains/cosmos"
"github.com/spf13/cobra"
"go.uber.org/zap"
)

// feegrantConfigureCmd returns the fee grant configuration commands for this module
Expand Down Expand Up @@ -52,18 +55,27 @@ func feegrantConfigureBasicCmd(a *appState) *cobra.Command {
if len(args) > 1 {
granterKeyOrAddr = args[1]
} else if prov.PCfg.FeeGrants != nil {
granterKeyOrAddr = prov.PCfg.FeeGrants.GranterKey
granterKeyOrAddr = prov.PCfg.FeeGrants.GranterKeyOrAddr
} else {
granterKeyOrAddr = prov.PCfg.Key
}

externalGranter := false

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

if externalGranter {
_, err := prov.DecodeBech32AccAddr(granterKeyOrAddr)
if err != nil {
return fmt.Errorf("an unknown granter was specified: '%s' is not a valid bech32 address", granterKeyOrAddr)
}
}

if delete {
fmt.Printf("Deleting %s feegrant configuration\n", chain)
a.log.Info("Deleting feegrant configuration", zap.String("chain", chain))

cfgErr := a.performConfigLockingOperation(cmd.Context(), func() error {
chain := a.config.Chains[chain]
Expand All @@ -75,11 +87,12 @@ func feegrantConfigureBasicCmd(a *appState) *cobra.Command {
return nil
}

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 {
if prov.PCfg.FeeGrants != nil && granterKey != prov.PCfg.FeeGrants.GranterKeyOrAddr && !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.GranterKeyOrAddr)
} else if prov.PCfg.FeeGrants != nil && granterKey != prov.PCfg.FeeGrants.GranterKeyOrAddr && update {
cfgErr := a.performConfigLockingOperation(cmd.Context(), func() error {
prov.PCfg.FeeGrants.GranterKey = granterKey
prov.PCfg.FeeGrants.GranterKeyOrAddr = granterKey
prov.PCfg.FeeGrants.IsExternalGranter = externalGranter
return nil
})
cobra.CheckErr(cfgErr)
Expand All @@ -88,11 +101,16 @@ func feegrantConfigureBasicCmd(a *appState) *cobra.Command {
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"
// No list of grantees was provided, so we will use the default naming convention "grantee1, ... granteeN"
if grantees == nil {
if externalGranter {
return fmt.Errorf("external granter %s was specified, pre-authorized grantees must also be specified", granterKeyOrAddr)
}
feegrantErr = prov.ConfigureFeegrants(numGrantees, granterKey)
} else {
} else if !externalGranter {
feegrantErr = prov.ConfigureWithGrantees(grantees, granterKey)
} else {
feegrantErr = prov.ConfigureWithExternalGranter(grantees, granterKeyOrAddr)
}

if feegrantErr != nil {
Expand All @@ -102,6 +120,7 @@ func feegrantConfigureBasicCmd(a *appState) *cobra.Command {
cfgErr := a.performConfigLockingOperation(cmd.Context(), func() error {
chain := a.config.Chains[chain]
oldProv := chain.ChainProvider.(*cosmos.CosmosProvider)
prov.PCfg.FeeGrants.IsExternalGranter = externalGranter
oldProv.PCfg.FeeGrants = prov.PCfg.FeeGrants
return nil
})
Expand All @@ -113,25 +132,32 @@ func feegrantConfigureBasicCmd(a *appState) *cobra.Command {
return err
}

gas := uint64(0)
gasStr, _ := cmd.Flags().GetString(sdkflags.FlagGas)
if gasStr != "" {
gasSetting, _ := sdkflags.ParseGasSetting(gasStr)
gas = gasSetting.Gas
}

ctx := cmd.Context()
_, err = prov.EnsureBasicGrants(ctx, memo)
_, err = prov.EnsureBasicGrants(ctx, memo, gas)
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.
// 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)
prov.PCfg.FeeGrants.IsExternalGranter = externalGranter
oldProv.PCfg.FeeGrants = prov.PCfg.FeeGrants
oldProv.PCfg.FeeGrants.BlockHeightVerified = h
fmt.Printf("Feegrant chain height marked: %d\n", h)
a.log.Info("feegrant configured", zap.Int64("height", h))
return nil
})
cobra.CheckErr(cfgErr)
Expand All @@ -147,6 +173,7 @@ func feegrantConfigureBasicCmd(a *appState) *cobra.Command {
cmd.Flags().IntVar(&numGrantees, "num-grantees", 10, "number of grantees that will be feegranted with basic allowances")
cmd.Flags().StringSliceVar(&grantees, "grantees", nil, "comma separated list of grantee key names (keys are created if they do not exist)")
cmd.MarkFlagsMutuallyExclusive("num-grantees", "grantees", "delete")
cmd.Flags().String(sdkflags.FlagGas, "", fmt.Sprintf("gas limit to set per-transaction; set to %q to calculate sufficient gas automatically (default %d)", sdkflags.GasFlagAuto, sdkflags.DefaultGasLimit))

memoFlag(a.viper, cmd)
return cmd
Expand All @@ -169,18 +196,6 @@ func feegrantBasicGrantsCmd(a *appState) *cobra.Command {
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
Expand All @@ -190,7 +205,7 @@ func feegrantBasicGrantsCmd(a *appState) *cobra.Command {

granterAcc, err := prov.AccountFromKeyOrAddress(keyNameOrAddress)
if err != nil {
fmt.Printf("Error retrieving account from key '%s'\n", keyNameOrAddress)
a.log.Error("Unknown account", zap.String("key_or_address", keyNameOrAddress), zap.Error(err))
return err
}
granterAddr := prov.MustEncodeAccAddr(granterAcc)
Expand All @@ -203,7 +218,7 @@ func feegrantBasicGrantsCmd(a *appState) *cobra.Command {
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)
a.log.Info("feegrant", zap.String("granter", grant.Granter), zap.String("grantee", grant.Grantee), zap.String("allowance", allowance))
}

return nil
Expand Down
5 changes: 4 additions & 1 deletion docs/advanced_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ For example, configure feegrants for Kujira:
- Note: above, `default` is the key that will need to contain funds (the granter)
- 10 grantees will be configured, so those 10 address will sign TXs in round robin order.

An external feegrant configuration can be applied with the following command:
- `rly chains configure feegrant basicallowance cosmoshub cosmosaddr --grantees grantee3`
- Note: above, `cosmosaddr` is a bech32 address that has already issued a feegrant allowance to `grantee3`.
- External configuration means that someone else controls `cosmosaddr` (you do not need the mnemonic).

You may also choose to specify the exact names of your grantees:
- `rly chains configure feegrant basicallowance kujira default --grantees "kuji1,kuji2,kuji3"`
Expand All @@ -78,7 +82,6 @@ Rerunning the feegrant command will simply confirm your configuration is correct
To remove the feegrant configuration:
- `rly chains configure feegrant basicallowance kujira --delete`


## Stuck Packet

There can be scenarios where a standard flush fails to clear a packet due to differences in the way packets are observed. The standard flush depends on the packet queries working properly. Sometimes the packet queries can miss things that the block scanning performed by the relayer during standard operation wouldn't. For packets affected by this, if they were emitted in recent blocks, the `--block-history` flag can be used to have the standard relayer block scanning start at a block height that many blocks behind the current chain tip. However, if the stuck packet occurred at an old height, farther back than would be reasonable for the `--block-history` scan from historical to current, there is an additional set of flags that can be used to zoom in on the block heights where the stuck packet occurred.
Expand Down
Loading

0 comments on commit 7cb083c

Please sign in to comment.