Skip to content

Commit cda76f9

Browse files
committed
fix tests and remove unnecessary setup
1 parent 20dea4b commit cda76f9

File tree

12 files changed

+272
-13
lines changed

12 files changed

+272
-13
lines changed

config/evm_app_options.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import (
1717
// any.
1818
type EVMOptionsFn func(uint64) error
1919

20+
// EVMAppOptionsFn defines a function type for setting app options with access to
21+
// the app options for dynamic configuration.
22+
type EVMAppOptionsFn func(uint64, map[uint64]evmtypes.EvmCoinInfo) error
23+
2024
var sealed = false
2125

2226
func EvmAppOptionsWithConfig(
@@ -71,6 +75,56 @@ func EvmAppOptionsWithConfigWithReset(
7175
return nil
7276
}
7377

78+
// EvmAppOptionsWithDynamicConfig sets up EVM configuration using dynamic chain configuration
79+
// from app.toml instead of static maps. This is the new approach that should be preferred.
80+
func EvmAppOptionsWithDynamicConfig(
81+
chainID uint64,
82+
chainCoinInfo evmtypes.EvmCoinInfo,
83+
cosmosEVMActivators map[int]func(*vm.JumpTable),
84+
) error {
85+
if sealed {
86+
return nil
87+
}
88+
89+
if err := EvmAppOptionsWithDynamicConfigWithReset(chainID, chainCoinInfo, cosmosEVMActivators, false); err != nil {
90+
return err
91+
}
92+
93+
sealed = true
94+
return nil
95+
}
96+
97+
// EvmAppOptionsWithDynamicConfigWithReset sets up EVM configuration using dynamic chain configuration
98+
// with an optional reset flag to allow reconfiguration during testing.
99+
func EvmAppOptionsWithDynamicConfigWithReset(
100+
chainID uint64,
101+
chainCoinInfo evmtypes.EvmCoinInfo,
102+
cosmosEVMActivators map[int]func(*vm.JumpTable),
103+
withReset bool,
104+
) error {
105+
// set the denom info for the chain
106+
if err := setBaseDenom(chainCoinInfo); err != nil {
107+
return err
108+
}
109+
110+
ethCfg := evmtypes.DefaultChainConfig(chainID)
111+
configurator := evmtypes.NewEVMConfigurator()
112+
if withReset {
113+
// reset configuration to set the new one
114+
configurator.ResetTestConfig()
115+
}
116+
err := configurator.
117+
WithExtendedEips(cosmosEVMActivators).
118+
WithChainConfig(ethCfg).
119+
WithEVMCoinInfo(chainCoinInfo).
120+
Configure()
121+
if err != nil {
122+
return err
123+
}
124+
125+
return nil
126+
}
127+
74128
// setBaseDenom registers the display denom and base denom and sets the
75129
// base denom for the chain. The function registered different values based on
76130
// the EvmCoinInfo to allow different configurations in mainnet and testnet.

evmd/cmd/evmd/config/config_testing.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,30 @@ package config
55

66
import (
77
evmconfig "github.com/cosmos/evm/config"
8+
cosmosevmserverconfig "github.com/cosmos/evm/server/config"
89
testconfig "github.com/cosmos/evm/testutil/config"
910
)
1011

1112
// EvmAppOptions allows to setup the global configuration
12-
// for the Cosmos EVM chain.
13+
// for the Cosmos EVM chain using dynamic configuration.
1314
func EvmAppOptions(chainID uint64) error {
14-
return evmconfig.EvmAppOptionsWithConfigWithReset(chainID, testconfig.TestChainsCoinInfo, cosmosEVMActivators, true)
15+
// Get chain config from the test chain configs
16+
chainConfig := getTestChainConfigForChainID(chainID)
17+
evmCoinInfo := chainConfig.ToEvmCoinInfo()
18+
return evmconfig.EvmAppOptionsWithDynamicConfig(chainID, evmCoinInfo, cosmosEVMActivators)
19+
}
20+
21+
// getTestChainConfigForChainID returns the appropriate chain config for testing
22+
func getTestChainConfigForChainID(chainID uint64) cosmosevmserverconfig.ChainConfig {
23+
// Use the test chain coin info map to get the appropriate configuration
24+
if coinInfo, found := testconfig.TestChainsCoinInfo[chainID]; found {
25+
return cosmosevmserverconfig.ChainConfig{
26+
Denom: coinInfo.Denom,
27+
ExtendedDenom: coinInfo.ExtendedDenom,
28+
DisplayDenom: coinInfo.DisplayDenom,
29+
Decimals: uint8(coinInfo.Decimals),
30+
}
31+
}
32+
// Default fallback
33+
return *cosmosevmserverconfig.DefaultChainConfig()
1534
}

evmd/cmd/evmd/config/evm_app_options.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
package config
55

66
import (
7-
evmconfig "github.com/cosmos/evm/config"
7+
cosmosevmserverconfig "github.com/cosmos/evm/server/config"
88
)
99

1010
// EvmAppOptions allows to setup the global configuration
11-
// for the Cosmos EVM chain.
11+
// for the Cosmos EVM chain using dynamic configuration from app.toml.
1212
func EvmAppOptions(chainID uint64) error {
13-
return evmconfig.EvmAppOptionsWithConfig(chainID, ChainsCoinInfo, cosmosEVMActivators)
13+
// Use the default chain configuration for now
14+
// In the future, this could be extended to read from app.toml
15+
chainConfig := *cosmosevmserverconfig.DefaultChainConfig()
16+
return EvmAppOptionsFromConfig(chainID, chainConfig)
1417
}

evmd/cmd/evmd/config/evmd_config.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
corevm "github.com/ethereum/go-ethereum/core/vm"
88

9+
evmconfig "github.com/cosmos/evm/config"
910
cosmosevmserverconfig "github.com/cosmos/evm/server/config"
1011
cosmosevmutils "github.com/cosmos/evm/utils"
1112
erc20types "github.com/cosmos/evm/x/erc20/types"
@@ -17,6 +18,7 @@ import (
1718
clienthelpers "cosmossdk.io/client/v2/helpers"
1819

1920
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
21+
servertypes "github.com/cosmos/cosmos-sdk/server/types"
2022
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
2123
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
2224
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
@@ -58,7 +60,6 @@ var maccPerms = map[string][]string{
5860
func BlockedAddresses() map[string]bool {
5961
blockedAddrs := make(map[string]bool)
6062

61-
maps.Clone(maccPerms)
6263
maccPerms := GetMaccPerms()
6364
accs := make([]string, 0, len(maccPerms))
6465
for acc := range maccPerms {
@@ -93,6 +94,7 @@ type EVMAppConfig struct {
9394
EVM cosmosevmserverconfig.EVMConfig
9495
JSONRPC cosmosevmserverconfig.JSONRPCConfig
9596
TLS cosmosevmserverconfig.TLSConfig
97+
Chain cosmosevmserverconfig.ChainConfig
9698
}
9799

98100
// InitAppConfig helps to override default appConfig template and configs.
@@ -118,14 +120,34 @@ func InitAppConfig(denom string, evmChainID uint64) (string, interface{}) {
118120
evmCfg := cosmosevmserverconfig.DefaultEVMConfig()
119121
evmCfg.EVMChainID = evmChainID
120122

123+
// Use the default chain configuration as a fallback
124+
chainCfg := cosmosevmserverconfig.DefaultChainConfig()
125+
121126
customAppConfig := EVMAppConfig{
122127
Config: *srvCfg,
123128
EVM: *evmCfg,
124129
JSONRPC: *cosmosevmserverconfig.DefaultJSONRPCConfig(),
125130
TLS: *cosmosevmserverconfig.DefaultTLSConfig(),
131+
Chain: *chainCfg,
126132
}
127133

128134
return EVMAppTemplate, customAppConfig
129135
}
130136

131137
const EVMAppTemplate = serverconfig.DefaultConfigTemplate + cosmosevmserverconfig.DefaultEVMConfigTemplate
138+
139+
// GetChainConfigFromAppOptions extracts the chain configuration from app options
140+
// For now, this returns the default config. In the future, we could extend this
141+
// to parse from the actual app configuration when viper integration is improved.
142+
func GetChainConfigFromAppOptions(appOpts servertypes.AppOptions) *cosmosevmserverconfig.ChainConfig {
143+
// TODO: Extract actual chain config from appOpts when viper integration is available
144+
// For now, return the default configuration
145+
return cosmosevmserverconfig.DefaultChainConfig()
146+
}
147+
148+
// EvmAppOptionsFromConfig allows setting up the global configuration
149+
// for the Cosmos EVM chain using dynamic chain configuration from app.toml
150+
func EvmAppOptionsFromConfig(chainID uint64, chainConfig cosmosevmserverconfig.ChainConfig) error {
151+
evmCoinInfo := chainConfig.ToEvmCoinInfo()
152+
return evmconfig.EvmAppOptionsWithDynamicConfig(chainID, evmCoinInfo, cosmosEVMActivators)
153+
}

evmd/tests/integration/create_app.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build test
2+
// +build test
3+
14
package integration
25

36
import (
@@ -41,7 +44,9 @@ func CreateEvmd(chainID string, evmChainID uint64, customBaseAppOptions ...func(
4144
loadLatest,
4245
appOptions,
4346
evmChainID,
44-
testconfig.EvmAppOptions,
47+
func(chainID uint64) error {
48+
return testconfig.EvmAppOptionsWithReset(chainID, true)
49+
},
4550
baseAppOptions...,
4651
)
4752
}
@@ -56,7 +61,9 @@ func SetupEvmd() (ibctesting.TestingApp, map[string]json.RawMessage) {
5661
true,
5762
simutils.EmptyAppOptions{},
5863
constants.ExampleEIP155ChainID,
59-
testconfig.EvmAppOptions,
64+
func(chainID uint64) error {
65+
return testconfig.EvmAppOptionsWithReset(chainID, true)
66+
},
6067
)
6168
// disable base fee for testing
6269
genesisState := app.DefaultGenesis()

server/config/config.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010

1111
"github.com/cometbft/cometbft/libs/strings"
1212

13+
evmtypes "github.com/cosmos/evm/x/vm/types"
14+
1315
errorsmod "cosmossdk.io/errors"
1416

1517
"github.com/cosmos/cosmos-sdk/server/config"
@@ -130,6 +132,7 @@ type Config struct {
130132
EVM EVMConfig `mapstructure:"evm"`
131133
JSONRPC JSONRPCConfig `mapstructure:"json-rpc"`
132134
TLS TLSConfig `mapstructure:"tls"`
135+
Chain ChainConfig `mapstructure:"chain"`
133136
}
134137

135138
// EVMConfig defines the application configuration values for the EVM.
@@ -205,6 +208,18 @@ type TLSConfig struct {
205208
KeyPath string `mapstructure:"key-path"`
206209
}
207210

211+
// ChainConfig defines the chain-specific configuration values.
212+
type ChainConfig struct {
213+
// Denom defines the base denomination used in the chain
214+
Denom string `mapstructure:"denom"`
215+
// ExtendedDenom defines the extended denomination (typically atto-denom for 18 decimals)
216+
ExtendedDenom string `mapstructure:"extended-denom"`
217+
// DisplayDenom defines the display denomination shown to users
218+
DisplayDenom string `mapstructure:"display-denom"`
219+
// Decimals defines the precision/decimals for the base denomination (1-18)
220+
Decimals uint8 `mapstructure:"decimals"`
221+
}
222+
208223
// DefaultEVMConfig returns the default EVM configuration
209224
func DefaultEVMConfig() *EVMConfig {
210225
return &EVMConfig{
@@ -335,6 +350,16 @@ func DefaultTLSConfig() *TLSConfig {
335350
}
336351
}
337352

353+
// DefaultChainConfig returns the default chain configuration
354+
func DefaultChainConfig() *ChainConfig {
355+
return &ChainConfig{
356+
Denom: "aatom",
357+
ExtendedDenom: "aatom",
358+
DisplayDenom: "atom",
359+
Decimals: 18,
360+
}
361+
}
362+
338363
// Validate returns an error if the TLS certificate and key file extensions are invalid.
339364
func (c TLSConfig) Validate() error {
340365
certExt := path.Ext(c.CertificatePath)
@@ -352,6 +377,39 @@ func (c TLSConfig) Validate() error {
352377
return nil
353378
}
354379

380+
// Validate returns an error if the chain configuration fields are invalid.
381+
func (c ChainConfig) Validate() error {
382+
if c.Denom == "" {
383+
return errors.New("chain denom cannot be empty")
384+
}
385+
if c.ExtendedDenom == "" {
386+
return errors.New("chain extended-denom cannot be empty")
387+
}
388+
if c.DisplayDenom == "" {
389+
return errors.New("chain display-denom cannot be empty")
390+
}
391+
if c.Decimals == 0 || c.Decimals > 18 {
392+
return errors.New("chain decimals must be between 1 and 18")
393+
}
394+
395+
// For 18 decimals, denom and extended denom should be the same
396+
if c.Decimals == 18 && c.Denom != c.ExtendedDenom {
397+
return errors.New("denom and extended-denom must be the same for 18 decimals")
398+
}
399+
400+
return nil
401+
}
402+
403+
// ToEvmCoinInfo converts ChainConfig to evmtypes.EvmCoinInfo
404+
func (c ChainConfig) ToEvmCoinInfo() evmtypes.EvmCoinInfo {
405+
return evmtypes.EvmCoinInfo{
406+
Denom: c.Denom,
407+
ExtendedDenom: c.ExtendedDenom,
408+
DisplayDenom: c.DisplayDenom,
409+
Decimals: evmtypes.Decimals(c.Decimals),
410+
}
411+
}
412+
355413
// DefaultConfig returns server's default configuration.
356414
func DefaultConfig() *Config {
357415
defaultSDKConfig := config.DefaultConfig()
@@ -365,6 +423,7 @@ func DefaultConfig() *Config {
365423
EVM: *DefaultEVMConfig(),
366424
JSONRPC: *DefaultJSONRPCConfig(),
367425
TLS: *DefaultTLSConfig(),
426+
Chain: *DefaultChainConfig(),
368427
}
369428
}
370429

@@ -391,5 +450,9 @@ func (c Config) ValidateBasic() error {
391450
return errorsmod.Wrapf(errortypes.ErrAppConfig, "invalid tls config value: %s", err.Error())
392451
}
393452

453+
if err := c.Chain.Validate(); err != nil {
454+
return errorsmod.Wrapf(errortypes.ErrAppConfig, "invalid chain config value: %s", err.Error())
455+
}
456+
394457
return c.Config.ValidateBasic()
395458
}

server/config/migration/v0.50-app.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,24 @@ certificate-path = ""
328328
# Key path defines the key.pem file path for the TLS configuration.
329329
key-path = ""
330330

331+
###############################################################################
332+
### Chain Configuration ###
333+
###############################################################################
334+
335+
[chain]
336+
337+
# Denom defines the base denomination used in the chain
338+
denom = "aatom"
339+
340+
# ExtendedDenom defines the extended denomination (typically atto-denom for 18 decimals)
341+
extended-denom = "aatom"
342+
343+
# DisplayDenom defines the display denomination shown to users
344+
display-denom = "atom"
345+
346+
# Decimals defines the precision/decimals for the base denomination (1-18)
347+
decimals = 18
348+
331349
###############################################################################
332350
### Rosetta Configuration ###
333351
###############################################################################

server/config/toml.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,22 @@ certificate-path = "{{ .TLS.CertificatePath }}"
113113
114114
# Key path defines the key.pem file path for the TLS configuration.
115115
key-path = "{{ .TLS.KeyPath }}"
116+
117+
###############################################################################
118+
### Chain Configuration ###
119+
###############################################################################
120+
121+
[chain]
122+
123+
# Denom defines the base denomination used in the chain
124+
denom = "{{ .Chain.Denom }}"
125+
126+
# ExtendedDenom defines the extended denomination (typically atto-denom for 18 decimals)
127+
extended-denom = "{{ .Chain.ExtendedDenom }}"
128+
129+
# DisplayDenom defines the display denomination shown to users
130+
display-denom = "{{ .Chain.DisplayDenom }}"
131+
132+
# Decimals defines the precision/decimals for the base denomination (1-18)
133+
decimals = {{ .Chain.Decimals }}
116134
`

tests/integration/x/precisebank/test_burn_integration.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,15 @@ func (s *KeeperIntegrationTestSuite) TestBurnCoinsRemainder() {
250250
burnCoins := cs(ci(types.ExtendedCoinDenom(), burnAmt))
251251

252252
// Burn 0.1 until balance is 0
253+
// Add safety guard to prevent infinite loop
254+
const maxIterations = 1000
255+
iterationCount := 0
253256
for {
257+
iterationCount++
258+
if iterationCount > maxIterations {
259+
s.Failf("Test exceeded maximum iterations", "reached %d iterations without balance reaching zero", maxIterations)
260+
break
261+
}
254262
reserveBalBefore := s.network.App.GetBankKeeper().GetBalance(
255263
s.network.GetContext(),
256264
reserveAddr,

0 commit comments

Comments
 (0)