diff --git a/README.md b/README.md index 5a0e926..771fbca 100644 --- a/README.md +++ b/README.md @@ -174,7 +174,7 @@ Modify `dymint.toml` in the chain directory (`~/.rollapp/config`) ```shell dasel put -f "${ROLLAPP_HOME_DIR}"/config/dymint.toml "settlement_layer" -v "dymension" -dasel put -f "${ROLLAPP_HOME_DIR}"/config/dymint.toml "node_address" -v "$HUB_RPC_URL" +dasel put -f "${ROLLAPP_HOME_DIR}"/config/dymint.toml "settlement_node_address" -v "$HUB_RPC_URL" dasel put -f "${ROLLAPP_HOME_DIR}"/config/dymint.toml "rollapp_id" -v "$ROLLAPP_CHAIN_ID" dasel put -f "${ROLLAPP_HOME_DIR}"/config/dymint.toml "max_idle_time" -v "2s" dasel put -f "${ROLLAPP_HOME_DIR}"/config/dymint.toml "max_proof_time" -v "1s" diff --git a/app/ante.go b/app/ante.go index c765015..23cafb6 100644 --- a/app/ante.go +++ b/app/ante.go @@ -1,27 +1,19 @@ package 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" - "github.com/cosmos/cosmos-sdk/crypto/types/multisig" 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/types/tx/signing" "github.com/cosmos/cosmos-sdk/x/auth/ante" - "github.com/cosmos/cosmos-sdk/x/auth/types" 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" "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" - "github.com/evmos/evmos/v12/crypto/ethsecp256k1" - tmlog "github.com/tendermint/tendermint/libs/log" ) // HandlerOptions are the options required for constructing a default SDK AnteHandler. @@ -34,24 +26,17 @@ type HandlerOptions struct { GaslessKeeper gaslesskeeper.Keeper } -func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) { +// 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 { if err := options.validate(); err != nil { - return nil, fmt.Errorf("options validate: %w", err) + panic(err) } - return func(ctx sdk.Context, tx sdk.Tx, sim bool) (_ sdk.Context, err error) { - defer Recover(ctx.Logger(), &err) - - return cosmosHandler(options)(ctx, tx, sim) - }, nil -} - -func cosmosHandler(options HandlerOptions) sdk.AnteHandler { return sdk.ChainAnteDecorators(getAnteDecorators(options)...) } -type SigGasConsumer func(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error - func getAnteDecorators(options HandlerOptions) []sdk.AnteDecorator { anteDecorators := []sdk.AnteDecorator{ ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first @@ -73,7 +58,7 @@ func getAnteDecorators(options HandlerOptions) []sdk.AnteDecorator { NewBypassIBCFeeDecorator(gasless.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker, options.GaslessKeeper)), ante.NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators ante.NewValidateSigCountDecorator(options.AccountKeeper), - ante.NewSigGasConsumeDecorator(options.AccountKeeper, defaultSigVerificationGasConsumer), + ante.NewSigGasConsumeDecorator(options.AccountKeeper, options.SigGasConsumer), NewSigCheckDecorator(options.AccountKeeper.(accountKeeper), options.SignModeHandler), ante.NewIncrementSequenceDecorator(options.AccountKeeper), } @@ -83,59 +68,6 @@ func getAnteDecorators(options HandlerOptions) []sdk.AnteDecorator { return anteDecorators } -const ( - secp256k1VerifyCost uint64 = 21000 -) - -// TODO: check with zero fee relayer -// Copied from github.com/evmos/ethermint -func defaultSigVerificationGasConsumer(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error { - pubkey := sig.PubKey - switch pubkey := pubkey.(type) { - case *ethsecp256k1.PubKey: - meter.ConsumeGas(secp256k1VerifyCost, "ante verify: eth_secp256k1") - return nil - - case multisig.PubKey: - // Multisig keys - multisignature, ok := sig.Data.(*signing.MultiSignatureData) - if !ok { - return fmt.Errorf("expected %T, got, %T", &signing.MultiSignatureData{}, sig.Data) - } - return consumeMultisignatureVerificationGas(meter, multisignature, pubkey, params, sig.Sequence) - - default: - return ante.DefaultSigVerificationGasConsumer(meter, sig, params) - } -} - -// Copied from github.com/evmos/ethermint -func consumeMultisignatureVerificationGas( - meter sdk.GasMeter, sig *signing.MultiSignatureData, pubkey multisig.PubKey, - params types.Params, accSeq uint64, -) error { - size := sig.BitArray.Count() - sigIndex := 0 - - for i := 0; i < size; i++ { - if !sig.BitArray.GetIndex(i) { - continue - } - sigV2 := signing.SignatureV2{ - PubKey: pubkey.GetPubKeys()[i], - Data: sig.Signatures[sigIndex], - Sequence: accSeq, - } - err := defaultSigVerificationGasConsumer(meter, sigV2, params) - if err != nil { - return err - } - sigIndex++ - } - - return nil -} - func (o HandlerOptions) validate() error { // From x/auth/ante.go if o.AccountKeeper == nil { @@ -155,22 +87,3 @@ func (o HandlerOptions) validate() error { } 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), - ) - } - } -} diff --git a/app/app.go b/app/app.go index 7505022..caa55b4 100644 --- a/app/app.go +++ b/app/app.go @@ -140,7 +140,7 @@ import ( cwerrorsKeeper "github.com/dymensionxyz/rollapp-wasm/x/cwerrors/keeper" cwerrorsTypes "github.com/dymensionxyz/rollapp-wasm/x/cwerrors/types" - rollappparams "github.com/dymensionxyz/dymension-rdk/x/rollappparams" + "github.com/dymensionxyz/dymension-rdk/x/rollappparams" rollappparamskeeper "github.com/dymensionxyz/dymension-rdk/x/rollappparams/keeper" rollappparamstypes "github.com/dymensionxyz/dymension-rdk/x/rollappparams/types" ) @@ -830,14 +830,14 @@ func NewRollapp( } func (app *App) setAnteHandler(txConfig client.TxConfig, wasmConfig wasmtypes.WasmConfig) { - anteHandler, err := NewAnteHandler( + handler := NewAnteHandler( HandlerOptions{ HandlerOptions: ante.HandlerOptions{ AccountKeeper: app.AccountKeeper, BankKeeper: app.BankKeeper, FeegrantKeeper: app.FeeGrantKeeper, SignModeHandler: txConfig.SignModeHandler(), - SigGasConsumer: ante.DefaultSigVerificationGasConsumer, + SigGasConsumer: defaultSigVerificationGasConsumer, }, IBCKeeper: app.IBCKeeper, WasmConfig: &wasmConfig, @@ -845,11 +845,8 @@ func (app *App) setAnteHandler(txConfig client.TxConfig, wasmConfig wasmtypes.Wa GaslessKeeper: app.GaslessKeeper, }, ) - if err != nil { - panic(err) - } - app.SetAnteHandler(anteHandler) + app.SetAnteHandler(handler) } func (app *App) setPostHandler() { diff --git a/app/sig_gas_consumer.go b/app/sig_gas_consumer.go new file mode 100644 index 0000000..32b8e01 --- /dev/null +++ b/app/sig_gas_consumer.go @@ -0,0 +1,64 @@ +package app + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/crypto/types/multisig" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/tx/signing" + "github.com/cosmos/cosmos-sdk/x/auth/ante" + "github.com/cosmos/cosmos-sdk/x/auth/types" + "github.com/evmos/evmos/v12/crypto/ethsecp256k1" +) + +const ( + secp256k1VerifyCost uint64 = 21000 +) + +// Copied from github.com/evmos/ethermint +func defaultSigVerificationGasConsumer(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error { + pubkey := sig.PubKey + switch pubkey := pubkey.(type) { + case *ethsecp256k1.PubKey: + meter.ConsumeGas(secp256k1VerifyCost, "ante verify: eth_secp256k1") + return nil + + case multisig.PubKey: + // Multisig keys + multisignature, ok := sig.Data.(*signing.MultiSignatureData) + if !ok { + return fmt.Errorf("expected %T, got, %T", &signing.MultiSignatureData{}, sig.Data) + } + return consumeMultisignatureVerificationGas(meter, multisignature, pubkey, params, sig.Sequence) + + default: + return ante.DefaultSigVerificationGasConsumer(meter, sig, params) + } +} + +// Copied from github.com/evmos/ethermint +func consumeMultisignatureVerificationGas( + meter sdk.GasMeter, sig *signing.MultiSignatureData, pubkey multisig.PubKey, + params types.Params, accSeq uint64, +) error { + size := sig.BitArray.Count() + sigIndex := 0 + + for i := 0; i < size; i++ { + if !sig.BitArray.GetIndex(i) { + continue + } + sigV2 := signing.SignatureV2{ + PubKey: pubkey.GetPubKeys()[i], + Data: sig.Signatures[sigIndex], + Sequence: accSeq, + } + err := defaultSigVerificationGasConsumer(meter, sigV2, params) + if err != nil { + return err + } + sigIndex++ + } + + return nil +}