2018-06-20 17:27:32 +00:00
|
|
|
# Nimbus
|
2019-01-06 19:19:48 +00:00
|
|
|
# Copyright (c) 2018-2019 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.
|
2018-10-05 00:20:12 +00:00
|
|
|
|
2023-02-14 20:27:17 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2018-07-31 17:18:43 +00:00
|
|
|
import
|
2022-09-10 13:05:32 +00:00
|
|
|
std/[times, tables, typetraits],
|
2020-04-20 18:12:44 +00:00
|
|
|
json_rpc/rpcserver, hexstrings, stint, stew/byteutils,
|
2022-01-24 13:08:33 +00:00
|
|
|
json_serialization, web3/conversions, json_serialization/std/options,
|
|
|
|
eth/common/eth_types_json_serialization,
|
2022-12-02 04:39:12 +00:00
|
|
|
eth/[keys, rlp, p2p],
|
|
|
|
".."/[transaction, vm_state, constants],
|
|
|
|
../db/state_db,
|
2021-05-31 10:36:40 +00:00
|
|
|
rpc_types, rpc_utils,
|
2022-01-23 11:39:43 +00:00
|
|
|
../transaction/call_evm,
|
2022-12-02 04:39:12 +00:00
|
|
|
../core/tx_pool,
|
|
|
|
../common/[common, context],
|
|
|
|
../utils/utils,
|
2022-06-29 15:44:08 +00:00
|
|
|
./filters
|
2018-08-14 17:22:31 +00:00
|
|
|
|
|
|
|
#[
|
|
|
|
Note:
|
|
|
|
* Hexstring types (HexQuantitySt, HexDataStr, EthAddressStr, EthHashStr)
|
|
|
|
are parsed to check format before the RPC blocks are executed and will
|
|
|
|
raise an exception if invalid.
|
2018-08-15 13:08:40 +00:00
|
|
|
* Many of the RPC calls do not validate hex string types when output, only
|
|
|
|
type cast to avoid extra processing.
|
2018-08-14 17:22:31 +00:00
|
|
|
]#
|
|
|
|
|
2022-09-10 13:05:32 +00:00
|
|
|
proc setupEthRpc*(
|
2022-12-02 04:39:12 +00:00
|
|
|
node: EthereumNode, ctx: EthContext, com: CommonRef,
|
2022-09-10 13:05:32 +00:00
|
|
|
txPool: TxPoolRef, server: RpcServer) =
|
2018-10-05 00:20:12 +00:00
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
let chainDB = com.db
|
2021-10-28 09:42:39 +00:00
|
|
|
proc getStateDB(header: BlockHeader): ReadOnlyStateDB =
|
2018-08-29 17:12:39 +00:00
|
|
|
## Retrieves the account db from canonical head
|
2020-07-22 16:51:26 +00:00
|
|
|
# we don't use accounst_cache here because it's only read operations
|
2023-08-04 11:10:09 +00:00
|
|
|
let ac = newAccountStateDB(chainDB, header.stateRoot, com.pruneTrie)
|
2020-06-22 03:10:12 +00:00
|
|
|
result = ReadOnlyStateDB(ac)
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2023-01-31 01:32:17 +00:00
|
|
|
proc stateDBFromTag(tag: string, readOnly = true): ReadOnlyStateDB
|
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2022-12-02 04:39:12 +00:00
|
|
|
result = getStateDB(chainDB.headerFromTag(tag))
|
2018-08-29 17:12:39 +00:00
|
|
|
|
2021-07-23 16:31:35 +00:00
|
|
|
server.rpc("eth_protocolVersion") do() -> Option[string]:
|
2021-07-22 16:42:57 +00:00
|
|
|
# Old Ethereum wiki documents this as returning a decimal string.
|
|
|
|
# Infura documents this as returning 0x-prefixed hex string.
|
|
|
|
# Geth 1.10.0 has removed this call "as it makes no sense".
|
|
|
|
# - https://eth.wiki/json-rpc/API#eth_protocolversion
|
|
|
|
# - https://infura.io/docs/ethereum/json-rpc/eth-protocolVersion
|
|
|
|
# - https://blog.ethereum.org/2021/03/03/geth-v1-10-0/#compatibility
|
2021-07-23 16:31:35 +00:00
|
|
|
for n in node.capabilities:
|
|
|
|
if n.name == "eth":
|
|
|
|
return some($n.version)
|
|
|
|
return none(string)
|
2018-10-05 00:20:12 +00:00
|
|
|
|
2022-09-10 13:05:32 +00:00
|
|
|
server.rpc("eth_chainId") do() -> HexQuantityStr:
|
2022-12-02 04:39:12 +00:00
|
|
|
return encodeQuantity(distinctBase(com.chainId))
|
2022-09-10 13:05:32 +00:00
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_syncing") do() -> JsonNode:
|
2018-08-13 18:29:38 +00:00
|
|
|
## Returns SyncObject or false when not syncing.
|
2020-07-23 07:54:32 +00:00
|
|
|
# TODO: make sure we are not syncing
|
|
|
|
# when we reach the recent block
|
2020-07-22 16:51:26 +00:00
|
|
|
let numPeers = node.peerPool.connectedNodes.len
|
|
|
|
if numPeers > 0:
|
|
|
|
var sync = SyncState(
|
2022-12-02 04:39:12 +00:00
|
|
|
startingBlock: encodeQuantity com.syncStart,
|
|
|
|
currentBlock : encodeQuantity com.syncCurrent,
|
|
|
|
highestBlock : encodeQuantity com.syncHighest
|
2020-07-22 16:51:26 +00:00
|
|
|
)
|
2018-08-13 18:29:38 +00:00
|
|
|
result = %sync
|
|
|
|
else:
|
|
|
|
result = newJBool(false)
|
2018-10-05 00:20:12 +00:00
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_coinbase") do() -> EthAddress:
|
2018-08-13 18:29:38 +00:00
|
|
|
## Returns the current coinbase address.
|
2020-07-22 16:51:26 +00:00
|
|
|
# currently we don't have miner
|
|
|
|
result = default(EthAddress)
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_mining") do() -> bool:
|
2018-08-13 18:29:38 +00:00
|
|
|
## Returns true if the client is mining, otherwise false.
|
2020-07-22 16:51:26 +00:00
|
|
|
# currently we don't have miner
|
|
|
|
result = false
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_hashrate") do() -> HexQuantityStr:
|
2018-08-13 18:29:38 +00:00
|
|
|
## Returns the number of hashes per second that the node is mining with.
|
2020-07-22 16:51:26 +00:00
|
|
|
# currently we don't have miner
|
|
|
|
result = encodeQuantity(0.uint)
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_gasPrice") do() -> HexQuantityStr:
|
2018-08-13 18:29:38 +00:00
|
|
|
## Returns an integer of the current gas price in wei.
|
2022-12-02 04:39:12 +00:00
|
|
|
result = encodeQuantity(calculateMedianGasPrice(chainDB).uint64)
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_accounts") do() -> seq[EthAddressStr]:
|
2021-09-11 14:58:01 +00:00
|
|
|
## Returns a list of addresses owned by client.
|
2021-09-07 12:30:12 +00:00
|
|
|
result = newSeqOfCap[EthAddressStr](ctx.am.numAccounts)
|
|
|
|
for k in ctx.am.addresses:
|
2020-07-23 07:54:32 +00:00
|
|
|
result.add ethAddressStr(k)
|
2018-07-31 17:18:43 +00:00
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_blockNumber") do() -> HexQuantityStr:
|
2018-08-13 18:29:38 +00:00
|
|
|
## Returns integer of the current block number the client is on.
|
2022-12-02 04:39:12 +00:00
|
|
|
result = encodeQuantity(chainDB.getCanonicalHead().blockNumber)
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_getBalance") do(data: EthAddressStr, quantityTag: string) -> HexQuantityStr:
|
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
|
2021-10-28 09:42:39 +00:00
|
|
|
accDB = stateDBFromTag(quantityTag)
|
2020-07-22 16:51:26 +00:00
|
|
|
address = data.toAddress
|
2020-07-23 07:54:32 +00:00
|
|
|
balance = accDB.getBalance(address)
|
2020-07-22 16:51:26 +00:00
|
|
|
result = encodeQuantity(balance)
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2022-06-27 09:49:51 +00:00
|
|
|
server.rpc("eth_getStorageAt") do(data: EthAddressStr, slot: HexDataStr, quantityTag: string) -> HexDataStr:
|
2018-08-13 18:29:38 +00:00
|
|
|
## Returns the value from a storage position at a given address.
|
|
|
|
##
|
|
|
|
## data: address of the storage.
|
2022-06-27 09:49:51 +00:00
|
|
|
## slot: integer of the position in the storage.
|
2018-08-13 18:29:38 +00:00
|
|
|
## quantityTag: integer block number, or the string "latest", "earliest" or "pending", see the default block parameter.
|
|
|
|
## Returns: the value at this storage position.
|
|
|
|
let
|
2021-10-28 09:42:39 +00:00
|
|
|
accDB = stateDBFromTag(quantityTag)
|
2020-07-22 16:51:26 +00:00
|
|
|
address = data.toAddress
|
2022-06-27 09:49:51 +00:00
|
|
|
key = fromHex(UInt256, slot.string)
|
2020-07-23 07:54:32 +00:00
|
|
|
value = accDB.getStorage(address, key)[0]
|
2020-07-22 16:51:26 +00:00
|
|
|
result = hexDataStr(value)
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_getTransactionCount") do(data: EthAddressStr, quantityTag: string) -> HexQuantityStr:
|
2018-08-13 18:29:38 +00:00
|
|
|
## 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
|
2020-07-22 16:51:26 +00:00
|
|
|
address = data.toAddress
|
2021-10-28 09:42:39 +00:00
|
|
|
accDB = stateDBFromTag(quantityTag)
|
2020-07-23 07:54:32 +00:00
|
|
|
result = encodeQuantity(accDB.getNonce(address))
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_getBlockTransactionCountByHash") do(data: EthHashStr) -> HexQuantityStr:
|
2018-08-13 18:29:38 +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.
|
2020-07-22 16:51:26 +00:00
|
|
|
let
|
2020-07-23 07:54:32 +00:00
|
|
|
blockHash = data.toHash
|
2022-12-02 04:39:12 +00:00
|
|
|
header = chainDB.getBlockHeader(blockHash)
|
|
|
|
txCount = chainDB.getTransactionCount(header.txRoot)
|
2020-07-22 16:51:26 +00:00
|
|
|
result = encodeQuantity(txCount.uint)
|
2020-07-23 07:54:32 +00:00
|
|
|
|
|
|
|
server.rpc("eth_getBlockTransactionCountByNumber") do(quantityTag: string) -> HexQuantityStr:
|
2018-08-13 18:29:38 +00:00
|
|
|
## 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.
|
2020-07-23 07:54:32 +00:00
|
|
|
let
|
2022-12-02 04:39:12 +00:00
|
|
|
header = chainDB.headerFromTag(quantityTag)
|
|
|
|
txCount = chainDB.getTransactionCount(header.txRoot)
|
2020-07-23 07:54:32 +00:00
|
|
|
result = encodeQuantity(txCount.uint)
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2020-07-23 07:54:32 +00:00
|
|
|
server.rpc("eth_getUncleCountByBlockHash") do(data: EthHashStr) -> HexQuantityStr:
|
2018-08-13 18:29:38 +00:00
|
|
|
## 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.
|
2020-07-23 07:54:32 +00:00
|
|
|
let
|
|
|
|
blockHash = data.toHash
|
2022-12-02 04:39:12 +00:00
|
|
|
header = chainDB.getBlockHeader(blockHash)
|
|
|
|
unclesCount = chainDB.getUnclesCount(header.ommersHash)
|
2020-07-23 07:54:32 +00:00
|
|
|
result = encodeQuantity(unclesCount.uint)
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2020-07-23 07:54:32 +00:00
|
|
|
server.rpc("eth_getUncleCountByBlockNumber") do(quantityTag: string) -> HexQuantityStr:
|
2018-08-13 18:29:38 +00:00
|
|
|
## 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.
|
2020-07-23 07:54:32 +00:00
|
|
|
let
|
2022-12-02 04:39:12 +00:00
|
|
|
header = chainDB.headerFromTag(quantityTag)
|
|
|
|
unclesCount = chainDB.getUnclesCount(header.ommersHash)
|
2020-07-23 07:54:32 +00:00
|
|
|
result = encodeQuantity(unclesCount.uint)
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_getCode") do(data: EthAddressStr, quantityTag: string) -> HexDataStr:
|
2018-08-13 18:29:38 +00:00
|
|
|
## 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.
|
2018-08-14 17:22:31 +00:00
|
|
|
let
|
2021-10-28 09:42:39 +00:00
|
|
|
accDB = stateDBFromTag(quantityTag)
|
2020-07-23 07:54:32 +00:00
|
|
|
address = data.toAddress
|
|
|
|
storage = accDB.getCode(address)
|
|
|
|
result = hexDataStr(storage)
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2018-08-22 21:15:26 +00:00
|
|
|
template sign(privateKey: PrivateKey, message: string): string =
|
2020-07-23 07:54:32 +00:00
|
|
|
# message length encoded as ASCII representation of decimal
|
2018-08-22 21:15:26 +00:00
|
|
|
let msgData = "\x19Ethereum Signed Message:\n" & $message.len & message
|
2020-06-24 10:07:33 +00:00
|
|
|
$sign(privateKey, msgData.toBytes())
|
2018-08-22 21:15:26 +00:00
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_sign") do(data: EthAddressStr, message: HexDataStr) -> HexDataStr:
|
2018-08-13 18:29:38 +00:00
|
|
|
## 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.
|
2020-07-23 07:54:32 +00:00
|
|
|
let
|
2021-09-11 14:58:01 +00:00
|
|
|
address = data.toAddress
|
2021-09-07 12:30:12 +00:00
|
|
|
acc = ctx.am.getAccount(address).tryGet()
|
2020-07-23 07:54:32 +00:00
|
|
|
msg = hexToSeqByte(message.string)
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2020-07-23 07:54:32 +00:00
|
|
|
if not acc.unlocked:
|
|
|
|
raise newException(ValueError, "Account locked, please unlock it first")
|
|
|
|
result = ("0x" & sign(acc.privateKey, cast[string](msg))).HexDataStr
|
2020-07-23 15:30:42 +00:00
|
|
|
|
|
|
|
server.rpc("eth_signTransaction") do(data: TxSend) -> HexDataStr:
|
2020-07-24 12:44:36 +00:00
|
|
|
## 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.source.toAddress
|
2021-09-07 12:30:12 +00:00
|
|
|
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
|
2021-10-28 09:42:39 +00:00
|
|
|
accDB = stateDBFromTag("latest")
|
2022-12-02 04:39:12 +00:00
|
|
|
tx = unsignedTx(data, chainDB, accDB.getNonce(address) + 1)
|
|
|
|
eip155 = com.isEIP155(com.syncCurrent)
|
|
|
|
signedTx = signTransaction(tx, acc.privateKey, com.chainId, eip155)
|
2020-07-23 15:30:42 +00:00
|
|
|
rlpTx = rlp.encode(signedTx)
|
2021-09-11 14:58:01 +00:00
|
|
|
|
2020-07-23 15:30:42 +00:00
|
|
|
result = hexDataStr(rlpTx)
|
2020-07-24 12:44:36 +00:00
|
|
|
|
|
|
|
server.rpc("eth_sendTransaction") do(data: TxSend) -> EthHashStr:
|
2018-08-13 18:29:38 +00:00
|
|
|
## 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.
|
2020-07-24 12:44:36 +00:00
|
|
|
let
|
|
|
|
address = data.source.toAddress
|
2021-09-07 12:30:12 +00:00
|
|
|
acc = ctx.am.getAccount(address).tryGet()
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2020-07-24 12:44:36 +00:00
|
|
|
if not acc.unlocked:
|
|
|
|
raise newException(ValueError, "Account locked, please unlock it first")
|
|
|
|
|
|
|
|
let
|
2021-10-28 09:42:39 +00:00
|
|
|
accDB = stateDBFromTag("latest")
|
2022-12-02 04:39:12 +00:00
|
|
|
tx = unsignedTx(data, chainDB, accDB.getNonce(address) + 1)
|
|
|
|
eip155 = com.isEIP155(com.syncCurrent)
|
|
|
|
signedTx = signTransaction(tx, acc.privateKey, com.chainId, eip155)
|
2021-09-11 14:58:01 +00:00
|
|
|
|
2022-04-08 08:38:47 +00:00
|
|
|
txPool.add(signedTx)
|
2023-06-24 13:56:44 +00:00
|
|
|
result = rlpHash(signedTx).ethHashStr
|
2020-07-24 12:44:36 +00:00
|
|
|
|
|
|
|
server.rpc("eth_sendRawTransaction") do(data: HexDataStr) -> EthHashStr:
|
2018-08-13 18:29:38 +00:00
|
|
|
## 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
|
2023-08-21 02:10:18 +00:00
|
|
|
txBytes = hexToSeqByte(data.string)
|
2022-12-15 06:30:18 +00:00
|
|
|
signedTx = decodeTx(txBytes)
|
2023-08-21 02:10:18 +00:00
|
|
|
txHash = rlpHash(signedTx)
|
2022-04-03 06:36:27 +00:00
|
|
|
|
2022-04-08 08:38:47 +00:00
|
|
|
txPool.add(signedTx)
|
2023-10-20 08:30:05 +00:00
|
|
|
let res = txPool.inPoolAndReason(txHash)
|
|
|
|
if res.isErr:
|
|
|
|
raise newException(ValueError, res.error)
|
2023-08-21 02:10:18 +00:00
|
|
|
result = txHash.ethHashStr
|
2018-11-20 17:31:17 +00:00
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_call") do(call: EthCall, quantityTag: string) -> HexDataStr:
|
2018-08-13 18:29:38 +00:00
|
|
|
## 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)
|
2021-10-26 15:18:08 +00:00
|
|
|
callData = callData(call)
|
2022-12-02 04:39:12 +00:00
|
|
|
res = rpcCallEvm(callData, header, com)
|
2021-10-26 15:18:08 +00:00
|
|
|
result = hexDataStr(res.output)
|
2019-01-06 19:19:48 +00:00
|
|
|
|
2020-07-29 05:42:32 +00:00
|
|
|
server.rpc("eth_estimateGas") do(call: EthCall, quantityTag: string) -> HexQuantityStr:
|
2018-08-13 18:29:38 +00:00
|
|
|
## 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.
|
2018-10-05 00:20:12 +00:00
|
|
|
##
|
2018-08-13 18:29:38 +00:00
|
|
|
## call: 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
|
2022-12-02 04:39:12 +00:00
|
|
|
header = chainDB.headerFromTag(quantityTag)
|
2021-10-26 15:18:08 +00:00
|
|
|
callData = callData(call)
|
|
|
|
# TODO: DEFAULT_RPC_GAS_CAP should configurable
|
2022-12-02 04:39:12 +00:00
|
|
|
gasUsed = rpcEstimateGas(callData, header, com, DEFAULT_RPC_GAS_CAP)
|
2021-04-24 03:56:22 +00:00
|
|
|
result = encodeQuantity(gasUsed.uint64)
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_getBlockByHash") do(data: EthHashStr, fullTransactions: bool) -> Option[BlockObject]:
|
2018-08-13 18:29:38 +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.
|
2020-07-30 07:21:11 +00:00
|
|
|
var
|
|
|
|
header: BlockHeader
|
|
|
|
hash = data.toHash
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
if chainDB.getBlockHeader(hash, header):
|
|
|
|
result = some populateBlockObject(header, chainDB, fullTransactions)
|
2022-01-24 13:08:33 +00:00
|
|
|
else:
|
|
|
|
result = none BlockObject
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_getBlockByNumber") do(quantityTag: string, fullTransactions: bool) -> Option[BlockObject]:
|
2018-08-13 18:29:38 +00:00
|
|
|
## 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)
|
|
|
|
result = some(populateBlockObject(header, chainDB, fullTransactions))
|
2023-02-14 20:27:17 +00:00
|
|
|
except CatchableError:
|
2020-07-30 07:21:11 +00:00
|
|
|
result = none(BlockObject)
|
2019-02-26 07:04:12 +00:00
|
|
|
|
2020-07-30 07:21:11 +00:00
|
|
|
server.rpc("eth_getTransactionByHash") do(data: EthHashStr) -> Option[TransactionObject]:
|
2018-08-13 18:29:38 +00:00
|
|
|
## Returns the information about a transaction requested by transaction hash.
|
|
|
|
##
|
|
|
|
## data: hash of a transaction.
|
|
|
|
## Returns requested transaction information.
|
2023-10-20 08:30:05 +00:00
|
|
|
let txHash = data.toHash()
|
|
|
|
let res = txPool.getItem(txHash)
|
|
|
|
if res.isOk:
|
|
|
|
return some(populateTransactionObject(res.get().tx))
|
|
|
|
|
|
|
|
let txDetails = chainDB.getTransactionKey(txHash)
|
2020-07-30 07:21:11 +00:00
|
|
|
if txDetails.index < 0:
|
|
|
|
return none(TransactionObject)
|
|
|
|
|
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
|
2022-12-02 04:39:12 +00:00
|
|
|
if chainDB.getTransaction(header.txRoot, txDetails.index, tx):
|
2023-10-20 08:30:05 +00:00
|
|
|
result = some(populateTransactionObject(tx, some(header), some(txDetails.index)))
|
2018-07-31 17:18:43 +00:00
|
|
|
|
2020-07-30 07:21:11 +00:00
|
|
|
server.rpc("eth_getTransactionByBlockHashAndIndex") do(data: EthHashStr, quantity: HexQuantityStr) -> Option[TransactionObject]:
|
2018-08-13 18:29:38 +00:00
|
|
|
## 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.
|
2020-07-30 07:21:11 +00:00
|
|
|
let index = hexToInt(quantity.string, int)
|
|
|
|
var header: BlockHeader
|
2022-12-02 04:39:12 +00:00
|
|
|
if not chainDB.getBlockHeader(data.toHash(), header):
|
2020-07-30 07:21:11 +00:00
|
|
|
return none(TransactionObject)
|
|
|
|
|
|
|
|
var tx: Transaction
|
2022-12-02 04:39:12 +00:00
|
|
|
if chainDB.getTransaction(header.txRoot, index, tx):
|
2023-10-20 08:30:05 +00:00
|
|
|
result = some(populateTransactionObject(tx, some(header), some(index)))
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2020-07-30 07:21:11 +00:00
|
|
|
server.rpc("eth_getTransactionByBlockNumberAndIndex") do(quantityTag: string, quantity: HexQuantityStr) -> Option[TransactionObject]:
|
2018-08-13 18:29:38 +00:00
|
|
|
## 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.
|
2018-08-17 19:06:43 +00:00
|
|
|
let
|
2022-12-02 04:39:12 +00:00
|
|
|
header = chainDB.headerFromTag(quantityTag)
|
2020-07-30 07:21:11 +00:00
|
|
|
index = hexToInt(quantity.string, int)
|
2018-12-10 16:27:12 +00:00
|
|
|
|
2020-07-30 07:21:11 +00:00
|
|
|
var tx: Transaction
|
2022-12-02 04:39:12 +00:00
|
|
|
if chainDB.getTransaction(header.txRoot, index, tx):
|
2023-10-20 08:30:05 +00:00
|
|
|
result = some(populateTransactionObject(tx, some(header), some(index)))
|
2018-08-20 19:42:40 +00:00
|
|
|
|
2020-07-30 07:21:11 +00:00
|
|
|
server.rpc("eth_getTransactionReceipt") do(data: EthHashStr) -> Option[ReceiptObject]:
|
2018-08-13 18:29:38 +00:00
|
|
|
## 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
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
let txDetails = chainDB.getTransactionKey(data.toHash())
|
2020-07-30 07:21:11 +00:00
|
|
|
if txDetails.index < 0:
|
|
|
|
return none(ReceiptObject)
|
|
|
|
|
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
|
2022-12-02 04:39:12 +00:00
|
|
|
if not chainDB.getTransaction(header.txRoot, txDetails.index, tx):
|
2020-07-30 07:21:11 +00:00
|
|
|
return none(ReceiptObject)
|
|
|
|
|
2018-08-22 17:45:31 +00:00
|
|
|
var
|
|
|
|
idx = 0
|
2018-11-28 15:04:57 +00:00
|
|
|
prevGasUsed = GasInt(0)
|
2018-12-31 03:27:02 +00:00
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
for receipt in chainDB.getReceipts(header.receiptRoot):
|
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:
|
2023-08-22 06:05:10 +00:00
|
|
|
return some(populateReceipt(receipt, gasUsed, tx, txDetails.index, header))
|
2018-08-20 19:42:40 +00:00
|
|
|
idx.inc
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2020-07-30 07:21:11 +00:00
|
|
|
server.rpc("eth_getUncleByBlockHashAndIndex") do(data: EthHashStr, quantity: HexQuantityStr) -> Option[BlockObject]:
|
2018-10-05 00:20:12 +00:00
|
|
|
## Returns information about a uncle of a block by hash and uncle index position.
|
2018-08-13 18:29:38 +00:00
|
|
|
##
|
2018-08-20 19:52:09 +00:00
|
|
|
## data: hash of block.
|
2018-08-13 18:29:38 +00:00
|
|
|
## quantity: the uncle's index position.
|
|
|
|
## Returns BlockObject or nil when no block was found.
|
2020-07-30 07:21:11 +00:00
|
|
|
let index = hexToInt(quantity.string, int)
|
|
|
|
var header: BlockHeader
|
2022-12-02 04:39:12 +00:00
|
|
|
if not chainDB.getBlockHeader(data.toHash(), header):
|
2020-07-30 07:21:11 +00:00
|
|
|
return none(BlockObject)
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
let uncles = chainDB.getUncles(header.ommersHash)
|
2020-07-30 07:21:11 +00:00
|
|
|
if index < 0 or index >= uncles.len:
|
|
|
|
return none(BlockObject)
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
var uncle = populateBlockObject(uncles[index], chainDB, false, true)
|
|
|
|
uncle.totalDifficulty = encodeQuantity(chainDB.getScore(header.hash))
|
2020-07-30 07:21:11 +00:00
|
|
|
result = some(uncle)
|
|
|
|
|
|
|
|
server.rpc("eth_getUncleByBlockNumberAndIndex") do(quantityTag: string, quantity: HexQuantityStr) -> Option[BlockObject]:
|
2018-08-13 18:29:38 +00:00
|
|
|
# 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
|
2020-07-30 07:21:11 +00:00
|
|
|
index = hexToInt(quantity.string, int)
|
2022-12-02 04:39:12 +00:00
|
|
|
header = chainDB.headerFromTag(quantityTag)
|
|
|
|
uncles = chainDB.getUncles(header.ommersHash)
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2020-07-30 07:21:11 +00:00
|
|
|
if index < 0 or index >= uncles.len:
|
|
|
|
return none(BlockObject)
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
var uncle = populateBlockObject(uncles[index], chainDB, false, true)
|
|
|
|
uncle.totalDifficulty = encodeQuantity(chainDB.getScore(header.hash))
|
2020-07-30 07:21:11 +00:00
|
|
|
result = some(uncle)
|
|
|
|
|
2022-06-29 15:44:08 +00:00
|
|
|
proc getLogsForBlock(
|
2023-08-04 11:10:09 +00:00
|
|
|
chain: CoreDbRef,
|
2022-06-29 15:44:08 +00:00
|
|
|
hash: Hash256,
|
|
|
|
header: BlockHeader,
|
2023-01-31 01:32:17 +00:00
|
|
|
opts: FilterOptions): seq[FilterLog]
|
|
|
|
{.gcsafe, raises: [RlpError,ValueError].} =
|
2022-06-29 15:44:08 +00:00
|
|
|
if headerBloomFilter(header, opts.address, opts.topics):
|
|
|
|
let blockBody = chain.getBlockBody(hash)
|
|
|
|
let receipts = chain.getReceipts(header.receiptRoot)
|
|
|
|
# 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(
|
2023-08-04 11:10:09 +00:00
|
|
|
chain: CoreDbRef,
|
2022-06-29 15:44:08 +00:00
|
|
|
start: UInt256,
|
|
|
|
finish: UInt256,
|
2023-01-31 01:32:17 +00:00
|
|
|
opts: FilterOptions): seq[FilterLog]
|
|
|
|
{.gcsafe, raises: [RlpError,ValueError].} =
|
2022-06-29 15:44:08 +00:00
|
|
|
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)
|
2022-06-29 15:44:08 +00:00
|
|
|
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.fromBlock)
|
2022-06-29 15:44:08 +00:00
|
|
|
|
|
|
|
# Note: if fromHeader.blockNumber > toHeader.blockNumber, 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(
|
2022-06-29 15:44:08 +00:00
|
|
|
fromHeader.blockNumber,
|
|
|
|
toHeader.blockNumber,
|
|
|
|
filterOptions
|
|
|
|
)
|
|
|
|
return logs
|
|
|
|
|
2020-07-30 07:21:11 +00:00
|
|
|
#[
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_newFilter") do(filterOptions: FilterOptions) -> int:
|
2018-08-13 18:29:38 +00:00
|
|
|
## Creates a filter object, based on filter options, to notify when the state changes (logs).
|
|
|
|
## To check if the state has changed, call eth_getFilterChanges.
|
|
|
|
## Topics are order-dependent. A transaction with a log with topics [A, B] will be matched by the following topic filters:
|
|
|
|
## [] "anything"
|
|
|
|
## [A] "A in first position (and anything after)"
|
|
|
|
## [null, B] "anything in first position AND B in second position (and anything after)"
|
|
|
|
## [A, B] "A in first position AND B in second position (and anything after)"
|
2018-10-05 00:20:12 +00:00
|
|
|
## [[A, B], [A, B]] "(A OR B) in first position AND (A OR B) in second position (and anything after)"
|
2018-08-13 18:29:38 +00:00
|
|
|
##
|
|
|
|
## filterOptions: settings for this filter.
|
|
|
|
## Returns integer filter id.
|
|
|
|
discard
|
2018-10-05 00:20:12 +00:00
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_newBlockFilter") do() -> int:
|
2018-08-13 18:29:38 +00:00
|
|
|
## Creates a filter in the node, to notify when a new block arrives.
|
|
|
|
## To check if the state has changed, call eth_getFilterChanges.
|
|
|
|
##
|
|
|
|
## Returns integer filter id.
|
|
|
|
discard
|
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_newPendingTransactionFilter") do() -> int:
|
2018-08-13 18:29:38 +00:00
|
|
|
## Creates a filter in the node, to notify when a new block arrives.
|
|
|
|
## To check if the state has changed, call eth_getFilterChanges.
|
|
|
|
##
|
|
|
|
## Returns integer filter id.
|
|
|
|
discard
|
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_uninstallFilter") do(filterId: int) -> bool:
|
2018-10-05 00:20:12 +00:00
|
|
|
## Uninstalls a filter with given id. Should always be called when watch is no longer needed.
|
2018-08-13 18:29:38 +00:00
|
|
|
## Additonally Filters timeout when they aren't requested with eth_getFilterChanges for a period of time.
|
|
|
|
##
|
|
|
|
## filterId: The filter id.
|
|
|
|
## Returns true if the filter was successfully uninstalled, otherwise false.
|
|
|
|
discard
|
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_getFilterChanges") do(filterId: int) -> seq[FilterLog]:
|
2018-08-13 18:29:38 +00:00
|
|
|
## Polling method for a filter, which returns an list of logs which occurred since last poll.
|
|
|
|
##
|
|
|
|
## filterId: the filter id.
|
|
|
|
result = @[]
|
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_getFilterLogs") do(filterId: int) -> seq[FilterLog]:
|
2018-08-13 18:29:38 +00:00
|
|
|
## filterId: the filter id.
|
|
|
|
## Returns a list of all logs matching filter with given id.
|
|
|
|
result = @[]
|
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_getWork") do() -> array[3, UInt256]:
|
2018-08-13 18:29:38 +00:00
|
|
|
## Returns the hash of the current block, the seedHash, and the boundary condition to be met ("target").
|
|
|
|
## Returned list has the following properties:
|
|
|
|
## DATA, 32 Bytes - current block header pow-hash.
|
|
|
|
## DATA, 32 Bytes - the seed hash used for the DAG.
|
|
|
|
## DATA, 32 Bytes - the boundary condition ("target"), 2^256 / difficulty.
|
2018-08-22 17:45:31 +00:00
|
|
|
discard
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_submitWork") do(nonce: int64, powHash: HexDataStr, mixDigest: HexDataStr) -> bool:
|
2018-08-13 18:29:38 +00:00
|
|
|
## Used for submitting a proof-of-work solution.
|
|
|
|
##
|
|
|
|
## nonce: the nonce found.
|
|
|
|
## headerPow: the header's pow-hash.
|
|
|
|
## mixDigest: the mix digest.
|
|
|
|
## Returns true if the provided solution is valid, otherwise false.
|
|
|
|
discard
|
|
|
|
|
2020-07-22 16:51:26 +00:00
|
|
|
server.rpc("eth_submitHashrate") do(hashRate: HexDataStr, id: HexDataStr) -> bool:
|
2018-08-13 18:29:38 +00:00
|
|
|
## Used for submitting mining hashrate.
|
|
|
|
##
|
|
|
|
## hashRate: a hexadecimal string representation (32 bytes) of the hash rate.
|
|
|
|
## id: a random hexadecimal(32 bytes) ID identifying the client.
|
|
|
|
## Returns true if submitting went through succesfully and false otherwise.
|
2020-07-22 16:51:26 +00:00
|
|
|
discard]#
|