forked from MetaMask/eth-block-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseBlockTracker.ts
More file actions
186 lines (151 loc) · 4.8 KB
/
BaseBlockTracker.ts
File metadata and controls
186 lines (151 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import SafeEventEmitter from '@metamask/safe-event-emitter';
import { JsonRpcRequest, JsonRpcResponse } from 'json-rpc-engine';
const sec = 1000;
const calculateSum = (accumulator: number, currentValue: number) => accumulator + currentValue;
const blockTrackerEvents: (string | symbol)[] = ['sync', 'latest'];
export interface Provider extends SafeEventEmitter {
sendAsync: <T, U>(req: JsonRpcRequest<T>, cb: (err: Error, response: JsonRpcResponse<U>) => void) => void;
}
interface BaseBlockTrackerArgs {
blockResetDuration?: number;
}
export class BaseBlockTracker extends SafeEventEmitter {
protected _isRunning: boolean;
private _blockResetDuration: number;
private _currentBlock: string | null;
private _blockResetTimeout?: ReturnType<typeof setTimeout>;
constructor(opts: BaseBlockTrackerArgs = {}) {
super();
// config
this._blockResetDuration = opts.blockResetDuration || 20 * sec;
// state
this._currentBlock = null;
this._isRunning = false;
// bind functions for internal use
this._onNewListener = this._onNewListener.bind(this);
this._onRemoveListener = this._onRemoveListener.bind(this);
this._resetCurrentBlock = this._resetCurrentBlock.bind(this);
// listen for handler changes
this._setupInternalEvents();
}
isRunning(): boolean {
return this._isRunning;
}
getCurrentBlock(): string | null {
return this._currentBlock;
}
async getLatestBlock(): Promise<string> {
// return if available
if (this._currentBlock) {
return this._currentBlock;
}
// wait for a new latest block
const latestBlock: string = await new Promise((resolve) => this.once('latest', resolve));
// return newly set current block
return latestBlock;
}
// dont allow module consumer to remove our internal event listeners
removeAllListeners(eventName: string | symbol) {
// perform default behavior, preserve fn arity
if (eventName) {
super.removeAllListeners(eventName);
} else {
super.removeAllListeners();
}
// re-add internal events
this._setupInternalEvents();
// trigger stop check just in case
this._onRemoveListener();
return this;
}
/**
* To be implemented in subclass.
*/
protected _start(): void {
// default behavior is noop
}
/**
* To be implemented in subclass.
*/
protected _end(): void {
// default behavior is noop
}
private _setupInternalEvents(): void {
// first remove listeners for idempotence
this.removeListener('newListener', this._onNewListener);
this.removeListener('removeListener', this._onRemoveListener);
// then add them
this.on('newListener', this._onNewListener);
this.on('removeListener', this._onRemoveListener);
}
private _onNewListener(eventName: string | symbol): void {
// `newListener` is called *before* the listener is added
if (blockTrackerEvents.includes(eventName)) {
this._maybeStart();
}
}
private _onRemoveListener(): void {
// `removeListener` is called *after* the listener is removed
if (this._getBlockTrackerEventCount() > 0) {
return;
}
this._maybeEnd();
}
private _maybeStart(): void {
if (this._isRunning) {
return;
}
this._isRunning = true;
// cancel setting latest block to stale
this._cancelBlockResetTimeout();
this._start();
}
private _maybeEnd(): void {
if (!this._isRunning) {
return;
}
this._isRunning = false;
this._setupBlockResetTimeout();
this._end();
}
private _getBlockTrackerEventCount(): number {
return blockTrackerEvents
.map((eventName) => this.listenerCount(eventName))
.reduce(calculateSum);
}
protected _newPotentialLatest(newBlock: string): void {
const currentBlock = this._currentBlock;
// only update if blok number is higher
if (currentBlock && (hexToInt(newBlock) <= hexToInt(currentBlock))) {
return;
}
this._setCurrentBlock(newBlock);
}
private _setCurrentBlock(newBlock: string): void {
const oldBlock = this._currentBlock;
this._currentBlock = newBlock;
this.emit('latest', newBlock);
this.emit('sync', { oldBlock, newBlock });
}
private _setupBlockResetTimeout(): void {
// clear any existing timeout
this._cancelBlockResetTimeout();
// clear latest block when stale
this._blockResetTimeout = setTimeout(this._resetCurrentBlock, this._blockResetDuration);
// nodejs - dont hold process open
if (this._blockResetTimeout.unref) {
this._blockResetTimeout.unref();
}
}
private _cancelBlockResetTimeout(): void {
if (this._blockResetTimeout) {
clearTimeout(this._blockResetTimeout);
}
}
private _resetCurrentBlock(): void {
this._currentBlock = null;
}
}
function hexToInt(hexInt: string): number {
return Number.parseInt(hexInt, 16);
}