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

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

Merged
merged 18 commits into from
May 20, 2022
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
Revert middlewares
  • Loading branch information
amaury1093 committed May 17, 2022
commit 8edac39f9da1f54746f1776cf4434c66bdac7eda
46 changes: 13 additions & 33 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/tx"
)

// Supported ABCI Query prefixes
Expand Down Expand Up @@ -262,11 +261,16 @@ func (app *BaseApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx {
return sdkerrors.ResponseCheckTxWithEvents(err, gInfo.GasWanted, gInfo.GasUsed, anteEvents, app.trace)
}

data, err := makeABCIData(result)
if err != nil {
return sdkerrors.ResponseCheckTxWithEvents(err, gInfo.GasWanted, gInfo.GasUsed, anteEvents, app.trace)
}

return abci.ResponseCheckTx{
GasWanted: int64(gInfo.GasWanted), // TODO: Should type accept unsigned ints?
GasUsed: int64(gInfo.GasUsed), // TODO: Should type accept unsigned ints?
Log: result.Log,
Data: result.Data,
Data: data,
Events: sdk.MarkEventsToIndex(result.Events, app.indexEvents),
}
}
Expand Down Expand Up @@ -295,11 +299,16 @@ func (app *BaseApp) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx
return sdkerrors.ResponseDeliverTxWithEvents(err, gInfo.GasWanted, gInfo.GasUsed, anteEvents, app.trace)
}

data, err := makeABCIData(result)
if err != nil {
return sdkerrors.ResponseDeliverTxWithEvents(err, gInfo.GasWanted, gInfo.GasUsed, anteEvents, app.trace)
}

return abci.ResponseDeliverTx{
GasWanted: int64(gInfo.GasWanted), // TODO: Should type accept unsigned ints?
GasUsed: int64(gInfo.GasUsed), // TODO: Should type accept unsigned ints?
Log: result.Log,
Data: result.Data,
Data: data,
Events: sdk.MarkEventsToIndex(result.Events, app.indexEvents),
}
}
Expand Down Expand Up @@ -865,35 +874,6 @@ func SplitABCIQueryPath(requestPath string) (path []string) {
}

