forked from MetaMask/eth-block-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolling.js
More file actions
150 lines (123 loc) · 4.57 KB
/
polling.js
File metadata and controls
150 lines (123 loc) · 4.57 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
const test = require('tape')
const GanacheCore = require('ganache-core')
const pify = require('pify')
const PollingBlockTracker = require('../src/polling')
const noop = () => {}
module.exports = (test, testLabel, PollingBlockTracker) => {
test(`${testLabel} - latest`, async (t) => {
const provider = GanacheCore.provider()
const blockTracker = new PollingBlockTracker({
provider,
pollingInterval: 100,
})
try {
t.equal(blockTracker.isRunning(), false, 'PollingBlockTracker should begin stopped')
const blocks = []
blockTracker.on('latest', (block) => blocks.push(block))
t.equal(blockTracker.isRunning(), true, 'PollingBlockTracker should start after listener is added')
t.equal(blocks.length, 0, 'no blocks so far')
await newLatestBlock(blockTracker)
t.equal(blocks.length, 1, 'saw 1st block')
await triggerNextBlock(provider)
await newLatestBlock(blockTracker)
t.equal(blocks.length, 2, 'saw 2nd block')
await triggerNextBlock(provider)
await triggerNextBlock(provider)
await triggerNextBlock(provider)
const lastBlock = await newLatestBlock(blockTracker)
t.equal(blocks.length, 3, 'saw only 5th block')
t.equal(Number.parseInt(lastBlock, 16), 4, 'saw correct block, with number 4')
blockTracker.removeAllListeners()
t.equal(blockTracker.isRunning(), false, 'PollingBlockTracker stops after all listeners are removed')
} catch (err) {
t.ifError(err)
}
// cleanup
blockTracker.removeAllListeners()
t.end()
})
test(`${testLabel} - error catch`, async (t) => {
const provider = GanacheCore.provider()
const blockTracker = new PollingBlockTracker({
provider,
pollingInterval: 100,
})
// ignore our error if registered as an uncaughtException
process.on('uncaughtException', ignoreError)
function ignoreError(err) {
// ignore our error
if (err.message.includes('boom')) return
// otherwise fail
t.ifError(err)
}
try {
// keep the block tracker polling
blockTracker.on('latest', () => { })
// throw error in handler in attempt to break block tracker
blockTracker.once('latest', () => { throw new Error('boom') })
// emit and observe a block
const nextBlockPromise = nextBlockSeen(blockTracker)
await triggerNextBlock(provider)
await nextBlockPromise
// emit and observe another block
const nextNextBlockPromise = nextBlockSeen(blockTracker)
await triggerNextBlock(provider)
await nextNextBlockPromise
} catch (err) {
t.ifError(err)
}
// setTimeout so we dont remove the uncaughtException handler before
// the SafeEventEmitter emits the event on next tick
setTimeout(() => {
// cleanup
process.removeListener('uncaughtException', ignoreError)
blockTracker.removeAllListeners()
t.end()
})
})
test(`${testLabel} - _fetchLatestBlock error handling`, async (t) => {
const provider = GanacheCore.provider()
const blockTracker = new PollingBlockTracker({
provider,
pollingInterval: 100,
})
// measure if errors are reported to the console
const consoleErrors = []
const originalConsoleErrorMethod = console.error
console.error = (err) => consoleErrors.push(err)
// override _fetchLatestBlock to throw an error
const originalFetchLatestBlock = blockTracker._fetchLatestBlock
blockTracker._fetchLatestBlock = async () => {
// restore fetch method
blockTracker._fetchLatestBlock = originalFetchLatestBlock
// throw error to try and break block tracker
throw new Error('TestError')
}
try {
const latestBlock = await blockTracker.getLatestBlock()
t.ok(latestBlock, 'got a block back')
t.ok(consoleErrors.length, 1, 'saw expected console error')
} catch (err) {
t.ifError(err)
}
// setTimeout so we dont remove the uncaughtException handler before
// the SafeEventEmitter emits the event on next tick
setTimeout(() => {
// cleanup
console.error = originalConsoleErrorMethod
blockTracker.removeAllListeners()
t.end()
})
})
}
async function triggerNextBlock(provider) {
await pify((cb) => provider.sendAsync({ id: 1, method: 'evm_mine', jsonrpc: '2.0', params: [] }, cb))()
}
async function newLatestBlock(blockTracker) {
return await pify(blockTracker.once, { errorFirst: false }).call(blockTracker, 'latest')
}
async function nextBlockSeen(blockTracker) {
return new Promise((resolve) => {
blockTracker.once('latest', resolve)
})
}