nimbus-eth1/nimbus/rpc/p2p.nim

608 lines
25 KiB
Nim
Raw Normal View History

2018-06-20 17:27:32 +00:00
# Nimbus
# Copyright (c) 2018-2024 Status Research & Development GmbH
2018-06-20 17:27:32 +00:00
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
{.push raises: [].}
2018-07-31 17:18:43 +00:00
import
std/[sequtils, times, tables, typetraits],
json_rpc/rpcserver,
stint,
stew/byteutils,
json_serialization,
web3/conversions,
json_serialization/stew/results,
eth/common/eth_types_json_serialization,
eth/[rlp, p2p],
".."/[transaction, evm/state, constants],
../db/ledger,
./rpc_types, ./rpc_utils, ./oracle,
../transaction/call_evm,
2022-12-02 04:39:12 +00:00
../core/tx_pool,
2024-04-04 00:25:56 +00:00
../core/eip4844,
2022-12-02 04:39:12 +00:00
../common/[common, context],
../utils/utils,
../beacon/web3_eth_conv,
../evm/evm_errors,
./filters
type
Header = eth_types.Header
Hash32 = eth_types.Hash32
proc getProof*(
accDB: LedgerRef,
address: eth_types.Address,
slots: seq[UInt256]): ProofResponse =
let
acc = accDB.getEthAccount(address)
accExists = accDB.accountExists(address)
accountProof = accDB.getAccountProof(address)
slotProofs = accDB.getStorageProof(address, slots)
var storage = newSeqOfCap[StorageProof](slots.len)
for i, slotKey in slots:
let slotValue = accDB.getStorage(address, slotKey)
storage.add(StorageProof(
key: slotKey,
value: slotValue,
proof: seq[RlpEncodedBytes](slotProofs[i])))
if accExists:
ProofResponse(
address: address,
accountProof: seq[RlpEncodedBytes](accountProof),
balance: acc.balance,
nonce: w3Qty(acc.nonce),
codeHash: acc.codeHash,
storageHash: acc.storageRoot,
storageProof: storage)
else:
ProofResponse(
address: address,
accountProof: seq[RlpEncodedBytes](accountProof),
storageProof: storage)
proc setupEthRpc*(
2022-12-02 04:39:12 +00:00
node: EthereumNode, ctx: EthContext, com: CommonRef,
txPool: TxPoolRef, oracle: Oracle, server: RpcServer) =
2022-12-02 04:39:12 +00:00
let chainDB = com.db
proc getStateDB(header:Header): LedgerRef =
## Retrieves the account db from canonical head
# we don't use accounst_cache here because it's only read operations
LedgerRef.init(chainDB)
proc stateDBFromTag(quantityTag: BlockTag, readOnly = true): LedgerRef
{.gcsafe, raises: [CatchableError].} =
getStateDB(chainDB.headerFromTag(quantityTag))
server.rpc("eth_chainId") do() -> Web3Quantity:
return w3Qty(distinctBase(com.chainId))
server.rpc("eth_syncing") do() -> SyncingStatus:
## Returns SyncObject or false when not syncing.
if com.syncState != Waiting:
let sync = SyncObject(
startingBlock: w3Qty com.syncStart,
currentBlock : w3Qty com.syncCurrent,
highestBlock : w3Qty com.syncHighest
)
return SyncingStatus(syncing: true, syncObject: sync)
else:
return SyncingStatus(syncing: false)
server.rpc("eth_gasPrice") do() -> Web3Quantity:
## Returns an integer of the current gas price in wei.
w3Qty(calculateMedianGasPrice(chainDB).uint64)
server.rpc("eth_accounts") do() -> seq[eth_types.Address]:
## Returns a list of addresses owned by client.
result = newSeqOfCap[eth_types.Address](ctx.am.numAccounts)
for k in ctx.am.addresses:
result.add k
2018-07-31 17:18:43 +00:00
server.rpc("eth_blockNumber") do() -> Web3Quantity:
## Returns integer of the current block number the client is on.
w3Qty(chainDB.getCanonicalHead().number)
server.rpc("eth_getBalance") do(data: eth_types.Address, quantityTag: BlockTag) -> UInt256:
2018-07-31 17:18:43 +00:00
## 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.
let
accDB = stateDBFromTag(quantityTag)
address = data
accDB.getBalance(address)
server.rpc("eth_getStorageAt") do(data: eth_types.Address, slot: UInt256, quantityTag: BlockTag) -> eth_types.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.
let
accDB = stateDBFromTag(quantityTag)
address = data
data = accDB.getStorage(address, slot)
data.to(Bytes32)
server.rpc("eth_getTransactionCount") do(data: eth_types.Address, quantityTag: BlockTag) -> Web3Quantity:
## 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.
2018-08-14 15:52:30 +00:00
let
address = data
accDB = stateDBFromTag(quantityTag)
w3Qty(accDB.getNonce(address))
server.rpc("eth_getBlockTransactionCountByHash") do(data: Hash32) -> Web3Quantity:
## 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
2022-12-02 04:39:12 +00:00
header = chainDB.getBlockHeader(blockHash)
txCount = chainDB.getTransactionCount(header.txRoot)
Web3Quantity(txCount)
server.rpc("eth_getBlockTransactionCountByNumber") do(quantityTag: BlockTag) -> Web3Quantity:
## Returns the number of transactions in a block matching the given block number.
##
## data: integer of a block number, or the string "earliest", "latest" or "pending", as in the default block parameter.
## Returns integer of the number of transactions in this block.
let
2022-12-02 04:39:12 +00:00
header = chainDB.headerFromTag(quantityTag)
txCount = chainDB.getTransactionCount(header.txRoot)
Web3Quantity(txCount)
server.rpc("eth_getUncleCountByBlockHash") do(data: Hash32) -> Web3Quantity:
## Returns the number of uncles in a block from a block matching the given block hash.
##
## data: hash of a block.
## Returns integer of the number of uncles in this block.
let
blockHash = data
2022-12-02 04:39:12 +00:00
header = chainDB.getBlockHeader(blockHash)
unclesCount = chainDB.getUnclesCount(header.ommersHash)
Web3Quantity(unclesCount)
server.rpc("eth_getUncleCountByBlockNumber") do(quantityTag: BlockTag) -> Web3Quantity:
## Returns the number of uncles in a block from a block matching the given block number.
##
## quantityTag: integer of a block number, or the string "latest", "earliest" or "pending", see the default block parameter.
## Returns integer of uncles in this block.
let
2022-12-02 04:39:12 +00:00
header = chainDB.headerFromTag(quantityTag)
unclesCount = chainDB.getUnclesCount(header.ommersHash)
Web3Quantity(unclesCount)
server.rpc("eth_getCode") do(data: eth_types.Address, quantityTag: BlockTag) -> 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.
let
accDB = stateDBFromTag(quantityTag)
address = data
accDB.getCode(address).bytes()
template sign(privateKey: PrivateKey, message: string): seq[byte] =
# message length encoded as ASCII representation of decimal
let msgData = "\x19Ethereum Signed Message:\n" & $message.len & message
@(sign(privateKey, msgData.toBytes()).toRaw())
server.rpc("eth_sign") do(data: eth_types.Address, message: seq[byte]) -> seq[byte]:
## The sign method calculates an Ethereum specific signature with: sign(keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))).
## By adding a prefix to the message makes the calculated signature recognisable as an Ethereum specific signature.
## This prevents misuse where a malicious DApp can sign arbitrary data (e.g. transaction) and use the signature to impersonate the victim.
## Note the address to sign with must be unlocked.
##
## data: address.
## message: message to sign.
## Returns signature.
let
address = data
acc = ctx.am.getAccount(address).tryGet()
if not acc.unlocked:
raise newException(ValueError, "Account locked, please unlock it first")
sign(acc.privateKey, cast[string](message))
2020-07-23 15:30:42 +00:00
server.rpc("eth_signTransaction") do(data: TransactionArgs) -> seq[byte]:
## Signs a transaction that can be submitted to the network at a later time using with
## eth_sendRawTransaction
2020-07-23 15:30:42 +00:00
let
address = data.`from`.get()
acc = ctx.am.getAccount(address).tryGet()
2020-07-23 15:30:42 +00:00
if not acc.unlocked:
raise newException(ValueError, "Account locked, please unlock it first")
let
accDB = stateDBFromTag(blockId("latest"))
tx = unsignedTx(data, chainDB, accDB.getNonce(address) + 1, com.chainId)
2022-12-02 04:39:12 +00:00
eip155 = com.isEIP155(com.syncCurrent)
signedTx = signTransaction(tx, acc.privateKey, eip155)
result = rlp.encode(signedTx)
server.rpc("eth_sendTransaction") do(data: TransactionArgs) -> Hash32:
## Creates new message call transaction or a contract creation, if the data field contains code.
##
## obj: the transaction object.
## Returns the transaction hash, or the zero hash if the transaction is not yet available.
## Note: Use eth_getTransactionReceipt to get the contract address, after the transaction was mined, when you created a contract.
let
address = data.`from`.get()
acc = ctx.am.getAccount(address).tryGet()
if not acc.unlocked:
raise newException(ValueError, "Account locked, please unlock it first")
let
accDB = stateDBFromTag(blockId("latest"))
tx = unsignedTx(data, chainDB, accDB.getNonce(address) + 1, com.chainId)
2022-12-02 04:39:12 +00:00
eip155 = com.isEIP155(com.syncCurrent)
signedTx = signTransaction(tx, acc.privateKey, eip155)
networkPayload =
if signedTx.txType == TxEip4844:
if data.blobs.isNone or data.commitments.isNone or data.proofs.isNone:
raise newException(ValueError, "EIP-4844 transaction needs blobs")
if data.blobs.get.len != signedTx.versionedHashes.len:
raise newException(ValueError, "Incorrect number of blobs")
if data.commitments.get.len != signedTx.versionedHashes.len:
raise newException(ValueError, "Incorrect number of commitments")
if data.proofs.get.len != signedTx.versionedHashes.len:
raise newException(ValueError, "Incorrect number of proofs")
NetworkPayload(
blobs: data.blobs.get.mapIt it.NetworkBlob,
commitments: data.commitments.get,
proofs: data.proofs.get)
else:
if data.blobs.isSome or data.commitments.isSome or data.proofs.isSome:
raise newException(ValueError, "Blobs require EIP-4844 transaction")
nil
pooledTx = PooledTransaction(tx: signedTx, networkPayload: networkPayload)
txPool.add(pooledTx)
rlpHash(signedTx)
server.rpc("eth_sendRawTransaction") do(txBytes: seq[byte]) -> Hash32:
## Creates new message call transaction or a contract creation for signed transactions.
##
## data: the signed transaction data.
## Returns the transaction hash, or the zero hash if the transaction is not yet available.
## Note: Use eth_getTransactionReceipt to get the contract address, after the transaction was mined, when you created a contract.
2022-04-03 06:36:27 +00:00
let
pooledTx = decodePooledTx(txBytes)
txHash = rlpHash(pooledTx)
2022-04-03 06:36:27 +00:00
txPool.add(pooledTx)
let res = txPool.inPoolAndReason(txHash)
if res.isErr:
raise newException(ValueError, res.error)
txHash
2018-11-20 17:31:17 +00:00
2024-04-16 12:52:07 +00:00
server.rpc("eth_call") do(args: TransactionArgs, quantityTag: BlockTag) -> seq[byte]:
## Executes a new message call immediately without creating a transaction on the block chain.
##
## call: the transaction call object.
## quantityTag: integer block number, or the string "latest", "earliest" or "pending", see the default block parameter.
## Returns the return value of executed contract.
2020-07-29 05:42:32 +00:00
let
2022-12-02 04:39:12 +00:00
header = headerFromTag(chainDB, quantityTag)
res = rpcCallEvm(args, header, com).valueOr:
raise newException(ValueError, "rpcCallEvm error: " & $error.code)
res.output
2024-04-16 12:52:07 +00:00
server.rpc("eth_estimateGas") do(args: TransactionArgs) -> Web3Quantity:
## Generates and returns an estimate of how much gas is necessary to allow the transaction to complete.
## The transaction will not be added to the blockchain. Note that the estimate may be significantly more than
## the amount of gas actually used by the transaction, for a variety of reasons including EVM mechanics and node performance.
##
2024-04-16 12:52:07 +00:00
## args: the transaction call object.
## quantityTag: integer block number, or the string "latest", "earliest" or "pending", see the default block parameter.
## Returns the amount of gas used.
2018-11-27 18:21:28 +00:00
let
header = chainDB.headerFromTag(blockId("latest"))
# TODO: DEFAULT_RPC_GAS_CAP should configurable
gasUsed = rpcEstimateGas(args, header, com, DEFAULT_RPC_GAS_CAP).valueOr:
raise newException(ValueError, "rpcEstimateGas error: " & $error.code)
w3Qty(gasUsed)
server.rpc("eth_getBlockByHash") do(data: Hash32, fullTransactions: bool) -> 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.
2020-07-30 07:21:11 +00:00
var
header:Header
hash = data
2020-07-30 07:21:11 +00:00
2022-12-02 04:39:12 +00:00
if chainDB.getBlockHeader(hash, header):
populateBlockObject(header, chainDB, fullTransactions)
else:
nil
server.rpc("eth_getBlockByNumber") do(quantityTag: BlockTag, fullTransactions: bool) -> BlockObject:
## Returns information about a block by block number.
##
## quantityTag: integer of a block number, or the string "earliest", "latest" or "pending", as in the default block parameter.
## 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.
2020-07-30 07:21:11 +00:00
try:
2022-12-02 04:39:12 +00:00
let header = chainDB.headerFromTag(quantityTag)
populateBlockObject(header, chainDB, fullTransactions)
except CatchableError:
nil
2019-02-26 07:04:12 +00:00
server.rpc("eth_getTransactionByHash") do(data: Hash32) -> TransactionObject:
## Returns the information about a transaction requested by transaction hash.
##
## data: hash of a transaction.
## Returns requested transaction information.
let txHash = data
let res = txPool.getItem(txHash)
if res.isOk:
return populateTransactionObject(res.get().tx)
let txDetails = chainDB.getTransactionKey(txHash)
2020-07-30 07:21:11 +00:00
if txDetails.index < 0:
return nil
2020-07-30 07:21:11 +00:00
2022-12-02 04:39:12 +00:00
let header = chainDB.getBlockHeader(txDetails.blockNumber)
2020-07-30 07:21:11 +00:00
var tx: Transaction
if chainDB.getTransactionByIndex(header.txRoot, uint16(txDetails.index), tx):
result = populateTransactionObject(tx, Opt.some(header), Opt.some(txDetails.index))
2018-07-31 17:18:43 +00:00
server.rpc("eth_getTransactionByBlockHashAndIndex") do(data: Hash32, quantity: Web3Quantity) -> TransactionObject:
## Returns information about a transaction by block hash and transaction index position.
##
## data: hash of a block.
## quantity: integer of the transaction index position.
## Returns requested transaction information.
let index = uint64(quantity)
var header:Header
if not chainDB.getBlockHeader(data, header):
return nil
2020-07-30 07:21:11 +00:00
var tx: Transaction
if chainDB.getTransactionByIndex(header.txRoot, uint16(index), tx):
populateTransactionObject(tx, Opt.some(header), Opt.some(index))
else:
nil
server.rpc("eth_getTransactionByBlockNumberAndIndex") do(quantityTag: BlockTag, quantity: Web3Quantity) -> TransactionObject:
## Returns information about a transaction by block number and transaction index position.
##
## quantityTag: a block number, or the string "earliest", "latest" or "pending", as in the default block parameter.
## quantity: the transaction index position.
let
2022-12-02 04:39:12 +00:00
header = chainDB.headerFromTag(quantityTag)
index = uint64(quantity)
2018-12-10 16:27:12 +00:00
2020-07-30 07:21:11 +00:00
var tx: Transaction
if chainDB.getTransactionByIndex(header.txRoot, uint16(index), tx):
populateTransactionObject(tx, Opt.some(header), Opt.some(index))
else:
nil
2018-08-20 19:42:40 +00:00
server.rpc("eth_getTransactionReceipt") do(data: Hash32) -> ReceiptObject:
## Returns the receipt of a transaction by transaction hash.
##
## data: hash of a transaction.
## Returns transaction receipt.
2020-07-30 07:21:11 +00:00
let txDetails = chainDB.getTransactionKey(data)
2020-07-30 07:21:11 +00:00
if txDetails.index < 0:
return nil
2020-07-30 07:21:11 +00:00
2022-12-02 04:39:12 +00:00
let header = chainDB.getBlockHeader(txDetails.blockNumber)
2020-07-30 07:21:11 +00:00
var tx: Transaction
if not chainDB.getTransactionByIndex(header.txRoot, uint16(txDetails.index), tx):
return nil
2020-07-30 07:21:11 +00:00
var
idx = 0'u64
2018-11-28 15:04:57 +00:00
prevGasUsed = GasInt(0)
2018-12-31 03:27:02 +00:00
for receipt in chainDB.getReceipts(header.receiptsRoot):
2018-11-28 15:04:57 +00:00
let gasUsed = receipt.cumulativeGasUsed - prevGasUsed
prevGasUsed = receipt.cumulativeGasUsed
2018-08-20 19:42:40 +00:00
if idx == txDetails.index:
return populateReceipt(receipt, gasUsed, tx, txDetails.index, header)
2018-08-20 19:42:40 +00:00
idx.inc
server.rpc("eth_getUncleByBlockHashAndIndex") do(data: Hash32, quantity: Web3Quantity) -> BlockObject:
## Returns information about a uncle of a block by hash and uncle index position.
##
## data: hash of block.
## quantity: the uncle's index position.
## Returns BlockObject or nil when no block was found.
let index = uint64(quantity)
var header:Header
if not chainDB.getBlockHeader(data, header):
return nil
2020-07-30 07:21:11 +00:00
2022-12-02 04:39:12 +00:00
let uncles = chainDB.getUncles(header.ommersHash)
if index < 0 or index >= uncles.len.uint64:
return nil
2020-07-30 07:21:11 +00:00
result = populateBlockObject(uncles[index], chainDB, false, true)
result.totalDifficulty = chainDB.getScore(header.blockHash).valueOr(0.u256)
2020-07-30 07:21:11 +00:00
server.rpc("eth_getUncleByBlockNumberAndIndex") do(quantityTag: BlockTag, quantity: Web3Quantity) -> BlockObject:
# Returns information about a uncle of a block by number and uncle index position.
##
## quantityTag: a block number, or the string "earliest", "latest" or "pending", as in the default block parameter.
## quantity: the uncle's index position.
## Returns BlockObject or nil when no block was found.
2018-08-20 20:06:26 +00:00
let
index = uint64(quantity)
2022-12-02 04:39:12 +00:00
header = chainDB.headerFromTag(quantityTag)
uncles = chainDB.getUncles(header.ommersHash)
if index < 0 or index >= uncles.len.uint64:
return nil
2020-07-30 07:21:11 +00:00
result = populateBlockObject(uncles[index], chainDB, false, true)
result.totalDifficulty = chainDB.getScore(header.blockHash).valueOr(0.u256)
2020-07-30 07:21:11 +00:00
proc getLogsForBlock(
Unified database frontend integration (#1670) * Nimbus folder environment update details: * Integrated `CoreDbRef` for the sources in the `nimbus` sub-folder. * The `nimbus` program does not compile yet as it needs the updates in the parallel `stateless` sub-folder. * Stateless environment update details: * Integrated `CoreDbRef` for the sources in the `stateless` sub-folder. * The `nimbus` program compiles now. * Premix environment update details: * Integrated `CoreDbRef` for the sources in the `premix` sub-folder. * Fluffy environment update details: * Integrated `CoreDbRef` for the sources in the `fluffy` sub-folder. * Tools environment update details: * Integrated `CoreDbRef` for the sources in the `tools` sub-folder. * Nodocker environment update details: * Integrated `CoreDbRef` for the sources in the `hive_integration/nodocker` sub-folder. * Tests environment update details: * Integrated `CoreDbRef` for the sources in the `tests` sub-folder. * The unit tests compile and run cleanly now. * Generalise `CoreDbRef` to any `select_backend` supported database why: Generalisation was just missed due to overcoming some compiler oddity which was tied to rocksdb for testing. * Suppress compiler warning for `newChainDB()` why: Warning was added to this function which must be wrapped so that any `CatchableError` is re-raised as `Defect`. * Split off persistent `CoreDbRef` constructor into separate file why: This allows to compile a memory only database version without linking the backend library. * Use memory `CoreDbRef` database by default detail: Persistent DB constructor needs to import `db/core_db/persistent why: Most tests use memory DB anyway. This avoids linking `-lrocksdb` or any other backend by default. * fix `toLegacyBackend()` availability check why: got garbled after memory/persistent split. * Clarify raw access to MPT for snap sync handler why: Logically, `kvt` is not the raw access for the hexary trie (although this holds for the legacy database)
2023-08-04 11:10:09 +00:00
chain: CoreDbRef,
hash: Hash32,
header:Header,
opts: FilterOptions): seq[FilterLog]
{.gcsafe, raises: [RlpError,BlockNotFound].} =
if headerBloomFilter(header, opts.address, opts.topics):
let blockBody = chain.getBlockBody(hash)
let receipts = chain.getReceipts(header.receiptsRoot)
# Note: this will hit assertion error if number of block transactions
# do not match block receipts.
# Although this is fine as number of receipts should always match number
# of transactions
let logs = deriveLogs(header, blockBody.transactions, receipts)
let filteredLogs = filterLogs(logs, opts.address, opts.topics)
return filteredLogs
else:
return @[]
proc getLogsForRange(
Unified database frontend integration (#1670) * Nimbus folder environment update details: * Integrated `CoreDbRef` for the sources in the `nimbus` sub-folder. * The `nimbus` program does not compile yet as it needs the updates in the parallel `stateless` sub-folder. * Stateless environment update details: * Integrated `CoreDbRef` for the sources in the `stateless` sub-folder. * The `nimbus` program compiles now. * Premix environment update details: * Integrated `CoreDbRef` for the sources in the `premix` sub-folder. * Fluffy environment update details: * Integrated `CoreDbRef` for the sources in the `fluffy` sub-folder. * Tools environment update details: * Integrated `CoreDbRef` for the sources in the `tools` sub-folder. * Nodocker environment update details: * Integrated `CoreDbRef` for the sources in the `hive_integration/nodocker` sub-folder. * Tests environment update details: * Integrated `CoreDbRef` for the sources in the `tests` sub-folder. * The unit tests compile and run cleanly now. * Generalise `CoreDbRef` to any `select_backend` supported database why: Generalisation was just missed due to overcoming some compiler oddity which was tied to rocksdb for testing. * Suppress compiler warning for `newChainDB()` why: Warning was added to this function which must be wrapped so that any `CatchableError` is re-raised as `Defect`. * Split off persistent `CoreDbRef` constructor into separate file why: This allows to compile a memory only database version without linking the backend library. * Use memory `CoreDbRef` database by default detail: Persistent DB constructor needs to import `db/core_db/persistent why: Most tests use memory DB anyway. This avoids linking `-lrocksdb` or any other backend by default. * fix `toLegacyBackend()` availability check why: got garbled after memory/persistent split. * Clarify raw access to MPT for snap sync handler why: Logically, `kvt` is not the raw access for the hexary trie (although this holds for the legacy database)
2023-08-04 11:10:09 +00:00
chain: CoreDbRef,
start: common.BlockNumber,
finish: common.BlockNumber,
opts: FilterOptions): seq[FilterLog]
{.gcsafe, raises: [RlpError,BlockNotFound].} =
var logs = newSeq[FilterLog]()
var i = start
while i <= finish:
let res = chain.getBlockHeaderWithHash(i)
if res.isSome():
let (hash, header)= res.unsafeGet()
let filtered = chain.getLogsForBlock(header, hash, opts)
logs.add(filtered)
else:
#
return logs
i = i + 1
return logs
server.rpc("eth_getLogs") do(filterOptions: FilterOptions) -> seq[FilterLog]:
## filterOptions: settings for this filter.
## Returns a list of all logs matching a given filter object.
## TODO: Current implementation is pretty naive and not efficient
## as it requires to fetch all transactions and all receipts from database.
## Other clients (Geth):
## - Store logs related data in receipts.
## - Have separate indexes for Logs in given block
## Both of those changes require improvements to the way how we keep our data
## in Nimbus.
if filterOptions.blockHash.isSome():
let hash = filterOptions.blockHash.unsafeGet()
2022-12-02 04:39:12 +00:00
let header = chainDB.getBlockHeader(hash)
return getLogsForBlock(chainDB, hash, header, filterOptions)
else:
# TODO: do something smarter with tags. It would be the best if
# tag would be an enum (Earliest, Latest, Pending, Number), and all operations
# would operate on this enum instead of raw strings. This change would need
# to be done on every endpoint to be consistent.
2022-12-02 04:39:12 +00:00
let fromHeader = chainDB.headerFromTag(filterOptions.fromBlock)
let toHeader = chainDB.headerFromTag(filterOptions.toBlock)
# Note: if fromHeader.number > toHeader.number, no logs will be
# returned. This is consistent with, what other ethereum clients return
2022-12-02 04:39:12 +00:00
let logs = chainDB.getLogsForRange(
fromHeader.number,
toHeader.number,
filterOptions
)
return logs
server.rpc("eth_getProof") do(data: eth_types.Address, slots: seq[UInt256], quantityTag: BlockTag) -> ProofResponse:
## Returns information about an account and storage slots (if the account is a contract
## and the slots are requested) 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 with storage proofs.
## 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
let
accDB = stateDBFromTag(quantityTag)
address = data
getProof(accDB, address, slots)
server.rpc("eth_getBlockReceipts") do(quantityTag: BlockTag) -> Opt[seq[ReceiptObject]]:
2024-04-04 00:25:56 +00:00
## Returns the receipts of a block.
try:
let header = chainDB.headerFromTag(quantityTag)
var
prevGasUsed = GasInt(0)
recs: seq[ReceiptObject]
txs: seq[Transaction]
index = 0'u64
for tx in chainDB.getBlockTransactions(header):
txs.add tx
for receipt in chainDB.getReceipts(header.receiptsRoot):
let gasUsed = receipt.cumulativeGasUsed - prevGasUsed
prevGasUsed = receipt.cumulativeGasUsed
recs.add populateReceipt(receipt, gasUsed, txs[index], index, header)
inc index
return Opt.some(recs)
except CatchableError:
return Opt.none(seq[ReceiptObject])
server.rpc("eth_createAccessList") do(args: TransactionArgs, quantityTag: BlockTag) -> AccessListResult:
2024-04-04 00:25:56 +00:00
## Generates an access list for a transaction.
try:
let
header = chainDB.headerFromTag(quantityTag)
return createAccessList(header, com, args)
except CatchableError as exc:
return AccessListResult(
error: Opt.some("createAccessList error: " & exc.msg),
)
2024-04-04 00:25:56 +00:00
server.rpc("eth_blobBaseFee") do() -> Web3Quantity:
## Returns the base fee per blob gas in wei.
let header = chainDB.headerFromTag(blockId("latest"))
if header.blobGasUsed.isNone:
raise newException(ValueError, "blobGasUsed missing from latest header")
if header.excessBlobGas.isNone:
raise newException(ValueError, "excessBlobGas missing from latest header")
let blobBaseFee = getBlobBaseFee(header.excessBlobGas.get) * header.blobGasUsed.get.u256
if blobBaseFee > high(uint64).u256:
raise newException(ValueError, "blobBaseFee is bigger than uint64.max")
return w3Qty blobBaseFee.truncate(uint64)
server.rpc("eth_feeHistory") do(blockCount: Quantity,
newestBlock: BlockTag,
rewardPercentiles: Opt[seq[float64]]) -> FeeHistoryResult:
let
blocks = blockCount.uint64
percentiles = rewardPercentiles.get(newSeq[float64]())
res = feeHistory(oracle, blocks, newestBlock, percentiles)
if res.isErr:
raise newException(ValueError, res.error)
return res.get