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:
co-authored by
Marie Gauthier
parent
fdbc32e50c
commit
fc256a3683
@@ -1,10 +1,11 @@
|
|||||||
package simulation
|
package simulation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"github.com/cosmos/cosmos-sdk/types/module"
|
"github.com/cosmos/cosmos-sdk/types/module"
|
||||||
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
||||||
"github.com/cosmos/cosmos-sdk/x/feegrant/types"
|
"github.com/cosmos/cosmos-sdk/x/feegrant/types"
|
||||||
@@ -13,9 +14,52 @@ import (
|
|||||||
// Simulation parameter constants
|
// Simulation parameter constants
|
||||||
const feegrant = "feegrant"
|
const feegrant = "feegrant"
|
||||||
|
|
||||||
// GenFeeGrants returns an empty slice of evidences.
|
// genFeeGrants returns a slice of randomly generated allowances.
|
||||||
func GenFeeGrants(_ *rand.Rand, _ []simtypes.Account) []types.FeeAllowanceGrant {
|
func genFeeGrants(r *rand.Rand, accounts []simtypes.Account) []types.FeeAllowanceGrant {
|
||||||
return []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
|
// RandomizedGenState generates a random GenesisState for feegrant
|
||||||
@@ -24,15 +68,15 @@ func RandomizedGenState(simState *module.SimulationState) {
|
|||||||
|
|
||||||
simState.AppParams.GetOrGenerate(
|
simState.AppParams.GetOrGenerate(
|
||||||
simState.Cdc, feegrant, &feegrants, simState.Rand,
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Selected randomly generated %s parameters:\n%s\n", types.ModuleName, bz)
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/codec"
|
"github.com/cosmos/cosmos-sdk/simapp"
|
||||||
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
|
||||||
"github.com/cosmos/cosmos-sdk/types/module"
|
"github.com/cosmos/cosmos-sdk/types/module"
|
||||||
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
||||||
"github.com/cosmos/cosmos-sdk/x/feegrant/simulation"
|
"github.com/cosmos/cosmos-sdk/x/feegrant/simulation"
|
||||||
@@ -16,18 +15,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestRandomizedGenState(t *testing.T) {
|
func TestRandomizedGenState(t *testing.T) {
|
||||||
interfaceRegistry := codectypes.NewInterfaceRegistry()
|
app := simapp.Setup(false)
|
||||||
cdc := codec.NewProtoCodec(interfaceRegistry)
|
|
||||||
|
|
||||||
s := rand.NewSource(1)
|
s := rand.NewSource(1)
|
||||||
r := rand.New(s)
|
r := rand.New(s)
|
||||||
|
|
||||||
|
accounts := simtypes.RandomAccounts(r, 3)
|
||||||
|
|
||||||
simState := module.SimulationState{
|
simState := module.SimulationState{
|
||||||
AppParams: make(simtypes.AppParams),
|
AppParams: make(simtypes.AppParams),
|
||||||
Cdc: cdc,
|
Cdc: app.AppCodec(),
|
||||||
Rand: r,
|
Rand: r,
|
||||||
NumBonded: 3,
|
NumBonded: 3,
|
||||||
Accounts: simtypes.RandomAccounts(r, 3),
|
Accounts: accounts,
|
||||||
InitialStake: 1000,
|
InitialStake: 1000,
|
||||||
GenState: make(map[string]json.RawMessage),
|
GenState: make(map[string]json.RawMessage),
|
||||||
}
|
}
|
||||||
@@ -36,5 +36,5 @@ func TestRandomizedGenState(t *testing.T) {
|
|||||||
var feegrantGenesis types.GenesisState
|
var feegrantGenesis types.GenesisState
|
||||||
simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &feegrantGenesis)
|
simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &feegrantGenesis)
|
||||||
|
|
||||||
require.Len(t, feegrantGenesis.FeeAllowances, 0)
|
require.Len(t, feegrantGenesis.FeeAllowances, len(accounts)-1)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,11 +68,11 @@ func SimulateMsgGrantFeeAllowance(ak types.AccountKeeper, bk types.BankKeeper, k
|
|||||||
granter, _ := simtypes.RandomAcc(r, accs)
|
granter, _ := simtypes.RandomAcc(r, accs)
|
||||||
grantee, _ := simtypes.RandomAcc(r, accs)
|
grantee, _ := simtypes.RandomAcc(r, accs)
|
||||||
if grantee.Address.String() == granter.Address.String() {
|
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 {
|
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)
|
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())
|
spendableCoins := bk.SpendableCoins(ctx, account.GetAddress())
|
||||||
fees, err := simtypes.RandomFees(r, ctx, spendableCoins)
|
fees, err := simtypes.RandomFees(r, ctx, spendableCoins)
|
||||||
if err != nil {
|
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)
|
spendableCoins = spendableCoins.Sub(fees)
|
||||||
if spendableCoins.Empty() {
|
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{
|
msg, err := types.NewMsgGrantFeeAllowance(&types.BasicFeeAllowance{
|
||||||
@@ -94,14 +94,14 @@ func SimulateMsgGrantFeeAllowance(ak types.AccountKeeper, bk types.BankKeeper, k
|
|||||||
}, granter.Address, grantee.Address)
|
}, granter.Address, grantee.Address)
|
||||||
|
|
||||||
if err != nil {
|
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
|
txGen := simappparams.MakeTestEncodingConfig().TxConfig
|
||||||
svcMsgClientConn := &msgservice.ServiceMsgClientConn{}
|
svcMsgClientConn := &msgservice.ServiceMsgClientConn{}
|
||||||
feegrantMsgClient := types.NewMsgClient(svcMsgClientConn)
|
feegrantMsgClient := types.NewMsgClient(svcMsgClientConn)
|
||||||
_, err = feegrantMsgClient.GrantFeeAllowance(context.Background(), msg)
|
_, err = feegrantMsgClient.GrantFeeAllowance(context.Background(), msg)
|
||||||
if err != nil {
|
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(
|
tx, err := helpers.GenTx(
|
||||||
txGen,
|
txGen,
|
||||||
@@ -175,7 +175,7 @@ func SimulateMsgRevokeFeeAllowance(ak types.AccountKeeper, bk types.BankKeeper,
|
|||||||
feegrantMsgClient := types.NewMsgClient(svcMsgClientConn)
|
feegrantMsgClient := types.NewMsgClient(svcMsgClientConn)
|
||||||
_, err = feegrantMsgClient.RevokeFeeAllowance(context.Background(), &msg)
|
_, err = feegrantMsgClient.RevokeFeeAllowance(context.Background(), &msg)
|
||||||
if err != nil {
|
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(
|
tx, err := helpers.GenTx(
|
||||||
|
|||||||
@@ -118,8 +118,8 @@ func (suite *SimTestSuite) TestSimulateMsgGrantFeeAllowance() {
|
|||||||
suite.app.AppCodec().UnmarshalJSON(operationMsg.Msg, &msg)
|
suite.app.AppCodec().UnmarshalJSON(operationMsg.Msg, &msg)
|
||||||
|
|
||||||
require.True(operationMsg.OK)
|
require.True(operationMsg.OK)
|
||||||
require.Equal("cosmos1ghekyjucln7y67ntx7cf27m9dpuxxemn4c8g4r", msg.Granter)
|
require.Equal(accounts[2].Address.String(), msg.Granter)
|
||||||
require.Equal("cosmos1p8wcgrjr4pjju90xg6u9cgq55dxwq8j7u4x9a0", msg.Grantee)
|
require.Equal(accounts[1].Address.String(), msg.Grantee)
|
||||||
require.Len(futureOperations, 0)
|
require.Len(futureOperations, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,12 +13,6 @@ var (
|
|||||||
_ types.UnpackInterfacesMessage = &MsgGrantFeeAllowance{}
|
_ types.UnpackInterfacesMessage = &MsgGrantFeeAllowance{}
|
||||||
)
|
)
|
||||||
|
|
||||||
// feegrant message types
|
|
||||||
const (
|
|
||||||
TypeMsgGrantFeeAllowance = "grant_fee_allowance"
|
|
||||||
TypeMsgRevokeFeeAllowance = "revoke_fee_allowance"
|
|
||||||
)
|
|
||||||
|
|
||||||
// NewMsgGrantFeeAllowance creates a new MsgGrantFeeAllowance.
|
// NewMsgGrantFeeAllowance creates a new MsgGrantFeeAllowance.
|
||||||
//nolint:interfacer
|
//nolint:interfacer
|
||||||
func NewMsgGrantFeeAllowance(feeAllowance FeeAllowanceI, granter, grantee sdk.AccAddress) (*MsgGrantFeeAllowance, error) {
|
func NewMsgGrantFeeAllowance(feeAllowance FeeAllowanceI, granter, grantee sdk.AccAddress) (*MsgGrantFeeAllowance, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user