cosmos-sdk/x/feegrant/types/filtered_fee.go

105 lines
3.0 KiB
Go
Raw Normal View History

package types
import (
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
x/feegrant audit: clean up / add test coverage to types package (#9193) * Squashed commit of the following: commit 58dc50051226a9eeb8d0ebea5bb0908fe5b9637f Author: technicallyty <48813565+tytech3@users.noreply.github.com> Date: Fri Apr 23 15:09:27 2021 -0700 remove comments commit a84107e1b3eaa31324cb0f4f097b49f02af79c69 Author: technicallyty <48813565+tytech3@users.noreply.github.com> Date: Fri Apr 23 15:02:41 2021 -0700 add tests for msgs.go commit 2ad16869237e9631b402c93cde650c3fc554daf2 Author: technicallyty <48813565+tytech3@users.noreply.github.com> Date: Fri Apr 23 13:20:21 2021 -0700 specify test name commit b7121277c9be586a7c80d010ec401e50b510e02a Merge: c0c134d971 3c65c3dacd Author: technicallyty <48813565+tytech3@users.noreply.github.com> Date: Fri Apr 23 12:54:55 2021 -0700 Merge branch 'master' into ty-9115-types_tests commit c0c134d97107194dc4f9d3c501a15d023ae083e5 Author: technicallyty <48813565+tytech3@users.noreply.github.com> Date: Thu Apr 22 19:59:11 2021 -0700 -add test case to cli_test.go for filtered fee -clean up identifiers -remove redundant import alias from filtered_fee.go commit f7ab3699da39be3ab886f96962d28d23438d2e8e Author: technicallyty <48813565+tytech3@users.noreply.github.com> Date: Thu Apr 22 09:57:31 2021 -0700 remove unecessary constant commit 9db59a82a7337cf5a7a3569c6900a44a6c81e8b4 Merge: a3e75ceb8a e28271b8e6 Author: technicallyty <48813565+tytech3@users.noreply.github.com> Date: Thu Apr 22 09:19:20 2021 -0700 Merge branch 'master' into ty-9115-types_tests commit a3e75ceb8a510ad9db43dd96073c43b7a8b062b0 Merge: 4d3dafab85 bffcae54a1 Author: technicallyty <48813565+tytech3@users.noreply.github.com> Date: Wed Apr 21 12:04:39 2021 -0700 Merge branch 'master' into ty-9115-types_tests commit 4d3dafab85d85526a7c94b045289605289ee6b0d Author: technicallyty <48813565+tytech3@users.noreply.github.com> Date: Wed Apr 21 12:03:41 2021 -0700 cleanup id's / add test case commit e8f6924931ba95e592bfc3057ba167700458da41 Author: technicallyty <48813565+tytech3@users.noreply.github.com> Date: Wed Apr 21 10:55:29 2021 -0700 add test for 0 allowance / remove unused field on exp test commit 516b6ef89a4582ad681cc6c5c101cf50a9a54fb5 Author: technicallyty <48813565+tytech3@users.noreply.github.com> Date: Tue Apr 20 15:22:48 2021 -0700 make names more clear, remove unused field * fix imports * fix tests * rename test field Co-authored-by: technicallyty <48813565+tytech3@users.noreply.github.com> Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> Co-authored-by: MD Aleem <72057206+aleem1314@users.noreply.github.com>
2021-04-28 07:18:38 -07:00
"github.com/gogo/protobuf/proto"
)
// TODO: Revisit this once we have propoer gas fee framework.
// Tracking issues https://github.com/cosmos/cosmos-sdk/issues/9054, https://github.com/cosmos/cosmos-sdk/discussions/9072
const (
gasCostPerIteration = uint64(10)
)
var _ FeeAllowanceI = (*AllowedMsgAllowance)(nil)
var _ types.UnpackInterfacesMessage = (*AllowedMsgAllowance)(nil)
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (a *AllowedMsgAllowance) UnpackInterfaces(unpacker types.AnyUnpacker) error {
var allowance FeeAllowanceI
return unpacker.UnpackAny(a.Allowance, &allowance)
}
// NewAllowedMsgFeeAllowance creates new filtered fee allowance.
func NewAllowedMsgAllowance(allowance FeeAllowanceI, allowedMsgs []string) (*AllowedMsgAllowance, error) {
msg, ok := allowance.(proto.Message)
if !ok {
return nil, sdkerrors.Wrapf(sdkerrors.ErrPackAny, "cannot proto marshal %T", msg)
}
any, err := types.NewAnyWithValue(msg)
if err != nil {
return nil, err
}
return &AllowedMsgAllowance{
Allowance: any,
AllowedMessages: allowedMsgs,
}, nil
}
// GetAllowance returns allowed fee allowance.
func (a *AllowedMsgAllowance) GetAllowance() (FeeAllowanceI, error) {
allowance, ok := a.Allowance.GetCachedValue().(FeeAllowanceI)
if !ok {
return nil, sdkerrors.Wrap(ErrNoAllowance, "failed to get allowance")
}
return allowance, nil
}
// Accept method checks for the filtered messages has valid expiry
func (a *AllowedMsgAllowance) Accept(ctx sdk.Context, fee sdk.Coins, msgs []sdk.Msg) (bool, error) {
if !a.allMsgTypesAllowed(ctx, msgs) {
return false, sdkerrors.Wrap(ErrMessageNotAllowed, "message does not exist in allowed messages")
}
allowance, err := a.GetAllowance()
if err != nil {
return false, err
}
return allowance.Accept(ctx, fee, msgs)
}
func (a *AllowedMsgAllowance) allowedMsgsToMap(ctx sdk.Context) map[string]bool {
msgsMap := make(map[string]bool, len(a.AllowedMessages))
for _, msg := range a.AllowedMessages {
ctx.GasMeter().ConsumeGas(gasCostPerIteration, "check msg")
msgsMap[msg] = true
}
return msgsMap
}
func (a *AllowedMsgAllowance) allMsgTypesAllowed(ctx sdk.Context, msgs []sdk.Msg) bool {
msgsMap := a.allowedMsgsToMap(ctx)
for _, msg := range msgs {
ctx.GasMeter().ConsumeGas(gasCostPerIteration, "check msg")
if !msgsMap[sdk.MsgTypeURL(msg)] {
return false
}
}
return true
}
// ValidateBasic implements FeeAllowance and enforces basic sanity checks
func (a *AllowedMsgAllowance) ValidateBasic() error {
if a.Allowance == nil {
return sdkerrors.Wrap(ErrNoAllowance, "allowance should not be empty")
}
if len(a.AllowedMessages) == 0 {
return sdkerrors.Wrap(ErrNoMessages, "allowed messages shouldn't be empty")
}
allowance, err := a.GetAllowance()
if err != nil {
return err
}
return allowance.ValidateBasic()
}