Skip to content

Commit

Permalink
Merge PR #902 from 'nodech/update-types'
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Oct 2, 2024
2 parents 5294be7 + 0ce3197 commit 45c6ac1
Show file tree
Hide file tree
Showing 70 changed files with 1,645 additions and 857 deletions.
3 changes: 1 addition & 2 deletions lib/blockchain/chaindb.js
Original file line number Diff line number Diff line change
Expand Up @@ -1177,9 +1177,8 @@ class ChainDB {

/**
* Get a coin (unspents only).
* @private
* @param {Outpoint} prevout
* @returns {Promise} - Returns {@link CoinEntry}.
* @returns {Promise<CoinEntry>} - Returns {@link CoinEntry}.
*/

async readCoin(prevout) {
Expand Down
25 changes: 13 additions & 12 deletions lib/blockstore/abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class AbstractBlockStore {
/**
* Create an abstract blockstore.
* @constructor
* @param {Object} [options]
*/

constructor(options) {
Expand Down Expand Up @@ -192,7 +193,7 @@ class AbstractBlockStore {

/**
* Create batch.
* @returns {Batch}
* @returns {AbstractBatch}
*/

batch() {
Expand All @@ -206,7 +207,6 @@ class AbstractBlockStore {
* @abstract
*/

// eslint-disable-next-line no-unused-vars
class AbstractBatch {
/**
* Create AbstractBatch.
Expand All @@ -218,9 +218,9 @@ class AbstractBatch {

/**
* Write merkle block data to the batch.
* @property {Buffer} hash
* @property {Buffer} data
* @returns {Batch}
* @param {Buffer} hash
* @param {Buffer} data
* @returns {this}
*/

writeMerkle(hash, data) {
Expand All @@ -231,7 +231,7 @@ class AbstractBatch {
* Write undo coin data to the batch.
* @param {Buffer} hash
* @param {Buffer} data
* @returns {Batch}
* @returns {this}
*/

writeUndo(hash, data) {
Expand All @@ -242,7 +242,7 @@ class AbstractBatch {
* Write block data to the batch.
* @param {Buffer} hash
* @param {Buffer} data
* @returns {Batch}
* @returns {this}
*/

writeBlock(hash, data) {
Expand All @@ -252,7 +252,7 @@ class AbstractBatch {
/**
* Remove merkle block data from the batch.
* @param {Buffer} hash
* @returns {Batch}
* @returns {this}
*/

pruneMerkle(hash) {
Expand All @@ -262,7 +262,7 @@ class AbstractBatch {
/**
* Remove undo data from the batch.
* @param {Buffer} hash
* @returns {Batch}
* @returns {this}
*/

pruneUndo(hash) {
Expand All @@ -272,7 +272,7 @@ class AbstractBatch {
/**
* Prune block data from the batch.
* @param {Buffer} hash
* @returns {Batch}
* @returns {this}
*/

pruneBlock(hash) {
Expand All @@ -281,7 +281,7 @@ class AbstractBatch {

/**
* Clear the batch.
* @returns {Batch}
* @returns {this}
*/

clear() {
Expand All @@ -293,7 +293,7 @@ class AbstractBatch {
* @returns {Promise}
*/

write() {
commit() {
throw new Error('Abstract method.');
}
}
Expand All @@ -302,4 +302,5 @@ class AbstractBatch {
* Expose
*/

AbstractBlockStore.AbstractBatch = AbstractBatch;
module.exports = AbstractBlockStore;
63 changes: 38 additions & 25 deletions lib/blockstore/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const Network = require('../protocol/network');
const consensus = require('../protocol/consensus');
const Headers = require('../primitives/headers');
const AbstractBlockStore = require('./abstract');
const {AbstractBatch} = AbstractBlockStore;
const {BlockRecord, FileRecord} = require('./records');
const layout = require('./layout');
const {types, prefixes} = require('./common');
Expand All @@ -33,6 +34,7 @@ class FileBlockStore extends AbstractBlockStore {
/**
* Create a blockstore that stores blocks in files.
* @constructor
* @param {Object} [options]
*/

constructor(options) {
Expand Down Expand Up @@ -234,7 +236,7 @@ class FileBlockStore extends AbstractBlockStore {
* @private
* @param {Number} type - The type of block data
* @param {Number} fileno - The number of the file.
* @returns {Promise}
* @returns {String}
*/

filepath(type, fileno) {
Expand Down Expand Up @@ -266,7 +268,7 @@ class FileBlockStore extends AbstractBlockStore {
* @private
* @param {Number} type - The type of block data
* @param {Number} length - The number of bytes
* @returns {Promise}
* @returns {Promise<Object>}
*/

async allocate(type, length) {
Expand Down Expand Up @@ -321,7 +323,7 @@ class FileBlockStore extends AbstractBlockStore {
* This method stores merkle block data in files.
* @param {Buffer} hash - The block hash
* @param {Buffer} data - The block data
* @returns {Promise}
* @returns {Promise<Boolean>}
*/

async writeMerkle(hash, data) {
Expand All @@ -332,7 +334,7 @@ class FileBlockStore extends AbstractBlockStore {
* This method stores block undo coin data in files.
* @param {Buffer} hash - The block hash
* @param {Buffer} data - The block data
* @returns {Promise}
* @returns {Promise<Boolean>}
*/

async writeUndo(hash, data) {
Expand All @@ -343,7 +345,7 @@ class FileBlockStore extends AbstractBlockStore {
* This method stores block data in files.
* @param {Buffer} hash - The block hash
* @param {Buffer} data - The block data
* @returns {Promise}
* @returns {Promise<Boolean>}
*/

async writeBlock(hash, data) {
Expand All @@ -358,7 +360,7 @@ class FileBlockStore extends AbstractBlockStore {
* @param {Number} type - The type of block data
* @param {Buffer} hash - The block hash
* @param {Buffer} data - The block data
* @returns {Promise}
* @returns {Promise<Boolean>} - Whether the data was written.
*/

async _write(type, hash, data) {
Expand Down Expand Up @@ -451,7 +453,7 @@ class FileBlockStore extends AbstractBlockStore {
/**
* This method will retrieve merkle block data.
* @param {Buffer} hash - The block hash
* @returns {Promise}
* @returns {Promise<Buffer>}
*/

async readMerkle(hash) {
Expand All @@ -461,7 +463,7 @@ class FileBlockStore extends AbstractBlockStore {
/**
* This method will retrieve block undo coin data.
* @param {Buffer} hash - The block hash
* @returns {Promise}
* @returns {Promise<Buffer>}
*/

async readUndo(hash) {
Expand All @@ -488,9 +490,9 @@ class FileBlockStore extends AbstractBlockStore {
* @private
* @param {Number} type - The type of block data
* @param {Buffer} hash - The block hash
* @param {Number} offset - The offset within the block
* @param {Number} length - The number of bytes of the data
* @returns {Promise}
* @param {Number} [offset] - The offset within the block
* @param {Number} [length] - The number of bytes of the data
* @returns {Promise<Buffer>}
*/

async _read(type, hash, offset, length) {
Expand Down Expand Up @@ -536,7 +538,7 @@ class FileBlockStore extends AbstractBlockStore {
/**
* This will free resources for storing merkle block data.
* @param {Buffer} hash - The block hash
* @returns {Promise}
* @returns {Promise<Boolean>}
*/

async pruneMerkle(hash) {
Expand All @@ -546,7 +548,7 @@ class FileBlockStore extends AbstractBlockStore {
/**
* This will free resources for storing the block undo coin data.
* @param {Buffer} hash - The block hash
* @returns {Promise}
* @returns {Promise<Boolean>}
*/

async pruneUndo(hash) {
Expand All @@ -556,7 +558,7 @@ class FileBlockStore extends AbstractBlockStore {
/**
* This will free resources for storing the block data.
* @param {Buffer} hash - The block hash
* @returns {Promise}
* @returns {Promise<Boolean>}
*/

async pruneBlock(hash) {
Expand All @@ -569,8 +571,9 @@ class FileBlockStore extends AbstractBlockStore {
* block is removed and will not be able to be read. The underlying
* file is unlinked when all blocks in a file have been pruned.
* @private
* @param {Number} type - The type of block data
* @param {Buffer} hash - The block hash
* @returns {Promise}
* @returns {Promise<Boolean>}
*/

async _prune(type, hash) {
Expand Down Expand Up @@ -656,13 +659,16 @@ class FileBlockStore extends AbstractBlockStore {
* @alias module:blockstore.FileBatch
*/

class FileBatch {
class FileBatch extends AbstractBatch {
/**
* Create AbstractBatch.
* @constructor
* @param {FileBlockStore} blocks
*/

constructor(blocks) {
super();

this.blocks = blocks;
this.writes = [];
this.prunes = [];
Expand All @@ -676,76 +682,83 @@ class FileBatch {

/**
* Write merkle block data to the batch.
* @property {Buffer} hash
* @property {Buffer} data
* @returns {Batch}
* @param {Buffer} hash
* @param {Buffer} data
* @returns {this}
*/

writeMerkle(hash, data) {
this.writes.push(new WriteOp(types.MERKLE, hash, data));
return this;
}

/**
* Write undo coin data to the batch.
* @param {Buffer} hash
* @param {Buffer} data
* @returns {Batch}
* @returns {this}
*/

writeUndo(hash, data) {
this.writes.push(new WriteOp(types.UNDO, hash, data));
return this;
}

/**
* Write block data to the batch.
* @param {Buffer} hash
* @param {Buffer} data
* @returns {Batch}
* @returns {this}
*/

writeBlock(hash, data) {
this.writes.push(new WriteOp(types.BLOCK, hash, data));
return this;
}

/**
* Remove merkle block data from the batch.
* @param {Buffer} hash
* @returns {Batch}
* @returns {this}
*/

pruneMerkle(hash) {
this.prunes.push(new PruneOp(types.MERKLE, hash));
return this;
}

/**
* Remove undo data from the batch.
* @param {Buffer} hash
* @returns {Batch}
* @returns {this}
*/

pruneUndo(hash) {
this.prunes.push(new PruneOp(types.UNDO, hash));
return this;
}

/**
* Prune block data from the batch.
* @param {Buffer} hash
* @returns {Batch}
* @returns {this}
*/

pruneBlock(hash) {
this.prunes.push(new PruneOp(types.BLOCK, hash));
return this;
}

/**
* Clear the batch.
* @returns {Batch}
* @returns {this}
*/

clear() {
assert(!this.written, 'Already written all.');
this.writes.length = 0;
this.prunes.length = 0;
return this;
}

/**
Expand Down
Loading

0 comments on commit 45c6ac1

Please sign in to comment.