Skip to content

Commit

Permalink
blockstore: add abstract and level filter methods
Browse files Browse the repository at this point in the history
  • Loading branch information
braydonf committed Oct 18, 2019
1 parent b68b6ef commit f3e1ceb
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 15 deletions.
46 changes: 46 additions & 0 deletions lib/blockstore/abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ class AbstractBlockStore {
throw new Error('Abstract method.');
}

/**
* This method stores serialized block filter data in files.
* @returns {Promise}
*/

async writeFilter(hash, data) {
throw new Error('Abstract method.');
}

/**
* This method stores block data.
* @returns {Promise}
Expand All @@ -97,6 +106,24 @@ class AbstractBlockStore {
throw new Error('Abstract method.');
}

/**
* This method will retrieve serialized block filter data.
* @returns {Promise}
*/

async readFilter(hash) {
throw new Error('Abstract method.');
}

/**
* This method will retrieve block filter header only.
* @returns {Promise}
*/

async readFilterHeader(hash) {
throw new Error('Abstract method.');
}

/**
* This method will retrieve block undo coin data.
* @returns {Promise}
Expand Down Expand Up @@ -134,6 +161,15 @@ class AbstractBlockStore {
throw new Error('Abstract method.');
}

/**
* This will free resources for storing the serialized block filter data.
* @returns {Promise}
*/

async pruneFilter(hash) {
throw new Error('Abstract method.');
}

/**
* This will free resources for storing the block data.
* @returns {Promise}
Expand Down Expand Up @@ -163,6 +199,16 @@ class AbstractBlockStore {
throw new Error('Abstract method.');
}

/**
* This will check if a block filter has been stored
* and is available.
* @returns {Promise}
*/

async hasFilter(hash) {
throw new Error('Abstract method.');
}

/**
* This will check if a block has been stored and is available.
* @returns {Promise}
Expand Down
13 changes: 12 additions & 1 deletion lib/blockstore/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ class FileBlockStore extends AbstractBlockStore {
* @returns {Promise}
*/

async readHeader(hash) {
async readFilterHeader(hash) {
return this._read(types.FILTER, hash, 0, 32);
}

Expand Down Expand Up @@ -659,6 +659,17 @@ class FileBlockStore extends AbstractBlockStore {
return await this.db.has(layout.b.encode(types.UNDO, hash));
}

/**
* This will check if a block filter has been stored
* and is available.
* @param {Buffer} hash - The block hash
* @returns {Promise}
*/

async hasFilter(hash) {
return await this.db.has(layout.b.encode(types.FILTER, hash));
}

/**
* This will check if a block has been stored and is available.
* @param {Buffer} hash - The block hash
Expand Down
62 changes: 62 additions & 0 deletions lib/blockstore/level.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ class LevelBlockStore extends AbstractBlockStore {
return this.db.put(layout.b.encode(types.BLOCK, hash), data);
}

/**
* This method stores serialized block filter data in LevelDB.
* @param {Buffer} hash - The block hash
* @param {Buffer} data - The serialized block filter data.
* @returns {Promise}
*/

async writeFilter(hash, data) {
return this.db.put(layout.b.encode(types.FILTER, hash), data);
}

/**
* This method will retrieve merkle block data.
* @param {Buffer} hash - The block hash
Expand All @@ -123,6 +134,31 @@ class LevelBlockStore extends AbstractBlockStore {
return this.db.get(layout.b.encode(types.UNDO, hash));
}

/**
* This method will retrieve serialized block filter data.
* @param {Buffer} hash - The block hash
* @returns {Promise}
*/

async readFilter(hash) {
return this.db.get(layout.b.encode(types.FILTER, hash));
}

/**
* This method will retrieve block filter header only.
* @param {Buffer} hash - The block hash
* @returns {Promise}
*/

async readFilterHeader(hash) {
const data = await this.db.get(layout.b.encode(types.FILTER, hash));

if (!data)
return null;

return data.slice(0, 32);
}

/**
* This method will retrieve block data. Smaller portions of the
* block (e.g. transactions) can be returned using the offset and
Expand Down Expand Up @@ -181,6 +217,21 @@ class LevelBlockStore extends AbstractBlockStore {
return true;
}

/**
* This will free resources for storing the serialized block filter data.
* @param {Buffer} hash - The block hash
* @returns {Promise}
*/

async pruneFilter(hash) {
if (!await this.hasFilter(hash))
return false;

await this.db.del(layout.b.encode(types.FILTER, hash));

return true;
}

/**
* This will free resources for storing the block data. The block
* data may not be immediately removed from disk, and will be reclaimed
Expand Down Expand Up @@ -220,6 +271,17 @@ class LevelBlockStore extends AbstractBlockStore {
return this.db.has(layout.b.encode(types.UNDO, hash));
}

/**
* This will check if a block filter has been stored
* and is available.
* @param {Buffer} hash - The block hash
* @returns {Promise}
*/

async hasFilter(hash) {
return this.db.has(layout.b.encode(types.FILTER, hash));
}

/**
* This will check if a block has been stored and is available.
* @param {Buffer} hash - The block hash
Expand Down
2 changes: 1 addition & 1 deletion lib/indexer/filterindexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class FilterIndexer extends Indexer {
async getFilterHeader(hash) {
assert(hash);

return this.blocks.readHeader(hash);
return this.blocks.readFilterHeader(hash);
}
}

Expand Down
32 changes: 19 additions & 13 deletions test/blockstore-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,30 @@ describe('BlockStore', function() {
});

it('has unimplemented base methods', async () => {
const methods = ['open', 'close', 'write', 'writeUndo',
'writeMerkle', 'read', 'readUndo', 'readMerkle',
'prune', 'pruneUndo', 'pruneMerkle',
'has', 'hasUndo', 'hasMerkle', 'ensure'];
const groups = {
base: ['open', 'close', 'ensure'],
block: ['write', 'read', 'prune', 'has'],
undo: ['writeUndo', 'readUndo', 'pruneUndo', 'hasUndo'],
filter: ['writeFilter', 'readFilter', 'readFilterHeader',
'pruneFilter', 'hasFilter'],
merkle: ['writeMerkle', 'readMerkle', 'pruneMerkle', 'hasMerkle']
};

const store = new AbstractBlockStore();

for (const method of methods) {
assert(store[method]);
for (const methods of Object.values(groups)) {
for (const method of methods) {
assert(store[method]);

let err = null;
try {
await store[method]();
} catch (e) {
err = e;
let err = null;
try {
await store[method]();
} catch (e) {
err = e;
}
assert(err, `Expected unimplemented method ${method}.`);
assert.equal(err.message, 'Abstract method.');
}
assert(err, `Expected unimplemented method ${method}.`);
assert.equal(err.message, 'Abstract method.');
}
});
});
Expand Down

0 comments on commit f3e1ceb

Please sign in to comment.