From f5b26f69c1e7988f03b15695277239d36b834698 Mon Sep 17 00:00:00 2001 From: Eric <5089238+emizzle@users.noreply.github.com> Date: Thu, 8 Feb 2024 14:54:32 +1100 Subject: [PATCH] Move hash* shim to its own module --- ethers/nimshims/hashes.nim | 44 ++++++++++++++++++++ ethers/providers/jsonrpc.nim | 1 - ethers/providers/jsonrpc/subscriptions.nim | 48 +++------------------- testmodule/testEnums.nim | 1 + testmodule/testReturns.nim | 1 + testmodule/testTesting.nim | 1 + testmodule/testWallet.nim | 1 + 7 files changed, 53 insertions(+), 44 deletions(-) create mode 100644 ethers/nimshims/hashes.nim diff --git a/ethers/nimshims/hashes.nim b/ethers/nimshims/hashes.nim new file mode 100644 index 0000000..ebe2e3f --- /dev/null +++ b/ethers/nimshims/hashes.nim @@ -0,0 +1,44 @@ +import std/json +import std/hashes +import std/tables + +when (NimMajor) >= 2: + proc hash*[A](x: openArray[A]): Hash {.raises: [].} = + ## Efficient hashing of arrays and sequences. + ## There must be a `hash` proc defined for the element type `A`. + when A is byte: + result = murmurHash(x) + elif A is char: + when nimvm: + result = hashVmImplChar(x, 0, x.high) + else: + result = murmurHash(toOpenArrayByte(x, 0, x.high)) + else: + for a in x: + result = result !& hash(a) + result = !$result + +func hash*(n: OrderedTable[string, JsonNode]): Hash {.raises: [].} + +proc hash*(n: JsonNode): Hash {.noSideEffect, raises: [].} = + ## Compute the hash for a JSON node + case n.kind + of JArray: + result = hash(n.elems) + of JObject: + result = hash(n.fields) + of JInt: + result = hash(n.num) + of JFloat: + result = hash(n.fnum) + of JBool: + result = hash(n.bval.int) + of JString: + result = hash(n.str) + of JNull: + result = Hash(0) + +func hash*(n: OrderedTable[string, JsonNode]): Hash {.raises: [].} = + for key, val in n: + result = result xor (hash(key) !& hash(val)) + result = !$result \ No newline at end of file diff --git a/ethers/providers/jsonrpc.nim b/ethers/providers/jsonrpc.nim index bd2db32..f5390ba 100644 --- a/ethers/providers/jsonrpc.nim +++ b/ethers/providers/jsonrpc.nim @@ -12,7 +12,6 @@ import ./jsonrpc/rpccalls import ./jsonrpc/conversions import ./jsonrpc/subscriptions -export serde export basics export provider export chronicles diff --git a/ethers/providers/jsonrpc/subscriptions.nim b/ethers/providers/jsonrpc/subscriptions.nim index 489e684..a61dfcb 100644 --- a/ethers/providers/jsonrpc/subscriptions.nim +++ b/ethers/providers/jsonrpc/subscriptions.nim @@ -1,15 +1,18 @@ -import std/hashes +# import std/hashes import std/tables import std/sequtils import pkg/chronos import pkg/json_rpc/rpcclient -import pkg/serde +# import pkg/serde import ../../basics import ../../provider +include ../../nimshims/hashes #as hashes_shim import ./rpccalls import ./conversions import ./looping +# export hashes_shim + type JsonRpcSubscriptions* = ref object of RootObj client: RpcClient @@ -49,47 +52,6 @@ proc setMethodHandler( ) = subscriptions.methodHandlers[`method`] = handler -when (NimMajor) >= 2: - proc hash*[A](x: openArray[A]): Hash {.raises: [].} = - ## Efficient hashing of arrays and sequences. - ## There must be a `hash` proc defined for the element type `A`. - when A is byte: - result = murmurHash(x) - elif A is char: - when nimvm: - result = hashVmImplChar(x, 0, x.high) - else: - result = murmurHash(toOpenArrayByte(x, 0, x.high)) - else: - for a in x: - result = result !& hash(a) - result = !$result - -func hash*(n: OrderedTable[string, JsonNode]): Hash {.raises: [].} - -proc hash*(n: JsonNode): Hash {.noSideEffect, raises: [].} = - ## Compute the hash for a JSON node - case n.kind - of JArray: - result = hash(n.elems) - of JObject: - result = hash(n.fields) - of JInt: - result = hash(n.num) - of JFloat: - result = hash(n.fnum) - of JBool: - result = hash(n.bval.int) - of JString: - result = hash(n.str) - of JNull: - result = Hash(0) - -func hash*(n: OrderedTable[string, JsonNode]): Hash {.raises: [].} = - for key, val in n: - result = result xor (hash(key) !& hash(val)) - result = !$result - method subscribeBlocks*(subscriptions: JsonRpcSubscriptions, onBlock: BlockHandler): Future[JsonNode] diff --git a/testmodule/testEnums.nim b/testmodule/testEnums.nim index bc7ec42..3686b93 100644 --- a/testmodule/testEnums.nim +++ b/testmodule/testEnums.nim @@ -1,5 +1,6 @@ import pkg/asynctest import pkg/ethers +import pkg/serde import ./hardhat type diff --git a/testmodule/testReturns.nim b/testmodule/testReturns.nim index 5cdf03b..09d4a60 100644 --- a/testmodule/testReturns.nim +++ b/testmodule/testReturns.nim @@ -1,5 +1,6 @@ import pkg/asynctest import pkg/ethers +import pkg/serde import ./hardhat type diff --git a/testmodule/testTesting.nim b/testmodule/testTesting.nim index 8fec151..fb62ecc 100644 --- a/testmodule/testTesting.nim +++ b/testmodule/testTesting.nim @@ -3,6 +3,7 @@ import pkg/asynctest import pkg/chronos import pkg/ethers import pkg/ethers/testing +import pkg/serde import ./helpers suite "Testing helpers": diff --git a/testmodule/testWallet.nim b/testmodule/testWallet.nim index 703ccf5..a3cdf2d 100644 --- a/testmodule/testWallet.nim +++ b/testmodule/testWallet.nim @@ -1,4 +1,5 @@ import pkg/asynctest +import pkg/serde import pkg/stew/byteutils import ../ethers