Skip to content

Commit 0937573

Browse files
committed
init
1 parent 39a0667 commit 0937573

File tree

8 files changed

+484
-218
lines changed

8 files changed

+484
-218
lines changed

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,14 @@ PORTAL_TOOLS_CSV := $(subst $(SPACE),$(COMMA),$(FLUFFY_TOOLS))
8787
OS_PLATFORM = $(shell $(CC) -dumpmachine)
8888
ifneq (, $(findstring darwin, $(OS_PLATFORM)))
8989
SHAREDLIBEXT = dylib
90+
STATICLIBEXT = a
9091
else
9192
ifneq (, $(findstring mingw, $(OS_PLATFORM))$(findstring cygwin, $(OS_PLATFORM))$(findstring msys, $(OS_PLATFORM)))
9293
SHAREDLIBEXT = dll
94+
STATICLIBEXT = lib
9395
else
9496
SHAREDLIBEXT = so
97+
STATICLIBEXT = a
9598
endif
9699
endif
97100

@@ -165,7 +168,7 @@ all: | $(TOOLS) nimbus nimbus_execution_client
165168

166169
# "-d:release" cannot be added to config.nims
167170

168-
NIM_PARAMS += -d:release
171+
NIM_PARAMS += -d:debug
169172
ifneq ($(if $(ENABLE_LINE_NUMBERS),$(ENABLE_LINE_NUMBERS),0),0)
170173
NIM_PARAMS += -d:chronicles_line_numbers:1
171174
endif
@@ -348,7 +351,8 @@ nimbus-verified-proxy-test: | build deps
348351
libverifproxy: | build deps
349352
+ echo -e $(BUILD_MSG) "build/$@" && \
350353
$(ENV_SCRIPT) nim --version && \
351-
$(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
354+
echo $(NIM_PARAMS) && \
355+
$(ENV_SCRIPT) nim c --app:staticlib -d:"libp2p_pki_schemes=secp256k1" --noMain:on --out:$(VERIF_PROXY_OUT_PATH)/$@.$(STATICLIBEXT) $(NIM_PARAMS) nimbus_verified_proxy/libverifproxy/verifproxy.nim
352356
cp nimbus_verified_proxy/libverifproxy/verifproxy.h $(VERIF_PROXY_OUT_PATH)/
353357
echo -e $(BUILD_END_MSG) "build/$@"
354358

nimbus_verified_proxy/lc/lc.nim

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
import
1111
chronicles,
1212
chronos,
13+
eth/common/keys, # used for keys.rng
1314
beacon_chain/gossip_processing/light_client_processor,
14-
beacon_chain/beacon_clock,
15+
beacon_chain/[beacon_clock, conf],
1516
./lc_manager # use the modified light client manager
1617

1718
type
@@ -20,8 +21,8 @@ type
2021
) {.gcsafe, raises: [].}
2122

