log.py (726B)
1 # external imports 2 import sha3 3 4 5 class LogBloom: 6 """Helper for Ethereum receipt log bloom filters. 7 """ 8 def __init__(self): 9 self.content = bytearray(256) 10 11 12 def add(self, element): 13 """Add topic element to filter. 14 15 :param element: Topic element 16 :type element: bytes 17 """ 18 if not isinstance(element, bytes): 19 raise ValueError('element must be bytes') 20 h = sha3.keccak_256() 21 h.update(element) 22 z = h.digest() 23 24 for j in range(3): 25 c = j * 2 26 v = int.from_bytes(z[c:c+2], byteorder='big') 27 v &= 0x07ff 28 m = 255 - int(v / 8) 29 n = v % 8 30 self.content[m] |= (1 << n)