github.com/Finschia/finschia-sdk@v0.48.1/x/feegrant/simulation/operations_test.go (about) 1 package simulation_test 2 3 import ( 4 "math/rand" 5 "testing" 6 "time" 7 8 "github.com/stretchr/testify/suite" 9 tmproto "github.com/tendermint/tendermint/proto/tendermint/types" 10 11 ocabci "github.com/Finschia/ostracon/abci/types" 12 13 "github.com/Finschia/finschia-sdk/simapp" 14 simappparams "github.com/Finschia/finschia-sdk/simapp/params" 15 sdk "github.com/Finschia/finschia-sdk/types" 16 simtypes "github.com/Finschia/finschia-sdk/types/simulation" 17 "github.com/Finschia/finschia-sdk/x/feegrant" 18 "github.com/Finschia/finschia-sdk/x/feegrant/simulation" 19 ) 20 21 type SimTestSuite struct { 22 suite.Suite 23 24 ctx sdk.Context 25 app *simapp.SimApp 26 } 27 28 func (suite *SimTestSuite) SetupTest() { 29 checkTx := false 30 app := simapp.Setup(checkTx) 31 suite.app = app 32 suite.ctx = app.BaseApp.NewContext(checkTx, tmproto.Header{ 33 Time: time.Now(), 34 }) 35 } 36 37 func (suite *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Account { 38 accounts := simtypes.RandomAccounts(r, n) 39 40 initAmt := sdk.TokensFromConsensusPower(200, sdk.DefaultPowerReduction) 41 initCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt)) 42 43 // add coins to the accounts 44 for _, account := range accounts { 45 err := simapp.FundAccount(suite.app, suite.ctx, account.Address, initCoins) 46 suite.Require().NoError(err) 47 } 48 49 return accounts 50 } 51 52 func (suite *SimTestSuite) TestWeightedOperations() { 53 app, ctx := suite.app, suite.ctx 54 require := suite.Require() 55 56 ctx.WithChainID("test-chain") 57 58 cdc := app.AppCodec() 59 appParams := make(simtypes.AppParams) 60 61 weightedOps := simulation.WeightedOperations( 62 appParams, cdc, app.AccountKeeper, 63 app.BankKeeper, app.FeeGrantKeeper, 64 ) 65 66 s := rand.NewSource(1) 67 r := rand.New(s) 68 accs := suite.getTestingAccounts(r, 3) 69 70 expected := []struct { 71 weight int 72 opMsgRoute string 73 opMsgName string 74 }{ 75 { 76 simappparams.DefaultWeightGrantAllowance, 77 feegrant.ModuleName, 78 simulation.TypeMsgGrantAllowance, 79 }, 80 { 81 simappparams.DefaultWeightRevokeAllowance, 82 feegrant.ModuleName, 83 simulation.TypeMsgRevokeAllowance, 84 }, 85 } 86 87 for i, w := range weightedOps { 88 operationMsg, _, _ := w.Op()(r, app.BaseApp, ctx, accs, ctx.ChainID()) 89 // the following checks are very much dependent from the ordering of the output given 90 // by WeightedOperations. if the ordering in WeightedOperations changes some tests 91 // will fail 92 require.Equal(expected[i].weight, w.Weight(), "weight should be the same") 93 require.Equal(expected[i].opMsgRoute, operationMsg.Route, "route should be the same") 94 require.Equal(expected[i].opMsgName, operationMsg.Name, "operation Msg name should be the same") 95 } 96 } 97 98 func (suite *SimTestSuite) TestSimulateMsgGrantAllowance() { 99 app, ctx := suite.app, suite.ctx 100 require := suite.Require() 101 102 s := rand.NewSource(1) 103 r := rand.New(s) 104 accounts := suite.getTestingAccounts(r, 3) 105 106 // begin a new block 107 app.BeginBlock(ocabci.RequestBeginBlock{Header: tmproto.Header{Height: app.LastBlockHeight() + 1, AppHash: app.LastCommitID().Hash}}) 108 109 // execute operation 110 op := simulation.SimulateMsgGrantAllowance(app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper) 111 operationMsg, futureOperations, err := op(r, app.BaseApp, ctx, accounts, "") 112 require.NoError(err) 113 114 var msg feegrant.MsgGrantAllowance 115 suite.app.LegacyAmino().UnmarshalJSON(operationMsg.Msg, &msg) 116 117 require.True(operationMsg.OK) 118 require.Equal(accounts[2].Address.String(), msg.Granter) 119 require.Equal(accounts[1].Address.String(), msg.Grantee) 120 require.Len(futureOperations, 0) 121 } 122 123 func (suite *SimTestSuite) TestSimulateMsgRevokeAllowance() { 124 app, ctx := suite.app, suite.ctx 125 require := suite.Require() 126 127 s := rand.NewSource(1) 128 r := rand.New(s) 129 accounts := suite.getTestingAccounts(r, 3) 130 131 // begin a new block 132 app.BeginBlock(ocabci.RequestBeginBlock{Header: tmproto.Header{Height: suite.app.LastBlockHeight() + 1, AppHash: suite.app.LastCommitID().Hash}}) 133 134 feeAmt := app.StakingKeeper.TokensFromConsensusPower(ctx, 200000) 135 feeCoins := sdk.NewCoins(sdk.NewCoin("foo", feeAmt)) 136 137 granter, grantee := accounts[0], accounts[1] 138 139 oneYear := ctx.BlockTime().AddDate(1, 0, 0) 140 err := app.FeeGrantKeeper.GrantAllowance( 141 ctx, 142 granter.Address, 143 grantee.Address, 144 &feegrant.BasicAllowance{ 145 SpendLimit: feeCoins, 146 Expiration: &oneYear, 147 }, 148 ) 149 require.NoError(err) 150 151 // execute operation 152 op := simulation.SimulateMsgRevokeAllowance(app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper) 153 operationMsg, futureOperations, err := op(r, app.BaseApp, ctx, accounts, "") 154 require.NoError(err) 155 156 var msg feegrant.MsgRevokeAllowance 157 suite.app.LegacyAmino().UnmarshalJSON(operationMsg.Msg, &msg) 158 159 require.True(operationMsg.OK) 160 require.Equal(granter.Address.String(), msg.Granter) 161 require.Equal(grantee.Address.String(), msg.Grantee) 162 require.Len(futureOperations, 0) 163 } 164 165 func TestSimTestSuite(t *testing.T) { 166 suite.Run(t, new(SimTestSuite)) 167 }