diff --git a/lib/bloom.js b/lib/bloom.js index 9b506161dd..2cf25af99d 100644 --- a/lib/bloom.js +++ b/lib/bloom.js @@ -1,4 +1,3 @@ -const Buffer = require('safe-buffer').Buffer const assert = require('assert') const utils = require('ethereumjs-util') const byteSize = 256 @@ -77,9 +76,6 @@ Bloom.prototype.check = function (e) { Bloom.prototype.multiCheck = function (topics) { var self = this return topics.every(function (t) { - if (!Buffer.isBuffer(t)) { - t = Buffer.from(t, 'hex') - } return self.check(t) }) } diff --git a/tests/api/bloom.js b/tests/api/bloom.js new file mode 100644 index 0000000000..f52a061748 --- /dev/null +++ b/tests/api/bloom.js @@ -0,0 +1,76 @@ +const tape = require('tape') +const Bloom = require('../../lib/bloom') +const utils = require('ethereumjs-util') + +const byteSize = 256 + +tape('bloom', (t) => { + t.test('should initialize without params', (st) => { + const b = new Bloom() + st.deepEqual(b.bitvector, utils.zeros(byteSize), 'should be empty') + st.end() + }) + + t.test('shouldnt initialize with invalid bitvector', (st) => { + st.throws(() => new Bloom('invalid'), /AssertionError/, 'should fail for invalid type') + st.throws(() => new Bloom(utils.zeros(byteSize / 2), /AssertionError/), 'should fail for invalid length') + st.end() + }) + + t.test('should contain values of hardcoded bitvector', (st) => { + const hex = '00000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000' + const vector = Buffer.from(hex, 'hex') + + const b = new Bloom(vector) + st.true(b.check('value 1'), 'should contain string "value 1"') + st.true(b.check('value 2'), 'should contain string "value 2"') + st.end() + }) + + t.test('check shouldnt be tautology', (st) => { + const b = new Bloom() + st.false(b.check('random value'), 'should not contain string "random value"') + st.end() + }) + + t.test('should correctly add value', (st) => { + const b = new Bloom() + b.add('value') + let found = b.check('value') + st.true(found, 'should contain added value') + st.end() + }) + + t.test('should check multiple values', (st) => { + const b = new Bloom() + b.add('value 1') + b.add('value 2') + let found = b.multiCheck(['value 1', 'value 2']) + st.true(found, 'should contain both values') + st.end() + }) + + t.test('should or two filters', (st) => { + const b1 = new Bloom() + b1.add('value 1') + const b2 = new Bloom() + b2.add('value 2') + + b1.or(b2) + st.true(b1.check('value 2'), 'should contain "value 2" after or') + st.end() + }) + + t.test('should generate the correct bloom filter value', (st) => { + let bloom = new Bloom() + bloom.add(Buffer.from('1d7022f5b17d2f8b695918fb48fa1089c9f85401', 'hex')) + bloom.add(Buffer.from('8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925', 'hex')) + bloom.add(Buffer.from('0000000000000000000000005409ed021d9299bf6814279a6a1411a7e866a631', 'hex')) + bloom.add(Buffer.from('0000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c48', 'hex')) + st.equal( + bloom.bitvector.toString('hex'), + '00000000000000000000080000800000000000000000000000000000000000000000000000000000000000000000000000000000000004000000080000200000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000010008000000000000000002000000000000000000000000008000000000000' + ) + st.end() + }) +}) diff --git a/tests/bloomTest.js b/tests/bloomTest.js deleted file mode 100644 index c03a3fbd89..0000000000 --- a/tests/bloomTest.js +++ /dev/null @@ -1,16 +0,0 @@ -const tape = require('tape') -const Bloom = require('../lib/bloom.js') -tape('test the bloom filter', function (t) { - t.test('should generate the correct bloom filter value ', function (st) { - let bloom = new Bloom() - bloom.add(Buffer.from('1d7022f5b17d2f8b695918fb48fa1089c9f85401', 'hex')) - bloom.add(Buffer.from('8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925', 'hex')) - bloom.add(Buffer.from('0000000000000000000000005409ed021d9299bf6814279a6a1411a7e866a631', 'hex')) - bloom.add(Buffer.from('0000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c48', 'hex')) - t.equal( - bloom.bitvector.toString('hex'), - '00000000000000000000080000800000000000000000000000000000000000000000000000000000000000000000000000000000000004000000080000200000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000010008000000000000000002000000000000000000000000008000000000000' - ) - t.end() - }) -}) diff --git a/tests/tester.js b/tests/tester.js index dd0211cb7c..ae0368112d 100755 --- a/tests/tester.js +++ b/tests/tester.js @@ -248,7 +248,6 @@ function runTests (name, runnerArgs, cb) { function runAll () { require('./tester.js') require('./cacheTest.js') - require('./bloomTest.js') require('./genesishashes.js') async.series([ // runTests.bind(this, 'VMTests', {}), // VM tests disabled since we don't support Frontier gas costs