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

R4R: Simulation Refactor #3819

Merged
merged 21 commits into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
operation messges
int
  • Loading branch information
rigelrozanski committed Mar 7, 2019
commit 0a6ea3b9066884219a6216bc2b04307cbe601097
15 changes: 8 additions & 7 deletions x/auth/simulation/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ import (
func SimulateDeductFee(m auth.AccountKeeper, f auth.FeeCollectionKeeper) simulation.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simulation.Account, event func(string)) (
action string, ok bool, fOp []simulation.FutureOperation, err error) {
opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) {

account := simulation.RandomAcc(r, accs)
stored := m.GetAccount(ctx, account.Address)
initCoins := stored.GetCoins()
opMsg = simulation.NewOperationMsgBasic("auth", "deduct_fee", "", false, nil)

if len(initCoins) == 0 {
event(fmt.Sprintf("auth/SimulateDeductFee/false"))
return action, false, nil, nil
return opMsg, nil, nil
}

denomIndex := r.Intn(len(initCoins))
Expand All @@ -33,7 +34,7 @@ func SimulateDeductFee(m auth.AccountKeeper, f auth.FeeCollectionKeeper) simulat
amt, err := randPositiveInt(r, randCoin.Amount)
if err != nil {
event(fmt.Sprintf("auth/SimulateDeductFee/false"))
return action, false, nil, nil
return opMsg, nil, nil
}

// Create a random fee and verify the fees are within the account's spendable
Expand All @@ -42,14 +43,14 @@ func SimulateDeductFee(m auth.AccountKeeper, f auth.FeeCollectionKeeper) simulat
spendableCoins := stored.SpendableCoins(ctx.BlockHeader().Time)
if _, hasNeg := spendableCoins.SafeSub(fees); hasNeg {
event(fmt.Sprintf("auth/SimulateDeductFee/false"))
return action, false, nil, nil
return opMsg, nil, nil
}

// get the new account balance
newCoins, hasNeg := initCoins.SafeSub(fees)
if hasNeg {
event(fmt.Sprintf("auth/SimulateDeductFee/false"))
return action, false, nil, nil
return opMsg, nil, nil
}

if err := stored.SetCoins(newCoins); err != nil {
Expand All @@ -60,8 +61,8 @@ func SimulateDeductFee(m auth.AccountKeeper, f auth.FeeCollectionKeeper) simulat
f.AddCollectedFees(ctx, fees)
event(fmt.Sprintf("auth/SimulateDeductFee/true"))

action = "TestDeductFee"
return action, false, nil, nil
opMsg.OK = true
return opMsg, nil, nil
}
}

Expand Down
35 changes: 17 additions & 18 deletions x/bank/simulation/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ import (
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)) (
action string, ok bool, fOps []simulation.FutureOperation, err error) {
opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) {

fromAcc, action, msg, ok := createMsgSend(r, ctx, accs, mapper)
fromAcc, comment, msg, ok := createMsgSend(r, ctx, accs, mapper)
opMsg = simulation.NewOperationMsg(msg, ok, comment)
if !ok {
return action, ok, nil, nil
return opMsg, nil, nil
}
err = sendAndVerifyMsgSend(app, mapper, msg, ctx, []crypto.PrivKey{fromAcc.PrivKey}, handler)
if err != nil {
return "", false, nil, err
return opMsg, nil, err
}
event("bank/sendAndVerifyTxSend/ok")

return action, ok, nil, nil
return opMsg, nil, nil
}
}

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

fromAcc = simulation.RandomAcc(r, accs)
toAcc := simulation.RandomAcc(r, accs)
Expand All @@ -63,9 +63,7 @@ func createMsgSend(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, map

coins := sdk.Coins{sdk.NewCoin(initFromCoins[denomIndex].Denom, amt)}
msg = bank.NewMsgSend(fromAcc.Address, toAcc.Address, coins)
action = fmt.Sprintf("TestMsgSend: ok %v, msg %s", true, msg.GetSignBytes())

return fromAcc, action, msg, true
return fromAcc, "", msg, true
}

