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
197 changes: 0 additions & 197 deletions nimbus_verified_proxy/c_frontend.nim

This file was deleted.

2 changes: 1 addition & 1 deletion nimbus_verified_proxy/engine/transactions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ proc toTransactions*(txs: openArray[TxOrHash]): Result[seq[Transaction], string]
return ok(convertedTxs)

proc checkTxHash*(txObj: TransactionObject, txHash: Hash32): bool =
toTransaction(txObj).rlpHash == txHash
toTransaction(txObj).computeRlpHash == txHash

proc verifyTransactions*(
txRoot: Hash32, transactions: seq[TxOrHash]
Expand Down
138 changes: 138 additions & 0 deletions nimbus_verified_proxy/json_lc_backend.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# 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.

{.push raises: [], gcsafe.}

import
stint,
chronos,
chronicles,
presto/client,
beacon_chain/spec/eth2_apis/rest_light_client_calls,
beacon_chain/spec/presets,
beacon_chain/spec/forks,
./lc/lc_manager,
./nimbus_verified_proxy_conf

logScope:
topics = "SSZLCRestClient"

const
MaxMessageBodyBytes* = 128 * 1024 * 1024 # 128 MB (JSON encoded)
BASE_URL = "/eth/v1/beacon/light_client"

type
LCRestPeer = ref object
score: int
restClient: RestClientRef

LCRestClient* = ref object
cfg: RuntimeConfig
forkDigests: ref ForkDigests
peers: seq[LCRestPeer]
urls: seq[string]

func new*(
T: type LCRestClient, cfg: RuntimeConfig, forkDigests: ref ForkDigests
): LCRestClient =
LCRestClient(cfg: cfg, forkDigests: forkDigests, peers: @[])

proc addEndpoints*(client: LCRestClient, urlList: UrlList) {.raises: [ValueError].} =
for endpoint in urlList.urls:
if endpoint in client.urls:
continue

let restClient = RestClientRef.new(endpoint).valueOr:
raise newException(ValueError, $error)

client.peers.add(LCRestPeer(score: 0, restClient: restClient))
client.urls.add(endpoint)

proc closeAll*(client: LCRestClient) {.async: (raises: []).} =
for peer in client.peers:
await peer.restClient.closeWait()

client.peers.setLen(0)
client.urls.setLen(0)

proc getEthLCBackend*(client: LCRestClient): EthLCBackend =
let
getLCBootstrapProc = proc(
reqId: uint64, blockRoot: Eth2Digest
): Future[NetRes[ForkedLightClientBootstrap]] {.async: (raises: [CancelledError]).} =
let
peer = client.peers[reqId mod uint64(client.peers.len)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CL has a helper, lenu64, maybe available here

res =
try:
await peer.restClient.getLightClientBootstrap(
blockRoot, client.cfg, client.forkDigests
)
except CatchableError as e:
raise newException(CancelledError, e.msg)

ok(res)

getLCUpdatesProc = proc(
reqId: uint64, startPeriod: SyncCommitteePeriod, count: uint64
): Future[LightClientUpdatesByRangeResponse] {.async: (raises: [CancelledError]).} =
let
peer = client.peers[reqId mod uint64(client.peers.len)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if lenu64 is available, this is exactly what it's for

res =
try:
await peer.restClient.getLightClientUpdatesByRange(
startPeriod, count, client.cfg, client.forkDigests
)
except CatchableError as e:
raise newException(CancelledError, e.msg)

ok(res)

getLCFinalityProc = proc(
reqId: uint64
): Future[NetRes[ForkedLightClientFinalityUpdate]] {.
async: (raises: [CancelledError])
.} =
let
peer = client.peers[reqId mod uint64(client.peers.len)]
res =
try:
await peer.restClient.getLightClientFinalityUpdate(
client.cfg, client.forkDigests
)
except CatchableError as e:
raise newException(CancelledError, e.msg)

ok(res)

getLCOptimisticProc = proc(
reqId: uint64
): Future[NetRes[ForkedLightClientOptimisticUpdate]] {.
async: (raises: [CancelledError])
.} =
let
peer = client.peers[reqId mod uint64(client.peers.len)]
res =
try:
await peer.restClient.getLightClientOptimisticUpdate(
client.cfg, client.forkDigests
)
except CatchableError as e:
raise newException(CancelledError, e.msg)

ok(res)

updateScoreProc = proc(reqId: uint64, value: int) =
let peer = client.peers[reqId mod uint64(client.peers.len)]
peer.score += value

EthLCBackend(
getLightClientBootstrap: getLCBootstrapProc,
getLightClientUpdatesByRange: getLCUpdatesProc,
getLightClientFinalityUpdate: getLCFinalityProc,
getLightClientOptimisticUpdate: getLCOptimisticProc,
updateScore: updateScoreProc,
)
Loading