forked from MetaMask/web3-provider-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
150 lines (124 loc) · 3.97 KB
/
Copy pathindex.js
File metadata and controls
150 lines (124 loc) · 3.97 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 EventEmitter = require('events').EventEmitter
const inherits = require('util').inherits
const ethUtil = require('ethereumjs-util')
const map = require('async/map')
const eachSeries = require('async/eachSeries')
const Stoplight = require('./util/stoplight.js')
module.exports = Web3ProviderEngine
inherits(Web3ProviderEngine, EventEmitter)
function Web3ProviderEngine(opts) {
const self = this
EventEmitter.call(self)
self.setMaxListeners(30)
// parse options
opts = opts || {}
// set initialization blocker
self._ready = new Stoplight()
self._ready.go()
self._providers = []
}
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,
}
}