Comparing version 3.1.4-bbbc46c.0 to 3.1.4-c828c82.0
@@ -15,2 +15,3 @@ import * as Logger from 'bunyan'; | ||
protected log: Logger; | ||
protected initialized: boolean; | ||
private deferredEffects; | ||
@@ -32,2 +33,6 @@ private handlerVersionMap; | ||
/** | ||
* Performs all required initialization for the handler. | ||
*/ | ||
initialize(): Promise<void>; | ||
/** | ||
* Updates the `lastProcessedBlockNumber` and `lastProcessedBlockHash` meta state, coinciding with the block | ||
@@ -51,2 +56,6 @@ * that has just been processed. These are the same values read by `updateIndexState()`. | ||
/** | ||
* Idempotently performs any required setup. | ||
*/ | ||
protected abstract setup(): Promise<void>; | ||
/** | ||
* This method is used when matching the types of incoming actions against the types the `Updater`s and `Effect`s are | ||
@@ -83,2 +92,3 @@ * subscribed to. When this returns true, their corresponding functions will run. | ||
protected handleActions(state: any, context: any, nextBlock: NextBlock, isReplay: boolean): Promise<void>; | ||
private handleRollback; | ||
private range; | ||
@@ -88,2 +98,3 @@ private runOrDeferEffect; | ||
private getNextDeferredBlockNumber; | ||
private rollbackDeferredEffects; | ||
private initHandlerVersions; | ||
@@ -90,0 +101,0 @@ private refreshIndexState; |
@@ -19,2 +19,3 @@ "use strict"; | ||
const Logger = __importStar(require("bunyan")); | ||
const errors_1 = require("./errors"); | ||
/** | ||
@@ -36,2 +37,3 @@ * Takes `block`s output from implementations of `AbstractActionReader` and processes their actions through the | ||
this.handlerVersionName = 'v1'; | ||
this.initialized = false; | ||
this.deferredEffects = {}; | ||
@@ -50,12 +52,7 @@ this.handlerVersionMap = {}; | ||
const { isRollback, isEarliestBlock } = blockMeta; | ||
if (isRollback || (isReplay && isEarliestBlock)) { | ||
const rollbackBlockNumber = blockInfo.blockNumber - 1; | ||
const rollbackCount = this.lastProcessedBlockNumber - rollbackBlockNumber; | ||
this.log.info(`Rolling back ${rollbackCount} blocks to block ${rollbackBlockNumber}...`); | ||
yield this.rollbackTo(rollbackBlockNumber); | ||
yield this.refreshIndexState(); | ||
if (!this.initialized) { | ||
yield this.initialize(); | ||
this.initialized = true; | ||
} | ||
else if (this.lastProcessedBlockNumber === 0 && this.lastProcessedBlockHash === '') { | ||
yield this.refreshIndexState(); | ||
} | ||
yield this.handleRollback(isRollback, blockInfo.blockNumber, isReplay, isEarliestBlock); | ||
const nextBlockNeeded = this.lastProcessedBlockNumber + 1; | ||
@@ -78,3 +75,4 @@ // Just processed this block; skip | ||
if (blockInfo.previousBlockHash !== this.lastProcessedBlockHash) { | ||
throw Error('Block hashes do not match; block not part of current chain.'); | ||
const err = new errors_1.MismatchedBlockHashError(); | ||
throw err; | ||
} | ||
@@ -100,2 +98,10 @@ } | ||
/** | ||
* Performs all required initialization for the handler. | ||
*/ | ||
initialize() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield this.setup(); | ||
}); | ||
} | ||
/** | ||
* This method is used when matching the types of incoming actions against the types the `Updater`s and `Effect`s are | ||
@@ -179,2 +185,17 @@ * subscribed to. When this returns true, their corresponding functions will run. | ||
} | ||
handleRollback(isRollback, blockNumber, isReplay, isEarliestBlock) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (isRollback || (isReplay && isEarliestBlock)) { | ||
const rollbackBlockNumber = blockNumber - 1; | ||
const rollbackCount = this.lastProcessedBlockNumber - rollbackBlockNumber; | ||
this.log.info(`Rolling back ${rollbackCount} blocks to block ${rollbackBlockNumber}...`); | ||
yield this.rollbackTo(rollbackBlockNumber); | ||
this.rollbackDeferredEffects(blockNumber); | ||
yield this.refreshIndexState(); | ||
} | ||
else if (this.lastProcessedBlockNumber === 0 && this.lastProcessedBlockHash === '') { | ||
yield this.refreshIndexState(); | ||
} | ||
}); | ||
} | ||
range(start, end) { | ||
@@ -185,2 +206,3 @@ return Array(end - start).fill(0).map((_, i) => i + start); | ||
const { block, lastIrreversibleBlockNumber } = nextBlock; | ||
const { blockNumber } = block.blockInfo; | ||
const shouldRunImmediately = (!effect.deferUntilIrreversible || block.blockInfo.blockNumber <= lastIrreversibleBlockNumber); | ||
@@ -190,7 +212,7 @@ if (shouldRunImmediately) { | ||
} | ||
else if (!this.deferredEffects[block.blockInfo.blockNumber]) { | ||
this.deferredEffects[block.blockInfo.blockNumber] = [() => effect.run(payload, block, context)]; | ||
} | ||
else { | ||
this.deferredEffects[block.blockInfo.blockNumber].push(() => effect.run(payload, block, context)); | ||
if (!this.deferredEffects[blockNumber]) { | ||
this.deferredEffects[blockNumber] = []; | ||
} | ||
this.deferredEffects[blockNumber].push(() => effect.run(payload, block, context)); | ||
} | ||
@@ -205,3 +227,4 @@ } | ||
if (this.deferredEffects[blockNumber]) { | ||
for (const deferredEffect of this.deferredEffects[blockNumber]) { | ||
const effects = this.deferredEffects[blockNumber]; | ||
for (const deferredEffect of effects) { | ||
deferredEffect(); | ||
@@ -220,10 +243,16 @@ } | ||
} | ||
rollbackDeferredEffects(rollbackTo) { | ||
const blockNumbers = Object.keys(this.deferredEffects).map((num) => parseInt(num, 10)); | ||
const toRollBack = blockNumbers.filter((bn) => bn >= rollbackTo); | ||
for (const blockNumber of toRollBack) { | ||
delete this.deferredEffects[blockNumber]; | ||
} | ||
} | ||
initHandlerVersions(handlerVersions) { | ||
if (handlerVersions.length === 0) { | ||
throw new Error('Must have at least one handler version.'); | ||
throw new errors_1.MissingHandlerVersionError(); | ||
} | ||
for (const handlerVersion of handlerVersions) { | ||
if (this.handlerVersionMap.hasOwnProperty(handlerVersion.versionName)) { | ||
throw new Error(`Handler version name '${handlerVersion.versionName}' already exists. ` + | ||
'Handler versions must have unique names.'); | ||
throw new errors_1.DuplicateHandlerVersionError(handlerVersion.versionName); | ||
} | ||
@@ -230,0 +259,0 @@ this.handlerVersionMap[handlerVersion.versionName] = handlerVersion; |
@@ -15,3 +15,3 @@ import * as Logger from 'bunyan'; | ||
protected log: Logger; | ||
private initialized; | ||
protected initialized: boolean; | ||
constructor(options?: ActionReaderOptions); | ||
@@ -41,2 +41,6 @@ /** | ||
/** | ||
* Performs all required initialization for the reader. | ||
*/ | ||
initialize(): Promise<void>; | ||
/** | ||
* Changes the state of the `AbstractActionReader` instance to have just processed the block at the given block | ||
@@ -54,2 +58,6 @@ * number. If the block exists in its temporary block history, it will use this, otherwise it will fetch the block | ||
/** | ||
* Idempotently performs any required setup. | ||
*/ | ||
protected abstract setup(): Promise<void>; | ||
/** | ||
* Incrementally rolls back reader state one block at a time, comparing the blockHistory with | ||
@@ -56,0 +64,0 @@ * newly fetched blocks. Fork resolution is finished when either the current block's previous hash |
@@ -19,2 +19,3 @@ "use strict"; | ||
const Logger = __importStar(require("bunyan")); | ||
const errors_1 = require("./errors"); | ||
const defaultBlock = { | ||
@@ -59,6 +60,5 @@ blockInfo: { | ||
}; | ||
// TODO: Should this only be called when updating headBlockNumber? | ||
this.lastIrreversibleBlockNumber = yield this.getLastIrreversibleBlockNumber(); | ||
if (!this.initialized) { | ||
yield this.initBlockState(); | ||
yield this.initialize(); | ||
} | ||
@@ -96,2 +96,12 @@ if (this.currentBlockNumber === this.headBlockNumber) { | ||
/** | ||
* Performs all required initialization for the reader. | ||
*/ | ||
initialize() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield this.setup(); | ||
yield this.initBlockState(); | ||
this.initialized = true; | ||
}); | ||
} | ||
/** | ||
* Changes the state of the `AbstractActionReader` instance to have just processed the block at the given block | ||
@@ -107,6 +117,6 @@ * number. If the block exists in its temporary block history, it will use this, otherwise it will fetch the block | ||
if (blockNumber < this.startAtBlock) { | ||
throw new Error('Cannot seek to block before configured `startAtBlock` number.'); | ||
throw new errors_1.ImproperStartAtBlockError(); | ||
} | ||
if (blockNumber > this.headBlockNumber) { | ||
throw new Error(`Cannot seek to block number ${blockNumber} as it does not exist yet.`); | ||
throw new errors_1.ImproperSeekToBlockError(blockNumber); | ||
} | ||
@@ -165,2 +175,3 @@ this.currentBlockNumber = blockNumber - 1; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.lastIrreversibleBlockNumber = yield this.getLastIrreversibleBlockNumber(); | ||
this.headBlockNumber = yield this.getLatestNeededBlockNumber(); | ||
@@ -172,3 +183,2 @@ if (this.currentBlockNumber < 0) { | ||
yield this.reloadHistory(); | ||
this.initialized = true; | ||
}); | ||
@@ -250,3 +260,3 @@ } | ||
if (tryCount === maxTries) { | ||
throw new Error('Could not reload history.'); | ||
throw new errors_1.ReloadHistoryError(); | ||
} | ||
@@ -260,3 +270,3 @@ } | ||
if (this.currentBlockData.blockInfo.blockNumber < this.lastIrreversibleBlockNumber && checkIrreversiblility) { | ||
throw new Error('Last irreversible block has been passed without resolving fork'); | ||
throw new errors_1.UnresolvedForkError(); | ||
} | ||
@@ -263,0 +273,0 @@ this.blockHistory.push(yield this.getBlock(this.currentBlockData.blockInfo.blockNumber - 1)); |
@@ -22,2 +22,3 @@ import * as Logger from 'bunyan'; | ||
private error; | ||
private clean; | ||
constructor(actionReader: AbstractActionReader, actionHandler: AbstractActionHandler, pollInterval: number); | ||
@@ -52,2 +53,3 @@ /** | ||
protected checkForBlocks(isReplay?: boolean): Promise<void>; | ||
private readonly status; | ||
} |
@@ -19,2 +19,3 @@ "use strict"; | ||
const Logger = __importStar(require("bunyan")); | ||
const interfaces_1 = require("./interfaces"); | ||
/** | ||
@@ -32,2 +33,3 @@ * Coordinates implementations of `AbstractActionReader`s and `AbstractActionHandler`s in | ||
this.error = null; | ||
this.clean = true; | ||
this.log = Logger.createLogger({ name: 'demux' }); | ||
@@ -56,2 +58,3 @@ } | ||
} | ||
this.clean = false; | ||
this.running = true; | ||
@@ -108,16 +111,6 @@ this.error = null; | ||
get info() { | ||
let status; | ||
if (this.running && !this.shouldPause) { | ||
status = 'indexing'; | ||
} | ||
else if (this.running && this.shouldPause) { | ||
status = 'pausing'; | ||
} | ||
else { | ||
status = 'paused'; | ||
} | ||
const info = { | ||
handler: this.actionHandler.info, | ||
reader: this.actionReader.info, | ||
status, | ||
indexingStatus: this.status, | ||
}; | ||
@@ -153,3 +146,18 @@ if (this.error) { | ||
} | ||
get status() { | ||
if (this.clean) { | ||
return interfaces_1.IndexingStatus.Initial; | ||
} | ||
if (this.running && !this.shouldPause) { | ||
return interfaces_1.IndexingStatus.Indexing; | ||
} | ||
if (this.running && this.shouldPause) { | ||
return interfaces_1.IndexingStatus.Pausing; | ||
} | ||
if (this.error) { | ||
return interfaces_1.IndexingStatus.Stopped; | ||
} | ||
return interfaces_1.IndexingStatus.Paused; | ||
} | ||
} | ||
exports.BaseActionWatcher = BaseActionWatcher; |
@@ -6,1 +6,2 @@ export { Action, Block, BlockInfo, Effect, HandlerVersion, IndexState, Updater } from './interfaces'; | ||
export { ExpressActionWatcher } from './ExpressActionWatcher'; | ||
export * from './errors'; |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -11,1 +14,2 @@ var AbstractActionHandler_1 = require("./AbstractActionHandler"); | ||
exports.ExpressActionWatcher = ExpressActionWatcher_1.ExpressActionWatcher; | ||
__export(require("./errors")); |
@@ -15,7 +15,2 @@ export interface ActionReaderOptions { | ||
onlyIrreversible?: boolean; | ||
/** | ||
* This determines how many blocks in the past are cached. This is used for determining | ||
* block validity during both normal operation and when rolling back. | ||
*/ | ||
maxHistoryLength?: number; | ||
} | ||
@@ -91,4 +86,11 @@ export interface Block { | ||
} | ||
export declare enum IndexingStatus { | ||
Initial = "initial", | ||
Indexing = "indexing", | ||
Pausing = "pausing", | ||
Paused = "paused", | ||
Stopped = "stopped" | ||
} | ||
export interface DemuxInfo { | ||
status: string; | ||
indexingStatus: IndexingStatus; | ||
error?: Error; | ||
@@ -95,0 +97,0 @@ handler: HandlerInfo; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var IndexingStatus; | ||
(function (IndexingStatus) { | ||
IndexingStatus["Initial"] = "initial"; | ||
IndexingStatus["Indexing"] = "indexing"; | ||
IndexingStatus["Pausing"] = "pausing"; | ||
IndexingStatus["Paused"] = "paused"; | ||
IndexingStatus["Stopped"] = "stopped"; | ||
})(IndexingStatus = exports.IndexingStatus || (exports.IndexingStatus = {})); |
{ | ||
"name": "demux", | ||
"version": "3.1.4-bbbc46c.0", | ||
"version": "3.1.4-c828c82.0", | ||
"author": { | ||
@@ -35,6 +35,6 @@ "name": "Julien Heller", | ||
"watch": "tsc -w", | ||
"example": "./run-example.sh", | ||
"example": "./scripts/run-example.sh", | ||
"lint": "tslint -c tslint.json -p tsconfig.json", | ||
"test": "jest --detectOpenHandles --maxWorkers=2", | ||
"build-docs": "./build-docs.sh", | ||
"build-docs": "./scripts/build-docs.sh", | ||
"current-version": "echo $npm_package_version" | ||
@@ -41,0 +41,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1274
0
63532
17