Skip to content

Commit

Permalink
Bump nim-web3: remove rpc types duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Dec 12, 2023
1 parent 2b71e3e commit ea4a8a0
Show file tree
Hide file tree
Showing 59 changed files with 864 additions and 2,122 deletions.
6 changes: 3 additions & 3 deletions fluffy/rpc/eth_rpc_client.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Nimbus
# Copyright (c) 2022 Status Research & Development GmbH
# Copyright (c) 2022-2023 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
Expand All @@ -10,9 +10,9 @@ import
json_rpc/rpcclient,
json_rpc/errors, # TODO: should be exported in json_rpc/clients/httpclient
web3/conversions, # sigh
../../nimbus/rpc/[rpc_types, hexstrings]
../../nimbus/rpc/[rpc_types]

export rpcclient, rpc_types, hexstrings, errors
export rpcclient, rpc_types, errors

createRpcSigs(RpcClient, currentSourcePath.parentDir / "rpc_calls" / "rpc_eth_calls.nim")
createRpcSigs(RpcClient, currentSourcePath.parentDir / "rpc_calls" / "rpc_web3_calls.nim")
13 changes: 10 additions & 3 deletions fluffy/rpc/rpc_calls/rpc_eth_calls.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
proc eth_chaindId(): HexQuantityStr
proc eth_getBlockByHash(data: EthHashStr, fullTransactions: bool): Option[BlockObject]
# Fluffy
# Copyright (c) 2021-2023 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed except according to those terms.

proc eth_chaindId(): Quantity
proc eth_getBlockByHash(data: Hash256, fullTransactions: bool): Option[BlockObject]
proc eth_getBlockByNumber(quantityTag: string, fullTransactions: bool): Option[BlockObject]
proc eth_getBlockTransactionCountByHash(data: EthHashStr): HexQuantityStr
proc eth_getBlockTransactionCountByHash(data: Hash256): Quantity
proc eth_getTransactionReceipt(data: Hash256): Option[ReceiptObject]
proc eth_getLogs(filterOptions: FilterOptions): seq[FilterLog]

Expand Down
99 changes: 48 additions & 51 deletions fluffy/rpc/rpc_eth_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import
std/[times, sequtils, strutils, typetraits],
json_rpc/[rpcproxy, rpcserver], stew/byteutils,
web3/conversions, # sigh, for FixedBytes marshalling
web3/[conversions, ethhexstrings], # sigh, for FixedBytes marshalling
eth/[common/eth_types, rlp],
beacon_chain/spec/forks,
../../nimbus/rpc/[rpc_types, hexstrings, filters],
../../nimbus/rpc/[rpc_types, filters],
../../nimbus/transaction,
# TODO: this is a bit weird but having this import makes beacon_light_client
# to fail compilation due throwing undeclared `CatchableError` in
Expand All @@ -35,83 +35,80 @@ import

# Some similar code as from nimbus `rpc_utils`, but avoiding that import as it
# brings in a lot more. Should restructure `rpc_utils` a bit before using that.
func toHash*(value: array[32, byte]): Hash256 =
result.data = value

func toHash*(value: EthHashStr): Hash256 {.raises: [ValueError].} =
hexToPaddedByteArray[32](value.string).toHash
func toHash*(value: rpc_types.Hash256): eth_types.Hash256 =
result.data = value.bytes

func init*(
T: type TransactionObject,
tx: eth_types.Transaction, header: BlockHeader, txIndex: int):
tx: eth_types.Transaction, header: eth_types.BlockHeader, txIndex: int):
T {.raises: [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,
blockHash: some(w3Hash header.blockHash),
blockNumber: some(Quantity(header.blockNumber.truncate(uint64))),
`from`: w3Addr tx.getSender(),
gas: Quantity(tx.gasLimit),
gasPrice: Quantity(tx.gasPrice),
hash: w3Hash 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),
`type`: encodeQuantity(tx.txType.uint64),
maxFeePerGas: encodeQuantity(tx.maxFee.uint64),
maxPriorityFeePerGas: encodeQuantity(tx.maxPriorityFee.uint64),
nonce: Quantity(tx.nonce),
to: some(w3Addr tx.destination),
transactionIndex: some(Quantity(txIndex)),
value: tx.value,
v: Quantity(tx.V),
r: tx.R,
s: tx.S,
`type`: some(Quantity(tx.txType)),
maxFeePerGas: some(Quantity(tx.maxFee)),
maxPriorityFeePerGas: some(Quantity(tx.maxPriorityFee)),
)

# Note: Similar as `populateBlockObject` from rpc_utils, but lacking the
# total difficulty
func init*(
T: type BlockObject,
header: BlockHeader, body: BlockBody,
header: eth_types.BlockHeader, body: BlockBody,
fullTx = true, isUncle = false):
T {.raises: [ValidationError].} =
let blockHash = header.blockHash

