Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions config/evm_app_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package config

import (
"fmt"

"github.com/ethereum/go-ethereum/core/vm"

evmtypes "github.com/cosmos/evm/x/vm/types"

"cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// EVMOptionsFn defines a function type for setting app options specifically for
// the Cosmos EVM app. The function should receive the chainID and return an error if
// any.
type EVMOptionsFn func(uint64) error

var sealed = false

func EvmAppOptionsWithConfig(
chainID uint64,
chainsCoinInfo map[uint64]evmtypes.EvmCoinInfo,
cosmosEVMActivators map[int]func(*vm.JumpTable),
) error {
if sealed {
return nil
}

if err := EvmAppOptionsWithConfigWithReset(chainID, chainsCoinInfo, cosmosEVMActivators, false); err != nil {
return err
}

sealed = true
return nil
}

func EvmAppOptionsWithConfigWithReset(
chainID uint64,
chainsCoinInfo map[uint64]evmtypes.EvmCoinInfo,
cosmosEVMActivators map[int]func(*vm.JumpTable),
withReset bool,
) error {
coinInfo, found := chainsCoinInfo[chainID]
if !found {
return fmt.Errorf("unknown chain id: %d", chainID)
}

// set the denom info for the chain
if err := setBaseDenom(coinInfo); err != nil {
return err
}

ethCfg := evmtypes.DefaultChainConfig(chainID)
configurator := evmtypes.NewEVMConfigurator()
if withReset {
// reset configuration to set the new one
configurator.ResetTestConfig()
}
err := configurator.
WithExtendedEips(cosmosEVMActivators).
WithChainConfig(ethCfg).
// NOTE: we're using the 18 decimals default for the example chain
WithEVMCoinInfo(coinInfo).
Configure()
if err != nil {
return err
}

return nil
}

// setBaseDenom registers the display denom and base denom and sets the
// base denom for the chain. The function registered different values based on
// the EvmCoinInfo to allow different configurations in mainnet and testnet.
func setBaseDenom(ci evmtypes.EvmCoinInfo) (err error) {
// Defer setting the base denom, and capture any potential error from it.
// So when failing because the denom was already registered, we ignore it and set
// the corresponding denom to be base denom
defer func() {
err = sdk.SetBaseDenom(ci.Denom)
}()
if err := sdk.RegisterDenom(ci.DisplayDenom, math.LegacyOneDec()); err != nil {
return err
}

// sdk.RegisterDenom will automatically overwrite the base denom when the
// new setBaseDenom() units are lower than the current base denom's units.
return sdk.RegisterDenom(ci.Denom, math.LegacyNewDecWithPrec(1, int64(ci.Decimals)))
}
5 changes: 3 additions & 2 deletions evmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (
dbm "github.com/cosmos/cosmos-db"
evmante "github.com/cosmos/evm/ante"
cosmosevmante "github.com/cosmos/evm/ante/evm"
evmconfig "github.com/cosmos/evm/config"
evmosencoding "github.com/cosmos/evm/encoding"
"github.com/cosmos/evm/evmd/ante"
evmdconfig "github.com/cosmos/evm/evmd/cmd/evmd/config"
srvflags "github.com/cosmos/evm/server/flags"
cosmosevmtypes "github.com/cosmos/evm/types"
"github.com/cosmos/evm/x/erc20"
Expand All @@ -32,6 +32,7 @@ import (
ibccallbackskeeper "github.com/cosmos/evm/x/ibc/callbacks/keeper"

// NOTE: override ICS20 keeper to support IBC transfers of ERC20 tokens
evmdconfig "github.com/cosmos/evm/evmd/cmd/evmd/config"
"github.com/cosmos/evm/x/ibc/transfer"
transferkeeper "github.com/cosmos/evm/x/ibc/transfer/keeper"
transferv2 "github.com/cosmos/evm/x/ibc/transfer/v2"
Expand Down Expand Up @@ -209,7 +210,7 @@ func NewExampleApp(
loadLatest bool,
appOpts servertypes.AppOptions,
evmChainID uint64,
evmAppOptions evmdconfig.EVMOptionsFn,
evmAppOptions evmconfig.EVMOptionsFn,
baseAppOptions ...func(*baseapp.BaseApp),
) *EVMD {
encodingConfig := evmosencoding.MakeConfig(evmChainID)
Expand Down
107 changes: 3 additions & 104 deletions evmd/cmd/evmd/config/config_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,113 +4,12 @@
package config

import (
"fmt"

evmtypes "github.com/cosmos/evm/x/vm/types"

"cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
evmconfig "github.com/cosmos/evm/config"
testconfig "github.com/cosmos/evm/testutil/config"
)

// TestChainsCoinInfo is a map of the chain id and its corresponding EvmCoinInfo
// that allows initializing the app with different coin info based on the
// chain id
var TestChainsCoinInfo = map[uint64]evmtypes.EvmCoinInfo{
EighteenDecimalsChainID: {
Denom: ExampleChainDenom,
ExtendedDenom: ExampleChainDenom,
DisplayDenom: ExampleDisplayDenom,
Decimals: evmtypes.EighteenDecimals,
},
SixDecimalsChainID: {
Denom: "utest",
ExtendedDenom: "atest",
DisplayDenom: "test",
Decimals: evmtypes.SixDecimals,
},
TwelveDecimalsChainID: {
Denom: "ptest2",
ExtendedDenom: "atest2",
DisplayDenom: "test2",
Decimals: evmtypes.TwelveDecimals,
},
TwoDecimalsChainID: {
Denom: "ctest3",
ExtendedDenom: "atest3",
DisplayDenom: "test3",
Decimals: evmtypes.TwoDecimals,
},
TestChainID1: {
Denom: ExampleChainDenom,
ExtendedDenom: ExampleChainDenom,
DisplayDenom: ExampleChainDenom,
Decimals: evmtypes.EighteenDecimals,
},
TestChainID2: {
Denom: ExampleChainDenom,
ExtendedDenom: ExampleChainDenom,
DisplayDenom: ExampleChainDenom,
Decimals: evmtypes.EighteenDecimals,
},
}

// EVMOptionsFn defines a function type for setting app options specifically for
// the Cosmos EVM app. The function should receive the chainID and return an error if
// any.
type EVMOptionsFn func(uint64) error

// NoOpEVMOptions is a no-op function that can be used when the app does not
// need any specific configuration.
func NoOpEVMOptions(_ uint64) error {
return nil
}

// EvmAppOptions allows to setup the global configuration
// for the Cosmos EVM chain.
func EvmAppOptions(chainID uint64) error {
coinInfo, found := TestChainsCoinInfo[chainID]
if !found {
return fmt.Errorf("unknown chain id: %d", chainID)
}

// set the base denom considering if its mainnet or testnet
if err := setBaseDenom(coinInfo); err != nil {
return err
}

ethCfg := evmtypes.DefaultChainConfig(chainID)

configurator := evmtypes.NewEVMConfigurator()
// reset configuration to set the new one
configurator.ResetTestConfig()
err := configurator.
WithExtendedEips(cosmosEVMActivators).
WithChainConfig(ethCfg).
WithEVMCoinInfo(coinInfo).
Configure()
if err != nil {
return err
}

return nil
}

// setBaseDenom registers the display denom and base denom and sets the
// base denom for the chain. The function registered different values based on
// the EvmCoinInfo to allow different configurations in mainnet and testnet.
func setBaseDenom(ci evmtypes.EvmCoinInfo) (err error) {
// Defer setting the base denom, and capture any potential error from it.
// So when failing because the denom was already registered, we ignore it and set
// the corresponding denom to be base denom
defer func() {
err = sdk.SetBaseDenom(ci.Denom)
}()
if err := sdk.RegisterDenom(ci.DisplayDenom, math.LegacyOneDec()); err != nil {
return err
}

// sdk.RegisterDenom will automatically overwrite the base denom when the
// new setBaseDenom() units are lower than the current base denom's units.
return sdk.RegisterDenom(ci.Denom, math.LegacyNewDecWithPrec(1, int64(ci.Decimals)))
return evmconfig.EvmAppOptionsWithConfigWithReset(chainID, testconfig.TestChainsCoinInfo, cosmosEVMActivators, true)
}
56 changes: 2 additions & 54 deletions evmd/cmd/evmd/config/evm_app_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,11 @@
package config

import (
"fmt"

evmtypes "github.com/cosmos/evm/x/vm/types"

"cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
evmconfig "github.com/cosmos/evm/config"
)

// EVMOptionsFn defines a function type for setting app options specifically for
// the Cosmos EVM app. The function should receive the chainID and return an error if
// any.
type EVMOptionsFn func(uint64) error

var sealed = false

// EvmAppOptions allows to setup the global configuration
// for the Cosmos EVM chain.
func EvmAppOptions(chainID uint64) error {
if sealed {
return nil
}

coinInfo, found := ChainsCoinInfo[chainID]
if !found {
return fmt.Errorf("unknown chain id: %d", chainID)
}

// set the denom info for the chain
if err := setBaseDenom(coinInfo); err != nil {
return err
}

ethCfg := evmtypes.DefaultChainConfig(chainID)

err := evmtypes.NewEVMConfigurator().
WithExtendedEips(cosmosEVMActivators).
WithChainConfig(ethCfg).
// NOTE: we're using the 18 decimals default for the example chain
WithEVMCoinInfo(coinInfo).
Configure()
if err != nil {
return err
}

sealed = true
return nil
}

// setBaseDenom registers the display denom and base denom and sets the
// base denom for the chain.
func setBaseDenom(ci evmtypes.EvmCoinInfo) error {
if err := sdk.RegisterDenom(ci.DisplayDenom, math.LegacyOneDec()); err != nil {
return err
}

// sdk.RegisterDenom will automatically overwrite the base denom when the
// new setBaseDenom() are lower than the current base denom's units.
return sdk.RegisterDenom(ci.Denom, math.LegacyNewDecWithPrec(1, int64(ci.Decimals)))
return evmconfig.EvmAppOptionsWithConfig(chainID, ChainsCoinInfo, cosmosEVMActivators)
}
8 changes: 4 additions & 4 deletions evmd/eips/eips_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
//nolint:revive,ST1001 // dot imports are fine for Ginkgo
. "github.com/onsi/gomega"

"github.com/cosmos/evm/evmd/eips"
"github.com/cosmos/evm/evmd/eips/testdata"
"github.com/cosmos/evm/evmd/tests/integration"
testconfig "github.com/cosmos/evm/testutil/config"
"github.com/cosmos/evm/testutil/integration/evm/factory"
"github.com/cosmos/evm/testutil/integration/evm/grpc"
"github.com/cosmos/evm/testutil/integration/evm/network"
Expand Down Expand Up @@ -107,7 +107,7 @@ func RunTests(t *testing.T, create network.CreateEvmApp, options ...network.Conf
})

It("should enable the new EIP", func() {
eips.Multiplier = eipMultiplier
testconfig.Multiplier = eipMultiplier
newEIP := 0o000

qRes, err := gh.GetEvmParams()
Expand Down Expand Up @@ -259,7 +259,7 @@ func RunTests(t *testing.T, create network.CreateEvmApp, options ...network.Conf
Expect(counter.String()).To(Equal(fmt.Sprintf("%d", initialCounterValue+1)), "counter is not correct")
})
It("should enable the new EIP", func() {
eips.Multiplier = eipMultiplier
testconfig.Multiplier = eipMultiplier
newEIP := 0o001

qRes, err := gh.GetEvmParams()
Expand Down Expand Up @@ -403,7 +403,7 @@ func RunTests(t *testing.T, create network.CreateEvmApp, options ...network.Conf
})

It("should enable the new EIP", func() {
eips.SstoreConstantGas = constantGas
testconfig.SstoreConstantGas = constantGas
newEIP := 0o002

qRes, err := gh.GetEvmParams()
Expand Down
7 changes: 4 additions & 3 deletions evmd/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"fmt"
"testing"

"github.com/cosmos/evm/evmd/cmd/evmd/config"
testconfig "github.com/cosmos/evm/testutil/config"
"github.com/stretchr/testify/require"

abci "github.com/cometbft/cometbft/abci/types"
cmttypes "github.com/cometbft/cometbft/types"

dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/evm/evmd/cmd/evmd/config"
feemarkettypes "github.com/cosmos/evm/x/feemarket/types"
ibctesting "github.com/cosmos/ibc-go/v10/testing"

Expand Down Expand Up @@ -54,7 +55,7 @@ func setup(withGenesis bool, invCheckPeriod uint, chainID string, evmChainID uin
appOptions[flags.FlagHome] = defaultNodeHome
appOptions[server.FlagInvCheckPeriod] = invCheckPeriod

app := NewExampleApp(log.NewNopLogger(), db, nil, true, appOptions, evmChainID, config.EvmAppOptions, baseapp.SetChainID(chainID))
app := NewExampleApp(log.NewNopLogger(), db, nil, true, appOptions, evmChainID, testconfig.EvmAppOptions, baseapp.SetChainID(chainID))
if withGenesis {
return app, app.DefaultGenesis()
}
Expand Down Expand Up @@ -131,7 +132,7 @@ func SetupTestingApp(chainID string, evmChainID uint64) func() (ibctesting.Testi
db, nil, true,
simtestutil.NewAppOptionsWithFlagHome(defaultNodeHome),
evmChainID,
config.EvmAppOptions,
testconfig.EvmAppOptions,
baseapp.SetChainID(chainID),
)
return app, app.DefaultGenesis()
Expand Down
Loading
Loading