2017-12-18 01:36:03 +01:00
|
|
|
const ObservableStore = require('obs-store')
|
|
|
|
const extend = require('xtend')
|
2018-01-12 19:25:36 +01:00
|
|
|
const EthQuery = require('eth-query')
|
2018-04-12 23:06:59 +02:00
|
|
|
const log = require('loglevel')
|
2018-05-17 06:13:53 +02:00
|
|
|
const pify = require('pify')
|
2019-02-08 22:24:55 +01:00
|
|
|
const {
|
|
|
|
ROPSTEN,
|
|
|
|
RINKEBY,
|
|
|
|
KOVAN,
|
|
|
|
MAINNET,
|
2019-04-17 19:34:49 +02:00
|
|
|
GOERLI,
|
2019-02-08 22:24:55 +01:00
|
|
|
} = require('./network/enums')
|
2019-04-17 19:34:49 +02:00
|
|
|
const INFURA_PROVIDER_TYPES = [ROPSTEN, RINKEBY, KOVAN, MAINNET, GOERLI]
|
2019-02-08 22:24:55 +01:00
|
|
|
|
2017-12-18 01:36:03 +01:00
|
|
|
|
|
|
|
class RecentBlocksController {
|
|
|
|
|
2018-04-19 17:38:56 +02:00
|
|
|
/**
|
|
|
|
* Controller responsible for storing, updating and managing the recent history of blocks. Blocks are back filled
|
2018-05-24 01:20:35 +02:00
|
|
|
* upon the controller's construction and then the list is updated when the given block tracker gets a 'latest' event
|
2018-04-19 17:38:56 +02:00
|
|
|
* (indicating that there is a new block to process).
|
|
|
|
*
|
|
|
|
* @typedef {Object} RecentBlocksController
|
|
|
|
* @param {object} opts Contains objects necessary for tracking blocks and querying the blockchain
|
|
|
|
* @param {BlockTracker} opts.blockTracker Contains objects necessary for tracking blocks and querying the blockchain
|
|
|
|
* @param {BlockTracker} opts.provider The provider used to create a new EthQuery instance.
|
|
|
|
* @property {BlockTracker} blockTracker Points to the passed BlockTracker. On RecentBlocksController construction,
|
2018-05-24 01:20:35 +02:00
|
|
|
* listens for 'latest' events so that new blocks can be processed and added to storage.
|
2018-04-19 17:38:56 +02:00
|
|
|
* @property {EthQuery} ethQuery Points to the EthQuery instance created with the passed provider
|
|
|
|
* @property {number} historyLength The maximum length of blocks to track
|
|
|
|
* @property {object} store Stores the recentBlocks
|
|
|
|
* @property {array} store.recentBlocks Contains all recent blocks, up to a total that is equal to this.historyLength
|
|
|
|
*
|
|
|
|
*/
|
2017-12-18 01:36:03 +01:00
|
|
|
constructor (opts = {}) {
|
2019-02-08 22:24:55 +01:00
|
|
|
const { blockTracker, provider, networkController } = opts
|
2017-12-18 01:36:03 +01:00
|
|
|
this.blockTracker = blockTracker
|
2018-01-12 19:25:36 +01:00
|
|
|
this.ethQuery = new EthQuery(provider)
|
2017-12-18 01:36:03 +01:00
|
|
|
this.historyLength = opts.historyLength || 40
|
|
|
|
|
|
|
|
const initState = extend({
|
|
|
|
recentBlocks: [],
|
|
|
|
}, opts.initState)
|
|
|
|
this.store = new ObservableStore(initState)
|
2019-02-08 22:24:55 +01:00
|
|
|
const blockListner = async (newBlockNumberHex) => {
|
2018-05-24 07:46:20 +02:00
|
|
|
try {
|
|
|
|
await this.processBlock(newBlockNumberHex)
|
|
|
|
} catch (err) {
|
|
|
|
log.error(err)
|
|
|
|
}
|
2019-02-08 22:24:55 +01:00
|
|
|
}
|
2019-02-12 20:05:06 +01:00
|
|
|
let isListening = false
|
2019-02-08 22:24:55 +01:00
|
|
|
const { type } = networkController.getProviderConfig()
|
|
|
|
if (!INFURA_PROVIDER_TYPES.includes(type) && type !== 'loading') {
|
|
|
|
this.blockTracker.on('latest', blockListner)
|
2019-02-12 20:05:06 +01:00
|
|
|
isListening = true
|
2019-02-08 22:24:55 +01:00
|
|
|
}
|
|
|
|
networkController.on('networkDidChange', (newType) => {
|
2019-02-12 20:05:06 +01:00
|
|
|
if (INFURA_PROVIDER_TYPES.includes(newType) && isListening) {
|
2019-02-08 22:24:55 +01:00
|
|
|
this.blockTracker.removeListener('latest', blockListner)
|
|
|
|
} else if (
|
|
|
|
!INFURA_PROVIDER_TYPES.includes(type) &&
|
|
|
|
type !== 'loading' &&
|
2019-02-12 20:05:06 +01:00
|
|
|
!isListening
|
2019-02-08 22:24:55 +01:00
|
|
|
) {
|
|
|
|
this.blockTracker.on('latest', blockListner)
|
|
|
|
|
|
|
|
}
|
2018-05-24 07:46:20 +02:00
|
|
|
})
|
2018-01-12 00:00:48 +01:00
|
|
|
this.backfill()
|
2017-12-18 01:36:03 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 17:38:56 +02:00
|
|
|
/**
|
|
|
|
* Sets store.recentBlocks to an empty array
|
|
|
|
*
|
|
|
|
*/
|
2017-12-18 01:36:03 +01:00
|
|
|
resetState () {
|
|
|
|
this.store.updateState({
|
|
|
|
recentBlocks: [],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-19 17:38:56 +02:00
|
|
|
/**
|
|
|
|
* Receives a new block and modifies it with this.mapTransactionsToPrices. Then adds that block to the recentBlocks
|
|
|
|
* array in storage. If the recentBlocks array contains the maximum number of blocks, the oldest block is removed.
|
|
|
|
*
|
|
|
|
* @param {object} newBlock The new block to modify and add to the recentBlocks array
|
|
|
|
*
|
|
|
|
*/
|
2018-05-17 06:13:53 +02:00
|
|
|
async processBlock (newBlockNumberHex) {
|
|
|
|
const newBlockNumber = Number.parseInt(newBlockNumberHex, 16)
|
2018-05-24 07:32:33 +02:00
|
|
|
const newBlock = await this.getBlockByNumber(newBlockNumber, true)
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!newBlock) {
|
|
|
|
return
|
|
|
|
}
|
2018-05-17 06:13:53 +02:00
|
|
|
|
2018-01-12 00:00:48 +01:00
|
|
|
const block = this.mapTransactionsToPrices(newBlock)
|
2017-12-19 21:22:48 +01:00
|
|
|
|
2017-12-18 01:36:03 +01:00
|
|
|
const state = this.store.getState()
|
2017-12-19 21:22:48 +01:00
|
|
|
state.recentBlocks.push(block)
|
2017-12-18 01:36:03 +01:00
|
|
|
|
|
|
|
while (state.recentBlocks.length > this.historyLength) {
|
|
|
|
state.recentBlocks.shift()
|
|
|
|
}
|
|
|
|
|
|
|
|
this.store.updateState(state)
|
|
|
|
}
|
2018-01-12 00:00:48 +01:00
|
|
|
|
2018-04-19 17:38:56 +02:00
|
|
|
/**
|
|
|
|
* Receives a new block and modifies it with this.mapTransactionsToPrices. Adds that block to the recentBlocks
|
|
|
|
* array in storage, but only if the recentBlocks array contains fewer than the maximum permitted.
|
|
|
|
*
|
|
|
|
* Unlike this.processBlock, backfillBlock adds the modified new block to the beginning of the recent block array.
|
|
|
|
*
|
|
|
|
* @param {object} newBlock The new block to modify and add to the beginning of the recentBlocks array
|
|
|
|
*
|
|
|
|
*/
|
2018-01-12 00:00:48 +01:00
|
|
|
backfillBlock (newBlock) {
|
|
|
|
const block = this.mapTransactionsToPrices(newBlock)
|
|
|
|
|
|
|
|
const state = this.store.getState()
|
|
|
|
|
|
|
|
if (state.recentBlocks.length < this.historyLength) {
|
|
|
|
state.recentBlocks.unshift(block)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.store.updateState(state)
|
|
|
|
}
|
|
|
|
|
2018-04-19 17:38:56 +02:00
|
|
|
/**
|
|
|
|
* Receives a block and gets the gasPrice of each of its transactions. These gas prices are added to the block at a
|
|
|
|
* new property, and the block's transactions are removed.
|
|
|
|
*
|
|
|
|
* @param {object} newBlock The block to modify. It's transaction array will be replaced by a gasPrices array.
|
|
|
|
* @returns {object} The modified block.
|
|
|
|
*
|
|
|
|
*/
|
2018-01-12 00:00:48 +01:00
|
|
|
mapTransactionsToPrices (newBlock) {
|
|
|
|
const block = extend(newBlock, {
|
|
|
|
gasPrices: newBlock.transactions.map((tx) => {
|
|
|
|
return tx.gasPrice
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
delete block.transactions
|
|
|
|
return block
|
|
|
|
}
|
|
|
|
|
2018-04-19 17:38:56 +02:00
|
|
|
/**
|
2018-05-24 01:20:35 +02:00
|
|
|
* On this.blockTracker's first 'latest' event after this RecentBlocksController's instantiation, the store.recentBlocks
|
2018-04-19 17:38:56 +02:00
|
|
|
* array is populated with this.historyLength number of blocks. The block number of the this.blockTracker's first
|
2018-05-24 01:20:35 +02:00
|
|
|
* 'latest' event is used to iteratively generate all the numbers of the previous blocks, which are obtained by querying
|
2018-04-19 17:38:56 +02:00
|
|
|
* the blockchain. These blocks are backfilled so that the recentBlocks array is ordered from oldest to newest.
|
|
|
|
*
|
|
|
|
* Each iteration over the block numbers is delayed by 100 milliseconds.
|
|
|
|
*
|
|
|
|
* @returns {Promise<void>} Promises undefined
|
|
|
|
*/
|
2018-07-03 00:49:33 +02:00
|
|
|
async backfill () {
|
2018-05-17 06:13:53 +02:00
|
|
|
this.blockTracker.once('latest', async (blockNumberHex) => {
|
2018-05-23 01:22:10 +02:00
|
|
|
const currentBlockNumber = Number.parseInt(blockNumberHex, 16)
|
2018-05-22 10:58:36 +02:00
|
|
|
const blocksToFetch = Math.min(currentBlockNumber, this.historyLength)
|
|
|
|
const prevBlockNumber = currentBlockNumber - 1
|
|
|
|
const targetBlockNumbers = Array(blocksToFetch).fill().map((_, index) => prevBlockNumber - index)
|
|
|
|
await Promise.all(targetBlockNumbers.map(async (targetBlockNumber) => {
|
2018-01-12 00:00:48 +01:00
|
|
|
try {
|
2018-05-24 07:32:33 +02:00
|
|
|
const newBlock = await this.getBlockByNumber(targetBlockNumber, true)
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!newBlock) {
|
|
|
|
return
|
|
|
|
}
|
2018-01-12 00:00:48 +01:00
|
|
|
|
2018-05-24 22:43:16 +02:00
|
|
|
this.backfillBlock(newBlock)
|
2018-01-12 00:00:48 +01:00
|
|
|
} catch (e) {
|
|
|
|
log.error(e)
|
|
|
|
}
|
2018-05-22 10:58:36 +02:00
|
|
|
}))
|
2018-01-12 00:00:48 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-19 17:38:56 +02:00
|
|
|
/**
|
|
|
|
* Uses EthQuery to get a block that has a given block number.
|
|
|
|
*
|
|
|
|
* @param {number} number The number of the block to get
|
|
|
|
* @returns {Promise<object>} Promises A block with the passed number
|
|
|
|
*
|
|
|
|
*/
|
2018-01-12 00:00:48 +01:00
|
|
|
async getBlockByNumber (number) {
|
2018-05-17 06:13:53 +02:00
|
|
|
const blockNumberHex = '0x' + number.toString(16)
|
|
|
|
return await pify(this.ethQuery.getBlockByNumber).call(this.ethQuery, blockNumberHex, true)
|
2018-01-12 00:00:48 +01:00
|
|
|
}
|
|
|
|
|
2017-12-18 01:36:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = RecentBlocksController
|