Skip to content

Commit 43b70ee

Browse files
yihuangvladjdk
andauthored
fix: non-deterministic evm pre-blocker (#729)
* fix: non-deterministic evm pre-blocker Closes: #728 Solution: - remove the state mutation out from the pre-blocker. - production chains should put the state mutation part in their upgrade handler. * Update CHANGELOG.md --------- Co-authored-by: Vlad J <[email protected]>
1 parent 172f11a commit 43b70ee

File tree

4 files changed

+55
-38
lines changed

4 files changed

+55
-38
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- [\#687](https://github.com/cosmos/evm/pull/687) Avoid blocking node shutdown when evm indexer is enabled, log startup failures instead of using errgroup.
2323
- [\#689](https://github.com/cosmos/evm/pull/689) Align debug addr for hex address.
2424
- [\#668](https://github.com/cosmos/evm/pull/668) Fix panic in legacy mempool when Reset() was called with a skipped header between old and new block.
25+
- [\#729](https://github.com/cosmos/evm/pull/729) Remove non-deterministic state mutation from EVM pre-blocker.
2526
- [\#725](https://github.com/cosmos/evm/pull/725) Fix inconsistent block hash in json-rpc.
2627
- [\#727](https://github.com/cosmos/evm/pull/727) Avoid nil pointer for `tx evm raw` due to uninitialized EVM coin info.
2728
- [\#730](https://github.com/cosmos/evm/pull/730) Fix panic if evm mempool not used.

x/vm/genesis.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,13 @@ func InitGenesis(
6060
}
6161
}
6262

63+
if err := k.InitEvmCoinInfo(ctx); err != nil {
64+
panic(fmt.Errorf("error initializing evm coin info: %s", err))
65+
}
66+
67+
coinInfo := k.GetEvmCoinInfo(ctx)
6368
initializer.Do(func() {
64-
SetGlobalConfigVariables(ctx, k, bankKeeper, data.Params)
69+
SetGlobalConfigVariables(coinInfo)
6570
})
6671

6772
if err := k.AddPreinstalls(ctx, data.Preinstalls); err != nil {

x/vm/keeper/coin_info.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,56 @@
11
package keeper
22

33
import (
4+
"fmt"
5+
46
"github.com/cosmos/evm/x/vm/types"
57

68
sdk "github.com/cosmos/cosmos-sdk/types"
79
)
810

11+
// LoadEvmCoinInfo load EvmCoinInfo from bank denom metadata
12+
func (k Keeper) LoadEvmCoinInfo(ctx sdk.Context) (types.EvmCoinInfo, error) {
13+
var decimals types.Decimals
14+
15+
params := k.GetParams(ctx)
16+
evmDenomMetadata, found := k.bankWrapper.GetDenomMetaData(ctx, params.EvmDenom)
17+
if !found {
18+
return types.EvmCoinInfo{}, fmt.Errorf("denom metadata %s could not be found", params.EvmDenom)
19+
}
20+
21+
for _, denomUnit := range evmDenomMetadata.DenomUnits {
22+
if denomUnit.Denom == evmDenomMetadata.Display {
23+
decimals = types.Decimals(denomUnit.Exponent)
24+
}
25+
}
26+
27+
var extendedDenom string
28+
if decimals == 18 {
29+
extendedDenom = params.EvmDenom
30+
} else {
31+
if params.ExtendedDenomOptions == nil {
32+
return types.EvmCoinInfo{}, fmt.Errorf("extended denom options cannot be nil for non-18-decimal chains")
33+
}
34+
extendedDenom = params.ExtendedDenomOptions.ExtendedDenom
35+
}
36+
37+
return types.EvmCoinInfo{
38+
Denom: params.EvmDenom,
39+
ExtendedDenom: extendedDenom,
40+
DisplayDenom: evmDenomMetadata.Display,
41+
Decimals: decimals.Uint32(),
42+
}, nil
43+
}
44+
45+
// InitEvmCoinInfo load EvmCoinInfo from bank denom metadata and store it in the module
46+
func (k Keeper) InitEvmCoinInfo(ctx sdk.Context) error {
47+
coinInfo, err := k.LoadEvmCoinInfo(ctx)
48+
if err != nil {
49+
return err
50+
}
51+
return k.SetEvmCoinInfo(ctx, coinInfo)
52+
}
53+
954
// GetEvmCoinInfo returns the EVM Coin Info stored in the module
1055
func (k Keeper) GetEvmCoinInfo(ctx sdk.Context) (coinInfo types.EvmCoinInfo) {
1156
store := ctx.KVStore(k.storeKey)

x/vm/module.go

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
138138

139139
func (am AppModule) PreBlock(goCtx context.Context) (appmodule.ResponsePreBlock, error) {
140140
ctx := sdk.UnwrapSDKContext(goCtx)
141-
params := am.keeper.GetParams(ctx)
141+
coinInfo := am.keeper.GetEvmCoinInfo(ctx)
142142
am.initializer.Do(func() {
143-
SetGlobalConfigVariables(ctx, am.keeper, am.bankKeeper, params)
143+
SetGlobalConfigVariables(coinInfo)
144144
})
145145
return &sdk.ResponsePreBlock{ConsensusParamsChanged: false}, nil
146146
}
@@ -212,46 +212,12 @@ func setBaseDenom(ci types.EvmCoinInfo) (err error) {
212212
return sdk.RegisterDenom(ci.Denom, math.LegacyNewDecWithPrec(1, int64(ci.Decimals)))
213213
}
214214

215-
func SetGlobalConfigVariables(ctx sdk.Context, vmKeeper *keeper.Keeper, bankKeeper types.BankKeeper, params types.Params) {
216-
var decimals types.Decimals
217-
218-
evmDenomMetadata, found := bankKeeper.GetDenomMetaData(ctx, params.EvmDenom)
219-
if !found {
220-
panic(fmt.Sprintf("denom metadata %s could not be found", params.EvmDenom))
221-
}
222-
223-
for _, denomUnit := range evmDenomMetadata.DenomUnits {
224-
if denomUnit.Denom == evmDenomMetadata.Display {
225-
decimals = types.Decimals(denomUnit.Exponent)
226-
}
227-
}
228-
229-
var extendedDenom string
230-
if decimals == 18 {
231-
extendedDenom = params.EvmDenom
232-
} else {
233-
if params.ExtendedDenomOptions == nil {
234-
panic(fmt.Errorf("extended denom options cannot be nil for non-18-decimal chains"))
235-
}
236-
extendedDenom = params.ExtendedDenomOptions.ExtendedDenom
237-
}
238-
239-
coinInfo := types.EvmCoinInfo{
240-
Denom: params.EvmDenom,
241-
ExtendedDenom: extendedDenom,
242-
DisplayDenom: evmDenomMetadata.Display,
243-
Decimals: decimals.Uint32(),
244-
}
245-
215+
func SetGlobalConfigVariables(coinInfo types.EvmCoinInfo) {
246216
// set the denom info for the chain
247217
if err := setBaseDenom(coinInfo); err != nil {
248218
panic(err)
249219
}
250220

251-
if err := vmKeeper.SetEvmCoinInfo(ctx, coinInfo); err != nil {
252-
panic(err)
253-
}
254-
255221
configurator := types.NewEVMConfigurator()
256222
err := configurator.
257223
WithExtendedEips(types.DefaultCosmosEVMActivators).

0 commit comments

Comments
 (0)