2019-10-23 02:14:45 -07:00
|
|
|
package simulation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"math/rand"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
2019-12-05 01:29:54 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2019-10-23 02:14:45 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp/helpers"
|
2019-12-05 01:29:54 -08:00
|
|
|
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
|
2019-10-23 02:14:45 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-03-23 04:55:44 -07:00
|
|
|
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
2019-10-23 02:14:45 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/simulation"
|
2020-02-19 16:04:13 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/slashing/keeper"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/slashing/types"
|
2019-10-23 02:14:45 -07:00
|
|
|
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
|
|
|
)
|
|
|
|
|
2019-12-05 01:29:54 -08:00
|
|
|
// Simulation operation weights constants
|
|
|
|
const (
|
|
|
|
OpWeightMsgUnjail = "op_weight_msg_unjail"
|
|
|
|
)
|
|
|
|
|
|
|
|
// WeightedOperations returns all the operations from the module with their respective weights
|
|
|
|
func WeightedOperations(
|
2021-04-29 03:46:22 -07:00
|
|
|
appParams simtypes.AppParams, cdc codec.JSONCodec, ak types.AccountKeeper,
|
2020-01-30 13:31:16 -08:00
|
|
|
bk types.BankKeeper, k keeper.Keeper, sk stakingkeeper.Keeper,
|
2019-12-05 01:29:54 -08:00
|
|
|
) simulation.WeightedOperations {
|
|
|
|
|
|
|
|
var weightMsgUnjail int
|
|
|
|
appParams.GetOrGenerate(cdc, OpWeightMsgUnjail, &weightMsgUnjail, nil,
|
|
|
|
func(_ *rand.Rand) {
|
|
|
|
weightMsgUnjail = simappparams.DefaultWeightMsgUnjail
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return simulation.WeightedOperations{
|
|
|
|
simulation.NewWeightedOperation(
|
|
|
|
weightMsgUnjail,
|
2020-01-30 13:31:16 -08:00
|
|
|
SimulateMsgUnjail(ak, bk, k, sk),
|
2019-12-05 01:29:54 -08:00
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 02:14:45 -07:00
|
|
|
// SimulateMsgUnjail generates a MsgUnjail with random values
|
2020-03-23 04:55:44 -07:00
|
|
|
func SimulateMsgUnjail(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper, sk stakingkeeper.Keeper) simtypes.Operation {
|
2019-10-23 02:14:45 -07:00
|
|
|
return func(
|
|
|
|
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
|
2020-03-23 04:55:44 -07:00
|
|
|
accs []simtypes.Account, chainID string,
|
|
|
|
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
|
2019-10-23 02:14:45 -07:00
|
|
|
|
|
|
|
validator, ok := stakingkeeper.RandomValidator(r, sk, ctx)
|
|
|
|
if !ok {
|
2020-05-26 09:52:23 -07:00
|
|
|
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgUnjail, "validator is not ok"), nil, nil // skip
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
|
2020-03-23 04:55:44 -07:00
|
|
|
simAccount, found := simtypes.FindAccount(accs, sdk.AccAddress(validator.GetOperator()))
|
2019-10-23 02:14:45 -07:00
|
|
|
if !found {
|
2020-05-26 09:52:23 -07:00
|
|
|
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgUnjail, "unable to find account"), nil, nil // skip
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if !validator.IsJailed() {
|
|
|
|
// TODO: due to this condition this message is almost, if not always, skipped !
|
2020-05-26 09:52:23 -07:00
|
|
|
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgUnjail, "validator is not jailed"), nil, nil
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
|
2020-11-09 08:01:43 -08:00
|
|
|
consAddr, err := validator.GetConsAddr()
|
2020-10-23 05:07:52 -07:00
|
|
|
if err != nil {
|
|
|
|
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgUnjail, "unable to get validator consensus key"), nil, err
|
|
|
|
}
|
2019-10-23 02:14:45 -07:00
|
|
|
info, found := k.GetValidatorSigningInfo(ctx, consAddr)
|
|
|
|
if !found {
|
2020-05-26 09:52:23 -07:00
|
|
|
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgUnjail, "unable to find validator signing info"), nil, nil // skip
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
selfDel := sk.Delegation(ctx, simAccount.Address, validator.GetOperator())
|
|
|
|
if selfDel == nil {
|
2020-05-26 09:52:23 -07:00
|
|
|
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgUnjail, "self delegation is nil"), nil, nil // skip
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
account := ak.GetAccount(ctx, sdk.AccAddress(validator.GetOperator()))
|
2020-01-30 13:31:16 -08:00
|
|
|
spendable := bk.SpendableCoins(ctx, account.GetAddress())
|
|
|
|
|
2020-03-23 04:55:44 -07:00
|
|
|
fees, err := simtypes.RandomFees(r, ctx, spendable)
|
2019-10-23 02:14:45 -07:00
|
|
|
if err != nil {
|
2020-05-26 09:52:23 -07:00
|
|
|
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgUnjail, "unable to generate fees"), nil, err
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := types.NewMsgUnjail(validator.GetOperator())
|
|
|
|
|
2020-10-27 04:33:48 -07:00
|
|
|
txGen := simappparams.MakeTestEncodingConfig().TxConfig
|
2020-07-14 10:33:31 -07:00
|
|
|
tx, err := helpers.GenTx(
|
|
|
|
txGen,
|
2019-10-23 02:14:45 -07:00
|
|
|
[]sdk.Msg{msg},
|
|
|
|
fees,
|
2019-12-18 06:48:22 -08:00
|
|
|
helpers.DefaultGenTxGas,
|
2019-10-23 02:14:45 -07:00
|
|
|
chainID,
|
|
|
|
[]uint64{account.GetAccountNumber()},
|
|
|
|
[]uint64{account.GetSequence()},
|
|
|
|
simAccount.PrivKey,
|
|
|
|
)
|
2020-07-14 10:33:31 -07:00
|
|
|
if err != nil {
|
|
|
|
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
|
|
|
|
}
|
2019-10-23 02:14:45 -07:00
|
|
|
|
refactor!: BaseApp {Check,Deliver}Tx with middleware design (#9920)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->
## Description
ref: #9585
This PR if the 1st step (out of 2) in the #9585 refactor. It transforms baseapp's ABCI {Check,Deliver}Tx into middleware stacks. A middleware is defined by the following interfaces:
```go
// types/tx package
type Handler interface {
CheckTx(ctx context.Context, tx sdk.Tx, req abci.RequestCheckTx) (abci.ResponseCheckTx, error)
DeliverTx(ctx context.Context, tx sdk.Tx, req abci.RequestDeliverTx) (abci.ResponseDeliverTx, error)
SimulateTx(ctx context.Context, tx sdk.Tx, req RequestSimulateTx) (ResponseSimulateTx, error)
}
type Middleware func(Handler) Handler
```
This Pr doesn't migrate antehandlers, but only baseapp's runTx and runMsgs as middlewares. It bundles antehandlers into one single middleware (for now).
More specifically, it introduces the 5 following middlewares:
| Middleware | Description |
| ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| RunMsgsTxMiddleware | This middleware replaces the old baseapp's `runMsgs`. |
| LegacyAnteMiddleware | This middleware is temporary, it bundles all antehandlers into one middleware. It's only created for the purpose of breaking the refactor into 2 pieces. **It will be removed in Part 2**, and each antehandler will be replaced by its own middleware. |
| IndexEventsTxMiddleware | This is a simple middleware that chooses which events to index in Tendermint. Replaces `baseapp.indexEvents` (which unfortunately still exists in baseapp too, because it's used to index Begin/EndBlock events) |
| RecoveryTxMiddleware | This index recovers from panics. It replaces baseapp.runTx's panic recovery. |
| GasTxMiddleware | This replaces the [`Setup`](https://github.com/cosmos/cosmos-sdk/blob/master/x/auth/ante/setup.go) Antehandler. It sets a GasMeter on sdk.Context. Note that before, GasMeter was set on sdk.Context inside the antehandlers, and there was some mess around the fact that antehandlers had their own panic recovery system so that the GasMeter could be read by baseapp's recovery system. Now, this mess is all removed: one middleware sets GasMeter, another one handles recovery. |
---
### Author Checklist
*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*
I have...
- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed
### Reviewers Checklist
*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*
I have...
- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2021-08-25 07:40:33 -07:00
|
|
|
_, res, err := app.SimDeliver(txGen.TxEncoder(), tx)
|
2019-10-23 02:14:45 -07:00
|
|
|
|
|
|
|
// result should fail if:
|
|
|
|
// - validator cannot be unjailed due to tombstone
|
|
|
|
// - validator is still in jailed period
|
|
|
|
// - self delegation too low
|
|
|
|
if info.Tombstoned ||
|
|
|
|
ctx.BlockHeader().Time.Before(info.JailedUntil) ||
|
|
|
|
validator.TokensFromShares(selfDel.GetShares()).TruncateInt().LT(validator.GetMinSelfDelegation()) {
|
2019-12-27 09:57:54 -08:00
|
|
|
if res != nil && err == nil {
|
2019-10-23 02:14:45 -07:00
|
|
|
if info.Tombstoned {
|
2021-01-25 08:41:30 -08:00
|
|
|
return simtypes.NewOperationMsg(msg, true, "", nil), nil, errors.New("validator should not have been unjailed if validator tombstoned")
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
if ctx.BlockHeader().Time.Before(info.JailedUntil) {
|
2021-01-25 08:41:30 -08:00
|
|
|
return simtypes.NewOperationMsg(msg, true, "", nil), nil, errors.New("validator unjailed while validator still in jail period")
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
if validator.TokensFromShares(selfDel.GetShares()).TruncateInt().LT(validator.GetMinSelfDelegation()) {
|
2021-01-25 08:41:30 -08:00
|
|
|
return simtypes.NewOperationMsg(msg, true, "", nil), nil, errors.New("validator unjailed even though self-delegation too low")
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// msg failed as expected
|
2021-01-25 08:41:30 -08:00
|
|
|
return simtypes.NewOperationMsg(msg, false, "", nil), nil, nil
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
|
2019-12-27 09:57:54 -08:00
|
|
|
if err != nil {
|
2020-05-26 09:52:23 -07:00
|
|
|
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, errors.New(res.Log)
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
|
2021-01-25 08:41:30 -08:00
|
|
|
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
}
|