// Sends and verifies the transition of a msg send.
Expand Down Expand Up @@ -118,24 +116,26 @@ func sendAndVerifyMsgSend(app *baseapp.BaseApp, mapper auth.AccountKeeper, msg b
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)) (
action string, ok bool, fOps []simulation.FutureOperation, err error) {
opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) {

fromAcc, action, msg, ok := createSingleInputMsgMultiSend(r, ctx, accs, mapper)
fromAcc, comment, msg, ok := createSingleInputMsgMultiSend(r, ctx, accs, mapper)
opMsg = simulation.NewOperationMsg(msg, ok, comment)
if !ok {
return action, ok, nil, nil
return opMsg, nil, nil
}
err = sendAndVerifyMsgMultiSend(app, mapper, msg, ctx, []crypto.PrivKey{fromAcc.PrivKey}, handler)
if err != nil {
return "", false, nil, err
return opMsg, nil, err
}
event("bank/sendAndVerifyMsgMultiSend/ok")

return action, ok, nil, nil
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, action string, msg bank.MsgMultiSend, ok bool) {
fromAcc simulation.Account, comment string, msg bank.MsgMultiSend, ok bool) {

fromAcc = simulation.RandomAcc(r, accs)
toAcc := simulation.RandomAcc(r, accs)
Expand Down Expand Up @@ -164,8 +164,7 @@ func createSingleInputMsgMultiSend(r *rand.Rand, ctx sdk.Context, accs []simulat
Inputs: []bank.Input{bank.NewInput(fromAcc.Address, coins)},
Outputs: []bank.Output{bank.NewOutput(toAddr, coins)},
}
action = fmt.Sprintf("TestMsgMultiSend: ok %v, msg %s", true, msg.GetSignBytes())
return fromAcc, action, msg, true
return fromAcc, "", msg, true
}

// Sends and verifies the transition of a msg multisend. This fails if there are repeated inputs or outputs
Expand Down
42 changes: 18 additions & 24 deletions x/distribution/simulation/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,25 @@ func SimulateMsgSetWithdrawAddress(m auth.AccountKeeper, k distribution.Keeper)
handler := distribution.NewHandler(k)
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simulation.Account, event func(string)) (
action string, ok bool, fOp []simulation.FutureOperation, err error) {
opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) {

accountOrigin := simulation.RandomAcc(r, accs)
accountDestination := simulation.RandomAcc(r, accs)
msg := distribution.NewMsgSetWithdrawAddress(accountOrigin.Address, accountDestination.Address)

if msg.ValidateBasic() != nil {
return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes())
return simulation.NoOpMsg(), nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes())
}

ctx, write := ctx.CacheContext()
result := handler(ctx, msg)
if result.IsOK() {
ok := handler(ctx, msg).IsOK()
if ok {
write()
}

ok = result.IsOK()
event(fmt.Sprintf("distribution/MsgSetWithdrawAddress/%v", ok))

action = fmt.Sprintf("TestMsgSetWithdrawAddress: ok %v, msg %s", ok, msg.GetSignBytes())
return action, ok, nil, nil
opMsg = simulation.NewOperationMsg(msg, ok, "")
return opMsg, nil, nil
}
}

Expand All @@ -45,27 +43,25 @@ func SimulateMsgWithdrawDelegatorReward(m auth.AccountKeeper, k distribution.Kee
handler := distribution.NewHandler(k)
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simulation.Account, event func(string)) (
action string, ok bool, fOp []simulation.FutureOperation, err error) {
opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) {

delegatorAccount := simulation.RandomAcc(r, accs)
validatorAccount := simulation.RandomAcc(r, accs)
msg := distribution.NewMsgWithdrawDelegatorReward(delegatorAccount.Address, sdk.ValAddress(validatorAccount.Address))

if msg.ValidateBasic() != nil {
return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes())
return simulation.NoOpMsg(), nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes())
}

