From afeb2c0b93e819d85772648dea31a4fbbd23df08 Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Tue, 5 Feb 2019 16:22:21 +0200 Subject: [PATCH] Moved eth_bloom to eth, some fixes --- eth/bloom.nim | 36 ++++++++++++++++++++++++++++++++++ tests/keyfile/test_keyfile.nim | 2 +- tests/keyfile/test_uuid.nim | 2 +- tests/test_bloom.nim | 17 ++++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 eth/bloom.nim create mode 100644 tests/test_bloom.nim diff --git a/eth/bloom.nim b/eth/bloom.nim new file mode 100644 index 0000000..b4e8231 --- /dev/null +++ b/eth/bloom.nim @@ -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)) diff --git a/tests/keyfile/test_keyfile.nim b/tests/keyfile/test_keyfile.nim index b0c58ec..a67ac82 100644 --- a/tests/keyfile/test_keyfile.nim +++ b/tests/keyfile/test_keyfile.nim @@ -7,7 +7,7 @@ # Apache License, version 2.0, (LICENSE-APACHEv2) # 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 # https://github.com/ethereum/tests/blob/develop/KeyStoreTests/basic_tests.json diff --git a/tests/keyfile/test_uuid.nim b/tests/keyfile/test_uuid.nim index 581de47..f2f013a 100644 --- a/tests/keyfile/test_uuid.nim +++ b/tests/keyfile/test_uuid.nim @@ -7,7 +7,7 @@ # Apache License, version 2.0, (LICENSE-APACHEv2) # MIT license (LICENSE-MIT) -import eth_keyfile/uuid, strutils, unittest +import eth/keyfile/uuid, strutils, unittest suite "Cross-platform UUID test suite": test "Platform UUID check": diff --git a/tests/test_bloom.nim b/tests/test_bloom.nim new file mode 100644 index 0000000..d4561e4 --- /dev/null +++ b/tests/test_bloom.nim @@ -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])