|
|
@ -9,13 +9,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
import
|
|
|
|
import
|
|
|
|
std/[times, sequtils],
|
|
|
|
std/[times, sequtils],
|
|
|
|
json_rpc/[rpcproxy, rpcserver], nimcrypto/[hash, keccak],
|
|
|
|
json_rpc/[rpcproxy, rpcserver], nimcrypto/[hash, keccak], stew/byteutils,
|
|
|
|
web3/conversions, # sigh, for FixedBytes marshalling
|
|
|
|
web3/conversions, # sigh, for FixedBytes marshalling
|
|
|
|
eth/[common/eth_types, rlp],
|
|
|
|
eth/[common/eth_types, rlp],
|
|
|
|
# TODO: Using the Nimbus json-rpc helpers, but they could use some rework as
|
|
|
|
../../nimbus/rpc/[rpc_types, hexstrings],
|
|
|
|
# they bring a whole lot of other stuff with them.
|
|
|
|
../../nimbus/transaction,
|
|
|
|
../../nimbus/rpc/[rpc_types, hexstrings, rpc_utils],
|
|
|
|
|
|
|
|
../../nimbus/errors, # for ValidationError, should be exported instead
|
|
|
|
|
|
|
|
../network/history/[history_network, history_content]
|
|
|
|
../network/history/[history_network, history_content]
|
|
|
|
|
|
|
|
|
|
|
|
# Subset of Eth JSON-RPC API: https://eth.wiki/json-rpc/API
|
|
|
|
# Subset of Eth JSON-RPC API: https://eth.wiki/json-rpc/API
|
|
|
@ -28,51 +26,84 @@ import
|
|
|
|
# Can be done by just forwarding the rpc call, or by adding a call here, but
|
|
|
|
# Can be done by just forwarding the rpc call, or by adding a call here, but
|
|
|
|
# that would introduce a unnecessary serializing/deserializing step.
|
|
|
|
# that would introduce a unnecessary serializing/deserializing step.
|
|
|
|
|
|
|
|
|
|
|
|
# Note: Similar as `populateBlockObject` from rpc_utils, but more limited as
|
|
|
|
# Some similar code as from nimbus `rpc_utils`, but avoiding that import as it
|
|
|
|
# there is currently only access to the block header.
|
|
|
|
# brings in a lot more. Should restructure `rpc_utils` a bit before using that.
|
|
|
|
proc buildBlockObject*(
|
|
|
|
func toHash*(value: array[32, byte]): Hash256 =
|
|
|
|
|
|
|
|
result.data = value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func toHash*(value: EthHashStr): Hash256 {.raises: [Defect, ValueError].} =
|
|
|
|
|
|
|
|
hexToPaddedByteArray[32](value.string).toHash
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func init*(
|
|
|
|
|
|
|
|
T: type TransactionObject,
|
|
|
|
|
|
|
|
tx: Transaction, header: BlockHeader, txIndex: int):
|
|
|
|
|
|
|
|
T {.raises: [Defect, ValidationError].} =
|
|
|
|
|
|
|
|
TransactionObject(
|
|
|
|
|
|
|
|
blockHash: some(header.blockHash),
|
|
|
|
|
|
|
|
blockNumber: some(encodeQuantity(header.blockNumber)),
|
|
|
|
|
|
|
|
`from`: tx.getSender(),
|
|
|
|
|
|
|
|
gas: encodeQuantity(tx.gasLimit.uint64),
|
|
|
|
|
|
|
|
gasPrice: encodeQuantity(tx.gasPrice.uint64),
|
|
|
|
|
|
|
|
hash: tx.rlpHash,
|
|
|
|
|
|
|
|
input: tx.payload,
|
|
|
|
|
|
|
|
nonce: encodeQuantity(tx.nonce.uint64),
|
|
|
|
|
|
|
|
to: some(tx.destination),
|
|
|
|
|
|
|
|
transactionIndex: some(encodeQuantity(txIndex.uint64)),
|
|
|
|
|
|
|
|
value: encodeQuantity(tx.value),
|
|
|
|
|
|
|
|
v: encodeQuantity(tx.V.uint),
|
|
|
|
|
|
|
|
r: encodeQuantity(tx.R),
|
|
|
|
|
|
|
|
s: encodeQuantity(tx.S)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Note: Similar as `populateBlockObject` from rpc_utils, but lacking the
|
|
|
|
|
|
|
|
# total difficulty
|
|
|
|
|
|
|
|
func init*(
|
|
|
|
|
|
|
|
T: type BlockObject,
|
|
|
|
header: BlockHeader, body: BlockBody,
|
|
|
|
header: BlockHeader, body: BlockBody,
|
|
|
|
fullTx = true, isUncle = false):
|
|
|
|
fullTx = true, isUncle = false):
|
|
|
|
BlockObject {.raises: [Defect, ValidationError].} =
|
|
|
|
T {.raises: [Defect, ValidationError].} =
|
|
|
|
let blockHash = header.blockHash
|
|
|
|
let blockHash = header.blockHash
|
|
|
|
|
|
|
|
|
|
|
|
result.number = some(encodeQuantity(header.blockNumber))
|
|
|
|
var blockObject = BlockObject(
|
|
|
|
result.hash = some(blockHash)
|
|
|
|
number: some(encodeQuantity(header.blockNumber)),
|
|
|
|
result.parentHash = header.parentHash
|
|
|
|
hash: some(blockHash),
|
|
|
|
result.nonce = some(hexDataStr(header.nonce))
|
|
|
|
parentHash: header.parentHash,
|
|
|
|
result.sha3Uncles = header.ommersHash
|
|
|
|
nonce: some(hexDataStr(header.nonce)),
|
|
|
|
result.logsBloom = FixedBytes[256] header.bloom
|
|
|
|
sha3Uncles: header.ommersHash,
|
|
|
|
result.transactionsRoot = header.txRoot
|
|
|
|
logsBloom: FixedBytes[256] header.bloom,
|
|
|
|
result.stateRoot = header.stateRoot
|
|
|
|
transactionsRoot: header.txRoot,
|
|
|
|
result.receiptsRoot = header.receiptRoot
|
|
|
|
stateRoot: header.stateRoot,
|
|
|
|
result.miner = header.coinbase
|
|
|
|
receiptsRoot: header.receiptRoot,
|
|
|
|
result.difficulty = encodeQuantity(header.difficulty)
|
|
|
|
miner: header.coinbase,
|
|
|
|
result.extraData = hexDataStr(header.extraData)
|
|
|
|
difficulty: encodeQuantity(header.difficulty),
|
|
|
|
|
|
|
|
extraData: hexDataStr(header.extraData),
|
|
|
|
# TODO: This is optional according to
|
|
|
|
# TODO: This is optional according to
|
|
|
|
# https://playground.open-rpc.org/?schemaUrl=https://raw.githubusercontent.com/ethereum/eth1.0-apis/assembled-spec/openrpc.json
|
|
|
|
# https://playground.open-rpc.org/?schemaUrl=https://raw.githubusercontent.com/ethereum/eth1.0-apis/assembled-spec/openrpc.json
|
|
|
|
# So we should probably change `BlockObject`.
|
|
|
|
# So we should probably change `BlockObject`.
|
|
|
|
result.totalDifficulty = encodeQuantity(UInt256.low())
|
|
|
|
totalDifficulty: encodeQuantity(UInt256.low()),
|
|
|
|
|
|
|
|
gasLimit: encodeQuantity(header.gasLimit.uint64),
|
|
|
|
|
|
|
|
gasUsed: encodeQuantity(header.gasUsed.uint64),
|
|
|
|
|
|
|
|
timestamp: encodeQuantity(header.timeStamp.toUnix.uint64)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
let size = sizeof(BlockHeader) - sizeof(Blob) + header.extraData.len
|
|
|
|
let size = sizeof(BlockHeader) - sizeof(Blob) + header.extraData.len
|
|
|
|
result.size = encodeQuantity(size.uint)
|
|
|
|
blockObject.size = encodeQuantity(size.uint)
|
|
|
|
|
|
|
|
|
|
|
|
result.gasLimit = encodeQuantity(header.gasLimit.uint64)
|
|
|
|
|
|
|
|
result.gasUsed = encodeQuantity(header.gasUsed.uint64)
|
|
|
|
|
|
|
|
result.timestamp = encodeQuantity(header.timeStamp.toUnix.uint64)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not isUncle:
|
|
|
|
if not isUncle:
|
|
|
|
result.uncles = body.uncles.map(proc(h: BlockHeader): Hash256 = h.blockHash)
|
|
|
|
blockObject.uncles =
|
|
|
|
|
|
|
|
body.uncles.map(proc(h: BlockHeader): Hash256 = h.blockHash)
|
|
|
|
|
|
|
|
|
|
|
|
if fullTx:
|
|
|
|
if fullTx:
|
|
|
|
var i = 0
|
|
|
|
var i = 0
|
|
|
|
for tx in body.transactions:
|
|
|
|
for tx in body.transactions:
|
|
|
|
# ValidationError from tx.getSender in populateTransactionObject
|
|
|
|
# ValidationError from tx.getSender in TransactionObject.init
|
|
|
|
result.transactions.add %(populateTransactionObject(tx, header, i))
|
|
|
|
blockObject.transactions.add %(TransactionObject.init(tx, header, i))
|
|
|
|
inc i
|
|
|
|
inc i
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
for tx in body.transactions:
|
|
|
|
for tx in body.transactions:
|
|
|
|
result.transactions.add %(keccak256.digest(rlp.encode(tx)))
|
|
|
|
blockObject.transactions.add %(keccak256.digest(rlp.encode(tx)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
blockObject
|
|
|
|
|
|
|
|
|
|
|
|
proc installEthApiHandlers*(
|
|
|
|
proc installEthApiHandlers*(
|
|
|
|
# Currently only HistoryNetwork needed, later we might want a master object
|
|
|
|
# Currently only HistoryNetwork needed, later we might want a master object
|
|
|
@ -164,19 +195,17 @@ proc installEthApiHandlers*(
|
|
|
|
## data: Hash of a block.
|
|
|
|
## data: Hash of a block.
|
|
|
|
## fullTransactions: If true it returns the full transaction objects, if
|
|
|
|
## fullTransactions: If true it returns the full transaction objects, if
|
|
|
|
## false only the hashes of the transactions.
|
|
|
|
## false only the hashes of the transactions.
|
|
|
|
## Note: transactions and uncles are currently not implemented.
|
|
|
|
|
|
|
|
##
|
|
|
|
##
|
|
|
|
## Returns BlockObject or nil when no block was found.
|
|
|
|
## Returns BlockObject or nil when no block was found.
|
|
|
|
let
|
|
|
|
let
|
|
|
|
blockHash = data.toHash()
|
|
|
|
blockHash = data.toHash()
|
|
|
|
|
|
|
|
blockRes = await historyNetwork.getBlock(1'u16, blockHash)
|
|
|
|
|
|
|
|
|
|
|
|
let maybeHeaderAndBody = await historyNetwork.getBlock(1'u16, blockHash)
|
|
|
|
if blockRes.isNone():
|
|
|
|
|
|
|
|
|
|
|
|
if maybeHeaderAndBody.isNone():
|
|
|
|
|
|
|
|
return none(BlockObject)
|
|
|
|
return none(BlockObject)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
let (header, body) = maybeHeaderAndBody.unsafeGet()
|
|
|
|
let (header, body) = blockRes.unsafeGet()
|
|
|
|
return some(buildBlockObject(header, body))
|
|
|
|
return some(BlockObject.init(header, body))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rpcServerWithProxy.rpc("eth_getBlockTransactionCountByHash") do(
|
|
|
|
rpcServerWithProxy.rpc("eth_getBlockTransactionCountByHash") do(
|
|
|
@ -188,13 +217,12 @@ proc installEthApiHandlers*(
|
|
|
|
## Returns integer of the number of transactions in this block.
|
|
|
|
## Returns integer of the number of transactions in this block.
|
|
|
|
let
|
|
|
|
let
|
|
|
|
blockHash = data.toHash()
|
|
|
|
blockHash = data.toHash()
|
|
|
|
|
|
|
|
blockRes = await historyNetwork.getBlock(1'u16, blockHash)
|
|
|
|
|
|
|
|
|
|
|
|
let maybeHeaderAndBody = await historyNetwork.getBlock(1'u16, blockHash)
|
|
|
|
if blockRes.isNone():
|
|
|
|
|
|
|
|
|
|
|
|
if maybeHeaderAndBody.isNone():
|
|
|
|
|
|
|
|
raise newException(ValueError, "Could not find block with requested hash")
|
|
|
|
raise newException(ValueError, "Could not find block with requested hash")
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
let (_, body) = maybeHeaderAndBody.unsafeGet()
|
|
|
|
let (_, body) = blockRes.unsafeGet()
|
|
|
|
var txCount:uint = 0
|
|
|
|
var txCount:uint = 0
|
|
|
|
for tx in body.transactions:
|
|
|
|
for tx in body.transactions:
|
|
|
|
txCount.inc()
|
|
|
|
txCount.inc()
|
|
|
|