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

feat(x/ecocredit): add basket simulations #769

Merged
merged 31 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3c4f955
wip: add simulations
aleem1314 Feb 14, 2022
449f5d0
feat: implement simulate msg-create-basket
aleem1314 Feb 14, 2022
b08d3cf
Merge branch 'release/v2.2.x' into aleem/basket-sims
aleem1314 Feb 15, 2022
83a23d2
feat: add put into basket sim
aleem1314 Feb 15, 2022
8abdf8b
wip: sims
aleem1314 Feb 15, 2022
a5bdfc3
wip: try fix sims
aleem1314 Feb 15, 2022
b7fa656
feat: add msg-take
aleem1314 Feb 15, 2022
1719045
chore: cleanup
aleem1314 Feb 15, 2022
d17301c
chore: inc basket sims gas
aleem1314 Feb 15, 2022
1b08beb
Merge branch 'release/v2.2.x' into aleem/basket-sims
aleem1314 Feb 16, 2022
c4ca68d
add logs
aleem1314 Feb 16, 2022
a15a783
fix error
aleem1314 Feb 16, 2022
b7f0d56
add basket fee check
aleem1314 Feb 16, 2022
ec8c8f9
try fix sims
aleem1314 Feb 17, 2022
adf93eb
Merge branch 'release/v3.0.x' into aleem/basket-sims
aaronc Feb 17, 2022
1996c2f
convert basket decimal to integer using string
aaronc Feb 17, 2022
d1c0eca
Merge branch 'release/v3.0.x' into aleem/basket-sims
aleem1314 Feb 18, 2022
eeb3525
cleanup
aleem1314 Feb 18, 2022
876e156
update app/app.go
aleem1314 Feb 18, 2022
74cbc4d
fix random amt issue
aleem1314 Feb 18, 2022
7d10eb4
fix sim error
aleem1314 Feb 18, 2022
116082e
Merge branch 'release/v3.0.x' into aleem/basket-sims
aleem1314 Feb 18, 2022
ec18dc2
chore: create helpers
aleem1314 Feb 18, 2022
ac7826f
Merge branch 'release/v3.0.x' into aleem/basket-sims
aleem1314 Feb 18, 2022
246ac8d
add date criteria
aleem1314 Feb 18, 2022
fdfa386
Merge branch 'release/v3.0.x' into aleem/basket-sims
robert-zaremba Feb 18, 2022
361a49a
Merge branch 'release/v3.0.x' into aleem/basket-sims
aleem1314 Feb 20, 2022
0e9c364
fix supply invariant
ryanchristo Feb 22, 2022
a5c9594
chore: review changes
aleem1314 Feb 23, 2022
df30c09
Merge branch 'release/v3.0.x' into aleem/basket-sims
aleem1314 Feb 23, 2022
c23a926
Merge branch 'release/v3.0.x' into aleem/basket-sims
robert-zaremba Feb 23, 2022
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
Prev Previous commit
Next Next commit
chore: create helpers
  • Loading branch information
aleem1314 committed Feb 18, 2022
commit ec18dc2678f836e9172b6795925f876328d43861
49 changes: 18 additions & 31 deletions x/ecocredit/simulation/basket/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,12 @@ func SimulateMsgPut(ak ecocredit.AccountKeeper, bk ecocredit.BankKeeper,
if tradableAmount != "0" {
d, err := math.NewPositiveDecFromString(tradableAmount)
if err != nil {
return simtypes.NoOpMsg(ecocredit.ModuleName, TypeMsgPut, err.Error()), nil, err
return simtypes.NoOpMsg(ecocredit.ModuleName, TypeMsgPut, err.Error()), nil, nil
}

dInt, err := d.Int64()
if err != nil {
return simtypes.NoOpMsg(ecocredit.ModuleName, TypeMsgPut, err.Error()), nil, err
return simtypes.NoOpMsg(ecocredit.ModuleName, TypeMsgPut, err.Error()), nil, nil
}

if dInt == 1 {
Expand Down Expand Up @@ -432,35 +432,22 @@ func SimulateMsgTake(ak ecocredit.AccountKeeper, bk ecocredit.BankKeeper,
}

spendable := bk.SpendableCoins(sdkCtx, owner.Address)
fees, err := simtypes.RandomFees(r, sdkCtx, spendable)
if err != nil {
return simtypes.NoOpMsg(ecocredit.ModuleName, TypeMsgTake, "fee error"), nil, err
}

account := ak.GetAccount(sdkCtx, owner.Address)
txGen := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
2000000,
chainID,
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
owner.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(ecocredit.ModuleName, TypeMsgTake, "unable to generate mock tx"), nil, err
}
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
if err != nil {
if strings.Contains(err.Error(), "insufficient funds") {
return simtypes.NoOpMsg(ecocredit.ModuleName, TypeMsgTake, "not enough balance"), nil, nil
}
return simtypes.NoOpMsg(ecocredit.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
}

return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Cdc: nil,
Msg: msg,
MsgType: msg.Type(),
Context: sdkCtx,
SimAccount: owner,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: ecocredit.ModuleName,
CoinsSpentInMsg: spendable,
}

return GenAndDeliverTxWithRandFees(txCtx)
}
}

