This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 678
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Eslint and prettier formatting, cleaned up linter errors throughout application, and fixed any broken tests.
- Loading branch information
1 parent
398125f
commit 7fb71a8
Showing
81 changed files
with
7,018 additions
and
4,737 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,71 @@ | ||
// this replaces `eth-block-tracker` in the provider-engine, as that block tracker is meant to work with | ||
// an external provider instance | ||
|
||
const EventEmitter = require('events') | ||
const BlockSerializer = require('./database/blockserializer') | ||
var blockHelper = require('./utils/block_helper'); | ||
const to = require('./utils/to') | ||
const EventEmitter = require("events"); | ||
var blockHelper = require("./utils/block_helper"); | ||
|
||
function GanacheBlockTracker(opts) { | ||
opts = opts || {} | ||
EventEmitter.apply(this) | ||
if (!opts.blockchain) throw new Error('RpcBlockTracker - no blockchain specified.') | ||
if (!opts.blockchain.on) throw new Error('RpcBlockTracker - blockchain is not an EventEmitter.') | ||
this._blockchain = opts.blockchain | ||
this.start = this.start.bind(this) | ||
this.stop = this.stop.bind(this) | ||
this.getTrackingBlock = this.getTrackingBlock.bind(this) | ||
this.awaitCurrentBlock = this.awaitCurrentBlock.bind(this) | ||
this._setCurrentBlock = this._setCurrentBlock.bind(this) | ||
opts = opts || {}; | ||
EventEmitter.apply(this); | ||
if (!opts.blockchain) { | ||
throw new Error("RpcBlockTracker - no blockchain specified."); | ||
} | ||
if (!opts.blockchain.on) { | ||
throw new Error("RpcBlockTracker - blockchain is not an EventEmitter."); | ||
} | ||
this._blockchain = opts.blockchain; | ||
this.start = this.start.bind(this); | ||
this.stop = this.stop.bind(this); | ||
this.getTrackingBlock = this.getTrackingBlock.bind(this); | ||
this.awaitCurrentBlock = this.awaitCurrentBlock.bind(this); | ||
this._setCurrentBlock = this._setCurrentBlock.bind(this); | ||
} | ||
|
||
GanacheBlockTracker.prototype = Object.create(EventEmitter.prototype) | ||
GanacheBlockTracker.prototype.constructor = GanacheBlockTracker | ||
GanacheBlockTracker.prototype = Object.create(EventEmitter.prototype); | ||
GanacheBlockTracker.prototype.constructor = GanacheBlockTracker; | ||
|
||
GanacheBlockTracker.prototype.getTrackingBlock = function() { | ||
return this._currentBlock | ||
} | ||
return this._currentBlock; | ||
}; | ||
|
||
GanacheBlockTracker.prototype.getCurrentBlock = function() { | ||
return this._currentBlock | ||
} | ||
return this._currentBlock; | ||
}; | ||
|
||
GanacheBlockTracker.prototype.awaitCurrentBlock = function() { | ||
const self = this | ||
const self = this; | ||
// return if available | ||
if (this._currentBlock) return this._currentBlock | ||
if (this._currentBlock) { | ||
return this._currentBlock; | ||
} | ||
// wait for "sync" event | ||
return new Promise(resolve => this.once('block', resolve)) | ||
.then(() => self._currentBlock) | ||
} | ||
return new Promise((resolve) => this.once("block", resolve)).then(() => self._currentBlock); | ||
}; | ||
|
||
GanacheBlockTracker.prototype.start = function(opts = {}) { | ||
this._blockchain.on('block', this._setCurrentBlock) | ||
return Promise.resolve() | ||
} | ||
this._blockchain.on("block", this._setCurrentBlock); | ||
return Promise.resolve(); | ||
}; | ||
|
||
GanacheBlockTracker.prototype.stop = function() { | ||
this._isRunning = false | ||
this._blockchain.removeListener('block', this._setCurrentBlock) | ||
} | ||
this._isRunning = false; | ||
this._blockchain.removeListener("block", this._setCurrentBlock); | ||
}; | ||
|
||
// | ||
// private | ||
// | ||
// | ||
// private | ||
// | ||
|
||
GanacheBlockTracker.prototype._setCurrentBlock = function(newBlock) { | ||
let block = blockHelper.toJSON(newBlock, true) | ||
if (this._currentBlock && (this._currentBlock.hash === block.hash)) return | ||
const oldBlock = this._currentBlock | ||
this._currentBlock = block | ||
this.emit('latest', block) | ||
this.emit('sync', { block, oldBlock }) | ||
this.emit('block', block) | ||
} | ||
let block = blockHelper.toJSON(newBlock, true); | ||
if (this._currentBlock && this._currentBlock.hash === block.hash) { | ||
return; | ||
} | ||
const oldBlock = this._currentBlock; | ||
this._currentBlock = block; | ||
this.emit("latest", block); | ||
this.emit("sync", { block, oldBlock }); | ||
this.emit("block", block); | ||
}; | ||
|
||
module.exports = GanacheBlockTracker | ||
module.exports = GanacheBlockTracker; |
Oops, something went wrong.