Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update sims operations to use baseapp #4946

Merged
merged 84 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
f22313a
update operations to use baseapp
fedekunze Aug 22, 2019
8f1240c
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into fe…
fedekunze Aug 26, 2019
5a31d65
updates and cleanup operations
fedekunze Aug 26, 2019
89cf7b9
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into fe…
fedekunze Aug 27, 2019
5f606ea
update operations
fedekunze Aug 27, 2019
d450ebb
restructure sim ops params
fedekunze Aug 27, 2019
abb288f
merge master
fedekunze Aug 27, 2019
51bf12b
merge master
fedekunze Aug 28, 2019
fe3f6f3
rename sim /operations/msg.go to /operations.go
fedekunze Aug 28, 2019
f3a398b
move GenTx to a helper pkg to avoid circle deps
fedekunze Aug 28, 2019
666a301
rm msg.ValidateBasic
fedekunze Aug 28, 2019
1bb0adc
changelog
fedekunze Aug 28, 2019
b300de0
random fees; delete auth's DeductFees sim operation
fedekunze Aug 29, 2019
bcf6796
add chain-id for sig verification
fedekunze Aug 29, 2019
bb47279
Update x/simulation/account.go
fedekunze Aug 29, 2019
b52a122
fix bank, gov and distr errors
fedekunze Aug 29, 2019
08af7df
Merge branch 'fedekunze/4935-sim-ops-baseapp' of https://github.com/c…
fedekunze Aug 29, 2019
a0a4105
fix staking and slashing errors; increase prob for send enabled
fedekunze Aug 29, 2019
0d33e53
increase gas x10
fedekunze Aug 29, 2019
d2d8e06
make format
fedekunze Aug 29, 2019
882b529
fix some distr and staking edge cases
fedekunze Aug 29, 2019
86798bd
fix all edge cases
fedekunze Aug 30, 2019
8a5a8f7
golang ci
fedekunze Aug 30, 2019
d2433c8
rename acc vars; default no fees to 0stake
fedekunze Aug 30, 2019
0172569
cleanup; check for exchange rate and skip invalid ops
fedekunze Aug 30, 2019
2a2c369
fixes
fedekunze Aug 30, 2019
50f6959
check for max entries
fedekunze Aug 30, 2019
309348a
add pubkey to genaccounts
fedekunze Aug 30, 2019
4e2d3b3
fix gov bug
fedekunze Sep 2, 2019
e662201
update staking sim ops
fedekunze Sep 2, 2019
f1c3e12
fix small redelegation error
fedekunze Sep 2, 2019
3298d5d
fix small self delegation on unjail
fedekunze Sep 2, 2019
8501356
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into fe…
fedekunze Sep 2, 2019
e615ee1
rm inf loop on random val/accs
fedekunze Sep 2, 2019
9e81756
copy array
fedekunze Sep 2, 2019
490bc77
add ok boolean to RandomValidator return values
fedekunze Sep 2, 2019
2139e75
format
fedekunze Sep 2, 2019
7da6f34
Update x/bank/simulation/operations.go
fedekunze Sep 4, 2019
bd7ed37
Update simapp/helpers/test_helpers.go
fedekunze Sep 4, 2019
59620ed
address @colin-axner comments
fedekunze Sep 4, 2019
b2b4a63
add genaccount pubkey validation
fedekunze Sep 4, 2019
c739839
fix test
fedekunze Sep 4, 2019
8d5a90e
update operations and move RandomFees to x/simulation
fedekunze Sep 5, 2019
52875e5
update gov ops
fedekunze Sep 5, 2019
ae94071
merge master
fedekunze Sep 5, 2019
c49543f
address @alexanderbez comments
fedekunze Sep 6, 2019
86503a9
avoid modifications to config
fedekunze Sep 6, 2019
496fa15
reorder params
fedekunze Sep 6, 2019
337a25c
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into fe…
fedekunze Sep 9, 2019
efcab1d
changelog
fedekunze Sep 9, 2019
d4df4bf
Update x/distribution/simulation/genesis.go
fedekunze Sep 11, 2019
4f9961a
merge master
fedekunze Sep 24, 2019
8396254
Merge branch 'fedekunze/4935-sim-ops-baseapp' of https://github.com/c…
fedekunze Sep 24, 2019
a188a77
remove named return values
fedekunze Sep 24, 2019
ea3594b
ensure all operations are simulated
fedekunze Sep 25, 2019
f39fc77
Merge branch 'master' into fedekunze/4935-sim-ops-baseapp
fedekunze Sep 25, 2019
c79bc9a
golangci
fedekunze Sep 25, 2019
5491db8
Merge branch 'fedekunze/4935-sim-ops-baseapp' of https://github.com/c…
fedekunze Sep 25, 2019
c9faeeb
add nolint
fedekunze Sep 25, 2019
b310b12
disable whitespace and funlen linter
fedekunze Sep 25, 2019
446179d
disable godox
fedekunze Sep 25, 2019
42eebc3
add TODO on unjail
fedekunze Sep 25, 2019
272e08a
update ops weights
fedekunze Sep 25, 2019
5a3712d
address @alexanderbez comments
fedekunze Sep 30, 2019
c45382b
remove dup
fedekunze Sep 30, 2019
efd2edb
update godoc
fedekunze Sep 30, 2019
daa8206
Merge branch 'master' into fedekunze/4935-sim-ops-baseapp
fedekunze Sep 30, 2019
def149f
x/slashing/simulation/operations.go linting
alexanderbez Sep 30, 2019
57bcfbb
x/staking/simulation/operations.go linting
alexanderbez Sep 30, 2019
5719686
update operations format
fedekunze Oct 1, 2019
0bf30c3
Merge branch 'master' into fedekunze/4935-sim-ops-baseapp
fedekunze Oct 1, 2019
7c3314e
x/bank/simulation/operations.go linting
alexanderbez Oct 1, 2019
4a24fb8
x/distribution/simulation/operations.go linting
alexanderbez Oct 2, 2019
a15b55f
x/staking/simulation/operations.go linting
alexanderbez Oct 2, 2019
95f0b04
merge master
fedekunze Oct 15, 2019
42fd548
start changes: make bank simulate send multiple coins, code cleanup
AdityaSripal Oct 17, 2019
0c65eda
fix nondeterminism bug
AdityaSripal Oct 17, 2019
cf215f8
fix txsiglimit err
AdityaSripal Oct 17, 2019
99baf18
fix multisend bug
AdityaSripal Oct 17, 2019
33ee0e0
simplify simulation, cleanup opt privkey args
AdityaSripal Oct 18, 2019
bc1027d
make slashing test invalid unjail msgs
AdityaSripal Oct 18, 2019
202fe2b
Merge branch 'master' into fedekunze/4935-sim-ops-baseapp
alexanderbez Oct 22, 2019
716d00d
Update simapp/state.go
alexanderbez Oct 22, 2019
6ed2005
golangCI changes
fedekunze Oct 23, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions simapp/test_helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package simapp

