From 623533ab1508c93f0c347f0c4a1002ce93ff1ae2 Mon Sep 17 00:00:00 2001 From: kumavis Date: Wed, 16 May 2018 21:13:53 -0700 Subject: recent-blocks - update for eth-block-tracker@4 --- app/scripts/controllers/recent-blocks.js | 41 +++++++++++--------------------- 1 file changed, 14 insertions(+), 27 deletions(-) (limited to 'app/scripts/controllers/recent-blocks.js') diff --git a/app/scripts/controllers/recent-blocks.js b/app/scripts/controllers/recent-blocks.js index 1377c1ba9..4101814eb 100644 --- a/app/scripts/controllers/recent-blocks.js +++ b/app/scripts/controllers/recent-blocks.js @@ -3,6 +3,8 @@ const extend = require('xtend') const BN = require('ethereumjs-util').BN const EthQuery = require('eth-query') const log = require('loglevel') +const pify = require('pify') +const timeout = (duration) => new Promise(resolve => setTimeout(duration, resolve)) class RecentBlocksController { @@ -34,7 +36,7 @@ class RecentBlocksController { }, opts.initState) this.store = new ObservableStore(initState) - this.blockTracker.on('block', this.processBlock.bind(this)) + this.blockTracker.on('latest', this.processBlock.bind(this)) this.backfill() } @@ -55,7 +57,10 @@ class RecentBlocksController { * @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 state = this.store.getState() @@ -118,17 +123,16 @@ class RecentBlocksController { * @returns {Promise} Promises undefined */ async backfill() { - this.blockTracker.once('block', async (block) => { - let blockNum = block.number + this.blockTracker.once('latest', async (blockNumberHex) => { let recentBlocks + const blockNumber = Number.parseInt(blockNumberHex, 16) let state = this.store.getState() recentBlocks = state.recentBlocks while (recentBlocks.length < this.historyLength) { try { - let blockNumBn = new BN(blockNum.substr(2), 16) - const newNum = blockNumBn.subn(1).toString(10) - const newBlock = await this.getBlockByNumber(newNum) + const prevBlockNumber = blockNumber - 1 + const newBlock = await this.getBlockByNumber(prevBlockNumber) if (newBlock) { this.backfillBlock(newBlock) @@ -140,23 +144,11 @@ class RecentBlocksController { } catch (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} Promises undefined - * - */ - async wait () { - return new Promise((resolve) => { - setTimeout(resolve, 100) - }) - } - /** * Uses EthQuery to get a block that has a given block number. * @@ -165,13 +157,8 @@ class RecentBlocksController { * */ async getBlockByNumber (number) { - const bn = new BN(number) - return new Promise((resolve, reject) => { - this.ethQuery.getBlockByNumber('0x' + bn.toString(16), true, (err, block) => { - if (err) reject(err) - resolve(block) - }) - }) + const blockNumberHex = '0x' + number.toString(16) + return await pify(this.ethQuery.getBlockByNumber).call(this.ethQuery, blockNumberHex, true) } } -- cgit From 41c04ef7219eafeca71bcd577abe6bca3a637a89 Mon Sep 17 00:00:00 2001 From: kumavis Date: Wed, 16 May 2018 23:13:25 -0700 Subject: controllers - recent-blocks - fix pifyd setTimeout args --- app/scripts/controllers/recent-blocks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/scripts/controllers/recent-blocks.js') diff --git a/app/scripts/controllers/recent-blocks.js b/app/scripts/controllers/recent-blocks.js index 4101814eb..28eba0f26 100644 --- a/app/scripts/controllers/recent-blocks.js +++ b/app/scripts/controllers/recent-blocks.js @@ -4,7 +4,7 @@ const BN = require('ethereumjs-util').BN const EthQuery = require('eth-query') const log = require('loglevel') const pify = require('pify') -const timeout = (duration) => new Promise(resolve => setTimeout(duration, resolve)) +const timeout = (duration) => new Promise(resolve => setTimeout(resolve, duration)) class RecentBlocksController { -- cgit From 3084dc47d10e3e455c924e5aad0b0961c500ec8d Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 17 May 2018 00:13:20 -0700 Subject: recent-blocks - fix backfill blockNumber tracking --- app/scripts/controllers/recent-blocks.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'app/scripts/controllers/recent-blocks.js') diff --git a/app/scripts/controllers/recent-blocks.js b/app/scripts/controllers/recent-blocks.js index 28eba0f26..9a215d0e5 100644 --- a/app/scripts/controllers/recent-blocks.js +++ b/app/scripts/controllers/recent-blocks.js @@ -1,6 +1,5 @@ const ObservableStore = require('obs-store') const extend = require('xtend') -const BN = require('ethereumjs-util').BN const EthQuery = require('eth-query') const log = require('loglevel') const pify = require('pify') @@ -125,7 +124,7 @@ class RecentBlocksController { async backfill() { this.blockTracker.once('latest', async (blockNumberHex) => { let recentBlocks - const blockNumber = Number.parseInt(blockNumberHex, 16) + let blockNumber = Number.parseInt(blockNumberHex, 16) let state = this.store.getState() recentBlocks = state.recentBlocks @@ -136,7 +135,7 @@ class RecentBlocksController { if (newBlock) { this.backfillBlock(newBlock) - blockNum = newBlock.number + blockNumber = Number.parseInt(newBlock.number, 16) } state = this.store.getState() -- cgit From 53b946362a59fbaea1a3ae3a7e7af97f427afed4 Mon Sep 17 00:00:00 2001 From: kumavis Date: Wed, 23 May 2018 16:20:35 -0700 Subject: controllers - recent-blocks - doc update --- app/scripts/controllers/recent-blocks.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'app/scripts/controllers/recent-blocks.js') diff --git a/app/scripts/controllers/recent-blocks.js b/app/scripts/controllers/recent-blocks.js index a6208b89c..e0b5551dd 100644 --- a/app/scripts/controllers/recent-blocks.js +++ b/app/scripts/controllers/recent-blocks.js @@ -8,7 +8,7 @@ class RecentBlocksController { /** * Controller responsible for storing, updating and managing the recent history of blocks. Blocks are back filled - * upon the controller's construction and then the list is updated when the given block tracker gets a 'block' event + * upon the controller's construction and then the list is updated when the given block tracker gets a 'latest' event * (indicating that there is a new block to process). * * @typedef {Object} RecentBlocksController @@ -16,7 +16,7 @@ class RecentBlocksController { * @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, - * listens for 'block' events so that new blocks can be processed and added to storage. + * listens for 'latest' events so that new blocks can be processed and added to storage. * @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 @@ -111,9 +111,9 @@ class RecentBlocksController { } /** - * On this.blockTracker's first 'block' event after this RecentBlocksController's instantiation, the store.recentBlocks + * On this.blockTracker's first 'latest' event after this RecentBlocksController's instantiation, the store.recentBlocks * array is populated with this.historyLength number of blocks. The block number of the this.blockTracker's first - * 'block' event is used to iteratively generate all the numbers of the previous blocks, which are obtained by querying + * 'latest' event is used to iteratively generate all the numbers of the previous blocks, which are obtained by querying * 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. -- cgit From 22e59af741a43ab9a9dc8e96415788f681a8efda Mon Sep 17 00:00:00 2001 From: kumavis Date: Wed, 23 May 2018 22:32:33 -0700 Subject: controllers - recent-blocks - ensure full blocks --- app/scripts/controllers/recent-blocks.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/scripts/controllers/recent-blocks.js') diff --git a/app/scripts/controllers/recent-blocks.js b/app/scripts/controllers/recent-blocks.js index e0b5551dd..215638666 100644 --- a/app/scripts/controllers/recent-blocks.js +++ b/app/scripts/controllers/recent-blocks.js @@ -57,7 +57,7 @@ class RecentBlocksController { */ async processBlock (newBlockNumberHex) { const newBlockNumber = Number.parseInt(newBlockNumberHex, 16) - const newBlock = await this.getBlockByNumber(newBlockNumber) + const newBlock = await this.getBlockByNumber(newBlockNumber, true) const block = this.mapTransactionsToPrices(newBlock) @@ -128,7 +128,7 @@ class RecentBlocksController { const targetBlockNumbers = Array(blocksToFetch).fill().map((_, index) => prevBlockNumber - index) await Promise.all(targetBlockNumbers.map(async (targetBlockNumber) => { try { - const newBlock = await this.getBlockByNumber(targetBlockNumber) + const newBlock = await this.getBlockByNumber(targetBlockNumber, true) if (newBlock) { this.backfillBlock(newBlock) -- cgit From ee800de025397af7958d8e8e382b31b1d09c338c Mon Sep 17 00:00:00 2001 From: kumavis Date: Wed, 23 May 2018 22:46:20 -0700 Subject: controllers - recent-blocks - wrap block-tracker event in try-catch --- app/scripts/controllers/recent-blocks.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'app/scripts/controllers/recent-blocks.js') diff --git a/app/scripts/controllers/recent-blocks.js b/app/scripts/controllers/recent-blocks.js index 215638666..e256c62e0 100644 --- a/app/scripts/controllers/recent-blocks.js +++ b/app/scripts/controllers/recent-blocks.js @@ -34,7 +34,13 @@ class RecentBlocksController { }, opts.initState) this.store = new ObservableStore(initState) - this.blockTracker.on('latest', this.processBlock.bind(this)) + this.blockTracker.on('latest', async (newBlockNumberHex) => { + try { + await this.processBlock(newBlockNumberHex) + } catch (err) { + log.error(err) + } + }) this.backfill() } -- cgit From 49ef93b99158b9fae21d841828d7e7d105f837df Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 24 May 2018 13:43:16 -0700 Subject: controllers - recent-blocks - guard against empty block --- app/scripts/controllers/recent-blocks.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'app/scripts/controllers/recent-blocks.js') diff --git a/app/scripts/controllers/recent-blocks.js b/app/scripts/controllers/recent-blocks.js index e256c62e0..4009c35ae 100644 --- a/app/scripts/controllers/recent-blocks.js +++ b/app/scripts/controllers/recent-blocks.js @@ -64,6 +64,7 @@ class RecentBlocksController { async processBlock (newBlockNumberHex) { const newBlockNumber = Number.parseInt(newBlockNumberHex, 16) const newBlock = await this.getBlockByNumber(newBlockNumber, true) + if (!newBlock) return const block = this.mapTransactionsToPrices(newBlock) @@ -135,10 +136,9 @@ class RecentBlocksController { await Promise.all(targetBlockNumbers.map(async (targetBlockNumber) => { try { const newBlock = await this.getBlockByNumber(targetBlockNumber, true) + if (!newBlock) return - if (newBlock) { - this.backfillBlock(newBlock) - } + this.backfillBlock(newBlock) } catch (e) { log.error(e) } -- cgit