Skip to content

Commit

Permalink
x/feegrant simulation audit changes (#9145)
Browse files Browse the repository at this point in the history
* simulation genesis changes

* update msg types

* refactor

* update operations test

* fix sims

* fix tests

* fix tests

* address review changes

Co-authored-by: Marie Gauthier <[email protected]>

Co-authored-by: Marie Gauthier <[email protected]>
  • Loading branch information
atheeshp and blushi authored Apr 27, 2021
1 parent fdbc32e commit fc256a3
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 30 deletions.
60 changes: 52 additions & 8 deletions x/feegrant/simulation/genesis.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package simulation

import (
"encoding/json"
"fmt"
"math/rand"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/feegrant/types"
Expand All @@ -13,9 +14,52 @@ import (
// Simulation parameter constants
const feegrant = "feegrant"

// GenFeeGrants returns an empty slice of evidences.
func GenFeeGrants(_ *rand.Rand, _ []simtypes.Account) []types.FeeAllowanceGrant {
return []types.FeeAllowanceGrant{}
// genFeeGrants returns a slice of randomly generated allowances.
func genFeeGrants(r *rand.Rand, accounts []simtypes.Account) []types.FeeAllowanceGrant {
allowances := make([]types.FeeAllowanceGrant, len(accounts)-1)
for i := 0; i < len(accounts)-1; i++ {
granter := accounts[i].Address
grantee := accounts[i+1].Address
allowances[i] = generateRandomAllowances(granter, grantee, r)
}
return allowances
}

func generateRandomAllowances(granter, grantee sdk.AccAddress, r *rand.Rand) types.FeeAllowanceGrant {
allowances := make([]types.FeeAllowanceGrant, 3)
spendLimit := sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(100)))
periodSpendLimit := sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(10)))

basic := types.BasicFeeAllowance{
SpendLimit: spendLimit,
}

basicAllowance, err := types.NewFeeAllowanceGrant(granter, grantee, &basic)
if err != nil {
panic(err)
}
allowances[0] = basicAllowance

periodicAllowance, err := types.NewFeeAllowanceGrant(granter, grantee, &types.PeriodicFeeAllowance{
Basic: basic,
PeriodSpendLimit: periodSpendLimit,
Period: types.ClockDuration(time.Hour),
})
if err != nil {
panic(err)
}
allowances[1] = periodicAllowance

filteredAllowance, err := types.NewFeeAllowanceGrant(granter, grantee, &types.AllowedMsgFeeAllowance{
Allowance: basicAllowance.GetAllowance(),
AllowedMessages: []string{"/cosmos.gov.v1beta1.Msg/SubmitProposal"},
})
if err != nil {
panic(err)
}
allowances[2] = filteredAllowance

return allowances[r.Intn(len(allowances))]
}

// RandomizedGenState generates a random GenesisState for feegrant
Expand All @@ -24,15 +68,15 @@ func RandomizedGenState(simState *module.SimulationState) {

simState.AppParams.GetOrGenerate(
simState.Cdc, feegrant, &feegrants, simState.Rand,
func(r *rand.Rand) { feegrants = GenFeeGrants(r, simState.Accounts) },
func(r *rand.Rand) { feegrants = genFeeGrants(r, simState.Accounts) },
)
feegrantGenesis := types.NewGenesisState(feegrants)

bz, err := json.MarshalIndent(&feegrantGenesis, "", " ")
feegrantGenesis := types.NewGenesisState(feegrants)
bz, err := simState.Cdc.MarshalJSON(feegrantGenesis)
if err != nil {
panic(err)
}

fmt.Printf("Selected randomly generated %s parameters:\n%s\n", types.ModuleName, bz)
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(feegrantGenesis)
simState.GenState[types.ModuleName] = bz
}
14 changes: 7 additions & 7 deletions x/feegrant/simulation/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ import (

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/feegrant/simulation"
"github.com/cosmos/cosmos-sdk/x/feegrant/types"
)