import (
"math/rand"
"os"
"testing"

Expand All @@ -15,6 +16,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/genaccounts"
"github.com/cosmos/cosmos-sdk/x/simulation"
)

// Setup initializes a new SimApp. A Nop logger is set in SimApp.
Expand Down Expand Up @@ -86,7 +88,12 @@ func GenTx(msgs []sdk.Msg, accnums []uint64, seq []uint64, priv ...crypto.PrivKe
}

sigs := make([]auth.StdSignature, len(priv))
memo := "testmemotestmemo"

// create a random length memo
seed := rand.Int63()
r := rand.New(rand.NewSource(seed))

memo := simulation.RandStringOfLength(r, simulation.RandIntBetween(r, 0, 140))

for i, p := range priv {
// use a empty chainID for ease of testing
Expand All @@ -112,7 +119,7 @@ func SignCheckDeliver(
t *testing.T, cdc *codec.Codec, app *bam.BaseApp, header abci.Header, msgs []sdk.Msg,
accNums, seq []uint64, expSimPass, expPass bool, priv ...crypto.PrivKey,
) sdk.Result {

tx := GenTx(msgs, accNums, seq, priv...)

txBytes, err := cdc.MarshalBinaryLengthPrefixed(tx)
Expand Down
159 changes: 58 additions & 101 deletions x/bank/simulation/operations/msgs.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
package operations

import (
"fmt"
"errors"
"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/bank"
"github.com/cosmos/cosmos-sdk/x/bank/internal/keeper"
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
"github.com/cosmos/cosmos-sdk/x/mock"
"github.com/cosmos/cosmos-sdk/x/simulation"
)

// SimulateMsgSend tests and runs a single msg send where both
// accounts already exist.
func SimulateMsgSend(mapper types.AccountKeeper, bk bank.Keeper) simulation.Operation {
handler := bank.NewHandler(bk)
func SimulateMsgSend(ak types.AccountKeeper, bk keeper.Keeper) simulation.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account) (
opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) {

fromAcc, comment, msg, ok := createMsgSend(r, ctx, accs, mapper)
opMsg = simulation.NewOperationMsg(msg, ok, comment)
fromAcc, comment, msg, ok := createMsgSend(r, ctx, accs, ak)
if !ok {
return opMsg, nil, nil
return simulation.NoOpMsg(types.ModuleName), nil, errors.New(comment)
}
err = sendAndVerifyMsgSend(app, mapper, msg, ctx, []crypto.PrivKey{fromAcc.PrivKey}, handler)

err = sendAndVerifyMsgSend(app, ak, msg, ctx, []crypto.PrivKey{fromAcc.PrivKey}, handler)
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return opMsg, nil, err
return simulation.NoOpMsg(types.ModuleName), nil, err
}
return opMsg, nil, nil

return simulation.NewOperationMsg(msg, true, comment), nil, nil
}
}

