UNPKG

7.93 kBJavaScriptView Raw
1const ethUtil = require('ethereumjs-util')
2const Tx = require('ethereumjs-tx')
3const Trie = require('merkle-patricia-tree')
4const BN = ethUtil.BN
5const rlp = ethUtil.rlp
6const async = require('async')
7const BlockHeader = require('./header')
8const params = require('ethereum-common')
9
10/**
11 * Creates a new block object
12 * @constructor the raw serialized or the deserialized block.
13 * @param {Array|Buffer|Object} data
14 * @prop {Header} header the block's header
15 * @prop {Array.<Header>} uncleList an array of uncle headers
16 * @prop {Array.<Buffer>} raw an array of buffers containing the raw blocks.
17 */
18var 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 // defaults
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 // parse uncle headers
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 // parse transactions
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
65Block.Header = BlockHeader
66
67/**
68 * Produces a hash the RLP of the block
69 * @method hash
70 */
71Block.prototype.hash = function () {
72 return this.header.hash()
73}
74
75/**
76 * Determines if a given block is the genesis block
77 * @method isGenisis
78 * @return Boolean
79 */
80Block.prototype.isGenesis = function () {
81 return this.header.isGenesis()
82}
83
84/**
85 * Determines if a given block part of homestead or not
86 * @method isHomestead
87 * @return Boolean
88 */
89Block.prototype.isHomestead = function () {
90 return this.header.isHomestead()
91}
92
93/**
94 * turns the block in to the canonical genesis block
95 * @method setGenesisParams
96 */
97Block.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 * Produces a serialization of the block.
108 * @method serialize
109 * @param {Boolean} rlpEncode whether to rlp encode the block or not
110 */
111Block.prototype.serialize = function (rlpEncode) {
112 var raw = [this.header.raw, [],
113 []
114 ]
115
116 // rlpEnode defaults to true
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 * Generate transaction trie. The tx trie must be generated before the transaction trie can
134 * be validated with `validateTransactionTrie`
135 * @method genTxTrie
136 * @param {Function} cb the callback
137 */
138Block.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 * Validates the transaction trie
150 * @method validateTransactionTrie
151 * @return {Boolean}
152 */
153Block.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 * Validates the transactions
164 * @method validateTransactions
165 * @param {Boolean} [stringError=false] whether to return a string with a dscription of why the validation failed or return a Bloolean
166 * @return {Boolean}
167 */
168Block.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 * Validates the entire block. Returns a string to the callback if block is invalid
187 * @method validate
188 * @param {BlockChain} blockChain the blockchain that this block wants to be part of
189 * @param {Function} cb the callback which is given a `String` if the block is not valid
190 */
191Block.prototype.validate = function (blockChain, cb) {
192 var self = this
193 var errors = []
194
195 async.parallel([
196 // validate uncles
197 self.validateUncles.bind(self, blockChain),
198 // validate block
199 self.header.validate.bind(self.header, blockChain),
200 // generate the transaction trie
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 * Validates the uncle's hash
226 * @method validateUncleHash
227 * @return {Boolean}
228 */
229Block.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 * Validates the uncles that are in the block if any. Returns a string to the callback if uncles are invalid
241 * @method validateUncles
242 * @param {Blockchain} blockChaina an instance of the Blockchain
243 * @param {Function} cb the callback
244 */
245Block.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 // check to make sure the uncle is not already in the blockchain
269 function (cb3) {
270 blockChain.getDetails(uncle.hash(), function (err, blockInfo) {
271 // TODO: remove uncles from BC
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 * Converts the block toJSON
285 * @method toJSON
286 * @param {Bool} labeled whether to create an labeled object or an array
287 * @return {Object}
288 */
289Block.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
310function 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}