2223
LightClient* = ref object
23-
cfg: RuntimeConfig
24-
forkDigests: ref ForkDigests
24+
cfg*: RuntimeConfig
25+
forkDigests*: ref ForkDigests
2526
getBeaconTime*: GetBeaconTimeFn
2627
store*: ref ForkedLightClientStore
2728
processor*: ref LightClientProcessor
@@ -144,16 +145,59 @@ proc new*(
144145

145146
lightClient
146147

148+
proc new*(
149+
T: type LightClient, chain: Option[string], trustedBlockRoot: Option[Eth2Digest]
150+
): T =
151+
let metadata = loadEth2Network(chain)
152+
153+
# just for short hand convenience
154+
template cfg(): auto =
155+
metadata.cfg
156+
157+
# initialize beacon node genesis data, beacon clock and forkDigests
158+
let
159+
genesisState =
160+
try:
161+
template genesisData(): auto =
162+
metadata.genesis.bakedBytes
163+
164+
newClone(
165+
readSszForkedHashedBeaconState(
166+
cfg, genesisData.toOpenArray(genesisData.low, genesisData.high)
167+
)
168+
)
169+
except CatchableError as err:
170+
raiseAssert "Invalid baked-in state: " & err.msg
171+
172+
# getStateField reads seeks info directly from a byte array
173+
# get genesis time and instantiate the beacon clock
174+
genesisTime = getStateField(genesisState[], genesis_time)
175+
beaconClock = BeaconClock.init(cfg.timeParams, genesisTime).valueOr:
176+
error "Invalid genesis time in state", genesisTime
177+
quit QuitFailure
178+
179+
# get the function that itself get the current beacon time
180+
getBeaconTime = beaconClock.getBeaconTimeFn()
181+
genesis_validators_root = getStateField(genesisState[], genesis_validators_root)
182+
forkDigests = newClone ForkDigests.init(cfg, genesis_validators_root)
183+
184+
rng = keys.newRng()
185+
186+
# light client is set to optimistic finalization mode
187+
lightClient = LightClient.new(
188+
rng, cfg, forkDigests, getBeaconTime, genesis_validators_root,
189+
LightClientFinalizationMode.Optimistic,
190+
)
191+
192+
lightClient.trustedBlockRoot = trustedBlockRoot
193+
lightClient
194+
147195
proc setBackend*(lightClient: LightClient, backend: EthLCBackend) =
148196
lightClient.manager.backend = backend
149197

150-
proc start*(lightClient: LightClient) =
198+
proc start*(lightClient: LightClient) {.async: (raises: [CancelledError]).} =
151199
info "Starting beacon light client", trusted_block_root = lightClient.trustedBlockRoot
152-
lightClient.manager.start()
153-
154-
proc stop*(lightClient: LightClient) {.async: (raises: []).} =
155-
info "Stopping beacon light client"
156-
await lightClient.manager.stop()
200+
await lightClient.manager.start()
157201

158202
proc resetToFinalizedHeader*(
159203
lightClient: LightClient,

nimbus_verified_proxy/lc/lc_manager.nim

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,6 @@ proc loop(self: LightClientManager) {.async: (raises: [CancelledError]).} =
386386
# check for updates every slot
387387
await sleepAsync(chronos.seconds(int64(self.timeParams.SECONDS_PER_SLOT)))
388388

389-
proc start*(self: var LightClientManager) =
389+
proc start*(self: LightClientManager) {.async: (raises: [CancelledError]).} =
390390
## Start light client manager's loop.
391-
doAssert self.loopFuture == nil
392-
self.loopFuture = self.loop()
393-
394-
proc stop*(self: var LightClientManager) {.async: (raises: []).} =
395-
## Stop light client manager's loop.
396-
if self.loopFuture != nil:
397-
await noCancel self.loopFuture.cancelAndWait()
398-
self.loopFuture = nil
391+
await self.loop()
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* nimbus_verified_proxy
3+
* Copyright (c) 2025 Status Research & Development GmbH
4+
* Licensed and distributed under either of
5+
* * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
6+
* * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
7+
* at your option. This file may not be copied, modified, or distributed except according to those terms.
8+
*/
9+
10+
#include "./verifproxy.h"
11+
#include <stdio.h>
12+
#include <unistd.h>
13+
#include <stdbool.h>
14+
15+
static bool waitOver = true;
16+
17+
void onBlockNumber(Context *ctx, int status, char *res) {
18+
printf("response: %s\n", res);
19+
freeResponse(res);
20+
}
21+
22+
void onStart(Context *ctx, int status, char *res) {
23+
printf("Verified Proxy started successfully\n");
24+
printf("status: %d\n", status);
25+
printf("response: %s\n", res);
26+
if (status < 0) stopVerifProxy(ctx);
27+
freeResponse(res);
28+
}
29+
30+
void waitIsOver(Context *ctx, int status, char *res) {
31+
printf("waiting finished successfully\n");
32+
printf("status: %d\n", status);
33+
34+
eth_blockNumber(ctx, onBlockNumber);
35+
waitOver = true;
36+
37+
freeResponse(res);
38+
}
39+
40+
int main() {
41+
NimMain();
42+
Context *ctx = createAsyncTaskContext();
43+
44+
char* jsonConfig =
45+
"{"
46+
"\"eth2Network\": \"mainnet\","
47+
"\"trustedBlockRoot\": \"0xdd8db7bfd8c96c993a4cb78e0e6607cf1dcca3f379764388248c63d2bc40443b\","
48+
"\"backendUrl\": \"https://eth.llamarpc.com\","
49+
"\"beaconApiUrls\": \"http://testing.mainnet.beacon-api.nimbus.team,http://lightclientdata.org\","
50+
"\"logLevel\": \"INFO\","
51+
"\"logStdout\": \"Auto\""
52+
"}";
53+
54+
startVerifProxy(ctx, jsonConfig, onStart);
55+
56+
while(true) {
57+
if (waitOver) {
58+
waitOver = false;
59+
nonBusySleep(ctx, 10, waitIsOver);
60+
}
61+
pollAsyncTaskEngine(ctx);
62+
}
63+
freeContext(ctx);
64+
}
Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* nimbus_verified_proxy
3-
* Copyright (c) 2024 Status Research & Development GmbH
3+
* Copyright (c) 2025 Status Research & Development GmbH
44
* Licensed and distributed under either of
55
* * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
66
* * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
@@ -10,12 +10,42 @@
1010
#ifndef __verifproxy__
1111
#define __verifproxy__
1212

13-
typedef struct VerifProxyContext VerifProxyContext;
14-
typedef void (*onHeaderCallback)(const char* s, int t);
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
1516

16-
void quit(void);
17+
#ifndef __has_attribute
18+
#define __has_attribute(x) 0
19+
#endif
1720

18-
VerifProxyContext* startVerifProxy(const char* configJson, onHeaderCallback onHeader);
19-
void stopVerifProxy(VerifProxyContext*);
21+
#ifndef __has_feature
22+
#define __has_feature(x) 0
23+
#endif
24+
25+
#if __has_attribute(warn_unused_result)
26+
#define ETH_RESULT_USE_CHECK __attribute__((warn_unused_result))
27+
#else
28+
#define ETH_RESULT_USE_CHECK
29+
#endif
30+
31+
void NimMain(void);
32+
33+
typedef struct Context Context;
34+
35+
ETH_RESULT_USE_CHECK Context *createAsyncTaskContext();
36+
37+
typedef void (*CallBackProc) (Context *ctx, int status, char *res);
38+
39+
void eth_blockNumber(Context *ctx, CallBackProc cb);
40+
void freeResponse(char *res);
41+
void freeContext(Context *ctx);
42+
void nonBusySleep(Context *ctx, int secs, CallBackProc cb);
43+
void startVerifProxy(Context *ctx, char* configJson, CallBackProc onstart);
44+
void stopVerifProxy(Context *ctx);
45+
void pollAsyncTaskEngine(Context *ctx);
46+
47+
#ifdef __cplusplus
48+
}
49+
#endif
2050

2151
#endif /* __verifproxy__ */

0 commit comments

Comments
 (0)