2023-12-13 13:49:10 +00:00
|
|
|
# Fluffy
|
2024-01-24 15:28:03 +00:00
|
|
|
# Copyright (c) 2021-2024 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.
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2021-06-18 17:20:48 +00:00
|
|
|
|
|
|
|
import
|
2024-10-04 21:21:26 +00:00
|
|
|
std/sequtils,
|
2024-09-18 07:46:50 +00:00
|
|
|
json_rpc/rpcserver,
|
2024-09-12 17:03:58 +00:00
|
|
|
chronicles,
|
2024-10-04 21:21:26 +00:00
|
|
|
web3/[eth_api_types, conversions],
|
|
|
|
eth/common/transaction_utils,
|
2023-10-09 15:49:15 +00:00
|
|
|
beacon_chain/spec/forks,
|
|
|
|
../network/history/[history_network, history_content],
|
2024-06-14 17:03:18 +00:00
|
|
|
../network/state/[state_network, state_content, state_endpoints],
|
2024-09-25 03:28:45 +00:00
|
|
|
../network/beacon/beacon_light_client,
|
|
|
|
../version
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2024-10-04 14:34:31 +00:00
|
|
|
from ../../nimbus/errors import ValidationError
|
2024-04-25 13:21:09 +00:00
|
|
|
from ../../nimbus/rpc/filters import headerBloomFilter, deriveLogs, filterLogs
|
|
|
|
|
2024-10-04 21:21:26 +00:00
|
|
|
from eth/common/eth_types_rlp import rlpHash
|
|
|
|
|
2024-09-25 03:28:45 +00:00
|
|
|
export rpcserver
|
|
|
|
|
|
|
|
# See the list of Ethereum execution JSON-RPC APIs which will be supported by
|
|
|
|
# Portal Network clients such as Fluffy:
|
|
|
|
# https://github.com/ethereum/portal-network-specs?tab=readme-ov-file#the-json-rpc-api
|
2021-06-18 17:20:48 +00:00
|
|
|
|
2022-05-22 20:44:15 +00:00
|
|
|
func init*(
|
|
|
|
T: type TransactionObject,
|
2024-10-04 21:21:26 +00:00
|
|
|
tx: transactions.Transaction,
|
|
|
|
header: Header,
|
2024-02-28 17:31:45 +00:00
|
|
|
txIndex: int,
|
|
|
|
): T {.raises: [ValidationError].} =
|
2024-10-04 14:34:31 +00:00
|
|
|
let sender = tx.recoverSender().valueOr:
|
|
|
|
raise (ref ValidationError)(msg: "Invalid tx signature")
|
|
|
|
|
2022-05-22 20:44:15 +00:00
|
|
|
TransactionObject(
|
2024-10-07 18:54:48 +00:00
|
|
|
blockHash: Opt.some(header.rlpHash),
|
2024-10-04 21:21:26 +00:00
|
|
|
blockNumber: Opt.some(Quantity(header.number)),
|
2024-10-04 14:34:31 +00:00
|
|
|
`from`: sender,
|
2023-12-08 09:35:50 +00:00
|
|
|
gas: Quantity(tx.gasLimit),
|
|
|
|
gasPrice: Quantity(tx.gasPrice),
|
2024-10-07 18:54:48 +00:00
|
|
|
hash: tx.rlpHash,
|
2022-05-22 20:44:15 +00:00
|
|
|
input: tx.payload,
|
2023-12-08 09:35:50 +00:00
|
|
|
nonce: Quantity(tx.nonce),
|
2024-10-07 18:54:48 +00:00
|
|
|
to: Opt.some(tx.destination),
|
2024-06-14 07:31:08 +00:00
|
|
|
transactionIndex: Opt.some(Quantity(txIndex)),
|
2023-12-08 09:35:50 +00:00
|
|
|
value: tx.value,
|
|
|
|
v: Quantity(tx.V),
|
|
|
|
r: tx.R,
|
|
|
|
s: tx.S,
|
2024-06-14 07:31:08 +00:00
|
|
|
`type`: Opt.some(Quantity(tx.txType)),
|
|
|
|
maxFeePerGas: Opt.some(Quantity(tx.maxFeePerGas)),
|
|
|
|
maxPriorityFeePerGas: Opt.some(Quantity(tx.maxPriorityFeePerGas)),
|
2022-05-22 20:44:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Note: Similar as `populateBlockObject` from rpc_utils, but lacking the
|
|
|
|
# total difficulty
|
|
|
|
func init*(
|
2024-10-04 21:21:26 +00:00
|
|
|
T: type BlockObject, header: Header, body: BlockBody, fullTx = true, isUncle = false
|
2024-02-28 17:31:45 +00:00
|
|
|
): T {.raises: [ValidationError].} =
|
2024-10-04 21:21:26 +00:00
|
|
|
let blockHash = header.rlpHash
|
2022-02-11 13:43:10 +00:00
|
|
|
|
2022-05-22 20:44:15 +00:00
|
|
|
var blockObject = BlockObject(
|
2024-10-04 21:21:26 +00:00
|
|
|
number: Quantity(header.number),
|
2024-10-07 18:54:48 +00:00
|
|
|
hash: blockHash,
|
|
|
|
parentHash: header.parentHash,
|
2024-06-14 07:31:08 +00:00
|
|
|
nonce: Opt.some(FixedBytes[8](header.nonce)),
|
2024-10-07 18:54:48 +00:00
|
|
|
sha3Uncles: header.ommersHash,
|
2024-06-14 07:31:08 +00:00
|
|
|
logsBloom: FixedBytes[256] header.logsBloom,
|
2024-10-07 18:54:48 +00:00
|
|
|
transactionsRoot: header.txRoot,
|
|
|
|
stateRoot: header.stateRoot,
|
|
|
|
receiptsRoot: header.receiptsRoot,
|
|
|
|
miner: header.coinbase,
|
2023-12-08 09:35:50 +00:00
|
|
|
difficulty: header.difficulty,
|
|
|
|
extraData: HistoricExtraData header.extraData,
|
2022-05-22 20:44:15 +00:00
|
|
|
# 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`.
|
2023-12-08 09:35:50 +00:00
|
|
|
totalDifficulty: UInt256.low(),
|
2024-06-14 07:31:08 +00:00
|
|
|
gasLimit: Quantity(header.gasLimit),
|
|
|
|
gasUsed: Quantity(header.gasUsed),
|
|
|
|
timestamp: Quantity(header.timestamp),
|
2022-05-22 20:44:15 +00:00
|
|
|
)
|
2022-02-11 13:43:10 +00:00
|
|
|
|
2024-10-04 21:21:26 +00:00
|
|
|
# TODO: This was copied from `rpc_utils`, but the block size calculation does
|
|
|
|
# not make sense. TO FIX.
|
|
|
|
let size = sizeof(Header) - sizeof(seq[byte]) + header.extraData.len
|
2023-12-08 09:35:50 +00:00
|
|
|
blockObject.size = Quantity(size.uint)
|
2022-02-11 13:43:10 +00:00
|
|
|
|
2022-02-22 10:52:44 +00:00
|
|
|
if not isUncle:
|
2024-02-28 17:31:45 +00:00
|
|
|
blockObject.uncles = body.uncles.map(
|
2024-10-04 21:21:26 +00:00
|
|
|
proc(h: Header): Hash32 =
|
2024-10-07 18:54:48 +00:00
|
|
|
h.rlpHash
|
2024-02-28 17:31:45 +00:00
|
|
|
)
|
2022-02-22 10:52:44 +00:00
|
|
|
|
|
|
|
if fullTx:
|
|
|
|
var i = 0
|
|
|
|
for tx in body.transactions:
|
2023-12-08 09:35:50 +00:00
|
|
|
blockObject.transactions.add txOrHash(TransactionObject.init(tx, header, i))
|
2022-02-22 10:52:44 +00:00
|
|
|
inc i
|
|
|
|
else:
|
|
|
|
for tx in body.transactions:
|
2024-10-07 18:54:48 +00:00
|
|
|
blockObject.transactions.add txOrHash(rlpHash(tx))
|
2022-05-22 20:44:15 +00:00
|
|
|
|
|
|
|
blockObject
|
2022-02-22 10:52:44 +00:00
|
|
|
|
2024-09-25 14:44:46 +00:00
|
|
|
template getOrRaise(historyNetwork: Opt[HistoryNetwork]): HistoryNetwork =
|
|
|
|
let hn = historyNetwork.valueOr:
|
|
|
|
raise newException(ValueError, "history sub-network not enabled")
|
|
|
|
hn
|
|
|
|
|
|
|
|
template getOrRaise(beaconLightClient: Opt[LightClient]): LightClient =
|
|
|
|
let sn = beaconLightClient.valueOr:
|
|
|
|
raise newException(ValueError, "beacon sub-network not enabled")
|
|
|
|
sn
|
|
|
|
|
|
|
|
template getOrRaise(stateNetwork: Opt[StateNetwork]): StateNetwork =
|
|
|
|
let sn = stateNetwork.valueOr:
|
|
|
|
raise newException(ValueError, "state sub-network not enabled")
|
|
|
|
sn
|
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
proc installEthApiHandlers*(
|
2024-09-18 07:46:50 +00:00
|
|
|
rpcServer: RpcServer,
|
2024-09-25 14:44:46 +00:00
|
|
|
historyNetwork: Opt[HistoryNetwork],
|
2024-02-28 17:31:45 +00:00
|
|
|
beaconLightClient: Opt[LightClient],
|
2024-06-14 17:03:18 +00:00
|
|
|
stateNetwork: Opt[StateNetwork],
|
2024-02-28 17:31:45 +00:00
|
|
|
) =
|
2024-09-25 03:28:45 +00:00
|
|
|
rpcServer.rpc("web3_clientVersion") do() -> string:
|
|
|
|
return clientVersion
|
2022-02-11 13:43:10 +00:00
|
|
|
|
2024-09-18 07:46:50 +00:00
|
|
|
rpcServer.rpc("eth_chainId") do() -> Quantity:
|
2022-12-27 14:57:10 +00:00
|
|
|
# The Portal Network can only support MainNet at the moment, so always return
|
|
|
|
# 1
|
2023-12-08 09:35:50 +00:00
|
|
|
return Quantity(uint64(1))
|
2022-09-10 13:05:32 +00:00
|
|
|
|
2024-09-18 07:46:50 +00:00
|
|
|
rpcServer.rpc("eth_getBlockByHash") do(
|
2024-10-04 21:21:26 +00:00
|
|
|
blockHash: Hash32, fullTransactions: bool
|
2024-06-14 07:31:08 +00:00
|
|
|
) -> Opt[BlockObject]:
|
2022-02-11 13:43:10 +00:00
|
|
|
## 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
|
2024-09-25 14:44:46 +00:00
|
|
|
hn = historyNetwork.getOrRaise()
|
|
|
|
(header, body) = (await hn.getBlock(blockHash)).valueOr:
|
2024-06-14 07:31:08 +00:00
|
|
|
return Opt.none(BlockObject)
|
2022-02-22 10:52:44 +00:00
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
return Opt.some(BlockObject.init(header, body, fullTransactions))
|
2022-03-02 14:29:12 +00:00
|
|
|
|
2024-09-18 07:46:50 +00:00
|
|
|
rpcServer.rpc("eth_getBlockByNumber") do(
|
2024-04-25 13:21:09 +00:00
|
|
|
quantityTag: RtBlockIdentifier, fullTransactions: bool
|
2024-06-14 07:31:08 +00:00
|
|
|
) -> Opt[BlockObject]:
|
2024-09-25 14:44:46 +00:00
|
|
|
let hn = historyNetwork.getOrRaise()
|
|
|
|
|
2023-12-27 00:48:53 +00:00
|
|
|
if quantityTag.kind == bidAlias:
|
|
|
|
let tag = quantityTag.alias.toLowerAscii
|
|
|
|
case tag
|
|
|
|
of "latest":
|
|
|
|
# TODO:
|
|
|
|
# I assume this would refer to the content in the latest optimistic update
|
|
|
|
# in case the majority treshold is not met. And if it is met it is the
|
|
|
|
# same as the safe version?
|
|
|
|
raise newException(ValueError, "Latest tag not yet implemented")
|
|
|
|
of "earliest":
|
|
|
|
raise newException(ValueError, "Earliest tag not yet implemented")
|
|
|
|
of "safe":
|
2024-09-25 14:44:46 +00:00
|
|
|
let blc = beaconLightClient.getOrRaise()
|
2023-12-27 00:48:53 +00:00
|
|
|
|
2024-09-25 14:44:46 +00:00
|
|
|
withForkyStore(blc.store[]):
|
2023-12-27 00:48:53 +00:00
|
|
|
when lcDataFork > LightClientDataFork.Altair:
|
|
|
|
let
|
2024-10-04 21:21:26 +00:00
|
|
|
blockHash = forkyStore.optimistic_header.execution.block_hash.to(Hash32)
|
2024-09-25 14:44:46 +00:00
|
|
|
(header, body) = (await hn.getBlock(blockHash)).valueOr:
|
2024-06-14 07:31:08 +00:00
|
|
|
return Opt.none(BlockObject)
|
2023-12-27 00:48:53 +00:00
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
return Opt.some(BlockObject.init(header, body, fullTransactions))
|
2023-12-27 00:48:53 +00:00
|
|
|
else:
|
2024-02-28 17:31:45 +00:00
|
|
|
raise newException(ValueError, "Not available before Capella - not synced?")
|
2023-12-27 00:48:53 +00:00
|
|
|
of "finalized":
|
2024-09-25 14:44:46 +00:00
|
|
|
let blc = beaconLightClient.getOrRaise()
|
2023-12-27 00:48:53 +00:00
|
|
|
|
2024-09-25 14:44:46 +00:00
|
|
|
withForkyStore(blc.store[]):
|
2023-12-27 00:48:53 +00:00
|
|
|
when lcDataFork > LightClientDataFork.Altair:
|
|
|
|
let
|
2024-10-04 21:21:26 +00:00
|
|
|
blockHash = forkyStore.finalized_header.execution.block_hash.to(Hash32)
|
2024-09-25 14:44:46 +00:00
|
|
|
(header, body) = (await hn.getBlock(blockHash)).valueOr:
|
2024-06-14 07:31:08 +00:00
|
|
|
return Opt.none(BlockObject)
|
2023-12-27 00:48:53 +00:00
|
|
|
|
2024-06-14 07:31:08 +00:00
|
|
|
return Opt.some(BlockObject.init(header, body, fullTransactions))
|
2023-12-27 00:48:53 +00:00
|
|
|
else:
|
2024-02-28 17:31:45 +00:00
|
|
|
raise newException(ValueError, "Not available before Capella - not synced?")
|
2023-12-27 00:48:53 +00:00
|
|
|
of "pending":
|
|
|
|
raise newException(ValueError, "Pending tag not yet implemented")
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Unsupported block tag " & tag)
|
2023-10-09 15:49:15 +00:00
|
|
|
else:
|
|
|
|
let
|
2024-09-12 17:03:58 +00:00
|
|
|
blockNumber = quantityTag.number.uint64
|
2024-09-25 14:44:46 +00:00
|
|
|
(header, body) = (await hn.getBlock(blockNumber)).valueOr:
|
2024-09-12 17:03:58 +00:00
|
|
|
return Opt.none(BlockObject)
|
2022-08-04 06:34:53 +00:00
|
|
|
|
2024-09-12 17:03:58 +00:00
|
|
|
return Opt.some(BlockObject.init(header, body, fullTransactions))
|
2022-08-04 06:34:53 +00:00
|
|
|
|
2024-10-04 21:21:26 +00:00
|
|
|
rpcServer.rpc("eth_getBlockTransactionCountByHash") do(blockHash: Hash32) -> Quantity:
|
2022-02-22 10:52:44 +00:00
|
|
|
## 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
|
2024-09-25 14:44:46 +00:00
|
|
|
hn = historyNetwork.getOrRaise()
|
|
|
|
(_, body) = (await hn.getBlock(blockHash)).valueOr:
|
2022-12-09 16:59:36 +00:00
|
|
|
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
|
|
|
|
2023-12-08 09:35:50 +00:00
|
|
|
return Quantity(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.
|
2024-09-18 07:46:50 +00:00
|
|
|
# rpcServer.rpc("eth_getTransactionReceipt") do(
|
2024-06-14 07:31:08 +00:00
|
|
|
# data: EthHashStr) -> Opt[ReceiptObject]:
|
2022-06-29 15:44:08 +00:00
|
|
|
|
2024-09-18 07:46:50 +00:00
|
|
|
rpcServer.rpc("eth_getLogs") do(filterOptions: FilterOptions) -> seq[LogObject]:
|
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.
|
2024-09-12 17:03:58 +00:00
|
|
|
# TODO: Can impolement range queries by block number now.
|
2024-02-28 17:31:45 +00:00
|
|
|
raise newException(
|
|
|
|
ValueError,
|
|
|
|
"Unsupported query: Only `blockHash` queries are currently supported",
|
|
|
|
)
|
2022-12-09 16:59:36 +00:00
|
|
|
|
2024-09-25 14:44:46 +00:00
|
|
|
let
|
|
|
|
hn = historyNetwork.getOrRaise()
|
2024-10-07 18:54:48 +00:00
|
|
|
hash = filterOptions.blockHash.value()
|
2024-09-25 14:44:46 +00:00
|
|
|
header = (await hn.getVerifiedBlockHeader(hash)).valueOr:
|
|
|
|
raise newException(ValueError, "Could not find header with requested hash")
|
2022-12-09 16:59:36 +00:00
|
|
|
|
|
|
|
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
|
2024-09-25 14:44:46 +00:00
|
|
|
body = (await hn.getBlockBody(hash, header)).valueOr:
|
2024-02-28 17:31:45 +00:00
|
|
|
raise newException(ValueError, "Could not find block body for requested hash")
|
2024-09-25 14:44:46 +00:00
|
|
|
receipts = (await hn.getReceipts(hash, header)).valueOr:
|
2024-02-28 17:31:45 +00:00
|
|
|
raise newException(ValueError, "Could not find receipts for requested hash")
|
2022-12-09 16:59:36 +00:00
|
|
|
|
|
|
|
logs = deriveLogs(header, body.transactions, receipts)
|
2024-02-28 17:31:45 +00:00
|
|
|
filteredLogs = filterLogs(logs, filterOptions.address, filterOptions.topics)
|
2022-12-09 16:59:36 +00:00
|
|
|
|
|
|
|
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
|
2024-06-14 17:03:18 +00:00
|
|
|
return @[]
|
|
|
|
|
2024-09-18 07:46:50 +00:00
|
|
|
rpcServer.rpc("eth_getBalance") do(
|
2024-10-04 21:21:26 +00:00
|
|
|
data: Address, quantityTag: RtBlockIdentifier
|
2024-06-14 17:03:18 +00:00
|
|
|
) -> UInt256:
|
|
|
|
## Returns the balance of the account of given address.
|
|
|
|
##
|
|
|
|
## data: address to check for balance.
|
|
|
|
## quantityTag: integer block number, or the string "latest", "earliest" or "pending", see the default block parameter.
|
|
|
|
## Returns integer of the current balance in wei.
|
2024-09-13 03:46:35 +00:00
|
|
|
|
2024-06-14 17:03:18 +00:00
|
|
|
if quantityTag.kind == bidAlias:
|
|
|
|
# TODO: Implement
|
|
|
|
raise newException(ValueError, "tag not yet implemented")
|
|
|
|
|
2024-09-25 14:44:46 +00:00
|
|
|
# This endpoint requires history network to be enabled in order to look up
|
|
|
|
# the state root by block number in the call to getBalance
|
|
|
|
discard historyNetwork.getOrRaise()
|
|
|
|
|
2024-09-13 03:46:35 +00:00
|
|
|
let
|
2024-09-25 14:44:46 +00:00
|
|
|
sn = stateNetwork.getOrRaise()
|
2024-09-13 03:46:35 +00:00
|
|
|
blockNumber = quantityTag.number.uint64
|
2024-10-16 06:51:38 +00:00
|
|
|
balance = (await sn.getBalance(blockNumber, data)).valueOr:
|
2024-09-13 03:46:35 +00:00
|
|
|
raise newException(ValueError, "Unable to get balance")
|
2024-06-14 17:03:18 +00:00
|
|
|
|
2024-09-13 03:46:35 +00:00
|
|
|
return balance
|
2024-06-14 17:03:18 +00:00
|
|
|
|
2024-09-18 07:46:50 +00:00
|
|
|
rpcServer.rpc("eth_getTransactionCount") do(
|
2024-10-04 21:21:26 +00:00
|
|
|
data: Address, quantityTag: RtBlockIdentifier
|
2024-06-14 17:03:18 +00:00
|
|
|
) -> Quantity:
|
|
|
|
## Returns the number of transactions sent from an address.
|
|
|
|
##
|
|
|
|
## data: address.
|
|
|
|
## quantityTag: integer block number, or the string "latest", "earliest" or "pending", see the default block parameter.
|
|
|
|
## Returns integer of the number of transactions send from this address.
|
2024-09-13 03:46:35 +00:00
|
|
|
|
2024-06-14 17:03:18 +00:00
|
|
|
if quantityTag.kind == bidAlias:
|
|
|
|
# TODO: Implement
|
|
|
|
raise newException(ValueError, "tag not yet implemented")
|
|
|
|
|
2024-09-25 14:44:46 +00:00
|
|
|
# This endpoint requires history network to be enabled in order to look up
|
|
|
|
# the state root by block number in the call to getTransactionCount
|
|
|
|
discard historyNetwork.getOrRaise()
|
|
|
|
|
2024-09-13 03:46:35 +00:00
|
|
|
let
|
2024-09-25 14:44:46 +00:00
|
|
|
sn = stateNetwork.getOrRaise()
|
2024-09-13 03:46:35 +00:00
|
|
|
blockNumber = quantityTag.number.uint64
|
2024-10-16 06:51:38 +00:00
|
|
|
nonce = (await sn.getTransactionCount(blockNumber, data)).valueOr:
|
2024-09-13 03:46:35 +00:00
|
|
|
raise newException(ValueError, "Unable to get transaction count")
|
|
|
|
return nonce.Quantity
|
2024-06-14 17:03:18 +00:00
|
|
|
|
2024-09-18 07:46:50 +00:00
|
|
|
rpcServer.rpc("eth_getStorageAt") do(
|
2024-10-04 21:21:26 +00:00
|
|
|
data: Address, slot: UInt256, quantityTag: RtBlockIdentifier
|
2024-06-14 17:03:18 +00:00
|
|
|
) -> FixedBytes[32]:
|
|
|
|
## Returns the value from a storage position at a given address.
|
|
|
|
##
|
|
|
|
## data: address of the storage.
|
|
|
|
## slot: integer of the position in the storage.
|
|
|
|
## quantityTag: integer block number, or the string "latest", "earliest" or "pending", see the default block parameter.
|
|
|
|
## Returns: the value at this storage position.
|
2024-09-13 03:46:35 +00:00
|
|
|
|
2024-06-14 17:03:18 +00:00
|
|
|
if quantityTag.kind == bidAlias:
|
|
|
|
# TODO: Implement
|
|
|
|
raise newException(ValueError, "tag not yet implemented")
|
|
|
|
|
2024-09-25 14:44:46 +00:00
|
|
|
# This endpoint requires history network to be enabled in order to look up
|
|
|
|
# the state root by block number in the call to getStorageAt
|
|
|
|
discard historyNetwork.getOrRaise()
|
|
|
|
|
2024-09-13 03:46:35 +00:00
|
|
|
let
|
2024-09-25 14:44:46 +00:00
|
|
|
sn = stateNetwork.getOrRaise()
|
2024-09-13 03:46:35 +00:00
|
|
|
blockNumber = quantityTag.number.uint64
|
2024-10-16 06:51:38 +00:00
|
|
|
slotValue = (await sn.getStorageAt(blockNumber, data, slot)).valueOr:
|
2024-09-13 03:46:35 +00:00
|
|
|
raise newException(ValueError, "Unable to get storage slot")
|
|
|
|
return FixedBytes[32](slotValue.toBytesBE())
|
2024-06-14 17:03:18 +00:00
|
|
|
|
2024-09-18 07:46:50 +00:00
|
|
|
rpcServer.rpc("eth_getCode") do(
|
2024-10-04 21:21:26 +00:00
|
|
|
data: Address, quantityTag: RtBlockIdentifier
|
2024-06-14 17:03:18 +00:00
|
|
|
) -> seq[byte]:
|
|
|
|
## Returns code at a given address.
|
|
|
|
##
|
|
|
|
## data: address
|
|
|
|
## quantityTag: integer block number, or the string "latest", "earliest" or "pending", see the default block parameter.
|
|
|
|
## Returns the code from the given address.
|
2024-09-13 03:46:35 +00:00
|
|
|
|
2024-06-14 17:03:18 +00:00
|
|
|
if quantityTag.kind == bidAlias:
|
|
|
|
# TODO: Implement
|
|
|
|
raise newException(ValueError, "tag not yet implemented")
|
|
|
|
|
2024-09-25 14:44:46 +00:00
|
|
|
# This endpoint requires history network to be enabled in order to look up
|
|
|
|
# the state root by block number in the call to getCode
|
|
|
|
discard historyNetwork.getOrRaise()
|
|
|
|
|
2024-09-13 03:46:35 +00:00
|
|
|
let
|
2024-09-25 14:44:46 +00:00
|
|
|
sn = stateNetwork.getOrRaise()
|
2024-09-13 03:46:35 +00:00
|
|
|
blockNumber = quantityTag.number.uint64
|
2024-10-16 06:51:38 +00:00
|
|
|
bytecode = (await sn.getCode(blockNumber, data)).valueOr:
|
2024-09-13 03:46:35 +00:00
|
|
|
raise newException(ValueError, "Unable to get code")
|
|
|
|
return bytecode.asSeq()
|
|
|
|
|
2024-09-18 07:46:50 +00:00
|
|
|
rpcServer.rpc("eth_getProof") do(
|
2024-10-04 21:21:26 +00:00
|
|
|
data: Address, slots: seq[UInt256], quantityTag: RtBlockIdentifier
|
2024-09-13 03:46:35 +00:00
|
|
|
) -> ProofResponse:
|
|
|
|
## Returns information about an account and storage slots along with account
|
|
|
|
## and storage proofs which prove the existence of the values in the state.
|
|
|
|
## See spec here: https://eips.ethereum.org/EIPS/eip-1186
|
|
|
|
##
|
|
|
|
## data: address of the account.
|
|
|
|
## slots: integers of the positions in the storage to return.
|
|
|
|
## quantityTag: integer block number, or the string "latest", "earliest" or "pending", see the default block parameter.
|
|
|
|
## Returns: the proof response containing the account, account proof and storage proof
|
|
|
|
|
|
|
|
if quantityTag.kind == bidAlias:
|
|
|
|
# TODO: Implement
|
|
|
|
raise newException(ValueError, "tag not yet implemented")
|
|
|
|
|
2024-09-25 14:44:46 +00:00
|
|
|
# This endpoint requires history network to be enabled in order to look up
|
|
|
|
# the state root by block number in the call to getProof
|
|
|
|
discard historyNetwork.getOrRaise()
|
|
|
|
|
2024-09-13 03:46:35 +00:00
|
|
|
let
|
2024-09-25 14:44:46 +00:00
|
|
|
sn = stateNetwork.getOrRaise()
|
2024-09-13 03:46:35 +00:00
|
|
|
blockNumber = quantityTag.number.uint64
|
2024-10-16 06:51:38 +00:00
|
|
|
proofs = (await sn.getProofs(blockNumber, data, slots)).valueOr:
|
2024-09-13 03:46:35 +00:00
|
|
|
raise newException(ValueError, "Unable to get proofs")
|
|
|
|
|
|
|
|
var storageProof = newSeqOfCap[StorageProof](slots.len)
|
|
|
|
for i, slot in slots:
|
|
|
|
let (slotKey, slotValue) = proofs.slots[i]
|
|
|
|
storageProof.add(
|
|
|
|
StorageProof(
|
|
|
|
key: slotKey,
|
|
|
|
value: slotValue,
|
|
|
|
proof: seq[RlpEncodedBytes](proofs.slotProofs[i]),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return ProofResponse(
|
|
|
|
address: data,
|
|
|
|
accountProof: seq[RlpEncodedBytes](proofs.accountProof),
|
|
|
|
balance: proofs.account.balance,
|
2024-10-04 21:21:26 +00:00
|
|
|
nonce: Quantity(proofs.account.nonce),
|
|
|
|
codeHash: proofs.account.codeHash,
|
|
|
|
storageHash: proofs.account.storageRoot,
|
2024-09-13 03:46:35 +00:00
|
|
|
storageProof: storageProof,
|
|
|
|
)
|