var blockObject = BlockObject(
number: some(encodeQuantity(header.blockNumber)),
hash: some(blockHash),
parentHash: header.parentHash,
nonce: some(hexDataStr(header.nonce)),
sha3Uncles: header.ommersHash,
number: Quantity(header.blockNumber.truncate(uint64)),
hash: w3Hash blockHash,
parentHash: w3Hash header.parentHash,
nonce: some(FixedBytes[8](header.nonce)),
sha3Uncles: w3Hash header.ommersHash,
logsBloom: FixedBytes[256] header.bloom,
transactionsRoot: header.txRoot,
stateRoot: header.stateRoot,
receiptsRoot: header.receiptRoot,
miner: header.coinbase,
difficulty: encodeQuantity(header.difficulty),
extraData: hexDataStr(header.extraData),
transactionsRoot: w3Hash header.txRoot,
stateRoot: w3Hash header.stateRoot,
receiptsRoot: w3Hash header.receiptRoot,
miner: w3Addr header.coinbase,
difficulty: header.difficulty,
extraData: HistoricExtraData header.extraData,
# TODO: This is optional according to
# https://playground.open-rpc.org/?schemaUrl=https://raw.githubusercontent.com/ethereum/eth1.0-apis/assembled-spec/openrpc.json
# So we should probably change `BlockObject`.
totalDifficulty: encodeQuantity(UInt256.low()),
gasLimit: encodeQuantity(header.gasLimit.uint64),
gasUsed: encodeQuantity(header.gasUsed.uint64),
timestamp: encodeQuantity(header.timestamp.uint64)
totalDifficulty: UInt256.low(),
gasLimit: Quantity(header.gasLimit.uint64),
gasUsed: Quantity(header.gasUsed.uint64),
timestamp: Quantity(header.timestamp.uint64)
)

let size = sizeof(BlockHeader) - sizeof(Blob) + header.extraData.len
blockObject.size = encodeQuantity(size.uint)
blockObject.size = Quantity(size.uint)

if not isUncle:
blockObject.uncles =
body.uncles.map(proc(h: BlockHeader): Hash256 = h.blockHash)
body.uncles.map(proc(h: BlockHeader): rpc_types.Hash256 = w3Hash h.blockHash)

if fullTx:
var i = 0
for tx in body.transactions:
# ValidationError from tx.getSender in TransactionObject.init
blockObject.transactions.add %(TransactionObject.init(tx, header, i))
blockObject.transactions.add txOrHash(TransactionObject.init(tx, header, i))
inc i
else:
for tx in body.transactions:
blockObject.transactions.add %(keccakHash(rlp.encode(tx)))
blockObject.transactions.add txOrHash(w3Hash keccakHash(rlp.encode(tx)))

blockObject

