forked from sc-forks/provider-engine-sc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
188 lines (159 loc) · 5.24 KB
/
index.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
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
187
188
const EventEmitter = require('events').EventEmitter
const inherits = require('util').inherits
const ethUtil = require('ethereumjs-util')
const EthBlockTracker = require('eth-block-tracker')
const map = require('async/map')
const eachSeries = require('async/eachSeries')
const Stoplight = require('./util/stoplight.js')
const cacheUtils = require('./util/rpc-cache-utils.js')
const createPayload = require('./util/create-payload.js')
module.exports = Web3ProviderEngine
inherits(Web3ProviderEngine, EventEmitter)
function Web3ProviderEngine(opts) {
const self = this
EventEmitter.call(self)
self.setMaxListeners(30)
// parse options
opts = opts || {}
// block polling
const skipInitLockProvider = { sendAsync: self._handleAsync.bind(self) }
const blockTrackerProvider = opts.blockTrackerProvider || skipInitLockProvider
self._blockTracker = opts.blockTracker || new EthBlockTracker({
provider: blockTrackerProvider,
pollingInterval: opts.pollingInterval || 4000,
})
// handle new block
self._blockTracker.on('block', (jsonBlock) => {
const bufferBlock = toBufferBlock(jsonBlock)
self._setCurrentBlock(bufferBlock)
})
// emit block events from the block tracker
self._blockTracker.on('block', self.emit.bind(self, 'rawBlock'))
self._blockTracker.on('sync', self.emit.bind(self, 'sync'))
self._blockTracker.on('latest', self.emit.bind(self, 'latest'))
// set initialization blocker
self._ready = new Stoplight()
// unblock initialization after first block
self._blockTracker.once('block', () => {
self._ready.go()
})
// local state
self.currentBlock = null
self._providers = []
}
// public
Web3ProviderEngine.prototype.start = function(){
const self = this
// start block polling
self._blockTracker.start()
}
Web3ProviderEngine.prototype.stop = function(){
const self = this
// stop block polling
self._blockTracker.stop()
}
Web3ProviderEngine.prototype.addProvider = function(source){
const self = this
self._providers.push(source)
source.setEngine(this)
}
Web3ProviderEngine.prototype.send = function(payload){
throw new Error('Web3ProviderEngine does not support synchronous requests.')
}
Web3ProviderEngine.prototype.sendAsync = function(payload, cb){
const self = this
self._ready.await(function(){
if (Array.isArray(payload)) {
// handle batch
map(payload, self._handleAsync.bind(self), cb)
} else {
// handle single
self._handleAsync(payload, cb)
}
})
}
// private
Web3ProviderEngine.prototype._handleAsync = function(payload, finished) {
var self = this
var currentProvider = -1
var result = null
var error = null
var stack = []
next()
function next(after) {
currentProvider += 1
stack.unshift(after)
// Bubbled down as far as we could go, and the request wasn't
// handled. Return an error.
if (currentProvider >= self._providers.length) {
end(new Error('Request for method "' + payload.method + '" not handled by any subprovider. Please check your subprovider configuration to ensure this method is handled.'))
} else {
try {
var provider = self._providers[currentProvider]
provider.handleRequest(payload, next, end)
} catch (e) {
end(e)
}
}
}
function end(_error, _result) {
error = _error
result = _result
eachSeries(stack, function(fn, callback) {
if (fn) {
fn(error, result, callback)
} else {
callback()
}
}, function() {
// console.log('COMPLETED:', payload)
// console.log('RESULT: ', result)
var resultObj = {
id: payload.id,
jsonrpc: payload.jsonrpc,
result: result
}
if (error != null) {
resultObj.error = {
message: error.stack || error.message || error,
code: -32000
}
// respond with both error formats
finished(error, resultObj)
} else {
finished(null, resultObj)
}
})
}
}
//
// from remote-data
//
Web3ProviderEngine.prototype._setCurrentBlock = function(block){
const self = this
self.currentBlock = block
self.emit('block', block)
}
// util
function toBufferBlock (jsonBlock) {
return {
number: ethUtil.toBuffer(jsonBlock.number),
hash: ethUtil.toBuffer(jsonBlock.hash),
parentHash: ethUtil.toBuffer(jsonBlock.parentHash),
nonce: ethUtil.toBuffer(jsonBlock.nonce),
sha3Uncles: ethUtil.toBuffer(jsonBlock.sha3Uncles),
logsBloom: ethUtil.toBuffer(jsonBlock.logsBloom),
transactionsRoot: ethUtil.toBuffer(jsonBlock.transactionsRoot),
stateRoot: ethUtil.toBuffer(jsonBlock.stateRoot),
receiptsRoot: ethUtil.toBuffer(jsonBlock.receiptRoot || jsonBlock.receiptsRoot),
miner: ethUtil.toBuffer(jsonBlock.miner),
difficulty: ethUtil.toBuffer(jsonBlock.difficulty),
totalDifficulty: ethUtil.toBuffer(jsonBlock.totalDifficulty),
size: ethUtil.toBuffer(jsonBlock.size),
extraData: ethUtil.toBuffer(jsonBlock.extraData),
gasLimit: ethUtil.toBuffer(jsonBlock.gasLimit),
gasUsed: ethUtil.toBuffer(jsonBlock.gasUsed),
timestamp: ethUtil.toBuffer(jsonBlock.timestamp),
transactions: jsonBlock.transactions,
}
}