Skip to content

Commit

Permalink
x/authz: simulation audit changes (#9107)
Browse files Browse the repository at this point in the history
* 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
3 people authored May 11, 2021
1 parent 56c0595 commit 8cfa2c2
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 142 deletions.
1 change: 1 addition & 0 deletions x/authz/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ type AccountKeeper interface {
// BankKeeper defines the expected interface needed to retrieve account balances.
type BankKeeper interface {
SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins
IsSendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error
}
10 changes: 5 additions & 5 deletions x/authz/simulation/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ func TestDecodeStore(t *testing.T) {

tests := []struct {
name string
expectErr bool
expectedLog string
}{
{"Grant", fmt.Sprintf("%v\n%v", grant, grant)},
{"other", ""},
{"Grant", false, fmt.Sprintf("%v\n%v", grant, grant)},
{"other", true, ""},
}

for i, tt := range tests {
i, tt := i, tt
t.Run(tt.name, func(t *testing.T) {
switch i {
case len(tests) - 1:
if tt.expectErr {
require.Panics(t, func() { dec(kvPairs.Pairs[i], kvPairs.Pairs[i]) }, tt.name)
default:
} else {
require.Equal(t, tt.expectedLog, dec(kvPairs.Pairs[i], kvPairs.Pairs[i]), tt.name)
}
})
Expand Down
50 changes: 37 additions & 13 deletions x/authz/simulation/genesis.go
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)
}
10 changes: 4 additions & 6 deletions x/authz/simulation/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,22 @@ 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/authz"
"github.com/cosmos/cosmos-sdk/x/authz/simulation"
)

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

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

simState := module.SimulationState{
AppParams: make(simtypes.AppParams),
Cdc: cdc,
Cdc: app.AppCodec(),
Rand: r,
NumBonded: 3,
Accounts: simtypes.RandomAccounts(r, 3),
Expand All @@ -36,5 +34,5 @@ func TestRandomizedGenState(t *testing.T) {
var authzGenesis authz.GenesisState
simState.Cdc.MustUnmarshalJSON(simState.GenState[authz.ModuleName], &authzGenesis)

require.Len(t, authzGenesis.Authorization, 0)
require.Len(t, authzGenesis.Authorization, len(simState.Accounts) - 1)
}
Loading

0 comments on commit 8cfa2c2

Please sign in to comment.