ctx, write := ctx.CacheContext()
result := handler(ctx, msg)
if result.IsOK() {
ok := handler(ctx, msg).IsOK()
if ok {
write()
}

ok = result.IsOK()
event(fmt.Sprintf("distribution/MsgWithdrawDelegatorReward/%v", ok))

action = fmt.Sprintf("TestMsgWithdrawDelegatorReward: ok %v, msg %s", ok, msg.GetSignBytes())
return action, ok, nil, nil
opMsg = simulation.NewOperationMsg(msg, ok, "")
return opMsg, nil, nil
}
}

Expand All @@ -74,25 +70,23 @@ func SimulateMsgWithdrawValidatorCommission(m auth.AccountKeeper, k distribution
handler := distribution.NewHandler(k)
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simulation.Account, event func(string)) (
action string, ok bool, fOp []simulation.FutureOperation, err error) {
opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) {

account := simulation.RandomAcc(r, accs)
msg := distribution.NewMsgWithdrawValidatorCommission(sdk.ValAddress(account.Address))

if msg.ValidateBasic() != nil {
return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes())
return simulation.NoOpMsg(), nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes())
}

ctx, write := ctx.CacheContext()
result := handler(ctx, msg)
if result.IsOK() {
ok := handler(ctx, msg).IsOK()
if ok {
write()
}

ok = result.IsOK()
event(fmt.Sprintf("distribution/MsgWithdrawValidatorCommission/%v", ok))

action = fmt.Sprintf("TestMsgWithdrawValidatorCommission: ok %v, msg %s", ok, msg.GetSignBytes())
return action, ok, nil, nil
opMsg = simulation.NewOperationMsg(msg, ok, "")
return opMsg, nil, nil
}
}
55 changes: 29 additions & 26 deletions x/gov/simulation/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,19 @@ func SimulateSubmittingVotingAndSlashingForProposal(k gov.Keeper) simulation.Ope
statePercentageArray := []float64{1, .9, .75, .4, .15, 0}
curNumVotesState := 1
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) (
action string, ok bool, fOps []simulation.FutureOperation, err error) {
opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) {

// 1) submit proposal now
sender := simulation.RandomAcc(r, accs)
msg, err := simulationCreateMsgSubmitProposal(r, sender)
if err != nil {
return "", false, nil, err
return simulation.NoOpMsg(), nil, err
}
action, ok = simulateHandleMsgSubmitProposal(msg, handler, ctx, event)
ok := simulateHandleMsgSubmitProposal(msg, handler, ctx, event)
opMsg = simulation.NewOperationMsg(msg, ok, "")
// don't schedule votes if proposal failed
if !ok {
return action, ok, nil, nil
return opMsg, nil, nil
}
proposalID := k.GetLastProposalID(ctx)
// 2) Schedule operations for votes
Expand All @@ -71,7 +72,7 @@ func SimulateSubmittingVotingAndSlashingForProposal(k gov.Keeper) simulation.Ope
// TODO: Find a way to check if a validator was slashed other than just checking their balance a block
// before and after.

return action, ok, fops, nil
return opMsg, fops, nil
}
}

Expand All @@ -80,28 +81,28 @@ func SimulateSubmittingVotingAndSlashingForProposal(k gov.Keeper) simulation.Ope
func SimulateMsgSubmitProposal(k gov.Keeper) simulation.Operation {
handler := gov.NewHandler(k)
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) (
action string, ok bool, fOps []simulation.FutureOperation, err error) {
opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) {

sender := simulation.RandomAcc(r, accs)
msg, err := simulationCreateMsgSubmitProposal(r, sender)
if err != nil {
return "", false, nil, err
return simulation.NoOpMsg(), nil, err
}
action, ok = simulateHandleMsgSubmitProposal(msg, handler, ctx, event)
return action, ok, nil, nil
ok := simulateHandleMsgSubmitProposal(msg, handler, ctx, event)
opMsg = simulation.NewOperationMsg(msg, ok, "")
return opMsg, nil, nil
}
}

