Skip to content

Commit 8383a5b

Browse files
committed
evmc: Handle EVMC initialization when processing flags
1 parent 12ba5ca commit 8383a5b

File tree

2 files changed

+44
-38
lines changed

2 files changed

+44
-38
lines changed

core/vm/evm.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (sel Selector) String() string {
3535
if sel == "" {
3636
return "interpreter"
3737
}
38-
return "unknown"
38+
return string(sel)
3939
}
4040

4141
// MarshalText implements encoding.TextMarshaler interface.
@@ -52,7 +52,17 @@ func (sel *Selector) UnmarshalText(text []byte) error {
5252
*sel = ""
5353
return nil
5454
}
55-
return fmt.Errorf(`unknown option %q, want "interpreter"`, text)
55+
createMu.Lock()
56+
defer createMu.Unlock()
57+
if evmcInstance != nil {
58+
return fmt.Errorf("only single EVMC module is currently supported")
59+
}
60+
instance, err := initEvmcModule(string(text))
61+
if err != nil {
62+
return err
63+
}
64+
evmcInstance = instance
65+
return nil
5666
}
5767

5868
// emptyCodeHash is used by create to ensure deployment is disallowed to already

core/vm/evmc.go

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/ethereum/evmc/bindings/go/evmc"
3030
"github.com/ethereum/go-ethereum/common"
3131
"github.com/ethereum/go-ethereum/core/types"
32-
"github.com/ethereum/go-ethereum/log"
3332
"github.com/ethereum/go-ethereum/params"
3433
)
3534

@@ -47,48 +46,45 @@ var (
4746
evmcInstance *evmc.Instance // The EVMC VM instance.
4847
)
4948

50-
// NewEVMC creates new EVMC-based VM execution context.
51-
func NewEVMC(config string, env *EVM) *EVMC {
52-
createMu.Lock()
53-
defer createMu.Unlock()
54-
55-
if evmcInstance == nil {
56-
options := strings.Split(config, ",")
57-
path := options[0]
49+
func initEvmcModule(config string) (*evmc.Instance, error) {
50+
options := strings.Split(config, ",")
51+
path := options[0]
5852

59-
if path == "" {
60-
panic("EVMC VM path not provided, set --vm.(evm|ewasm)=/path/to/vm")
61-
}
53+
instance, err := evmc.Load(path)
54+
if err != nil {
55+
return nil, err
56+
}
6257

63-
var err error
64-
evmcInstance, err = evmc.Load(path)
65-
if err != nil {
66-
panic(err.Error())
67-
}
68-
log.Info("EVMC VM loaded", "name", evmcInstance.Name(), "version", evmcInstance.Version(), "path", path)
69-
70-
for _, option := range options[1:] {
71-
if idx := strings.Index(option, "="); idx >= 0 {
72-
name := option[:idx]
73-
value := option[idx+1:]
74-
err := evmcInstance.SetOption(name, value)
75-
if err == nil {
76-
log.Info("EVMC VM option set", "name", name, "value", value)
77-
} else {
78-
log.Warn("EVMC VM option setting failed", "name", name, "error", err)
79-
}
58+
// log.Info("EVMC VM loaded", "name", instance.Name(), "version", instance.Version(), "path", path)
59+
fmt.Printf("EVMC VM loaded %s %s %s\n", instance.Name(), instance.Version(), path)
60+
61+
for _, option := range options[1:] {
62+
if idx := strings.Index(option, "="); idx >= 0 {
63+
name := option[:idx]
64+
value := option[idx+1:]
65+
err := instance.SetOption(name, value)
66+
if err == nil {
67+
// log.Info("EVMC VM option set", "name", name, "value", value)
68+
fmt.Printf("EVMC VM option set %s=%s\n", name, value)
69+
} else {
70+
return nil, fmt.Errorf("EVMC VM option setting failed '%s=%s'", name, value)
8071
}
8172
}
73+
}
8274

83-
evm1Cap := evmcInstance.HasCapability(evmc.CapabilityEVM1)
84-
ewasmCap := evmcInstance.HasCapability(evmc.CapabilityEWASM)
85-
log.Info("EVMC VM capabilities", "evm1", evm1Cap, "ewasm", ewasmCap)
75+
evm1Cap := instance.HasCapability(evmc.CapabilityEVM1)
76+
ewasmCap := instance.HasCapability(evmc.CapabilityEWASM)
77+
// log.Info("EVMC VM capabilities", "evm1", evm1Cap, "ewasm", ewasmCap)
78+
fmt.Printf("EVMC VM capabilities: evm=%t ewasm=%t\n", evm1Cap, ewasmCap)
8679

87-
evmcConfig = config // Remember the config.
88-
} else if evmcConfig != config {
89-
log.Error("New EVMC VM requested", "newconfig", config, "oldconfig", evmcConfig)
90-
}
80+
return instance, nil
81+
}
9182

83+
// NewEVMC creates new EVMC-based VM execution context.
84+
func NewEVMC(config string, env *EVM) *EVMC {
85+
if evmcInstance == nil {
86+
panic("EVMC module not loaded")
87+
}
9288
return &EVMC{evmcInstance, env, false}
9389
}
9490

0 commit comments

Comments
 (0)