Merge remote-tracking branch 'origin/rpc_eth_call'
This commit is contained in:
commit
a5aad977dd
|
@ -175,38 +175,47 @@ proc `%`*(value: ref BloomFilter): JsonNode =
|
||||||
|
|
||||||
# Marshalling from JSON to Nim types that includes format checking
|
# Marshalling from JSON to Nim types that includes format checking
|
||||||
|
|
||||||
|
func invalidMsg(name: string): string = "When marshalling from JSON, parameter \"" & name & "\" is not valid"
|
||||||
|
|
||||||
proc fromJson*(n: JsonNode, argName: string, result: var HexQuantityStr) =
|
proc fromJson*(n: JsonNode, argName: string, result: var HexQuantityStr) =
|
||||||
n.kind.expect(JString, argName)
|
n.kind.expect(JString, argName)
|
||||||
let hexStr = n.getStr()
|
let hexStr = n.getStr()
|
||||||
if not hexStr.isValidHexQuantity:
|
if not hexStr.isValidHexQuantity:
|
||||||
raise newException(ValueError, "Parameter \"" & argName & "\" is not valid as an Ethereum hex quantity \"" & hexStr & "\"")
|
raise newException(ValueError, invalidMsg(argName) & " as an Ethereum hex quantity \"" & hexStr & "\"")
|
||||||
result = hexStr.hexQuantityStr
|
result = hexStr.hexQuantityStr
|
||||||
|
|
||||||
proc fromJson*(n: JsonNode, argName: string, result: var HexDataStr) =
|
proc fromJson*(n: JsonNode, argName: string, result: var HexDataStr) =
|
||||||
n.kind.expect(JString, argName)
|
n.kind.expect(JString, argName)
|
||||||
let hexStr = n.getStr()
|
let hexStr = n.getStr()
|
||||||
if not hexStr.isValidHexData:
|
if not hexStr.isValidHexData:
|
||||||
raise newException(ValueError, "Parameter \"" & argName & "\" is not valid as Ethereum data \"" & hexStr & "\"")
|
raise newException(ValueError, invalidMsg(argName) & " as Ethereum data \"" & hexStr & "\"")
|
||||||
result = hexStr.hexDataStr
|
result = hexStr.hexDataStr
|
||||||
|
|
||||||
proc fromJson*(n: JsonNode, argName: string, result: var EthAddressStr) =
|
proc fromJson*(n: JsonNode, argName: string, result: var EthAddressStr) =
|
||||||
n.kind.expect(JString, argName)
|
n.kind.expect(JString, argName)
|
||||||
let hexStr = n.getStr()
|
let hexStr = n.getStr()
|
||||||
if not hexStr.isValidEthAddress:
|
if not hexStr.isValidEthAddress:
|
||||||
raise newException(ValueError, "Parameter \"" & argName & "\" is not valid as an Ethereum address \"" & hexStr & "\"")
|
raise newException(ValueError, invalidMsg(argName) & "\" as an Ethereum address \"" & hexStr & "\"")
|
||||||
result = hexStr.EthAddressStr
|
result = hexStr.EthAddressStr
|
||||||
|
|
||||||
proc fromJson*(n: JsonNode, argName: string, result: var EthHashStr) =
|
proc fromJson*(n: JsonNode, argName: string, result: var EthHashStr) =
|
||||||
n.kind.expect(JString, argName)
|
n.kind.expect(JString, argName)
|
||||||
let hexStr = n.getStr()
|
let hexStr = n.getStr()
|
||||||
if not hexStr.isValidEthHash:
|
if not hexStr.isValidEthHash:
|
||||||
raise newException(ValueError, "Parameter \"" & argName & "\" is not valid as an Ethereum hash \"" & hexStr & "\"")
|
raise newException(ValueError, invalidMsg(argName) & " as an Ethereum hash \"" & hexStr & "\"")
|
||||||
result = hexStr.EthHashStr
|
result = hexStr.EthHashStr
|
||||||
|
|
||||||
proc fromJson*(n: JsonNode, argName: string, result: var WhisperIdentityStr) =
|
proc fromJson*(n: JsonNode, argName: string, result: var WhisperIdentityStr) =
|
||||||
n.kind.expect(JString, argName)
|
n.kind.expect(JString, argName)
|
||||||
let hexStr = n.getStr()
|
let hexStr = n.getStr()
|
||||||
if not hexStr.isValidWhisperIdentity:
|
if not hexStr.isValidWhisperIdentity:
|
||||||
raise newException(ValueError, "Parameter \"" & argName & "\" is not valid as a Whisper identity \"" & hexStr & "\"")
|
raise newException(ValueError, invalidMsg(argName) & " as a Whisper identity \"" & hexStr & "\"")
|
||||||
result = hexStr.WhisperIdentityStr
|
result = hexStr.WhisperIdentityStr
|
||||||
|
|
||||||
|
proc fromJson*(n: JsonNode, argName: string, result: var UInt256) =
|
||||||
|
n.kind.expect(JString, argName)
|
||||||
|
let hexStr = n.getStr()
|
||||||
|
if hexStr.len <= 66 and hexStr.isValidHexData:
|
||||||
|
raise newException(ValueError, invalidMsg(argName) & " as a UInt256 \"" & hexStr & "\"")
|
||||||
|
result = readUintBE[256](hexToPaddedByteArray[32](hexStr))
|
||||||
|
|
||||||
|
|
|
@ -11,9 +11,10 @@ import
|
||||||
strutils, times, options,
|
strutils, times, options,
|
||||||
nimcrypto, json_rpc/rpcserver, hexstrings, stint, byteutils, ranges/typedranges,
|
nimcrypto, json_rpc/rpcserver, hexstrings, stint, byteutils, ranges/typedranges,
|
||||||
eth_common, eth_p2p, eth_keys, eth_trie/db, rlp,
|
eth_common, eth_p2p, eth_keys, eth_trie/db, rlp,
|
||||||
../utils/header, ../transaction, ../config, ../vm_state, ../constants,
|
../utils/header, ../transaction, ../config, ../vm_state, ../constants, ../vm_types,
|
||||||
|
../vm_state_transactions,
|
||||||
../db/[db_chain, state_db, storage_types],
|
../db/[db_chain, state_db, storage_types],
|
||||||
rpc_types, rpc_utils
|
rpc_types, rpc_utils, ../vm/[message, computation]
|
||||||
|
|
||||||
#[
|
#[
|
||||||
Note:
|
Note:
|
||||||
|
@ -28,9 +29,17 @@ import
|
||||||
proc `%`*(value: Time): JsonNode =
|
proc `%`*(value: Time): JsonNode =
|
||||||
result = %value.toSeconds
|
result = %value.toSeconds
|
||||||
|
|
||||||
|
func toAddress(value: EthAddressStr): EthAddress = hexToPaddedByteArray[20](value.string)
|
||||||
|
|
||||||
|
func toHash(value: array[32, byte]): Hash256 {.inline.} =
|
||||||
|
result.data = value
|
||||||
|
|
||||||
|
func toHash(value: EthHashStr): Hash256 {.inline.} =
|
||||||
|
result = hexToPaddedByteArray[32](value.string).toHash
|
||||||
|
|
||||||
template balance(addressDb: AccountStateDb, address: EthAddress): GasInt =
|
template balance(addressDb: AccountStateDb, address: EthAddress): GasInt =
|
||||||
# TODO: Account balance u256 but GasInt is int64?
|
# TODO: Account balance u256 but GasInt is int64?
|
||||||
cast[GasInt](addressDb.get_balance(address).data.lo)
|
addressDb.getBalance(address).truncate(int64)
|
||||||
|
|
||||||
func headerFromTag(chain:BaseChainDB, blockTag: string): BlockHeader =
|
func headerFromTag(chain:BaseChainDB, blockTag: string): BlockHeader =
|
||||||
let tag = blockTag.toLowerAscii
|
let tag = blockTag.toLowerAscii
|
||||||
|
@ -104,7 +113,7 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) =
|
||||||
## Returns integer of the current block number the client is on.
|
## Returns integer of the current block number the client is on.
|
||||||
result = chain.getCanonicalHead().blockNumber
|
result = chain.getCanonicalHead().blockNumber
|
||||||
|
|
||||||
rpcsrv.rpc("eth_getBalance") do(data: EthAddressStr, quantityTag: string) -> GasInt:
|
rpcsrv.rpc("eth_getBalance") do(data: EthAddressStr, quantityTag: string) -> UInt256:
|
||||||
## Returns the balance of the account of given address.
|
## Returns the balance of the account of given address.
|
||||||
##
|
##
|
||||||
## data: address to check for balance.
|
## data: address to check for balance.
|
||||||
|
@ -112,10 +121,10 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) =
|
||||||
## Returns integer of the current balance in wei.
|
## Returns integer of the current balance in wei.
|
||||||
let
|
let
|
||||||
accountDb = accountDbFromTag(quantityTag)
|
accountDb = accountDbFromTag(quantityTag)
|
||||||
addrBytes = strToAddress(data.string)
|
addrBytes = data.toAddress
|
||||||
balance = accountDb.get_balance(addrBytes)
|
balance = accountDb.get_balance(addrBytes)
|
||||||
|
|
||||||
result = balance.toInt
|
result = balance
|
||||||
|
|
||||||
rpcsrv.rpc("eth_getStorageAt") do(data: EthAddressStr, quantity: int, quantityTag: string) -> UInt256:
|
rpcsrv.rpc("eth_getStorageAt") do(data: EthAddressStr, quantity: int, quantityTag: string) -> UInt256:
|
||||||
## Returns the value from a storage position at a given address.
|
## Returns the value from a storage position at a given address.
|
||||||
|
@ -126,7 +135,7 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) =
|
||||||
## Returns: the value at this storage position.
|
## Returns: the value at this storage position.
|
||||||
let
|
let
|
||||||
accountDb = accountDbFromTag(quantityTag)
|
accountDb = accountDbFromTag(quantityTag)
|
||||||
addrBytes = strToAddress(data.string)
|
addrBytes = data.toAddress
|
||||||
storage = accountDb.getStorage(addrBytes, quantity.u256)
|
storage = accountDb.getStorage(addrBytes, quantity.u256)
|
||||||
if storage[1]:
|
if storage[1]:
|
||||||
result = storage[0]
|
result = storage[0]
|
||||||
|
@ -138,16 +147,16 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) =
|
||||||
## quantityTag: integer block number, or the string "latest", "earliest" or "pending", see the default block parameter.
|
## 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.
|
## Returns integer of the number of transactions send from this address.
|
||||||
let
|
let
|
||||||
addrBytes = data.string.strToAddress()
|
addrBytes = data.toAddress
|
||||||
accountDb = accountDbFromTag(quantityTag)
|
accountDb = accountDbFromTag(quantityTag)
|
||||||
result = accountDb.getNonce(addrBytes)
|
result = accountDb.getNonce(addrBytes)
|
||||||
|
|
||||||
rpcsrv.rpc("eth_getBlockTransactionCountByHash") do(data: HexDataStr) -> int:
|
rpcsrv.rpc("eth_getBlockTransactionCountByHash") do(data: EthHashStr) -> int:
|
||||||
## Returns the number of transactions in a block from a block matching the given block hash.
|
## Returns the number of transactions in a block from a block matching the given block hash.
|
||||||
##
|
##
|
||||||
## data: hash of a block
|
## data: hash of a block
|
||||||
## Returns integer of the number of transactions in this block.
|
## Returns integer of the number of transactions in this block.
|
||||||
var hashData = strToHash(data.string)
|
var hashData = data.toHash
|
||||||
result = getBlockBody(hashData).transactions.len
|
result = getBlockBody(hashData).transactions.len
|
||||||
|
|
||||||
rpcsrv.rpc("eth_getBlockTransactionCountByNumber") do(quantityTag: string) -> int:
|
rpcsrv.rpc("eth_getBlockTransactionCountByNumber") do(quantityTag: string) -> int:
|
||||||
|
@ -158,12 +167,12 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) =
|
||||||
let header = chain.headerFromTag(quantityTag)
|
let header = chain.headerFromTag(quantityTag)
|
||||||
result = getBlockBody(header.hash).transactions.len
|
result = getBlockBody(header.hash).transactions.len
|
||||||
|
|
||||||
rpcsrv.rpc("eth_getUncleCountByBlockHash") do(data: HexDataStr) -> int:
|
rpcsrv.rpc("eth_getUncleCountByBlockHash") do(data: EthHashStr) -> int:
|
||||||
## Returns the number of uncles in a block from a block matching the given block hash.
|
## Returns the number of uncles in a block from a block matching the given block hash.
|
||||||
##
|
##
|
||||||
## data: hash of a block.
|
## data: hash of a block.
|
||||||
## Returns integer of the number of uncles in this block.
|
## Returns integer of the number of uncles in this block.
|
||||||
var hashData = strToHash(data.string)
|
var hashData = data.toHash
|
||||||
result = getBlockBody(hashData).uncles.len
|
result = getBlockBody(hashData).uncles.len
|
||||||
|
|
||||||
rpcsrv.rpc("eth_getUncleCountByBlockNumber") do(quantityTag: string) -> int:
|
rpcsrv.rpc("eth_getUncleCountByBlockNumber") do(quantityTag: string) -> int:
|
||||||
|
@ -182,7 +191,7 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) =
|
||||||
## Returns the code from the given address.
|
## Returns the code from the given address.
|
||||||
let
|
let
|
||||||
accountDb = accountDbFromTag(quantityTag)
|
accountDb = accountDbFromTag(quantityTag)
|
||||||
addrBytes = strToAddress(data.string)
|
addrBytes = toAddress(data)
|
||||||
storage = accountDb.getCode(addrBytes)
|
storage = accountDb.getCode(addrBytes)
|
||||||
# Easier to return the string manually here rather than expect ByteRange to be marshalled
|
# Easier to return the string manually here rather than expect ByteRange to be marshalled
|
||||||
result = byteutils.toHex(storage.toOpenArray).HexDataStr
|
result = byteutils.toHex(storage.toOpenArray).HexDataStr
|
||||||
|
@ -205,12 +214,24 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) =
|
||||||
var privateKey: PrivateKey # TODO: Get from key store
|
var privateKey: PrivateKey # TODO: Get from key store
|
||||||
result = ("0x" & sign(privateKey, message.string)).HexDataStr
|
result = ("0x" & sign(privateKey, message.string)).HexDataStr
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
rpcsrv.rpc("eth_sendTransaction") do(obj: EthSend) -> HexDataStr:
|
rpcsrv.rpc("eth_sendTransaction") do(obj: EthSend) -> HexDataStr:
|
||||||
## Creates new message call transaction or a contract creation, if the data field contains code.
|
## Creates new message call transaction or a contract creation, if the data field contains code.
|
||||||
##
|
##
|
||||||
## obj: the transaction object.
|
## obj: the transaction object.
|
||||||
## Returns the transaction hash, or the zero hash if the transaction is not yet available.
|
## 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.
|
## Note: Use eth_getTransactionReceipt to get the contract address, after the transaction was mined, when you created a contract.
|
||||||
|
# TODO: Relies on pending pool implementation
|
||||||
discard
|
discard
|
||||||
|
|
||||||
rpcsrv.rpc("eth_sendRawTransaction") do(data: string, quantityTag: int) -> HexDataStr:
|
rpcsrv.rpc("eth_sendRawTransaction") do(data: string, quantityTag: int) -> HexDataStr:
|
||||||
|
@ -219,15 +240,62 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) =
|
||||||
## data: the signed transaction data.
|
## data: the signed transaction data.
|
||||||
## Returns the transaction hash, or the zero hash if the transaction is not yet available.
|
## 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.
|
## Note: Use eth_getTransactionReceipt to get the contract address, after the transaction was mined, when you created a contract.
|
||||||
|
# TODO: Relies on pending pool implementation
|
||||||
discard
|
discard
|
||||||
|
|
||||||
|
proc setupComputation(vmState: BaseVMState, blockNumber: BlockNumber,
|
||||||
|
value: UInt256, data: seq[byte],
|
||||||
|
sender, destination: EthAddress,
|
||||||
|
gasLimit, gasPrice: GasInt): BaseComputation =
|
||||||
|
let
|
||||||
|
# Handle optional defaults.
|
||||||
|
message = newMessage(
|
||||||
|
gas = gasLimit,
|
||||||
|
gasPrice = gasPrice,
|
||||||
|
to = destination,
|
||||||
|
sender = sender,
|
||||||
|
value = value,
|
||||||
|
data = data,
|
||||||
|
code = vmState.readOnlyStateDB.getCode(destination).toSeq,
|
||||||
|
options = newMessageOptions(origin = sender,
|
||||||
|
createAddress = destination))
|
||||||
|
|
||||||
|
result = newBaseComputation(vmState, blockNumber, message)
|
||||||
|
|
||||||
rpcsrv.rpc("eth_call") do(call: EthCall, quantityTag: string) -> HexDataStr:
|
rpcsrv.rpc("eth_call") do(call: EthCall, quantityTag: string) -> HexDataStr:
|
||||||
## Executes a new message call immediately without creating a transaction on the block chain.
|
## Executes a new message call immediately without creating a transaction on the block chain.
|
||||||
##
|
##
|
||||||
## call: the transaction call object.
|
## call: the transaction call object.
|
||||||
## quantityTag: integer block number, or the string "latest", "earliest" or "pending", see the default block parameter.
|
## quantityTag: integer block number, or the string "latest", "earliest" or "pending", see the default block parameter.
|
||||||
## Returns the return value of executed contract.
|
## Returns the return value of executed contract.
|
||||||
discard
|
let header = headerFromTag(chain, quantityTag)
|
||||||
|
var
|
||||||
|
vmState = newBaseVMState(header, chain)
|
||||||
|
gasLimit =
|
||||||
|
if call.gas.isSome: call.gas.get
|
||||||
|
else: 0.GasInt
|
||||||
|
gasPrice =
|
||||||
|
if call.gasPrice.isSome: call.gasPrice.get
|
||||||
|
else: 0.GasInt
|
||||||
|
|
||||||
|
# Set defaults for gas limit if required
|
||||||
|
# Price remains zero by default
|
||||||
|
if gaslimit == 0.GasInt:
|
||||||
|
gasLimit = header.gasLimit
|
||||||
|
|
||||||
|
var
|
||||||
|
sender = if call.source.isSome: call.source.get.toAddress else: ZERO_ADDRESS
|
||||||
|
# Note that destination is a required parameter for call.
|
||||||
|
# In geth if it's zero they use the first wallet address,
|
||||||
|
# if no wallets, remains as ZERO_ADDRESS
|
||||||
|
# TODO: Wallets
|
||||||
|
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)
|
||||||
|
|
||||||
|
discard comp.execComputation
|
||||||
|
result = ("0x" & nimcrypto.toHex(comp.output)).HexDataStr
|
||||||
|
|
||||||
rpcsrv.rpc("eth_estimateGas") do(call: EthCall, quantityTag: string) -> GasInt:
|
rpcsrv.rpc("eth_estimateGas") do(call: EthCall, quantityTag: string) -> GasInt:
|
||||||
## Generates and returns an estimate of how much gas is necessary to allow the transaction to complete.
|
## Generates and returns an estimate of how much gas is necessary to allow the transaction to complete.
|
||||||
|
@ -271,14 +339,14 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) =
|
||||||
for i in 0 ..< blockBody.uncles.len:
|
for i in 0 ..< blockBody.uncles.len:
|
||||||
result.uncles[i] = blockBody.uncles[i].hash
|
result.uncles[i] = blockBody.uncles[i].hash
|
||||||
|
|
||||||
rpcsrv.rpc("eth_getBlockByHash") do(data: HexDataStr, fullTransactions: bool) -> Option[BlockObject]:
|
rpcsrv.rpc("eth_getBlockByHash") do(data: EthHashStr, fullTransactions: bool) -> Option[BlockObject]:
|
||||||
## Returns information about a block by hash.
|
## Returns information about a block by hash.
|
||||||
##
|
##
|
||||||
## data: Hash of a block.
|
## data: Hash of a block.
|
||||||
## fullTransactions: If true it returns the full transaction objects, if false only the hashes of the transactions.
|
## 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.
|
## Returns BlockObject or nil when no block was found.
|
||||||
let
|
let
|
||||||
h = data.string.strToHash
|
h = data.toHash
|
||||||
header = chain.getBlockHeader(h)
|
header = chain.getBlockHeader(h)
|
||||||
result = some(populateBlockObject(header, getBlockBody(h)))
|
result = some(populateBlockObject(header, getBlockBody(h)))
|
||||||
|
|
||||||
|
@ -313,13 +381,13 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) =
|
||||||
result.gas = accountGas
|
result.gas = accountGas
|
||||||
result.input = transaction.payload
|
result.input = transaction.payload
|
||||||
|
|
||||||
rpcsrv.rpc("eth_getTransactionByHash") do(data: HexDataStr) -> TransactionObject:
|
rpcsrv.rpc("eth_getTransactionByHash") do(data: EthHashStr) -> TransactionObject:
|
||||||
## Returns the information about a transaction requested by transaction hash.
|
## Returns the information about a transaction requested by transaction hash.
|
||||||
##
|
##
|
||||||
## data: hash of a transaction.
|
## data: hash of a transaction.
|
||||||
## Returns requested transaction information.
|
## Returns requested transaction information.
|
||||||
let
|
let
|
||||||
h = data.string.strToHash()
|
h = data.toHash()
|
||||||
txDetails = chain.getTransactionKey(h)
|
txDetails = chain.getTransactionKey(h)
|
||||||
header = chain.getBlockHeader(txDetails.blockNumber)
|
header = chain.getBlockHeader(txDetails.blockNumber)
|
||||||
blockHash = chain.getBlockHash(txDetails.blockNumber)
|
blockHash = chain.getBlockHash(txDetails.blockNumber)
|
||||||
|
@ -328,14 +396,14 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) =
|
||||||
# TODO: if the requested transaction not in blockchain
|
# TODO: if the requested transaction not in blockchain
|
||||||
# try to look for pending transaction in txpool
|
# try to look for pending transaction in txpool
|
||||||
|
|
||||||
rpcsrv.rpc("eth_getTransactionByBlockHashAndIndex") do(data: HexDataStr, quantity: int) -> TransactionObject:
|
rpcsrv.rpc("eth_getTransactionByBlockHashAndIndex") do(data: EthHashStr, quantity: int) -> TransactionObject:
|
||||||
## Returns information about a transaction by block hash and transaction index position.
|
## Returns information about a transaction by block hash and transaction index position.
|
||||||
##
|
##
|
||||||
## data: hash of a block.
|
## data: hash of a block.
|
||||||
## quantity: integer of the transaction index position.
|
## quantity: integer of the transaction index position.
|
||||||
## Returns requested transaction information.
|
## Returns requested transaction information.
|
||||||
let
|
let
|
||||||
blockHash = data.string.strToHash()
|
blockHash = data.toHash()
|
||||||
header = chain.getBlockHeader(blockHash)
|
header = chain.getBlockHeader(blockHash)
|
||||||
transaction = getBlockBody(blockHash).transactions[quantity]
|
transaction = getBlockBody(blockHash).transactions[quantity]
|
||||||
populateTransactionObject(transaction, quantity, header, blockHash)
|
populateTransactionObject(transaction, quantity, header, blockHash)
|
||||||
|
@ -371,13 +439,13 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) =
|
||||||
# 1 = success, 0 = failure.
|
# 1 = success, 0 = failure.
|
||||||
result.status = some(receipt.status)
|
result.status = some(receipt.status)
|
||||||
|
|
||||||
rpcsrv.rpc("eth_getTransactionReceipt") do(data: HexDataStr) -> ReceiptObject:
|
rpcsrv.rpc("eth_getTransactionReceipt") do(data: EthHashStr) -> ReceiptObject:
|
||||||
## Returns the receipt of a transaction by transaction hash.
|
## Returns the receipt of a transaction by transaction hash.
|
||||||
##
|
##
|
||||||
## data: hash of a transaction.
|
## data: hash of a transaction.
|
||||||
## Returns transaction receipt.
|
## Returns transaction receipt.
|
||||||
let
|
let
|
||||||
h = data.string.strToHash()
|
h = data.toHash
|
||||||
txDetails = chain.getTransactionKey(h)
|
txDetails = chain.getTransactionKey(h)
|
||||||
header = chain.getBlockHeader(txDetails.blockNumber)
|
header = chain.getBlockHeader(txDetails.blockNumber)
|
||||||
body = getBlockBody(header.hash)
|
body = getBlockBody(header.hash)
|
||||||
|
@ -391,14 +459,14 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) =
|
||||||
return populateReceipt(receipt, gasUsed, body.transactions[txDetails.index], txDetails.index, header)
|
return populateReceipt(receipt, gasUsed, body.transactions[txDetails.index], txDetails.index, header)
|
||||||
idx.inc
|
idx.inc
|
||||||
|
|
||||||
rpcsrv.rpc("eth_getUncleByBlockHashAndIndex") do(data: HexDataStr, quantity: int) -> Option[BlockObject]:
|
rpcsrv.rpc("eth_getUncleByBlockHashAndIndex") do(data: EthHashStr, quantity: int) -> Option[BlockObject]:
|
||||||
## Returns information about a uncle of a block by hash and uncle index position.
|
## Returns information about a uncle of a block by hash and uncle index position.
|
||||||
##
|
##
|
||||||
## data: hash of block.
|
## data: hash of block.
|
||||||
## quantity: the uncle's index position.
|
## quantity: the uncle's index position.
|
||||||
## Returns BlockObject or nil when no block was found.
|
## Returns BlockObject or nil when no block was found.
|
||||||
let
|
let
|
||||||
blockHash = data.string.strToHash()
|
blockHash = data.toHash()
|
||||||
body = getBlockBody(blockHash)
|
body = getBlockBody(blockHash)
|
||||||
if quantity < 0 or quantity >= body.uncles.len:
|
if quantity < 0 or quantity >= body.uncles.len:
|
||||||
raise newException(ValueError, "Uncle index out of range")
|
raise newException(ValueError, "Uncle index out of range")
|
||||||
|
|
|
@ -24,18 +24,18 @@ type
|
||||||
to*: EthAddressStr # (optional when creating new contract) the address the transaction is directed to.
|
to*: EthAddressStr # (optional when creating new contract) the address the transaction is directed to.
|
||||||
gas*: GasInt # (optional, default: 90000) integer of the gas provided for the transaction execution. It will return unused gas.
|
gas*: GasInt # (optional, default: 90000) integer of the gas provided for the transaction execution. It will return unused gas.
|
||||||
gasPrice*: GasInt # (optional, default: To-Be-Determined) integer of the gasPrice used for each paid gas.
|
gasPrice*: GasInt # (optional, default: To-Be-Determined) integer of the gasPrice used for each paid gas.
|
||||||
value*: int # (optional) integer of the value sent with this transaction.
|
value*: UInt256 # (optional) integer of the value sent with this transaction.
|
||||||
data*: EthHashStr # TODO: Support more data. The compiled code of a contract OR the hash of the invoked method signature and encoded parameters. For details see Ethereum Contract ABI.
|
data*: EthHashStr # TODO: Support more data. The compiled code of a contract OR the hash of the invoked method signature and encoded parameters. For details see Ethereum Contract ABI.
|
||||||
nonce*: int # (optional) integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce
|
nonce*: AccountNonce # (optional) integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce
|
||||||
|
|
||||||
EthCall* = object
|
EthCall* = object
|
||||||
# Parameter from user
|
# Parameter from user
|
||||||
source*: EthAddressStr # (optional) The address the transaction is send from.
|
source*: Option[EthAddressStr] # (optional) The address the transaction is send from.
|
||||||
to*: EthAddressStr # The address the transaction is directed to.
|
to*: Option[EthAddressStr] # (optional in eth_estimateGas, not in eth_call) The address the transaction is directed to.
|
||||||
gas*: GasInt # (optional) Integer of the gas provided for the transaction execution. eth_call consumes zero gas, but this parameter may be needed by some executions.
|
gas*: Option[GasInt] # (optional) Integer of the gas provided for the transaction execution. eth_call consumes zero gas, but this parameter may be needed by some executions.
|
||||||
gasPrice*: GasInt # (optional) Integer of the gasPrice used for each paid gas.
|
gasPrice*: Option[GasInt] # (optional) Integer of the gasPrice used for each paid gas.
|
||||||
value*: int # (optional) Integer of the value sent with this transaction.
|
value*: Option[UInt256] # (optional) Integer of the value sent with this transaction.
|
||||||
data*: EthHashStr # (optional) Hash of the method signature and encoded parameters. For details see Ethereum Contract ABI.
|
data*: Option[EthHashStr] # (optional) Hash of the method signature and encoded parameters. For details see Ethereum Contract ABI.
|
||||||
|
|
||||||
## A block object, or null when no block was found
|
## A block object, or null when no block was found
|
||||||
## Note that this includes slightly different information from eth_common.BlockHeader
|
## Note that this includes slightly different information from eth_common.BlockHeader
|
||||||
|
|
|
@ -8,6 +8,18 @@
|
||||||
import
|
import
|
||||||
constants, errors, eth_common, eth_keys, nimcrypto, rlp
|
constants, errors, eth_common, eth_keys, nimcrypto, rlp
|
||||||
|
|
||||||
|
proc initTransaction*(nonce: AccountNonce, gasPrice, gasLimit: GasInt, to: EthAddress,
|
||||||
|
value: UInt256, payload: Blob, V: byte, R, S: UInt256, isContractCreation = false): Transaction =
|
||||||
|
result.accountNonce = nonce
|
||||||
|
result.gasPrice = gasPrice
|
||||||
|
result.gasLimit = gasLimit
|
||||||
|
result.to = to
|
||||||
|
result.value = value
|
||||||
|
result.V = V
|
||||||
|
result.R = R
|
||||||
|
result.S = S
|
||||||
|
result.isContractCreation = isContractCreation
|
||||||
|
|
||||||
proc intrinsicGas*(t: Transaction): GasInt =
|
proc intrinsicGas*(t: Transaction): GasInt =
|
||||||
# Compute the baseline gas cost for this transaction. This is the amount
|
# Compute the baseline gas cost for this transaction. This is the amount
|
||||||
# of gas needed to send this transaction (but that is not actually used
|
# of gas needed to send this transaction (but that is not actually used
|
||||||
|
|
|
@ -16,7 +16,7 @@ proc eth_hashrate(): int
|
||||||
proc eth_gasPrice(): GasInt
|
proc eth_gasPrice(): GasInt
|
||||||
proc eth_accounts(): seq[EthAddressStr]
|
proc eth_accounts(): seq[EthAddressStr]
|
||||||
proc eth_blockNumber(): BlockNumber
|
proc eth_blockNumber(): BlockNumber
|
||||||
proc eth_getBalance(data: EthAddressStr, quantityTag: string): int
|
proc eth_getBalance(data: EthAddressStr, quantityTag: string): UInt256
|
||||||
proc eth_getStorageAt(data: EthAddressStr, quantity: int, quantityTag: string): seq[byte]
|
proc eth_getStorageAt(data: EthAddressStr, quantity: int, quantityTag: string): seq[byte]
|
||||||
proc eth_getTransactionCount(data: EthAddressStr, quantityTag: string)
|
proc eth_getTransactionCount(data: EthAddressStr, quantityTag: string)
|
||||||
proc eth_getBlockTransactionCountByHash(data: array[32, byte])
|
proc eth_getBlockTransactionCountByHash(data: array[32, byte])
|
||||||
|
@ -25,12 +25,12 @@ proc eth_getUncleCountByBlockHash(data: array[32, byte])
|
||||||
proc eth_getUncleCountByBlockNumber(quantityTag: string)
|
proc eth_getUncleCountByBlockNumber(quantityTag: string)
|
||||||
proc eth_getCode(data: EthAddressStr, quantityTag: string): HexDataStr
|
proc eth_getCode(data: EthAddressStr, quantityTag: string): HexDataStr
|
||||||
proc eth_sign(data:EthAddressStr, message: HexDataStr): HexDataStr
|
proc eth_sign(data:EthAddressStr, message: HexDataStr): HexDataStr
|
||||||
|
#proc eth_sendRawTransaction(data: string, quantityTag: int): UInt256
|
||||||
|
proc eth_call(call: EthCall, quantityTag: string): string
|
||||||
|
|
||||||
# TODO: Use eth_common types
|
# TODO: Use eth_common types
|
||||||
|
|
||||||
#[proc eth_sendTransaction(obj: EthSend): UInt256
|
#[proc eth_sendTransaction(obj: EthSend): UInt256
|
||||||
proc eth_sendRawTransaction(data: string, quantityTag: int): UInt256
|
|
||||||
proc eth_call(call: EthCall, quantityTag: string): UInt256
|
|
||||||
proc eth_estimateGas(call: EthCall, quantityTag: string): UInt256
|
proc eth_estimateGas(call: EthCall, quantityTag: string): UInt256
|
||||||
proc eth_getBlockByHash(data: array[32, byte], fullTransactions: bool): BlockObject
|
proc eth_getBlockByHash(data: array[32, byte], fullTransactions: bool): BlockObject
|
||||||
proc eth_getBlockByNumber(quantityTag: string, fullTransactions: bool): BlockObject
|
proc eth_getBlockByNumber(quantityTag: string, fullTransactions: bool): BlockObject
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
import
|
import
|
||||||
unittest, json, strformat, nimcrypto, rlp,
|
unittest, json, strformat, nimcrypto, rlp, options,
|
||||||
json_rpc/[rpcserver, rpcclient],
|
json_rpc/[rpcserver, rpcclient],
|
||||||
../nimbus/rpc/[common, p2p, hexstrings],
|
../nimbus/rpc/[common, p2p, hexstrings, rpc_types],
|
||||||
../nimbus/constants,
|
../nimbus/constants,
|
||||||
../nimbus/nimbus/[vm_state, config],
|
../nimbus/nimbus/[vm_state, config],
|
||||||
../nimbus/db/[state_db, db_chain], eth_common, byteutils,
|
../nimbus/db/[state_db, db_chain], eth_common, byteutils,
|
||||||
../nimbus/p2p/chain,
|
../nimbus/p2p/chain,
|
||||||
|
../nimbus/genesis,
|
||||||
eth_trie/db,
|
eth_trie/db,
|
||||||
eth_p2p, eth_keys
|
eth_p2p, eth_keys
|
||||||
import rpcclient/test_hexstrings
|
import rpcclient/test_hexstrings
|
||||||
|
@ -53,6 +54,8 @@ proc doTests =
|
||||||
let
|
let
|
||||||
balance = 100.u256
|
balance = 100.u256
|
||||||
address: EthAddress = hexToByteArray[20]("0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6")
|
address: EthAddress = hexToByteArray[20]("0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6")
|
||||||
|
conf = getConfiguration()
|
||||||
|
defaultGenesisBlockForNetwork(conf.net.networkId.toPublicNetwork()).commit(chain)
|
||||||
state.mutateStateDB:
|
state.mutateStateDB:
|
||||||
db.setBalance(address, balance)
|
db.setBalance(address, balance)
|
||||||
|
|
||||||
|
@ -68,6 +71,13 @@ proc doTests =
|
||||||
waitFor client.connect("localhost", Port(8545))
|
waitFor client.connect("localhost", Port(8545))
|
||||||
|
|
||||||
suite "Remote Procedure Calls":
|
suite "Remote Procedure Calls":
|
||||||
|
# TODO: Currently returning 'block not found' when fetching header in p2p, so cannot perform tests
|
||||||
|
test "eth_call":
|
||||||
|
let
|
||||||
|
blockNum = state.blockheader.blockNumber
|
||||||
|
callParams = EthCall(value: some(100.u256))
|
||||||
|
var r = waitFor client.eth_call(callParams, "0x" & blockNum.toHex)
|
||||||
|
echo r
|
||||||
test "eth_getBalance":
|
test "eth_getBalance":
|
||||||
expect ValueError:
|
expect ValueError:
|
||||||
# check error is raised on null address
|
# check error is raised on null address
|
||||||
|
|
Loading…
Reference in New Issue