2021-06-18 17:20:48 +00:00
|
|
|
# Nimbus
|
2022-02-11 13:43:10 +00:00
|
|
|
# Copyright (c) 2021-2022 Status Research & Development GmbH
|
2021-06-18 17:20:48 +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.
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
import
|
2022-09-10 13:05:32 +00:00
|
|
|
std/[times, sequtils, typetraits],
|
2022-09-03 18:15:35 +00:00
|
|
|
json_rpc/[rpcproxy, rpcserver], stew/byteutils,
|
2022-02-11 13:43:10 +00:00
|
|
|
web3/conversions, # sigh, for FixedBytes marshalling
|
2022-02-22 10:52:44 +00:00
|
|
|
eth/[common/eth_types, rlp],
|
2022-06-29 15:44:08 +00:00
|
|
|
../../nimbus/rpc/[rpc_types, hexstrings, filters],
|
2022-12-02 04:39:12 +00:00
|
|
|
../../nimbus/transaction,
|
|
|
|
../../nimbus/common/chain_config,
|
2022-02-11 13:43:10 +00:00
|
|
|
../network/history/[history_network, history_content]
|
2021-06-18 17:20:48 +00:00
|
|
|
|
|
|
|
# Subset of Eth JSON-RPC API: https://eth.wiki/json-rpc/API
|
|
|
|
# Supported subset will eventually be found here:
|
|
|
|
# https://github.com/ethereum/stateless-ethereum-specs/blob/master/portal-network.md#json-rpc-api
|
|
|
|
#
|
|
|
|
# In order to already support these calls before every part of the Portal
|
|
|
|
# Network is up, one plan is to get the data directly from an external client
|
|
|
|
# through RPC calls. Practically just playing a proxy to that client.
|
|
|
|
# Can be done by just forwarding the rpc call, or by adding a call here, but
|
|
|
|
# that would introduce a unnecessary serializing/deserializing step.
|
|
|
|
|
2022-05-22 20:44:15 +00:00
|
|
|
# Some similar code as from nimbus `rpc_utils`, but avoiding that import as it
|
|
|
|
# brings in a lot more. Should restructure `rpc_utils` a bit before using that.
|
|
|
|
func toHash*(value: array[32, byte]): Hash256 =
|
|
|
|
result.data = value
|
|
|
|
|
|
|
|
func toHash*(value: EthHashStr): Hash256 {.raises: [Defect, ValueError].} =
|
|
|
|
hexToPaddedByteArray[32](value.string).toHash
|
|
|
|
|
|
|
|
func init*(
|
|
|
|
T: type TransactionObject,
|
|
|
|
tx: Transaction, header: BlockHeader, txIndex: int):
|
|
|
|
T {.raises: [Defect, ValidationError].} =
|
|
|
|
TransactionObject(
|
|
|
|
blockHash: some(header.blockHash),
|
|
|
|
blockNumber: some(encodeQuantity(header.blockNumber)),
|
|
|
|
`from`: tx.getSender(),
|
|
|
|
gas: encodeQuantity(tx.gasLimit.uint64),
|
|
|
|
gasPrice: encodeQuantity(tx.gasPrice.uint64),
|
|
|
|
hash: tx.rlpHash,
|
|
|
|
input: tx.payload,
|
|
|
|
nonce: encodeQuantity(tx.nonce.uint64),
|
|
|
|
to: some(tx.destination),
|
|
|
|
transactionIndex: some(encodeQuantity(txIndex.uint64)),
|
|
|
|
value: encodeQuantity(tx.value),
|
|
|
|
v: encodeQuantity(tx.V.uint),
|
|
|
|
r: encodeQuantity(tx.R),
|
|
|
|
s: encodeQuantity(tx.S)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Note: Similar as `populateBlockObject` from rpc_utils, but lacking the
|
|
|
|
# total difficulty
|
|
|
|
func init*(
|
|
|
|
T: type BlockObject,
|
2022-02-22 10:52:44 +00:00
|
|
|
header: BlockHeader, body: BlockBody,
|
|
|
|
fullTx = true, isUncle = false):
|
2022-05-22 20:44:15 +00:00
|
|
|
T {.raises: [Defect, ValidationError].} =
|
2022-02-11 13:43:10 +00:00
|
|
|
let blockHash = header.blockHash
|
|
|
|
|
2022-05-22 20:44:15 +00:00
|
|
|
var blockObject = BlockObject(
|
|
|
|
number: some(encodeQuantity(header.blockNumber)),
|
|
|
|
hash: some(blockHash),
|
|
|
|
parentHash: header.parentHash,
|
|
|
|
nonce: some(hexDataStr(header.nonce)),
|
|
|
|
sha3Uncles: header.ommersHash,
|
|
|
|
logsBloom: FixedBytes[256] header.bloom,
|
|
|
|
transactionsRoot: header.txRoot,
|
|
|
|
stateRoot: header.stateRoot,
|
|
|
|
receiptsRoot: header.receiptRoot,
|
|
|
|
miner: header.coinbase,
|
|
|
|
difficulty: encodeQuantity(header.difficulty),
|
|
|
|
extraData: hexDataStr(header.extraData),
|
|
|
|
# TODO: This is optional according to
|
|
|
|
# https://playground.open-rpc.org/?schemaUrl=https://raw.githubusercontent.com/ethereum/eth1.0-apis/assembled-spec/openrpc.json
|
|
|
|
# So we should probably change `BlockObject`.
|
|
|
|
totalDifficulty: encodeQuantity(UInt256.low()),
|
|
|
|
gasLimit: encodeQuantity(header.gasLimit.uint64),
|
|
|
|
gasUsed: encodeQuantity(header.gasUsed.uint64),
|
2022-09-10 19:00:27 +00:00
|
|
|
timestamp: encodeQuantity(header.timestamp.toUnix.uint64)
|
2022-05-22 20:44:15 +00:00
|
|
|
)
|
2022-02-11 13:43:10 +00:00
|
|
|
|
|
|
|
let size = sizeof(BlockHeader) - sizeof(Blob) + header.extraData.len
|
2022-05-22 20:44:15 +00:00
|
|
|
blockObject.size = encodeQuantity(size.uint)
|
2022-02-11 13:43:10 +00:00
|
|
|
|
2022-02-22 10:52:44 +00:00
|
|
|
if not isUncle:
|
2022-05-22 20:44:15 +00:00
|
|
|
blockObject.uncles =
|
|
|
|
body.uncles.map(proc(h: BlockHeader): Hash256 = h.blockHash)
|
2022-02-22 10:52:44 +00:00
|
|
|
|
|
|
|
if fullTx:
|
|
|
|
var i = 0
|
|
|
|
for tx in body.transactions:
|
2022-05-22 20:44:15 +00:00
|
|
|
# ValidationError from tx.getSender in TransactionObject.init
|
|
|
|
blockObject.transactions.add %(TransactionObject.init(tx, header, i))
|
2022-02-22 10:52:44 +00:00
|
|
|
inc i
|
|
|
|
else:
|
|
|
|
for tx in body.transactions:
|
2022-09-03 18:15:35 +00:00
|
|
|
blockObject.transactions.add %(keccakHash(rlp.encode(tx)))
|
2022-05-22 20:44:15 +00:00
|
|
|
|
|
|
|
blockObject
|
2022-02-22 10:52:44 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
proc installEthApiHandlers*(
|
|
|
|
# Currently only HistoryNetwork needed, later we might want a master object
|
|
|
|
# holding all the networks.
|
|
|
|
rpcServerWithProxy: var RpcProxy, historyNetwork: HistoryNetwork)
|
2021-06-18 17:20:48 +00:00
|
|
|
{.raises: [Defect, CatchableError].} =
|
|
|
|
|
|
|
|
# Supported API
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_blockNumber")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_call")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-09-10 13:05:32 +00:00
|
|
|
# rpcServerWithProxy.registerProxyMethod("eth_chainId")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_estimateGas")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_feeHistory")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_getBalance")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
# rpcServerWithProxy.registerProxyMethod("eth_getBlockByHash")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-08-04 06:34:53 +00:00
|
|
|
# rpcServerWithProxy.registerProxyMethod("eth_getBlockByNumber")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-22 10:52:44 +00:00
|
|
|
# rpcServerWithProxy.registerProxyMethod("eth_getBlockTransactionCountByHash")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_getBlockTransactionCountByNumber")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_getCode")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_getRawTransactionByHash")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_getRawTransactionByBlockHashAndIndex")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_getRawTransactionByBlockNumberAndIndex")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_getStorageAt")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_getTransactionByBlockHashAndIndex")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_getTransactionByBlockNumberAndIndex")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_getTransactionByHash")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_getTransactionCount")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_getTransactionReceipt")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_getUncleByBlockHashAndIndex")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_getUncleByBlockNumberAndIndex")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_getUncleCountByBlockHash")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_getUncleCountByBlockNumber")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_getProof")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_sendRawTransaction")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
|
|
|
# Optional API
|
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_gasPrice")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_getFilterChanges")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_getFilterLogs")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-06-29 15:44:08 +00:00
|
|
|
# rpcServerWithProxy.registerProxyMethod("eth_getLogs")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_newBlockFilter")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_newFilter")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_newPendingTransactionFilter")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_pendingTransactions")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_syncing")
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2021-08-05 06:14:25 +00:00
|
|
|
rpcServerWithProxy.registerProxyMethod("eth_uninstallFilter")
|
2022-02-11 13:43:10 +00:00
|
|
|
|
|
|
|
# Supported API through the Portal Network
|
|
|
|
|
2022-09-10 13:05:32 +00:00
|
|
|
rpcServerWithProxy.rpc("eth_chainId") do() -> HexQuantityStr:
|
|
|
|
# The Portal Network can only support MainNet at the moment
|
|
|
|
return encodeQuantity(distinctBase(MainNet.ChainId))
|
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
rpcServerWithProxy.rpc("eth_getBlockByHash") do(
|
|
|
|
data: EthHashStr, fullTransactions: bool) -> Option[BlockObject]:
|
|
|
|
## Returns information about a block by hash.
|
|
|
|
##
|
|
|
|
## data: Hash of a block.
|
|
|
|
## fullTransactions: If true it returns the full transaction objects, if
|
|
|
|
## false only the hashes of the transactions.
|
|
|
|
##
|
|
|
|
## Returns BlockObject or nil when no block was found.
|
2022-02-22 10:52:44 +00:00
|
|
|
let
|
|
|
|
blockHash = data.toHash()
|
2022-12-09 16:59:36 +00:00
|
|
|
(header, body) = (await historyNetwork.getBlock(blockHash)).valueOr:
|
|
|
|
return none(BlockObject)
|
2022-02-22 10:52:44 +00:00
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
return some(BlockObject.init(header, body))
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2022-09-10 13:05:32 +00:00
|
|
|
# TODO: add test to local testnet, it requires activating accumulator
|
|
|
|
# in testnet script
|
|
|
|
rpcServerWithProxy.rpc("eth_getBlockByNumber") do(
|
|
|
|
quantityTag: string, fullTransactions: bool) -> Option[BlockObject]:
|
|
|
|
# TODO: for now support only numeric queries, as it is not obvious how to
|
|
|
|
# retrieve pending or even latest block.
|
2022-08-04 06:34:53 +00:00
|
|
|
if not isValidHexQuantity(quantityTag):
|
|
|
|
raise newException(ValueError, "Provided tag should be valid hex number")
|
|
|
|
|
2022-09-10 13:05:32 +00:00
|
|
|
let
|
|
|
|
blockNumber = fromHex(UInt256, quantityTag)
|
2022-12-09 16:59:36 +00:00
|
|
|
maybeBlock = (await historyNetwork.getBlock(blockNumber)).valueOr:
|
|
|
|
raise newException(ValueError, error)
|
2022-08-04 06:34:53 +00:00
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
if maybeBlock.isNone():
|
|
|
|
return none(BlockObject)
|
2022-08-04 06:34:53 +00:00
|
|
|
else:
|
2022-12-09 16:59:36 +00:00
|
|
|
let (header, body) = maybeBlock.get()
|
|
|
|
return some(BlockObject.init(header, body))
|
2022-08-04 06:34:53 +00:00
|
|
|
|
2022-02-22 10:52:44 +00:00
|
|
|
rpcServerWithProxy.rpc("eth_getBlockTransactionCountByHash") do(
|
|
|
|
data: EthHashStr) -> HexQuantityStr:
|
|
|
|
## Returns the number of transactions in a block from a block matching the
|
|
|
|
## given block hash.
|
|
|
|
##
|
|
|
|
## data: hash of a block
|
|
|
|
## Returns integer of the number of transactions in this block.
|
|
|
|
let
|
|
|
|
blockHash = data.toHash()
|
2022-12-09 16:59:36 +00:00
|
|
|
(_, body) = (await historyNetwork.getBlock(blockHash)).valueOr:
|
|
|
|
raise newException(ValueError, "Could not find block with requested hash")
|
2022-02-22 10:52:44 +00:00
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
var txCount: uint = 0
|
|
|
|
for tx in body.transactions:
|
|
|
|
txCount.inc()
|
2022-02-22 10:52:44 +00:00
|
|
|
|
2022-12-09 16:59:36 +00:00
|
|
|
return encodeQuantity(txCount)
|
2022-02-22 10:52:44 +00:00
|
|
|
|
|
|
|
# Note: can't implement this yet as the fluffy node doesn't know the relation
|
|
|
|
# of tx hash -> block number -> block hash, in order to get the receipt
|
|
|
|
# from from the block with that block hash. The Canonical Indices Network
|
|
|
|
# would need to be implemented to get this information.
|
|
|
|
# rpcServerWithProxy.rpc("eth_getTransactionReceipt") do(
|
|
|
|
# data: EthHashStr) -> Option[ReceiptObject]:
|
2022-06-29 15:44:08 +00:00
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
rpcServerWithProxy.rpc("eth_getLogs") do(
|
|
|
|
filterOptions: FilterOptions) -> seq[FilterLog]:
|
2022-09-10 19:00:27 +00:00
|
|
|
if filterOptions.blockHash.isNone():
|
2022-07-05 12:42:55 +00:00
|
|
|
# Currently only queries by blockhash are supported.
|
|
|
|
# To support range queries the Indicies network is required.
|
|
|
|
raise newException(ValueError,
|
2022-09-10 19:00:27 +00:00
|
|
|
"Unsupported query: Only `blockHash` queries are currently supported")
|
2022-12-09 16:59:36 +00:00
|
|
|
|
|
|
|
let hash = filterOptions.blockHash.unsafeGet()
|
|
|
|
|
|
|
|
let header = (await historyNetwork.getVerifiedBlockHeader(hash)).valueOr:
|
|
|
|
raise newException(ValueError,
|
|
|
|
"Could not find header with requested hash")
|
|
|
|
|
|
|
|
if headerBloomFilter(header, filterOptions.address, filterOptions.topics):
|
|
|
|
# TODO: These queries could be done concurrently, investigate if there
|
|
|
|
# are no assumptions about usage of concurrent queries on portal
|
|
|
|
# wire protocol level
|
|
|
|
let
|
|
|
|
body = (await historyNetwork.getBlockBody(hash, header)).valueOr:
|
|
|
|
raise newException(ValueError,
|
|
|
|
"Could not find block body for requested hash")
|
|
|
|
receipts = (await historyNetwork.getReceipts(hash, header)).valueOr:
|
|
|
|
raise newException(ValueError,
|
|
|
|
"Could not find receipts for requested hash")
|
|
|
|
|
|
|
|
logs = deriveLogs(header, body.transactions, receipts)
|
|
|
|
filteredLogs = filterLogs(
|
|
|
|
logs, filterOptions.address, filterOptions.topics)
|
|
|
|
|
|
|
|
return filteredLogs
|
2022-06-29 15:44:08 +00:00
|
|
|
else:
|
2022-12-09 16:59:36 +00:00
|
|
|
# bloomfilter returned false, there are no logs matching the criteria
|
|
|
|
return @[]
|