github.com/Finschia/finschia-sdk@v0.49.1/x/feegrant/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 simappparams "github.com/Finschia/finschia-sdk/simapp/params" 14 sdk "github.com/Finschia/finschia-sdk/types" 15 simtypes "github.com/Finschia/finschia-sdk/types/simulation" 16 "github.com/Finschia/finschia-sdk/x/feegrant" 17 "github.com/Finschia/finschia-sdk/x/feegrant/simulation" 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 Time: time.Now(), 33 }) 34 } 35 36 func (suite *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Account { 37 accounts := simtypes.RandomAccounts(r, n) 38 39 initAmt := sdk.TokensFromConsensusPower(200, sdk.DefaultPowerReduction) 40 initCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt)) 41 42 // add coins to the accounts 43 for _, account := range accounts { 44 err := simapp.FundAccount(suite.app, suite.ctx, account.Address, initCoins) 45 suite.Require().NoError(err) 46 } 47 48 return accounts 49 } 50 51 func (suite *SimTestSuite) TestWeightedOperations() { 52 app, ctx := suite.app, suite.ctx 53 require := suite.Require() 54 55 ctx.WithChainID("test-chain") 56 57 cdc := app.AppCodec() 58 appParams := make(simtypes.AppParams) 59 60 weightedOps := simulation.WeightedOperations( 61 appParams, cdc, app.AccountKeeper, 62 app.BankKeeper, app.FeeGrantKeeper, 63 ) 64 65 s := rand.NewSource(1) 66 r := rand.New(s) 67 accs := suite.getTestingAccounts(r, 3) 68 69 expected := []struct { 70 weight int 71 opMsgRoute string 72 opMsgName string 73 }{ 74 { 75 simappparams.DefaultWeightGrantAllowance, 76 feegrant.ModuleName, 77 simulation.TypeMsgGrantAllowance, 78 }, 79 { 80 simappparams.DefaultWeightRevokeAllowance, 81 feegrant.ModuleName, 82 simulation.TypeMsgRevokeAllowance, 83 }, 84 } 85 86 for i, w := range weightedOps { 87 operationMsg, _, _ := w.Op()(r, app.BaseApp, ctx, accs, ctx.ChainID()) 88 // the following checks are very much dependent from the ordering of the output given 89 // by WeightedOperations. if the ordering in WeightedOperations changes some tests 90 // will fail 91 require.Equal(expected[i].weight, w.Weight(), "weight should be the same") 92 require.Equal(expected[i].opMsgRoute, operationMsg.Route, "route should be the same") 93 require.Equal(expected[i].opMsgName, operationMsg.Name, "operation Msg name should be the same") 94 } 95 } 96 97 func (suite *SimTestSuite) TestSimulateMsgGrantAllowance() { 98 app, ctx := suite.app, suite.ctx 99 require := suite.Require() 100 101 s := rand.NewSource(1) 102 r := rand.New(s) 103 accounts := suite.getTestingAccounts(r, 3) 104 105 // begin a new block 106 app.BeginBlock(ocabci.RequestBeginBlock{Header: tmproto.Header{Height: app.LastBlockHeight() + 1, AppHash: app.LastCommitID().Hash}}) 107 108 // execute operation 109 op := simulation.SimulateMsgGrantAllowance(app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper) 110 operationMsg, futureOperations, err := op(r, app.BaseApp, ctx, accounts, "") 111 require.NoError(err) 112 113 var msg feegrant.MsgGrantAllowance 114 err = suite.app.LegacyAmino().UnmarshalJSON(operationMsg.Msg, &msg) 115 require.NoError(err) 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 err = suite.app.LegacyAmino().UnmarshalJSON(operationMsg.Msg, &msg) 158 require.NoError(err) 159 160 require.True(operationMsg.OK) 161 require.Equal(granter.Address.String(), msg.Granter) 162 require.Equal(grantee.Address.String(), msg.Grantee) 163 require.Len(futureOperations, 0) 164 } 165 166 func TestSimTestSuite(t *testing.T) { 167 suite.Run(t, new(SimTestSuite)) 168 }