@metamask-previews/eth-block-tracker 15.0.0-preview-7bc2d97e → 15.0.0-preview-6bed60a6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +0 -4
- package/dist/PollingBlockTracker.cjs +209 -205
- package/dist/PollingBlockTracker.cjs.map +1 -1
- package/dist/PollingBlockTracker.d.cts +33 -0
- package/dist/PollingBlockTracker.d.cts.map +1 -1
- package/dist/PollingBlockTracker.d.mts +33 -0
- package/dist/PollingBlockTracker.d.mts.map +1 -1
- package/dist/PollingBlockTracker.mjs +208 -204
- package/dist/PollingBlockTracker.mjs.map +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -7,10 +7,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
-
### Changed
|
|
11
|
-
|
|
12
|
-
- Upgrade `@metamask/utils` from `^11.8.1` to `^11.9.0` ([#7511](https://github.com/MetaMask/core/pull/7511))
|
|
13
|
-
|
|
14
10
|
## [15.0.0]
|
|
15
11
|
|
|
16
12
|
### Added
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
+
};
|
|
2
7
|
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
8
|
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
9
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
10
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
11
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
12
|
};
|
|
8
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
-
};
|
|
13
13
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
14
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
15
|
};
|
|
16
|
-
var _PollingBlockTracker_instances,
|
|
16
|
+
var _PollingBlockTracker_instances, _PollingBlockTracker_internalEventListeners, _PollingBlockTracker_pendingLatestBlock, _PollingBlockTracker_pendingFetch, _PollingBlockTracker_addInternalListener, _PollingBlockTracker_removeInternalListener, _PollingBlockTracker_rejectPendingLatestBlock;
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.PollingBlockTracker = void 0;
|
|
19
19
|
const safe_event_emitter_1 = __importDefault(require("@metamask/safe-event-emitter"));
|
|
@@ -32,57 +32,44 @@ class PollingBlockTracker extends safe_event_emitter_1.default {
|
|
|
32
32
|
}
|
|
33
33
|
super();
|
|
34
34
|
_PollingBlockTracker_instances.add(this);
|
|
35
|
-
_PollingBlockTracker_isRunning.set(this, void 0);
|
|
36
|
-
_PollingBlockTracker_blockResetDuration.set(this, void 0);
|
|
37
|
-
_PollingBlockTracker_usePastBlocks.set(this, void 0);
|
|
38
|
-
_PollingBlockTracker_currentBlock.set(this, void 0);
|
|
39
|
-
_PollingBlockTracker_blockResetTimeout.set(this, void 0);
|
|
40
|
-
_PollingBlockTracker_pollingTimeout.set(this, void 0);
|
|
41
|
-
_PollingBlockTracker_provider.set(this, void 0);
|
|
42
|
-
_PollingBlockTracker_pollingInterval.set(this, void 0);
|
|
43
|
-
_PollingBlockTracker_retryTimeout.set(this, void 0);
|
|
44
|
-
_PollingBlockTracker_keepEventLoopActive.set(this, void 0);
|
|
45
|
-
_PollingBlockTracker_setSkipCacheFlag.set(this, void 0);
|
|
46
35
|
_PollingBlockTracker_internalEventListeners.set(this, []);
|
|
47
36
|
_PollingBlockTracker_pendingLatestBlock.set(this, void 0);
|
|
48
37
|
_PollingBlockTracker_pendingFetch.set(this, void 0);
|
|
49
|
-
_PollingBlockTracker_onNewListener.set(this, void 0);
|
|
50
|
-
_PollingBlockTracker_onRemoveListener.set(this, void 0);
|
|
51
|
-
_PollingBlockTracker_resetCurrentBlock.set(this, void 0);
|
|
52
38
|
// config
|
|
53
|
-
|
|
54
|
-
|
|
39
|
+
this._blockResetDuration = opts.blockResetDuration || 20 * sec;
|
|
40
|
+
this._usePastBlocks = opts.usePastBlocks || false;
|
|
55
41
|
// state
|
|
56
|
-
|
|
57
|
-
|
|
42
|
+
this._currentBlock = null;
|
|
43
|
+
this._isRunning = false;
|
|
58
44
|
// bind functions for internal use
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
45
|
+
this._onNewListener = this._onNewListener.bind(this);
|
|
46
|
+
this._onRemoveListener = this._onRemoveListener.bind(this);
|
|
47
|
+
this._resetCurrentBlock = this._resetCurrentBlock.bind(this);
|
|
62
48
|
// listen for handler changes
|
|
63
|
-
|
|
49
|
+
this._setupInternalEvents();
|
|
64
50
|
// config
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
51
|
+
this._provider = opts.provider;
|
|
52
|
+
this._pollingInterval = opts.pollingInterval || 20 * sec;
|
|
53
|
+
this._retryTimeout = opts.retryTimeout || this._pollingInterval / 10;
|
|
54
|
+
this._keepEventLoopActive =
|
|
55
|
+
opts.keepEventLoopActive === undefined ? true : opts.keepEventLoopActive;
|
|
56
|
+
this._setSkipCacheFlag = opts.setSkipCacheFlag || false;
|
|
70
57
|
}
|
|
71
58
|
async destroy() {
|
|
72
|
-
|
|
59
|
+
this._cancelBlockResetTimeout();
|
|
73
60
|
super.removeAllListeners();
|
|
74
|
-
|
|
61
|
+
this._maybeEnd();
|
|
75
62
|
}
|
|
76
63
|
isRunning() {
|
|
77
|
-
return
|
|
64
|
+
return this._isRunning;
|
|
78
65
|
}
|
|
79
66
|
getCurrentBlock() {
|
|
80
|
-
return
|
|
67
|
+
return this._currentBlock;
|
|
81
68
|
}
|
|
82
69
|
async getLatestBlock({ useCache = true, } = {}) {
|
|
83
70
|
// return if available
|
|
84
|
-
if (
|
|
85
|
-
return
|
|
71
|
+
if (this._currentBlock && useCache) {
|
|
72
|
+
return this._currentBlock;
|
|
86
73
|
}
|
|
87
74
|
if (__classPrivateFieldGet(this, _PollingBlockTracker_pendingLatestBlock, "f")) {
|
|
88
75
|
return await __classPrivateFieldGet(this, _PollingBlockTracker_pendingLatestBlock, "f").promise;
|
|
@@ -91,7 +78,7 @@ class PollingBlockTracker extends safe_event_emitter_1.default {
|
|
|
91
78
|
suppressUnhandledRejection: true,
|
|
92
79
|
});
|
|
93
80
|
__classPrivateFieldSet(this, _PollingBlockTracker_pendingLatestBlock, { reject, promise }, "f");
|
|
94
|
-
if (
|
|
81
|
+
if (this._isRunning) {
|
|
95
82
|
try {
|
|
96
83
|
// If tracker is running, wait for next block with timeout
|
|
97
84
|
const onLatestBlock = (value) => {
|
|
@@ -114,7 +101,7 @@ class PollingBlockTracker extends safe_event_emitter_1.default {
|
|
|
114
101
|
else {
|
|
115
102
|
// If tracker isn't running, just fetch directly
|
|
116
103
|
try {
|
|
117
|
-
const latestBlock = await
|
|
104
|
+
const latestBlock = await this._updateLatestBlock();
|
|
118
105
|
resolve(latestBlock);
|
|
119
106
|
return latestBlock;
|
|
120
107
|
}
|
|
@@ -128,11 +115,11 @@ class PollingBlockTracker extends safe_event_emitter_1.default {
|
|
|
128
115
|
// achieve this by delaying the unsetting of the #pendingLatestBlock promise.
|
|
129
116
|
setTimeout(() => {
|
|
130
117
|
__classPrivateFieldSet(this, _PollingBlockTracker_pendingLatestBlock, undefined, "f");
|
|
131
|
-
},
|
|
118
|
+
}, this._pollingInterval);
|
|
132
119
|
}
|
|
133
120
|
}
|
|
134
121
|
}
|
|
135
|
-
//
|
|
122
|
+
// dont allow module consumer to remove our internal event listeners
|
|
136
123
|
removeAllListeners(eventName) {
|
|
137
124
|
// perform default behavior, preserve fn arity
|
|
138
125
|
if (eventName) {
|
|
@@ -142,192 +129,209 @@ class PollingBlockTracker extends safe_event_emitter_1.default {
|
|
|
142
129
|
super.removeAllListeners();
|
|
143
130
|
}
|
|
144
131
|
// re-add internal events
|
|
145
|
-
|
|
132
|
+
this._setupInternalEvents();
|
|
146
133
|
// trigger stop check just in case
|
|
147
|
-
|
|
134
|
+
this._onRemoveListener();
|
|
148
135
|
return this;
|
|
149
136
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
async checkForLatestBlock() {
|
|
158
|
-
await __classPrivateFieldGet(this, _PollingBlockTracker_instances, "m", _PollingBlockTracker_updateLatestBlock).call(this);
|
|
159
|
-
return await this.getLatestBlock();
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
exports.PollingBlockTracker = PollingBlockTracker;
|
|
163
|
-
_PollingBlockTracker_isRunning = new WeakMap(), _PollingBlockTracker_blockResetDuration = new WeakMap(), _PollingBlockTracker_usePastBlocks = new WeakMap(), _PollingBlockTracker_currentBlock = new WeakMap(), _PollingBlockTracker_blockResetTimeout = new WeakMap(), _PollingBlockTracker_pollingTimeout = new WeakMap(), _PollingBlockTracker_provider = new WeakMap(), _PollingBlockTracker_pollingInterval = new WeakMap(), _PollingBlockTracker_retryTimeout = new WeakMap(), _PollingBlockTracker_keepEventLoopActive = new WeakMap(), _PollingBlockTracker_setSkipCacheFlag = new WeakMap(), _PollingBlockTracker_internalEventListeners = new WeakMap(), _PollingBlockTracker_pendingLatestBlock = new WeakMap(), _PollingBlockTracker_pendingFetch = new WeakMap(), _PollingBlockTracker_onNewListener = new WeakMap(), _PollingBlockTracker_onRemoveListener = new WeakMap(), _PollingBlockTracker_resetCurrentBlock = new WeakMap(), _PollingBlockTracker_instances = new WeakSet(), _PollingBlockTracker_setupInternalEvents = function _PollingBlockTracker_setupInternalEvents() {
|
|
164
|
-
// first remove listeners for idempotence
|
|
165
|
-
this.removeListener('newListener', __classPrivateFieldGet(this, _PollingBlockTracker_onNewListener, "f"));
|
|
166
|
-
this.removeListener('removeListener', __classPrivateFieldGet(this, _PollingBlockTracker_onRemoveListener, "f"));
|
|
167
|
-
// then add them
|
|
168
|
-
this.on('newListener', __classPrivateFieldGet(this, _PollingBlockTracker_onNewListener, "f"));
|
|
169
|
-
this.on('removeListener', __classPrivateFieldGet(this, _PollingBlockTracker_onRemoveListener, "f"));
|
|
170
|
-
}, _PollingBlockTracker_onNewListenerUnbound = function _PollingBlockTracker_onNewListenerUnbound(eventName) {
|
|
171
|
-
// `newListener` is called *before* the listener is added
|
|
172
|
-
if (blockTrackerEvents.includes(eventName)) {
|
|
173
|
-
// TODO: Handle dangling promise
|
|
174
|
-
__classPrivateFieldGet(this, _PollingBlockTracker_instances, "m", _PollingBlockTracker_maybeStart).call(this);
|
|
137
|
+
_setupInternalEvents() {
|
|
138
|
+
// first remove listeners for idempotence
|
|
139
|
+
this.removeListener('newListener', this._onNewListener);
|
|
140
|
+
this.removeListener('removeListener', this._onRemoveListener);
|
|
141
|
+
// then add them
|
|
142
|
+
this.on('newListener', this._onNewListener);
|
|
143
|
+
this.on('removeListener', this._onRemoveListener);
|
|
175
144
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
145
|
+
_onNewListener(eventName) {
|
|
146
|
+
// `newListener` is called *before* the listener is added
|
|
147
|
+
if (blockTrackerEvents.includes(eventName)) {
|
|
148
|
+
// TODO: Handle dangling promise
|
|
149
|
+
this._maybeStart();
|
|
150
|
+
}
|
|
180
151
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
152
|
+
_onRemoveListener() {
|
|
153
|
+
// `removeListener` is called *after* the listener is removed
|
|
154
|
+
if (this._getBlockTrackerEventCount() > 0) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
this._maybeEnd();
|
|
185
158
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
159
|
+
_maybeStart() {
|
|
160
|
+
if (this._isRunning) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
this._isRunning = true;
|
|
164
|
+
// cancel setting latest block to stale
|
|
165
|
+
this._cancelBlockResetTimeout();
|
|
166
|
+
this._start();
|
|
167
|
+
this.emit('_started');
|
|
194
168
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
.
|
|
203
|
-
.
|
|
204
|
-
// internal listeners are not included in the count
|
|
205
|
-
.filter((listener) => __classPrivateFieldGet(this, _PollingBlockTracker_internalEventListeners, "f").every((internalListener) => !Object.is(internalListener, listener))).length);
|
|
206
|
-
}, _PollingBlockTracker_shouldUseNewBlock = function _PollingBlockTracker_shouldUseNewBlock(newBlock) {
|
|
207
|
-
const currentBlock = __classPrivateFieldGet(this, _PollingBlockTracker_currentBlock, "f");
|
|
208
|
-
if (!currentBlock) {
|
|
209
|
-
return true;
|
|
169
|
+
_maybeEnd() {
|
|
170
|
+
if (!this._isRunning) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
this._isRunning = false;
|
|
174
|
+
this._setupBlockResetTimeout();
|
|
175
|
+
this._end();
|
|
176
|
+
__classPrivateFieldGet(this, _PollingBlockTracker_instances, "m", _PollingBlockTracker_rejectPendingLatestBlock).call(this, new Error('Block tracker destroyed'));
|
|
177
|
+
this.emit('_ended');
|
|
210
178
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
return;
|
|
179
|
+
_getBlockTrackerEventCount() {
|
|
180
|
+
return (blockTrackerEvents
|
|
181
|
+
.map((eventName) => this.listeners(eventName))
|
|
182
|
+
.flat()
|
|
183
|
+
// internal listeners are not included in the count
|
|
184
|
+
.filter((listener) => __classPrivateFieldGet(this, _PollingBlockTracker_internalEventListeners, "f").every((internalListener) => !Object.is(internalListener, listener))).length);
|
|
218
185
|
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
// clear latest block when stale
|
|
229
|
-
__classPrivateFieldSet(this, _PollingBlockTracker_blockResetTimeout, setTimeout(__classPrivateFieldGet(this, _PollingBlockTracker_resetCurrentBlock, "f"), __classPrivateFieldGet(this, _PollingBlockTracker_blockResetDuration, "f")), "f");
|
|
230
|
-
// nodejs - dont hold process open
|
|
231
|
-
if (__classPrivateFieldGet(this, _PollingBlockTracker_blockResetTimeout, "f").unref) {
|
|
232
|
-
__classPrivateFieldGet(this, _PollingBlockTracker_blockResetTimeout, "f").unref();
|
|
186
|
+
_shouldUseNewBlock(newBlock) {
|
|
187
|
+
const currentBlock = this._currentBlock;
|
|
188
|
+
if (!currentBlock) {
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
const newBlockInt = hexToInt(newBlock);
|
|
192
|
+
const currentBlockInt = hexToInt(currentBlock);
|
|
193
|
+
return ((this._usePastBlocks && newBlockInt < currentBlockInt) ||
|
|
194
|
+
newBlockInt > currentBlockInt);
|
|
233
195
|
}
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
196
|
+
_newPotentialLatest(newBlock) {
|
|
197
|
+
if (!this._shouldUseNewBlock(newBlock)) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
this._setCurrentBlock(newBlock);
|
|
237
201
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
__classPrivateFieldGet(this, _PollingBlockTracker_instances, "m", _PollingBlockTracker_updateAndQueue).call(this);
|
|
244
|
-
}, _PollingBlockTracker_end = function _PollingBlockTracker_end() {
|
|
245
|
-
__classPrivateFieldGet(this, _PollingBlockTracker_instances, "m", _PollingBlockTracker_clearPollingTimeout).call(this);
|
|
246
|
-
}, _PollingBlockTracker_updateLatestBlock = async function _PollingBlockTracker_updateLatestBlock() {
|
|
247
|
-
// fetch + set latest block
|
|
248
|
-
const latestBlock = await __classPrivateFieldGet(this, _PollingBlockTracker_instances, "m", _PollingBlockTracker_fetchLatestBlock).call(this);
|
|
249
|
-
__classPrivateFieldGet(this, _PollingBlockTracker_instances, "m", _PollingBlockTracker_newPotentialLatest).call(this, latestBlock);
|
|
250
|
-
if (!__classPrivateFieldGet(this, _PollingBlockTracker_isRunning, "f")) {
|
|
251
|
-
// Ensure the one-time update is eventually reset once it's stale
|
|
252
|
-
__classPrivateFieldGet(this, _PollingBlockTracker_instances, "m", _PollingBlockTracker_setupBlockResetTimeout).call(this);
|
|
202
|
+
_setCurrentBlock(newBlock) {
|
|
203
|
+
const oldBlock = this._currentBlock;
|
|
204
|
+
this._currentBlock = newBlock;
|
|
205
|
+
this.emit('latest', newBlock);
|
|
206
|
+
this.emit('sync', { oldBlock, newBlock });
|
|
253
207
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
208
|
+
_setupBlockResetTimeout() {
|
|
209
|
+
// clear any existing timeout
|
|
210
|
+
this._cancelBlockResetTimeout();
|
|
211
|
+
// clear latest block when stale
|
|
212
|
+
this._blockResetTimeout = setTimeout(this._resetCurrentBlock, this._blockResetDuration);
|
|
213
|
+
// nodejs - dont hold process open
|
|
214
|
+
if (this._blockResetTimeout.unref) {
|
|
215
|
+
this._blockResetTimeout.unref();
|
|
216
|
+
}
|
|
261
217
|
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
});
|
|
266
|
-
__classPrivateFieldSet(this, _PollingBlockTracker_pendingFetch, { reject, promise }, "f");
|
|
267
|
-
try {
|
|
268
|
-
const req = {
|
|
269
|
-
jsonrpc: '2.0',
|
|
270
|
-
id: createRandomId(),
|
|
271
|
-
method: 'eth_blockNumber',
|
|
272
|
-
params: [],
|
|
273
|
-
};
|
|
274
|
-
if (__classPrivateFieldGet(this, _PollingBlockTracker_setSkipCacheFlag, "f")) {
|
|
275
|
-
req.skipCache = true;
|
|
218
|
+
_cancelBlockResetTimeout() {
|
|
219
|
+
if (this._blockResetTimeout) {
|
|
220
|
+
clearTimeout(this._blockResetTimeout);
|
|
276
221
|
}
|
|
277
|
-
log('Making request', req);
|
|
278
|
-
const result = await __classPrivateFieldGet(this, _PollingBlockTracker_provider, "f").request(req);
|
|
279
|
-
log('Got result', result);
|
|
280
|
-
resolve(result);
|
|
281
|
-
return result;
|
|
282
222
|
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
reject(error);
|
|
286
|
-
__classPrivateFieldGet(this, _PollingBlockTracker_instances, "m", _PollingBlockTracker_rejectPendingLatestBlock).call(this, error);
|
|
287
|
-
throw error;
|
|
223
|
+
_resetCurrentBlock() {
|
|
224
|
+
this._currentBlock = null;
|
|
288
225
|
}
|
|
289
|
-
|
|
290
|
-
|
|
226
|
+
/**
|
|
227
|
+
* Checks for the latest block, updates the internal state, and returns the
|
|
228
|
+
* value immediately rather than waiting for the next polling interval.
|
|
229
|
+
*
|
|
230
|
+
* @deprecated Use {@link getLatestBlock} instead.
|
|
231
|
+
* @returns A promise that resolves to the latest block number.
|
|
232
|
+
*/
|
|
233
|
+
async checkForLatestBlock() {
|
|
234
|
+
await this._updateLatestBlock();
|
|
235
|
+
return await this.getLatestBlock();
|
|
291
236
|
}
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
*/
|
|
297
|
-
async function _PollingBlockTracker_updateAndQueue() {
|
|
298
|
-
let interval = __classPrivateFieldGet(this, _PollingBlockTracker_pollingInterval, "f");
|
|
299
|
-
try {
|
|
300
|
-
await __classPrivateFieldGet(this, _PollingBlockTracker_instances, "m", _PollingBlockTracker_updateLatestBlock).call(this);
|
|
237
|
+
_start() {
|
|
238
|
+
// Intentionally not awaited as this starts the polling via a timeout chain.
|
|
239
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
240
|
+
this._updateAndQueue();
|
|
301
241
|
}
|
|
302
|
-
|
|
242
|
+
_end() {
|
|
243
|
+
this._clearPollingTimeout();
|
|
244
|
+
}
|
|
245
|
+
async _updateLatestBlock() {
|
|
246
|
+
// fetch + set latest block
|
|
247
|
+
const latestBlock = await this._fetchLatestBlock();
|
|
248
|
+
this._newPotentialLatest(latestBlock);
|
|
249
|
+
if (!this._isRunning) {
|
|
250
|
+
// Ensure the one-time update is eventually reset once it's stale
|
|
251
|
+
this._setupBlockResetTimeout();
|
|
252
|
+
}
|
|
253
|
+
// _newPotentialLatest() ensures that this._currentBlock is not null
|
|
254
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
255
|
+
return this._currentBlock;
|
|
256
|
+
}
|
|
257
|
+
async _fetchLatestBlock() {
|
|
258
|
+
// If there's already a pending fetch, reuse it
|
|
259
|
+
if (__classPrivateFieldGet(this, _PollingBlockTracker_pendingFetch, "f")) {
|
|
260
|
+
return await __classPrivateFieldGet(this, _PollingBlockTracker_pendingFetch, "f").promise;
|
|
261
|
+
}
|
|
262
|
+
// Create a new deferred promise for this request
|
|
263
|
+
const { promise, resolve, reject } = (0, utils_1.createDeferredPromise)({
|
|
264
|
+
suppressUnhandledRejection: true,
|
|
265
|
+
});
|
|
266
|
+
__classPrivateFieldSet(this, _PollingBlockTracker_pendingFetch, { reject, promise }, "f");
|
|
303
267
|
try {
|
|
304
|
-
|
|
268
|
+
const req = {
|
|
269
|
+
jsonrpc: '2.0',
|
|
270
|
+
id: createRandomId(),
|
|
271
|
+
method: 'eth_blockNumber',
|
|
272
|
+
params: [],
|
|
273
|
+
};
|
|
274
|
+
if (this._setSkipCacheFlag) {
|
|
275
|
+
req.skipCache = true;
|
|
276
|
+
}
|
|
277
|
+
log('Making request', req);
|
|
278
|
+
const result = await this._provider.request(req);
|
|
279
|
+
log('Got result', result);
|
|
280
|
+
resolve(result);
|
|
281
|
+
return result;
|
|
305
282
|
}
|
|
306
|
-
catch {
|
|
307
|
-
|
|
283
|
+
catch (error) {
|
|
284
|
+
log('Encountered error fetching block', (0, utils_1.getErrorMessage)(error));
|
|
285
|
+
reject(error);
|
|
286
|
+
__classPrivateFieldGet(this, _PollingBlockTracker_instances, "m", _PollingBlockTracker_rejectPendingLatestBlock).call(this, error);
|
|
287
|
+
throw error;
|
|
288
|
+
}
|
|
289
|
+
finally {
|
|
290
|
+
__classPrivateFieldSet(this, _PollingBlockTracker_pendingFetch, undefined, "f");
|
|
308
291
|
}
|
|
309
|
-
interval = __classPrivateFieldGet(this, _PollingBlockTracker_retryTimeout, "f");
|
|
310
|
-
}
|
|
311
|
-
if (!__classPrivateFieldGet(this, _PollingBlockTracker_isRunning, "f")) {
|
|
312
|
-
return;
|
|
313
292
|
}
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
293
|
+
/**
|
|
294
|
+
* The core polling function that runs after each interval.
|
|
295
|
+
* Updates the latest block and then queues the next update.
|
|
296
|
+
*/
|
|
297
|
+
async _updateAndQueue() {
|
|
298
|
+
let interval = this._pollingInterval;
|
|
299
|
+
try {
|
|
300
|
+
await this._updateLatestBlock();
|
|
301
|
+
}
|
|
302
|
+
catch (error) {
|
|
303
|
+
try {
|
|
304
|
+
this.emit('error', error);
|
|
305
|
+
}
|
|
306
|
+
catch {
|
|
307
|
+
console.error(`Error updating latest block: ${(0, utils_1.getErrorMessage)(error)}`);
|
|
308
|
+
}
|
|
309
|
+
interval = this._retryTimeout;
|
|
310
|
+
}
|
|
311
|
+
if (!this._isRunning) {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
this._clearPollingTimeout();
|
|
315
|
+
const timeoutRef = setTimeout(() => {
|
|
316
|
+
// Intentionally not awaited as this just continues the polling loop.
|
|
317
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
318
|
+
this._updateAndQueue();
|
|
319
|
+
}, interval);
|
|
320
|
+
if (timeoutRef.unref && !this._keepEventLoopActive) {
|
|
321
|
+
timeoutRef.unref();
|
|
322
|
+
}
|
|
323
|
+
this._pollingTimeout = timeoutRef;
|
|
324
|
+
this.emit('_waitingForNextIteration');
|
|
322
325
|
}
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
__classPrivateFieldSet(this, _PollingBlockTracker_pollingTimeout, undefined, "f");
|
|
326
|
+
_clearPollingTimeout() {
|
|
327
|
+
if (this._pollingTimeout) {
|
|
328
|
+
clearTimeout(this._pollingTimeout);
|
|
329
|
+
this._pollingTimeout = undefined;
|
|
330
|
+
}
|
|
329
331
|
}
|
|
330
|
-
}
|
|
332
|
+
}
|
|
333
|
+
exports.PollingBlockTracker = PollingBlockTracker;
|
|
334
|
+
_PollingBlockTracker_internalEventListeners = new WeakMap(), _PollingBlockTracker_pendingLatestBlock = new WeakMap(), _PollingBlockTracker_pendingFetch = new WeakMap(), _PollingBlockTracker_instances = new WeakSet(), _PollingBlockTracker_addInternalListener = function _PollingBlockTracker_addInternalListener(listener) {
|
|
331
335
|
__classPrivateFieldGet(this, _PollingBlockTracker_internalEventListeners, "f").push(listener);
|
|
332
336
|
}, _PollingBlockTracker_removeInternalListener = function _PollingBlockTracker_removeInternalListener(listener) {
|
|
333
337
|
__classPrivateFieldGet(this, _PollingBlockTracker_internalEventListeners, "f").splice(__classPrivateFieldGet(this, _PollingBlockTracker_internalEventListeners, "f").indexOf(listener), 1);
|