From 017f9f1103466cc6d5f0fbbce877878ea360c12c Mon Sep 17 00:00:00 2001 From: andri lim Date: Sat, 13 Jan 2024 08:36:17 +0700 Subject: [PATCH] bump nim-web3 to bf1ce869b5f76d9744444b248f6f5c6c782fedc7 (#5722) Etan Kissling (2): remove unused `skip0xPrefix` keep the internal count helper Will (1): Bugfix/nully values (#61) Yuriy Glukhov (5): Contract constructor support Fixed compilation error in exec function Added string encoding Fixed source->from field of EthCall More flexibility to contract DSL, Async contract caller jangko (5): Reduce compiler warnings when using Nim v2 Migrate to json-serialization Add tests of json rpc marshalled types Resolve contract_dsl ambiguity Event handler passing around JsonString instead of JsonNode Share encoder between json-rpc and chronicles (#119) Simplify generic constraint of rpc and chronicles encoders Feature/execution api spec (#69) v0.3.0 bump nim-json-rpc to a6475e49b26d3afc58aaa3d67621c94eafef8efb coffeepots (1): Use nim-json-serialization for RPCs (#172) jangko (10): Add copyright to source file Remove StringOfJson Fix optional parameter parsing fails in rpc macro with generics Rename jrpc_sys module back to jsonmarshal Reenable test hhtps Add test for createRpcSigsFromNim and createSingleRpcSig Let the OS choose the port for tests Add onProcessMessage hook to client Fix example in the README.md Move errors module back to json_rpc folder Upgrade rpc router internals (#178) RPC server handle null return value correctly v0.3.0 kdeme (1): Add example test case that currently fails the Option parsing --- beacon_chain/el/el_manager.nim | 37 ++++++++---------- beacon_chain/libnimbus_lc/libnimbus_lc.nim | 45 +++++++++------------- ncli/ncli_testnet.nim | 11 +----- research/fakeee.nim | 6 +-- vendor/nim-json-rpc | 2 +- vendor/nim-web3 | 2 +- 6 files changed, 42 insertions(+), 61 deletions(-) diff --git a/beacon_chain/el/el_manager.nim b/beacon_chain/el/el_manager.nim index 3bb4978d4..9821d8b0d 100644 --- a/beacon_chain/el/el_manager.nim +++ b/beacon_chain/el/el_manager.nim @@ -12,7 +12,7 @@ import # Nimble packages: chronos, metrics, chronicles/timings, json_rpc/[client, errors], - web3, web3/ethhexstrings, web3/engine_api, + web3, web3/[engine_api, primitives, conversions], eth/common/[eth_types, transaction], eth/async_utils, stew/[assign2, byteutils, objects, results, shims/hashes, endians2], # Local modules: @@ -1440,10 +1440,11 @@ proc exchangeTransitionConfiguration*(m: ELManager) {.async.} = if cancelled == requests.len: warn "Failed to exchange configuration with the configured EL end-points" -template readJsonField(j: JsonNode, fieldName: string, ValueType: type): untyped = - var res: ValueType - fromJson(j[fieldName], fieldName, res) - res +template readJsonField(logEvent, field: untyped, ValueType: type): untyped = + if logEvent.field.isNone: + raise newException(CatchableError, + "Web3 provider didn't return needed logEvent field " & astToStr(field)) + logEvent.field.get template init[N: static int](T: type DynamicBytes[N, N]): T = T newSeq[byte](N) @@ -1460,19 +1461,15 @@ proc fetchTimestamp(connection: ELConnection, blk.timestamp = Eth1BlockTimestamp web3block.timestamp -func depositEventsToBlocks(depositsList: JsonNode): seq[Eth1Block] {. +func depositEventsToBlocks(depositsList: openArray[JsonString]): seq[Eth1Block] {. raises: [CatchableError].} = - if depositsList.kind != JArray: - raise newException(CatchableError, - "Web3 provider didn't return a list of deposit events") - var lastEth1Block: Eth1Block - for logEvent in depositsList: + for logEventData in depositsList: let - blockNumber = Eth1BlockNumber readJsonField(logEvent, "blockNumber", Quantity) - blockHash = readJsonField(logEvent, "blockHash", BlockHash) - logData = hexToSeqByte(logEvent["data"].getStr) + logEvent = JrpcConv.decode(logEventData.string, LogObject) + blockNumber = Eth1BlockNumber readJsonField(logEvent, blockNumber, Quantity) + blockHash = readJsonField(logEvent, blockHash, BlockHash) if lastEth1Block == nil or lastEth1Block.number != blockNumber: lastEth1Block = Eth1Block( @@ -1493,11 +1490,11 @@ func depositEventsToBlocks(depositsList: JsonNode): seq[Eth1Block] {. index = init Int64LeBytes var offset = 0 - offset += decode(logData, 0, offset, pubkey) - offset += decode(logData, 0, offset, withdrawalCredentials) - offset += decode(logData, 0, offset, amount) - offset += decode(logData, 0, offset, signature) - offset += decode(logData, 0, offset, index) + offset += decode(logEvent.data, 0, offset, pubkey) + offset += decode(logEvent.data, 0, offset, withdrawalCredentials) + offset += decode(logEvent.data, 0, offset, amount) + offset += decode(logEvent.data, 0, offset, signature) + offset += decode(logEvent.data, 0, offset, index) if pubkey.len != 48 or withdrawalCredentials.len != 32 or @@ -1906,7 +1903,7 @@ proc syncBlockRange(m: ELManager, var currentBlock = fromBlock while currentBlock <= toBlock: var - depositLogs: JsonNode = nil + depositLogs: seq[JsonString] maxBlockNumberRequested: Eth1BlockNumber backoff = 100 diff --git a/beacon_chain/libnimbus_lc/libnimbus_lc.nim b/beacon_chain/libnimbus_lc/libnimbus_lc.nim index f3815d2d6..79ec70bfa 100644 --- a/beacon_chain/libnimbus_lc/libnimbus_lc.nim +++ b/beacon_chain/libnimbus_lc/libnimbus_lc.nim @@ -1209,15 +1209,12 @@ proc ETHExecutionBlockHeaderCreateFromJson( ## ## See: ## * https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getblockbyhash - let node = - try: - parseJson($blockHeaderJson) - except Exception: - return nil - var data: BlockObject - try: - fromJson(node, argName = "", data) - except KeyError, ValueError: + let data = try: + # a direct parameter like JrpcConv.decode($blockHeaderJson, BlockObject) + # will cause premature garbage collector kick in. + let jsonBytes = $blockHeaderJson + JrpcConv.decode(jsonBytes, BlockObject) + except SerializationError: return nil if data == nil: return nil @@ -1445,15 +1442,12 @@ proc ETHTransactionsCreateFromJson( ## ## See: ## * https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getblockbyhash - let node = - try: - parseJson($transactionsJson) - except Exception: - return nil - var datas: seq[TransactionObject] - try: - fromJson(node, argName = "", datas) - except KeyError, ValueError: + var datas = try: + # a direct parameter like JrpcConv.decode($transactionsJson, seq[TransactionObject]) + # will cause premature garbage collector kick in. + let jsonBytes = $transactionsJson + JrpcConv.decode(jsonBytes, seq[TransactionObject]) + except SerializationError: return nil var txs = newSeqOfCap[ETHTransaction](datas.len) @@ -2085,15 +2079,12 @@ proc ETHReceiptsCreateFromJson( ## ## See: ## * https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_gettransactionreceipt - let node = - try: - parseJson($receiptsJson) - except Exception: - return nil - var datas: seq[ReceiptObject] - try: - fromJson(node, argName = "", datas) - except KeyError, ValueError: + var datas = try: + # a direct parameter like JrpcConv.decode($receiptsJson, seq[ReceiptObject]) + # will cause premature garbage collector kick in. + let jsonBytes = $receiptsJson + JrpcConv.decode(jsonBytes, seq[ReceiptObject]) + except SerializationError: return nil if datas.len != ETHTransactionsGetCount(transactions): return nil diff --git a/ncli/ncli_testnet.nim b/ncli/ncli_testnet.nim index 7802eaf2c..766572d43 100644 --- a/ncli/ncli_testnet.nim +++ b/ncli/ncli_testnet.nim @@ -443,15 +443,8 @@ proc doCreateTestnet*(config: CliConfig, quit 1 try: - let blockAsJson = try: - parseJson genesisBlockContents.get - except CatchableError as err: - error "Failed to parse the genesis block json", err = err.msg - quit 1 - except: - # TODO The Nim json library should not raise bare exceptions - raiseAssert "The Nim json library raise a bare exception" - fromJson(blockAsJson, "", genesisBlock) + let blockAsJson = genesisBlockContents.get + genesisBlock = JrpcConv.decode(blockAsJson, BlockObject) except CatchableError as err: error "Failed to load the genesis block from json", err = err.msg diff --git a/research/fakeee.nim b/research/fakeee.nim index 52c955395..dea7e8e30 100644 --- a/research/fakeee.nim +++ b/research/fakeee.nim @@ -77,13 +77,13 @@ proc setupEngineAPI*(server: RpcServer) = status: PayloadExecutionStatus.syncing)) server.rpc("eth_getBlockByNumber") do( - quantityTag: string, fullTransactions: bool) -> JsonNode: + quantityTag: string, fullTransactions: bool) -> JsonString: info "eth_getBlockByNumber", quantityTag, fullTransactions return if quantityTag == "latest": - %BlockObject(number: 1000.Quantity) + JrpcConv.encode(BlockObject(number: 1000.Quantity)).JsonString else: - newJObject() + "{}".JsonString server.rpc("eth_getBlockByHash") do( data: string, fullTransactions: bool) -> BlockObject: diff --git a/vendor/nim-json-rpc b/vendor/nim-json-rpc index 08d7fccfe..a6475e49b 160000 --- a/vendor/nim-json-rpc +++ b/vendor/nim-json-rpc @@ -1 +1 @@ -Subproject commit 08d7fccfe2025d5a2ecca68e6e7b4dd4a7974a8e +Subproject commit a6475e49b26d3afc58aaa3d67621c94eafef8efb diff --git a/vendor/nim-web3 b/vendor/nim-web3 index 45d09b29d..bf1ce869b 160000 --- a/vendor/nim-web3 +++ b/vendor/nim-web3 @@ -1 +1 @@ -Subproject commit 45d09b29d22da63052b259679539a3d4141273a7 +Subproject commit bf1ce869b5f76d9744444b248f6f5c6c782fedc7