Skip to content

Commit 98be39e

Browse files
committed
evmc: Simplify EVMC initialization
1 parent 1aa1499 commit 98be39e

File tree

5 files changed

+47
-67
lines changed

5 files changed

+47
-67
lines changed

cmd/evm/runner.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ func runCmd(ctx *cli.Context) error {
183183
},
184184
}
185185

186-
vm.InitEVMC(runtimeConfig.EVMConfig.EVMInterpreter, runtimeConfig.EVMConfig.EWASMInterpreter)
186+
if runtimeConfig.EVMConfig.EVMInterpreter != "" {
187+
vm.InitEVMCEVM(runtimeConfig.EVMConfig.EVMInterpreter)
188+
}
187189

188190
if cpuProfilePath := ctx.GlobalString(CPUProfileFlag.Name); cpuProfilePath != "" {
189191
f, err := os.Create(cpuProfilePath)

cmd/utils/flags.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,10 +1449,12 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
14491449

14501450
if ctx.GlobalIsSet(EWASMInterpreterFlag.Name) {
14511451
cfg.EWASMInterpreter = ctx.GlobalString(EWASMInterpreterFlag.Name)
1452+
vm.InitEVMCEwasm(cfg.EWASMInterpreter)
14521453
}
14531454

14541455
if ctx.GlobalIsSet(EVMInterpreterFlag.Name) {
14551456
cfg.EVMInterpreter = ctx.GlobalString(EVMInterpreterFlag.Name)
1457+
vm.InitEVMCEVM(cfg.EVMInterpreter)
14561458
}
14571459
if ctx.GlobalIsSet(RPCGlobalGasCap.Name) {
14581460
cfg.RPCGasCap = new(big.Int).SetUint64(ctx.GlobalUint64(RPCGlobalGasCap.Name))
@@ -1502,8 +1504,6 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
15021504
cfg.Miner.GasPrice = big.NewInt(1)
15031505
}
15041506
}
1505-
1506-
vm.InitEVMC(cfg.EVMInterpreter, cfg.EWASMInterpreter)
15071507
}
15081508

15091509
// SetDashboardConfig applies dashboard related command line flags to the config.

