nimbus-eth1/fluffy/tools/portal_bridge/portal_bridge_common.nim
web3-developer f9956eba59
Fluffy State Bridge - Building state using state diffs. (#2486)
* Started state bridge.

* Implement call to fetch stateDiffs using trace_replayBlockTransactions.

* Convert JSON responses to stateDiff types.

* State updates working for first few blocks.

* Correctly building state for first 200K blocks.

* Add storage of code and cleanup.

* Start state bridge refactor.

* More cleanup and fixes.

* Use RocksDb as backend for state.

* Implement transactions.

* Build RocksDb dependency when building fluffy tools.

* Move code to world state helper.

* Implement producer and consumer queue.

* Cleanup exceptions.

* Improve logging.

* Add update caches to DatabaseRef backends.
2024-07-18 17:01:40 +08:00

68 lines
2.2 KiB
Nim

# Fluffy
# Copyright (c) 2024 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: [].}
import
chronicles,
json_rpc/rpcclient,
web3/[eth_api, eth_api_types],
../../rpc/rpc_calls/rpc_trace_calls,
./portal_bridge_conf
export rpcclient
proc newRpcClientConnect*(url: JsonRpcUrl): RpcClient =
## Instantiate a new JSON-RPC client and try to connect. Will quit on failure.
case url.kind
of HttpUrl:
let client = newRpcHttpClient()
try:
waitFor client.connect(url.value)
except CatchableError as e:
fatal "Failed to connect to JSON-RPC server", error = $e.msg, url = url.value
quit QuitFailure
client
of WsUrl:
let client = newRpcWebSocketClient()
try:
waitFor client.connect(url.value)
except CatchableError as e:
fatal "Failed to connect to JSON-RPC server", error = $e.msg, url = url.value
quit QuitFailure
client
proc getBlockByNumber*(
client: RpcClient, blockId: BlockIdentifier, fullTransactions: bool = true
): Future[Result[BlockObject, string]] {.async: (raises: []).} =
let blck =
try:
let res = await client.eth_getBlockByNumber(blockId, fullTransactions)
if res.isNil:
return err("EL failed to provide requested block")
res
except CatchableError as e:
return err("EL JSON-RPC eth_getBlockByNumber failed: " & e.msg)
return ok(blck)
proc getUncleByBlockNumberAndIndex*(
client: RpcClient, blockId: BlockIdentifier, index: Quantity
): Future[Result[BlockObject, string]] {.async: (raises: []).} =
let blck =
try:
let res = await client.eth_getUncleByBlockNumberAndIndex(blockId, index)
if res.isNil:
return err("EL failed to provide requested uncle block")
res
except CatchableError as e:
return err("EL JSON-RPC eth_getUncleByBlockNumberAndIndex failed: " & e.msg)
return ok(blck)