Skip to content

Commit

Permalink
cleanup sim tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-zaremba committed Mar 18, 2022
1 parent cc6cb8f commit 7375a06
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
1 change: 1 addition & 0 deletions x/authz/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ func (k Keeper) GetAuthorizations(ctx sdk.Context, grantee sdk.AccAddress, grant
// IterateGrants iterates over all authorization grants
// This function should be used with caution because it can involve significant IO operations.
// It should not be used in query or msg services without charging additional gas.
// The iteration stops when the handler function returns true or the iterator exhaust.
func (k Keeper) IterateGrants(ctx sdk.Context,
handler func(granterAddr sdk.AccAddress, granteeAddr sdk.AccAddress, grant authz.Grant) bool) {
store := ctx.KVStore(k.storeKey)
Expand Down
31 changes: 15 additions & 16 deletions x/authz/simulation/operations.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package simulation

import (
"fmt"
"math/rand"
"time"

Expand Down Expand Up @@ -216,19 +215,28 @@ func SimulateMsgExec(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keepe
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 authz.Grant
var granterAddr sdk.AccAddress
var granteeAddr sdk.AccAddress
var sendAuth *banktype.SendAuthorization
var err error
k.IterateGrants(ctx, func(granter, grantee sdk.AccAddress, grant authz.Grant) bool {
targetGrant = grant
granterAddr = granter
granteeAddr = grantee
hasGrant = true
return true
var a authz.Authorization
a, err = grant.GetAuthorization()
if err != nil {
return true
}
var ok bool
sendAuth, ok = a.(*banktype.SendAuthorization)
return ok
})

if !hasGrant {
if err != nil {
return simtypes.NoOpMsg(authz.ModuleName, TypeMsgExec, err.Error()), nil, err
}

if sendAuth == nil {
return simtypes.NoOpMsg(authz.ModuleName, TypeMsgExec, "no grant found"), nil, nil
}

Expand All @@ -250,15 +258,6 @@ func SimulateMsgExec(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keepe
}

msg := []sdk.Msg{banktype.NewMsgSend(granterAddr, granteeAddr, coins)}
authorization, err := targetGrant.GetAuthorization()
if err != nil {
return simtypes.NoOpMsg(authz.ModuleName, TypeMsgExec, err.Error()), nil, err
}

sendAuth, ok := authorization.(*banktype.SendAuthorization)
if !ok {
return simtypes.NoOpMsg(authz.ModuleName, TypeMsgExec, fmt.Sprintf("not a send authorization, got: %T", authorization)), nil, nil
}

_, err = sendAuth.Accept(ctx, msg[0])
if err != nil {
Expand Down
12 changes: 7 additions & 5 deletions x/authz/simulation/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@ func (suite *SimTestSuite) TestWeightedOperations() {

require := suite.Require()
for i, w := range weightedOps {
operationMsg, _, err := w.Op()(r, suite.app.BaseApp, suite.ctx, accs, "")
op, _, err := w.Op()(r, suite.app.BaseApp, suite.ctx, accs, "")
require.NoError(err)
// the following checks are very much dependent from the ordering of the output given
// by WeightedOperations. if the ordering in WeightedOperations changes some tests
// will fail
require.Equal(expected[i].weight, w.Weight(), "weight should be the same")
require.Equal(expected[i].opMsgRoute, operationMsg.Route,
"route should be the same. %v", operationMsg.Comment)
require.Equal(expected[i].opMsgRoute, operationMsg.Name, "operation Msg name should be the same")
require.Equal(expected[i].weight, w.Weight(),
"weight should be the same. %v", op.Comment)
require.Equal(expected[i].opMsgRoute, op.Route,
"route should be the same. %v", op.Comment)
require.Equal(expected[i].opMsgRoute, op.Name,
"operation Msg name should be the same %v", op.Comment)
}
}

Expand Down

0 comments on commit 7375a06

Please sign in to comment.