chore: x/feegrant audit changes (#16652)

This commit is contained in:
atheeshp 2023-06-23 13:18:51 +05:30 committed by GitHub
parent 3193b3db35
commit d0b42dc829
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 35 additions and 18 deletions

View File

@ -12,12 +12,12 @@ option go_package = "cosmossdk.io/x/feegrant";
// Query defines the gRPC querier service.
service Query {
// Allowance returns fee granted to the grantee by the granter.
// Allowance returns granted allwance to the grantee by the granter.
rpc Allowance(QueryAllowanceRequest) returns (QueryAllowanceResponse) {
option (google.api.http).get = "/cosmos/feegrant/v1beta1/allowance/{granter}/{grantee}";
}
// Allowances returns all the grants for address.
// Allowances returns all the grants for the given grantee address.
rpc Allowances(QueryAllowancesRequest) returns (QueryAllowancesResponse) {
option (google.api.http).get = "/cosmos/feegrant/v1beta1/allowances/{grantee}";
}

View File

@ -58,6 +58,7 @@ func (a BasicAllowance) ValidateBasic() error {
return nil
}
// ExpiresAt returns the expiry time of the BasicAllowance.
func (a BasicAllowance) ExpiresAt() (*time.Time, error) {
return a.Expiration, nil
}

View File

@ -26,11 +26,11 @@ const (
FlagAllowedMsgs = "allowed-messages"
)
// GetTxCmd returns the transaction commands for this module
// GetTxCmd returns the transaction commands for feegrant module
func GetTxCmd(ac address.Codec) *cobra.Command {
feegrantTxCmd := &cobra.Command{
Use: feegrant.ModuleName,
Short: "Feegrant transactions subcommands",
Short: "Feegrant transactions sub-commands",
Long: "Grant and revoke fee allowance for a grantee by a granter",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
@ -45,14 +45,14 @@ func GetTxCmd(ac address.Codec) *cobra.Command {
return feegrantTxCmd
}
// NewCmdFeeGrant returns a CLI command handler for creating a MsgGrantAllowance transaction.
// NewCmdFeeGrant returns a CLI command handler to create a MsgGrantAllowance transaction.
func NewCmdFeeGrant(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "grant [granter_key_or_address] [grantee]",
Short: "Grant Fee allowance to an address",
Long: strings.TrimSpace(
fmt.Sprintf(
`Grant authorization to pay fees from your address. Note, the'--from' flag is
`Grant authorization to pay fees from your address. Note, the '--from' flag is
ignored as it is implied from [granter].
Examples:
@ -65,7 +65,10 @@ Examples:
),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.Flags().Set(flags.FlagFrom, args[0])
if err := cmd.Flags().Set(flags.FlagFrom, args[0]); err != nil {
return err
}
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
@ -82,7 +85,8 @@ Examples:
return err
}
// if `FlagSpendLimit` isn't set, limit will be nil
// if `FlagSpendLimit` isn't set, limit will be nil.
// Hence, there won't be any spendlimit for the grantee.
limit, err := sdk.ParseCoinsNormalized(sl)
if err != nil {
return err
@ -119,7 +123,8 @@ Examples:
return err
}
// Check any of period or periodLimit flags set, If set consider it as periodic fee allowance.
// check any of period or periodLimit flags are set,
// if set consider it as periodic fee allowance.
if periodClock > 0 || periodLimitVal != "" {
periodLimit, err := sdk.ParseCoinsNormalized(periodLimitVal)
if err != nil {
@ -181,13 +186,13 @@ Examples:
return cmd
}
// NewCmdRevokeFeegrant returns a CLI command handler for creating a MsgRevokeAllowance transaction.
// NewCmdRevokeFeegrant returns a CLI command handler to create a MsgRevokeAllowance transaction.
func NewCmdRevokeFeegrant(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "revoke [granter] [grantee]",
Short: "revoke fee-grant",
Long: strings.TrimSpace(
fmt.Sprintf(`revoke fee grant from a granter to a grantee. Note, the'--from' flag is
fmt.Sprintf(`revoke fee grant from a granter to a grantee. Note, the '--from' flag is
ignored as it is implied from [granter].
Example:

View File

@ -8,7 +8,7 @@ import (
)
// FeeAllowance implementations are tied to a given fee delegator and delegatee,
// and are used to enforce fee grant limits.
// and are used to enforce feegrant limits.
type FeeAllowanceI interface {
// Accept can use fee payment requested as well as timestamp of the current block
// to determine whether or not to process this. This is checked in

View File

@ -128,6 +128,7 @@ func (a *AllowedMsgAllowance) ValidateBasic() error {
return allowance.ValidateBasic()
}
// ExpiresAt returns the expiry time of the AllowedMsgAllowance.
func (a *AllowedMsgAllowance) ExpiresAt() (*time.Time, error) {
allowance, err := a.GetAllowance()
if err != nil {

View File

@ -19,7 +19,7 @@ import (
var _ feegrant.QueryServer = Keeper{}
// Allowance returns fee granted to the grantee by the granter.
// Allowance returns granted allowance to the grantee by the granter.
func (q Keeper) Allowance(c context.Context, req *feegrant.QueryAllowanceRequest) (*feegrant.QueryAllowanceResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")

View File

@ -28,7 +28,7 @@ type Keeper struct {
var _ ante.FeegrantKeeper = &Keeper{}
// NewKeeper creates a fee grant Keeper
// NewKeeper creates a feegrant Keeper
func NewKeeper(cdc codec.BinaryCodec, storeService store.KVStoreService, ak feegrant.AccountKeeper) Keeper {
return Keeper{
cdc: cdc,
@ -144,7 +144,7 @@ func (k Keeper) UpdateAllowance(ctx context.Context, granter, grantee sdk.AccAdd
// revokeAllowance removes an existing grant
func (k Keeper) revokeAllowance(ctx context.Context, granter, grantee sdk.AccAddress) error {
_, err := k.getGrant(ctx, granter, grantee)
grant, err := k.GetAllowance(ctx, granter, grantee)
if err != nil {
return err
}
@ -156,6 +156,17 @@ func (k Keeper) revokeAllowance(ctx context.Context, granter, grantee sdk.AccAdd
return err
}
exp, err := grant.ExpiresAt()
if err != nil {
return err
}
if exp != nil {
if err := store.Delete(feegrant.FeeAllowancePrefixQueue(exp, feegrant.FeeAllowanceKey(grantee, granter)[1:])); err != nil {
return err
}
}
sdk.UnwrapSDKContext(ctx).EventManager().EmitEvent(
sdk.NewEvent(
feegrant.EventTypeRevokeFeeGrant,

View File

@ -57,8 +57,6 @@ func (msg MsgGrantAllowance) UnpackInterfaces(unpacker types.AnyUnpacker) error
// NewMsgRevokeAllowance returns a message to revoke a fee allowance for a given
// granter and grantee
//
func NewMsgRevokeAllowance(granter, grantee sdk.AccAddress) MsgRevokeAllowance {
return MsgRevokeAllowance{Granter: granter.String(), Grantee: grantee.String()}
}

View File

@ -91,7 +91,7 @@ func (a PeriodicAllowance) ValidateBasic() error {
if !a.PeriodCanSpend.IsValid() {
return errorsmod.Wrapf(sdkerrors.ErrInvalidCoins, "can spend amount is invalid: %s", a.PeriodCanSpend)
}
// We allow 0 for CanSpend
// We allow 0 for `PeriodCanSpend`
if a.PeriodCanSpend.IsAnyNegative() {
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, "can spend must not be negative")
}
@ -109,6 +109,7 @@ func (a PeriodicAllowance) ValidateBasic() error {
return nil
}
// ExpiresAt returns the expiry time of the PeriodicAllowance.
func (a PeriodicAllowance) ExpiresAt() (*time.Time, error) {
return a.Basic.ExpiresAt()
}