From 899c0f7135a563b875ff1386e3f6755b6e3b31d6 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Fri, 27 Sep 2024 19:27:54 +0200 Subject: [PATCH] bloom: fix missing keccak --- eth/bloom.nim | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/eth/bloom.nim b/eth/bloom.nim index 7caa46bc..c3e0d62e 100644 --- a/eth/bloom.nim +++ b/eth/bloom.nim @@ -1,8 +1,8 @@ -import stint, ./common/[base, hashes] +import stint, ./common/[addresses, base, hashes] type UInt2048 = StUint[2048] -iterator chunksForBloom(h: Hash32|Bytes32): array[2, uint8] = +iterator chunksForBloom(h: Hash32): array[2, uint8] = yield [h.data[0], h.data[1]] yield [h.data[2], h.data[3]] yield [h.data[4], h.data[5]] @@ -12,28 +12,34 @@ proc chunkToBloomBits(chunk: array[2, uint8]): UInt2048 = let l = chunk[1].int one(UInt2048) shl ((l + (h shl 8)) and 2047) -iterator bloomBits(h: Hash32|Bytes32): UInt2048 = +iterator bloomBits(h: Hash32): UInt2048 = for chunk in chunksForBloom(h): yield chunkToBloomBits(chunk) type BloomFilter* = object value*: UInt2048 -proc incl*(f: var BloomFilter, h: Hash32|Bytes32) = +proc incl*(f: var BloomFilter, h: Hash32) = for bits in bloomBits(h): f.value = f.value or bits -proc init*(_: type BloomFilter, h: Hash32|Bytes32): BloomFilter = +proc init*(_: type BloomFilter, h: Hash32): BloomFilter = result.incl(h) -# 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(v)) -proc incl*(f: var BloomFilter, v: openArray[byte]) = f.incl(keccak256(v)) +proc incl*[T: byte|char](f: var BloomFilter, v: openArray[T]) = + f.incl(keccak256(v)) -proc contains*(f: BloomFilter, h: Hash32|Bytes32): bool = +proc incl*(f: var BloomFilter, v: Address | Bytes32) = + f.incl(v.data) + +proc contains*(f: BloomFilter, h: Hash32): bool = for bits in bloomBits(h): - if (f.value and bits).isZero: return false + if (f.value and bits).isZero: + return false return true template contains*(f: BloomFilter, v: openArray): bool = f.contains(keccak256(v)) + +proc contains*(f: BloomFilter, v: Address | Bytes32): bool = + f.contains(v.data)