Skip to content
Open
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
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ EXCLUDED_NIM_PACKAGES := \
vendor/nimbus-eth2/vendor/nim-eth \
vendor/nimbus-eth2/vendor/nim-faststreams \
vendor/nimbus-eth2/vendor/nim-http-utils \
vendor/nimbus-eth2/vendor/nim-ngtcp2 \
vendor/nimbus-eth2/vendor/nim-quic \
vendor/nimbus-eth2/vendor/nim-json-rpc \
vendor/nimbus-eth2/vendor/nim-json-serialization \
vendor/nimbus-eth2/vendor/nim-libbacktrace \
Expand Down Expand Up @@ -89,11 +91,14 @@ PORTAL_TOOLS_CSV := $(subst $(SPACE),$(COMMA),$(FLUFFY_TOOLS))
OS_PLATFORM = $(shell $(CC) -dumpmachine)
ifneq (, $(findstring darwin, $(OS_PLATFORM)))
SHAREDLIBEXT = dylib
STATICLIBEXT = a
else
ifneq (, $(findstring mingw, $(OS_PLATFORM))$(findstring cygwin, $(OS_PLATFORM))$(findstring msys, $(OS_PLATFORM)))
SHAREDLIBEXT = dll
STATICLIBEXT = lib
else
SHAREDLIBEXT = so
STATICLIBEXT = a
endif
endif

Expand Down Expand Up @@ -350,7 +355,8 @@ nimbus-verified-proxy-test: | build deps
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
echo $(NIM_PARAMS) && \
$(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
cp nimbus_verified_proxy/libverifproxy/verifproxy.h $(VERIF_PROXY_OUT_PATH)/
echo -e $(BUILD_END_MSG) "build/$@"

Expand Down
62 changes: 53 additions & 9 deletions nimbus_verified_proxy/lc/lc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
import
chronicles,
chronos,
eth/common/keys, # used for keys.rng
beacon_chain/gossip_processing/light_client_processor,
beacon_chain/beacon_clock,
beacon_chain/[beacon_clock, conf],
./lc_manager # use the modified light client manager

type
Expand All @@ -20,8 +21,8 @@ type
) {.gcsafe, raises: [].}

