types: update blockstore.
This commit is contained in:
parent
685a2202ad
commit
e13f180c96
6 changed files with 80 additions and 60 deletions
|
|
@ -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
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -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<Object>}
|
||||
*/
|
||||
|
||||
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<Boolean>}
|
||||
*/
|
||||
|
||||
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<Boolean>}
|
||||
*/
|
||||
|
||||
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<Boolean>}
|
||||
*/
|
||||
|
||||
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<Boolean>} - 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<Buffer>}
|
||||
*/
|
||||
|
||||
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<Buffer>}
|
||||
*/
|
||||
|
||||
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<Buffer>}
|
||||
*/
|
||||
|
||||
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<Boolean>}
|
||||
*/
|
||||
|
||||
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<Boolean>}
|
||||
*/
|
||||
|
||||
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<Boolean>}
|
||||
*/
|
||||
|
||||
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<Boolean>}
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue