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')
|
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 | Block.prototype.setGenesisParams = function () {
|
98 | this.header.gasLimit = params.genesisGasLimit.v
|
99 | this.header.difficulty = params.genesisDifficulty.v
|
100 | this.header.extraData = params.genesisExtraData.v
|
101 | this.header.nonce = params.genesisNonce.v
|
102 | this.header.stateRoot = params.genesisStateRoot.v
|
103 | this.header.number = new Buffer([])
|
104 | }
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | Block.prototype.serialize = function (rlpEncode) {
|
112 | var raw = [this.header.raw, [],
|
113 | []
|
114 | ]
|
115 |
|
116 |
|
117 | if (typeof rlpEncode === 'undefined') {
|
118 | rlpEncode = true
|
119 | }
|
120 |
|
121 | this.transactions.forEach(function (tx) {
|
122 | raw[1].push(tx.raw)
|
123 | })
|
124 |
|
125 | this.uncleHeaders.forEach(function (uncle) {
|
126 | raw[2].push(uncle.raw)
|
127 | })
|
128 |
|
129 | return rlpEncode ? rlp.encode(raw) : raw
|
130 | }
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | Block.prototype.genTxTrie = function (cb) {
|
139 | var i = 0
|
140 | var self = this
|
141 |
|
142 | async.eachSeries(this.transactions, function (tx, done) {
|
143 | self.txTrie.put(rlp.encode(i), tx.serialize(), done)
|
144 | i++
|
145 | }, cb)
|
146 | }
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | Block.prototype.validateTransactionsTrie = function () {
|
154 | var txT = this.header.transactionsTrie.toString('hex')
|
155 | if (this.transactions.length) {
|
156 | return txT === this.txTrie.root.toString('hex')
|
157 | } else {
|
158 | return txT === ethUtil.SHA3_RLP.toString('hex')
|
159 | }
|
160 | }
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | Block.prototype.validateTransactions = function (stringError) {
|
169 | var errors = []
|
170 |
|
171 | this.transactions.forEach(function (tx, i) {
|
172 | var error = tx.validate(true)
|
173 | if (error) {
|
174 | errors.push(error + ' at tx ' + i)
|
175 | }
|
176 | })
|
177 |
|
178 | if (stringError === undefined || stringError === false) {
|
179 | return errors.length === 0
|
180 | } else {
|
181 | return arrayToString(errors)
|
182 | }
|
183 | }
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | Block.prototype.validate = function (blockChain, cb) {
|
192 | var self = this
|
193 | var errors = []
|
194 |
|
195 | async.parallel([
|
196 |
|
197 | self.validateUncles.bind(self, blockChain),
|
198 |
|
199 | self.header.validate.bind(self.header, blockChain),
|
200 |
|
201 | self.genTxTrie.bind(self)
|
202 | ], function (err) {
|
203 | if (err) {
|
204 | errors.push(err)
|
205 | }
|
206 |
|
207 | if (!self.validateTransactionsTrie()) {
|
208 | errors.push('invalid transaction true')
|
209 | }
|
210 |
|
211 | var txErrors = self.validateTransactions(true)
|
212 | if (txErrors !== '') {
|
213 | errors.push(txErrors)
|
214 | }
|
215 |
|
216 | if (!self.validateUnclesHash()) {
|
217 | errors.push('invild uncle hash')
|
218 | }
|
219 |
|
220 | cb(arrayToString(errors))
|
221 | })
|
222 | }
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 | Block.prototype.validateUnclesHash = function () {
|
230 | var raw = []
|
231 | this.uncleHeaders.forEach(function (uncle) {
|
232 | raw.push(uncle.raw)
|
233 | })
|
234 |
|
235 | raw = rlp.encode(raw)
|
236 | return ethUtil.sha3(raw).toString('hex') === this.header.uncleHash.toString('hex')
|
237 | }
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 | Block.prototype.validateUncles = function (blockChain, cb) {
|
246 | if (this.isGenesis()) {
|
247 | return cb()
|
248 | }
|
249 |
|
250 | var self = this
|
251 |
|
252 | if (self.uncleHeaders.length > 2) {
|
253 | return cb('too many uncle headers')
|
254 | }
|
255 |
|
256 | var uncleHashes = self.uncleHeaders.map(function (header) {
|
257 | return header.hash().toString('hex')
|
258 | })
|
259 |
|
260 | if (!((new Set(uncleHashes)).size === uncleHashes.length)) {
|
261 | return cb('dublicate unlces')
|
262 | }
|
263 |
|
264 | async.each(self.uncleHeaders, function (uncle, cb2) {
|
265 | var height = new BN(self.header.number)
|
266 | async.parallel([
|
267 | uncle.validate.bind(uncle, blockChain, height),
|
268 |
|
269 | function (cb3) {
|
270 | blockChain.getDetails(uncle.hash(), function (err, blockInfo) {
|
271 |
|
272 | if (blockInfo && blockInfo.isUncle) {
|
273 | cb3(err || 'uncle already included')
|
274 | } else {
|
275 | cb3()
|
276 | }
|
277 | })
|
278 | }
|
279 | ], cb2)
|
280 | }, cb)
|
281 | }
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 | Block.prototype.toJSON = function (labeled) {
|
290 | if (labeled) {
|
291 | var obj = {
|
292 | header: this.header.toJSON(true),
|
293 | transactions: [],
|
294 | uncleHeaders: []
|
295 | }
|
296 |
|
297 | this.transactions.forEach(function (tx) {
|
298 | obj.transactions.push(tx.toJSON(labeled))
|
299 | })
|
300 |
|
301 | this.uncleHeaders.forEach(function (uh) {
|
302 | obj.uncleHeaders.push(uh.toJSON())
|
303 | })
|
304 | return obj
|
305 | } else {
|
306 | return ethUtil.baToJSON(this.raw)
|
307 | }
|
308 | }
|
309 |
|
310 | function arrayToString (array) {
|
311 | try {
|
312 | return array.reduce(function (str, err) {
|
313 | if (str) {
|
314 | str += ' '
|
315 | }
|
316 | return str + err
|
317 | })
|
318 | } catch (e) {
|
319 | return ''
|
320 | }
|
321 | }
|