2022-11-23 16:11:38 +00:00
|
|
|
# nimbus_verified_proxy
|
2023-01-31 12:38:08 +00:00
|
|
|
# Copyright (c) 2022-2023 Status Research & Development GmbH
|
2022-08-26 11:54:10 +00:00
|
|
|
# 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.
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2022-08-26 11:54:10 +00:00
|
|
|
|
|
|
|
import
|
2022-09-28 08:01:32 +00:00
|
|
|
std/strutils,
|
2022-08-31 05:48:48 +00:00
|
|
|
stint,
|
2022-09-28 08:01:32 +00:00
|
|
|
stew/[byteutils, results],
|
2022-08-31 05:48:48 +00:00
|
|
|
chronicles,
|
2022-09-24 11:57:27 +00:00
|
|
|
json_rpc/[rpcproxy, rpcserver, rpcclient],
|
2022-11-21 12:28:15 +00:00
|
|
|
eth/common/eth_types as etypes,
|
2022-08-31 05:48:48 +00:00
|
|
|
web3,
|
2022-09-21 16:44:46 +00:00
|
|
|
web3/[ethhexstrings, ethtypes],
|
2023-06-04 06:00:50 +00:00
|
|
|
beacon_chain/el/el_manager,
|
2022-09-21 16:44:46 +00:00
|
|
|
beacon_chain/networking/network_metadata,
|
2022-09-06 16:14:50 +00:00
|
|
|
beacon_chain/spec/forks,
|
2022-11-23 11:46:09 +00:00
|
|
|
./rpc_utils,
|
2022-09-28 08:01:32 +00:00
|
|
|
../validate_proof,
|
|
|
|
../block_cache
|
2022-08-26 11:54:10 +00:00
|
|
|
|
2022-08-31 05:48:48 +00:00
|
|
|
export forks
|
|
|
|
|
|
|
|
logScope:
|
2022-11-23 16:11:38 +00:00
|
|
|
topics = "verified_proxy"
|
2022-08-26 11:54:10 +00:00
|
|
|
|
2022-09-21 16:44:46 +00:00
|
|
|
proc `==`(x, y: Quantity): bool {.borrow, noSideEffect.}
|
|
|
|
|
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-09-09 13:59:36 +00:00
|
|
|
template encodeHexData(value: UInt256): HexDataStr =
|
2022-09-10 19:00:27 +00:00
|
|
|
hexDataStr("0x" & toBytesBE(value).toHex)
|
2022-09-09 13:59:36 +00:00
|
|
|
|
2022-11-21 12:28:15 +00:00
|
|
|
template bytesToHex(bytes: seq[byte]): HexDataStr =
|
|
|
|
hexDataStr("0x" & toHex(bytes))
|
|
|
|
|
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
|
|
|
|
2022-09-28 08:01:32 +00:00
|
|
|
type
|
2022-11-23 16:11:38 +00:00
|
|
|
VerifiedRpcProxy* = ref object
|
2022-09-28 08:01:32 +00:00
|
|
|
proxy: RpcProxy
|
|
|
|
blockCache: BlockCache
|
|
|
|
chainId: Quantity
|
|
|
|
|
|
|
|
QuantityTagKind = enum
|
|
|
|
LatestBlock, BlockNumber
|
|
|
|
|
|
|
|
QuantityTag = object
|
|
|
|
case kind: QuantityTagKind
|
|
|
|
of LatestBlock:
|
|
|
|
discard
|
|
|
|
of BlockNumber:
|
|
|
|
blockNumber: Quantity
|
|
|
|
|
|
|
|
func parseHexIntResult(tag: string): Result[uint64, string] =
|
|
|
|
try:
|
|
|
|
ok(parseHexInt(tag).uint64)
|
|
|
|
except ValueError as e:
|
|
|
|
err(e.msg)
|
|
|
|
|
|
|
|
func parseHexQuantity(tag: string): Result[Quantity, string] =
|
|
|
|
let hexQuantity = hexQuantityStr(tag)
|
|
|
|
if validate(hexQuantity):
|
|
|
|
let parsed = ? parseHexIntResult(tag)
|
|
|
|
return ok(Quantity(parsed))
|
|
|
|
else:
|
2022-11-24 13:41:00 +00:00
|
|
|
return err("Invalid hex quantity.")
|
2022-09-28 08:01:32 +00:00
|
|
|
|
|
|
|
func parseQuantityTag(blockTag: string): Result[QuantityTag, string] =
|
|
|
|
let tag = blockTag.toLowerAscii
|
|
|
|
case tag
|
|
|
|
of "latest":
|
|
|
|
return ok(QuantityTag(kind: LatestBlock))
|
|
|
|
else:
|
|
|
|
let quantity = ? parseHexQuantity(tag)
|
|
|
|
return ok(QuantityTag(kind: BlockNumber, blockNumber: quantity))
|
|
|
|
|
2022-11-23 16:11:38 +00:00
|
|
|
template checkPreconditions(proxy: VerifiedRpcProxy) =
|
2022-09-28 08:01:32 +00:00
|
|
|
if proxy.blockCache.isEmpty():
|
2022-09-09 13:59:36 +00:00
|
|
|
raise newException(ValueError, "Syncing")
|
|
|
|
|
2022-11-24 13:41:00 +00:00
|
|
|
template rpcClient(lcProxy: VerifiedRpcProxy): RpcClient =
|
|
|
|
lcProxy.proxy.getClient()
|
2022-09-24 11:57:27 +00:00
|
|
|
|
2022-09-28 08:01:32 +00:00
|
|
|
proc getPayloadByTag(
|
2022-11-23 16:11:38 +00:00
|
|
|
proxy: VerifiedRpcProxy,
|
2022-11-24 13:41:00 +00:00
|
|
|
quantityTag: string):
|
2023-01-31 12:38:08 +00:00
|
|
|
results.Opt[ExecutionData] {.raises: [ValueError].} =
|
2022-09-28 08:01:32 +00:00
|
|
|
checkPreconditions(proxy)
|
|
|
|
|
|
|
|
let tagResult = parseQuantityTag(quantityTag)
|
|
|
|
|
|
|
|
if tagResult.isErr:
|
|
|
|
raise newException(ValueError, tagResult.error)
|
|
|
|
|
|
|
|
let tag = tagResult.get()
|
|
|
|
|
|
|
|
case tag.kind
|
|
|
|
of LatestBlock:
|
2022-11-23 11:46:09 +00:00
|
|
|
# this will always return some block, as we always checkPreconditions
|
|
|
|
return proxy.blockCache.latest
|
2022-09-28 08:01:32 +00:00
|
|
|
of BlockNumber:
|
2022-11-23 11:46:09 +00:00
|
|
|
return proxy.blockCache.getByNumber(tag.blockNumber)
|
|
|
|
|
|
|
|
proc getPayloadByTagOrThrow(
|
2022-11-23 16:11:38 +00:00
|
|
|
proxy: VerifiedRpcProxy,
|
2023-01-31 12:38:08 +00:00
|
|
|
quantityTag: string): ExecutionData {.raises: [ValueError].} =
|
2022-09-28 08:01:32 +00:00
|
|
|
|
2022-11-23 11:46:09 +00:00
|
|
|
let tagResult = getPayloadByTag(proxy, quantityTag)
|
2022-09-28 08:01:32 +00:00
|
|
|
|
2022-11-23 11:46:09 +00:00
|
|
|
if tagResult.isErr:
|
|
|
|
raise newException(ValueError, "No block stored for given tag " & quantityTag)
|
2022-08-26 11:54:10 +00:00
|
|
|
|
2022-11-23 11:46:09 +00:00
|
|
|
return tagResult.get()
|
|
|
|
|
2022-11-23 16:11:38 +00:00
|
|
|
proc installEthApiHandlers*(lcProxy: VerifiedRpcProxy) =
|
2022-09-24 11:57:27 +00:00
|
|
|
lcProxy.proxy.rpc("eth_chainId") do() -> HexQuantityStr:
|
2022-09-21 16:44:46 +00:00
|
|
|
return encodeQuantity(lcProxy.chainId)
|
|
|
|
|
2022-09-24 11:57:27 +00:00
|
|
|
lcProxy.proxy.rpc("eth_blockNumber") do() -> HexQuantityStr:
|
2022-11-24 13:41:00 +00:00
|
|
|
## Returns the number of the most recent block.
|
2022-09-28 08:01:32 +00:00
|
|
|
checkPreconditions(lcProxy)
|
2022-08-26 11:54:10 +00:00
|
|
|
|
2022-09-28 08:01:32 +00:00
|
|
|
return encodeQuantity(lcProxy.blockCache.latest.get.blockNumber)
|
2022-08-26 11:54:10 +00:00
|
|
|
|
2022-11-24 13:41:00 +00:00
|
|
|
lcProxy.proxy.rpc("eth_getBalance") do(
|
|
|
|
address: Address, quantityTag: string) -> HexQuantityStr:
|
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
|
2022-09-06 16:14:50 +00:00
|
|
|
let
|
2022-11-23 11:46:09 +00:00
|
|
|
executionPayload = lcProxy.getPayloadByTagOrThrow(quantityTag)
|
2022-09-06 16:14:50 +00:00
|
|
|
blockNumber = executionPayload.blockNumber.uint64
|
2022-08-31 05:48:48 +00:00
|
|
|
|
2022-11-24 13:41:00 +00:00
|
|
|
info "Forwarding eth_getBalance call", blockNumber
|
2022-08-31 05:48:48 +00:00
|
|
|
|
2022-11-24 13:41:00 +00:00
|
|
|
let proof = await lcProxy.rpcClient.eth_getProof(
|
|
|
|
address, @[], blockId(blockNumber))
|
2022-09-06 16:14:50 +00:00
|
|
|
|
2022-09-15 11:04:41 +00:00
|
|
|
let accountResult = getAccountFromProof(
|
2022-09-06 16:14:50 +00:00
|
|
|
executionPayload.stateRoot,
|
|
|
|
proof.address,
|
|
|
|
proof.balance,
|
|
|
|
proof.nonce,
|
|
|
|
proof.codeHash,
|
|
|
|
proof.storageHash,
|
|
|
|
proof.accountProof
|
|
|
|
)
|
|
|
|
|
2022-09-15 11:04:41 +00:00
|
|
|
if accountResult.isOk():
|
|
|
|
return encodeQuantity(accountResult.get.balance)
|
2022-09-06 16:14:50 +00:00
|
|
|
else:
|
2022-09-15 11:04:41 +00:00
|
|
|
raise newException(ValueError, accountResult.error)
|
2022-09-09 13:59:36 +00:00
|
|
|
|
2022-11-24 13:41:00 +00:00
|
|
|
lcProxy.proxy.rpc("eth_getStorageAt") do(
|
|
|
|
address: Address, slot: HexDataStr, quantityTag: string) -> HexDataStr:
|
2022-09-09 13:59:36 +00:00
|
|
|
let
|
2022-11-23 11:46:09 +00:00
|
|
|
executionPayload = lcProxy.getPayloadByTagOrThrow(quantityTag)
|
2022-09-09 13:59:36 +00:00
|
|
|
uslot = UInt256.fromHex(slot.string)
|
|
|
|
blockNumber = executionPayload.blockNumber.uint64
|
|
|
|
|
2022-11-24 13:41:00 +00:00
|
|
|
info "Forwarding eth_getStorageAt", blockNumber
|
2022-09-09 13:59:36 +00:00
|
|
|
|
2022-11-24 13:41:00 +00:00
|
|
|
let proof = await lcProxy.rpcClient.eth_getProof(
|
|
|
|
address, @[uslot], blockId(blockNumber))
|
2022-09-09 13:59:36 +00:00
|
|
|
|
|
|
|
let dataResult = getStorageData(executionPayload.stateRoot, uslot, proof)
|
|
|
|
|
|
|
|
if dataResult.isOk():
|
|
|
|
let slotValue = dataResult.get()
|
|
|
|
return encodeHexData(slotValue)
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, dataResult.error)
|
2022-09-21 16:44:46 +00:00
|
|
|
|
2022-11-24 13:41:00 +00:00
|
|
|
lcProxy.proxy.rpc("eth_getTransactionCount") do(
|
|
|
|
address: Address, quantityTag: string) -> HexQuantityStr:
|
2022-09-28 20:43:10 +00:00
|
|
|
let
|
2022-11-23 11:46:09 +00:00
|
|
|
executionPayload = lcProxy.getPayloadByTagOrThrow(quantityTag)
|
2022-09-28 20:43:10 +00:00
|
|
|
blockNumber = executionPayload.blockNumber.uint64
|
|
|
|
|
2022-11-24 13:41:00 +00:00
|
|
|
info "Forwarding eth_getTransactionCount", blockNumber
|
2022-09-28 20:43:10 +00:00
|
|
|
|
2022-11-24 13:41:00 +00:00
|
|
|
let proof = await lcProxy.rpcClient.eth_getProof(
|
|
|
|
address, @[], blockId(blockNumber))
|
2022-09-28 20:43:10 +00:00
|
|
|
|
|
|
|
let accountResult = getAccountFromProof(
|
|
|
|
executionPayload.stateRoot,
|
|
|
|
proof.address,
|
|
|
|
proof.balance,
|
|
|
|
proof.nonce,
|
|
|
|
proof.codeHash,
|
|
|
|
proof.storageHash,
|
|
|
|
proof.accountProof
|
|
|
|
)
|
|
|
|
|
|
|
|
if accountResult.isOk():
|
|
|
|
return hexQuantityStr(encodeQuantity(accountResult.get.nonce))
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, accountResult.error)
|
|
|
|
|
2022-11-24 13:41:00 +00:00
|
|
|
lcProxy.proxy.rpc("eth_getCode") do(
|
|
|
|
address: Address, quantityTag: string) -> HexDataStr:
|
2022-11-21 12:28:15 +00:00
|
|
|
let
|
2022-11-23 11:46:09 +00:00
|
|
|
executionPayload = lcProxy.getPayloadByTagOrThrow(quantityTag)
|
2022-11-21 12:28:15 +00:00
|
|
|
blockNumber = executionPayload.blockNumber.uint64
|
|
|
|
|
|
|
|
let
|
2022-11-24 13:41:00 +00:00
|
|
|
proof = await lcProxy.rpcClient.eth_getProof(
|
|
|
|
address, @[], blockId(blockNumber))
|
2022-11-21 12:28:15 +00:00
|
|
|
accountResult = getAccountFromProof(
|
|
|
|
executionPayload.stateRoot,
|
|
|
|
proof.address,
|
|
|
|
proof.balance,
|
|
|
|
proof.nonce,
|
|
|
|
proof.codeHash,
|
|
|
|
proof.storageHash,
|
|
|
|
proof.accountProof
|
|
|
|
)
|
|
|
|
|
|
|
|
if accountResult.isErr():
|
|
|
|
raise newException(ValueError, accountResult.error)
|
|
|
|
|
|
|
|
let account = accountResult.get()
|
|
|
|
|
|
|
|
if account.codeHash == etypes.EMPTY_CODE_HASH:
|
|
|
|
# account does not have any code, return empty hex data
|
|
|
|
return hexDataStr("0x")
|
|
|
|
|
2022-11-24 13:41:00 +00:00
|
|
|
info "Forwarding eth_getCode", blockNumber
|
|
|
|
|
2022-11-21 12:28:15 +00:00
|
|
|
let code = await lcProxy.rpcClient.eth_getCode(
|
|
|
|
address,
|
|
|
|
blockId(blockNumber)
|
|
|
|
)
|
|
|
|
|
|
|
|
if isValidCode(account, code):
|
|
|
|
return bytesToHex(code)
|
|
|
|
else:
|
2022-11-24 13:41:00 +00:00
|
|
|
raise newException(ValueError,
|
|
|
|
"Received code which does not match the account code hash")
|
2022-11-21 12:28:15 +00:00
|
|
|
|
2022-11-24 13:41:00 +00:00
|
|
|
# TODO:
|
|
|
|
# Following methods are forwarded directly to the web3 provider and therefore
|
|
|
|
# are not validated in any way.
|
2022-09-24 11:57:27 +00:00
|
|
|
lcProxy.proxy.registerProxyMethod("net_version")
|
|
|
|
lcProxy.proxy.registerProxyMethod("eth_call")
|
2022-09-28 20:43:10 +00:00
|
|
|
lcProxy.proxy.registerProxyMethod("eth_sendRawTransaction")
|
|
|
|
lcProxy.proxy.registerProxyMethod("eth_getTransactionReceipt")
|
2022-09-24 11:57:27 +00:00
|
|
|
|
2022-11-23 11:46:09 +00:00
|
|
|
# TODO currently we do not handle fullTransactions flag. It require updates on
|
|
|
|
# nim-web3 side
|
2022-11-24 13:41:00 +00:00
|
|
|
lcProxy.proxy.rpc("eth_getBlockByNumber") do(
|
|
|
|
quantityTag: string, fullTransactions: bool) -> Option[BlockObject]:
|
2022-11-23 11:46:09 +00:00
|
|
|
let executionPayload = lcProxy.getPayloadByTag(quantityTag)
|
|
|
|
|
|
|
|
if executionPayload.isErr:
|
|
|
|
return none(BlockObject)
|
|
|
|
|
|
|
|
return some(asBlockObject(executionPayload.get()))
|
|
|
|
|
2022-11-24 13:41:00 +00:00
|
|
|
lcProxy.proxy.rpc("eth_getBlockByHash") do(
|
|
|
|
blockHash: BlockHash, fullTransactions: bool) -> Option[BlockObject]:
|
2022-11-23 11:46:09 +00:00
|
|
|
let executionPayload = lcProxy.blockCache.getPayloadByHash(blockHash)
|
|
|
|
|
|
|
|
if executionPayload.isErr:
|
|
|
|
return none(BlockObject)
|
|
|
|
|
|
|
|
return some(asBlockObject(executionPayload.get()))
|
2022-09-24 11:57:27 +00:00
|
|
|
|
2022-09-21 16:44:46 +00:00
|
|
|
proc new*(
|
2022-11-23 16:11:38 +00:00
|
|
|
T: type VerifiedRpcProxy,
|
2022-09-24 11:57:27 +00:00
|
|
|
proxy: RpcProxy,
|
2022-09-28 08:01:32 +00:00
|
|
|
blockCache: BlockCache,
|
2022-09-21 16:44:46 +00:00
|
|
|
chainId: Quantity): T =
|
2022-11-24 13:41:00 +00:00
|
|
|
VerifiedRpcProxy(
|
2022-09-24 11:57:27 +00:00
|
|
|
proxy: proxy,
|
2022-09-28 08:01:32 +00:00
|
|
|
blockCache: blockCache,
|
2022-11-24 13:41:00 +00:00
|
|
|
chainId: chainId)
|
2022-09-21 16:44:46 +00:00
|
|
|
|
2023-03-09 23:40:55 +00:00
|
|
|
# Used to be in eth1_monitor.nim; not sure why it was deleted,
|
|
|
|
# so I copied it here. --Adam
|
|
|
|
template awaitWithRetries*[T](lazyFutExpr: Future[T],
|
|
|
|
retries = 3,
|
|
|
|
timeout = 60.seconds): untyped =
|
|
|
|
const
|
|
|
|
reqType = astToStr(lazyFutExpr)
|
|
|
|
var
|
|
|
|
retryDelayMs = 16000
|
|
|
|
f: Future[T]
|
|
|
|
attempts = 0
|
|
|
|
|
|
|
|
while true:
|
|
|
|
f = lazyFutExpr
|
|
|
|
yield f or sleepAsync(timeout)
|
|
|
|
if not f.finished:
|
|
|
|
await cancelAndWait(f)
|
|
|
|
elif f.failed:
|
|
|
|
when not (f.error of CatchableError):
|
|
|
|
static: doAssert false, "f.error not CatchableError"
|
|
|
|
debug "Web3 request failed", req = reqType, err = f.error.msg
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
inc attempts
|
|
|
|
if attempts >= retries:
|
|
|
|
var errorMsg = reqType & " failed " & $retries & " times"
|
|
|
|
if f.failed: errorMsg &= ". Last error: " & f.error.msg
|
|
|
|
raise newException(DataProviderFailure, errorMsg)
|
|
|
|
|
|
|
|
await sleepAsync(chronos.milliseconds(retryDelayMs))
|
|
|
|
retryDelayMs *= 2
|
|
|
|
|
|
|
|
read(f)
|
|
|
|
|
2022-11-23 16:11:38 +00:00
|
|
|
proc verifyChaindId*(p: VerifiedRpcProxy): Future[void] {.async.} =
|
2022-09-21 16:44:46 +00:00
|
|
|
let localId = p.chainId
|
|
|
|
|
2022-11-24 13:41:00 +00:00
|
|
|
# retry 2 times, if the data provider fails despite the re-tries, propagate
|
2022-09-21 16:44:46 +00:00
|
|
|
# exception to the caller.
|
|
|
|
let providerId = awaitWithRetries(
|
2022-09-24 11:57:27 +00:00
|
|
|
p.rpcClient.eth_chainId(),
|
2022-09-21 16:44:46 +00:00
|
|
|
retries = 2,
|
|
|
|
timeout = seconds(30)
|
|
|
|
)
|
|
|
|
|
2022-11-24 13:41:00 +00:00
|
|
|
# This is a chain/network mismatch error between the Nimbus verified proxy and
|
|
|
|
# the application using it. Fail fast to avoid misusage. The user must fix
|
|
|
|
# the configuration.
|
2022-09-21 16:44:46 +00:00
|
|
|
if localId != providerId:
|
|
|
|
fatal "The specified data provider serves data for a different chain",
|
|
|
|
expectedChain = distinctBase(localId),
|
|
|
|
providerChain = distinctBase(providerId)
|
|
|
|
quit 1
|
|
|
|
|
|
|
|
return
|
|
|
|
|