Add cleanup code, address review comments
This commit is contained in:
parent
89bab200b6
commit
b5d137e92e
|
@ -21,5 +21,6 @@
|
|||
typedef struct VerifProxyContext VerifProxyContext;
|
||||
typedef void (*onHeaderCallback)(NCSTRING s, int t);
|
||||
void quit(void);
|
||||
VerifProxyContext* startLightClientProxy(NCSTRING configJson, onHeaderCallback onHeader);
|
||||
VerifProxyContext* startVerifProxy(NCSTRING configJson, onHeaderCallback onHeader);
|
||||
void stopVerifProxy(VerifProxyContext*);
|
||||
#endif /* __verifproxy__ */
|
||||
|
|
|
@ -10,12 +10,6 @@ proc NimMain() {.importc.}
|
|||
|
||||
var initialized: Atomic[bool]
|
||||
|
||||
type Context* = object
|
||||
thread*: Thread[ptr Context]
|
||||
configJson*: cstring
|
||||
stop*: bool
|
||||
onHeader*: OnHeaderCallback
|
||||
|
||||
proc initLib() =
|
||||
if not initialized.exchange(true):
|
||||
NimMain() # Every Nim library needs to call `NimMain` once exactly
|
||||
|
@ -50,9 +44,11 @@ proc runContext(ctx: ptr Context) {.thread.} =
|
|||
discv5Enabled: true,
|
||||
)
|
||||
|
||||
run(myConfig, ctx.onHeader)
|
||||
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)
|
||||
|
@ -64,7 +60,7 @@ proc runContext(ctx: ptr Context) {.thread.} =
|
|||
# do cleanup
|
||||
node.stop()]#
|
||||
|
||||
proc startLightClientProxy*(configJson: cstring, onHeader: OnHeaderCallback): ptr Context {.exportc, dynlib.} =
|
||||
proc startVerifProxy*(configJson: cstring, onHeader: OnHeaderCallback): ptr Context {.exportc, dynlib.} =
|
||||
initLib()
|
||||
|
||||
let ctx = createShared(Context, 1)
|
||||
|
@ -76,6 +72,11 @@ proc startLightClientProxy*(configJson: cstring, onHeader: OnHeaderCallback): pt
|
|||
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
|
||||
|
||||
|
||||
|
|
|
@ -27,6 +27,16 @@ 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():
|
||||
|
@ -42,10 +52,10 @@ func getConfiguredChainId(networkMetadata: Eth2NetworkMetadata): Quantity =
|
|||
else:
|
||||
return networkMetadata.cfg.DEPOSIT_CHAIN_ID.Quantity
|
||||
|
||||
proc run*(config: VerifiedProxyConf, callbacks: varargs[OnHeaderCallback]) {.raises: [CatchableError], gcsafe.} =
|
||||
proc run*(config: VerifiedProxyConf, ctx: ptr Context) {.raises: [CatchableError], gcsafe.} =
|
||||
var headerCallback: OnHeaderCallback
|
||||
if len(callbacks) == 1:
|
||||
headerCallback = callbacks[0]
|
||||
if ctx != nil:
|
||||
headerCallback = ctx.onHeader
|
||||
|
||||
# Required as both Eth2Node and LightClient requires correct config type
|
||||
var lcConfig = config.asLightClientConf()
|
||||
|
@ -174,7 +184,6 @@ proc run*(config: VerifiedProxyConf, callbacks: varargs[OnHeaderCallback]) {.rai
|
|||
info "New LC finalized header",
|
||||
finalized_header = shortLog(forkyHeader)
|
||||
if headerCallback != nil:
|
||||
notice "### Invoking finalizedHeaderCallback"
|
||||
try:
|
||||
headerCallback(Json.encode(forkyHeader), 0)
|
||||
except SerializationError as e:
|
||||
|
@ -190,7 +199,6 @@ proc run*(config: VerifiedProxyConf, callbacks: varargs[OnHeaderCallback]) {.rai
|
|||
optimisticProcessor.setOptimisticHeader(forkyHeader.beacon)
|
||||
if headerCallback != nil:
|
||||
try:
|
||||
notice "### Invoking optimisticHeaderCallback"
|
||||
headerCallback(Json.encode(forkyHeader), 1)
|
||||
except SerializationError as e:
|
||||
notice "optimisticHeaderCallback exception"
|
||||
|
@ -272,10 +280,18 @@ proc run*(config: VerifiedProxyConf, callbacks: varargs[OnHeaderCallback]) {.rai
|
|||
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 and appType != "lib":
|
||||
when isMainModule:
|
||||
{.pop.}
|
||||
var config = makeBannerAndConfig(
|
||||
"Nimbus verified proxy " & fullVersionStr, VerifiedProxyConf)
|
||||
{.push raises: [].}
|
||||
run(config)
|
||||
run(config, nil)
|
||||
|
|
Loading…
Reference in New Issue