Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nimbus-verified-proxy integration with status-go #1572

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
branch = master
[submodule "vendor/nim-confutils"]
path = vendor/nim-confutils
url = https://github.com/status-im/nim-confutils.git
url = https://github.com/siphiuel/nim-confutils.git
ignore = dirty
branch = master
[submodule "vendor/nim-blscurve"]
Expand Down
25 changes: 24 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ FLUFFY_TOOLS_CSV := $(subst $(SPACE),$(COMMA),$(FLUFFY_TOOLS))
nimbus \
fluffy \
nimbus_verified_proxy \
libverifproxy \
test \
test-reproducibility \
clean \
Expand Down Expand Up @@ -299,9 +300,31 @@ evmstate_test: | build deps evmstate
txparse: | build deps
$(ENV_SCRIPT) nim c $(NIM_PARAMS) "tools/txparse/[email protected]"

# Shared library for verified proxy
OS = $(shell $(CC) -dumpmachine)
ifneq (, $(findstring darwin, $(OS)))
SHAREDLIBEXT = dylib
else
ifneq (, $(findstring mingw, $(OS))$(findstring cygwin, $(OS))$(findstring msys, $(OS)))
SHAREDLIBEXT = dll
else
SHAREDLIBEXT = so
endif
endif

VERIF_PROXY_OUT_PATH ?= build/libverifproxy/

libverifproxy: | build deps
+ 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)/$@.$(SHAREDLIBEXT) $(NIM_PARAMS) nimbus_verified_proxy/libverifproxy/verifproxy.nim
cp nimbus_verified_proxy/libverifproxy/verifproxy.h $(VERIF_PROXY_OUT_PATH)/
cp vendor/nimbus-build-system/vendor/Nim-csources-v1/c_code/nimbase.h $(VERIF_PROXY_OUT_PATH)/
echo -e $(BUILD_END_MSG) "build/$@"

# usual cleaning
clean: | clean-common
rm -rf build/{nimbus,fluffy,nimbus_verified_proxy,$(TOOLS_CSV),$(FLUFFY_TOOLS_CSV),all_tests,test_kvstore_rocksdb,test_rpc,all_fluffy_tests,all_fluffy_portal_spec_tests,test_portal_testnet,portalcli,blockwalk,eth_data_exporter,utp_test_app,utp_test,*.dSYM}
rm -rf build/{nimbus,fluffy,libverifproxy,nimbus_verified_proxy,$(TOOLS_CSV),$(FLUFFY_TOOLS_CSV),all_tests,test_kvstore_rocksdb,test_rpc,all_fluffy_tests,all_fluffy_portal_spec_tests,test_portal_testnet,portalcli,blockwalk,eth_data_exporter,utp_test_app,utp_test,*.dSYM}
rm -rf tools/t8n/{t8n,t8n_test}
rm -rf tools/evmstate/{evmstate,evmstate_test}
ifneq ($(USE_LIBBACKTRACE), 0)
Expand Down
7 changes: 5 additions & 2 deletions config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,11 @@ if not defined(windows):
--threads:on
--opt:speed
--excessiveStackTrace:on
# enable metric collection
--define:metrics
# enable metric collection
# (commented away for now due nimbus_verified proxy error: "metrics error:
# Adding a new combination of label values from another thread than the one
# in which the collector was created is not allowed")
#--define:metrics
# for heap-usage-by-instance-type metrics and object base-type strings
--define:nimTypeNames

Expand Down
26 changes: 26 additions & 0 deletions nimbus_verified_proxy/libverifproxy/verifproxy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* Generated by Nim Compiler v1.6.12 */
#ifndef __verifproxy__
#define __verifproxy__
#define NIM_INTBITS 64
#define NIM_EmulateOverflowChecks

