forked from enigbe/pbjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bloomfilter.py
42 lines (35 loc) · 1.16 KB
/
bloomfilter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from helper import (
encode_varint,
int_to_little_endian,
murmur3,
bit_field_to_bytes
)
from network import GenericMessage
BIP37_CONSTANT = 0xfba4c795
class BloomFilter:
"""
Attributes and methods for bloom filters
"""
def __init__(self, size: int, function_count: int, tweak: int) -> None:
"""
Instantiates a new bloom filter
"""
self.size = size
self.bit_field = [0] * (size * 8)
self.function_count = function_count
self.tweak = tweak
def add(self, item):
for i in range(self.function_count):
seed = i * BIP37_CONSTANT + self.tweak
h = murmur3(item, seed=seed)
bit = h % (self.size * 8)
self.bit_field[bit] = 1
def filterload(self, flag=1):
payload = encode_varint(self.size)
payload += self.filter_bytes()
payload += int_to_little_endian(self.function_count, 4)
payload += int_to_little_endian(self.tweak, 4)
payload += int_to_little_endian(flag, 1)
return GenericMessage(b'filterload', payload)
def filter_bytes(self):
return bit_field_to_bytes(self.bit_field)