Expand Down
53 changes: 53 additions & 0 deletions x/ecocredit/simulation/basket/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package basketsims

import (
"math/rand"
"strings"

"github.com/cosmos/cosmos-sdk/simapp/helpers"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"

regentypes "github.com/regen-network/regen-ledger/types"
"github.com/regen-network/regen-ledger/x/ecocredit"
Expand Down Expand Up @@ -50,3 +54,52 @@ func Contains(s []string, e string) bool {
}
return false
}

// GenAndDeliverTxWithRandFees generates a transaction with a random fee and delivers it.
func GenAndDeliverTxWithRandFees(txCtx simulation.OperationInput) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
blushi marked this conversation as resolved.
Show resolved Hide resolved
account := txCtx.AccountKeeper.GetAccount(txCtx.Context, txCtx.SimAccount.Address)
spendable := txCtx.Bankkeeper.SpendableCoins(txCtx.Context, account.GetAddress())

var fees sdk.Coins
var err error

coins, hasNeg := spendable.SafeSub(txCtx.CoinsSpentInMsg)
if hasNeg {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "message doesn't leave room for fees"), nil, err
}

fees, err = simtypes.RandomFees(txCtx.R, txCtx.Context, coins)
if err != nil {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to generate fees"), nil, err
}
return GenAndDeliverTx(txCtx, fees)
}

// GenAndDeliverTx generates a transactions and delivers it.
func GenAndDeliverTx(txCtx simulation.OperationInput, fees sdk.Coins) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
blushi marked this conversation as resolved.
Show resolved Hide resolved
account := txCtx.AccountKeeper.GetAccount(txCtx.Context, txCtx.SimAccount.Address)
tx, err := helpers.GenTx(
txCtx.TxGen,
[]sdk.Msg{txCtx.Msg},
fees,
6000000,
txCtx.Context.ChainID(),
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
txCtx.SimAccount.PrivKey,
)

if err != nil {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to generate mock tx"), nil, err
}

_, _, err = txCtx.App.Deliver(txCtx.TxGen.TxEncoder(), tx)
if err != nil {
if strings.Contains(err.Error(), "insufficient funds") {
return simtypes.NoOpMsg(ecocredit.ModuleName, txCtx.MsgType, "not enough balance"), nil, nil
}
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to deliver tx"), nil, err
}

return simtypes.NewOperationMsg(txCtx.Msg, true, "", txCtx.Cdc), nil, nil
}
63 changes: 8 additions & 55 deletions x/ecocredit/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"math/rand"
"time"

"github.com/cosmos/cosmos-sdk/simapp/helpers"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
Expand Down Expand Up @@ -208,7 +207,7 @@ func SimulateMsgCreateClass(ak ecocredit.AccountKeeper, bk ecocredit.BankKeeper,
CoinsSpentInMsg: spendable,
}

return GenAndDeliverTxWithRandFees(txCtx)
return basketsims.GenAndDeliverTxWithRandFees(txCtx)
}
}

Expand Down Expand Up @@ -271,7 +270,7 @@ func SimulateMsgCreateBatch(ak ecocredit.AccountKeeper, bk ecocredit.BankKeeper,
CoinsSpentInMsg: spendable,
}

