-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathoperations_test.go
More file actions
216 lines (176 loc) · 7.11 KB
/
operations_test.go
File metadata and controls
216 lines (176 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package simulation_test
import (
"math/rand"
"testing"
"time"
"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/authz"
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
"github.com/cosmos/cosmos-sdk/x/authz/simulation"
"github.com/cosmos/cosmos-sdk/x/authz/testutil"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktestutil "github.com/cosmos/cosmos-sdk/x/bank/testutil"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)
type SimTestSuite struct {
suite.Suite
ctx sdk.Context
app *runtime.App
legacyAmino *codec.LegacyAmino
codec codec.Codec
interfaceRegistry codectypes.InterfaceRegistry
accountKeeper authkeeper.AccountKeeper
bankKeeper bankkeeper.Keeper
authzKeeper authzkeeper.Keeper
}
func (suite *SimTestSuite) SetupTest() {
app, err := simtestutil.Setup(
testutil.AppConfig,
&suite.legacyAmino,
&suite.codec,
&suite.interfaceRegistry,
&suite.accountKeeper,
&suite.bankKeeper,
&suite.authzKeeper,
)
suite.Require().NoError(err)
suite.app = app
suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{})
}
func (suite *SimTestSuite) TestWeightedOperations() {
cdc := suite.codec
appParams := make(simtypes.AppParams)
weightedOps := simulation.WeightedOperations(suite.interfaceRegistry, appParams, cdc, suite.accountKeeper,
suite.bankKeeper, suite.authzKeeper)
s := rand.NewSource(3)
r := rand.New(s)
// setup 2 accounts
accs := suite.getTestingAccounts(r, 2)
expected := []struct {
weight int
opMsgRoute string
opMsgName string
}{
{simulation.WeightGrant, authz.ModuleName, simulation.TypeMsgGrant},
{simulation.WeightExec, authz.ModuleName, simulation.TypeMsgExec},
{simulation.WeightRevoke, authz.ModuleName, simulation.TypeMsgRevoke},
}
require := suite.Require()
for i, w := range weightedOps {
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. %v", op.Comment)
require.Equal(expected[i].opMsgRoute, op.Route, "route should be the same. %v", op.Comment)
require.Equal(expected[i].opMsgName, op.Name, "operation Msg name should be the same %v", op.Comment)
}
}
func (suite *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Account {
accounts := simtypes.RandomAccounts(r, n)
initAmt := sdk.TokensFromConsensusPower(200000, sdk.DefaultPowerReduction)
initCoins := sdk.NewCoins(sdk.NewCoin("stake", initAmt))
// add coins to the accounts
for _, account := range accounts {
acc := suite.accountKeeper.NewAccountWithAddress(suite.ctx, account.Address)
suite.accountKeeper.SetAccount(suite.ctx, acc)
suite.Require().NoError(banktestutil.FundAccount(suite.bankKeeper, suite.ctx, account.Address, initCoins))
}
return accounts
}
func (suite *SimTestSuite) TestSimulateGrant() {
s := rand.NewSource(1)
r := rand.New(s)
accounts := suite.getTestingAccounts(r, 2)
blockTime := time.Now().UTC()
ctx := suite.ctx.WithBlockTime(blockTime)
// begin a new block
suite.app.BeginBlock(abci.RequestBeginBlock{
Header: tmproto.Header{
Height: suite.app.LastBlockHeight() + 1,
AppHash: suite.app.LastCommitID().Hash,
},
})
granter := accounts[0]
grantee := accounts[1]
// execute operation
op := simulation.SimulateMsgGrant(codec.NewProtoCodec(suite.interfaceRegistry), suite.accountKeeper, suite.bankKeeper, suite.authzKeeper)
operationMsg, futureOperations, err := op(r, suite.app.BaseApp, ctx, accounts, "")
suite.Require().NoError(err)
var msg authz.MsgGrant
suite.legacyAmino.UnmarshalJSON(operationMsg.Msg, &msg)
suite.Require().True(operationMsg.OK)
suite.Require().Equal(granter.Address.String(), msg.Granter)
suite.Require().Equal(grantee.Address.String(), msg.Grantee)
suite.Require().Len(futureOperations, 0)
}
func (suite *SimTestSuite) TestSimulateRevoke() {
// setup 3 accounts
s := rand.NewSource(2)
r := rand.New(s)
accounts := suite.getTestingAccounts(r, 3)
// begin a new block
suite.app.BeginBlock(abci.RequestBeginBlock{
Header: tmproto.Header{
Height: suite.app.LastBlockHeight() + 1,
AppHash: suite.app.LastCommitID().Hash,
},
})
initAmt := sdk.TokensFromConsensusPower(200000, sdk.DefaultPowerReduction)
initCoins := sdk.NewCoins(sdk.NewCoin("stake", initAmt))
granter := accounts[0]
grantee := accounts[1]
a := banktypes.NewSendAuthorization(initCoins, nil)
expire := time.Now().Add(30 * time.Hour)
err := suite.authzKeeper.SaveGrant(suite.ctx, grantee.Address, granter.Address, a, &expire)
suite.Require().NoError(err)
// execute operation
op := simulation.SimulateMsgRevoke(codec.NewProtoCodec(suite.interfaceRegistry), suite.accountKeeper, suite.bankKeeper, suite.authzKeeper)
operationMsg, futureOperations, err := op(r, suite.app.BaseApp, suite.ctx, accounts, "")
suite.Require().NoError(err)
var msg authz.MsgRevoke
suite.legacyAmino.UnmarshalJSON(operationMsg.Msg, &msg)
suite.Require().True(operationMsg.OK)
suite.Require().Equal(granter.Address.String(), msg.Granter)
suite.Require().Equal(grantee.Address.String(), msg.Grantee)
suite.Require().Equal(banktypes.SendAuthorization{}.MsgTypeURL(), msg.MsgTypeUrl)
suite.Require().Len(futureOperations, 0)
}
func (suite *SimTestSuite) TestSimulateExec() {
// setup 3 accounts
s := rand.NewSource(1)
r := rand.New(s)
accounts := suite.getTestingAccounts(r, 3)
// begin a new block
suite.app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: suite.app.LastBlockHeight() + 1, AppHash: suite.app.LastCommitID().Hash}})
initAmt := sdk.TokensFromConsensusPower(200000, sdk.DefaultPowerReduction)
initCoins := sdk.NewCoins(sdk.NewCoin("stake", initAmt))
granter := accounts[0]
grantee := accounts[1]
a := banktypes.NewSendAuthorization(initCoins, nil)
expire := suite.ctx.BlockTime().Add(1 * time.Hour)
err := suite.authzKeeper.SaveGrant(suite.ctx, grantee.Address, granter.Address, a, &expire)
suite.Require().NoError(err)
// execute operation
op := simulation.SimulateMsgExec(codec.NewProtoCodec(suite.interfaceRegistry), suite.accountKeeper, suite.bankKeeper, suite.authzKeeper, suite.codec)
operationMsg, futureOperations, err := op(r, suite.app.BaseApp, suite.ctx, accounts, "")
suite.Require().NoError(err)
var msg authz.MsgExec
suite.legacyAmino.UnmarshalJSON(operationMsg.Msg, &msg)
suite.Require().True(operationMsg.OK)
suite.Require().Equal(grantee.Address.String(), msg.Grantee)
suite.Require().Len(futureOperations, 0)
}
func TestSimTestSuite(t *testing.T) {
suite.Run(t, new(SimTestSuite))
}