Expand Down Expand Up @@ -199,13 +196,13 @@ proc installEthApiHandlers*(

# Supported API through the Portal Network

rpcServerWithProxy.rpc("eth_chainId") do() -> HexQuantityStr:
rpcServerWithProxy.rpc("eth_chainId") do() -> Quantity:
# The Portal Network can only support MainNet at the moment, so always return
# 1
return encodeQuantity(uint64(1))
return Quantity(uint64(1))

rpcServerWithProxy.rpc("eth_getBlockByHash") do(
data: EthHashStr, fullTransactions: bool) -> Option[BlockObject]:
data: rpc_types.Hash256, fullTransactions: bool) -> Option[BlockObject]:
## Returns information about a block by hash.
##
## data: Hash of a block.
Expand Down Expand Up @@ -265,7 +262,7 @@ proc installEthApiHandlers*(
of "pending":
raise newException(ValueError, "Pending tag not yet implemented")
else:
if not isValidHexQuantity(quantityTag):
if not validate(quantityTag.HexQuantityStr):
raise newException(ValueError, "Provided block number is not a hex number")

let
Expand All @@ -280,7 +277,7 @@ proc installEthApiHandlers*(
return some(BlockObject.init(header, body))

rpcServerWithProxy.rpc("eth_getBlockTransactionCountByHash") do(
data: EthHashStr) -> HexQuantityStr:
data: rpc_types.Hash256) -> Quantity:
## Returns the number of transactions in a block from a block matching the
## given block hash.
##
Expand All @@ -295,7 +292,7 @@ proc installEthApiHandlers*(
for tx in body.transactions:
txCount.inc()

return encodeQuantity(txCount)
return Quantity(txCount)

# Note: can't implement this yet as the fluffy node doesn't know the relation
# of tx hash -> block number -> block hash, in order to get the receipt
Expand All @@ -312,7 +309,7 @@ proc installEthApiHandlers*(
raise newException(ValueError,
"Unsupported query: Only `blockHash` queries are currently supported")

let hash = filterOptions.blockHash.unsafeGet()
let hash = ethHash filterOptions.blockHash.unsafeGet()

let header = (await historyNetwork.getVerifiedBlockHeader(hash)).valueOr:
raise newException(ValueError,
Expand Down
15 changes: 7 additions & 8 deletions fluffy/scripts/test_portal_testnet.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import
unittest2, testutils, confutils, chronos,
stew/byteutils,
eth/p2p/discoveryv5/random2, eth/keys,
../../nimbus/rpc/[hexstrings, rpc_types],
../../nimbus/rpc/[rpc_types],
../rpc/portal_rpc_client,
../rpc/eth_rpc_client,
../eth_data/[
Expand Down Expand Up @@ -270,7 +270,7 @@ procSuite "Portal testnet tests":
let content = await retryUntil(
proc (): Future[Option[BlockObject]] {.async.} =
try:
let res = await client.eth_getBlockByHash(hash.ethHashStr(), false)
let res = await client.eth_getBlockByHash(w3Hash hash, false)
await client.close()
return res
except CatchableError as exc:
Expand All @@ -283,15 +283,14 @@ procSuite "Portal testnet tests":
)
check content.isSome()
let blockObj = content.get()
check blockObj.hash.get() == hash
check blockObj.hash == w3Hash hash

for tx in blockObj.transactions:
var txObj: TransactionObject
tx.fromJson("tx", txObj)
check txObj.blockHash.get() == hash
doAssert(tx.kind == tohTx)
check tx.tx.blockHash.get == w3Hash hash

let filterOptions = FilterOptions(
blockHash: some(hash)
blockHash: some(w3Hash hash)
)

let logs = await retryUntil(
Expand All @@ -311,7 +310,7 @@ procSuite "Portal testnet tests":

for l in logs:
check:
l.blockHash == some(hash)
l.blockHash == some(w3Hash hash)

# TODO: Check ommersHash, need the headers and not just the hashes
# for uncle in blockObj.uncles:
Expand Down
36 changes: 11 additions & 25 deletions fluffy/tools/beacon_lc_bridge/beacon_lc_bridge.nim
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ import

from stew/objects import checkedEnumAssign
from stew/byteutils import readHexChar
from web3/ethtypes as web3types import BlockHash
from web3/primitives as web3types import BlockHash

from beacon_chain/gossip_processing/block_processor import newExecutionPayload
from beacon_chain/gossip_processing/eth2_processor import toValidationResult
Expand Down Expand Up @@ -123,13 +123,8 @@ func hexToInt(

res

func asTxType(quantity: HexQuantityStr): Result[TxType, string] =
let value =
try:
hexToInt(quantity.string, uint8)
except ValueError as e:
return err("Invalid data for TxType: " & e.msg)

func asTxType(quantity: Option[Quantity]): Result[TxType, string] =
let value = quantity.get(0.Quantity).uint8
var txType: TxType
if not checkedEnumAssign(txType, value):
err("Invalid data for TxType: " & $value)
Expand All @@ -144,28 +139,19 @@ func asReceipt(
var logs: seq[Log]
if receiptObject.logs.len > 0:
for log in receiptObject.logs:
var topics: seq[Topic]
var topics: seq[eth_types.Topic]
for topic in log.topics:
topics.add(Topic(topic.data))
topics.add(eth_types.Topic(topic))

logs.add(Log(
address: log.address,
address: ethAddr log.address,
data: log.data,
topics: topics
))

let cumulativeGasUsed =
try:
hexToInt(receiptObject.cumulativeGasUsed.string, GasInt)
except ValueError as e:
return err("Invalid data for cumulativeGasUsed: " & e.msg)

let cumulativeGasUsed = receiptObject.cumulativeGasUsed.GasInt
if receiptObject.status.isSome():
let status =
try:
hexToInt(receiptObject.status.get().string, int)
except ValueError as e:
return err("Invalid data for status: " & e.msg)
let status = receiptObject.status.get().int
ok(Receipt(
receiptType: receiptType,
isHash: false,
Expand All @@ -178,7 +164,7 @@ func asReceipt(
ok(Receipt(
receiptType: receiptType,
isHash: true,
hash: receiptObject.root.get(),
hash: ethHash receiptObject.root.get(),
cumulativeGasUsed: cumulativeGasUsed,
bloom: BloomFilter(receiptObject.logsBloom),
logs: logs
Expand Down Expand Up @@ -336,7 +322,7 @@ proc getBlockReceipts(
let receiptObjects =
# TODO: Add some retries depending on the failure
try:
await client.eth_getBlockReceipts(blockHash)
await client.eth_getBlockReceipts(w3Hash blockHash)
except CatchableError as e:
await client.close()
return err("JSON-RPC eth_getBlockReceipts failed: " & e.msg)
Expand All @@ -362,7 +348,7 @@ proc getBlockReceipts(
let receiptObjectOpt =
# TODO: Add some retries depending on the failure
try:
await client.eth_getTransactionReceipt(txHash)
await client.eth_getTransactionReceipt(w3Hash txHash)
except CatchableError as e:
await client.close()
return err("JSON-RPC eth_getTransactionReceipt failed: " & e.msg)
Expand Down
Loading

0 comments on commit ea4a8a0

Please sign in to comment.