// makeABCIData generates the Data field to be sent to ABCI Check/DeliverTx.
func makeABCIData(txRes tx.Response) ([]byte, error) {
func makeABCIData(txRes *sdk.Result) ([]byte, error) {
return proto.Marshal(&sdk.TxMsgData{MsgResponses: txRes.MsgResponses})
}

// convertTxResponseToCheckTx converts a tx.Response into a abci.ResponseCheckTx.
func convertTxResponseToCheckTx(txRes tx.Response, checkRes tx.ResponseCheckTx) (abci.ResponseCheckTx, error) {
data, err := makeABCIData(txRes)
if err != nil {
return abci.ResponseCheckTx{}, nil
}

return abci.ResponseCheckTx{
Data: data,
Log: txRes.Log,
Events: txRes.Events,
Priority: checkRes.Priority,
}, nil
}

// convertTxResponseToDeliverTx converts a tx.Response into a abci.ResponseDeliverTx.
func convertTxResponseToDeliverTx(txRes tx.Response) (abci.ResponseDeliverTx, error) {
data, err := makeABCIData(txRes)
if err != nil {
return abci.ResponseDeliverTx{}, nil
}

return abci.ResponseDeliverTx{
Data: data,
Log: txRes.Log,
Events: txRes.Events,
}, nil
}
2 changes: 1 addition & 1 deletion baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func NewBaseApp(
db: db,
cms: store.NewCommitMultiStore(db),
storeLoader: DefaultStoreLoader,
router: NewRouter(),
router: NewLegacyRouter(),
queryRouter: NewQueryRouter(),
grpcQueryRouter: NewGRPCQueryRouter(),
msgServiceRouter: NewMsgServiceRouter(),
Expand Down
2 changes: 1 addition & 1 deletion baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion baseapp/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func processRecovery(recoveryObj interface{}, middleware recoveryMiddleware) err
return processRecovery(recoveryObj, next)
}

// newRecoveryMiddleware creates a RecoveryHandler middleware.
// newRecoveryMiddleware creates a RecoveryHandler ante.
func newRecoveryMiddleware(handler RecoveryHandler, next recoveryMiddleware) recoveryMiddleware {
return func(recoveryObj interface{}) (recoveryMiddleware, error) {
if err := handler(recoveryObj); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions baseapp/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ type Router struct {
routes map[string]sdk.Handler
}

var _ sdk.Router = NewRouter()
var _ sdk.Router = NewLegacyRouter()

// NewRouter returns a reference to a new router.
func NewRouter() *Router {
func NewLegacyRouter() *Router {
return &Router{
routes: make(map[string]sdk.Handler),
}
Expand Down
2 changes: 1 addition & 1 deletion proto/cosmos/base/abci/v1beta1/abci.proto
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ message TxResponse {
string timestamp = 12;
// Events defines all the events emitted by processing a transaction. Note,
// these events include those emitted by processing all the messages and those
// emitted from the middleware. Whereas Logs contains the events, with
// emitted from the ante. Whereas Logs contains the events, with
// additional metadata, emitted only by processing the messages.
//
// Since: cosmos-sdk 0.42.11, 0.44.5, 0.45
Expand Down
22 changes: 11 additions & 11 deletions server/mock/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ import (
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"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
)

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),
func testTxHandler(options ante.TxHandlerOptions) tx.Handler {
return ante.ComposeMiddlewares(
ante.NewRunMsgsTxHandler(options.MsgServiceRouter, options.LegacyRouter),
ante.NewTxDecoderMiddleware(options.TxDecoder),
ante.GasTxMiddleware,
ante.RecoveryTxMiddleware,
ante.NewIndexEventsTxMiddleware(options.IndexEvents),
)
}

Expand All @@ -53,14 +53,14 @@ func NewApp(rootDir string, logger log.Logger) (abci.Application, error) {

// Set a Route.
encCfg := simapp.MakeTestEncodingConfig()
legacyRouter := middleware.NewLegacyRouter()
legacyRouter := ante.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{
ante.TxHandlerOptions{
LegacyRouter: legacyRouter,
MsgServiceRouter: middleware.NewMsgServiceRouter(encCfg.InterfaceRegistry),
MsgServiceRouter: ante.NewMsgServiceRouter(encCfg.InterfaceRegistry),
TxDecoder: decodeTx,
},
)
Expand Down
4 changes: 2 additions & 2 deletions server/mock/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/middleware"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
)

// kvstoreTx defines a tx for mock purposes. The `key` and `value` fields will
Expand All @@ -27,7 +27,7 @@ func (msg *kvstoreTx) ProtoMessage() {}

var _ sdk.Tx = &kvstoreTx{}
var _ sdk.Msg = &kvstoreTx{}
var _ middleware.GasTx = &kvstoreTx{}
var _ ante.GasTx = &kvstoreTx{}

func NewTx(key, value string) kvstoreTx {
bytes := fmt.Sprintf("%s=%s", key, value)
Expand Down
36 changes: 17 additions & 19 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import (
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authmiddleware "github.com/cosmos/cosmos-sdk/x/auth/middleware"
authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -155,7 +155,7 @@ type SimApp struct {
legacyAmino *codec.LegacyAmino
appCodec codec.Codec
interfaceRegistry types.InterfaceRegistry
msgSvcRouter *authmiddleware.MsgServiceRouter
msgSvcRouter *baseapp.MsgServiceRouter
legacyRouter sdk.Router

invCheckPeriod uint
Expand Down Expand Up @@ -241,8 +241,8 @@ func NewSimApp(
legacyAmino: legacyAmino,
appCodec: appCodec,
interfaceRegistry: interfaceRegistry,
legacyRouter: authmiddleware.NewLegacyRouter(),
msgSvcRouter: authmiddleware.NewMsgServiceRouter(interfaceRegistry),
legacyRouter: baseapp.NewLegacyRouter(),
msgSvcRouter: baseapp.NewMsgServiceRouter(),
invCheckPeriod: invCheckPeriod,
keys: keys,
tkeys: tkeys,
Expand Down Expand Up @@ -447,7 +447,7 @@ func NewSimApp(
app.SetInitChainer(app.InitChainer)
app.SetBeginBlocker(app.BeginBlocker)
app.SetEndBlocker(app.EndBlocker)
app.setTxHandler(encodingConfig.TxConfig, cast.ToStringSlice(appOpts.Get(server.FlagIndexEvents)))
app.setAnteHandler(encodingConfig.TxConfig, cast.ToStringSlice(appOpts.Get(server.FlagIndexEvents)))

if loadLatest {
if err := app.LoadLatestVersion(); err != nil {
Expand All @@ -458,28 +458,26 @@ func NewSimApp(
return app
}

func (app *SimApp) setTxHandler(txConfig client.TxConfig, indexEventsStr []string) {
func (app *SimApp) setAnteHandler(txConfig client.TxConfig, indexEventsStr []string) {
indexEvents := map[string]struct{}{}
for _, e := range indexEventsStr {
indexEvents[e] = struct{}{}
}
txHandler, err := authmiddleware.NewDefaultTxHandler(authmiddleware.TxHandlerOptions{
Debug: app.Trace(),
IndexEvents: indexEvents,
LegacyRouter: app.legacyRouter,
MsgServiceRouter: app.msgSvcRouter,
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
FeegrantKeeper: app.FeeGrantKeeper,
SignModeHandler: txConfig.SignModeHandler(),
SigGasConsumer: authmiddleware.DefaultSigVerificationGasConsumer,
TxDecoder: txConfig.TxDecoder(),
})
anteHandler, err := ante.NewAnteHandler(
ante.HandlerOptions{
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
SignModeHandler: txConfig.SignModeHandler(),
FeegrantKeeper: app.FeeGrantKeeper,
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
},
)

if err != nil {
panic(err)
}

app.SetTxHandler(txHandler)
app.SetAnteHandler(anteHandler)
}

// Name returns the name of the App
Expand Down
4 changes: 2 additions & 2 deletions simapp/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
authmiddleware "github.com/cosmos/cosmos-sdk/x/auth/middleware"
authmiddleware "github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
authzmodule "github.com/cosmos/cosmos-sdk/x/authz/module"
"github.com/cosmos/cosmos-sdk/x/bank"
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestRunMigrations(t *testing.T) {
bApp := baseapp.NewBaseApp(appName, logger, db)
bApp.SetCommitMultiStoreTracer(nil)
bApp.SetInterfaceRegistry(encCfg.InterfaceRegistry)
msr := authmiddleware.NewMsgServiceRouter(encCfg.InterfaceRegistry)
msr := authante.NewMsgServiceRouter(encCfg.InterfaceRegistry)
app.BaseApp = bApp
app.configurator = module.NewConfigurator(app.appCodec, msr, app.GRPCQueryRouter())

Expand Down
71 changes: 0 additions & 71 deletions types/tx/middleware.go

This file was deleted.

Loading