From ced68e4e022eb1020a4d15ab9a64552b4f92744a Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 13 Jul 2018 19:44:21 +0200 Subject: Expose Blockstreams blockRetention config as an OrderWatcher config --- packages/order-watcher/src/order_watcher/event_watcher.ts | 6 ++++++ packages/order-watcher/src/order_watcher/order_watcher.ts | 4 +++- packages/order-watcher/src/types.ts | 2 ++ 3 files changed, 11 insertions(+), 1 deletion(-) (limited to 'packages/order-watcher/src') diff --git a/packages/order-watcher/src/order_watcher/event_watcher.ts b/packages/order-watcher/src/order_watcher/event_watcher.ts index 08ecf81cb..38ba8101e 100644 --- a/packages/order-watcher/src/order_watcher/event_watcher.ts +++ b/packages/order-watcher/src/order_watcher/event_watcher.ts @@ -27,13 +27,16 @@ export class EventWatcher { private _pollingIntervalMs: number; private _stateLayer: BlockParamLiteral; private _isVerbose: boolean; + private _blockRetention: number; constructor( web3Wrapper: Web3Wrapper, pollingIntervalIfExistsMs: undefined | number, stateLayer: BlockParamLiteral = BlockParamLiteral.Latest, + blockRetention: number, isVerbose: boolean, ) { this._isVerbose = isVerbose; + this._blockRetention = blockRetention; this._web3Wrapper = web3Wrapper; this._stateLayer = stateLayer; this._pollingIntervalMs = _.isUndefined(pollingIntervalIfExistsMs) @@ -69,6 +72,9 @@ export class EventWatcher { this._web3Wrapper.getBlockAsync.bind(this._web3Wrapper, this._stateLayer), this._web3Wrapper.getLogsAsync.bind(this._web3Wrapper, eventFilter), this._onBlockAndLogStreamerError.bind(this), + { + blockRetention: this._blockRetention, + }, ); const catchAllLogFilter = {}; this._blockAndLogStreamerIfExists.addLogFilter(catchAllLogFilter); diff --git a/packages/order-watcher/src/order_watcher/order_watcher.ts b/packages/order-watcher/src/order_watcher/order_watcher.ts index b09ba8d9d..2fe012593 100644 --- a/packages/order-watcher/src/order_watcher/order_watcher.ts +++ b/packages/order-watcher/src/order_watcher/order_watcher.ts @@ -61,6 +61,7 @@ interface OrderStateByOrderHash { // tslint:disable-next-line:custom-no-magic-numbers const DEFAULT_CLEANUP_JOB_INTERVAL_MS = 1000 * 60 * 60; // 1h +const DEFAULT_BLOCK_RETENTION = 100; /** * This class includes all the functionality related to watching a set of orders @@ -94,7 +95,8 @@ export class OrderWatcher { const stateLayer = _.isUndefined(config) || _.isUndefined(config.stateLayer) ? BlockParamLiteral.Latest : config.stateLayer; const isVerbose = !_.isUndefined(config) && !_.isUndefined(config.isVerbose) ? config.isVerbose : false; - this._eventWatcher = new EventWatcher(this._web3Wrapper, pollingIntervalIfExistsMs, stateLayer, isVerbose); + const blockRetention = !_.isUndefined(config) && !_.isUndefined(config.blockRetention) ? config.blockRetention : DEFAULT_BLOCK_RETENTION; + this._eventWatcher = new EventWatcher(this._web3Wrapper, pollingIntervalIfExistsMs, stateLayer, blockRetention, isVerbose); this._balanceAndProxyAllowanceLazyStore = new BalanceAndProxyAllowanceLazyStore( this._contractWrappers.token, stateLayer, diff --git a/packages/order-watcher/src/types.ts b/packages/order-watcher/src/types.ts index 63e4e7848..e9b8626b6 100644 --- a/packages/order-watcher/src/types.ts +++ b/packages/order-watcher/src/types.ts @@ -14,6 +14,7 @@ export type EventWatcherCallback = (err: null | Error, log?: LogEntryEvent) => v * of an orders expiration. Default=0. * cleanupJobIntervalMs: How often to run a cleanup job which revalidates all the orders. Default=1hr. * stateLayer: Optional blockchain state layer OrderWatcher will monitor for new events. Default=latest. + * blockRetention: The numbers of blocks to retain in-memory, in order to handle block-reorgs. Default=100. */ export interface OrderWatcherConfig { stateLayer: BlockParamLiteral; @@ -22,6 +23,7 @@ export interface OrderWatcherConfig { expirationMarginMs?: number; cleanupJobIntervalMs?: number; isVerbose?: boolean; + blockRetention?: number; } export type OnOrderStateChangeCallback = (err: Error | null, orderState?: OrderState) => void; -- cgit v1.2.3 From 5fc7d9a6032b816156f6c2b39002936649bcbabd Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 13 Jul 2018 19:44:56 +0200 Subject: Fix bug that make all getBlock and getLog requests go to latest block --- packages/order-watcher/src/order_watcher/event_watcher.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'packages/order-watcher/src') diff --git a/packages/order-watcher/src/order_watcher/event_watcher.ts b/packages/order-watcher/src/order_watcher/event_watcher.ts index 38ba8101e..0d3652012 100644 --- a/packages/order-watcher/src/order_watcher/event_watcher.ts +++ b/packages/order-watcher/src/order_watcher/event_watcher.ts @@ -64,13 +64,9 @@ export class EventWatcher { if (!_.isUndefined(this._blockAndLogStreamerIfExists)) { throw new Error(OrderWatcherError.SubscriptionAlreadyPresent); } - const eventFilter = { - fromBlock: this._stateLayer, - toBlock: this._stateLayer, - }; this._blockAndLogStreamerIfExists = new BlockAndLogStreamer( - this._web3Wrapper.getBlockAsync.bind(this._web3Wrapper, this._stateLayer), - this._web3Wrapper.getLogsAsync.bind(this._web3Wrapper, eventFilter), + this._web3Wrapper.getBlockAsync.bind(this._web3Wrapper), + this._web3Wrapper.getLogsAsync.bind(this._web3Wrapper), this._onBlockAndLogStreamerError.bind(this), { blockRetention: this._blockRetention, -- cgit v1.2.3 From 151ce6e3c77e43659899ce299ac9f1b37cf6a042 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Mon, 16 Jul 2018 10:24:34 +0200 Subject: Pass stateLater into getBlockAsync call --- packages/order-watcher/src/order_watcher/event_watcher.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/order-watcher/src') diff --git a/packages/order-watcher/src/order_watcher/event_watcher.ts b/packages/order-watcher/src/order_watcher/event_watcher.ts index 0d3652012..d52c6a21e 100644 --- a/packages/order-watcher/src/order_watcher/event_watcher.ts +++ b/packages/order-watcher/src/order_watcher/event_watcher.ts @@ -106,7 +106,7 @@ export class EventWatcher { await this._emitDifferencesAsync(log, isRemoved ? LogEventState.Removed : LogEventState.Added, callback); } private async _reconcileBlockAsync(): Promise { - const latestBlock = await this._web3Wrapper.getBlockAsync(BlockParamLiteral.Latest); + const latestBlock = await this._web3Wrapper.getBlockAsync(this._stateLayer); // We need to coerce to Block type cause Web3.Block includes types for mempool blocks if (!_.isUndefined(this._blockAndLogStreamerIfExists)) { // If we clear the interval while fetching the block - this._blockAndLogStreamer will be undefined -- cgit v1.2.3 From 766ac3f1febcb6f6e4f6c20198283f01b278f671 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 17 Jul 2018 11:57:52 +0200 Subject: Remove blockRetention config --- packages/order-watcher/src/order_watcher/event_watcher.ts | 6 ------ packages/order-watcher/src/order_watcher/order_watcher.ts | 4 +--- packages/order-watcher/src/types.ts | 2 -- 3 files changed, 1 insertion(+), 11 deletions(-) (limited to 'packages/order-watcher/src') diff --git a/packages/order-watcher/src/order_watcher/event_watcher.ts b/packages/order-watcher/src/order_watcher/event_watcher.ts index d52c6a21e..1cd198a62 100644 --- a/packages/order-watcher/src/order_watcher/event_watcher.ts +++ b/packages/order-watcher/src/order_watcher/event_watcher.ts @@ -27,16 +27,13 @@ export class EventWatcher { private _pollingIntervalMs: number; private _stateLayer: BlockParamLiteral; private _isVerbose: boolean; - private _blockRetention: number; constructor( web3Wrapper: Web3Wrapper, pollingIntervalIfExistsMs: undefined | number, stateLayer: BlockParamLiteral = BlockParamLiteral.Latest, - blockRetention: number, isVerbose: boolean, ) { this._isVerbose = isVerbose; - this._blockRetention = blockRetention; this._web3Wrapper = web3Wrapper; this._stateLayer = stateLayer; this._pollingIntervalMs = _.isUndefined(pollingIntervalIfExistsMs) @@ -68,9 +65,6 @@ export class EventWatcher { this._web3Wrapper.getBlockAsync.bind(this._web3Wrapper), this._web3Wrapper.getLogsAsync.bind(this._web3Wrapper), this._onBlockAndLogStreamerError.bind(this), - { - blockRetention: this._blockRetention, - }, ); const catchAllLogFilter = {}; this._blockAndLogStreamerIfExists.addLogFilter(catchAllLogFilter); diff --git a/packages/order-watcher/src/order_watcher/order_watcher.ts b/packages/order-watcher/src/order_watcher/order_watcher.ts index 2fe012593..b09ba8d9d 100644 --- a/packages/order-watcher/src/order_watcher/order_watcher.ts +++ b/packages/order-watcher/src/order_watcher/order_watcher.ts @@ -61,7 +61,6 @@ interface OrderStateByOrderHash { // tslint:disable-next-line:custom-no-magic-numbers const DEFAULT_CLEANUP_JOB_INTERVAL_MS = 1000 * 60 * 60; // 1h -const DEFAULT_BLOCK_RETENTION = 100; /** * This class includes all the functionality related to watching a set of orders @@ -95,8 +94,7 @@ export class OrderWatcher { const stateLayer = _.isUndefined(config) || _.isUndefined(config.stateLayer) ? BlockParamLiteral.Latest : config.stateLayer; const isVerbose = !_.isUndefined(config) && !_.isUndefined(config.isVerbose) ? config.isVerbose : false; - const blockRetention = !_.isUndefined(config) && !_.isUndefined(config.blockRetention) ? config.blockRetention : DEFAULT_BLOCK_RETENTION; - this._eventWatcher = new EventWatcher(this._web3Wrapper, pollingIntervalIfExistsMs, stateLayer, blockRetention, isVerbose); + this._eventWatcher = new EventWatcher(this._web3Wrapper, pollingIntervalIfExistsMs, stateLayer, isVerbose); this._balanceAndProxyAllowanceLazyStore = new BalanceAndProxyAllowanceLazyStore( this._contractWrappers.token, stateLayer, diff --git a/packages/order-watcher/src/types.ts b/packages/order-watcher/src/types.ts index e9b8626b6..63e4e7848 100644 --- a/packages/order-watcher/src/types.ts +++ b/packages/order-watcher/src/types.ts @@ -14,7 +14,6 @@ export type EventWatcherCallback = (err: null | Error, log?: LogEntryEvent) => v * of an orders expiration. Default=0. * cleanupJobIntervalMs: How often to run a cleanup job which revalidates all the orders. Default=1hr. * stateLayer: Optional blockchain state layer OrderWatcher will monitor for new events. Default=latest. - * blockRetention: The numbers of blocks to retain in-memory, in order to handle block-reorgs. Default=100. */ export interface OrderWatcherConfig { stateLayer: BlockParamLiteral; @@ -23,7 +22,6 @@ export interface OrderWatcherConfig { expirationMarginMs?: number; cleanupJobIntervalMs?: number; isVerbose?: boolean; - blockRetention?: number; } export type OnOrderStateChangeCallback = (err: Error | null, orderState?: OrderState) => void; -- cgit v1.2.3 From 1e787a764615bd3df839f2cb117b711be297b9d5 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 17 Jul 2018 12:04:29 +0200 Subject: Remove stateLayer OrderWatcher config and instead temporarily hard-code until we get pending block to work with Blockstream --- packages/order-watcher/src/order_watcher/event_watcher.ts | 2 +- packages/order-watcher/src/order_watcher/order_watcher.ts | 9 ++++----- packages/order-watcher/src/types.ts | 2 -- 3 files changed, 5 insertions(+), 8 deletions(-) (limited to 'packages/order-watcher/src') diff --git a/packages/order-watcher/src/order_watcher/event_watcher.ts b/packages/order-watcher/src/order_watcher/event_watcher.ts index 1cd198a62..60a3c6297 100644 --- a/packages/order-watcher/src/order_watcher/event_watcher.ts +++ b/packages/order-watcher/src/order_watcher/event_watcher.ts @@ -30,7 +30,7 @@ export class EventWatcher { constructor( web3Wrapper: Web3Wrapper, pollingIntervalIfExistsMs: undefined | number, - stateLayer: BlockParamLiteral = BlockParamLiteral.Latest, + stateLayer: BlockParamLiteral, isVerbose: boolean, ) { this._isVerbose = isVerbose; diff --git a/packages/order-watcher/src/order_watcher/order_watcher.ts b/packages/order-watcher/src/order_watcher/order_watcher.ts index b09ba8d9d..95671684f 100644 --- a/packages/order-watcher/src/order_watcher/order_watcher.ts +++ b/packages/order-watcher/src/order_watcher/order_watcher.ts @@ -61,6 +61,7 @@ interface OrderStateByOrderHash { // tslint:disable-next-line:custom-no-magic-numbers const DEFAULT_CLEANUP_JOB_INTERVAL_MS = 1000 * 60 * 60; // 1h +const STATE_LAYER = BlockParamLiteral.Latest; /** * This class includes all the functionality related to watching a set of orders @@ -91,17 +92,15 @@ export class OrderWatcher { }); this._contractWrappers = new ContractWrappers(provider, { networkId }); const pollingIntervalIfExistsMs = _.isUndefined(config) ? undefined : config.eventPollingIntervalMs; - const stateLayer = - _.isUndefined(config) || _.isUndefined(config.stateLayer) ? BlockParamLiteral.Latest : config.stateLayer; const isVerbose = !_.isUndefined(config) && !_.isUndefined(config.isVerbose) ? config.isVerbose : false; - this._eventWatcher = new EventWatcher(this._web3Wrapper, pollingIntervalIfExistsMs, stateLayer, isVerbose); + this._eventWatcher = new EventWatcher(this._web3Wrapper, pollingIntervalIfExistsMs, STATE_LAYER, isVerbose); this._balanceAndProxyAllowanceLazyStore = new BalanceAndProxyAllowanceLazyStore( this._contractWrappers.token, - stateLayer, + STATE_LAYER, ); this._orderFilledCancelledLazyStore = new OrderFilledCancelledLazyStore( this._contractWrappers.exchange, - stateLayer, + STATE_LAYER, ); this._orderStateUtils = new OrderStateUtils( this._balanceAndProxyAllowanceLazyStore, diff --git a/packages/order-watcher/src/types.ts b/packages/order-watcher/src/types.ts index 63e4e7848..f8ec91a51 100644 --- a/packages/order-watcher/src/types.ts +++ b/packages/order-watcher/src/types.ts @@ -13,10 +13,8 @@ export type EventWatcherCallback = (err: null | Error, log?: LogEntryEvent) => v * expirationMarginMs: Amount of time before order expiry that you'd like to be notified * of an orders expiration. Default=0. * cleanupJobIntervalMs: How often to run a cleanup job which revalidates all the orders. Default=1hr. - * stateLayer: Optional blockchain state layer OrderWatcher will monitor for new events. Default=latest. */ export interface OrderWatcherConfig { - stateLayer: BlockParamLiteral; orderExpirationCheckingIntervalMs?: number; eventPollingIntervalMs?: number; expirationMarginMs?: number; -- cgit v1.2.3 From bf8ac3b9e6ee59f267f7850418febfe84dedceb8 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 17 Jul 2018 12:59:02 +0200 Subject: Fix tslint issues --- .../src/order_watcher/event_watcher.ts | 8 ++++---- .../src/order_watcher/expiration_watcher.ts | 10 +++++----- .../src/order_watcher/order_watcher.ts | 22 +++++++++++----------- packages/order-watcher/src/utils/assert.ts | 2 +- 4 files changed, 21 insertions(+), 21 deletions(-) (limited to 'packages/order-watcher/src') diff --git a/packages/order-watcher/src/order_watcher/event_watcher.ts b/packages/order-watcher/src/order_watcher/event_watcher.ts index 08ecf81cb..b45e2c8c0 100644 --- a/packages/order-watcher/src/order_watcher/event_watcher.ts +++ b/packages/order-watcher/src/order_watcher/event_watcher.ts @@ -19,14 +19,14 @@ enum LogEventState { * depth. */ export class EventWatcher { - private _web3Wrapper: Web3Wrapper; + private readonly _web3Wrapper: Web3Wrapper; private _blockAndLogStreamerIfExists: BlockAndLogStreamer | undefined; private _blockAndLogStreamIntervalIfExists?: NodeJS.Timer; private _onLogAddedSubscriptionToken: string | undefined; private _onLogRemovedSubscriptionToken: string | undefined; - private _pollingIntervalMs: number; - private _stateLayer: BlockParamLiteral; - private _isVerbose: boolean; + private readonly _pollingIntervalMs: number; + private readonly _stateLayer: BlockParamLiteral; + private readonly _isVerbose: boolean; constructor( web3Wrapper: Web3Wrapper, pollingIntervalIfExistsMs: undefined | number, diff --git a/packages/order-watcher/src/order_watcher/expiration_watcher.ts b/packages/order-watcher/src/order_watcher/expiration_watcher.ts index 31fda7dca..c1f34d13d 100644 --- a/packages/order-watcher/src/order_watcher/expiration_watcher.ts +++ b/packages/order-watcher/src/order_watcher/expiration_watcher.ts @@ -13,10 +13,10 @@ const DEFAULT_ORDER_EXPIRATION_CHECKING_INTERVAL_MS = 50; * It stores them in a min heap by expiration time and checks for expired ones every `orderExpirationCheckingIntervalMs` */ export class ExpirationWatcher { - private _orderHashByExpirationRBTree: RBTree; - private _expiration: { [orderHash: string]: BigNumber } = {}; - private _orderExpirationCheckingIntervalMs: number; - private _expirationMarginMs: number; + private readonly _orderHashByExpirationRBTree: RBTree; + private readonly _expiration: { [orderHash: string]: BigNumber } = {}; + private readonly _orderExpirationCheckingIntervalMs: number; + private readonly _expirationMarginMs: number; private _orderExpirationCheckingIntervalIdIfExists?: NodeJS.Timer; constructor(expirationMarginIfExistsMs?: number, orderExpirationCheckingIntervalIfExistsMs?: number) { this._orderExpirationCheckingIntervalMs = @@ -44,7 +44,7 @@ export class ExpirationWatcher { this._orderExpirationCheckingIntervalIdIfExists = intervalUtils.setInterval( this._pruneExpiredOrders.bind(this, callback), this._orderExpirationCheckingIntervalMs, - _.noop, // _pruneExpiredOrders never throws + _.noop.bind(_), // _pruneExpiredOrders never throws ); } public unsubscribe(): void { diff --git a/packages/order-watcher/src/order_watcher/order_watcher.ts b/packages/order-watcher/src/order_watcher/order_watcher.ts index b09ba8d9d..b32354687 100644 --- a/packages/order-watcher/src/order_watcher/order_watcher.ts +++ b/packages/order-watcher/src/order_watcher/order_watcher.ts @@ -69,18 +69,18 @@ const DEFAULT_CLEANUP_JOB_INTERVAL_MS = 1000 * 60 * 60; // 1h * the order should be deemed invalid. */ export class OrderWatcher { - private _contractWrappers: ContractWrappers; - private _orderStateByOrderHashCache: OrderStateByOrderHash = {}; - private _orderByOrderHash: OrderByOrderHash = {}; - private _dependentOrderHashes: DependentOrderHashes = {}; + private readonly _contractWrappers: ContractWrappers; + private readonly _orderStateByOrderHashCache: OrderStateByOrderHash = {}; + private readonly _orderByOrderHash: OrderByOrderHash = {}; + private readonly _dependentOrderHashes: DependentOrderHashes = {}; private _callbackIfExists?: OnOrderStateChangeCallback; - private _eventWatcher: EventWatcher; - private _web3Wrapper: Web3Wrapper; - private _expirationWatcher: ExpirationWatcher; - private _orderStateUtils: OrderStateUtils; - private _orderFilledCancelledLazyStore: OrderFilledCancelledLazyStore; - private _balanceAndProxyAllowanceLazyStore: BalanceAndProxyAllowanceLazyStore; - private _cleanupJobInterval: number; + private readonly _eventWatcher: EventWatcher; + private readonly _web3Wrapper: Web3Wrapper; + private readonly _expirationWatcher: ExpirationWatcher; + private readonly _orderStateUtils: OrderStateUtils; + private readonly _orderFilledCancelledLazyStore: OrderFilledCancelledLazyStore; + private readonly _balanceAndProxyAllowanceLazyStore: BalanceAndProxyAllowanceLazyStore; + private readonly _cleanupJobInterval: number; private _cleanupJobIntervalIdIfExists?: NodeJS.Timer; constructor(provider: Provider, networkId: number, config?: OrderWatcherConfig) { this._web3Wrapper = new Web3Wrapper(provider); diff --git a/packages/order-watcher/src/utils/assert.ts b/packages/order-watcher/src/utils/assert.ts index 9c992d9b4..4a1441474 100644 --- a/packages/order-watcher/src/utils/assert.ts +++ b/packages/order-watcher/src/utils/assert.ts @@ -12,6 +12,6 @@ export const assert = { ...sharedAssert, isValidSignature(orderHash: string, ecSignature: ECSignature, signerAddress: string): void { const isValid = isValidSignature(orderHash, ecSignature, signerAddress); - this.assert(isValid, `Expected order with hash '${orderHash}' to have a valid signature`); + assert.assert(isValid, `Expected order with hash '${orderHash}' to have a valid signature`); }, }; -- cgit v1.2.3 From f56a7d0cb2bbe8ba9b6c4fb18151ff068165793b Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 17 Jul 2018 13:29:36 +0200 Subject: Fix linter --- packages/order-watcher/src/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/order-watcher/src') diff --git a/packages/order-watcher/src/types.ts b/packages/order-watcher/src/types.ts index f8ec91a51..fd71267a2 100644 --- a/packages/order-watcher/src/types.ts +++ b/packages/order-watcher/src/types.ts @@ -1,4 +1,4 @@ -import { BlockParamLiteral, LogEntryEvent, OrderState } from '@0xproject/types'; +import { LogEntryEvent, OrderState } from '@0xproject/types'; export enum OrderWatcherError { SubscriptionAlreadyPresent = 'SUBSCRIPTION_ALREADY_PRESENT', -- cgit v1.2.3