func TestRandomizedGenState(t *testing.T) {
interfaceRegistry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(interfaceRegistry)
app := simapp.Setup(false)

s := rand.NewSource(1)
r := rand.New(s)

accounts := simtypes.RandomAccounts(r, 3)

simState := module.SimulationState{
AppParams: make(simtypes.AppParams),
Cdc: cdc,
Cdc: app.AppCodec(),
Rand: r,
NumBonded: 3,
Accounts: simtypes.RandomAccounts(r, 3),
Accounts: accounts,
InitialStake: 1000,
GenState: make(map[string]json.RawMessage),
}
Expand All @@ -36,5 +36,5 @@ func TestRandomizedGenState(t *testing.T) {
var feegrantGenesis types.GenesisState
simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &feegrantGenesis)

require.Len(t, feegrantGenesis.FeeAllowances, 0)
require.Len(t, feegrantGenesis.FeeAllowances, len(accounts)-1)
}
14 changes: 7 additions & 7 deletions x/feegrant/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,24 @@ func SimulateMsgGrantFeeAllowance(ak types.AccountKeeper, bk types.BankKeeper, k
granter, _ := simtypes.RandomAcc(r, accs)
grantee, _ := simtypes.RandomAcc(r, accs)
if grantee.Address.String() == granter.Address.String() {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgGrantFeeAllowance, "grantee and granter cannot be same"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgGrantFeeAllowance, "grantee and granter cannot be same"), nil, nil
}

if f, _ := k.GetFeeAllowance(ctx, granter.Address, grantee.Address); f != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgGrantFeeAllowance, "fee allowance exists"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgGrantFeeAllowance, "fee allowance exists"), nil, nil
}

account := ak.GetAccount(ctx, granter.Address)

spendableCoins := bk.SpendableCoins(ctx, account.GetAddress())
fees, err := simtypes.RandomFees(r, ctx, spendableCoins)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgGrantFeeAllowance, err.Error()), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgGrantFeeAllowance, err.Error()), nil, err
}

spendableCoins = spendableCoins.Sub(fees)
if spendableCoins.Empty() {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgGrantFeeAllowance, "unable to grant empty coins as SpendLimit"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgGrantFeeAllowance, "unable to grant empty coins as SpendLimit"), nil, nil
}

msg, err := types.NewMsgGrantFeeAllowance(&types.BasicFeeAllowance{
Expand All @@ -94,14 +94,14 @@ func SimulateMsgGrantFeeAllowance(ak types.AccountKeeper, bk types.BankKeeper, k
}, granter.Address, grantee.Address)

if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgGrantFeeAllowance, err.Error()), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgGrantFeeAllowance, err.Error()), nil, err
}
txGen := simappparams.MakeTestEncodingConfig().TxConfig
svcMsgClientConn := &msgservice.ServiceMsgClientConn{}
feegrantMsgClient := types.NewMsgClient(svcMsgClientConn)
_, err = feegrantMsgClient.GrantFeeAllowance(context.Background(), msg)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgGrantFeeAllowance, err.Error()), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgGrantFeeAllowance, err.Error()), nil, err
}
tx, err := helpers.GenTx(
txGen,
Expand Down Expand Up @@ -175,7 +175,7 @@ func SimulateMsgRevokeFeeAllowance(ak types.AccountKeeper, bk types.BankKeeper,
feegrantMsgClient := types.NewMsgClient(svcMsgClientConn)
_, err = feegrantMsgClient.RevokeFeeAllowance(context.Background(), &msg)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgGrantFeeAllowance, err.Error()), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgGrantFeeAllowance, err.Error()), nil, err
}

tx, err := helpers.GenTx(
Expand Down
4 changes: 2 additions & 2 deletions x/feegrant/simulation/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ func (suite *SimTestSuite) TestSimulateMsgGrantFeeAllowance() {
suite.app.AppCodec().UnmarshalJSON(operationMsg.Msg, &msg)

require.True(operationMsg.OK)
require.Equal("cosmos1ghekyjucln7y67ntx7cf27m9dpuxxemn4c8g4r", msg.Granter)
require.Equal("cosmos1p8wcgrjr4pjju90xg6u9cgq55dxwq8j7u4x9a0", msg.Grantee)
require.Equal(accounts[2].Address.String(), msg.Granter)
require.Equal(accounts[1].Address.String(), msg.Grantee)
require.Len(futureOperations, 0)
}

Expand Down
6 changes: 0 additions & 6 deletions x/feegrant/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ var (
_ types.UnpackInterfacesMessage = &MsgGrantFeeAllowance{}
)

// feegrant message types
const (
TypeMsgGrantFeeAllowance = "grant_fee_allowance"
TypeMsgRevokeFeeAllowance = "revoke_fee_allowance"
)

// NewMsgGrantFeeAllowance creates a new MsgGrantFeeAllowance.
//nolint:interfacer
func NewMsgGrantFeeAllowance(feeAllowance FeeAllowanceI, granter, grantee sdk.AccAddress) (*MsgGrantFeeAllowance, error) {
Expand Down

0 comments on commit fc256a3

Please sign in to comment.