forked from MetaMask/eth-block-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.js
More file actions
79 lines (62 loc) · 1.98 KB
/
basic.js
File metadata and controls
79 lines (62 loc) · 1.98 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
const test = require('tape')
const RpcBlockTracker = require('../lib/index')
const JsonRpcEngine = require('json-rpc-engine')
const TestBlockMiddleware = require('./util/testBlockMiddleware')
test('basic tests - constructor', (t) => {
t.plan(1)
const provider = {}
const blockTracker = new RpcBlockTracker({ provider })
t.pass('constructor did not error')
t.end()
})
test('basic tests - walking', (t) => {
t.plan(4)
const engine = new JsonRpcEngine()
const testBlockSource = new TestBlockMiddleware()
testBlockSource.nextBlock()
testBlockSource.nextBlock()
engine.push(testBlockSource.createMiddleware())
const provider = {
sendAsync: engine.handle.bind(engine),
}
const blockTracker = new RpcBlockTracker({ provider })
blockTracker.once('block', () => {
t.pass('saw 1st block')
blockTracker.once('block', () => {
t.pass('saw 2nd block')
blockTracker.once('block', () => {
t.pass('saw 3rd block')
})
})
})
blockTracker.once('latest', () => {
t.pass('saw latest block')
blockTracker.stop()
t.end()
})
blockTracker.start({ fromBlock: '0x01' })
})
test('param validity', (t) => {
const engine = new JsonRpcEngine()
const testBlockSource = new TestBlockMiddleware()
testBlockSource.nextBlock()
testBlockSource.nextBlock()
engine.push(testBlockSource.createMiddleware())
const provider = {
sendAsync: engine.handle.bind(engine),
}
const blockTracker = new RpcBlockTracker({ provider })
const methodCache = blockTracker._query.getBlockByNumber
const fakeMethod = (blockNumber, fullTxs, cb) => {
t.ok(blockNumber.substr(2).indexOf('0') !== 0, 'no leading zeroes')
methodCache.call(blockTracker._query, blockNumber, fullTxs, cb)
}
blockTracker._query.getBlockByNumber = fakeMethod
blockTracker.once('block', () => {
t.pass('saw 1st block')
blockTracker._query.getBlockByNumber = methodCache
blockTracker.stop()
t.end()
})
blockTracker.start({ fromBlock: '0x01' })
})