2022-08-26 11:54:10 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2022 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: [Defect].}
|
|
|
|
|
|
|
|
import
|
2022-08-31 05:48:48 +00:00
|
|
|
stint,
|
|
|
|
chronicles,
|
|
|
|
json_rpc/[rpcserver, rpcclient],
|
|
|
|
web3,
|
|
|
|
web3/ethhexstrings,
|
2022-08-26 11:54:10 +00:00
|
|
|
beacon_chain/eth1/eth1_monitor,
|
|
|
|
beacon_chain/spec/forks
|
|
|
|
|
2022-08-31 05:48:48 +00:00
|
|
|
export forks
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "light_proxy"
|
2022-08-26 11:54:10 +00:00
|
|
|
|
|
|
|
template encodeQuantity(value: UInt256): HexQuantityStr =
|
2022-08-31 05:48:48 +00:00
|
|
|
hexQuantityStr("0x" & value.toHex())
|
2022-08-26 11:54:10 +00:00
|
|
|
|
2022-08-31 05:48:48 +00:00
|
|
|
template encodeQuantity(value: Quantity): HexQuantityStr =
|
|
|
|
hexQuantityStr(encodeQuantity(value.uint64))
|
2022-08-26 11:54:10 +00:00
|
|
|
|
|
|
|
type LightClientRpcProxy* = ref object
|
2022-08-31 05:48:48 +00:00
|
|
|
client*: RpcClient
|
|
|
|
server*: RpcHttpServer
|
2022-08-26 11:54:10 +00:00
|
|
|
executionPayload*: Opt[ExecutionPayloadV1]
|
|
|
|
|
|
|
|
proc installEthApiHandlers*(lcProxy: LightClientRpcProxy) =
|
|
|
|
template payload(): Opt[ExecutionPayloadV1] = lcProxy.executionPayload
|
|
|
|
|
2022-08-31 05:48:48 +00:00
|
|
|
lcProxy.server.rpc("eth_blockNumber") do() -> HexQuantityStr:
|
2022-08-26 11:54:10 +00:00
|
|
|
## Returns the number of most recent block.
|
|
|
|
if payload.isNone:
|
|
|
|
raise newException(ValueError, "Syncing")
|
|
|
|
|
|
|
|
return encodeQuantity(payload.get.blockNumber)
|
|
|
|
|
2022-08-31 05:48:48 +00:00
|
|
|
# TODO quantity tag should be better typed
|
|
|
|
lcProxy.server.rpc("eth_getBalance") do(address: Address, quantityTag: string) -> HexQuantityStr:
|
2022-08-26 11:54:10 +00:00
|
|
|
if payload.isNone:
|
|
|
|
raise newException(ValueError, "Syncing")
|
|
|
|
|
|
|
|
if quantityTag != "latest":
|
2022-08-31 05:48:48 +00:00
|
|
|
# TODO for now we support only latest block, as its semanticly most streight
|
|
|
|
# forward i.e it is last received and valid ExecutionPayloadV1.
|
|
|
|
# Ultimatly we could keep track of n last valid payload and support number
|
|
|
|
# queries for this set of blocks
|
|
|
|
# `Pending` coud be mapped to some optimisc header with block fetched on demand
|
2022-08-26 11:54:10 +00:00
|
|
|
raise newException(ValueError, "Only latest block is supported")
|
|
|
|
|
2022-08-31 05:48:48 +00:00
|
|
|
# When requesting state for `latest` block number, we need to translate
|
|
|
|
# `latest` to actual block number as `latest` on proxy and on data provider
|
|
|
|
# can mean different blocks and ultimatly piece received piece of state
|
|
|
|
# must by validated against correct state root
|
|
|
|
let blockNumber = payload.get.blockNumber.uint64
|
|
|
|
|
|
|
|
info "Forwarding get_Balance", executionBn = blockNumber
|
|
|
|
|
|
|
|
# TODO this could be realised by eth_getProof as it return also balance
|
|
|
|
# of the account
|
|
|
|
let b = await lcProxy.client.eth_getBalance(address, blockId(blockNumber))
|
2022-08-26 11:54:10 +00:00
|
|
|
|
2022-08-31 05:48:48 +00:00
|
|
|
return encodeQuantity(b)
|