#include "nimbase.h"
#undef LANGUAGE_C
#undef MIPSEB
#undef MIPSEL
#undef PPC
#undef R3000
#undef R4000
#undef i386
#undef linux
#undef mips
#undef near
#undef far
#undef powerpc
#undef unix
typedef struct VerifProxyContext VerifProxyContext;
typedef void (*onHeaderCallback)(NCSTRING s, int t);
void quit(void);
VerifProxyContext* startVerifProxy(NCSTRING configJson, onHeaderCallback onHeader);
void stopVerifProxy(VerifProxyContext*);
#endif /* __verifproxy__ */
82 changes: 82 additions & 0 deletions nimbus_verified_proxy/libverifproxy/verifproxy.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import
std/[atomics, json, os, strutils],
../nimbus_verified_proxy,
../nimbus_verified_proxy_conf

proc quit*() {.exportc, dynlib.} =
echo "Quitting"

proc NimMain() {.importc.}

var initialized: Atomic[bool]

proc initLib() =
if not initialized.exchange(true):
NimMain() # Every Nim library needs to call `NimMain` once exactly
when declared(setupForeignThreadGc): setupForeignThreadGc()
when declared(nimGC_setStackBottom):
var locals {.volatile, noinit.}: pointer
locals = addr(locals)
nimGC_setStackBottom(locals)


proc runContext(ctx: ptr Context) {.thread.} =
let str = $ctx.configJson
try:
let jsonNode = parseJson(str)

let rpcAddr = jsonNode["RpcAddress"].getStr()
let myConfig = VerifiedProxyConf(
rpcAddress: ValidIpAddress.init(rpcAddr),
listenAddress: defaultListenAddress,
eth2Network: some(jsonNode["Eth2Network"].getStr()),
trustedBlockRoot: Eth2Digest.fromHex(jsonNode["TrustedBlockRoot"].getStr()),
web3Url: parseCmdArg(Web3Url, jsonNode["Web3Url"].getStr()),
rpcPort: Port(jsonNode["RpcPort"].getInt()),
logLevel: jsonNode["LogLevel"].getStr(),
maxPeers: 160,
nat: NatConfig(hasExtIp: false, nat: NatAny),
logStdout: StdoutLogKind.Auto,
dataDir: OutDir(defaultVerifiedProxyDataDir()),
tcpPort: Port(defaultEth2TcpPort),
udpPort: Port(defaultEth2TcpPort),
agentString: "nimbus",
discv5Enabled: true,
)

run(myConfig, ctx)
except Exception as err:
echo "Exception when running ", getCurrentExceptionMsg(), err.getStackTrace()
ctx.onHeader(getCurrentExceptionMsg(), 3)
ctx.cleanup()


#[let node = parseConfigAndRun(ctx.configJson)

while not ctx[].stop: # and node.running:
let timeout = sleepAsync(100.millis)
waitFor timeout

# do cleanup
node.stop()]#

proc startVerifProxy*(configJson: cstring, onHeader: OnHeaderCallback): ptr Context {.exportc, dynlib.} =
initLib()

let ctx = createShared(Context, 1)
ctx.configJson = cast[cstring](allocShared0(len(configJson) + 1))
ctx.onHeader = onHeader
copyMem(ctx.configJson, configJson, len(configJson))

try:
createThread(ctx.thread, runContext, ctx)
except Exception as err:
echo "Exception when attempting to invoke createThread ", getCurrentExceptionMsg(), err.getStackTrace()
ctx.onHeader(getCurrentExceptionMsg(), 3)
ctx.cleanup()
return ctx

proc stopVerifProxy*(ctx: ptr Context) {.exportc, dynlib.} =
ctx.stop = true


53 changes: 46 additions & 7 deletions nimbus_verified_proxy/nimbus_verified_proxy.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{.push raises: [].}

import
std/[os, strutils],
std/[atomics, json, os, strutils],
chronicles, chronicles/chronos_tools, chronos, confutils,
eth/keys,
json_rpc/rpcproxy,
Expand All @@ -26,6 +26,18 @@ import
from beacon_chain/gossip_processing/block_processor import newExecutionPayload
from beacon_chain/gossip_processing/eth2_processor import toValidationResult

