From 54c2e44a2d6e227c45109d02f7f40f64e4c040e4 Mon Sep 17 00:00:00 2001 From: rhuairahrighairigh Date: Wed, 4 Mar 2020 19:50:30 +0000 Subject: [PATCH] add antehandler to app --- app/app.go | 31 +++++++++++++++++++++++++------ x/committee/spec/README.md | 2 +- x/shutdown/ante/ante.go | 22 ++++++++++------------ x/shutdown/spec/README.md | 2 +- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/app/app.go b/app/app.go index 3d4daa72..51f811db 100644 --- a/app/app.go +++ b/app/app.go @@ -4,11 +4,6 @@ import ( "io" "os" - "github.com/kava-labs/kava/x/auction" - "github.com/kava-labs/kava/x/cdp" - "github.com/kava-labs/kava/x/pricefeed" - validatorvesting "github.com/kava-labs/kava/x/validator-vesting" - abci "github.com/tendermint/tendermint/abci/types" cmn "github.com/tendermint/tendermint/libs/common" "github.com/tendermint/tendermint/libs/log" @@ -32,6 +27,13 @@ import ( "github.com/cosmos/cosmos-sdk/x/slashing" "github.com/cosmos/cosmos-sdk/x/staking" "github.com/cosmos/cosmos-sdk/x/supply" + + "github.com/kava-labs/kava/x/auction" + "github.com/kava-labs/kava/x/cdp" + "github.com/kava-labs/kava/x/pricefeed" + validatorvesting "github.com/kava-labs/kava/x/validator-vesting" + shutdownAnte "github.com/kava-labs/kava/x/shutdown/ante" + "github.com/kava-labs/kava/x/shutdown" ) const ( @@ -319,7 +321,7 @@ func NewApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool, // initialize the app app.SetInitChainer(app.InitChainer) app.SetBeginBlocker(app.BeginBlocker) - app.SetAnteHandler(auth.NewAnteHandler(app.accountKeeper, app.supplyKeeper, auth.DefaultSigVerificationGasConsumer)) + app.SetAnteHandler(NewAnteHandler(app.accountKeeper, app.supplyKeeper, app.shutdownKeeper, auth.DefaultSigVerificationGasConsumer)) app.SetEndBlocker(app.EndBlocker) // load store @@ -333,6 +335,23 @@ func NewApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool, return app } +func NewAnteHandler(ak auth.AccountKeeper, supplyKeeper supply.SupplyKeeper, shutdownKeeper shutdown.Keeper, sigGasConsumer SignatureVerificationGasConsumer) sdk.AnteHandler { + return sdk.ChainAnteDecorators( + auth.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first + shutdownAnte.NewDisableMsgDecorator(shutdownKeeper) + auth.NewMempoolFeeDecorator(), + auth.NewValidateBasicDecorator(), + auth.NewValidateMemoDecorator(ak), + auth.NewConsumeGasForTxSizeDecorator(ak), + auth.NewSetPubKeyDecorator(ak), // SetPubKeyDecorator must be called before all signature verification decorators + auth.NewValidateSigCountDecorator(ak), + auth.NewDeductFeeDecorator(ak, supplyKeeper), + auth.NewSigGasConsumeDecorator(ak, sigGasConsumer), + auth.NewSigVerificationDecorator(ak), + auth.NewIncrementSequenceDecorator(ak), // innermost AnteDecorator + ) +} + // custom tx codec func MakeCodec() *codec.Codec { var cdc = codec.New() diff --git a/x/committee/spec/README.md b/x/committee/spec/README.md index ac8103d5..f084da68 100644 --- a/x/committee/spec/README.md +++ b/x/committee/spec/README.md @@ -15,7 +15,7 @@ Members vote on proposals, with just simple one vote per member, no deposits or A permission acts as a filter for incoming gov proposals, rejecting them if they do not pass. A permission can be anything with a method `Allows(p Proposal) bool`. They reject all proposals that they don't explicitly allow. -This allows permissions to be parameterized to allow fine grained control specified at runtime. For example a generic parameter permission type can exist, but then on a live chain a permission can be added to a group to allow them to only change a particular param, even restricting the range of allowed change. +This allows permissions to be parameterized to allow fine grained control specified at runtime. For example a generic parameter permission type can allow a group to only change a particular param, or only change params within a certain percentage. Design Alternatives diff --git a/x/shutdown/ante/ante.go b/x/shutdown/ante/ante.go index 9a748340..a19da955 100644 --- a/x/shutdown/ante/ante.go +++ b/x/shutdown/ante/ante.go @@ -8,30 +8,28 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -// CircuitBreakerDecorator needs to be combined with other standard decorators (from auth) to create the app's AnteHandler. - -// CircuitBreakerDecorator errors if a tx contains a disallowed msg type -// Call next AnteHandler if all msgs are allowed -type CircuitBreakerDecorator struct { - cbk keeper.Keeper +// DisableMsgDecorator errors if a tx contains a disallowed msg type and calls the next AnteHandler if all msgs are allowed +type DisableMsgDecorator struct { + shutdownKeeper keeper.Keeper } -func NewCircuitBreakerDecorator(cbk keeper.Keeper) CircuitBreakerDecorator { - return CircuitBreakerDecorator{ - cbk: cbk, +func NewDisableMsgDecorator(shutdownKeeper keeper.Keeper) DisableMsgDecorator { + return DisableMsgDecorator{ + shutdownKeeper: shutdownKeeper, } } -func (cbd CircuitBreakerDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { +func (dmd DisableMsgDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { // get msg route, error if not allowed - disallowedRoutes := cbd.cbk.GetMsgRoutes(ctx) + disallowedRoutes := dmd.shutdownKeeper.GetMsgRoutes(ctx) for _, m := range tx.GetMsgs() { for _, r := range disallowedRoutes { if r.Route == m.Route() && r.Msg == m.Type() { - return ctx, fmt.Errorf("route %s has been circuit broken, tx rejected", r) + return ctx, fmt.Errorf("route %s has been disabled, tx rejected", r) } } } + // otherwise continue to next antehandler decorator return next(ctx, tx, simulate) } diff --git a/x/shutdown/spec/README.md b/x/shutdown/spec/README.md index 96a98410..2bfe0107 100644 --- a/x/shutdown/spec/README.md +++ b/x/shutdown/spec/README.md @@ -14,5 +14,5 @@ The list of disallowed msg types is updated via a custom governance proposal and Design Alternatives: -- store list of disallowed msg types in params, then don't need custom gov proposal +- store list of disallowed msg types in params, then we don't need the custom gov proposal - replace the app Router with a custom one to avoid using the antehandler - can't be done with current baseapp, but v0.38.x enables this. (https://github.com/cosmos/cosmos-sdk/issues/5455) \ No newline at end of file