return GenAndDeliverTxWithRandFees(txCtx)
return basketsims.GenAndDeliverTxWithRandFees(txCtx)
}
}

Expand Down Expand Up @@ -375,7 +374,7 @@ func SimulateMsgSend(ak ecocredit.AccountKeeper, bk ecocredit.BankKeeper,
CoinsSpentInMsg: spendable,
}

return GenAndDeliverTxWithRandFees(txCtx)
return basketsims.GenAndDeliverTxWithRandFees(txCtx)
}
}

Expand Down Expand Up @@ -454,7 +453,7 @@ func SimulateMsgRetire(ak ecocredit.AccountKeeper, bk ecocredit.BankKeeper,
CoinsSpentInMsg: spendable,
}

return GenAndDeliverTxWithRandFees(txCtx)
return basketsims.GenAndDeliverTxWithRandFees(txCtx)
}
}

Expand Down Expand Up @@ -523,7 +522,7 @@ func SimulateMsgCancel(ak ecocredit.AccountKeeper, bk ecocredit.BankKeeper,
CoinsSpentInMsg: spendable,
}

return GenAndDeliverTxWithRandFees(txCtx)
return basketsims.GenAndDeliverTxWithRandFees(txCtx)
}
}

Expand Down Expand Up @@ -569,7 +568,7 @@ func SimulateMsgUpdateClassAdmin(ak ecocredit.AccountKeeper, bk ecocredit.BankKe
CoinsSpentInMsg: spendable,
}

return GenAndDeliverTxWithRandFees(txCtx)
return basketsims.GenAndDeliverTxWithRandFees(txCtx)
}
}

Expand Down Expand Up @@ -610,7 +609,7 @@ func SimulateMsgUpdateClassMetadata(ak ecocredit.AccountKeeper, bk ecocredit.Ban
CoinsSpentInMsg: spendable,
}

return GenAndDeliverTxWithRandFees(txCtx)
return basketsims.GenAndDeliverTxWithRandFees(txCtx)
}
}

Expand Down Expand Up @@ -652,7 +651,7 @@ func SimulateMsgUpdateClassIssuers(ak ecocredit.AccountKeeper, bk ecocredit.Bank
CoinsSpentInMsg: spendable,
}

return GenAndDeliverTxWithRandFees(txCtx)
return basketsims.GenAndDeliverTxWithRandFees(txCtx)
}
}

Expand Down Expand Up @@ -734,49 +733,3 @@ func generateBatchIssuance(r *rand.Rand, accs []simtypes.Account) []*ecocredit.M

return res
}

// GenAndDeliverTxWithRandFees generates a transaction with a random fee and delivers it.
func GenAndDeliverTxWithRandFees(txCtx simulation.OperationInput) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
account := txCtx.AccountKeeper.GetAccount(txCtx.Context, txCtx.SimAccount.Address)
spendable := txCtx.Bankkeeper.SpendableCoins(txCtx.Context, account.GetAddress())

var fees sdk.Coins
var err error

coins, hasNeg := spendable.SafeSub(txCtx.CoinsSpentInMsg)
if hasNeg {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "message doesn't leave room for fees"), nil, err
}

fees, err = simtypes.RandomFees(txCtx.R, txCtx.Context, coins)
if err != nil {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to generate fees"), nil, err
}
return GenAndDeliverTx(txCtx, fees)
}

// GenAndDeliverTx generates a transactions and delivers it.
func GenAndDeliverTx(txCtx simulation.OperationInput, fees sdk.Coins) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
account := txCtx.AccountKeeper.GetAccount(txCtx.Context, txCtx.SimAccount.Address)
tx, err := helpers.GenTx(
txCtx.TxGen,
[]sdk.Msg{txCtx.Msg},
fees,
10000000,
txCtx.Context.ChainID(),
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
txCtx.SimAccount.PrivKey,
)

if err != nil {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to generate mock tx"), nil, err
}

_, _, err = txCtx.App.Deliver(txCtx.TxGen.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to deliver tx"), nil, err
}

return simtypes.NewOperationMsg(txCtx.Msg, true, "", txCtx.Cdc), nil, nil
}