type OnHeaderCallback* = proc (s: cstring, t: int) {.cdecl, raises: [], gcsafe.}
type Context* = object
thread*: Thread[ptr Context]
configJson*: cstring
stop*: bool
onHeader*: OnHeaderCallback

proc cleanup*(ctx: ptr Context) =
dealloc(ctx.configJson)
freeShared(ctx)


func getConfiguredChainId(networkMetadata: Eth2NetworkMetadata): Quantity =
if networkMetadata.eth1Network.isSome():
let
Expand All @@ -40,14 +52,22 @@ func getConfiguredChainId(networkMetadata: Eth2NetworkMetadata): Quantity =
else:
return networkMetadata.cfg.DEPOSIT_CHAIN_ID.Quantity

proc run(config: VerifiedProxyConf) {.raises: [CatchableError].} =
proc run*(config: VerifiedProxyConf, ctx: ptr Context) {.raises: [CatchableError], gcsafe.} =
var headerCallback: OnHeaderCallback
if ctx != nil:
headerCallback = ctx.onHeader

# Required as both Eth2Node and LightClient requires correct config type
var lcConfig = config.asLightClientConf()

setupLogging(config.logLevel, config.logStdout, none(OutFile))
{.gcsafe.}:
setupLogging(config.logLevel, config.logStdout, none(OutFile))

notice "Launching Nimbus verified proxy",
version = fullVersionStr, cmdParams = commandLineParams(), config
try:
notice "Launching Nimbus verified proxy",
version = fullVersionStr, cmdParams = getCLIParams(), config
except Exception:
notice "getCLIParams() exception"

let
metadata = loadEth2Network(config.eth2Network)
Expand Down Expand Up @@ -163,6 +183,12 @@ proc run(config: VerifiedProxyConf) {.raises: [CatchableError].} =
when lcDataFork > LightClientDataFork.None:
info "New LC finalized header",
finalized_header = shortLog(forkyHeader)
if headerCallback != nil:
try:
headerCallback(Json.encode(forkyHeader), 0)
except SerializationError as e:
notice "finalizedHeaderCallback exception"


proc onOptimisticHeader(
lightClient: LightClient, optimisticHeader: ForkedLightClientHeader) =
Expand All @@ -171,6 +197,12 @@ proc run(config: VerifiedProxyConf) {.raises: [CatchableError].} =
info "New LC optimistic header",
optimistic_header = shortLog(forkyHeader)
optimisticProcessor.setOptimisticHeader(forkyHeader.beacon)
if headerCallback != nil:
try:
headerCallback(Json.encode(forkyHeader), 1)
except SerializationError as e:
notice "optimisticHeaderCallback exception"


lightClient.onFinalizedHeader = onFinalizedHeader
lightClient.onOptimisticHeader = onOptimisticHeader
Expand Down Expand Up @@ -248,11 +280,18 @@ proc run(config: VerifiedProxyConf) {.raises: [CatchableError].} =
asyncSpawn runOnSecondLoop()
while true:
poll()
if ctx != nil and ctx.stop:
# Cleanup
waitFor network.stop()
waitFor rpcProxy.stop()
ctx.cleanup()
# Notify client that cleanup is finished
headerCallback(nil, 2)
break

when isMainModule:
{.pop.}
var config = makeBannerAndConfig(
"Nimbus verified proxy " & fullVersionStr, VerifiedProxyConf)
{.push raises: [].}

run(config)
run(config, nil)
2 changes: 1 addition & 1 deletion nimbus_verified_proxy/nimbus_verified_proxy_conf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func asLightClientConf*(pc: VerifiedProxyConf): LightClientConf =
discv5Enabled: pc.discv5Enabled,
directPeers: pc.directPeers,
trustedBlockRoot: pc.trustedBlockRoot,
web3Urls: @[],
web3Urls: @[EngineApiUrlConfigValue(url: pc.web3url.web3Url)],
jwtSecret: none(InputFile),
stopAtEpoch: 0
)
Expand Down
2 changes: 1 addition & 1 deletion vendor/nim-confutils
Loading