core/vm/evm.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,14 @@ func NewEVM(ctx Context, statedb StateDB, chainConfig *params.ChainConfig, vmCon
144144

145145
if chainConfig.IsEWASM(ctx.BlockNumber) {
146146
if vmConfig.EWASMInterpreter != "" {
147-
evm.interpreters = append(evm.interpreters, NewEVMC(evmc.CapabilityEWASM, vmConfig.EWASMInterpreter, evm))
147+
evm.interpreters = append(evm.interpreters, &EVMC{ewasmModule, evm, evmc.CapabilityEWASM, false})
148148
} else {
149149
panic("The default ewasm interpreter not supported yet.")
150150
}
151151
}
152152

153153
if vmConfig.EVMInterpreter != "" {
154-
evm.interpreters = append(evm.interpreters, NewEVMC(evmc.CapabilityEVM1, vmConfig.EVMInterpreter, evm))
154+
evm.interpreters = append(evm.interpreters, &EVMC{evmModule, evm, evmc.CapabilityEVM1, false})
155155
} else {
156156
evm.interpreters = append(evm.interpreters, NewEVMInterpreter(evm, vmConfig))
157157
}

core/vm/evmc.go

Lines changed: 34 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"fmt"
2525
"math/big"
2626
"strings"
27-
"sync"
2827

2928
"github.com/ethereum/evmc/bindings/go/evmc"
3029
"github.com/ethereum/go-ethereum/common"
@@ -42,77 +41,51 @@ type EVMC struct {
4241
readOnly bool // The readOnly flag (TODO: Try to get rid of it).
4342
}
4443

45-
// EVMCModule represents the loaded EVMC module.
46-
type EVMCModule struct {
47-
instance *evmc.Instance // The EVMC VM instance.
48-
once sync.Once // The mutex protecting EVMC VM instance creation.
49-
}
50-
5144
var (
52-
evmModule EVMCModule
53-
ewasmModule EVMCModule
45+
evmModule *evmc.Instance
46+
ewasmModule *evmc.Instance
5447
)
5548

56-
// InitEVMC initializes the EVMC modules.
57-
func InitEVMC(evmConfig string, ewasmConfig string) {
58-
if evmConfig != "" {
59-
initEVMC(&evmModule, evmc.CapabilityEVM1, evmConfig)
60-
}
61-
if ewasmConfig != "" {
62-
initEVMC(&ewasmModule, evmc.CapabilityEWASM, ewasmConfig)
63-
}
49+
func InitEVMCEVM(config string) {
50+
evmModule = initEVMC(evmc.CapabilityEVM1, config)
6451
}
6552

66-
func initEVMC(module *EVMCModule, cap evmc.Capability, config string) {
67-
module.once.Do(func() {
68-
options := strings.Split(config, ",")
69-
path := options[0]
53+
func InitEVMCEwasm(config string) {
54+
ewasmModule = initEVMC(evmc.CapabilityEWASM, config)
55+
}
7056

71-
if path == "" {
72-
panic("EVMC VM path not provided, set --vm.(evm|ewasm)=/path/to/vm")
73-
}
57+
func initEVMC(cap evmc.Capability, config string) *evmc.Instance {
58+
options := strings.Split(config, ",")
59+
path := options[0]
7460

75-
var err error
76-
module.instance, err = evmc.Load(path)
77-
if err != nil {
78-
panic(err.Error())
79-
}
80-
log.Info("EVMC VM loaded", "name", module.instance.Name(), "version", module.instance.Version(), "path", path)
81-
82-
// Set options before checking capabilities.
83-
for _, option := range options[1:] {
84-
if idx := strings.Index(option, "="); idx >= 0 {
85-
name := option[:idx]
86-
value := option[idx+1:]
87-
err := module.instance.SetOption(name, value)
88-
if err == nil {
89-
log.Info("EVMC VM option set", "name", name, "value", value)
90-
} else {
91-
log.Warn("EVMC VM option setting failed", "name", name, "error", err)
92-
}
93-
}
94-
}
61+
if path == "" {
62+
panic("EVMC VM path not provided, set --vm.(evm|ewasm)=/path/to/vm")
63+
}
9564

96-
if !module.instance.HasCapability(cap) {
97-
panic(fmt.Errorf("The EVMC module %s does not have requested capability %d", path, cap))
65+
instance, err := evmc.Load(path)
66+
if err != nil {
67+
panic(err.Error())
68+
}
69+
log.Info("EVMC VM loaded", "name", instance.Name(), "version", instance.Version(), "path", path)
70+
71+
// Set options before checking capabilities.
72+
for _, option := range options[1:] {
73+
if idx := strings.Index(option, "="); idx >= 0 {
74+
name := option[:idx]
75+
value := option[idx+1:]
76+
err := instance.SetOption(name, value)
77+
if err == nil {
78+
log.Info("EVMC VM option set", "name", name, "value", value)
79+
} else {
80+
log.Warn("EVMC VM option setting failed", "name", name, "error", err)
81+
}
9882
}
99-
})
100-
}
101-
102-
// NewEVMC creates new EVMC-based VM execution context.
103-
func NewEVMC(cap evmc.Capability, config string, env *EVM) *EVMC {
104-
var module *EVMCModule
105-
106-
switch cap {
107-
case evmc.CapabilityEVM1:
108-
module = &evmModule
109-
case evmc.CapabilityEWASM:
110-
module = &ewasmModule
111-
default:
112-
panic(fmt.Errorf("EVMC: Unknown capability %d", cap))
11383
}
11484

115-
return &EVMC{module.instance, env, cap, false}
85+
if !instance.HasCapability(cap) {
86+
panic(fmt.Errorf("The EVMC module %s does not have requested capability %d", path, cap))
87+
}
88+
return instance
11689
}
11790

11891
// hostContext implements evmc.HostContext interface.

tests/state_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ var testVMConfig = func() vm.Config {
7777
flag.StringVar(&vmconfig.EVMInterpreter, utils.EVMInterpreterFlag.Name, utils.EVMInterpreterFlag.Value, utils.EVMInterpreterFlag.Usage)
7878
flag.StringVar(&vmconfig.EWASMInterpreter, utils.EWASMInterpreterFlag.Name, utils.EWASMInterpreterFlag.Value, utils.EWASMInterpreterFlag.Usage)
7979
flag.Parse()
80-
vm.InitEVMC(vmconfig.EVMInterpreter, vmconfig.EWASMInterpreter)
80+
if vmconfig.EVMInterpreter != "" {
81+
vm.InitEVMCEVM(vmconfig.EVMInterpreter)
82+
}
83+
if vmconfig.EWASMInterpreter != "" {
84+
vm.InitEVMCEwasm(vmconfig.EWASMInterpreter)
85+
}
8186
return vmconfig
8287
}()
8388

0 commit comments

Comments
 (0)