/
block.js
519 lines (470 loc) · 11.9 KB
/
block.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
* Copyright © 2018 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/
'use strict';
const crypto = require('crypto');
const ByteBuffer = require('bytebuffer');
const Bignum = require('../helpers/bignum.js');
const transactionTypes = require('../helpers/transaction_types.js');
const blockVersion = require('./block_version.js');
const BlockReward = require('./block_reward.js');
const { MAX_PAYLOAD_LENGTH, FEES } = global.constants;
const __private = {};
/**
* Main Block logic.
*
* @class
* @memberof logic
* @see Parent: {@link logic}
* @requires bytebuffer
* @requires crypto
* @requires helpers/bignum
* @requires helpers/transaction_types
* @requires logic/block_reward
* @param {Object} ed
* @param {ZSchema} schema
* @param {Transaction} transaction
* @param {function} cb - Callback function
* @returns {SetImmediate} error, this
* @todo Add description for the params
*/
class Block {
constructor(ed, schema, transaction, cb) {
this.scope = {
ed,
schema,
transaction,
};
__private.blockReward = new BlockReward();
if (cb) {
return setImmediate(cb, null, this);
}
}
/**
* Sorts input data transactions.
* Calculates reward based on previous block data.
* Generates new block.
*
* @param {Object} data
* @returns {block} block
* @todo Add description for the params
*/
create(data) {
const transactions = data.transactions.sort((a, b) => {
// Place MULTI transaction after all other transaction types
if (
a.type === transactionTypes.MULTI &&
b.type !== transactionTypes.MULTI
) {
return 1;
}
// Place all other transaction types before MULTI transaction
if (
a.type !== transactionTypes.MULTI &&
b.type === transactionTypes.MULTI
) {
return -1;
}
// Place depending on type (lower first)
if (a.type < b.type) {
return -1;
}
if (a.type > b.type) {
return 1;
}
// Place depending on amount (lower first)
if (a.amount.isLessThan(b.amount)) {
return -1;
}
if (a.amount.isGreaterThan(b.amount)) {
return 1;
}
return 0;
});
const nextHeight = data.previousBlock ? data.previousBlock.height + 1 : 1;
const reward = __private.blockReward.calcReward(nextHeight);
let totalFee = new Bignum(0);
let totalAmount = new Bignum(0);
let size = 0;
const blockTransactions = [];
const payloadHash = crypto.createHash('sha256');
for (let i = 0; i < transactions.length; i++) {
const transaction = transactions[i];
const bytes = this.scope.transaction.getBytes(transaction);
if (size + bytes.length > MAX_PAYLOAD_LENGTH) {
break;
}
size += bytes.length;
totalFee = totalFee.plus(transaction.fee);
totalAmount = totalAmount.plus(transaction.amount);
blockTransactions.push(transaction);
payloadHash.update(bytes);
}
let block = {
version: blockVersion.currentBlockVersion,
totalAmount,
totalFee,
reward,
payloadHash: payloadHash.digest().toString('hex'),
timestamp: data.timestamp,
numberOfTransactions: blockTransactions.length,
payloadLength: size,
previousBlock: data.previousBlock.id,
generatorPublicKey: data.keypair.publicKey.toString('hex'),
transactions: blockTransactions,
};
try {
block.blockSignature = this.sign(block, data.keypair);
block = this.objectNormalize(block);
} catch (e) {
throw e;
}
return block;
}
/**
* Creates a block signature.
*
* @param {block} block
* @param {Object} keypair
* @returns {signature} Block signature
* @todo Add description for the params
*/
sign(block, keypair) {
const hash = this.getHash(block);
return this.scope.ed.sign(hash, keypair.privateKey).toString('hex');
}
/**
* Creates hash based on block bytes.
*
* @param {block} block
* @returns {Buffer} SHA256 hash
* @todo Add description for the params
*/
getHash(block) {
return crypto
.createHash('sha256')
.update(this.getBytes(block))
.digest();
}
/**
* Verifies block hash, generator block publicKey and block signature.
*
* @param {block} block
* @throws {Error}
* @returns {boolean} Verified hash, signature and publicKey
* @todo Add description for the params
*/
verifySignature(block) {
const signatureLength = 64;
let res;
try {
const data = this.getBytes(block);
const dataWithoutSignature = Buffer.alloc(data.length - signatureLength);
for (let i = 0; i < dataWithoutSignature.length; i++) {
dataWithoutSignature[i] = data[i];
}
const hash = crypto
.createHash('sha256')
.update(dataWithoutSignature)
.digest();
const blockSignatureBuffer = this.scope.ed.hexToBuffer(
block.blockSignature
);
const generatorPublicKeyBuffer = this.scope.ed.hexToBuffer(
block.generatorPublicKey
);
res = this.scope.ed.verify(
hash,
blockSignatureBuffer || ' ',
generatorPublicKeyBuffer || ' '
);
} catch (e) {
throw e;
}
return res;
}
/**
* Description of the function.
*
* @param {block} block
* @throws {string|Error}
* @returns {Object} Normalized block
* @todo Add description for the function and the params
*/
objectNormalize(block) {
for (const i of Object.keys(block)) {
if (block[i] === null || typeof block[i] === 'undefined') {
delete block[i];
}
}
const report = this.scope.schema.validate(block, Block.prototype.schema);
if (!report) {
throw `Failed to validate block schema: ${this.scope.schema
.getLastErrors()
.map(err => err.message)
.join(', ')}`;
}
try {
for (let i = 0; i < block.transactions.length; i++) {
block.transactions[i] = this.scope.transaction.objectNormalize(
block.transactions[i]
);
}
} catch (e) {
throw e;
}
return block;
}
}
/**
* Gets address by public.
*
* @private
* @param {publicKey} publicKey
* @returns {address} address
* @todo Add description for the params
*/
__private.getAddressByPublicKey = function(publicKey) {
const publicKeyHash = crypto
.createHash('sha256')
.update(publicKey, 'hex')
.digest();
const temp = Buffer.alloc(8);
for (let i = 0; i < 8; i++) {
temp[i] = publicKeyHash[7 - i];
}
const address = `${Bignum.fromBuffer(temp).toString()}L`;
return address;
};
// TODO: The below functions should be converted into static functions,
// however, this will lead to incompatibility with modules and tests implementation.
/**
* @typedef {Object} block
* @property {string} id - Between 1 and 20 chars
* @property {number} height
* @property {signature} blockSignature
* @property {publicKey} generatorPublicKey
* @property {number} numberOfTransactions
* @property {string} payloadHash
* @property {number} payloadLength
* @property {string} previousBlock - Between 1 and 20 chars
* @property {number} timestamp
* @property {number} totalAmount - Minimun 0
* @property {number} totalFee - Minimun 0
* @property {number} reward - Minimun 0
* @property {Array} transactions - Unique items
* @property {number} version - Minimun 0
*/
Block.prototype.schema = {
id: 'Block',
type: 'object',
properties: {
id: {
type: 'string',
format: 'id',
minLength: 1,
maxLength: 20,
},
height: {
type: 'integer',
},
blockSignature: {
type: 'string',
format: 'signature',
},
generatorPublicKey: {
type: 'string',
format: 'publicKey',
},
numberOfTransactions: {
type: 'integer',
},
payloadHash: {
type: 'string',
format: 'hex',
},
payloadLength: {
type: 'integer',
},
previousBlock: {
type: 'string',
format: 'id',
minLength: 1,
maxLength: 20,
},
timestamp: {
type: 'integer',
},
totalAmount: {
type: 'object',
format: 'amount',
},
totalFee: {
type: 'object',
format: 'amount',
},
reward: {
type: 'object',
format: 'amount',
},
transactions: {
type: 'array',
uniqueItems: true,
},
version: {
type: 'integer',
minimum: 0,
},
},
required: [
'blockSignature',
'generatorPublicKey',
'numberOfTransactions',
'payloadHash',
'payloadLength',
'timestamp',
'totalAmount',
'totalFee',
'reward',
'transactions',
'version',
],
};
/**
* Description of the function.
*
* @param {block} block
* @throws {Error}
* @returns {!Array} Contents as an ArrayBuffer
* @todo Add description for the function and the params
*/
Block.prototype.getBytes = function(block) {
const capacity =
4 + // version (int)
4 + // timestamp (int)
8 + // previousBlock
4 + // numberOfTransactions (int)
8 + // totalAmount (long)
8 + // totalFee (long)
8 + // reward (long)
4 + // payloadLength (int)
32 + // payloadHash
32 + // generatorPublicKey
64 + // blockSignature or unused
4; // unused
let bytes;
try {
const byteBuffer = new ByteBuffer(capacity, true);
byteBuffer.writeInt(block.version);
byteBuffer.writeInt(block.timestamp);
if (block.previousBlock) {
const pb = new Bignum(block.previousBlock).toBuffer({ size: '8' });
for (let i = 0; i < 8; i++) {
byteBuffer.writeByte(pb[i]);
}
} else {
for (let i = 0; i < 8; i++) {
byteBuffer.writeByte(0);
}
}
byteBuffer.writeInt(block.numberOfTransactions);
byteBuffer.writeLong(block.totalAmount.toString());
byteBuffer.writeLong(block.totalFee.toString());
byteBuffer.writeLong(block.reward.toString());
byteBuffer.writeInt(block.payloadLength);
const payloadHashBuffer = this.scope.ed.hexToBuffer(block.payloadHash);
for (let i = 0; i < payloadHashBuffer.length; i++) {
byteBuffer.writeByte(payloadHashBuffer[i]);
}
const generatorPublicKeyBuffer = this.scope.ed.hexToBuffer(
block.generatorPublicKey
);
for (let i = 0; i < generatorPublicKeyBuffer.length; i++) {
byteBuffer.writeByte(generatorPublicKeyBuffer[i]);
}
if (block.blockSignature) {
const blockSignatureBuffer = this.scope.ed.hexToBuffer(
block.blockSignature
);
for (let i = 0; i < blockSignatureBuffer.length; i++) {
byteBuffer.writeByte(blockSignatureBuffer[i]);
}
}
byteBuffer.flip();
bytes = byteBuffer.toBuffer();
} catch (e) {
throw e;
}
return bytes;
};
/**
* Calculates block id based on block.
*
* @param {block} block
* @returns {string} Block id
* @todo Add description for the params
*/
Block.prototype.getId = function(block) {
const hash = crypto
.createHash('sha256')
.update(this.getBytes(block))
.digest();
const temp = Buffer.alloc(8);
for (let i = 0; i < 8; i++) {
temp[i] = hash[7 - i];
}
// eslint-disable-next-line new-cap
const id = new Bignum.fromBuffer(temp).toString();
return id;
};
/**
* Returns send fees from constants.
*
* @returns {Bignumber} Transaction fee
* @todo Delete unused param
*/
Block.prototype.calculateFee = function() {
return new Bignum(FEES.SEND);
};
/**
* Creates block object based on raw data.
*
* @param {Object} raw
* @returns {null|block} Block object
* @todo Add description for the params
*/
Block.prototype.dbRead = function(raw) {
if (!raw.b_id) {
return null;
}
const block = {
id: raw.b_id,
version: parseInt(raw.b_version),
timestamp: parseInt(raw.b_timestamp),
height: parseInt(raw.b_height),
previousBlock: raw.b_previousBlock,
numberOfTransactions: parseInt(raw.b_numberOfTransactions),
totalAmount: new Bignum(raw.b_totalAmount),
totalFee: new Bignum(raw.b_totalFee),
reward: new Bignum(raw.b_reward),
payloadLength: parseInt(raw.b_payloadLength),
payloadHash: raw.b_payloadHash,
generatorPublicKey: raw.b_generatorPublicKey,
generatorId: __private.getAddressByPublicKey(raw.b_generatorPublicKey),
blockSignature: raw.b_blockSignature,
confirmations: parseInt(raw.b_confirmations),
};
block.totalForged = block.totalFee.plus(block.reward).toString();
return block;
};
module.exports = Block;