-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathmsgs.go
232 lines (202 loc) · 7.78 KB
/
msgs.go
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package simulation
import (
"errors"
"fmt"
"math/big"
"math/rand"
"github.com/tendermint/tendermint/crypto"
"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/mock"
"github.com/cosmos/cosmos-sdk/x/simulation"
)
// SendTx tests and runs a single msg send where both
// accounts already exist.
func SimulateMsgSend(mapper auth.AccountKeeper, bk bank.Keeper) simulation.Operation {
handler := bank.NewHandler(bk)
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) (
opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) {
fromAcc, comment, msg, ok := createMsgSend(r, ctx, accs, mapper)
opMsg = simulation.NewOperationMsg(msg, ok, comment)
if !ok {
return opMsg, nil, nil
}
err = sendAndVerifyMsgSend(app, mapper, msg, ctx, []crypto.PrivKey{fromAcc.PrivKey}, handler)
if err != nil {
return opMsg, nil, err
}
event("bank/sendAndVerifyTxSend/ok")
return opMsg, nil, nil
}
}
func createMsgSend(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, mapper auth.AccountKeeper) (
fromAcc simulation.Account, comment string, msg bank.MsgSend, ok bool) {
fromAcc = simulation.RandomAcc(r, accs)
toAcc := simulation.RandomAcc(r, accs)
// Disallow sending money to yourself
for {
if !fromAcc.PubKey.Equals(toAcc.PubKey) {
break
}
toAcc = simulation.RandomAcc(r, accs)
}
initFromCoins := mapper.GetAccount(ctx, fromAcc.Address).SpendableCoins(ctx.BlockHeader().Time)
if len(initFromCoins) == 0 {
return fromAcc, "skipping, no coins at all", msg, false
}
denomIndex := r.Intn(len(initFromCoins))
amt, goErr := randPositiveInt(r, initFromCoins[denomIndex].Amount)
if goErr != nil {
return fromAcc, "skipping bank send due to account having no coins of denomination " + initFromCoins[denomIndex].Denom, msg, false
}
coins := sdk.Coins{sdk.NewCoin(initFromCoins[denomIndex].Denom, amt)}
msg = bank.NewMsgSend(fromAcc.Address, toAcc.Address, coins)
return fromAcc, "", msg, true
}
// Sends and verifies the transition of a msg send.
func sendAndVerifyMsgSend(app *baseapp.BaseApp, mapper auth.AccountKeeper, msg bank.MsgSend, ctx sdk.Context, privkeys []crypto.PrivKey, handler sdk.Handler) error {
fromAcc := mapper.GetAccount(ctx, msg.FromAddress)
AccountNumbers := []uint64{fromAcc.GetAccountNumber()}
SequenceNumbers := []uint64{fromAcc.GetSequence()}
initialFromAddrCoins := fromAcc.GetCoins()
toAcc := mapper.GetAccount(ctx, msg.ToAddress)
initialToAddrCoins := toAcc.GetCoins()
if handler != nil {
res := handler(ctx, msg)
if !res.IsOK() {
if res.Code == bank.CodeSendDisabled {
return nil
}
// TODO: Do this in a more 'canonical' way
return fmt.Errorf("handling msg failed %v", res)
}
} else {
tx := mock.GenTx([]sdk.Msg{msg},
AccountNumbers,
SequenceNumbers,
privkeys...)
res := app.Deliver(tx)
if !res.IsOK() {
// TODO: Do this in a more 'canonical' way
return fmt.Errorf("Deliver failed %v", res)
}
}
fromAcc = mapper.GetAccount(ctx, msg.FromAddress)
toAcc = mapper.GetAccount(ctx, msg.ToAddress)
if !initialFromAddrCoins.Sub(msg.Amount).IsEqual(fromAcc.GetCoins()) {
return fmt.Errorf("fromAddress %s had an incorrect amount of coins", fromAcc.GetAddress())
}
if !initialToAddrCoins.Add(msg.Amount).IsEqual(toAcc.GetCoins()) {
return fmt.Errorf("toAddress %s had an incorrect amount of coins", toAcc.GetAddress())
}
return nil
}
// SingleInputSendMsg tests and runs a single msg multisend, with one input and one output, where both
// accounts already exist.
func SimulateSingleInputMsgMultiSend(mapper auth.AccountKeeper, bk bank.Keeper) simulation.Operation {
handler := bank.NewHandler(bk)
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) (
opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) {
fromAcc, comment, msg, ok := createSingleInputMsgMultiSend(r, ctx, accs, mapper)
opMsg = simulation.NewOperationMsg(msg, ok, comment)
if !ok {
return opMsg, nil, nil
}
err = sendAndVerifyMsgMultiSend(app, mapper, msg, ctx, []crypto.PrivKey{fromAcc.PrivKey}, handler)
if err != nil {
return opMsg, nil, err
}
event("bank/sendAndVerifyMsgMultiSend/ok")
opMsg = simulation.NewOperationMsg(msg, ok, comment)
return opMsg, nil, nil
}
}
func createSingleInputMsgMultiSend(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, mapper auth.AccountKeeper) (
fromAcc simulation.Account, comment string, msg bank.MsgMultiSend, ok bool) {
fromAcc = simulation.RandomAcc(r, accs)
toAcc := simulation.RandomAcc(r, accs)
// Disallow sending money to yourself
for {
if !fromAcc.PubKey.Equals(toAcc.PubKey) {
break
}
toAcc = simulation.RandomAcc(r, accs)
}
toAddr := toAcc.Address
initFromCoins := mapper.GetAccount(ctx, fromAcc.Address).SpendableCoins(ctx.BlockHeader().Time)
if len(initFromCoins) == 0 {
return fromAcc, "skipping, no coins at all", msg, false
}
denomIndex := r.Intn(len(initFromCoins))
amt, goErr := randPositiveInt(r, initFromCoins[denomIndex].Amount)
if goErr != nil {
return fromAcc, "skipping bank send due to account having no coins of denomination " + initFromCoins[denomIndex].Denom, msg, false
}
coins := sdk.Coins{sdk.NewCoin(initFromCoins[denomIndex].Denom, amt)}
msg = bank.MsgMultiSend{
Inputs: []bank.Input{bank.NewInput(fromAcc.Address, coins)},
Outputs: []bank.Output{bank.NewOutput(toAddr, coins)},
}
return fromAcc, "", msg, true
}
// Sends and verifies the transition of a msg multisend. This fails if there are repeated inputs or outputs
// pass in handler as nil to handle txs, otherwise handle msgs
func sendAndVerifyMsgMultiSend(app *baseapp.BaseApp, mapper auth.AccountKeeper, msg bank.MsgMultiSend,
ctx sdk.Context, privkeys []crypto.PrivKey, handler sdk.Handler) error {
initialInputAddrCoins := make([]sdk.Coins, len(msg.Inputs))
initialOutputAddrCoins := make([]sdk.Coins, len(msg.Outputs))
AccountNumbers := make([]uint64, len(msg.Inputs))
SequenceNumbers := make([]uint64, len(msg.Inputs))
for i := 0; i < len(msg.Inputs); i++ {
acc := mapper.GetAccount(ctx, msg.Inputs[i].Address)
AccountNumbers[i] = acc.GetAccountNumber()
SequenceNumbers[i] = acc.GetSequence()
initialInputAddrCoins[i] = acc.GetCoins()
}
for i := 0; i < len(msg.Outputs); i++ {
acc := mapper.GetAccount(ctx, msg.Outputs[i].Address)
initialOutputAddrCoins[i] = acc.GetCoins()
}
if handler != nil {
res := handler(ctx, msg)
if !res.IsOK() {
if res.Code == bank.CodeSendDisabled {
return nil
}
// TODO: Do this in a more 'canonical' way
return fmt.Errorf("handling msg failed %v", res)
}
} else {
tx := mock.GenTx([]sdk.Msg{msg},
AccountNumbers,
SequenceNumbers,
privkeys...)
res := app.Deliver(tx)
if !res.IsOK() {
// TODO: Do this in a more 'canonical' way
return fmt.Errorf("Deliver failed %v", res)
}
}
for i := 0; i < len(msg.Inputs); i++ {
terminalInputCoins := mapper.GetAccount(ctx, msg.Inputs[i].Address).GetCoins()
if !initialInputAddrCoins[i].Sub(msg.Inputs[i].Coins).IsEqual(terminalInputCoins) {
return fmt.Errorf("input #%d had an incorrect amount of coins", i)
}
}
for i := 0; i < len(msg.Outputs); i++ {
terminalOutputCoins := mapper.GetAccount(ctx, msg.Outputs[i].Address).GetCoins()
if !terminalOutputCoins.IsEqual(initialOutputAddrCoins[i].Add(msg.Outputs[i].Coins)) {
return fmt.Errorf("output #%d had an incorrect amount of coins", i)
}
}
return nil
}
func randPositiveInt(r *rand.Rand, max sdk.Int) (sdk.Int, error) {
if !max.GT(sdk.OneInt()) {
return sdk.Int{}, errors.New("max too small")
}
max = max.Sub(sdk.OneInt())
return sdk.NewIntFromBigInt(new(big.Int).Rand(r, max.BigInt())).Add(sdk.OneInt()), nil
}