Move hash* shim to its own module

This commit is contained in:
Eric 2024-02-08 14:54:32 +11:00
parent fae4339ab6
commit f5b26f69c1
No known key found for this signature in database
7 changed files with 53 additions and 44 deletions

View File

@ -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

View File

@ -12,7 +12,6 @@ import ./jsonrpc/rpccalls
import ./jsonrpc/conversions
import ./jsonrpc/subscriptions
export serde
export basics
export provider
export chronicles

View File

@ -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]

View File

@ -1,5 +1,6 @@
import pkg/asynctest
import pkg/ethers
import pkg/serde
import ./hardhat
type

View File

@ -1,5 +1,6 @@
import pkg/asynctest
import pkg/ethers
import pkg/serde
import ./hardhat
type

View File

@ -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":

View File

@ -1,4 +1,5 @@
import pkg/asynctest
import pkg/serde
import pkg/stew/byteutils
import ../ethers