Merge PR #3819: Simulation Refactor

This commit is contained in:
frog power 4000
2019-03-14 19:13:15 +01:00
committed by Christopher Goes
parent 48b6b3884f
commit f0d1efa43c
22 changed files with 582 additions and 528 deletions
+10 -15
View File
@@ -2,29 +2,28 @@ package simulation
import (
"errors"
"fmt"
"math/big"
"math/rand"
"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/mock/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
)
// SimulateDeductFee
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, fOp []simulation.FutureOperation, err error) {
accs []simulation.Account) (
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, nil, nil
return opMsg, nil, nil
}
denomIndex := r.Intn(len(initCoins))
@@ -32,8 +31,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, nil, nil
return opMsg, nil, nil
}
// Create a random fee and verify the fees are within the account's spendable
@@ -41,15 +39,13 @@ func SimulateDeductFee(m auth.AccountKeeper, f auth.FeeCollectionKeeper) simulat
fees := sdk.Coins{sdk.NewCoin(randCoin.Denom, amt)}
spendableCoins := stored.SpendableCoins(ctx.BlockHeader().Time)
if _, hasNeg := spendableCoins.SafeSub(fees); hasNeg {
event(fmt.Sprintf("auth/SimulateDeductFee/false"))
return action, 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, nil, nil
return opMsg, nil, nil
}
if err := stored.SetCoins(newCoins); err != nil {
@@ -58,10 +54,9 @@ func SimulateDeductFee(m auth.AccountKeeper, f auth.FeeCollectionKeeper) simulat
m.SetAccount(ctx, stored)
f.AddCollectedFees(ctx, fees)
event(fmt.Sprintf("auth/SimulateDeductFee/true"))
action = "TestDeductFee"
return action, nil, nil
opMsg.OK = true
return opMsg, nil, nil
}
}