forked from jimmysong/programmingbitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbloomfilter.py
More file actions
69 lines (56 loc) · 2.05 KB
/
bloomfilter.py
File metadata and controls
69 lines (56 loc) · 2.05 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from unittest import TestCase
from helper import (
bit_field_to_bytes,
encode_varint,
int_to_little_endian,
murmur3,
)
from network import GenericMessage
BIP37_CONSTANT = 0xfba4c795
# tag::source1[]
class BloomFilter:
def __init__(self, size, function_count, tweak):
self.size = size
self.bit_field = [0] * (size * 8)
self.function_count = function_count
self.tweak = tweak
# end::source1[]
def add(self, item):
'''Add an item to the filter'''
# iterate self.function_count number of times
# BIP0037 spec seed is i*BIP37_CONSTANT + self.tweak
# get the murmur3 hash given that seed
# set the bit at the hash mod the bitfield size (self.size*8)
# set the bit field at bit to be 1
raise NotImplementedError
def filter_bytes(self):
return bit_field_to_bytes(self.bit_field)
def filterload(self, flag=1):
'''Return the filterload message'''
# start the payload with the size of the filter in bytes
# next add the bit field using self.filter_bytes()
# function count is 4 bytes little endian
# tweak is 4 bytes little endian
# flag is 1 byte little endian
# return a GenericMessage whose command is b'filterload'
# and payload is what we've calculated
raise NotImplementedError
class BloomFilterTest(TestCase):
def test_add(self):
bf = BloomFilter(10, 5, 99)
item = b'Hello World'
bf.add(item)
expected = '0000000a080000000140'
self.assertEqual(bf.filter_bytes().hex(), expected)
item = b'Goodbye!'
bf.add(item)
expected = '4000600a080000010940'
self.assertEqual(bf.filter_bytes().hex(), expected)
def test_filterload(self):
bf = BloomFilter(10, 5, 99)
item = b'Hello World'
bf.add(item)
item = b'Goodbye!'
bf.add(item)
expected = '0a4000600a080000010940050000006300000001'
self.assertEqual(bf.filterload().serialize().hex(), expected)