* WIP: Msg authorization module added * fixing errors * fixed errors * fixed module.go * Add msg_tests * fixes compile issues * fix test * fix test * Add msg types tests * Fix Getmsgs * fixed codec issue * Fix syntax issues * Fix keeper * fixed proto issues * Fix keeper tests * fixed router in keeper * Fix query proto * Fix cli txs * Add grpc query client implementation * Add grpc-keeper test * Add grpc query tests Add revoke and exec authorization cli commands * Fix linting issues * Fix cli query * fix lint errors * Add Genesis state * Fix query authorization * Review changes * Fix grant authorization handler * Add cli tests * Add cli tests * Fix genesis test * Fix issues * update module to use proto msg services * Add simultion tests * Fix lint * fix lint * WIP simulations * WIP simulations * add msg tests * Fix simulation * Fix errors * fix genesis import export * fix sim tests * fix sim * fix test * Register RegisterMsgServer * WIP * WIP * Update keeper test * change msg_authorization module name to authz * changed type conversion for serviceMsg * serviceMsg change to any * Fix issues * fix msg tests * fix errors * proto format * remove LegacyQuerierHandler * Use MsgServiceRouter * fix keeper-test * fix query authorizations * fix NewCmdSendAs * fix simtests * fix error * fix lint * fix lint * add tests for generic authorization * fix imports * format * Update error message * remove println * add query all grants * Add pagination for queries * format * fix lint * review changes * fix grpc tests * add pagination to cli query * review changes * replace panic with error * lint * fix errors * fix tests * remove gogoproto extensions * update function doc * review changes * fix errors * fix query flags * fix grpc query test * init service-msg * remove unsed field * add proto-codec for simulations * fix codec issue * update authz simulations * change msgauth to authz * add check for invalid msg-type * change expiration flag to Unix * doc * update module.go * fix sims * fix grant-authorization sims * fix error * fix error * add build flag * fix codec issue * rename * review changes * format * review changes * go.mod * refactor * proto-gen * Update x/authz/keeper/grpc_query_test.go Co-authored-by: Amaury <amaury.martiny@protonmail.com> * Update x/authz/keeper/grpc_query_test.go Co-authored-by: Amaury <amaury.martiny@protonmail.com> * Update x/authz/keeper/grpc_query_test.go Co-authored-by: Amaury <amaury.martiny@protonmail.com> * Fix review comments * fix protogen * Follow Msg...Request style for msg requests * update comment * Fix error codes * fix review comment * improve msg validations * Handle error in casting msgs * rename actor => grantStoreKey * add godoc * add godoc * Fix simulations * Fix cli, cli_tests * Fix simulations * rename to GetOrRevokeAuthorization * Move events to keeper * Fix fmt * Update x/authz/client/cli/tx.go Co-authored-by: Amaury <amaury.martiny@protonmail.com> * rename actor * fix lint Co-authored-by: atheesh <atheesh@vitwit.com> Co-authored-by: atheeshp <59333759+atheeshp@users.noreply.github.com> Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> Co-authored-by: MD Aleem <72057206+aleem1413@users.noreply.github.com> Co-authored-by: Anil Kumar Kammari <anil@vitwit.com>
292 lines
10 KiB
Go
292 lines
10 KiB
Go
package simulation
|
|
|
|
import (
|
|
"context"
|
|
"math/rand"
|
|
"strings"
|
|
|
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
|
|
"github.com/cosmos/cosmos-sdk/simapp/helpers"
|
|
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/types/msgservice"
|
|
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
|
"github.com/cosmos/cosmos-sdk/x/authz/keeper"
|
|
"github.com/cosmos/cosmos-sdk/x/authz/types"
|
|
banktype "github.com/cosmos/cosmos-sdk/x/bank/types"
|
|
"github.com/cosmos/cosmos-sdk/x/simulation"
|
|
)
|
|
|
|
// authz message types
|
|
const (
|
|
TypeMsgGrantAuthorization = "/cosmos.authz.v1beta1.Msg/GrantAuthorization"
|
|
TypeMsgRevokeAuthorization = "/cosmos.authz.v1beta1.Msg/RevokeAuthorization"
|
|
TypeMsgExecDelegated = "/cosmos.authz.v1beta1.Msg/ExecAuthorized"
|
|
)
|
|
|
|
// Simulation operation weights constants
|
|
const (
|
|
OpWeightMsgGrantAuthorization = "op_weight_msg_grant_authorization"
|
|
OpWeightRevokeAuthorization = "op_weight_msg_revoke_authorization"
|
|
OpWeightExecAuthorized = "op_weight_msg_execute_authorized"
|
|
)
|
|
|
|
// WeightedOperations returns all the operations from the module with their respective weights
|
|
func WeightedOperations(
|
|
appParams simtypes.AppParams, cdc codec.JSONMarshaler, ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper, appCdc cdctypes.AnyUnpacker, protoCdc *codec.ProtoCodec) simulation.WeightedOperations {
|
|
|
|
var (
|
|
weightMsgGrantAuthorization int
|
|
weightRevokeAuthorization int
|
|
weightExecAuthorized int
|
|
)
|
|
|
|
appParams.GetOrGenerate(cdc, OpWeightMsgGrantAuthorization, &weightMsgGrantAuthorization, nil,
|
|
func(_ *rand.Rand) {
|
|
weightMsgGrantAuthorization = simappparams.DefaultWeightMsgDelegate
|
|
},
|
|
)
|
|
|
|
appParams.GetOrGenerate(cdc, OpWeightRevokeAuthorization, &weightRevokeAuthorization, nil,
|
|
func(_ *rand.Rand) {
|
|
weightRevokeAuthorization = simappparams.DefaultWeightMsgUndelegate
|
|
},
|
|
)
|
|
|
|
appParams.GetOrGenerate(cdc, OpWeightExecAuthorized, &weightExecAuthorized, nil,
|
|
func(_ *rand.Rand) {
|
|
weightExecAuthorized = simappparams.DefaultWeightMsgSend
|
|
},
|
|
)
|
|
|
|
return simulation.WeightedOperations{
|
|
simulation.NewWeightedOperation(
|
|
weightMsgGrantAuthorization,
|
|
SimulateMsgGrantAuthorization(ak, bk, k, protoCdc),
|
|
),
|
|
simulation.NewWeightedOperation(
|
|
weightRevokeAuthorization,
|
|
SimulateMsgRevokeAuthorization(ak, bk, k, protoCdc),
|
|
),
|
|
simulation.NewWeightedOperation(
|
|
weightExecAuthorized,
|
|
SimulateMsgExecuteAuthorized(ak, bk, k, appCdc, protoCdc),
|
|
),
|
|
}
|
|
}
|
|
|
|
// SimulateMsgGrantAuthorization generates a MsgGrantAuthorization with random values.
|
|
// nolint: funlen
|
|
func SimulateMsgGrantAuthorization(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper,
|
|
protoCdc *codec.ProtoCodec) simtypes.Operation {
|
|
return func(
|
|
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
|
|
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
|
|
granter := accs[0]
|
|
grantee := accs[1]
|
|
|
|
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, TypeMsgGrantAuthorization, err.Error()), nil, err
|
|
}
|
|
|
|
blockTime := ctx.BlockTime()
|
|
msg, err := types.NewMsgGrantAuthorization(granter.Address, grantee.Address,
|
|
types.NewSendAuthorization(spendableCoins.Sub(fees)), blockTime.AddDate(1, 0, 0))
|
|
|
|
if err != nil {
|
|
return simtypes.NoOpMsg(types.ModuleName, TypeMsgGrantAuthorization, err.Error()), nil, err
|
|
}
|
|
txGen := simappparams.MakeTestEncodingConfig().TxConfig
|
|
svcMsgClientConn := &msgservice.ServiceMsgClientConn{}
|
|
authzMsgClient := types.NewMsgClient(svcMsgClientConn)
|
|
_, err = authzMsgClient.GrantAuthorization(context.Background(), msg)
|
|
if err != nil {
|
|
return simtypes.NoOpMsg(types.ModuleName, TypeMsgGrantAuthorization, err.Error()), nil, err
|
|
}
|
|
tx, err := helpers.GenTx(
|
|
txGen,
|
|
svcMsgClientConn.GetMsgs(),
|
|
fees,
|
|
helpers.DefaultGenTxGas,
|
|
chainID,
|
|
[]uint64{account.GetAccountNumber()},
|
|
[]uint64{account.GetSequence()},
|
|
granter.PrivKey,
|
|
)
|
|
|
|
if err != nil {
|
|
return simtypes.NoOpMsg(types.ModuleName, TypeMsgGrantAuthorization, "unable to generate mock tx"), nil, err
|
|
}
|
|
|
|
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
|
|
if err != nil {
|
|
return simtypes.NoOpMsg(types.ModuleName, svcMsgClientConn.GetMsgs()[0].Type(), "unable to deliver tx"), nil, err
|
|
}
|
|
return simtypes.NewOperationMsg(svcMsgClientConn.GetMsgs()[0], true, "", protoCdc), nil, err
|
|
}
|
|
}
|
|
|
|
// SimulateMsgRevokeAuthorization generates a MsgRevokeAuthorization with random values.
|
|
// nolint: funlen
|
|
func SimulateMsgRevokeAuthorization(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper, protoCdc *codec.ProtoCodec) simtypes.Operation {
|
|
return func(
|
|
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
|
|
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
|
|
hasGrant := false
|
|
var targetGrant types.AuthorizationGrant
|
|
var granterAddr sdk.AccAddress
|
|
var granteeAddr sdk.AccAddress
|
|
k.IterateGrants(ctx, func(granter, grantee sdk.AccAddress, grant types.AuthorizationGrant) bool {
|
|
targetGrant = grant
|
|
granterAddr = granter
|
|
granteeAddr = grantee
|
|
hasGrant = true
|
|
return true
|
|
})
|
|
|
|
if !hasGrant {
|
|
return simtypes.NoOpMsg(types.ModuleName, TypeMsgRevokeAuthorization, "no grants"), nil, nil
|
|
}
|
|
|
|
granter, ok := simtypes.FindAccount(accs, granterAddr)
|
|
if !ok {
|
|
return simtypes.NoOpMsg(types.ModuleName, TypeMsgRevokeAuthorization, "Account not found"), 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, TypeMsgRevokeAuthorization, "fee error"), nil, err
|
|
}
|
|
auth := targetGrant.GetAuthorizationGrant()
|
|
msg := types.NewMsgRevokeAuthorization(granterAddr, granteeAddr, auth.MethodName())
|
|
|
|
txGen := simappparams.MakeTestEncodingConfig().TxConfig
|
|
svcMsgClientConn := &msgservice.ServiceMsgClientConn{}
|
|
authzMsgClient := types.NewMsgClient(svcMsgClientConn)
|
|
_, err = authzMsgClient.RevokeAuthorization(context.Background(), &msg)
|
|
if err != nil {
|
|
return simtypes.NoOpMsg(types.ModuleName, TypeMsgRevokeAuthorization, err.Error()), nil, err
|
|
}
|
|
tx, err := helpers.GenTx(
|
|
txGen,
|
|
svcMsgClientConn.GetMsgs(),
|
|
fees,
|
|
helpers.DefaultGenTxGas,
|
|
chainID,
|
|
[]uint64{account.GetAccountNumber()},
|
|
[]uint64{account.GetSequence()},
|
|
granter.PrivKey,
|
|
)
|
|
|
|
if err != nil {
|
|
return simtypes.NoOpMsg(types.ModuleName, TypeMsgRevokeAuthorization, err.Error()), nil, err
|
|
}
|
|
|
|
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
|
|
return simtypes.NewOperationMsg(svcMsgClientConn.GetMsgs()[0], true, "", protoCdc), nil, err
|
|
}
|
|
}
|
|
|
|
// SimulateMsgExecuteAuthorized generates a MsgExecuteAuthorized with random values.
|
|
// nolint: funlen
|
|
func SimulateMsgExecuteAuthorized(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper, cdc cdctypes.AnyUnpacker, protoCdc *codec.ProtoCodec) simtypes.Operation {
|
|
return func(
|
|
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
|
|
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
|
|
|
|
hasGrant := false
|
|
var targetGrant types.AuthorizationGrant
|
|
var granterAddr sdk.AccAddress
|
|
var granteeAddr sdk.AccAddress
|
|
k.IterateGrants(ctx, func(granter, grantee sdk.AccAddress, grant types.AuthorizationGrant) bool {
|
|
targetGrant = grant
|
|
granterAddr = granter
|
|
granteeAddr = grantee
|
|
hasGrant = true
|
|
return true
|
|
})
|
|
|
|
if !hasGrant {
|
|
return simtypes.NoOpMsg(types.ModuleName, TypeMsgExecDelegated, "Not found"), nil, nil
|
|
}
|
|
|
|
grantee, _ := simtypes.FindAccount(accs, granteeAddr)
|
|
granterAccount := ak.GetAccount(ctx, granterAddr)
|
|
granteeAccount := ak.GetAccount(ctx, granteeAddr)
|
|
|
|
granterspendableCoins := bk.SpendableCoins(ctx, granterAccount.GetAddress())
|
|
if granterspendableCoins.Empty() {
|
|
return simtypes.NoOpMsg(types.ModuleName, TypeMsgExecDelegated, "no coins"), nil, nil
|
|
}
|
|
|
|
if targetGrant.Expiration.Before(ctx.BlockHeader().Time) {
|
|
return simtypes.NoOpMsg(types.ModuleName, TypeMsgExecDelegated, "grant expired"), nil, nil
|
|
}
|
|
|
|
granteespendableCoins := bk.SpendableCoins(ctx, granteeAccount.GetAddress())
|
|
fees, err := simtypes.RandomFees(r, ctx, granteespendableCoins)
|
|
if err != nil {
|
|
return simtypes.NoOpMsg(types.ModuleName, TypeMsgExecDelegated, "fee error"), nil, err
|
|
}
|
|
sendCoins := sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(10)))
|
|
|
|
execMsg := sdk.ServiceMsg{
|
|
MethodName: types.SendAuthorization{}.MethodName(),
|
|
Request: banktype.NewMsgSend(
|
|
granterAddr,
|
|
granteeAddr,
|
|
sendCoins,
|
|
),
|
|
}
|
|
|
|
msg := types.NewMsgExecAuthorized(grantee.Address, []sdk.ServiceMsg{execMsg})
|
|
sendGrant := targetGrant.Authorization.GetCachedValue().(*types.SendAuthorization)
|
|
allow, _, _ := sendGrant.Accept(execMsg, ctx.BlockHeader())
|
|
if !allow {
|
|
return simtypes.NoOpMsg(types.ModuleName, TypeMsgExecDelegated, "not allowed"), nil, nil
|
|
}
|
|
|
|
txGen := simappparams.MakeTestEncodingConfig().TxConfig
|
|
svcMsgClientConn := &msgservice.ServiceMsgClientConn{}
|
|
authzMsgClient := types.NewMsgClient(svcMsgClientConn)
|
|
_, err = authzMsgClient.ExecAuthorized(context.Background(), &msg)
|
|
if err != nil {
|
|
return simtypes.NoOpMsg(types.ModuleName, TypeMsgExecDelegated, err.Error()), nil, err
|
|
}
|
|
tx, err := helpers.GenTx(
|
|
txGen,
|
|
svcMsgClientConn.GetMsgs(),
|
|
fees,
|
|
helpers.DefaultGenTxGas,
|
|
chainID,
|
|
[]uint64{granteeAccount.GetAccountNumber()},
|
|
[]uint64{granteeAccount.GetSequence()},
|
|
grantee.PrivKey,
|
|
)
|
|
|
|
if err != nil {
|
|
return simtypes.NoOpMsg(types.ModuleName, TypeMsgExecDelegated, err.Error()), nil, err
|
|
}
|
|
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "insufficient fee") {
|
|
return simtypes.NoOpMsg(types.ModuleName, TypeMsgExecDelegated, "insufficient fee"), nil, nil
|
|
}
|
|
return simtypes.NoOpMsg(types.ModuleName, TypeMsgExecDelegated, err.Error()), nil, err
|
|
}
|
|
msg.UnpackInterfaces(cdc)
|
|
if err != nil {
|
|
return simtypes.NoOpMsg(types.ModuleName, TypeMsgExecDelegated, "unmarshal error"), nil, err
|
|
}
|
|
return simtypes.NewOperationMsg(svcMsgClientConn.GetMsgs()[0], true, "success", protoCdc), nil, nil
|
|
}
|
|
}
|