Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Tests for the bloom module #330

Merged
merged 5 commits into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions lib/bloom.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const Buffer = require('safe-buffer').Buffer
const assert = require('assert')
const utils = require('ethereumjs-util')
const byteSize = 256
Expand Down Expand Up @@ -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)
})
}
Expand Down
76 changes: 76 additions & 0 deletions tests/api/bloom.js
Original file line number Diff line number Diff line change
@@ -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()
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.


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()
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, looks good.


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()
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.


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()
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Phew, the whole usage of types (string vs Buffer) in the Bloom implementation respectively the corresponding documentation is really a mess: but right, this is exactly what the tests you have written are for to discover! 😄 I've opened an according issue #332.

Within the current implementation the Buffer conversion in the Bloom.multiCheck() method has to be removed, opened the following issue on this: #333

Re-architecturing whole parts of the library definitely doesn't have to be part of the scope of the work you are doing here, it's already great that we've discovered this now.

If you want you can just comment out this test, then we can re-add once the issue from above is fixed. I would then approve this part of your PR series.


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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

})
})
16 changes: 0 additions & 16 deletions tests/bloomTest.js

This file was deleted.

1 change: 0 additions & 1 deletion tests/tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down