Skip to content

Commit

Permalink
Merge PR #923 from 'nodech/type-cleanup'
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Feb 7, 2025
2 parents 0da3a98 + efb0029 commit 73533cd
Show file tree
Hide file tree
Showing 17 changed files with 361 additions and 270 deletions.
177 changes: 95 additions & 82 deletions lib/blockchain/chain.js

Large diffs are not rendered by default.

193 changes: 104 additions & 89 deletions lib/blockchain/chaindb.js

Large diffs are not rendered by default.

22 changes: 14 additions & 8 deletions lib/blockchain/chainentry.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ const Headers = require('../primitives/headers');
const InvItem = require('../primitives/invitem');
const util = require('../utils/util');

/** @typedef {import('../types').BufioWriter} BufioWriter */
/** @typedef {import('../protocol/network')} Network */
/** @typedef {import('../primitives/block')} Block */
/** @typedef {import('../primitives/merkleblock')} MerkleBlock */

/*
* Constants
*/
Expand Down Expand Up @@ -69,8 +74,8 @@ class ChainEntry extends bio.Struct {

/**
* Inject properties from options.
* @private
* @param {Object} options
* @returns {this}
*/

fromOptions(options) {
Expand Down Expand Up @@ -126,6 +131,7 @@ class ChainEntry extends bio.Struct {
/**
* Calculate the chainwork by
* adding proof to previous chainwork.
* @param {ChainEntry?} [prev] - Previous entry.
* @returns {BN} chainwork
*/

Expand Down Expand Up @@ -169,9 +175,9 @@ class ChainEntry extends bio.Struct {

/**
* Inject properties from block.
* @private
* @param {Block|MerkleBlock} block
* @param {ChainEntry} prev - Previous entry.
* @param {ChainEntry?} [prev] - Previous entry.
* @returns {this}
*/

fromBlock(block, prev) {
Expand Down Expand Up @@ -203,7 +209,8 @@ class ChainEntry extends bio.Struct {

/**
* Serialize the entry to internal database format.
* @returns {Buffer}
* @param {BufioWriter} bw
* @returns {BufioWriter}
*/

write(bw) {
Expand Down Expand Up @@ -234,8 +241,7 @@ class ChainEntry extends bio.Struct {

/**
* Inject properties from serialized data.
* @private
* @param {Buffer} data
* @param {bio.BufferReader} br
*/

read(br) {
Expand Down Expand Up @@ -291,8 +297,8 @@ class ChainEntry extends bio.Struct {

/**
* Inject properties from json object.
* @private
* @param {Object} json
* @returns {this}
*/

fromJSON(json) {
Expand Down Expand Up @@ -355,7 +361,7 @@ class ChainEntry extends bio.Struct {
/**
* Instantiate chainentry from block.
* @param {Block|MerkleBlock} block
* @param {ChainEntry} prev - Previous entry.
* @param {ChainEntry?} [prev] - Previous entry.
* @returns {ChainEntry}
*/

Expand Down
11 changes: 7 additions & 4 deletions lib/blockchain/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

'use strict';

/** @typedef {import('@handshake-org/bfilter').BloomFilter} BloomFilter */
/** @typedef {import('../types').LockFlags} LockFlags */

/**
* @module blockchain/common
*/
Expand All @@ -23,16 +26,16 @@ exports.lockFlags = {};
* @default
*/

exports.lockFlags.MANDATORY_LOCKTIME_FLAGS = 0;
exports.MANDATORY_LOCKTIME_FLAGS = 0;

/**
* Standard locktime flags (used for mempool validation).
* @const {LockFlags}
* @default
*/

exports.lockFlags.STANDARD_LOCKTIME_FLAGS = 0
| exports.lockFlags.MANDATORY_LOCKTIME_FLAGS;
exports.STANDARD_LOCKTIME_FLAGS = 0
| exports.MANDATORY_LOCKTIME_FLAGS;

/**
* Threshold states for versionbits
Expand Down Expand Up @@ -66,7 +69,7 @@ exports.flags = {
* @default
*/

exports.flags.DEFAULT_FLAGS = 0
exports.DEFAULT_FLAGS = 0
| exports.flags.VERIFY_POW
| exports.flags.VERIFY_BODY;

Expand Down
61 changes: 37 additions & 24 deletions lib/blockchain/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const {
} = require('../migrations/migrator');

/** @typedef {import('../types').Hash} Hash */
/** @typedef {ReturnType<bdb.DB['batch']>} Batch */
/** @typedef {import('../migrations/migrator').types} MigrationType */

/**
* Switch to new migrations layout.
Expand All @@ -46,6 +48,10 @@ class MigrateMigrations extends AbstractMigration {
this.layout = MigrateMigrations.layout();
}

/**
* @returns {Promise<MigrationType>}
*/

async check() {
return types.MIGRATE;
}
Expand All @@ -59,8 +65,8 @@ class MigrateMigrations extends AbstractMigration {
async migrate(b) {
this.logger.info('Migrating migrations..');

const oldLayout = this.layout.oldLayout.wdb;
const newLayout = this.layout.newLayout.wdb;
const oldLayout = this.layout.oldLayout;
const newLayout = this.layout.newLayout;
let nextMigration = 1;
const skipped = [];

Expand All @@ -83,10 +89,17 @@ class MigrateMigrations extends AbstractMigration {
}

this.db.writeVersion(b, 2);
b.put(newLayout.M.encode(),
this.encodeMigrationState(nextMigration, skipped));

const rawState = this.encodeMigrationState(nextMigration, skipped);
b.put(newLayout.M.encode(), rawState);
}

/**
* @param {Number} nextMigration
* @param {Number[]} skipped
* @returns {Buffer}
*/

encodeMigrationState(nextMigration, skipped) {
let size = 4;
size += encoding.sizeVarint(nextMigration);
Expand Down Expand Up @@ -116,14 +129,10 @@ class MigrateMigrations extends AbstractMigration {
static layout() {
return {
oldLayout: {
wdb: {
M: bdb.key('M', ['uint32'])
}
M: bdb.key('M', ['uint32'])
},
newLayout: {
wdb: {
M: bdb.key('M')
}
M: bdb.key('M')
}
};
}
Expand All @@ -138,7 +147,7 @@ class MigrateChainState extends AbstractMigration {
/**
* Create migration chain state
* @constructor
* @param {ChainMigrator} options
* @param {ChainMigratorOptions} options
*/

constructor(options) {
Expand Down Expand Up @@ -261,7 +270,7 @@ class MigrateChainState extends AbstractMigration {
/**
* Get Block (old layout)
* @param {Hash} hash
* @returns {Promise} - Block
* @returns {Promise<Block>}
*/

async getBlock(hash) {
Expand All @@ -276,7 +285,7 @@ class MigrateChainState extends AbstractMigration {

/**
* Get block view (old layout)
* @param {Hash} hash
* @param {Block} block
* @returns {Promise} - UndoCoins
*/

Expand Down Expand Up @@ -369,7 +378,6 @@ class MigrateBlockStore extends AbstractMigration {

/**
* Migrate blocks and undo blocks
* @param {Batch} b
* @returns {Promise}
*/

Expand Down Expand Up @@ -480,7 +488,7 @@ class MigrateTreeState extends AbstractMigration {
/**
* Create tree state migrator
* @constructor
* @param {ChainMigrator} options
* @param {ChainMigratorOptions} options
*/

constructor(options) {
Expand All @@ -498,6 +506,11 @@ class MigrateTreeState extends AbstractMigration {
return types.MIGRATE;
}

/**
* @param {Batch} b
* @returns {Promise}
*/

async migrate(b) {
if (this.options.spv) {
this.db.writeVersion(b, 3);
Expand Down Expand Up @@ -612,7 +625,7 @@ class ChainMigratorOptions {
this.network = Network.primary;
this.logger = Logger.global;

this.migrations = exports.migrations;
this.migrations = ChainMigrator.migrations;
this.migrateFlag = -1;

this.dbVersion = 0;
Expand Down Expand Up @@ -672,23 +685,23 @@ class ChainMigratorOptions {
assert(typeof options.prune === 'boolean');
this.prune = options.prune;
}

return this;
}
}

exports = ChainMigrator;

// List of the migrations with ids
exports.migrations = {
ChainMigrator.migrations = {
0: MigrateMigrations,
1: MigrateChainState,
2: MigrateBlockStore,
3: MigrateTreeState
};

// Expose migrations
exports.MigrateChainState = MigrateChainState;
exports.MigrateMigrations = MigrateMigrations;
exports.MigrateBlockStore = MigrateBlockStore;
exports.MigrateTreeState = MigrateTreeState;
ChainMigrator.MigrateChainState = MigrateChainState;
ChainMigrator.MigrateMigrations = MigrateMigrations;
ChainMigrator.MigrateBlockStore = MigrateBlockStore;
ChainMigrator.MigrateTreeState = MigrateTreeState;

module.exports = exports;
module.exports = ChainMigrator;
5 changes: 3 additions & 2 deletions lib/covenants/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const rules = exports;

/** @typedef {import('../types').Amount} AmountValue */
/** @typedef {import('../types').Hash} Hash */
/** @typedef {import('../types').NameFlags} NameFlags */
/** @typedef {import('../protocol/network')} Network */
/** @typedef {import('../primitives/tx')} TX */
/** @typedef {import('../coins/coinview')} CoinView */
Expand Down Expand Up @@ -130,11 +131,11 @@ rules.nameFlags = {

/**
* Standard verify flags for covenants.
* @const {NameFlags}
* @const {rules.NameFlags}
* @default
*/

rules.nameFlags.MANDATORY_VERIFY_COVENANT_FLAGS = 0;
rules.MANDATORY_VERIFY_COVENANT_FLAGS = 0;

/**
* Maximum covenant size.
Expand Down
6 changes: 3 additions & 3 deletions lib/mempool/mempool.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ class Mempool extends EventEmitter {
const state = await this.getNextState();
const mtp = await this.chain.getMedianTime(this.chain.tip);
const remove = [];
const lockFlags = common.lockFlags.STANDARD_LOCKTIME_FLAGS;
const lockFlags = common.STANDARD_LOCKTIME_FLAGS;

this.contracts.clear();

Expand Down Expand Up @@ -1486,7 +1486,7 @@ class Mempool extends EventEmitter {
async insertTX(tx, id) {
assert(!tx.mutable, 'Cannot add mutable TX to mempool.');

const lockFlags = common.lockFlags.STANDARD_LOCKTIME_FLAGS;
const lockFlags = common.STANDARD_LOCKTIME_FLAGS;
const height = this.chain.height;
const hash = tx.hash();

Expand Down Expand Up @@ -1604,7 +1604,7 @@ class Mempool extends EventEmitter {
const network = this.network;
const height = this.chain.height + 1;
const state = await this.getNextState();
const lockFlags = common.lockFlags.STANDARD_LOCKTIME_FLAGS;
const lockFlags = common.STANDARD_LOCKTIME_FLAGS;
const tx = entry.tx;

// Verify sequence locks.
Expand Down
10 changes: 7 additions & 3 deletions lib/migrations/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

'use strict';

/** @typedef {import('bdb').DB} DB */
/** @typedef {ReturnType<DB['batch']>} Batch */
/** @typedef {import('./migrator').types} MigrationType */

/**
* Abstract class for single migration.
* @alias module:migrations.AbstractMigration
Expand All @@ -23,7 +27,7 @@ class AbstractMigration {

/**
* Check if the migration applies to the database
* @returns {Promise}
* @returns {Promise<MigrationType>}
*/

async check() {
Expand All @@ -32,11 +36,11 @@ class AbstractMigration {

/**
* Run the actual migration
* @param {Batch} batch
* @param {Batch} b
* @returns {Promise}
*/

async migrate() {
async migrate(b) {
throw new Error('Abstract method.');
}

Expand Down
Loading

0 comments on commit 73533cd

Please sign in to comment.