Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zale144 committed Sep 27, 2024
1 parent d1c0da9 commit a3f79e1
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 85 deletions.
112 changes: 99 additions & 13 deletions app/ante.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package app

Check failure on line 1 in app/ante.go

View workflow job for this annotation

GitHub Actions / golangci-lint

: # github.com/dymensionxyz/rollapp-wasm/app

import (
"fmt"
"runtime/debug"

errorsmod "cosmossdk.io/errors"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
conntypes "github.com/cosmos/ibc-go/v6/modules/core/03-connection/types"
ibcante "github.com/cosmos/ibc-go/v6/modules/core/ante"
ibckeeper "github.com/cosmos/ibc-go/v6/modules/core/keeper"
cosmosante "github.com/evmos/evmos/v12/app/ante/cosmos"
evmostypes "github.com/evmos/evmos/v12/types"
evmtypes "github.com/evmos/evmos/v12/x/evm/types"
tmlog "github.com/tendermint/tendermint/libs/log"

"github.com/dymensionxyz/dymension-rdk/x/gasless"
gaslesskeeper "github.com/dymensionxyz/dymension-rdk/x/gasless/keeper"
cosmosante "github.com/evmos/evmos/v12/app/ante/cosmos"
)

// HandlerOptions are the options required for constructing a default SDK AnteHandler.
Expand All @@ -29,16 +36,71 @@ type HandlerOptions struct {
// NewAnteHandler returns an AnteHandler that checks and increments sequence
// numbers, checks signatures & account numbers, and deducts fees from the first
// signer.
func NewAnteHandler(options HandlerOptions) sdk.AnteHandler {
func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
if err := options.validate(); err != nil {
panic(err)
return nil, fmt.Errorf("options validate: %w", err)
}

return sdk.ChainAnteDecorators(getAnteDecorators(options)...)
return func(
ctx sdk.Context, tx sdk.Tx, sim bool,
) (newCtx sdk.Context, err error) {
var anteHandler sdk.AnteHandler

defer Recover(ctx.Logger(), &err)

txWithExtensions, ok := tx.(ante.HasExtensionOptionsTx)
if ok {
opts := txWithExtensions.GetExtensionOptions()
if len(opts) > 0 {
switch typeURL := opts[0].GetTypeUrl(); typeURL {
case "/ethermint.types.v1.ExtensionOptionsWeb3Tx":
// Deprecated: Handle as normal Cosmos SDK tx, except signature is checked for Legacy EIP712 representation
options.ExtensionOptionChecker = func(c *codectypes.Any) bool {
_, ok := c.GetCachedValue().(*evmostypes.ExtensionOptionsWeb3Tx)
return ok
}
anteHandler = cosmosHandler(
options,
// nolint:staticcheck
cosmosante.NewLegacyEip712SigVerificationDecorator(options.AccountKeeper.(evmtypes.AccountKeeper), options.SignModeHandler), // Use old signature verification: uses EIP instead of the cosmos signature validator
)
default:
return ctx, errorsmod.Wrapf(
sdkerrors.ErrUnknownExtensionOptions,
"rejecting tx with unsupported extension option: %s", typeURL,
)
}

return anteHandler(ctx, tx, sim)
}
}

// handle as totally normal Cosmos SDK tx
switch tx.(type) {
case sdk.Tx:
// we reject any extension
anteHandler = cosmosHandler(
options,
ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler), // Use modern signature verification
)
default:
return ctx, errorsmod.Wrapf(sdkerrors.ErrUnknownRequest, "invalid transaction type: %T", tx)
}

return anteHandler(ctx, tx, sim)
}, nil
}

