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
|
|
|
|
2018-07-31 17:18:43 +00:00
|
|
|
import
|
2018-10-16 00:10:01 +00:00
|
|
|
strutils, times, options,
|
2018-10-05 00:20:12 +00:00
|
|
|
nimcrypto, json_rpc/rpcserver, hexstrings, stint, byteutils, ranges/typedranges,
|
|
|
|
eth_common, eth_p2p, eth_keys, eth_trie/db, rlp,
|
2018-11-20 17:30:04 +00:00
|
|
|
../utils/header, ../transaction, ../config, ../vm_state, ../constants, ../vm_types,
|
2018-12-10 16:27:12 +00:00
|
|
|
../vm_state_transactions, ../utils/addresses,
|
2019-01-06 19:19:48 +00:00
|
|
|
../db/[db_chain, state_db, storage_types],
|
2018-11-27 18:21:28 +00:00
|
|
|
rpc_types, rpc_utils, ../vm/[message, computation, interpreter_dispatch]
|
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
|
|
|
]#
|
|
|
|
|
2018-08-15 13:08:40 +00:00
|
|
|
# Work around for https://github.com/nim-lang/Nim/issues/8645
|
|
|
|
proc `%`*(value: Time): JsonNode =
|
|
|
|
result = %value.toSeconds
|
|
|
|
|
2018-12-31 03:27:02 +00:00
|
|
|
template balance(addressDb: ReadOnlyStateDb, address: EthAddress): GasInt =
|
2018-08-23 20:37:59 +00:00
|
|
|
# TODO: Account balance u256 but GasInt is int64?
|
2018-11-20 17:30:04 +00:00
|
|
|
addressDb.getBalance(address).truncate(int64)
|
2018-08-23 20:37:59 +00:00
|
|
|
|
2018-11-27 18:21:28 +00:00
|
|
|
proc binarySearchGas(vmState: var BaseVMState, transaction: Transaction, sender: EthAddress, gasPrice: GasInt, tolerance = 1): GasInt =
|
|
|
|
proc dummyComputation(vmState: var BaseVMState, transaction: Transaction, sender: EthAddress): BaseComputation =
|
|
|
|
# Note that vmState may be altered
|
|
|
|
setupComputation(
|
|
|
|
vmState,
|
|
|
|
transaction,
|
|
|
|
sender)
|
|
|
|
|
2018-11-27 18:21:28 +00:00
|
|
|
proc dummyTransaction(gasLimit, gasPrice: GasInt, destination: EthAddress, value: UInt256): Transaction =
|
|
|
|
Transaction(
|
|
|
|
accountNonce: 0.AccountNonce,
|
|
|
|
gasPrice: gasPrice,
|
|
|
|
gasLimit: gasLimit,
|
|
|
|
to: destination,
|
|
|
|
value: value
|
|
|
|
)
|
|
|
|
var
|
|
|
|
hiGas = vmState.gasLimit
|
|
|
|
loGas = transaction.intrinsicGas
|
|
|
|
gasPrice = transaction.gasPrice # TODO: Or zero?
|
2019-01-06 19:19:48 +00:00
|
|
|
|
2018-11-27 18:21:28 +00:00
|
|
|
proc tryTransaction(vmState: var BaseVMState, gasLimit: GasInt): bool =
|
|
|
|
var
|
|
|
|
spoofTransaction = dummyTransaction(gasLimit, gasPrice, transaction.to, transaction.value)
|
|
|
|
computation = vmState.dummyComputation(spoofTransaction, sender)
|
|
|
|
computation.executeOpcodes
|
|
|
|
if not computation.isError:
|
|
|
|
return true
|
|
|
|
|
|
|
|
if vmState.tryTransaction(loGas):
|
|
|
|
return loGas
|
|
|
|
if not vmState.tryTransaction(hiGas):
|
|
|
|
return 0.GasInt # TODO: Reraise error from computation
|
|
|
|
|
|
|
|
var
|
|
|
|
minVal = vmState.gasLimit
|
|
|
|
maxVal = transaction.intrinsicGas
|
|
|
|
while loGas - hiGas > tolerance:
|
|
|
|
let midPoint = (loGas + hiGas) div 2
|
|
|
|
if vmState.tryTransaction(midPoint):
|
|
|
|
minVal = midPoint
|
|
|
|
else:
|
|
|
|
maxVal = midPoint
|
|
|
|
result = minVal
|
|
|
|
|
2018-08-29 08:49:01 +00:00
|
|
|
proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) =
|
2018-10-05 00:20:12 +00:00
|
|
|
|
2018-12-31 03:27:02 +00:00
|
|
|
func getAccountDb(header: BlockHeader): ReadOnlyStateDB =
|
2018-08-29 17:12:39 +00:00
|
|
|
## Retrieves the account db from canonical head
|
|
|
|
let vmState = newBaseVMState(header, chain)
|
2018-12-31 03:27:02 +00:00
|
|
|
result = vmState.readOnlyStateDB()
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2018-12-31 03:27:02 +00:00
|
|
|
func accountDbFromTag(tag: string, readOnly = true): ReadOnlyStateDB =
|
2018-10-05 00:20:12 +00:00
|
|
|
result = getAccountDb(chain.headerFromTag(tag))
|
2018-08-29 17:12:39 +00:00
|
|
|
|
2018-08-29 08:49:01 +00:00
|
|
|
proc getBlockBody(hash: KeccakHash): BlockBody =
|
|
|
|
if not chain.getBlockBody(hash, result):
|
|
|
|
raise newException(ValueError, "Cannot find hash")
|
|
|
|
|
2018-07-22 21:34:43 +00:00
|
|
|
rpcsrv.rpc("net_version") do() -> uint:
|
2018-06-20 17:27:32 +00:00
|
|
|
let conf = getConfiguration()
|
|
|
|
result = conf.net.networkId
|
2018-10-05 00:20:12 +00:00
|
|
|
|
2018-08-13 18:29:38 +00:00
|
|
|
rpcsrv.rpc("eth_syncing") do() -> JsonNode:
|
|
|
|
## Returns SyncObject or false when not syncing.
|
|
|
|
# TODO: Requires PeerPool to check sync state.
|
|
|
|
# TODO: Use variant objects
|
|
|
|
var
|
|
|
|
res: JsonNode
|
|
|
|
sync: SyncState
|
|
|
|
if true:
|
|
|
|
# TODO: Populate sync state, this is a placeholder
|
2018-08-15 13:08:40 +00:00
|
|
|
sync.startingBlock = GENESIS_BLOCK_NUMBER
|
|
|
|
sync.currentBlock = chain.getCanonicalHead().blockNumber
|
|
|
|
sync.highestBlock = chain.getCanonicalHead().blockNumber
|
2018-08-13 18:29:38 +00:00
|
|
|
result = %sync
|
|
|
|
else:
|
|
|
|
result = newJBool(false)
|
2018-10-05 00:20:12 +00:00
|
|
|
|
2018-08-13 18:29:38 +00:00
|
|
|
rpcsrv.rpc("eth_coinbase") do() -> EthAddress:
|
|
|
|
## Returns the current coinbase address.
|
|
|
|
result = chain.getCanonicalHead().coinbase
|
|
|
|
|
|
|
|
rpcsrv.rpc("eth_mining") do() -> bool:
|
|
|
|
## Returns true if the client is mining, otherwise false.
|
|
|
|
discard
|
|
|
|
|
|
|
|
rpcsrv.rpc("eth_hashrate") do() -> int:
|
|
|
|
## Returns the number of hashes per second that the node is mining with.
|
|
|
|
discard
|
|
|
|
|
|
|
|
rpcsrv.rpc("eth_gasPrice") do() -> int64:
|
|
|
|
## Returns an integer of the current gas price in wei.
|
|
|
|
discard
|
|
|
|
|
|
|
|
rpcsrv.rpc("eth_accounts") do() -> seq[EthAddressStr]:
|
|
|
|
## Returns a list of addresses owned by client.
|
|
|
|
result = @[]
|
2018-07-31 17:18:43 +00:00
|
|
|
|
2018-08-13 18:29:38 +00:00
|
|
|
rpcsrv.rpc("eth_blockNumber") do() -> BlockNumber:
|
|
|
|
## Returns integer of the current block number the client is on.
|
|
|
|
result = chain.getCanonicalHead().blockNumber
|
|
|
|
|
2018-11-28 22:27:15 +00:00
|
|
|
rpcsrv.rpc("eth_getBalance") do(data: EthAddressStr, quantityTag: string) -> 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
|
2018-08-29 17:12:39 +00:00
|
|
|
accountDb = accountDbFromTag(quantityTag)
|
2018-11-20 17:30:04 +00:00
|
|
|
addrBytes = data.toAddress
|
2019-01-06 19:19:48 +00:00
|
|
|
balance = accountDb.getBalance(addrBytes)
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2018-11-28 22:27:15 +00:00
|
|
|
result = balance
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2018-08-15 13:08:40 +00:00
|
|
|
rpcsrv.rpc("eth_getStorageAt") do(data: EthAddressStr, quantity: int, quantityTag: string) -> UInt256:
|
2018-08-13 18:29:38 +00:00
|
|
|
## Returns the value from a storage position at a given address.
|
|
|
|
##
|
|
|
|
## data: address of the storage.
|
|
|
|
## quantity: 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
|
2018-08-29 17:12:39 +00:00
|
|
|
accountDb = accountDbFromTag(quantityTag)
|
2018-11-20 17:30:04 +00:00
|
|
|
addrBytes = data.toAddress
|
2018-08-16 17:25:17 +00:00
|
|
|
storage = accountDb.getStorage(addrBytes, quantity.u256)
|
2018-08-13 18:29:38 +00:00
|
|
|
if storage[1]:
|
2018-08-15 13:08:40 +00:00
|
|
|
result = storage[0]
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2018-08-31 17:38:51 +00:00
|
|
|
rpcsrv.rpc("eth_getTransactionCount") do(data: EthAddressStr, quantityTag: string) -> AccountNonce:
|
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
|
2018-11-20 17:30:04 +00:00
|
|
|
addrBytes = data.toAddress
|
2018-08-29 17:12:39 +00:00
|
|
|
accountDb = accountDbFromTag(quantityTag)
|
2018-08-16 17:25:17 +00:00
|
|
|
result = accountDb.getNonce(addrBytes)
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2018-11-20 17:30:04 +00:00
|
|
|
rpcsrv.rpc("eth_getBlockTransactionCountByHash") do(data: EthHashStr) -> int:
|
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.
|
2018-11-20 17:30:04 +00:00
|
|
|
var hashData = data.toHash
|
2018-08-29 08:49:01 +00:00
|
|
|
result = getBlockBody(hashData).transactions.len
|
2018-08-13 18:29:38 +00:00
|
|
|
|
|
|
|
rpcsrv.rpc("eth_getBlockTransactionCountByNumber") do(quantityTag: string) -> int:
|
|
|
|
## 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.
|
2018-08-29 08:49:01 +00:00
|
|
|
let header = chain.headerFromTag(quantityTag)
|
|
|
|
result = getBlockBody(header.hash).transactions.len
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2018-11-20 17:30:04 +00:00
|
|
|
rpcsrv.rpc("eth_getUncleCountByBlockHash") do(data: EthHashStr) -> int:
|
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.
|
2018-11-20 17:30:04 +00:00
|
|
|
var hashData = data.toHash
|
2018-08-29 08:49:01 +00:00
|
|
|
result = getBlockBody(hashData).uncles.len
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2018-08-14 15:55:57 +00:00
|
|
|
rpcsrv.rpc("eth_getUncleCountByBlockNumber") do(quantityTag: string) -> int:
|
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.
|
2018-08-29 08:49:01 +00:00
|
|
|
let header = chain.headerFromTag(quantityTag)
|
|
|
|
result = getBlockBody(header.hash).uncles.len
|
2018-08-13 18:29:38 +00:00
|
|
|
|
|
|
|
rpcsrv.rpc("eth_getCode") do(data: EthAddressStr, quantityTag: string) -> HexDataStr:
|
|
|
|
## 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
|
2018-08-29 17:12:39 +00:00
|
|
|
accountDb = accountDbFromTag(quantityTag)
|
2018-11-20 17:30:04 +00:00
|
|
|
addrBytes = toAddress(data)
|
2018-08-16 17:25:17 +00:00
|
|
|
storage = accountDb.getCode(addrBytes)
|
2018-08-15 13:08:40 +00:00
|
|
|
# Easier to return the string manually here rather than expect ByteRange to be marshalled
|
|
|
|
result = byteutils.toHex(storage.toOpenArray).HexDataStr
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2018-08-22 21:15:26 +00:00
|
|
|
template sign(privateKey: PrivateKey, message: string): string =
|
|
|
|
# TODO: Is message length encoded as bytes or characters?
|
|
|
|
let msgData = "\x19Ethereum Signed Message:\n" & $message.len & message
|
|
|
|
$signMessage(privateKey, msgData)
|
|
|
|
|
2018-08-13 18:29:38 +00:00
|
|
|
rpcsrv.rpc("eth_sign") do(data: EthAddressStr, message: HexDataStr) -> HexDataStr:
|
|
|
|
## 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.
|
2018-12-31 03:27:02 +00:00
|
|
|
let accountDb = getAccountDb(chain.getCanonicalHead())
|
2018-08-22 21:15:26 +00:00
|
|
|
var privateKey: PrivateKey # TODO: Get from key store
|
|
|
|
result = ("0x" & sign(privateKey, message.string)).HexDataStr
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2018-11-20 17:31:17 +00:00
|
|
|
proc setupTransaction(send: EthSend): Transaction =
|
|
|
|
let
|
|
|
|
source = send.source.toAddress
|
|
|
|
destination = send.to.toAddress
|
|
|
|
data = send.data.string.fromHex
|
|
|
|
contractCreation = false # TODO: Check if has code
|
|
|
|
v = 0.byte # TODO
|
|
|
|
r = 0.u256
|
|
|
|
s = 0.u256
|
|
|
|
result = initTransaction(send.nonce, send.gasPrice, send.gas, destination, send.value, data, v, r, s, contractCreation)
|
|
|
|
|
2018-08-13 18:29:38 +00:00
|
|
|
rpcsrv.rpc("eth_sendTransaction") do(obj: EthSend) -> HexDataStr:
|
|
|
|
## 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.
|
2018-11-20 17:31:17 +00:00
|
|
|
# TODO: Relies on pending pool implementation
|
2018-08-13 18:29:38 +00:00
|
|
|
discard
|
|
|
|
|
|
|
|
rpcsrv.rpc("eth_sendRawTransaction") do(data: string, quantityTag: int) -> HexDataStr:
|
|
|
|
## 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.
|
2018-11-20 17:31:17 +00:00
|
|
|
# TODO: Relies on pending pool implementation
|
2018-08-13 18:29:38 +00:00
|
|
|
discard
|
|
|
|
|
2018-11-23 17:20:20 +00:00
|
|
|
proc setupComputation(vmState: BaseVMState, blockNumber: BlockNumber,
|
|
|
|
value: UInt256, data: seq[byte],
|
|
|
|
sender, destination: EthAddress,
|
|
|
|
gasLimit, gasPrice: GasInt): BaseComputation =
|
2018-11-20 17:31:17 +00:00
|
|
|
let
|
2018-11-23 17:20:20 +00:00
|
|
|
# Handle optional defaults.
|
2018-11-20 17:31:17 +00:00
|
|
|
message = newMessage(
|
2018-11-23 17:20:20 +00:00
|
|
|
gas = gasLimit,
|
|
|
|
gasPrice = gasPrice,
|
2018-11-20 17:31:17 +00:00
|
|
|
to = destination,
|
|
|
|
sender = sender,
|
2018-11-23 17:20:20 +00:00
|
|
|
value = value,
|
|
|
|
data = data,
|
2018-11-20 17:31:17 +00:00
|
|
|
code = vmState.readOnlyStateDB.getCode(destination).toSeq,
|
|
|
|
options = newMessageOptions(origin = sender,
|
|
|
|
createAddress = destination))
|
|
|
|
|
|
|
|
result = newBaseComputation(vmState, blockNumber, message)
|
|
|
|
|
2018-08-13 18:29:38 +00:00
|
|
|
rpcsrv.rpc("eth_call") do(call: EthCall, quantityTag: string) -> HexDataStr:
|
|
|
|
## 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.
|
2018-11-20 17:31:17 +00:00
|
|
|
let header = headerFromTag(chain, quantityTag)
|
|
|
|
var
|
|
|
|
vmState = newBaseVMState(header, chain)
|
2018-11-23 18:24:42 +00:00
|
|
|
gasLimit =
|
|
|
|
if call.gas.isSome: call.gas.get
|
|
|
|
else: 0.GasInt
|
|
|
|
gasPrice =
|
|
|
|
if call.gasPrice.isSome: call.gasPrice.get
|
|
|
|
else: 0.GasInt
|
2018-12-10 16:27:12 +00:00
|
|
|
|
2018-11-26 19:40:29 +00:00
|
|
|
# Set defaults for gas limit if required
|
|
|
|
# Price remains zero by default
|
2018-11-23 18:24:42 +00:00
|
|
|
if gaslimit == 0.GasInt:
|
2018-11-26 11:28:38 +00:00
|
|
|
gasLimit = header.gasLimit
|
2018-11-23 18:24:42 +00:00
|
|
|
|
|
|
|
var
|
2018-11-23 17:20:20 +00:00
|
|
|
sender = if call.source.isSome: call.source.get.toAddress else: ZERO_ADDRESS
|
2018-11-23 18:24:42 +00:00
|
|
|
# Note that destination is a required parameter for call.
|
|
|
|
# In geth if it's zero they use the first wallet address,
|
2018-11-23 17:20:20 +00:00
|
|
|
# if no wallets, remains as ZERO_ADDRESS
|
2018-11-23 18:24:42 +00:00
|
|
|
# TODO: Wallets
|
2018-11-23 17:20:20 +00:00
|
|
|
destination = if call.to.isSome: call.to.get.toAddress else: ZERO_ADDRESS
|
|
|
|
data = if call.data.isSome: call.data.get.string.fromHex else: @[]
|
|
|
|
value = if call.value.isSome: call.value.get else: 0.u256
|
|
|
|
comp = setupComputation(vmState, header.blockNumber, value, data, sender, destination, gasLimit, gasPrice)
|
2018-12-10 16:27:12 +00:00
|
|
|
|
2018-11-20 17:31:17 +00:00
|
|
|
discard comp.execComputation
|
|
|
|
result = ("0x" & nimcrypto.toHex(comp.output)).HexDataStr
|
2019-01-06 19:19:48 +00:00
|
|
|
|
2018-08-23 20:13:05 +00:00
|
|
|
rpcsrv.rpc("eth_estimateGas") do(call: EthCall, quantityTag: string) -> GasInt:
|
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
|
|
|
var
|
|
|
|
header = chain.headerFromTag(quantityTag)
|
|
|
|
vmState = newBaseVMState(header, chain)
|
|
|
|
let
|
2018-11-29 18:30:07 +00:00
|
|
|
gasLimit = if
|
|
|
|
call.gas.isSome and call.gas.get > 0.GasInt: call.gas.get
|
|
|
|
else: header.gasLimit
|
|
|
|
gasPrice = if
|
|
|
|
call.gasPrice.isSome and call.gasPrice.get > 0: call.gasPrice.get
|
|
|
|
else: 0.GasInt
|
|
|
|
sender = if
|
|
|
|
call.source.isSome: call.source.get.toAddress
|
|
|
|
else: ZERO_ADDRESS
|
|
|
|
destination = if
|
|
|
|
call.to.isSome: call.to.get.toAddress
|
|
|
|
else: ZERO_ADDRESS
|
2019-01-06 19:19:48 +00:00
|
|
|
curState = vmState.readOnlyStateDb()
|
2018-11-27 18:21:28 +00:00
|
|
|
nonce = curState.getNonce(sender)
|
2018-11-29 18:30:07 +00:00
|
|
|
value = if
|
|
|
|
call.value.isSome: call.value.get
|
|
|
|
else: 0.u256
|
|
|
|
|
2018-11-27 18:21:28 +00:00
|
|
|
transaction = Transaction(
|
|
|
|
accountNonce: nonce,
|
|
|
|
gasPrice: gasPrice,
|
|
|
|
gasLimit: gasLimit,
|
|
|
|
to: destination,
|
2018-11-29 18:30:07 +00:00
|
|
|
value: value,
|
2018-11-27 18:21:28 +00:00
|
|
|
payload: @[]
|
|
|
|
)
|
|
|
|
result = vmState.binarySearchGas(transaction, sender, gasPrice)
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2018-08-29 08:49:01 +00:00
|
|
|
func populateBlockObject(header: BlockHeader, blockBody: BlockBody): BlockObject =
|
2018-08-28 20:45:09 +00:00
|
|
|
result.number = some(header.blockNumber)
|
|
|
|
result.hash = some(header.hash)
|
2018-08-15 13:08:40 +00:00
|
|
|
result.parentHash = header.parentHash
|
|
|
|
result.nonce = header.nonce.toUint
|
|
|
|
|
|
|
|
# Calculate hash for all uncle headers
|
|
|
|
var
|
|
|
|
rawdata = newSeq[byte](blockBody.uncles.len * 32)
|
|
|
|
startIdx = 0
|
|
|
|
for i in 0 ..< blockBody.uncles.len:
|
|
|
|
rawData[startIdx .. startIdx + 32] = blockBody.uncles[i].hash.data
|
2018-10-05 00:20:12 +00:00
|
|
|
startIdx += 32
|
2018-08-15 13:08:40 +00:00
|
|
|
result.sha3Uncles = keccak256.digest(rawData)
|
|
|
|
|
2018-08-28 20:45:09 +00:00
|
|
|
result.logsBloom = some(header.bloom)
|
2018-08-15 13:08:40 +00:00
|
|
|
result.transactionsRoot = header.txRoot
|
|
|
|
result.stateRoot = header.stateRoot
|
|
|
|
result.receiptsRoot = header.receiptRoot
|
|
|
|
result.miner = ZERO_ADDRESS # TODO: Get miner address
|
|
|
|
result.difficulty = header.difficulty
|
|
|
|
result.totalDifficulty = header.difficulty # TODO: Calculate
|
|
|
|
result.extraData = header.extraData
|
|
|
|
result.size = 0 # TODO: Calculate block size
|
|
|
|
result.gasLimit = header.gasLimit
|
|
|
|
result.gasUsed = header.gasUsed
|
|
|
|
result.timestamp = header.timeStamp
|
|
|
|
result.transactions = blockBody.transactions
|
2018-08-20 19:52:09 +00:00
|
|
|
result.uncles = @[]
|
|
|
|
for i in 0 ..< blockBody.uncles.len:
|
|
|
|
result.uncles[i] = blockBody.uncles[i].hash
|
2018-08-15 13:08:40 +00:00
|
|
|
|
2018-11-20 17:30:04 +00:00
|
|
|
rpcsrv.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.
|
2018-08-15 13:08:40 +00:00
|
|
|
let
|
2018-11-20 17:30:04 +00:00
|
|
|
h = data.toHash
|
2018-08-16 17:25:17 +00:00
|
|
|
header = chain.getBlockHeader(h)
|
2018-08-29 16:23:25 +00:00
|
|
|
result = some(populateBlockObject(header, getBlockBody(h)))
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2018-08-28 20:49:53 +00:00
|
|
|
rpcsrv.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.
|
2018-08-15 13:12:49 +00:00
|
|
|
let
|
|
|
|
header = chain.headerFromTag(quantityTag)
|
2018-08-29 16:23:25 +00:00
|
|
|
result = some(populateBlockObject(header, getBlockBody(header.hash)))
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2018-08-28 20:45:09 +00:00
|
|
|
proc populateTransactionObject(transaction: Transaction, txIndex: int64, blockHeader: BlockHeader, blockHash: Hash256): TransactionObject =
|
2018-08-23 21:05:38 +00:00
|
|
|
let
|
|
|
|
vmState = newBaseVMState(blockHeader, chain)
|
2018-12-31 03:27:02 +00:00
|
|
|
accountDb = vmState.readOnlyStateDB()
|
2018-08-23 21:45:14 +00:00
|
|
|
address = transaction.getSender()
|
2018-08-23 21:05:38 +00:00
|
|
|
txCount = accountDb.getNonce(address)
|
|
|
|
txHash = transaction.rlpHash
|
|
|
|
accountGas = accountDb.balance(address)
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2018-08-17 18:53:47 +00:00
|
|
|
result.hash = txHash
|
|
|
|
result.nonce = txCount
|
2018-08-28 20:45:09 +00:00
|
|
|
result.blockHash = some(blockHash)
|
|
|
|
result.blockNumber = some(blockHeader.blockNumber)
|
|
|
|
result.transactionIndex = some(txIndex)
|
2018-08-23 21:45:14 +00:00
|
|
|
result.source = transaction.getSender()
|
2018-08-28 20:45:09 +00:00
|
|
|
result.to = some(transaction.to)
|
2018-08-17 18:53:47 +00:00
|
|
|
result.value = transaction.value
|
|
|
|
result.gasPrice = transaction.gasPrice
|
2018-08-23 21:05:38 +00:00
|
|
|
result.gas = accountGas
|
2018-08-17 18:53:47 +00:00
|
|
|
result.input = transaction.payload
|
2018-10-05 00:20:12 +00:00
|
|
|
|
2018-11-20 17:30:04 +00:00
|
|
|
rpcsrv.rpc("eth_getTransactionByHash") do(data: EthHashStr) -> 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.
|
2018-08-17 18:53:47 +00:00
|
|
|
let
|
2018-11-20 17:30:04 +00:00
|
|
|
h = data.toHash()
|
2018-08-17 18:53:47 +00:00
|
|
|
txDetails = chain.getTransactionKey(h)
|
|
|
|
header = chain.getBlockHeader(txDetails.blockNumber)
|
|
|
|
blockHash = chain.getBlockHash(txDetails.blockNumber)
|
2018-08-29 08:49:01 +00:00
|
|
|
transaction = getBlockBody(blockHash).transactions[txDetails.index]
|
2018-08-23 21:05:38 +00:00
|
|
|
populateTransactionObject(transaction, txDetails.index, header, blockHash)
|
2018-11-29 03:19:59 +00:00
|
|
|
# TODO: if the requested transaction not in blockchain
|
|
|
|
# try to look for pending transaction in txpool
|
2018-07-31 17:18:43 +00:00
|
|
|
|
2018-11-20 17:30:04 +00:00
|
|
|
rpcsrv.rpc("eth_getTransactionByBlockHashAndIndex") do(data: EthHashStr, quantity: int) -> 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.
|
2018-08-17 19:03:30 +00:00
|
|
|
let
|
2018-11-20 17:30:04 +00:00
|
|
|
blockHash = data.toHash()
|
2018-08-17 19:03:30 +00:00
|
|
|
header = chain.getBlockHeader(blockHash)
|
2018-08-29 08:49:01 +00:00
|
|
|
transaction = getBlockBody(blockHash).transactions[quantity]
|
2018-08-23 21:05:38 +00:00
|
|
|
populateTransactionObject(transaction, quantity, header, blockHash)
|
2018-08-13 18:29:38 +00:00
|
|
|
|
|
|
|
rpcsrv.rpc("eth_getTransactionByBlockNumberAndIndex") do(quantityTag: string, quantity: int) -> 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.
|
2018-08-17 19:06:43 +00:00
|
|
|
let
|
|
|
|
header = chain.headerFromTag(quantityTag)
|
|
|
|
blockHash = header.hash
|
2018-08-29 08:49:01 +00:00
|
|
|
transaction = getBlockBody(blockHash).transactions[quantity]
|
2018-08-23 21:05:38 +00:00
|
|
|
populateTransactionObject(transaction, quantity, header, blockHash)
|
2018-08-20 19:42:40 +00:00
|
|
|
|
2018-12-10 16:27:12 +00:00
|
|
|
proc populateReceipt(receipt: Receipt, gasUsed: GasInt, tx: Transaction, txIndex: int, blockHeader: BlockHeader): ReceiptObject =
|
|
|
|
result.transactionHash = tx.rlpHash
|
2018-08-20 19:42:40 +00:00
|
|
|
result.transactionIndex = txIndex
|
|
|
|
result.blockHash = blockHeader.hash
|
|
|
|
result.blockNumber = blockHeader.blockNumber
|
2018-12-10 16:27:12 +00:00
|
|
|
result.sender = tx.getSender()
|
|
|
|
result.to = some(tx.to)
|
2018-11-28 15:04:57 +00:00
|
|
|
result.cumulativeGasUsed = receipt.cumulativeGasUsed
|
|
|
|
result.gasUsed = gasUsed
|
2018-12-10 16:27:12 +00:00
|
|
|
|
|
|
|
if tx.isContractCreation:
|
|
|
|
var sender: EthAddress
|
|
|
|
if tx.getSender(sender):
|
|
|
|
let contractAddress = generateAddress(sender, tx.accountNonce)
|
|
|
|
result.contractAddress = some(contractAddress)
|
|
|
|
else:
|
|
|
|
assert(false)
|
|
|
|
else:
|
|
|
|
result.contractAddress = none(EthAddress)
|
|
|
|
|
2018-08-22 17:45:31 +00:00
|
|
|
result.logs = receipt.logs
|
2018-12-10 16:27:12 +00:00
|
|
|
result.logsBloom = receipt.bloom
|
2018-08-20 19:42:40 +00:00
|
|
|
# post-transaction stateroot (pre Byzantium).
|
2018-11-28 15:04:57 +00:00
|
|
|
if receipt.hasStateRoot:
|
|
|
|
result.root = some(receipt.stateRoot)
|
|
|
|
else:
|
|
|
|
# 1 = success, 0 = failure.
|
|
|
|
result.status = some(receipt.status)
|
2018-08-20 19:42:40 +00:00
|
|
|
|
2018-11-20 17:30:04 +00:00
|
|
|
rpcsrv.rpc("eth_getTransactionReceipt") do(data: EthHashStr) -> 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.
|
2018-08-20 19:42:40 +00:00
|
|
|
let
|
2018-11-20 17:30:04 +00:00
|
|
|
h = data.toHash
|
2018-08-20 19:42:40 +00:00
|
|
|
txDetails = chain.getTransactionKey(h)
|
|
|
|
header = chain.getBlockHeader(txDetails.blockNumber)
|
2018-08-29 08:49:01 +00:00
|
|
|
body = getBlockBody(header.hash)
|
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
|
|
|
|
2018-12-10 16:27:12 +00:00
|
|
|
for receipt in chain.getReceipts(header):
|
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:
|
2018-11-28 15:04:57 +00:00
|
|
|
return populateReceipt(receipt, gasUsed, body.transactions[txDetails.index], txDetails.index, header)
|
2018-08-20 19:42:40 +00:00
|
|
|
idx.inc
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2018-11-20 17:30:04 +00:00
|
|
|
rpcsrv.rpc("eth_getUncleByBlockHashAndIndex") do(data: EthHashStr, quantity: int) -> 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.
|
2018-08-20 19:52:09 +00:00
|
|
|
let
|
2018-11-20 17:30:04 +00:00
|
|
|
blockHash = data.toHash()
|
2018-08-29 08:49:01 +00:00
|
|
|
body = getBlockBody(blockHash)
|
2018-08-20 20:14:30 +00:00
|
|
|
if quantity < 0 or quantity >= body.uncles.len:
|
|
|
|
raise newException(ValueError, "Uncle index out of range")
|
|
|
|
let uncle = body.uncles[quantity]
|
2018-08-28 20:49:53 +00:00
|
|
|
result = some(populateBlockObject(uncle, body))
|
2018-08-13 18:29:38 +00:00
|
|
|
|
2018-08-28 20:49:53 +00:00
|
|
|
rpcsrv.rpc("eth_getUncleByBlockNumberAndIndex") do(quantityTag: string, quantity: int) -> 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
|
|
|
|
header = chain.headerFromTag(quantityTag)
|
2018-08-29 08:49:01 +00:00
|
|
|
body = getBlockBody(header.hash)
|
2018-08-20 20:14:30 +00:00
|
|
|
if quantity < 0 or quantity >= body.uncles.len:
|
|
|
|
raise newException(ValueError, "Uncle index out of range")
|
|
|
|
let uncle = body.uncles[quantity]
|
2018-08-28 20:49:53 +00:00
|
|
|
result = some(populateBlockObject(uncle, body))
|
2018-08-13 18:29:38 +00:00
|
|
|
|
|
|
|
rpcsrv.rpc("eth_newFilter") do(filterOptions: FilterOptions) -> int:
|
|
|
|
## 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
|
|
|
|
2018-08-13 18:29:38 +00:00
|
|
|
rpcsrv.rpc("eth_newBlockFilter") do() -> int:
|
|
|
|
## 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
|
|
|
|
|
|
|
|
rpcsrv.rpc("eth_newPendingTransactionFilter") do() -> int:
|
|
|
|
## 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
|
|
|
|
|
|
|
|
rpcsrv.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
|
|
|
|
|
2018-08-22 17:45:31 +00:00
|
|
|
rpcsrv.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 = @[]
|
|
|
|
|
2018-08-22 17:45:31 +00:00
|
|
|
rpcsrv.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 = @[]
|
|
|
|
|
2018-08-22 17:45:31 +00:00
|
|
|
rpcsrv.rpc("eth_getLogs") do(filterOptions: FilterOptions) -> seq[FilterLog]:
|
2018-08-13 18:29:38 +00:00
|
|
|
## filterOptions: settings for this filter.
|
|
|
|
## Returns a list of all logs matching a given filter object.
|
|
|
|
result = @[]
|
|
|
|
|
2018-08-22 17:45:31 +00:00
|
|
|
rpcsrv.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
|
|
|
|
|
|
|
rpcsrv.rpc("eth_submitWork") do(nonce: int64, powHash: HexDataStr, mixDigest: HexDataStr) -> bool:
|
|
|
|
## 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
|
|
|
|
|
|
|
|
rpcsrv.rpc("eth_submitHashrate") do(hashRate: HexDataStr, id: HexDataStr) -> bool:
|
|
|
|
## 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.
|
|
|
|
discard
|
|
|
|
|
|
|
|
|