-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
x/authz: simulation audit changes (#9107)
* addressing audit changes * address simulation genesis changes * address simulation operations changes * fix tests * typo * add more authorizations to operations * fix tests * fix failing simulations * WIP * WIP * testing simulations * test simulations * try fixing tests * WIP * fix error * test * Add exec authorization * WIP * WIP * fix tests * WIP * WIP * WIP * WIP * WIP * fix errors * fix test * WIP * try fix test * update tests * fix errors * add exec authorization * fix docs * fix test * fix error * try fixing simulation * fix errors * fixing simulations * fix errors * rename GenTx -> GenerateTx * Update x/authz/simulation/genesis.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update x/authz/simulation/genesis.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update x/authz/simulation/operations.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * review changes * fix tests * rename GenerateTx => GenTx * remove Authorization suffix Co-authored-by: Robert Zaremba <robert@zaremba.ch> Co-authored-by: atheeshp <59333759+atheeshp@users.noreply.github.com>
- Loading branch information
1 parent
56c0595
commit 8cfa2c2
Showing
6 changed files
with
188 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,59 @@ | ||
package simulation | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"math/rand" | ||
|
||
codectypes "github.com/cosmos/cosmos-sdk/codec/types" | ||
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/authz" | ||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" | ||
) | ||
|
||
// GenAuthorizationGrant returns an empty slice of authorization grants. | ||
func GenAuthorizationGrant(_ *rand.Rand, _ []simtypes.Account) []authz.GrantAuthorization { | ||
return []authz.GrantAuthorization{} | ||
// genGrant returns a slice of authorization grants. | ||
func genGrant(r *rand.Rand, accounts []simtypes.Account) []authz.GrantAuthorization { | ||
authorizations := make([]authz.GrantAuthorization, len(accounts)-1) | ||
for i := 0; i < len(accounts)-1; i++ { | ||
granter := accounts[i] | ||
grantee := accounts[i+1] | ||
authorizations[i] = authz.GrantAuthorization{ | ||
Granter: granter.Address.String(), | ||
Grantee: grantee.Address.String(), | ||
Authorization: generateRandomGrant(r), | ||
} | ||
} | ||
|
||
return authorizations | ||
} | ||
|
||
func generateRandomGrant(r *rand.Rand) *codectypes.Any { | ||
authorizations := make([]*codectypes.Any, 2) | ||
authorizations[0] = newAnyAuthorization(banktypes.NewSendAuthorization(sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(1000))))) | ||
authorizations[1] = newAnyAuthorization(authz.NewGenericAuthorization(sdk.MsgTypeURL(&govtypes.MsgSubmitProposal{}))) | ||
|
||
return authorizations[r.Intn(len(authorizations))] | ||
} | ||
|
||
func newAnyAuthorization(a authz.Authorization) *codectypes.Any { | ||
any, err := codectypes.NewAnyWithValue(a) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return any | ||
} | ||
|
||
// RandomizedGenState generates a random GenesisState for authz. | ||
func RandomizedGenState(simState *module.SimulationState) { | ||
var grants []authz.GrantAuthorization | ||
|
||
simState.AppParams.GetOrGenerate( | ||
simState.Cdc, "authz", &grants, simState.Rand, | ||
func(r *rand.Rand) { grants = GenAuthorizationGrant(r, simState.Accounts) }, | ||
func(r *rand.Rand) { grants = genGrant(r, simState.Accounts) }, | ||
) | ||
authzGrantsGenesis := authz.NewGenesisState(grants) | ||
|
||
bz, err := json.MarshalIndent(&authzGrantsGenesis, "", " ") | ||
if err != nil { | ||
panic(err) | ||
} | ||
authzGrantsGenesis := authz.NewGenesisState(grants) | ||
|
||
fmt.Printf("Selected randomly generated %s parameters:\n%s\n", authz.ModuleName, bz) | ||
simState.GenState[authz.ModuleName] = simState.Cdc.MustMarshalJSON(authzGrantsGenesis) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.