func getAnteDecorators(options HandlerOptions) []sdk.AnteDecorator {
anteDecorators := []sdk.AnteDecorator{
func cosmosHandler(options HandlerOptions, sigChecker sdk.AnteDecorator) sdk.AnteHandler {
sigGasConsumer := options.SigGasConsumer
if sigGasConsumer == nil {
sigGasConsumer = ante.DefaultSigVerificationGasConsumer
}
// only override the modern sig checker, and preserve the legacy one
if _, ok := sigChecker.(ante.SigVerificationDecorator); ok {
sigChecker = NewSigCheckDecorator(options.AccountKeeper.(accountKeeper), options.SignModeHandler)
}
return sdk.ChainAnteDecorators(
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
cosmosante.NewRejectMessagesDecorator(
[]string{
Expand All @@ -59,13 +121,9 @@ func getAnteDecorators(options HandlerOptions) []sdk.AnteDecorator {
ante.NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators
ante.NewValidateSigCountDecorator(options.AccountKeeper),
ante.NewSigGasConsumeDecorator(options.AccountKeeper, options.SigGasConsumer),
NewSigCheckDecorator(options.AccountKeeper.(accountKeeper), options.SignModeHandler),
sigChecker,
ante.NewIncrementSequenceDecorator(options.AccountKeeper),
}

anteDecorators = append(anteDecorators, ibcante.NewRedundantRelayDecorator(options.IBCKeeper))

return anteDecorators
)
}

func (o HandlerOptions) validate() error {
Expand All @@ -85,5 +143,33 @@ func (o HandlerOptions) validate() error {
if o.WasmConfig == nil {
return errorsmod.Wrap(sdkerrors.ErrLogic, "wasm config is required for ante builder")
}

if o.TxCounterStoreKey == nil {
return errorsmod.Wrap(sdkerrors.ErrLogic, "tx counter store key is required for ante builder")
}

if o.SigGasConsumer == nil {
return errorsmod.Wrap(sdkerrors.ErrLogic, "signature gas consumer is required for ante builder")
}

return nil
}

func Recover(logger tmlog.Logger, err *error) {
if r := recover(); r != nil {
*err = errorsmod.Wrapf(sdkerrors.ErrPanic, "%v", r)

if e, ok := r.(error); ok {
logger.Error(
"ante handler panicked",
"error", e,
"stack trace", string(debug.Stack()),
)
} else {
logger.Error(
"ante handler panicked",
"recover", fmt.Sprintf("%v", r),
)
}
}
}
17 changes: 11 additions & 6 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/authz"
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
authzmodule "github.com/cosmos/cosmos-sdk/x/authz/module"
gaslessmodule "github.com/dymensionxyz/dymension-rdk/x/gasless"
gaslesskeeper "github.com/dymensionxyz/dymension-rdk/x/gasless/keeper"
gaslesstypes "github.com/dymensionxyz/dymension-rdk/x/gasless/types"
evmosante "github.com/evmos/evmos/v12/app/ante"
"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
abci "github.com/tendermint/tendermint/abci/types"
Expand All @@ -23,6 +21,10 @@ import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"

gaslessmodule "github.com/dymensionxyz/dymension-rdk/x/gasless"
gaslesskeeper "github.com/dymensionxyz/dymension-rdk/x/gasless/keeper"
gaslesstypes "github.com/dymensionxyz/dymension-rdk/x/gasless/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
nodeservice "github.com/cosmos/cosmos-sdk/client/grpc/node"
Expand Down Expand Up @@ -643,7 +645,7 @@ func NewRollapp(
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper, app.BankKeeper),
distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
sequencers.NewAppModule(appCodec, app.SequencersKeeper),
sequencers.NewAppModule(app.SequencersKeeper),

Check failure on line 648 in app/app.go

View workflow job for this annotation

GitHub Actions / golangci-lint

not enough arguments in call to sequencers.NewAppModule

Check failure on line 648 in app/app.go

View workflow job for this annotation

GitHub Actions / golangci-lint

not enough arguments in call to sequencers.NewAppModule

Check failure on line 648 in app/app.go

View workflow job for this annotation

GitHub Actions / golangci-lint

not enough arguments in call to sequencers.NewAppModule

Check failure on line 648 in app/app.go

View workflow job for this annotation

GitHub Actions / golangci-lint

not enough arguments in call to sequencers.NewAppModule

Check failure on line 648 in app/app.go

View workflow job for this annotation

GitHub Actions / Analyze

not enough arguments in call to sequencers.NewAppModule
epochs.NewAppModule(appCodec, app.EpochsKeeper),
params.NewAppModule(app.ParamsKeeper),
wasm.NewAppModule(appCodec, &app.WasmKeeper, app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
Expand Down Expand Up @@ -830,21 +832,24 @@ func NewRollapp(
}

func (app *App) setAnteHandler(txConfig client.TxConfig, wasmConfig wasmtypes.WasmConfig) {
handler := NewAnteHandler(
handler, err := NewAnteHandler(
HandlerOptions{
HandlerOptions: ante.HandlerOptions{
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
FeegrantKeeper: app.FeeGrantKeeper,
SignModeHandler: txConfig.SignModeHandler(),
SigGasConsumer: defaultSigVerificationGasConsumer,
SigGasConsumer: evmosante.SigVerificationGasConsumer,
},
IBCKeeper: app.IBCKeeper,
WasmConfig: &wasmConfig,
TxCounterStoreKey: app.keys[wasmtypes.StoreKey],
GaslessKeeper: app.GaslessKeeper,
},
)
if err != nil {
panic(err)
}

app.SetAnteHandler(handler)
}
Expand Down
2 changes: 0 additions & 2 deletions app/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/cosmos/cosmos-sdk/std"
sdk "github.com/cosmos/cosmos-sdk/types"
cryptocodec "github.com/evmos/evmos/v12/crypto/codec"
ethermint "github.com/evmos/evmos/v12/types"

"github.com/dymensionxyz/rollapp-wasm/app/params"
)
Expand All @@ -32,5 +31,4 @@ func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
func RegisterInterfaces(interfaceRegistry codectypes.InterfaceRegistry) {
std.RegisterInterfaces(interfaceRegistry)
cryptocodec.RegisterInterfaces(interfaceRegistry)
ethermint.RegisterInterfaces(interfaceRegistry)
}
64 changes: 0 additions & 64 deletions app/sig_gas_consumer.go

This file was deleted.

0 comments on commit a3f79e1

Please sign in to comment.