github.com/Finschia/finschia-sdk@v0.49.1/x/authz/simulation/operations_test.go (about) 1 package simulation_test 2 3 import ( 4 "math/rand" 5 "testing" 6 "time" 7 8 ocabci "github.com/Finschia/ostracon/abci/types" 9 "github.com/stretchr/testify/suite" 10 tmproto "github.com/tendermint/tendermint/proto/tendermint/types" 11 12 "github.com/Finschia/finschia-sdk/simapp" 13 sdk "github.com/Finschia/finschia-sdk/types" 14 simtypes "github.com/Finschia/finschia-sdk/types/simulation" 15 "github.com/Finschia/finschia-sdk/x/authz" 16 "github.com/Finschia/finschia-sdk/x/authz/simulation" 17 banktypes "github.com/Finschia/finschia-sdk/x/bank/types" 18 ) 19 20 type SimTestSuite struct { 21 suite.Suite 22 23 ctx sdk.Context 24 app *simapp.SimApp 25 } 26 27 func (suite *SimTestSuite) SetupTest() { 28 checkTx := false 29 app := simapp.Setup(checkTx) 30 suite.app = app 31 suite.ctx = app.BaseApp.NewContext(checkTx, tmproto.Header{}) 32 } 33 34 func (suite *SimTestSuite) TestWeightedOperations() { 35 cdc := suite.app.AppCodec() 36 appParams := make(simtypes.AppParams) 37 38 weightedOps := simulation.WeightedOperations(appParams, cdc, suite.app.AccountKeeper, 39 suite.app.BankKeeper, suite.app.AuthzKeeper, cdc, 40 ) 41 42 // setup 3 accounts 43 s := rand.NewSource(1) 44 r := rand.New(s) 45 accs := suite.getTestingAccounts(r, 3) 46 47 expected := []struct { 48 weight int 49 opMsgRoute string 50 opMsgName string 51 }{ 52 {simulation.WeightGrant, authz.ModuleName, simulation.TypeMsgGrant}, 53 {simulation.WeightRevoke, authz.ModuleName, simulation.TypeMsgRevoke}, 54 {simulation.WeightExec, authz.ModuleName, simulation.TypeMsgExec}, 55 } 56 57 for i, w := range weightedOps { 58 operationMsg, _, _ := w.Op()(r, suite.app.BaseApp, suite.ctx, accs, "") 59 // the following checks are very much dependent from the ordering of the output given 60 // by WeightedOperations. if the ordering in WeightedOperations changes some tests 61 // will fail 62 suite.Require().Equal(expected[i].weight, w.Weight(), "weight should be the same") 63 suite.Require().Equal(expected[i].opMsgRoute, operationMsg.Route, "route should be the same") 64 suite.Require().Equal(expected[i].opMsgName, operationMsg.Name, "operation Msg name should be the same") 65 } 66 } 67 68 func (suite *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Account { 69 accounts := simtypes.RandomAccounts(r, n) 70 71 initAmt := suite.app.StakingKeeper.TokensFromConsensusPower(suite.ctx, 200000) 72 initCoins := sdk.NewCoins(sdk.NewCoin("stake", initAmt)) 73 74 // add coins to the accounts 75 for _, account := range accounts { 76 acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, account.Address) 77 suite.app.AccountKeeper.SetAccount(suite.ctx, acc) 78 suite.Require().NoError(simapp.FundAccount(suite.app, suite.ctx, account.Address, initCoins)) 79 } 80 81 return accounts 82 } 83 84 func (suite *SimTestSuite) TestSimulateGrant() { 85 s := rand.NewSource(1) 86 r := rand.New(s) 87 accounts := suite.getTestingAccounts(r, 2) 88 blockTime := time.Now().UTC() 89 ctx := suite.ctx.WithBlockTime(blockTime) 90 91 // begin a new block 92 suite.app.BeginBlock(ocabci.RequestBeginBlock{ 93 Header: tmproto.Header{ 94 Height: suite.app.LastBlockHeight() + 1, 95 AppHash: suite.app.LastCommitID().Hash, 96 }, 97 }) 98 99 granter := accounts[0] 100 grantee := accounts[1] 101 102 // execute operation 103 op := simulation.SimulateMsgGrant(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.AuthzKeeper) 104 operationMsg, futureOperations, err := op(r, suite.app.BaseApp, ctx, accounts, "") 105 suite.Require().NoError(err) 106 107 var msg authz.MsgGrant 108 err = suite.app.LegacyAmino().UnmarshalJSON(operationMsg.Msg, &msg) 109 suite.Require().NoError(err) 110 111 suite.Require().True(operationMsg.OK) 112 suite.Require().Equal(granter.Address.String(), msg.Granter) 113 suite.Require().Equal(grantee.Address.String(), msg.Grantee) 114 suite.Require().Len(futureOperations, 0) 115 } 116 117 func (suite *SimTestSuite) TestSimulateRevoke() { 118 // setup 3 accounts 119 s := rand.NewSource(2) 120 r := rand.New(s) 121 accounts := suite.getTestingAccounts(r, 3) 122 123 // begin a new block 124 suite.app.BeginBlock(ocabci.RequestBeginBlock{ 125 Header: tmproto.Header{ 126 Height: suite.app.LastBlockHeight() + 1, 127 AppHash: suite.app.LastCommitID().Hash, 128 }, 129 }) 130 131 initAmt := suite.app.StakingKeeper.TokensFromConsensusPower(suite.ctx, 200000) 132 initCoins := sdk.NewCoins(sdk.NewCoin("stake", initAmt)) 133 134 granter := accounts[0] 135 grantee := accounts[1] 136 authorization := banktypes.NewSendAuthorization(initCoins) 137 138 err := suite.app.AuthzKeeper.SaveGrant(suite.ctx, grantee.Address, granter.Address, authorization, time.Now().Add(30*time.Hour)) 139 suite.Require().NoError(err) 140 141 // execute operation 142 op := simulation.SimulateMsgRevoke(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.AuthzKeeper) 143 operationMsg, futureOperations, err := op(r, suite.app.BaseApp, suite.ctx, accounts, "") 144 suite.Require().NoError(err) 145 146 var msg authz.MsgRevoke 147 err = suite.app.LegacyAmino().UnmarshalJSON(operationMsg.Msg, &msg) 148 suite.Require().NoError(err) 149 150 suite.Require().True(operationMsg.OK) 151 suite.Require().Equal(granter.Address.String(), msg.Granter) 152 suite.Require().Equal(grantee.Address.String(), msg.Grantee) 153 suite.Require().Equal(banktypes.SendAuthorization{}.MsgTypeURL(), msg.MsgTypeUrl) 154 suite.Require().Len(futureOperations, 0) 155 } 156 157 func (suite *SimTestSuite) TestSimulateExec() { 158 // setup 3 accounts 159 s := rand.NewSource(1) 160 r := rand.New(s) 161 accounts := suite.getTestingAccounts(r, 3) 162 163 // begin a new block 164 suite.app.BeginBlock(ocabci.RequestBeginBlock{Header: tmproto.Header{Height: suite.app.LastBlockHeight() + 1, AppHash: suite.app.LastCommitID().Hash}}) 165 166 initAmt := suite.app.StakingKeeper.TokensFromConsensusPower(suite.ctx, 200000) 167 initCoins := sdk.NewCoins(sdk.NewCoin("stake", initAmt)) 168 169 granter := accounts[0] 170 grantee := accounts[1] 171 authorization := banktypes.NewSendAuthorization(initCoins) 172 173 err := suite.app.AuthzKeeper.SaveGrant(suite.ctx, grantee.Address, granter.Address, authorization, time.Now().Add(30*time.Hour)) 174 suite.Require().NoError(err) 175 176 // execute operation 177 op := simulation.SimulateMsgExec(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.AuthzKeeper, suite.app.AppCodec()) 178 operationMsg, futureOperations, err := op(r, suite.app.BaseApp, suite.ctx, accounts, "") 179 suite.Require().NoError(err) 180 181 var msg authz.MsgExec 182 err = suite.app.LegacyAmino().UnmarshalJSON(operationMsg.Msg, &msg) 183 suite.Require().NoError(err) 184 185 suite.Require().True(operationMsg.OK) 186 suite.Require().Equal(grantee.Address.String(), msg.Grantee) 187 suite.Require().Len(futureOperations, 0) 188 } 189 190 func TestSimTestSuite(t *testing.T) { 191 suite.Run(t, new(SimTestSuite)) 192 }