mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Merge pull request #4337 from MetaMask/recent-blocks-parallel
controllers - recent-blocks - pull first historical blocks in parallel
This commit is contained in:
commit
ed01c6c826
@ -119,29 +119,21 @@ class RecentBlocksController {
|
|||||||
*/
|
*/
|
||||||
async backfill() {
|
async backfill() {
|
||||||
this.blockTracker.once('block', async (block) => {
|
this.blockTracker.once('block', async (block) => {
|
||||||
let blockNum = block.number
|
const currentBlockNumber = Number.parseInt(block.number, 16)
|
||||||
let recentBlocks
|
const blocksToFetch = Math.min(currentBlockNumber, this.historyLength)
|
||||||
let state = this.store.getState()
|
const prevBlockNumber = currentBlockNumber - 1
|
||||||
recentBlocks = state.recentBlocks
|
const targetBlockNumbers = Array(blocksToFetch).fill().map((_, index) => prevBlockNumber - index)
|
||||||
|
await Promise.all(targetBlockNumbers.map(async (targetBlockNumber) => {
|
||||||
while (recentBlocks.length < this.historyLength) {
|
|
||||||
try {
|
try {
|
||||||
let blockNumBn = new BN(blockNum.substr(2), 16)
|
const newBlock = await this.getBlockByNumber(targetBlockNumber)
|
||||||
const newNum = blockNumBn.subn(1).toString(10)
|
|
||||||
const newBlock = await this.getBlockByNumber(newNum)
|
|
||||||
|
|
||||||
if (newBlock) {
|
if (newBlock) {
|
||||||
this.backfillBlock(newBlock)
|
this.backfillBlock(newBlock)
|
||||||
blockNum = newBlock.number
|
|
||||||
}
|
}
|
||||||
|
|
||||||
state = this.store.getState()
|
|
||||||
recentBlocks = state.recentBlocks
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log.error(e)
|
log.error(e)
|
||||||
}
|
}
|
||||||
await this.wait()
|
}))
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user