refactor: Revert middlewares to antehandlers (part 1/2: baseapp) (#11979)

## Description

We decided to remove middlewares, and revert to antehandlers (exactly like in v045) for this release. A better middleware solution will be implemented after v046.

ref: #11955 

Because this refactor is big, so I decided to cut it into two. This PR is part 1 of 2:
- part 1: Revert baseapp and middlewares to v0.45.4
- part 2: Add posthandler, tips, priority

---
 Suggestion for reviewers:

This PR might still be hard to review though. I think it's easier to actually review the diff between v0.45.4 and this PR:
- `git difftool -d v0.45.4..am/revert-045-baseapp baseapp`
  - most important parts to review: runTx, runMsgs
- `git difftool -d v0.45.4..am/revert-045-baseapp x/auth/ante`
  - only cosmetic changes
- `git difftool -d v0.45.4..am/revert-045-baseapp simapp`



---

### 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...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] 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)
This commit is contained in:
Amaury
2022-05-20 09:27:27 +00:00
committed by GitHub
parent cf750b85d7
commit 01832e6239
76 changed files with 3158 additions and 4692 deletions
+7 -39
View File
@@ -6,36 +6,22 @@ import (
"fmt"
"path/filepath"
"github.com/tendermint/tendermint/types"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/types"
dbm "github.com/tendermint/tm-db"
bam "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/simapp"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/x/auth/middleware"
)
func testTxHandler(options middleware.TxHandlerOptions) tx.Handler {
return middleware.ComposeMiddlewares(
middleware.NewRunMsgsTxHandler(options.MsgServiceRouter, options.LegacyRouter),
middleware.NewTxDecoderMiddleware(options.TxDecoder),
middleware.GasTxMiddleware,
middleware.RecoveryTxMiddleware,
middleware.NewIndexEventsTxMiddleware(options.IndexEvents),
)
}
// NewApp creates a simple mock kvstore app for testing. It should work
// similar to a real app. Make sure rootDir is empty before running the test,
// in order to guarantee consistent results
func NewApp(rootDir string, logger log.Logger) (abci.Application, error) {
db, err := dbm.NewDB("mock", dbm.MemDBBackend, filepath.Join(rootDir, "data"))
db, err := sdk.NewLevelDB("mock", filepath.Join(rootDir, "data"))
if err != nil {
return nil, err
}
@@ -44,7 +30,7 @@ func NewApp(rootDir string, logger log.Logger) (abci.Application, error) {
capKeyMainStore := sdk.NewKVStoreKey("main")
// Create BaseApp.
baseApp := bam.NewBaseApp("kvstore", logger, db)
baseApp := bam.NewBaseApp("kvstore", logger, db, decodeTx)
// Set mounts for BaseApp's MultiStore.
baseApp.MountStores(capKeyMainStore)
@@ -52,19 +38,7 @@ func NewApp(rootDir string, logger log.Logger) (abci.Application, error) {
baseApp.SetInitChainer(InitChainer(capKeyMainStore))
// Set a Route.
encCfg := simapp.MakeTestEncodingConfig()
legacyRouter := middleware.NewLegacyRouter()
// We're adding a test legacy route here, which accesses the kvstore
// and simply sets the Msg's key/value pair in the kvstore.
legacyRouter.AddRoute(sdk.NewRoute("kvstore", KVStoreHandler(capKeyMainStore)))
txHandler := testTxHandler(
middleware.TxHandlerOptions{
LegacyRouter: legacyRouter,
MsgServiceRouter: middleware.NewMsgServiceRouter(encCfg.InterfaceRegistry),
TxDecoder: decodeTx,
},
)
baseApp.SetTxHandler(txHandler)
baseApp.Router().AddRoute(sdk.NewRoute("kvstore", KVStoreHandler(capKeyMainStore)))
// Load latest version.
if err := baseApp.LoadLatestVersion(); err != nil {
@@ -78,7 +52,7 @@ func NewApp(rootDir string, logger log.Logger) (abci.Application, error) {
// them to the db
func KVStoreHandler(storeKey storetypes.StoreKey) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
dTx, ok := msg.(*kvstoreTx)
dTx, ok := msg.(kvstoreTx)
if !ok {
return nil, errors.New("KVStoreHandler should only receive kvstoreTx")
}
@@ -90,14 +64,8 @@ func KVStoreHandler(storeKey storetypes.StoreKey) sdk.Handler {
store := ctx.KVStore(storeKey)
store.Set(key, value)
any, err := codectypes.NewAnyWithValue(msg)
if err != nil {
return nil, err
}
return &sdk.Result{
Log: fmt.Sprintf("set %s=%s", key, value),
MsgResponses: []*codectypes.Any{any},
Log: fmt.Sprintf("set %s=%s", key, value),
}, nil
}
}
+9 -20
View File
@@ -4,16 +4,12 @@ package mock
import (
"bytes"
"fmt"
"math"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/middleware"
)
// kvstoreTx defines a tx for mock purposes. The `key` and `value` fields will
// set those bytes in the kvstore, and the `bytes` field represents its
// GetSignBytes value.
// An sdk.Tx which is its own sdk.Msg.
type kvstoreTx struct {
key []byte
value []byte
@@ -21,15 +17,12 @@ type kvstoreTx struct {
}
// dummy implementation of proto.Message
func (msg *kvstoreTx) Reset() {}
func (msg *kvstoreTx) String() string { return "TODO" }
func (msg *kvstoreTx) ProtoMessage() {}
func (msg kvstoreTx) Reset() {}
func (msg kvstoreTx) String() string { return "TODO" }
func (msg kvstoreTx) ProtoMessage() {}
var (
_ sdk.Tx = &kvstoreTx{}
_ sdk.Msg = &kvstoreTx{}
_ middleware.GasTx = &kvstoreTx{}
)
var _ sdk.Tx = kvstoreTx{}
var _ sdk.Msg = kvstoreTx{}
func NewTx(key, value string) kvstoreTx {
bytes := fmt.Sprintf("%s=%s", key, value)
@@ -48,7 +41,7 @@ func (tx kvstoreTx) Type() string {
return "kvstore_tx"
}
func (tx *kvstoreTx) GetMsgs() []sdk.Msg {
func (tx kvstoreTx) GetMsgs() []sdk.Msg {
return []sdk.Msg{tx}
}
@@ -69,10 +62,6 @@ func (tx kvstoreTx) GetSigners() []sdk.AccAddress {
return nil
}
func (tx kvstoreTx) GetGas() uint64 {
return math.MaxUint64
}
// takes raw transaction bytes and decodes them into an sdk.Tx. An sdk.Tx has
// all the signatures and can be used to authenticate.
func decodeTx(txBytes []byte) (sdk.Tx, error) {
@@ -81,10 +70,10 @@ func decodeTx(txBytes []byte) (sdk.Tx, error) {
split := bytes.Split(txBytes, []byte("="))
if len(split) == 1 {
k := split[0]
tx = &kvstoreTx{k, k, txBytes}
tx = kvstoreTx{k, k, txBytes}
} else if len(split) == 2 {
k, v := split[0], split[1]
tx = &kvstoreTx{k, v, txBytes}
tx = kvstoreTx{k, v, txBytes}
} else {
return nil, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "too many '='")
}