x/feegrant simulation audit changes (#9145)

* simulation genesis changes

* update msg types

* refactor

* update operations test

* fix sims

* fix tests

* fix tests

* address review changes

Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com>

Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com>
This commit is contained in:
atheeshp 2021-04-27 13:12:48 +05:30 committed by GitHub
parent fdbc32e50c
commit fc256a3683
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 68 additions and 30 deletions

View File

@ -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"
@ -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
@ -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
}

View File

@ -7,8 +7,7 @@ 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"
@ -16,18 +15,19 @@ import (
)
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),
}
@ -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)
}

View File

@ -68,11 +68,11 @@ 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)
@ -80,12 +80,12 @@ func SimulateMsgGrantFeeAllowance(ak types.AccountKeeper, bk types.BankKeeper, k
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{
@ -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,
@ -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(

View File

@ -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)
}

View File

@ -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) {