mirror of https://github.com/status-im/nim-eth.git
Moved eth_bloom to eth, some fixes
This commit is contained in:
parent
d0fa1344e5
commit
afeb2c0b93
|
@ -0,0 +1,36 @@
|
||||||
|
import stint, nimcrypto
|
||||||
|
|
||||||
|
type UInt2048 = StUint[2048]
|
||||||
|
|
||||||
|
iterator chunksForBloom(h: MDigest[256]): array[2, uint8] =
|
||||||
|
yield [h.data[0], h.data[1]]
|
||||||
|
yield [h.data[2], h.data[3]]
|
||||||
|
yield [h.data[4], h.data[5]]
|
||||||
|
|
||||||
|
proc chunkToBloomBits(chunk: array[2, uint8]): UInt2048 =
|
||||||
|
let h = chunk[0].int
|
||||||
|
let l = chunk[1].int
|
||||||
|
one(UInt2048) shl ((l + (h shl 8)) and 2047)
|
||||||
|
|
||||||
|
iterator bloomBits(h: MDigest[256]): UInt2048 =
|
||||||
|
for chunk in chunksForBloom(h):
|
||||||
|
yield chunkToBloomBits(chunk)
|
||||||
|
|
||||||
|
type BloomFilter* = object
|
||||||
|
value*: UInt2048
|
||||||
|
|
||||||
|
proc incl(f: var BloomFilter, h: MDigest[256]) = # Should this be public?
|
||||||
|
for bits in bloomBits(h):
|
||||||
|
f.value = f.value or bits
|
||||||
|
|
||||||
|
# TODO: The following 2 procs should be one genric, but it doesn't compile. Nim bug?
|
||||||
|
proc incl*(f: var BloomFilter, v: string) = f.incl(keccak256.digest(v))
|
||||||
|
proc incl*(f: var BloomFilter, v: openarray[byte]) = f.incl(keccak256.digest(v))
|
||||||
|
|
||||||
|
proc contains(f: BloomFilter, h: MDigest[256]): bool = # Should this be public?
|
||||||
|
for bits in bloomBits(h):
|
||||||
|
if (f.value and bits).isZero: return false
|
||||||
|
return true
|
||||||
|
|
||||||
|
template contains*[T](f: BloomFilter, v: openarray[T]): bool =
|
||||||
|
f.contains(keccak256.digest(v))
|
|
@ -7,7 +7,7 @@
|
||||||
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
||||||
# MIT license (LICENSE-MIT)
|
# MIT license (LICENSE-MIT)
|
||||||
|
|
||||||
import eth_keys, eth_keyfile/[uuid, keyfile], json, strutils, os, unittest
|
import eth/keys, eth/keyfile/[uuid, keyfile], json, strutils, os, unittest
|
||||||
|
|
||||||
# Test vectors copied from
|
# Test vectors copied from
|
||||||
# https://github.com/ethereum/tests/blob/develop/KeyStoreTests/basic_tests.json
|
# https://github.com/ethereum/tests/blob/develop/KeyStoreTests/basic_tests.json
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
||||||
# MIT license (LICENSE-MIT)
|
# MIT license (LICENSE-MIT)
|
||||||
|
|
||||||
import eth_keyfile/uuid, strutils, unittest
|
import eth/keyfile/uuid, strutils, unittest
|
||||||
|
|
||||||
suite "Cross-platform UUID test suite":
|
suite "Cross-platform UUID test suite":
|
||||||
test "Platform UUID check":
|
test "Platform UUID check":
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
import eth/bloom, stint, unittest
|
||||||
|
|
||||||
|
suite "Simple Bloom Filter tests":
|
||||||
|
test "incl proc":
|
||||||
|
var f: BloomFilter
|
||||||
|
f.incl("hello")
|
||||||
|
check "hello" in f
|
||||||
|
check "hello1" notin f
|
||||||
|
f.incl("hello1")
|
||||||
|
check "hello" in f
|
||||||
|
check "hello1" in f
|
||||||
|
|
||||||
|
test "int value":
|
||||||
|
var f: BloomFilter
|
||||||
|
f.incl("value 1")
|
||||||
|
f.incl("value 2")
|
||||||
|
check f.value == parse("1090215279796298345343057319992441901006450066263950115824040002588950485497113027143927523755823134941133023716890165043342811041924870874305880232180990464248298835944719578227183672673286106858273952584661686762419935928160959430409028732374024192153399763277382459194254234587232383494962731940352290891816707697788111127980409605093135659121120897102645250001200507634146244124778321795865777525978540960830042468420173693965828992647991129039043403835835590424035347457188427354145120006479590726476620907513681178254852999008485376", StUint[2048])
|
Loading…
Reference in New Issue