From f033a404828857f2657a066a1261b266009aa11a Mon Sep 17 00:00:00 2001 From: bhartnett <51288821+bhartnett@users.noreply.github.com> Date: Mon, 10 Feb 2025 10:53:31 +0800 Subject: [PATCH] Build nimbus evmc shared library and fix issue to enable loading (#3050) --- Makefile | 20 +++++++++------ nimbus/transaction/evmc_dynamic_loader.nim | 8 +++--- nimbus/transaction/evmc_vm_glue.nim | 30 +++++++++++----------- 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index 455c2b34cb..9b34960628 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -# Copyright (c) 2018-2024 Status Research & Development GmbH. Licensed under +# Copyright (c) 2018-2025 Status Research & Development GmbH. Licensed under # either of: # - Apache License, version 2.0 # - MIT license @@ -83,14 +83,14 @@ FLUFFY_TOOLS_DIRS := \ FLUFFY_TOOLS_CSV := $(subst $(SPACE),$(COMMA),$(FLUFFY_TOOLS)) # Namespaced variables to avoid conflicts with other makefiles -VERIF_PROXY_OS = $(shell $(CC) -dumpmachine) -ifneq (, $(findstring darwin, $(VERIF_PROXY_OS))) - VERIF_PROXY_SHAREDLIBEXT = dylib +OS_PLATFORM = $(shell $(CC) -dumpmachine) +ifneq (, $(findstring darwin, $(OS_PLATFORM))) + SHAREDLIBEXT = dylib else -ifneq (, $(findstring mingw, $(VERIF_PROXY_OS))$(findstring cygwin, $(VERIF_PROXY_OS))$(findstring msys, $(VERIF_PROXY_OS))) - VERIF_PROXY_SHAREDLIBEXT = dll +ifneq (, $(findstring mingw, $(OS_PLATFORM))$(findstring cygwin, $(OS_PLATFORM))$(findstring msys, $(OS_PLATFORM))) + SHAREDLIBEXT = dll else - VERIF_PROXY_SHAREDLIBEXT = so + SHAREDLIBEXT = so endif endif @@ -333,7 +333,7 @@ nimbus-verified-proxy-test: | build deps rocksdb libverifproxy: | build deps rocksdb + echo -e $(BUILD_MSG) "build/$@" && \ $(ENV_SCRIPT) nim --version && \ - $(ENV_SCRIPT) nim c --app:lib -d:"libp2p_pki_schemes=secp256k1" --noMain:on --threads:on --nimcache:nimcache/libverifproxy -o:$(VERIF_PROXY_OUT_PATH)/$@.$(VERIF_PROXY_SHAREDLIBEXT) $(NIM_PARAMS) nimbus_verified_proxy/libverifproxy/verifproxy.nim + $(ENV_SCRIPT) nim c --app:lib -d:"libp2p_pki_schemes=secp256k1" --noMain:on --threads:on --nimcache:nimcache/libverifproxy -o:$(VERIF_PROXY_OUT_PATH)/$@.$(SHAREDLIBEXT) $(NIM_PARAMS) nimbus_verified_proxy/libverifproxy/verifproxy.nim cp nimbus_verified_proxy/libverifproxy/verifproxy.h $(VERIF_PROXY_OUT_PATH)/ echo -e $(BUILD_END_MSG) "build/$@" @@ -357,6 +357,10 @@ evmstate_test: | build deps evmstate txparse: | build deps $(ENV_SCRIPT) nim c $(NIM_PARAMS) "tools/txparse/$@.nim" +# builds the nimbus evmc shared library +libnimbusevm: | build deps + $(ENV_SCRIPT) nim c $(NIM_PARAMS) -d:evmc_enabled --app:lib --noMain -o:build/libnimbusevm/$@.$(SHAREDLIBEXT) nimbus/transaction/evmc_vm_glue.nim + # usual cleaning clean: | clean-common rm -rf build/{nimbus,nimbus_execution_client,fluffy,libverifproxy,nimbus_verified_proxy,$(TOOLS_CSV),$(FLUFFY_TOOLS_CSV),all_tests,test_kvstore_rocksdb,test_rpc,all_fluffy_tests,all_history_network_custom_chain_tests,test_portal_testnet,utp_test_app,utp_test,*.dSYM} diff --git a/nimbus/transaction/evmc_dynamic_loader.nim b/nimbus/transaction/evmc_dynamic_loader.nim index fbd459aabc..8633d68f45 100644 --- a/nimbus/transaction/evmc_dynamic_loader.nim +++ b/nimbus/transaction/evmc_dynamic_loader.nim @@ -1,6 +1,6 @@ # Nimbus - Dynamic loader for EVM modules as shared libraries / DLLs # -# Copyright (c) 2019-2024 Status Research & Development GmbH +# Copyright (c) 2019-2025 Status Research & Development GmbH # Licensed under either of # * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) # * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) @@ -14,9 +14,9 @@ import evmc/evmc, ../config # The built-in Nimbus EVM, via imported C function. -proc evmc_create_nimbus_evm(): ptr evmc_vm {.cdecl, importc, raises: [], gcsafe.} +proc evmc_create_nimbusevm(): ptr evmc_vm {.cdecl, importc, raises: [], gcsafe.} -# Import this module to link in the definition of `evmc_create_nimbus_evm`. +# Import this module to link in the definition of `evmc_create_nimbusevm`. # Nim thinks the module is unused because the function is only called via # `.exportc` -> `.importc`. {.warning[UnusedImport]: off.}: @@ -36,7 +36,7 @@ proc evmcLoadVMGetCreateFn(): (evmc_create_vm_name_fn, string) = # Use built-in EVM if no other is specified. if path.len == 0: - return (evmc_create_nimbus_evm, "built-in") + return (evmc_create_nimbusevm, "built-in") # The steps below match the EVMC Loader documentation, copied here: # diff --git a/nimbus/transaction/evmc_vm_glue.nim b/nimbus/transaction/evmc_vm_glue.nim index 9730611321..335da1e9de 100644 --- a/nimbus/transaction/evmc_vm_glue.nim +++ b/nimbus/transaction/evmc_vm_glue.nim @@ -1,6 +1,6 @@ # Nimbus - Binary compatibility on the VM side of the EVMC API interface # -# Copyright (c) 2019-2024 Status Research & Development GmbH +# Copyright (c) 2019-2025 Status Research & Development GmbH # Licensed under either of # * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) # * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) @@ -82,24 +82,24 @@ proc evmcSetOption(vm: ptr evmc_vm, name, value: cstring): evmc_set_option_resul return EVMC_SET_OPTION_INVALID_NAME proc evmcDestroy(vm: ptr evmc_vm) {.cdecl.} = - GC_unref(cast[ref evmc_vm](vm)) + dealloc(vm) -proc evmc_create_nimbus_evm(): ptr evmc_vm {.cdecl, exportc.} = +proc evmc_create_nimbusevm*(): ptr evmc_vm {.cdecl, exportc, dynlib.} = ## Entry point to the Nimbus EVM, using an EVMC compatible interface. ## This is an exported C function. EVMC specifies the function must ## have this name format when exported from a shared library. - let vm = (ref evmc_vm)( - abi_version: EVMC_ABI_VERSION, - name: evmcName, - version: evmcVersion, - destroy: evmcDestroy, - execute: evmcExecute, - get_capabilities: evmcGetCapabilities, - set_option: evmcSetOption - ) - # Keep an extra reference on this, until `evmcDestroy` is called. - GC_ref(vm) - return cast[ptr evmc_vm](vm) + + let vm = cast[ptr evmc_vm](alloc(sizeof(evmc_vm))) + + vm.abi_version = EVMC_ABI_VERSION + vm.name = evmcName + vm.version = evmcVersion + vm.destroy = evmcDestroy + vm.execute = evmcExecute + vm.get_capabilities = evmcGetCapabilities + vm.set_option = evmcSetOption + + return vm # This code assumes fields, methods and types of ABI version 12, and must be # checked for compatibility if the `import evmc/evmc` major version is updated.