func simulateHandleMsgSubmitProposal(msg gov.MsgSubmitProposal, handler sdk.Handler, ctx sdk.Context, event func(string)) (action string, ok bool) {
func simulateHandleMsgSubmitProposal(msg gov.MsgSubmitProposal, handler sdk.Handler, ctx sdk.Context, event func(string)) (ok bool) {
ctx, write := ctx.CacheContext()
result := handler(ctx, msg)
ok = result.IsOK()
if ok {
write()
}
event(fmt.Sprintf("gov/MsgSubmitProposal/%v", ok))
action = fmt.Sprintf("TestMsgSubmitProposal: ok %v, msg %s", ok, msg.GetSignBytes())
return
return ok
}

func simulationCreateMsgSubmitProposal(r *rand.Rand, sender simulation.Account) (msg gov.MsgSubmitProposal, err error) {
Expand All @@ -122,26 +123,28 @@ func simulationCreateMsgSubmitProposal(r *rand.Rand, sender simulation.Account)
// SimulateMsgDeposit
func SimulateMsgDeposit(k gov.Keeper) simulation.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) (
action string, ok bool, fOp []simulation.FutureOperation, err error) {
opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) {

acc := simulation.RandomAcc(r, accs)
proposalID, ok := randomProposalID(r, k, ctx)
if !ok {
return "no-operation", false, nil, nil
return simulation.NoOpMsg(), nil, nil
}
deposit := randomDeposit(r)
msg := gov.NewMsgDeposit(acc.Address, proposalID, deposit)
if msg.ValidateBasic() != nil {
return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes())
return simulation.NoOpMsg(), nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes())
}
ctx, write := ctx.CacheContext()
result := gov.NewHandler(k)(ctx, msg)
if result.IsOK() {
write()
}
event(fmt.Sprintf("gov/MsgDeposit/%v", result.IsOK()))
action = fmt.Sprintf("TestMsgDeposit: ok %v, msg %s", result.IsOK(), msg.GetSignBytes())
return action, result.IsOK(), nil, nil
ok = gov.NewHandler(k)(ctx, msg).IsOK()

event(fmt.Sprintf("gov/MsgDeposit/%v", ok))
opMsg = simulation.NewOperationMsg(msg, ok, "")
return opMsg, nil, nil
}
}

Expand All @@ -154,7 +157,7 @@ func SimulateMsgVote(k gov.Keeper) simulation.Operation {
// nolint: unparam
func operationSimulateMsgVote(k gov.Keeper, acc simulation.Account, proposalID uint64) simulation.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, event func(string)) (
action string, ok bool, fOp []simulation.FutureOperation, err error) {
opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) {

if acc.Equals(simulation.Account{}) {
acc = simulation.RandomAcc(r, accs)
Expand All @@ -164,25 +167,25 @@ func operationSimulateMsgVote(k gov.Keeper, acc simulation.Account, proposalID u
var ok bool
proposalID, ok = randomProposalID(r, k, ctx)
if !ok {
return "no-operation", false, nil, nil
return simulation.NoOpMsg(), nil, nil
}
}
option := randomVotingOption(r)

msg := gov.NewMsgVote(acc.Address, proposalID, option)
if msg.ValidateBasic() != nil {
return "", false, nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes())
return simulation.NoOpMsg(), nil, fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes())
}

ctx, write := ctx.CacheContext()
result := gov.NewHandler(k)(ctx, msg)
if result.IsOK() {
ok := gov.NewHandler(k)(ctx, msg).IsOK()
if ok {
write()
}

event(fmt.Sprintf("gov/MsgVote/%v", result.IsOK()))
action = fmt.Sprintf("TestMsgVote: ok %v, msg %s", result.IsOK(), msg.GetSignBytes())
return action, result.IsOK(), nil, nil
event(fmt.Sprintf("gov/MsgVote/%v", ok))
opMsg = simulation.NewOperationMsg(msg, ok, "")
return opMsg, nil, nil
}
}

Expand Down
Loading