From e13f180c962ea90fb11166bbefad250a3a59cd6f Mon Sep 17 00:00:00 2001 From: Nodari Chkuaselidze Date: Mon, 30 Sep 2024 14:29:03 +0400 Subject: [PATCH] types: update blockstore. --- lib/blockstore/abstract.js | 26 ++++++++-------- lib/blockstore/file.js | 63 +++++++++++++++++++++++--------------- lib/blockstore/level.js | 26 +++++++++------- lib/blockstore/records.js | 17 +++++----- lib/primitives/coin.js | 6 ++-- lib/script/sigcache.js | 2 +- 6 files changed, 80 insertions(+), 60 deletions(-) diff --git a/lib/blockstore/abstract.js b/lib/blockstore/abstract.js index d024f3e2..9c2a31b0 100644 --- a/lib/blockstore/abstract.js +++ b/lib/blockstore/abstract.js @@ -20,6 +20,7 @@ class AbstractBlockStore { /** * Create an abstract blockstore. * @constructor + * @param {Object} [options] */ constructor(options) { @@ -192,7 +193,7 @@ class AbstractBlockStore { /** * Create batch. - * @returns {Batch} + * @returns {AbstractBatch} */ batch() { @@ -206,7 +207,6 @@ class AbstractBlockStore { * @abstract */ -// eslint-disable-next-line no-unused-vars class AbstractBatch { /** * Create AbstractBatch. @@ -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) { @@ -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) { @@ -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) { @@ -252,7 +252,7 @@ class AbstractBatch { /** * Remove merkle block data from the batch. * @param {Buffer} hash - * @returns {Batch} + * @returns {this} */ pruneMerkle(hash) { @@ -262,7 +262,7 @@ class AbstractBatch { /** * Remove undo data from the batch. * @param {Buffer} hash - * @returns {Batch} + * @returns {this} */ pruneUndo(hash) { @@ -272,7 +272,7 @@ class AbstractBatch { /** * Prune block data from the batch. * @param {Buffer} hash - * @returns {Batch} + * @returns {this} */ pruneBlock(hash) { @@ -281,7 +281,7 @@ class AbstractBatch { /** * Clear the batch. - * @returns {Batch} + * @returns {this} */ clear() { @@ -293,11 +293,13 @@ class AbstractBatch { * @returns {Promise} */ - write() { + commit() { throw new Error('Abstract method.'); } } +AbstractBlockStore.AbstractBatch = AbstractBatch; + /* * Expose */ diff --git a/lib/blockstore/file.js b/lib/blockstore/file.js index 188cb327..b323c0ad 100644 --- a/lib/blockstore/file.js +++ b/lib/blockstore/file.js @@ -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'); @@ -33,6 +34,7 @@ class FileBlockStore extends AbstractBlockStore { /** * Create a blockstore that stores blocks in files. * @constructor + * @param {Object} [options] */ constructor(options) { @@ -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) { @@ -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} */ async allocate(type, length) { @@ -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} */ async writeMerkle(hash, data) { @@ -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} */ async writeUndo(hash, data) { @@ -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} */ async writeBlock(hash, data) { @@ -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} - Whether the data was written. */ async _write(type, hash, data) { @@ -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} */ async readMerkle(hash) { @@ -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} */ async readUndo(hash) { @@ -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} */ async _read(type, hash, offset, length) { @@ -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} */ async pruneMerkle(hash) { @@ -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} */ async pruneUndo(hash) { @@ -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} */ async pruneBlock(hash) { @@ -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} */ async _prune(type, hash) { @@ -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 = []; @@ -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; } /** diff --git a/lib/blockstore/level.js b/lib/blockstore/level.js index 69eb22e7..693eeabf 100644 --- a/lib/blockstore/level.js +++ b/lib/blockstore/level.js @@ -10,6 +10,7 @@ const assert = require('bsert'); const bdb = require('bdb'); const fs = require('bfile'); const AbstractBlockStore = require('./abstract'); +const {AbstractBatch} = require('./abstract'); const layout = require('./layout'); const {types} = require('./common'); @@ -24,6 +25,7 @@ class LevelBlockStore extends AbstractBlockStore { /** * Create a blockstore that stores blocks in LevelDB. * @constructor + * @param {Object} [options] */ constructor(options) { @@ -246,13 +248,15 @@ class LevelBlockStore extends AbstractBlockStore { * @alias module:blockstore.LevelBatch */ -class LevelBatch { +class LevelBatch extends AbstractBatch { /** * Create LevelBatch - * @param {DB} db + * @param {bdb.DB} db */ constructor(db) { + super(); + this.writesBatch = db.batch(); this.prunesBatch = db.batch(); this.committedWrites = false; @@ -265,9 +269,9 @@ class LevelBatch { /** * 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) { @@ -279,7 +283,7 @@ class LevelBatch { * Write undo coin data to the batch. * @param {Buffer} hash * @param {Buffer} data - * @returns {Batch} + * @returns {this} */ writeUndo(hash, data) { @@ -291,7 +295,7 @@ class LevelBatch { * Write block data to the batch. * @param {Buffer} hash * @param {Buffer} data - * @returns {Batch} + * @returns {this} */ writeBlock(hash, data) { @@ -302,7 +306,7 @@ class LevelBatch { /** * Remove merkle block data from the batch. * @param {Buffer} hash - * @returns {Batch} + * @returns {this} */ pruneMerkle(hash) { @@ -313,7 +317,7 @@ class LevelBatch { /** * Remove undo data from the batch. * @param {Buffer} hash - * @returns {Batch} + * @returns {this} */ pruneUndo(hash) { @@ -324,7 +328,7 @@ class LevelBatch { /** * Prune block data from the batch. * @param {Buffer} hash - * @returns {Batch} + * @returns {this} */ pruneBlock(hash) { @@ -334,7 +338,7 @@ class LevelBatch { /** * Clear the batch. - * @returns {Batch} + * @returns {this} */ clear() { diff --git a/lib/blockstore/records.js b/lib/blockstore/records.js index 9c275c93..2236eef7 100644 --- a/lib/blockstore/records.js +++ b/lib/blockstore/records.js @@ -9,6 +9,8 @@ const assert = require('bsert'); const bio = require('bufio'); +/** @typedef {import('../types').BufioWriter} BufioWriter */ + /** * @module blockstore/records */ @@ -46,8 +48,7 @@ class BlockRecord extends bio.Struct { /** * Inject properties from buffer reader. - * @private - * @param {BufferReader} data + * @param {bio.BufferReader} br */ read(br) { @@ -61,8 +62,8 @@ class BlockRecord extends bio.Struct { /** * Serialize the block record. * Write block record to a buffer writer - * @param {BufferWriter} bw - * @returns {BufferWriter} + * @param {BufioWriter} bw + * @returns {BufioWriter} */ write(bw) { @@ -107,8 +108,7 @@ class FileRecord extends bio.Struct { /** * Inject properties from buffer reader. - * @private - * @param {BufferReader} br + * @param {bio.BufferReader} br */ read(br) { @@ -121,14 +121,15 @@ class FileRecord extends bio.Struct { /** * Write serialized file record to the buffer writer. - * @param {BufferWriter} bw - * @returns {BufferWriter} + * @param {BufioWriter} bw + * @returns {BufioWriter} */ write(bw) { bw.writeU32(this.blocks); bw.writeU32(this.used); bw.writeU32(this.length); + return bw; } } diff --git a/lib/primitives/coin.js b/lib/primitives/coin.js index a1d64a87..cb83b7f3 100644 --- a/lib/primitives/coin.js +++ b/lib/primitives/coin.js @@ -14,8 +14,8 @@ const consensus = require('../protocol/consensus'); const Outpoint = require('./outpoint'); const util = require('../utils/util'); -/** @typedef {import('bufio').BufferWriter} BufferWriter */ /** @typedef {import('bufio').BufferReader} BufferReader */ +/** @typedef {import('../types').BufioWriter} BufioWriter */ /** @typedef {import('../types').NetworkType} NetworkType */ /** @typedef {import('../types').HexHash} HexHash */ /** @typedef {import('./tx')} TX */ @@ -270,8 +270,8 @@ class Coin extends Output { /** * Write the coin to a buffer writer. - * @param {BufferWriter} bw - * @returns {BufferWriter} + * @param {BufioWriter} bw + * @returns {BufioWriter} */ write(bw) { diff --git a/lib/script/sigcache.js b/lib/script/sigcache.js index 2ba0a86e..d0e81443 100644 --- a/lib/script/sigcache.js +++ b/lib/script/sigcache.js @@ -95,7 +95,7 @@ class SigCache { /** * Verify a signature, testing * it against the cache first. - * @param {Buffer} hash + * @param {Hash} hash * @param {Buffer} sig * @param {Buffer} key * @returns {Boolean}