|
1 | 1 | const assert = require('assert')
|
2 | 2 | const utils = require('ethereumjs-util')
|
3 |
| -const byteSize = 256 |
4 | 3 |
|
5 |
| -/** |
6 |
| - * Represents a Bloom |
7 |
| - * @constructor |
8 |
| - * @param {Buffer} bitvector |
9 |
| - */ |
10 |
| -var Bloom = module.exports = function (bitvector) { |
11 |
| - if (!bitvector) { |
12 |
| - this.bitvector = utils.zeros(byteSize) |
13 |
| - } else { |
14 |
| - assert(bitvector.length === byteSize, 'bitvectors must be 2048 bits long') |
15 |
| - this.bitvector = bitvector |
| 4 | +const BYTE_SIZE = 256 |
| 5 | + |
| 6 | +module.exports = class Bloom { |
| 7 | + /** |
| 8 | + * Represents a Bloom |
| 9 | + * @constructor |
| 10 | + * @param {Buffer} bitvector |
| 11 | + */ |
| 12 | + constructor (bitvector) { |
| 13 | + if (!bitvector) { |
| 14 | + this.bitvector = utils.zeros(BYTE_SIZE) |
| 15 | + } else { |
| 16 | + assert(bitvector.length === BYTE_SIZE, 'bitvectors must be 2048 bits long') |
| 17 | + this.bitvector = bitvector |
| 18 | + } |
16 | 19 | }
|
17 |
| -} |
18 | 20 |
|
19 |
| -/** |
20 |
| - * adds an element to a bit vector of a 64 byte bloom filter |
21 |
| - * @method add |
22 |
| - * @param {Buffer} e the element to add |
23 |
| - */ |
24 |
| -Bloom.prototype.add = function (e) { |
25 |
| - e = utils.keccak256(e) |
26 |
| - var mask = 2047 // binary 11111111111 |
| 21 | + /** |
| 22 | + * adds an element to a bit vector of a 64 byte bloom filter |
| 23 | + * @method add |
| 24 | + * @param {Buffer} e the element to add |
| 25 | + */ |
| 26 | + add (e) { |
| 27 | + e = utils.keccak256(e) |
| 28 | + const mask = 2047 // binary 11111111111 |
27 | 29 |
|
28 |
| - for (var i = 0; i < 3; i++) { |
29 |
| - var first2bytes = e.readUInt16BE(i * 2) |
30 |
| - var loc = mask & first2bytes |
31 |
| - var byteLoc = loc >> 3 |
32 |
| - var bitLoc = 1 << loc % 8 |
33 |
| - this.bitvector[byteSize - byteLoc - 1] |= bitLoc |
| 30 | + for (let i = 0; i < 3; i++) { |
| 31 | + const first2bytes = e.readUInt16BE(i * 2) |
| 32 | + const loc = mask & first2bytes |
| 33 | + const byteLoc = loc >> 3 |
| 34 | + const bitLoc = 1 << loc % 8 |
| 35 | + this.bitvector[BYTE_SIZE - byteLoc - 1] |= bitLoc |
| 36 | + } |
34 | 37 | }
|
35 |
| -} |
36 | 38 |
|
37 |
| -/** |
38 |
| - * checks if an element is in the bloom |
39 |
| - * @method check |
40 |
| - * @param {Buffer} e the element to check |
41 |
| - * @returns {boolean} Returns {@code true} if the element is in the bloom |
42 |
| - */ |
43 |
| -Bloom.prototype.check = function (e) { |
44 |
| - e = utils.keccak256(e) |
45 |
| - var mask = 2047 // binary 11111111111 |
46 |
| - var match = true |
| 39 | + /** |
| 40 | + * checks if an element is in the bloom |
| 41 | + * @method check |
| 42 | + * @param {Buffer} e the element to check |
| 43 | + * @returns {boolean} Returns {@code true} if the element is in the bloom |
| 44 | + */ |
| 45 | + check (e) { |
| 46 | + e = utils.keccak256(e) |
| 47 | + const mask = 2047 // binary 11111111111 |
| 48 | + let match = true |
47 | 49 |
|
48 |
| - for (var i = 0; i < 3 && match; i++) { |
49 |
| - var first2bytes = e.readUInt16BE(i * 2) |
50 |
| - var loc = mask & first2bytes |
51 |
| - var byteLoc = loc >> 3 |
52 |
| - var bitLoc = 1 << loc % 8 |
53 |
| - match = (this.bitvector[byteSize - byteLoc - 1] & bitLoc) |
54 |
| - } |
| 50 | + for (let i = 0; i < 3 && match; i++) { |
| 51 | + const first2bytes = e.readUInt16BE(i * 2) |
| 52 | + const loc = mask & first2bytes |
| 53 | + const byteLoc = loc >> 3 |
| 54 | + const bitLoc = 1 << loc % 8 |
| 55 | + match = (this.bitvector[BYTE_SIZE - byteLoc - 1] & bitLoc) |
| 56 | + } |
55 | 57 |
|
56 |
| - return Boolean(match) |
57 |
| -} |
| 58 | + return Boolean(match) |
| 59 | + } |
58 | 60 |
|
59 |
| -/** |
60 |
| - * checks if multiple topics are in a bloom |
61 |
| - * @method multiCheck |
62 |
| - * @param {Buffer} topics |
63 |
| - * @returns {boolean} Returns {@code true} if every topic is in the bloom |
64 |
| - */ |
65 |
| -Bloom.prototype.multiCheck = function (topics) { |
66 |
| - var self = this |
67 |
| - return topics.every(function (t) { |
68 |
| - return self.check(t) |
69 |
| - }) |
70 |
| -} |
| 61 | + /** |
| 62 | + * checks if multiple topics are in a bloom |
| 63 | + * @method multiCheck |
| 64 | + * @param {Buffer} topics |
| 65 | + * @returns {boolean} Returns {@code true} if every topic is in the bloom |
| 66 | + */ |
| 67 | + multiCheck (topics) { |
| 68 | + return topics.every((t) => this.check(t)) |
| 69 | + } |
71 | 70 |
|
72 |
| -/** |
73 |
| - * bitwise or blooms together |
74 |
| - * @method or |
75 |
| - * @param {Bloom} bloom |
76 |
| - */ |
77 |
| -Bloom.prototype.or = function (bloom) { |
78 |
| - if (bloom) { |
79 |
| - for (var i = 0; i <= byteSize; i++) { |
80 |
| - this.bitvector[i] = this.bitvector[i] | bloom.bitvector[i] |
| 71 | + /** |
| 72 | + * bitwise or blooms together |
| 73 | + * @method or |
| 74 | + * @param {Bloom} bloom |
| 75 | + */ |
| 76 | + or (bloom) { |
| 77 | + if (bloom) { |
| 78 | + for (let i = 0; i <= BYTE_SIZE; i++) { |
| 79 | + this.bitvector[i] = this.bitvector[i] | bloom.bitvector[i] |
| 80 | + } |
81 | 81 | }
|
82 | 82 | }
|
83 | 83 | }
|
0 commit comments