-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriptionManager.js
129 lines (115 loc) · 3.77 KB
/
subscriptionManager.js
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
const SafeEventEmitter = require('safe-event-emitter')
const createScaffoldMiddleware = require('eth-json-rpc-middleware/scaffold')
const createAsyncMiddleware = require('json-rpc-engine/src/createAsyncMiddleware')
const createFilterMiddleware = require('./index.js')
const { unsafeRandomBytes, incrementHexInt } = require('./hexUtils.js')
const getBlocksForRange = require('./getBlocksForRange.js')
module.exports = createSubscriptionMiddleware
function createSubscriptionMiddleware({ blockTracker, provider }) {
// state and utilities for handling subscriptions
const subscriptions = {}
const filterManager = createFilterMiddleware({ blockTracker, provider })
// create subscriptionManager api object
const events = new SafeEventEmitter()
const middleware = createScaffoldMiddleware({
eth_subscribe: createAsyncMiddleware(subscribe),
eth_unsubscribe: createAsyncMiddleware(unsubscribe),
})
return { events, middleware }
async function subscribe(req, res) {
const subscriptionType = req.params[0]
// subId is 16 byte hex string
const subId = unsafeRandomBytes(16)
// create sub
let sub
switch (subscriptionType) {
case 'newHeads':
sub = createSubNewHeads({ subId })
break
case 'logs':
const filterParams = req.params[1]
const filter = await filterManager.newLogFilter(filterParams)
sub = createSubFromFilter({ subId, filter })
break
default:
throw new Error(`SubscriptionManager - unsupported subscription type "${subscriptionType}"`)
}
subscriptions[subId] = sub
res.result = subId
return
function createSubNewHeads({ subId }) {
const sub = {
type: subscriptionType,
destroy: async () => {
blockTracker.removeListener('sync', sub.update)
},
update: async ({ oldBlock, newBlock }) => {
// for newHeads
const toBlock = newBlock
const fromBlock = incrementHexInt(oldBlock)
const rawBlocks = await getBlocksForRange({ provider, fromBlock, toBlock })
const results = rawBlocks.map(normalizeBlock)
results.forEach((value) => {
_emitSubscriptionResult(subId, value)
})
}
}
// check for subscription updates on new block
blockTracker.on('sync', sub.update)
return sub
}
function createSubFromFilter({ subId, filter }){
filter.on('update', result => _emitSubscriptionResult(subId, result))
const sub = {
type: subscriptionType,
destroy: async () => {
return await filterManager.uninstallFilter(filter.idHex)
},
}
return sub
}
}
async function unsubscribe(req, res) {
const id = req.params[0]
const subscription = subscriptions[id]
// if missing, return "false" to indicate it was not removed
if (!subscription) {
res.result = false
return
}
// cleanup subscription
delete subscriptions[id]
await subscription.destroy()
res.result = true
}
function _emitSubscriptionResult(filterIdHex, value) {
events.emit('notification', {
jsonrpc: '2.0',
method: 'eth_subscription',
params: {
subscription: filterIdHex,
result: value,
},
})
}
}
function normalizeBlock(block) {
return {
hash: block.hash,
parentHash: block.parentHash,
sha3Uncles: block.sha3Uncles,
miner: block.miner,
stateRoot: block.stateRoot,
transactionsRoot: block.transactionsRoot,
receiptsRoot: block.receiptsRoot,
logsBloom: block.logsBloom,
difficulty: block.difficulty,
number: block.number,
gasLimit: block.gasLimit,
gasUsed: block.gasUsed,
nonce: block.nonce,
mixHash: block.mixHash,
timestamp: block.timestamp,
extraData: block.extraData,
}
}