LightClient* = ref object
cfg: RuntimeConfig
forkDigests: ref ForkDigests
cfg*: RuntimeConfig
forkDigests*: ref ForkDigests
getBeaconTime*: GetBeaconTimeFn
store*: ref ForkedLightClientStore
processor*: ref LightClientProcessor
Expand Down Expand Up @@ -144,16 +145,59 @@ proc new*(

lightClient

proc new*(
T: type LightClient, chain: Option[string], trustedBlockRoot: Option[Eth2Digest]
): T =
let metadata = loadEth2Network(chain)

# just for short hand convenience
template cfg(): auto =
metadata.cfg

# initialize beacon node genesis data, beacon clock and forkDigests
let
genesisState =
try:
template genesisData(): auto =
metadata.genesis.bakedBytes

newClone(
readSszForkedHashedBeaconState(
cfg, genesisData.toOpenArray(genesisData.low, genesisData.high)
)
)
except CatchableError as err:
raiseAssert "Invalid baked-in state: " & err.msg

# getStateField reads seeks info directly from a byte array
# get genesis time and instantiate the beacon clock
genesisTime = getStateField(genesisState[], genesis_time)
beaconClock = BeaconClock.init(cfg.timeParams, genesisTime).valueOr:
error "Invalid genesis time in state", genesisTime
quit QuitFailure

# get the function that itself get the current beacon time
getBeaconTime = beaconClock.getBeaconTimeFn()
genesis_validators_root = getStateField(genesisState[], genesis_validators_root)
forkDigests = newClone ForkDigests.init(cfg, genesis_validators_root)

rng = keys.newRng()

# light client is set to optimistic finalization mode
lightClient = LightClient.new(
rng, cfg, forkDigests, getBeaconTime, genesis_validators_root,
LightClientFinalizationMode.Optimistic,
)

lightClient.trustedBlockRoot = trustedBlockRoot
lightClient

proc setBackend*(lightClient: LightClient, backend: EthLCBackend) =
lightClient.manager.backend = backend

proc start*(lightClient: LightClient) =
proc start*(lightClient: LightClient) {.async: (raises: [CancelledError]).} =
info "Starting beacon light client", trusted_block_root = lightClient.trustedBlockRoot
lightClient.manager.start()

proc stop*(lightClient: LightClient) {.async: (raises: []).} =
info "Stopping beacon light client"
await lightClient.manager.stop()
await lightClient.manager.start()

proc resetToFinalizedHeader*(
lightClient: LightClient,
Expand Down
13 changes: 3 additions & 10 deletions nimbus_verified_proxy/lc/lc_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ type
UpdateVerifier* = ValueVerifier[ForkedLightClientUpdate]
FinalityUpdateVerifier* = ValueVerifier[ForkedLightClientFinalityUpdate]
OptimisticUpdateVerifier* = ValueVerifier[ForkedLightClientOptimisticUpdate]

GetTrustedBlockRootCallback* = proc(): Option[Eth2Digest] {.gcsafe, raises: [].}
GetBoolCallback* = proc(): bool {.gcsafe, raises: [].}
GetSlotCallback* = proc(): Slot {.gcsafe, raises: [].}
Expand Down Expand Up @@ -293,6 +292,7 @@ proc query[E](

# cancel all workers
for i in 0 ..< NUM_WORKERS:
assert workers[i] != nil
workers[i].cancelSoon()

return success
Expand Down Expand Up @@ -385,13 +385,6 @@ proc loop(self: LightClientManager) {.async: (raises: [CancelledError]).} =
# check for updates every slot
await sleepAsync(self.timeParams.SLOT_DURATION)

proc start*(self: var LightClientManager) =
proc start*(self: LightClientManager) {.async: (raises: [CancelledError]).} =
## Start light client manager's loop.
doAssert self.loopFuture == nil
self.loopFuture = self.loop()

proc stop*(self: var LightClientManager) {.async: (raises: []).} =
## Stop light client manager's loop.
if self.loopFuture != nil:
await noCancel self.loopFuture.cancelAndWait()
self.loopFuture = nil
await self.loop()
13 changes: 13 additions & 0 deletions nimbus_verified_proxy/libverifproxy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### Quick Instructions

*tested on MacOS (ARM64)*

> NOTE: change the hashes under `makeCalls` functions in `example.c` to any recent blockhash and transaction hash because verify proxy cannot query more than 1000 blocks in the history (`maxBlockWalk`) under default configuration

> NOTE: update the `trustedBlockRoot` before compiling the example

```bash
./env.sh make -j12 nimbus_verified_proxy
gcc -I./build/libverifproxy/ -L./build/libverifproxy/ -lverifproxy -lstdc++ -o proxy_from_c ./nimbus_verified_proxy/libverifproxy/example.c
./proxy_from_c
```
164 changes: 164 additions & 0 deletions nimbus_verified_proxy/libverifproxy/example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/**
* nimbus_verified_proxy
* Copyright (c) 2025 Status Research & Development GmbH
* Licensed and distributed under either of
* * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
* * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
* at your option. This file may not be copied, modified, or distributed except according to those terms.
*/

#include "./verifproxy.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <stdbool.h>

char filterId[67];
bool filterCreated = false;

void onBlockNumber(Context *ctx, int status, char *res) {
printf("Blocknumber: %s\n", res);
freeResponse(res);
}

void onStart(Context *ctx, int status, char *res) {
if (status < 0){ // callback onStart is called only for errors
printf("Problem while starting verified proxy\n");
stopVerifProxy(ctx);
freeContext(ctx);
exit(EXIT_FAILURE);
}
}

void onStorage(Context *ctx, int status, char *res) {
printf("Storage: %s\n", res);
freeResponse(res);
}

void onBalance(Context *ctx, int status, char *res) {
printf("Balance: %s\n", res);
freeResponse(res);
}

void onNonce(Context *ctx, int status, char *res) {
printf("Nonce: %s\n", res);
freeResponse(res);
}

void onCode(Context *ctx, int status, char *res) {
printf("Code: %s\n", res);
freeResponse(res);
}

void genericCallback(Context *ctx, int status, char *res) {
printf("Status: %d\n", status);
if (status < 0) printf("Error: %s\n", res);
freeResponse(res);
}

void onFilterCreate(Context *ctx, int status, char *res) {
if (status == RET_SUCCESS) {
strncpy(filterId, &res[1], strlen(res) - 2); // remove quotes
filterId[strlen(res) - 2] = '\0';
filterCreated = true;
}
freeResponse(res);
}

void onCallComplete(Context *ctx, int status, char *res) {
if (status == RET_SUCCESS) {
printf("Call Complete: %s\n", res);
} else {
printf("Call Error: %s\n", res);
}
freeResponse(res);
}

void onLogs(Context *ctx, int status, char *res) {
if (status == RET_SUCCESS) {
printf("Logs fetch successful\n");
} else {
printf("Logs Fetch Error: %s\n", res);
}
freeResponse(res);
}

void makeCalls(Context *ctx) {
char *BLOCK_HASH = "0xc62fa4cbdd48175b1171d8b7cede250ac1bea47ace4d19db344b922cd1e63111";
char *TX_HASH = "0xbbcd3d9bc70874c03453caa19fd91239abb0eef84dc61ca33e2110df81df330c";
char *CALL_ARGS = "{\"to\": \"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2\",\"data\": \"0x70a08231000000000000000000000000De5ae63A348C4d63343C8E20Fb6286909418c8A4\"}";
char *FILTER_OPTIONS = "{\"fromBlock\": \"latest\", \"toBlock\": \"latest\", \"topics\":[\"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef\"]}";

eth_blockNumber(ctx, onBlockNumber);
eth_getBalance(ctx, "0x954a86C613fd1fBaC9C7A43a071A68254C75E4AC", "latest", onBalance);
eth_getStorageAt(ctx, "0x954a86C613fd1fBaC9C7A43a071A68254C75E4AC", "0x0", "latest", onStorage);
eth_getTransactionCount(ctx, "0x954a86C613fd1fBaC9C7A43a071A68254C75E4AC", "latest", onNonce);
eth_getCode(ctx, "0x954a86C613fd1fBaC9C7A43a071A68254C75E4AC", "latest", onCode);

/* -------- Blocks & Uncles -------- */

eth_getBlockByHash(ctx, BLOCK_HASH, false, genericCallback);
eth_getBlockByNumber(ctx, "latest", false, genericCallback);
eth_getUncleCountByBlockNumber(ctx, "latest", genericCallback);
eth_getUncleCountByBlockHash(ctx, BLOCK_HASH, genericCallback);

eth_getBlockTransactionCountByNumber(ctx, "latest", genericCallback);
eth_getBlockTransactionCountByHash(ctx, BLOCK_HASH, genericCallback);

/* -------- Transactions -------- */
eth_getTransactionByBlockNumberAndIndex(ctx, "latest", 0ULL, genericCallback);
eth_getTransactionByBlockHashAndIndex(ctx, BLOCK_HASH, 0ULL, genericCallback);

eth_getTransactionByHash(ctx, TX_HASH, genericCallback);
eth_getTransactionReceipt(ctx, TX_HASH, genericCallback);

eth_getBlockReceipts(ctx, "latest", genericCallback);

/* -------- Calls, Access Lists, Gas Estimation -------- */
eth_call(ctx, CALL_ARGS, "latest", true, onCallComplete);
eth_createAccessList(ctx, CALL_ARGS, "latest", false, onCallComplete);
eth_estimateGas(ctx, CALL_ARGS, "latest", false, onCallComplete);

/* -------- Logs & Filters -------- */
eth_getLogs(ctx, FILTER_OPTIONS, onLogs);
if (filterCreated) {
eth_getFilterLogs(ctx, filterId, onLogs);
eth_getFilterChanges(ctx, filterId, onLogs);
} else {
eth_newFilter(ctx, FILTER_OPTIONS, onFilterCreate);
}
}

int main() {
NimMain();

char* jsonConfig =
"{"
"\"eth2Network\": \"mainnet\","
"\"trustedBlockRoot\": \"0x2558d82e8b29c4151a0683e4f9d480d229d84b27b51a976f56722e014227e723\","
"\"backendUrl\": \"https://eth.blockrazor.xyz\","
"\"beaconApiUrls\": \"http://testing.mainnet.beacon-api.nimbus.team,http://www.lightclientdata.org\","
"\"logLevel\": \"FATAL\","
"\"logStdout\": \"None\""
"}";

Context *ctx = startVerifProxy(jsonConfig, onStart);

time_t start = time(NULL);

makeCalls(ctx);

while(true) {
if ((time(NULL) - start) > 12) { //all 24 methods should return
printf("\n\n Executing all eth api methods\n\n");
makeCalls(ctx);
start = time(NULL);
}
processVerifProxyTasks(ctx);
}
printf("it is here and this is the problem");
stopVerifProxy(ctx);
freeContext(ctx);
}
Loading