2021-06-28 07:48:36 +02:00
|
|
|
# external imports
|
|
|
|
import sha3
|
|
|
|
|
|
|
|
|
|
|
|
class LogBloom:
|
2021-08-21 09:27:40 +02:00
|
|
|
"""Helper for Ethereum receipt log bloom filters.
|
|
|
|
"""
|
2021-06-28 07:48:36 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.content = bytearray(256)
|
|
|
|
|
|
|
|
|
|
|
|
def add(self, element):
|
2021-08-21 09:27:40 +02:00
|
|
|
"""Add topic element to filter.
|
|
|
|
|
|
|
|
:param element: Topic element
|
|
|
|
:type element: bytes
|
|
|
|
"""
|
2021-06-28 07:48:36 +02:00
|
|
|
if not isinstance(element, bytes):
|
|
|
|
raise ValueError('element must be bytes')
|
|
|
|
h = sha3.keccak_256()
|
|
|
|
h.update(element)
|
|
|
|
z = h.digest()
|
|
|
|
|
|
|
|
for j in range(3):
|
|
|
|
c = j * 2
|
|
|
|
v = int.from_bytes(z[c:c+2], byteorder='big')
|
|
|
|
v &= 0x07ff
|
|
|
|
m = 255 - int(v / 8)
|
|
|
|
n = v % 8
|
|
|
|
self.content[m] |= (1 << n)
|