func createMsgSend(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, mapper types.AccountKeeper) (
func createMsgSend(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, ak types.AccountKeeper) (
fromAcc simulation.Account, comment string, msg types.MsgSend, ok bool) {

fromAcc = simulation.RandomAcc(r, accs)
Expand All @@ -46,15 +46,15 @@ func createMsgSend(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, map
}
toAcc = simulation.RandomAcc(r, accs)
}
initFromCoins := mapper.GetAccount(ctx, fromAcc.Address).SpendableCoins(ctx.BlockHeader().Time)
initFromCoins := ak.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 := simulation.RandPositiveInt(r, initFromCoins[denomIndex].Amount)
if goErr != nil {
amt, err := simulation.RandPositiveInt(r, initFromCoins[denomIndex].Amount)
if err != nil {
return fromAcc, "skipping bank send due to account having no coins of denomination " + initFromCoins[denomIndex].Denom, msg, false
}

Expand All @@ -64,92 +64,69 @@ func createMsgSend(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, map
}

// Sends and verifies the transition of a msg send.
func sendAndVerifyMsgSend(app *baseapp.BaseApp, mapper types.AccountKeeper, msg types.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 == types.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)
func sendAndVerifyMsgSend(app *baseapp.BaseApp, ak types.AccountKeeper, msg types.MsgSend, ctx sdk.Context,
privkeys []crypto.PrivKey) error {
fromAcc := ak.GetAccount(ctx, msg.FromAddress)
accountNumbers := []uint64{fromAcc.GetAccountNumber()}
sequenceNumbers := []uint64{fromAcc.GetSequence()}

if !initialFromAddrCoins.Sub(msg.Amount).IsEqual(fromAcc.GetCoins()) {
return fmt.Errorf("fromAddress %s had an incorrect amount of coins", fromAcc.GetAddress())
}
tx := mock.GenTx([]sdk.Msg{msg},
accountNumbers,
sequenceNumbers,
privkeys...)

if !initialToAddrCoins.Add(msg.Amount).IsEqual(toAcc.GetCoins()) {
return fmt.Errorf("toAddress %s had an incorrect amount of coins", toAcc.GetAddress())
res := app.Deliver(tx)
if !res.IsOK() {
return errors.New(res.Log)
}

return nil
}

// SimulateSingleInputMsgMultiSend tests and runs a single msg multisend, with one input and one output, where both
// accounts already exist.
func SimulateSingleInputMsgMultiSend(mapper types.AccountKeeper, bk bank.Keeper) simulation.Operation {
handler := bank.NewHandler(bk)
func SimulateSingleInputMsgMultiSend(ak types.AccountKeeper, bk keeper.Keeper) simulation.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account) (
opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) {

fromAcc, comment, msg, ok := createSingleInputMsgMultiSend(r, ctx, accs, mapper)
opMsg = simulation.NewOperationMsg(msg, ok, comment)
fromAcc, comment, msg, ok := createSingleInputMsgMultiSend(r, ctx, accs, ak)
if !ok {
return opMsg, nil, nil
return simulation.NoOpMsg(types.ModuleName), nil, errors.New(comment)
}
err = sendAndVerifyMsgMultiSend(app, mapper, msg, ctx, []crypto.PrivKey{fromAcc.PrivKey}, handler)

err = sendAndVerifyMsgMultiSend(app, ak, msg, ctx, []crypto.PrivKey{fromAcc.PrivKey})
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return opMsg, nil, err
return simulation.NoOpMsg(types.ModuleName), nil, err
}
return opMsg, nil, nil

return simulation.NewOperationMsg(msg, ok, comment), nil, nil
}
}

func createSingleInputMsgMultiSend(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, mapper types.AccountKeeper) (
func createSingleInputMsgMultiSend(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, ak types.AccountKeeper) (
fromAcc simulation.Account, comment string, msg types.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)
initFromCoins := ak.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 := simulation.RandPositiveInt(r, initFromCoins[denomIndex].Amount)
if goErr != nil {
amt, err := simulation.RandPositiveInt(r, initFromCoins[denomIndex].Amount)
if err != nil {
return fromAcc, "skipping bank send due to account having no coins of denomination " + initFromCoins[denomIndex].Denom, msg, false
}

Expand All @@ -158,61 +135,41 @@ func createSingleInputMsgMultiSend(r *rand.Rand, ctx sdk.Context, accs []simulat
Inputs: []types.Input{types.NewInput(fromAcc.Address, coins)},
Outputs: []types.Output{types.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 types.AccountKeeper, msg types.MsgMultiSend,
func sendAndVerifyMsgMultiSend(app *baseapp.BaseApp, ak types.AccountKeeper, msg types.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))
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()
acc := ak.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)
acc := ak.GetAccount(ctx, msg.Outputs[i].Address)
initialOutputAddrCoins[i] = acc.GetCoins()
}
if handler != nil {
res := handler(ctx, msg)
if !res.IsOK() {
if res.Code == types.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)
}
tx := mock.GenTx([]sdk.Msg{msg},
accountNumbers,
sequenceNumbers,
privkeys...)

res := app.Deliver(tx)
if !res.IsOK() {
return errors.New(res.Log)
}

return nil
}
Loading