Skip to content

Commit

Permalink
A bit of refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zale144 committed Sep 26, 2024
1 parent 6f725b6 commit d1c0da9
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 101 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
99 changes: 6 additions & 93 deletions app/ante.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand All @@ -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),
}
Expand All @@ -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 {
Expand All @@ -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),
)
}
}
}
11 changes: 4 additions & 7 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -830,26 +830,23 @@ 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,
TxCounterStoreKey: app.keys[wasmtypes.StoreKey],
GaslessKeeper: app.GaslessKeeper,
},
)
if err != nil {
panic(err)
}

app.SetAnteHandler(anteHandler)
app.SetAnteHandler(handler)
}

func (app *App) setPostHandler() {
Expand Down
64 changes: 64 additions & 0 deletions app/sig_gas_consumer.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit d1c0da9

Please sign in to comment.