mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
recent-blocks - update for eth-block-tracker@4
This commit is contained in:
parent
3e04840a71
commit
623533ab15
@ -3,6 +3,8 @@ const extend = require('xtend')
|
|||||||
const BN = require('ethereumjs-util').BN
|
const BN = require('ethereumjs-util').BN
|
||||||
const EthQuery = require('eth-query')
|
const EthQuery = require('eth-query')
|
||||||
const log = require('loglevel')
|
const log = require('loglevel')
|
||||||
|
const pify = require('pify')
|
||||||
|
const timeout = (duration) => new Promise(resolve => setTimeout(duration, resolve))
|
||||||
|
|
||||||
class RecentBlocksController {
|
class RecentBlocksController {
|
||||||
|
|
||||||
@ -34,7 +36,7 @@ class RecentBlocksController {
|
|||||||
}, opts.initState)
|
}, opts.initState)
|
||||||
this.store = new ObservableStore(initState)
|
this.store = new ObservableStore(initState)
|
||||||
|
|
||||||
this.blockTracker.on('block', this.processBlock.bind(this))
|
this.blockTracker.on('latest', this.processBlock.bind(this))
|
||||||
this.backfill()
|
this.backfill()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,7 +57,10 @@ class RecentBlocksController {
|
|||||||
* @param {object} newBlock The new block to modify and add to the recentBlocks array
|
* @param {object} newBlock The new block to modify and add to the recentBlocks array
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
processBlock (newBlock) {
|
async processBlock (newBlockNumberHex) {
|
||||||
|
const newBlockNumber = Number.parseInt(newBlockNumberHex, 16)
|
||||||
|
const newBlock = await this.getBlockByNumber(newBlockNumber)
|
||||||
|
|
||||||
const block = this.mapTransactionsToPrices(newBlock)
|
const block = this.mapTransactionsToPrices(newBlock)
|
||||||
|
|
||||||
const state = this.store.getState()
|
const state = this.store.getState()
|
||||||
@ -118,17 +123,16 @@ class RecentBlocksController {
|
|||||||
* @returns {Promise<void>} Promises undefined
|
* @returns {Promise<void>} Promises undefined
|
||||||
*/
|
*/
|
||||||
async backfill() {
|
async backfill() {
|
||||||
this.blockTracker.once('block', async (block) => {
|
this.blockTracker.once('latest', async (blockNumberHex) => {
|
||||||
let blockNum = block.number
|
|
||||||
let recentBlocks
|
let recentBlocks
|
||||||
|
const blockNumber = Number.parseInt(blockNumberHex, 16)
|
||||||
let state = this.store.getState()
|
let state = this.store.getState()
|
||||||
recentBlocks = state.recentBlocks
|
recentBlocks = state.recentBlocks
|
||||||
|
|
||||||
while (recentBlocks.length < this.historyLength) {
|
while (recentBlocks.length < this.historyLength) {
|
||||||
try {
|
try {
|
||||||
let blockNumBn = new BN(blockNum.substr(2), 16)
|
const prevBlockNumber = blockNumber - 1
|
||||||
const newNum = blockNumBn.subn(1).toString(10)
|
const newBlock = await this.getBlockByNumber(prevBlockNumber)
|
||||||
const newBlock = await this.getBlockByNumber(newNum)
|
|
||||||
|
|
||||||
if (newBlock) {
|
if (newBlock) {
|
||||||
this.backfillBlock(newBlock)
|
this.backfillBlock(newBlock)
|
||||||
@ -140,23 +144,11 @@ class RecentBlocksController {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
log.error(e)
|
log.error(e)
|
||||||
}
|
}
|
||||||
await this.wait()
|
await timeout(100)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* A helper for this.backfill. Provides an easy way to ensure a 100 millisecond delay using await
|
|
||||||
*
|
|
||||||
* @returns {Promise<void>} Promises undefined
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
async wait () {
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
setTimeout(resolve, 100)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uses EthQuery to get a block that has a given block number.
|
* Uses EthQuery to get a block that has a given block number.
|
||||||
*
|
*
|
||||||
@ -165,13 +157,8 @@ class RecentBlocksController {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
async getBlockByNumber (number) {
|
async getBlockByNumber (number) {
|
||||||
const bn = new BN(number)
|
const blockNumberHex = '0x' + number.toString(16)
|
||||||
return new Promise((resolve, reject) => {
|
return await pify(this.ethQuery.getBlockByNumber).call(this.ethQuery, blockNumberHex, true)
|
||||||
this.ethQuery.getBlockByNumber('0x' + bn.toString(16), true, (err, block) => {
|
|
||||||
if (err) reject(err)
|
|
||||||
resolve(block)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user