1 | const ethUtil = require('ethereumjs-util')
|
2 | const Tx = require('ethereumjs-tx')
|
3 | const Trie = require('merkle-patricia-tree')
|
4 | const BN = ethUtil.BN
|
5 | const rlp = ethUtil.rlp
|
6 | const async = require('async')
|
7 | const BlockHeader = require('./header')
|
8 | const params = require('ethereum-common/params.json')
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | var Block = module.exports = function (data) {
|
19 | this.transactions = []
|
20 | this.uncleHeaders = []
|
21 | this._inBlockChain = false
|
22 | this.txTrie = new Trie()
|
23 |
|
24 | Object.defineProperty(this, 'raw', {
|
25 | get: function () {
|
26 | return this.serialize(false)
|
27 | }
|
28 | })
|
29 |
|
30 | var rawTransactions, rawUncleHeaders
|
31 |
|
32 |
|
33 | if (!data) {
|
34 | data = [[], [], []]
|
35 | }
|
36 |
|
37 | if (Buffer.isBuffer(data)) {
|
38 | data = rlp.decode(data)
|
39 | }
|
40 |
|
41 | if (Array.isArray(data)) {
|
42 | this.header = new BlockHeader(data[0])
|
43 | rawTransactions = data[1]
|
44 | rawUncleHeaders = data[2]
|
45 | } else {
|
46 | this.header = new BlockHeader(data.header)
|
47 | rawTransactions = data.transactions || []
|
48 | rawUncleHeaders = data.uncleHeaders || []
|
49 | }
|
50 |
|
51 |
|
52 | for (var i = 0; i < rawUncleHeaders.length; i++) {
|
53 | this.uncleHeaders.push(new BlockHeader(rawUncleHeaders[i]))
|
54 | }
|
55 |
|
56 | var homestead = this.isHomestead()
|
57 |
|
58 | for (i = 0; i < rawTransactions.length; i++) {
|
59 | var tx = new Tx(rawTransactions[i])
|
60 | tx._homestead = homestead
|
61 | this.transactions.push(tx)
|
62 | }
|
63 | }
|
64 |
|
65 | Block.Header = BlockHeader
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | Block.prototype.hash = function () {
|
72 | return this.header.hash()
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | Block.prototype.isGenesis = function () {
|
81 | return this.header.isGenesis()
|
82 | }
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | Block.prototype.isHomestead = function () {
|
90 | return this.header.isHomestead()
|
91 | }
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | Block.prototype.isHomesteadReprice = function () {
|
99 | return this.header.isHomesteadReprice()
|
100 | }
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | Block.prototype.setGenesisParams = function () {
|
107 | this.header.gasLimit = params.genesisGasLimit.v
|
108 | this.header.difficulty = params.genesisDifficulty.v
|
109 | this.header.extraData = params.genesisExtraData.v
|
110 | this.header.nonce = params.genesisNonce.v
|
111 | this.header.stateRoot = params.genesisStateRoot.v
|
112 | this.header.number = new Buffer([])
|
113 | }
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | Block.prototype.serialize = function (rlpEncode) {
|
121 | var raw = [this.header.raw, [],
|
122 | []
|
123 | ]
|
124 |
|
125 |
|
126 | if (typeof rlpEncode === 'undefined') {
|
127 | rlpEncode = true
|
128 | }
|
129 |
|
130 | this.transactions.forEach(function (tx) {
|
131 | raw[1].push(tx.raw)
|
132 | })
|
133 |
|
134 | this.uncleHeaders.forEach(function (uncle) {
|
135 | raw[2].push(uncle.raw)
|
136 | })
|
137 |
|
138 | return rlpEncode ? rlp.encode(raw) : raw
|
139 | }
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | Block.prototype.genTxTrie = function (cb) {
|
148 | var i = 0
|
149 | var self = this
|
150 |
|
151 | async.eachSeries(this.transactions, function (tx, done) {
|
152 | self.txTrie.put(rlp.encode(i), tx.serialize(), done)
|
153 | i++
|
154 | }, cb)
|
155 | }
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | Block.prototype.validateTransactionsTrie = function () {
|
163 | var txT = this.header.transactionsTrie.toString('hex')
|
164 | if (this.transactions.length) {
|
165 | return txT === this.txTrie.root.toString('hex')
|
166 | } else {
|
167 | return txT === ethUtil.SHA3_RLP.toString('hex')
|
168 | }
|
169 | }
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | Block.prototype.validateTransactions = function (stringError) {
|
178 | var errors = []
|
179 |
|
180 | this.transactions.forEach(function (tx, i) {
|
181 | var error = tx.validate(true)
|
182 | if (error) {
|
183 | errors.push(error + ' at tx ' + i)
|
184 | }
|
185 | })
|
186 |
|
187 | if (stringError === undefined || stringError === false) {
|
188 | return errors.length === 0
|
189 | } else {
|
190 | return arrayToString(errors)
|
191 | }
|
192 | }
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 | Block.prototype.validate = function (blockChain, cb) {
|
201 | var self = this
|
202 | var errors = []
|
203 |
|
204 | async.parallel([
|
205 |
|
206 | self.validateUncles.bind(self, blockChain),
|
207 |
|
208 | self.header.validate.bind(self.header, blockChain),
|
209 |
|
210 | self.genTxTrie.bind(self)
|
211 | ], function (err) {
|
212 | if (err) {
|
213 | errors.push(err)
|
214 | }
|
215 |
|
216 | if (!self.validateTransactionsTrie()) {
|
217 | errors.push('invalid transaction true')
|
218 | }
|
219 |
|
220 | var txErrors = self.validateTransactions(true)
|
221 | if (txErrors !== '') {
|
222 | errors.push(txErrors)
|
223 | }
|
224 |
|
225 | if (!self.validateUnclesHash()) {
|
226 | errors.push('invild uncle hash')
|
227 | }
|
228 |
|
229 | cb(arrayToString(errors))
|
230 | })
|
231 | }
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 | Block.prototype.validateUnclesHash = function () {
|
239 | var raw = []
|
240 | this.uncleHeaders.forEach(function (uncle) {
|
241 | raw.push(uncle.raw)
|
242 | })
|
243 |
|
244 | raw = rlp.encode(raw)
|
245 | return ethUtil.sha3(raw).toString('hex') === this.header.uncleHash.toString('hex')
|
246 | }
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | Block.prototype.validateUncles = function (blockChain, cb) {
|
255 | if (this.isGenesis()) {
|
256 | return cb()
|
257 | }
|
258 |
|
259 | var self = this
|
260 |
|
261 | if (self.uncleHeaders.length > 2) {
|
262 | return cb('too many uncle headers')
|
263 | }
|
264 |
|
265 | var uncleHashes = self.uncleHeaders.map(function (header) {
|
266 | return header.hash().toString('hex')
|
267 | })
|
268 |
|
269 | if (!((new Set(uncleHashes)).size === uncleHashes.length)) {
|
270 | return cb('dublicate unlces')
|
271 | }
|
272 |
|
273 | async.each(self.uncleHeaders, function (uncle, cb2) {
|
274 | var height = new BN(self.header.number)
|
275 | async.parallel([
|
276 | uncle.validate.bind(uncle, blockChain, height),
|
277 |
|
278 | function (cb3) {
|
279 | blockChain.getDetails(uncle.hash(), function (err, blockInfo) {
|
280 |
|
281 | if (blockInfo && blockInfo.isUncle) {
|
282 | cb3(err || 'uncle already included')
|
283 | } else {
|
284 | cb3()
|
285 | }
|
286 | })
|
287 | }
|
288 | ], cb2)
|
289 | }, cb)
|
290 | }
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 | Block.prototype.toJSON = function (labeled) {
|
299 | if (labeled) {
|
300 | var obj = {
|
301 | header: this.header.toJSON(true),
|
302 | transactions: [],
|
303 | uncleHeaders: []
|
304 | }
|
305 |
|
306 | this.transactions.forEach(function (tx) {
|
307 | obj.transactions.push(tx.toJSON(labeled))
|
308 | })
|
309 |
|
310 | this.uncleHeaders.forEach(function (uh) {
|
311 | obj.uncleHeaders.push(uh.toJSON())
|
312 | })
|
313 | return obj
|
314 | } else {
|
315 | return ethUtil.baToJSON(this.raw)
|
316 | }
|
317 | }
|
318 |
|
319 | function arrayToString (array) {
|
320 | try {
|
321 | return array.reduce(function (str, err) {
|
322 | if (str) {
|
323 | str += ' '
|
324 | }
|
325 | return str + err
|
326 | })
|
327 | } catch (e) {
|
328 | return ''
|
329 | }
|
330 | }
|