mirror of
https://github.com/logos-storage/nim-ethers.git
synced 2026-01-24 08:23:13 +00:00
Move hash* shim to its own module
This commit is contained in:
parent
fae4339ab6
commit
f5b26f69c1
44
ethers/nimshims/hashes.nim
Normal file
44
ethers/nimshims/hashes.nim
Normal 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
|
||||
@ -12,7 +12,6 @@ import ./jsonrpc/rpccalls
|
||||
import ./jsonrpc/conversions
|
||||
import ./jsonrpc/subscriptions
|
||||
|
||||
export serde
|
||||
export basics
|
||||
export provider
|
||||
export chronicles
|
||||
|
||||
@ -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]
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import pkg/asynctest
|
||||
import pkg/ethers
|
||||
import pkg/serde
|
||||
import ./hardhat
|
||||
|
||||
type
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import pkg/asynctest
|
||||
import pkg/ethers
|
||||
import pkg/serde
|
||||
import ./hardhat
|
||||
|
||||
type
|
||||
|
||||
@ -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":
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import pkg/asynctest
|
||||
import pkg/serde
|
||||
import pkg/stew/byteutils
|
||||
import ../ethers
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user