From 7f5ab530bbde7512b7be83fceee6b760f72df89c Mon Sep 17 00:00:00 2001 From: Ezerous Date: Mon, 31 Aug 2020 21:08:04 +0300 Subject: [PATCH] Removed legacy block polling --- package.json | 1 - src/blocks/blocksSaga.js | 79 ++------------------------ src/blocks/constants.js | 2 - src/defaultOptions.js | 3 - src/drizzleStatus/drizzleStatusSaga.js | 14 +---- 5 files changed, 6 insertions(+), 93 deletions(-) diff --git a/package.json b/package.json index 8e3497c..dbe38da 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,6 @@ "repository": "github:Ezerous/drizzle", "dependencies": { "deepmerge": "4.2.2", - "eth-block-tracker": "4.4.3", "is-plain-object": "4.1.1", "redux": "4.0.5", "redux-saga": "1.1.3", diff --git a/src/blocks/blocksSaga.js b/src/blocks/blocksSaga.js index 86fcf09..7b424bc 100644 --- a/src/blocks/blocksSaga.js +++ b/src/blocks/blocksSaga.js @@ -1,14 +1,11 @@ import { END, eventChannel } from 'redux-saga' import { call, put, take, takeEvery, takeLatest, all } from 'redux-saga/effects' -import PollingBlockTracker from 'eth-block-tracker' import * as BlocksActions from './constants' -import * as ContractActions from '../contracts/constants' /* * Listen for Blocks */ - -export function createBlockChannel ({ drizzle, web3, syncAlways }) { +export function createBlockChannel ({ drizzle, web3 }) { return eventChannel(emit => { const blockEvents = web3.eth .subscribe('newBlockHeaders', (error, result) => { @@ -22,7 +19,7 @@ export function createBlockChannel ({ drizzle, web3, syncAlways }) { } }) .on('data', blockHeader => { - emit({ type: BlocksActions.BLOCK_RECEIVED, blockHeader, drizzle, web3, syncAlways }) + emit({ type: BlocksActions.BLOCK_RECEIVED, blockHeader, drizzle, web3 }) }) .on('error', error => { emit({ type: BlocksActions.BLOCKS_FAILED, error }) @@ -37,71 +34,10 @@ export function createBlockChannel ({ drizzle, web3, syncAlways }) { }) } -function * callCreateBlockChannel ({ drizzle, web3, syncAlways }) { +function * callCreateBlockChannel ({ drizzle, web3 }) { const blockChannel = yield call(createBlockChannel, { drizzle, - web3, - syncAlways - }) - - try { - while (true) { - var event = yield take(blockChannel) - yield put(event) - } - } finally { - blockChannel.close() - } -} - -/* - * Poll for Blocks - */ - -export function createBlockPollChannel ({ - drizzle, - interval, - web3, - syncAlways -}) { - return eventChannel(emit => { - const blockTracker = new PollingBlockTracker({ - provider: web3.currentProvider, - pollingInterval: interval - }) - - blockTracker.on('block', block => { - emit({ type: BlocksActions.BLOCK_FOUND, block, drizzle, web3, syncAlways }) - }) - - blockTracker.start().catch(error => { - emit({ type: BlocksActions.BLOCKS_FAILED, error }) - emit(END) - }) - - const unsubscribe = () => { - blockTracker.stop().catch(_ => { - // PollingBlockTracker assumes there is an outstanding event subscription. - // However for our tests we start and stop a PollingBlockTracker in succession - // that triggers an error. - }) - } - - return unsubscribe - }) -} - -function * callCreateBlockPollChannel ({ - drizzle, - interval, - web3, - syncAlways -}) { - const blockChannel = yield call(createBlockPollChannel, { - drizzle, - interval, - web3, - syncAlways + web3 }) try { @@ -117,7 +53,6 @@ function * callCreateBlockPollChannel ({ /* * Process Blocks */ - function * processBlockHeader ({ blockHeader, drizzle, web3, syncAlways }) { const blockNumber = blockHeader.number @@ -133,7 +68,7 @@ function * processBlockHeader ({ blockHeader, drizzle, web3, syncAlways }) { } } -function * processBlock ({ block, drizzle, web3, syncAlways }) { +function * processBlock ({ block, drizzle, syncAlways }) { try { // Emit block for addition to store. // Regardless of syncing success/failure, this is still the latest block. @@ -182,10 +117,6 @@ function * blocksSaga () { // Block Subscriptions yield takeLatest(BlocksActions.BLOCKS_LISTENING, callCreateBlockChannel) yield takeEvery(BlocksActions.BLOCK_RECEIVED, processBlockHeader) - - // Block Polling - yield takeLatest(BlocksActions.BLOCKS_POLLING, callCreateBlockPollChannel) - yield takeEvery(BlocksActions.BLOCK_FOUND, processBlock) } export default blocksSaga diff --git a/src/blocks/constants.js b/src/blocks/constants.js index 2045f88..2ad9e59 100644 --- a/src/blocks/constants.js +++ b/src/blocks/constants.js @@ -2,6 +2,4 @@ export const BLOCK_PROCESSING = 'BLOCK_PROCESSING' export const BLOCKS_FAILED = 'BLOCKS_FAILED' export const BLOCK_FAILED = 'BLOCK_FAILED' export const BLOCK_RECEIVED = 'BLOCK_RECEIVED' -export const BLOCK_FOUND = 'BLOCK_FOUND' export const BLOCKS_LISTENING = 'BLOCKS_LISTENING' -export const BLOCKS_POLLING = 'BLOCKS_POLLING' diff --git a/src/defaultOptions.js b/src/defaultOptions.js index 579d84b..b089253 100644 --- a/src/defaultOptions.js +++ b/src/defaultOptions.js @@ -7,9 +7,6 @@ const defaultOptions = { }, contracts: [], events: {}, - polls: { - blocks: 3000 - }, syncAlways: false, networkWhitelist: [] } diff --git a/src/drizzleStatus/drizzleStatusSaga.js b/src/drizzleStatus/drizzleStatusSaga.js index 89fc27b..3721513 100644 --- a/src/drizzleStatus/drizzleStatusSaga.js +++ b/src/drizzleStatus/drizzleStatusSaga.js @@ -48,19 +48,7 @@ export function * initializeDrizzle (action) { yield call([drizzle, drizzle.addContract], contractConfig, events) } - const syncAlways = options.syncAlways - - // Protect server-side environments by ensuring ethereum access is - // guarded by isMetaMask which should only be in browser environment. - // - if (web3.currentProvider.isMetaMask && !window.ethereum) { - // Using old MetaMask, attempt block polling. - const interval = options.polls.blocks - yield put({ type: BlocksActions.BLOCKS_POLLING, drizzle, interval, web3, syncAlways }) - } else { - // Not using old MetaMask, attempt subscription block listening. - yield put({ type: BlocksActions.BLOCKS_LISTENING, drizzle, web3, syncAlways }) - } + yield put({ type: BlocksActions.BLOCKS_LISTENING, drizzle, web3 }) // Accounts Polling if ('accounts' in options.polls) {