2018-07-18 00:05:48 -07:00
|
|
|
package simulation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"math/rand"
|
|
|
|
|
2018-12-10 06:27:25 -08:00
|
|
|
"github.com/tendermint/tendermint/crypto"
|
|
|
|
|
2018-07-18 00:05:48 -07:00
|
|
|
"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/mock/simulation"
|
|
|
|
)
|
|
|
|
|
2018-10-02 05:01:18 -07:00
|
|
|
// SingleInputSendTx tests and runs a single msg send w/ auth, with one input and one output, where both
|
2018-07-18 00:05:48 -07:00
|
|
|
// accounts already exist.
|
2018-10-20 23:22:48 -07:00
|
|
|
func SingleInputSendTx(mapper auth.AccountKeeper) simulation.Operation {
|
2018-09-22 20:20:53 -07:00
|
|
|
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) (action string, fOps []simulation.FutureOperation, err error) {
|
2018-10-02 05:01:18 -07:00
|
|
|
fromAcc, action, msg, abort := createSingleInputSendMsg(r, ctx, accs, mapper)
|
|
|
|
if abort {
|
|
|
|
return action, nil, nil
|
2018-07-18 00:05:48 -07:00
|
|
|
}
|
2018-10-02 05:01:18 -07:00
|
|
|
err = sendAndVerifyMsgSend(app, mapper, msg, ctx, []crypto.PrivKey{fromAcc.PrivKey}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
2018-07-18 00:05:48 -07:00
|
|
|
}
|
2018-10-02 05:01:18 -07:00
|
|
|
event("bank/sendAndVerifyTxSend/ok")
|
2018-07-18 00:05:48 -07:00
|
|
|
|
2018-10-02 05:01:18 -07:00
|
|
|
return action, nil, nil
|
|
|
|
}
|
|
|
|
}
|
2018-07-18 00:05:48 -07:00
|
|
|
|
2018-10-02 05:01:18 -07:00
|
|
|
// SingleInputSendMsg tests and runs a single msg send, with one input and one output, where both
|
|
|
|
// accounts already exist.
|
2018-10-20 23:22:48 -07:00
|
|
|
func SingleInputSendMsg(mapper auth.AccountKeeper, bk bank.Keeper) simulation.Operation {
|
2018-10-02 05:01:18 -07:00
|
|
|
handler := bank.NewHandler(bk)
|
|
|
|
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) (action string, fOps []simulation.FutureOperation, err error) {
|
|
|
|
fromAcc, action, msg, abort := createSingleInputSendMsg(r, ctx, accs, mapper)
|
|
|
|
if abort {
|
|
|
|
return action, nil, nil
|
2018-07-18 00:05:48 -07:00
|
|
|
}
|
2018-10-02 05:01:18 -07:00
|
|
|
err = sendAndVerifyMsgSend(app, mapper, msg, ctx, []crypto.PrivKey{fromAcc.PrivKey}, handler)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
2018-09-09 08:34:09 -07:00
|
|
|
}
|
2018-07-18 00:05:48 -07:00
|
|
|
event("bank/sendAndVerifyMsgSend/ok")
|
|
|
|
|
2018-08-27 14:27:00 -07:00
|
|
|
return action, nil, nil
|
2018-07-18 00:05:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 23:22:48 -07:00
|
|
|
func createSingleInputSendMsg(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, mapper auth.AccountKeeper) (fromAcc simulation.Account, action string, msg bank.MsgSend, abort bool) {
|
2018-10-02 05:01:18 -07:00
|
|
|
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
|
2019-01-17 08:15:13 -08:00
|
|
|
initFromCoins := mapper.GetAccount(ctx, fromAcc.Address).SpendableCoins(ctx.BlockHeader().Time)
|
2018-10-02 05:01:18 -07:00
|
|
|
|
|
|
|
if len(initFromCoins) == 0 {
|
|
|
|
return fromAcc, "skipping, no coins at all", msg, true
|
|
|
|
}
|
|
|
|
|
|
|
|
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, true
|
|
|
|
}
|
|
|
|
|
|
|
|
action = fmt.Sprintf("%s is sending %s %s to %s",
|
|
|
|
fromAcc.Address.String(),
|
|
|
|
amt.String(),
|
|
|
|
initFromCoins[denomIndex].Denom,
|
|
|
|
toAddr.String(),
|
|
|
|
)
|
|
|
|
|
2018-11-20 01:22:35 -08:00
|
|
|
coins := sdk.Coins{sdk.NewCoin(initFromCoins[denomIndex].Denom, amt)}
|
2018-10-02 05:01:18 -07:00
|
|
|
msg = bank.MsgSend{
|
|
|
|
Inputs: []bank.Input{bank.NewInput(fromAcc.Address, coins)},
|
|
|
|
Outputs: []bank.Output{bank.NewOutput(toAddr, coins)},
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-18 00:05:48 -07:00
|
|
|
// Sends and verifies the transition of a msg send. This fails if there are repeated inputs or outputs
|
2018-10-02 05:01:18 -07:00
|
|
|
// pass in handler as nil to handle txs, otherwise handle msgs
|
2018-10-20 23:22:48 -07:00
|
|
|
func sendAndVerifyMsgSend(app *baseapp.BaseApp, mapper auth.AccountKeeper, msg bank.MsgSend, ctx sdk.Context, privkeys []crypto.PrivKey, handler sdk.Handler) error {
|
2018-07-18 00:05:48 -07:00
|
|
|
initialInputAddrCoins := make([]sdk.Coins, len(msg.Inputs))
|
|
|
|
initialOutputAddrCoins := make([]sdk.Coins, len(msg.Outputs))
|
2018-11-26 03:29:21 -08:00
|
|
|
AccountNumbers := make([]uint64, len(msg.Inputs))
|
|
|
|
SequenceNumbers := make([]uint64, len(msg.Inputs))
|
2018-07-18 00:05:48 -07:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2018-10-02 05:01:18 -07:00
|
|
|
if handler != nil {
|
|
|
|
res := handler(ctx, msg)
|
|
|
|
if !res.IsOK() {
|
2019-01-28 19:06:48 -08:00
|
|
|
if res.Code == bank.CodeSendDisabled {
|
|
|
|
return nil
|
|
|
|
}
|
2018-10-02 05:01:18 -07:00
|
|
|
// 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)
|
|
|
|
}
|
2018-07-18 00:05:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(msg.Inputs); i++ {
|
|
|
|
terminalInputCoins := mapper.GetAccount(ctx, msg.Inputs[i].Address).GetCoins()
|
2018-09-09 08:34:09 -07:00
|
|
|
if !initialInputAddrCoins[i].Minus(msg.Inputs[i].Coins).IsEqual(terminalInputCoins) {
|
|
|
|
return fmt.Errorf("input #%d had an incorrect amount of coins", i)
|
|
|
|
}
|
2018-07-18 00:05:48 -07:00
|
|
|
}
|
|
|
|
for i := 0; i < len(msg.Outputs); i++ {
|
|
|
|
terminalOutputCoins := mapper.GetAccount(ctx, msg.Outputs[i].Address).GetCoins()
|
2018-08-29 23:02:15 -07:00
|
|
|
if !terminalOutputCoins.IsEqual(initialOutputAddrCoins[i].Plus(msg.Outputs[i].Coins)) {
|
2018-09-09 08:34:09 -07:00
|
|
|
return fmt.Errorf("output #%d had an incorrect amount of coins", i)
|
2018-08-29 23:02:15 -07:00
|
|
|
}
|
2018-07-18 00:05:48 -07:00
|
|
|
}
|
2018-09-09 08:34:09 -07:00
|
|
|
return nil
|
2018-07-18 00:05:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|