From dffeb1244a6b922ac1f3e15fac70c40fbc46e2a4 Mon Sep 17 00:00:00 2001 From: jangko Date: Sat, 13 Jan 2024 15:39:07 +0700 Subject: [PATCH] Use correct return type for eth_syncing --- tests/helpers/handlers.nim | 4 ++-- web3/conversions.nim | 21 ++++++++++++++++++++- web3/eth_api.nim | 2 +- web3/eth_api_types.nim | 4 ++++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/tests/helpers/handlers.nim b/tests/helpers/handlers.nim index 65f01b1..1f42271 100644 --- a/tests/helpers/handlers.nim +++ b/tests/helpers/handlers.nim @@ -19,8 +19,8 @@ type Hash256 = w3.Hash256 proc installHandlers*(server: RpcServer) = - server.rpc("eth_syncing") do(x: JsonString, ) -> bool: - return false + server.rpc("eth_syncing") do(x: JsonString, ) -> SyncingStatus: + return SyncingStatus(syncing: false) server.rpc("eth_sendRawTransaction") do(x: JsonString, data: seq[byte]) -> TxHash: let tx = rlp.decode(data, Transaction) diff --git a/web3/conversions.nim b/web3/conversions.nim index a061301..2a55770 100644 --- a/web3/conversions.nim +++ b/web3/conversions.nim @@ -291,7 +291,7 @@ proc readValue*(r: var JsonReader[JrpcConv], val: var RtBlockIdentifier) val = RtBlockIdentifier(kind: bidNumber, number: fromHex[uint64](hexStr)) else: val = RtBlockIdentifier(kind: bidAlias, alias: hexStr) - + proc writeValue*(w: var JsonWriter[JrpcConv], v: RtBlockIdentifier) {.gcsafe, raises: [IOError].} = case v.kind @@ -333,6 +333,25 @@ proc writeValue*(w: var JsonWriter[JrpcConv], v: SingleOrList) of slkSingle: w.writeValue(v.single) of slkList: w.writeValue(v.list) +proc readValue*(r: var JsonReader[JrpcConv], val: var SyncingStatus) + {.gcsafe, raises: [IOError, SerializationError].} = + let tok = r.tokKind() + case tok + of JsonValueKind.Bool: + val = SyncingStatus(syncing: r.parseBool()) + of JsonValueKind.Object: + val = SyncingStatus(syncing: true) + r.readValue(val.syncObject) + else: + r.raiseUnexpectedValue("SyncingStatus unexpected token kind =" & $tok) + +proc writeValue*(w: var JsonWriter[JrpcConv], v: SyncingStatus) + {.gcsafe, raises: [IOError].} = + if not v.syncing: + w.writeValue(false) + else: + w.writeValue(v.syncObject) + func `$`*(v: Quantity): string {.inline.} = encodeQuantity(v.uint64) diff --git a/web3/eth_api.nim b/web3/eth_api.nim index 3f78108..fa3ccaa 100644 --- a/web3/eth_api.nim +++ b/web3/eth_api.nim @@ -27,7 +27,7 @@ createRpcSigsFromNim(RpcClient): proc net_peerCount(): Quantity proc net_listening(): bool proc eth_protocolVersion(): string - proc eth_syncing(): SyncObject + proc eth_syncing(): SyncingStatus proc eth_coinbase(): Address proc eth_mining(): bool proc eth_hashrate(): Quantity diff --git a/web3/eth_api_types.nim b/web3/eth_api_types.nim index 507d40d..38c8eba 100644 --- a/web3/eth_api_types.nim +++ b/web3/eth_api_types.nim @@ -20,6 +20,10 @@ type currentBlock*: Quantity highestBlock*: Quantity + SyncingStatus* = object + syncing*: bool + syncObject*: SyncObject + HistoricExtraData* = DynamicBytes[0, 4096] ## In the current specs, the maximum is 32, but historically this value was ## used as Clique metadata which is dynamic in lenght and exceeds 32 bytes.