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