Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

Commit

Permalink
fix(hood-ante): I hate this so much (#1391)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit


- **Refactor**
- Enhanced transaction processing with a new structure for handling
different transaction types.

- **New Features**
- Integrated support for both EVM and Cosmos transaction handling in the
app.

- **Chores**
- Updated the application setup to incorporate the new transaction
handling mechanism.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
itsdevbear authored Jan 4, 2024
1 parent b0ffa67 commit 4be15ef
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 12 deletions.
69 changes: 63 additions & 6 deletions cosmos/runtime/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,75 @@
package ante

import (
"errors"

storetypes "cosmossdk.io/store/types"

"github.com/berachain/polaris/cosmos/crypto/keys/ethsecp256k1"
"github.com/berachain/polaris/cosmos/runtime/txpool"
evmtypes "github.com/berachain/polaris/cosmos/x/evm/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

// NewAnteHandler creates a new instance of AnteHandler with EjectOnRecheckTxDecorator.
func NewAnteHandler(mempool *txpool.Mempool) sdk.AnteHandler {
anteDecorators := []sdk.AnteDecorator{
ante.NewSetUpContextDecorator(),
mempool,
// Provider is a struct that holds the ante handlers for EVM and Cosmos.
type Provider struct {
evmAnteHandler sdk.AnteHandler // Ante handler for EVM transactions
cosmosAnteHandler sdk.AnteHandler // Ante handler for Cosmos transactions
}

// NewAnteHandler creates a new Provider with a mempool and Cosmos ante handler.
// It sets up the EVM ante handler with the necessary decorators.
func NewAnteHandler(
mempool *txpool.Mempool, cosmosAnteHandler sdk.AnteHandler,
) *Provider {
evmAnteDecorators := []sdk.AnteDecorator{
ante.NewSetUpContextDecorator(), // Set up the context decorator for the EVM ante handler
mempool, // Add the mempool as a decorator for the EVM ante handler
}

return sdk.ChainAnteDecorators(anteDecorators...)
// Return a new Provider with the set up EVM and provided Cosmos ante handlers
return &Provider{
evmAnteHandler: sdk.ChainAnteDecorators(evmAnteDecorators...),
cosmosAnteHandler: cosmosAnteHandler,
}
}

// AnteHandler returns a function that handles ante for both EVM and Cosmos transactions.
// It checks the type of the transaction and calls the appropriate ante handler.
func (ah *Provider) AnteHandler() func(
ctx sdk.Context, tx sdk.Tx, simulate bool,
) (sdk.Context, error) {
return func(ctx sdk.Context, tx sdk.Tx, simulate bool) (sdk.Context, error) {
// If the transaction contains a single EVM transaction, use the EVM ante handler
if len(tx.GetMsgs()) == 1 {
if _, ok := tx.GetMsgs()[0].(*evmtypes.WrappedEthereumTransaction); ok {
return ah.evmAnteHandler(ctx, tx, simulate)
} else if _, ok = tx.GetMsgs()[0].(*evmtypes.WrappedPayloadEnvelope); ok {
if ctx.ExecMode() != sdk.ExecModeCheck {
return ctx, nil
}
return ctx, errors.New("payload envelope is not supported in CheckTx")
}
}
// Otherwise, use the Cosmos ante handler
return ah.cosmosAnteHandler(ctx, tx, simulate)
}
}

// EthSecp256k1SigVerificationGasConsumer is a function that consumes gas for the verification
// of an Ethereum Secp256k1 signature.
func EthSecp256k1SigVerificationGasConsumer(
meter storetypes.GasMeter, sig signing.SignatureV2, params authtypes.Params,
) error {
switch sig.PubKey.(type) {
case *ethsecp256k1.PubKey:
meter.ConsumeGas(params.SigVerifyCostSecp256k1, "ante verify: ethsecp256k1")
return nil
default:
return ante.DefaultSigVerificationGasConsumer(meter, sig, params)
}
}
8 changes: 6 additions & 2 deletions cosmos/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ func New(
// Build is a function that sets up the Polaris struct.
// It takes a BaseApp and an EVMKeeper as arguments.
// It returns an error if the setup fails.
func (p *Polaris) Build(app CosmosApp, ek EVMKeeper, allowedValMsgs map[string]sdk.Msg) error {
func (p *Polaris) Build(
app CosmosApp, cosmHandler sdk.AnteHandler, ek EVMKeeper, allowedValMsgs map[string]sdk.Msg,
) error {
// Wrap the geth miner and txpool with the cosmos miner and txpool.
p.WrappedMiner = miner.New(p.Miner(), app, ek, allowedValMsgs)
p.WrappedBlockchain = chain.New(p.Blockchain(), app)
Expand All @@ -124,7 +126,9 @@ func (p *Polaris) Build(app CosmosApp, ek EVMKeeper, allowedValMsgs map[string]s
return err
}

app.SetAnteHandler(antelib.NewAnteHandler(p.WrappedTxPool))
app.SetAnteHandler(
antelib.NewAnteHandler(p.WrappedTxPool, cosmHandler).AnteHandler(),
)

return nil
}
Expand Down
2 changes: 2 additions & 0 deletions e2e/localnet/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL
github.com/checkpoint-restore/go-criu/v5 v5.3.0/go.mod h1:E/eQpaFtUKGOOSEBZgmKAcn+zUUwWxqcaKZlF54wK8E=
github.com/cilium/ebpf v0.7.0/go.mod h1:/oI2+1shJiTGAMgl6/RgJr36Eo1jzrRcAWbcXO2usCA=
github.com/cockroachdb/errors v1.11.1 h1:xSEW75zKaKCWzR3OfxXUxgrk/NtT4G1MiOv5lWZazG8=
github.com/cockroachdb/errors v1.11.1/go.mod h1:8MUxA3Gi6b25tYlFEBGLf+D8aISL+M4MIpiWMSNRfxw=
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b h1:r6VH0faHjZeQy818SGhaone5OnYfxFR/+AzdY3sf5aE=
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b/go.mod h1:Vz9DsVWQQhf3vs21MhPMZpMGSht7O/2vFW2xusFUVOs=
github.com/cockroachdb/pebble v0.0.0-20231101195458-481da04154d6 h1:g+Y6IAf28JinY3zNdXwpw71SBGhLEb72kGQgiR5XKZM=
Expand Down Expand Up @@ -84,6 +85,7 @@ github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08/go.mod h1:x
github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46 h1:BAIP2GihuqhwdILrV+7GJel5lyPV3u1+PgzrWLc0TkE=
github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46/go.mod h1:QNpY22eby74jVhqH4WhDLDwxc/vqsern6pW+u2kbkpc=
github.com/getsentry/sentry-go v0.25.0 h1:q6Eo+hS+yoJlTO3uu/azhQadsD8V+jQn2D8VvX1eOyI=
github.com/getsentry/sentry-go v0.25.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w1gwTf25trprY=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
Expand Down
30 changes: 26 additions & 4 deletions e2e/testapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
ethcryptocodec "github.com/berachain/polaris/cosmos/crypto/codec"
signinglib "github.com/berachain/polaris/cosmos/lib/signing"
polarruntime "github.com/berachain/polaris/cosmos/runtime"
"github.com/berachain/polaris/cosmos/runtime/ante"
"github.com/berachain/polaris/cosmos/runtime/miner"
evmkeeper "github.com/berachain/polaris/cosmos/x/evm/keeper"

Expand All @@ -50,7 +51,9 @@ import (
"github.com/cosmos/cosmos-sdk/server/api"
"github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
consensuskeeper "github.com/cosmos/cosmos-sdk/x/consensus/keeper"
Expand Down Expand Up @@ -191,13 +194,32 @@ func NewPolarisApp(
evmconfig.MustReadConfigFromAppOpts(appOpts), app.Logger(), app.EVMKeeper.Host, nil,
)

// Build cosmos ante handler for non-evm transactions.
cosmHandler, err := authante.NewAnteHandler(
authante.HandlerOptions{
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
FeegrantKeeper: nil,
SigGasConsumer: ante.EthSecp256k1SigVerificationGasConsumer,
SignModeHandler: app.txConfig.SignModeHandler(),
TxFeeChecker: func(ctx sdk.Context, tx sdk.Tx) (sdk.Coins, int64, error) {
return nil, 0, nil
},
},
)
if err != nil {
panic(err)
}

// Setup Polaris Runtime.
if err := app.Polaris.Build(app, app.EVMKeeper, miner.DefaultAllowedMsgs); err != nil {
if err = app.Polaris.Build(
app, cosmHandler, app.EVMKeeper, miner.DefaultAllowedMsgs,
); err != nil {
panic(err)
}

// register streaming services
if err := app.RegisterStreamingServices(appOpts, app.kvStoreKeys()); err != nil {
if err = app.RegisterStreamingServices(appOpts, app.kvStoreKeys()); err != nil {
panic(err)
}

Expand All @@ -211,12 +233,12 @@ func NewPolarisApp(
ethcryptocodec.RegisterInterfaces(app.interfaceRegistry)

// Load the app.
if err := app.Load(loadLatest); err != nil {
if err = app.Load(loadLatest); err != nil {
panic(err)
}

// Load the last state of the polaris evm.
if err := app.Polaris.LoadLastState(
if err = app.Polaris.LoadLastState(
app.CommitMultiStore(), uint64(app.LastBlockHeight()),
); err != nil {
panic(err)
Expand Down

0 comments on commit 4be15ef

Please sign in to comment.