@bitski/provider-engine
Advanced tools
Comparing version 0.4.1 to 0.4.2
@@ -12,2 +12,3 @@ import PollingBlockTracker from 'eth-block-tracker'; | ||
currentBlock: any; | ||
currentBlockNumber?: string; | ||
protected _blockTracker: PollingBlockTracker; | ||
@@ -17,2 +18,4 @@ protected _ready: Stoplight; | ||
protected _running: boolean; | ||
private blockTimeout; | ||
private maxBlockRetries; | ||
constructor(opts?: ProviderEngineOptions); | ||
@@ -26,4 +29,6 @@ isRunning(): boolean; | ||
protected sendPayload(payload: JSONRPCRequest): Promise<JSONRPCResponse>; | ||
protected loadBlock(blockNumber: string, callCount?: number): void; | ||
protected updateBlock(block: any): void; | ||
protected _getBlockByNumber(blockNumber: any): Promise<JSONRPCResponse>; | ||
protected _setCurrentBlock(block: any): void; | ||
} |
@@ -30,2 +30,6 @@ "use strict"; | ||
_this._running = false; | ||
// Number of milliseconds to wait before retrying | ||
_this.blockTimeout = 300; | ||
// Maximum attempts to load a block | ||
_this.maxBlockRetries = 3; | ||
_this.setMaxListeners(30); | ||
@@ -66,14 +70,4 @@ // parse options | ||
this._blockTracker.on('latest', function (blockNumber) { | ||
// get block body | ||
_this._getBlockByNumber(blockNumber).then(function (blockResponse) { | ||
var block = blockResponse.result; | ||
var bufferBlock = toBufferBlock(block); | ||
// set current + emit "block" event | ||
_this._setCurrentBlock(bufferBlock); | ||
// emit other events | ||
_this.emit('rawBlock', block); | ||
_this.emit('latest', block); | ||
}).catch(function (err) { | ||
_this.emit('error', err); | ||
}); | ||
_this.currentBlockNumber = blockNumber; | ||
_this.loadBlock(blockNumber); | ||
}); | ||
@@ -180,2 +174,34 @@ // forward other events | ||
}; | ||
// Tries to get the block payload recursively | ||
Web3ProviderEngine.prototype.loadBlock = function (blockNumber, callCount) { | ||
var _this = this; | ||
if (callCount === void 0) { callCount = 0; } | ||
this._getBlockByNumber(blockNumber).then(function (blockResponse) { | ||
// Result can be null if the block hasn't fully propagated to the nodes | ||
if (blockResponse.result) { | ||
_this.updateBlock(blockResponse.result); | ||
} | ||
else if (callCount < _this.maxBlockRetries && blockNumber === _this.currentBlockNumber) { | ||
// Only call recursively if the current block number is still the same | ||
// and if we are under the retry limit. | ||
setTimeout(function () { | ||
_this.loadBlock(blockNumber, callCount + 1); | ||
}, _this.blockTimeout); | ||
} | ||
else { | ||
throw new Error("Could not load block " + blockNumber + " after 3 tries"); | ||
} | ||
}).catch(function (err) { | ||
_this.emit('error', err); | ||
}); | ||
}; | ||
// Parse the block into a buffer representation and update subscribers. | ||
Web3ProviderEngine.prototype.updateBlock = function (block) { | ||
var bufferBlock = toBufferBlock(block); | ||
// set current + emit "block" event | ||
this._setCurrentBlock(bufferBlock); | ||
// emit other events | ||
this.emit('rawBlock', block); | ||
this.emit('latest', block); | ||
}; | ||
Web3ProviderEngine.prototype._getBlockByNumber = function (blockNumber) { | ||
@@ -182,0 +208,0 @@ var req = create_payload_1.createPayload({ method: 'eth_getBlockByNumber', params: [blockNumber, false], skipCache: true }); |
{ | ||
"name": "@bitski/provider-engine", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/BitskiCo/provider-engine", |
@@ -19,2 +19,5 @@ import eachSeries from 'async/eachSeries'; | ||
// The latest block number we have received | ||
public currentBlockNumber?: string; | ||
protected _blockTracker: PollingBlockTracker; | ||
@@ -25,2 +28,8 @@ protected _ready: Stoplight; | ||
// Number of milliseconds to wait before retrying | ||
private blockTimeout = 300; | ||
// Maximum attempts to load a block | ||
private maxBlockRetries = 3; | ||
constructor(opts?: ProviderEngineOptions) { | ||
@@ -66,14 +75,4 @@ super(); | ||
this._blockTracker.on('latest', (blockNumber) => { | ||
// get block body | ||
this._getBlockByNumber(blockNumber).then((blockResponse) => { | ||
const block = blockResponse.result; | ||
const bufferBlock = toBufferBlock(block); | ||
// set current + emit "block" event | ||
this._setCurrentBlock(bufferBlock); | ||
// emit other events | ||
this.emit('rawBlock', block); | ||
this.emit('latest', block); | ||
}).catch((err) => { | ||
this.emit('error', err); | ||
}); | ||
this.currentBlockNumber = blockNumber; | ||
this.loadBlock(blockNumber); | ||
}); | ||
@@ -190,2 +189,32 @@ | ||
// Tries to get the block payload recursively | ||
protected loadBlock(blockNumber: string, callCount: number = 0) { | ||
this._getBlockByNumber(blockNumber).then((blockResponse) => { | ||
// Result can be null if the block hasn't fully propagated to the nodes | ||
if (blockResponse.result) { | ||
this.updateBlock(blockResponse.result); | ||
} else if (callCount < this.maxBlockRetries && blockNumber === this.currentBlockNumber) { | ||
// Only call recursively if the current block number is still the same | ||
// and if we are under the retry limit. | ||
setTimeout(() => { | ||
this.loadBlock(blockNumber, callCount + 1); | ||
}, this.blockTimeout); | ||
} else { | ||
throw new Error(`Could not load block ${blockNumber} after 3 tries`); | ||
} | ||
}).catch((err) => { | ||
this.emit('error', err); | ||
}); | ||
} | ||
// Parse the block into a buffer representation and update subscribers. | ||
protected updateBlock(block: any) { | ||
const bufferBlock = toBufferBlock(block); | ||
// set current + emit "block" event | ||
this._setCurrentBlock(bufferBlock); | ||
// emit other events | ||
this.emit('rawBlock', block); | ||
this.emit('latest', block); | ||
} | ||
protected _getBlockByNumber(blockNumber): Promise<JSONRPCResponse> { | ||
@@ -192,0 +221,0 @@ const req = createPayload({ method: 'eth_getBlockByNumber', params: [blockNumber, false], skipCache: true }); |
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
237265
5997