This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 457
/
Copy pathverify.js
887 lines (812 loc) · 24.9 KB
/
verify.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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
/*
* 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 _ = require('lodash');
const async = require('async');
const BlockReward = require('../../logic/block_reward.js');
const slots = require('../../helpers/slots.js');
const blockVersion = require('../../logic/block_version.js');
const Bignum = require('../../helpers/bignum.js');
let modules;
let library;
let self;
const exceptions = global.exceptions;
const {
BLOCK_SLOT_WINDOW,
MAX_PAYLOAD_LENGTH,
MAX_TRANSACTIONS_PER_BLOCK,
} = global.constants;
const __private = {};
__private.lastNBlockIds = [];
/**
* Description of the class.
*
* @class
* @memberof modules.blocks
* @see Parent: {@link modules.blocks}
* @requires async
* @requires crypto
* @requires lodash
* @requires helpers/slots
* @requires logic/block_reward
* @todo Add @param tags
* @todo Add description for the class
*/
class Verify {
constructor(logger, block, transaction, db, config) {
library = {
logger,
db,
logic: {
block,
transaction,
},
config: {
loading: {
snapshotRound: config.loading.snapshotRound,
},
},
};
self = this;
__private.blockReward = new BlockReward();
library.logger.trace('Blocks->Verify: Submodule initialized.');
return self;
}
}
/**
* Check transaction - perform transaction validation when processing block.
* FIXME: Some checks are probably redundant, see: logic.transactionPool
*
* @private
* @func checkTransaction
* @param {Object} block - Block object
* @param {Object} transaction - Transaction object
* @param {boolean} checkExists - Check if transaction already exists in database
* @param {function} cb - Callback function
* @returns {function} cb - Callback function from params (through setImmediate)
* @returns {Object} cb.err - Error if occurred
*/
__private.checkTransaction = function(block, transaction, checkExists, cb) {
async.waterfall(
[
function getTransactionId(waterCb) {
try {
// Calculate transaction ID
// FIXME: Can have poor performance, because of hash calculation
transaction.id = library.logic.transaction.getId(transaction);
} catch (e) {
return setImmediate(waterCb, e.toString());
}
// Apply block ID to transaction
transaction.blockId = block.id;
return setImmediate(waterCb);
},
function getAccount(waterCb) {
// Get account from database if any (otherwise cold wallet)
// DATABASE: read only
modules.accounts.getAccount(
{ publicKey: transaction.senderPublicKey },
waterCb
);
},
function verifyTransaction(sender, waterCb) {
// Check if transaction id valid against database state (mem_* tables)
// DATABASE: read only
library.logic.transaction.verify(
transaction,
sender,
null,
checkExists,
waterCb,
null
);
},
],
waterCbErr => {
if (waterCbErr && waterCbErr.match(/Transaction is already confirmed/)) {
// Fork: Transaction already confirmed.
modules.delegates.fork(block, 2);
// Undo the offending transaction.
// DATABASE: write
modules.transactions.undoUnconfirmed(
transaction,
undoUnconfirmedErr => {
modules.transactions.removeUnconfirmedTransaction(transaction.id);
return setImmediate(cb, undoUnconfirmedErr || waterCbErr);
}
);
} else {
return setImmediate(cb, waterCbErr);
}
}
);
};
/**
* Set height according to the given last block.
*
* @private
* @func setHeight
* @param {Object} block - Target block
* @param {Object} lastBlock - Last block
* @returns {Object} block - Target block
*/
__private.setHeight = function(block, lastBlock) {
block.height = lastBlock.height + 1;
return block;
};
/**
* Verify block signature.
*
* @private
* @func verifySignature
* @param {Object} block - Target block
* @param {Object} result - Verification results
* @returns {Object} result - Verification results
* @returns {boolean} result.verified - Indicator that verification passed
* @returns {Array} result.errors - Array of validation errors
*/
__private.verifySignature = function(block, result) {
let valid;
try {
valid = library.logic.block.verifySignature(block);
} catch (e) {
result.errors.push(e.toString());
}
if (!valid) {
result.errors.push('Failed to verify block signature');
}
return result;
};
/**
* Verify previous block.
*
* @private
* @func verifyPreviousBlock
* @param {Object} block - Target block
* @param {Object} result - Verification results
* @returns {Object} result - Verification results
* @returns {boolean} result.verified - Indicator that verification passed
* @returns {Array} result.errors - Array of validation errors
*/
__private.verifyPreviousBlock = function(block, result) {
if (!block.previousBlock && block.height !== 1) {
result.errors.push('Invalid previous block');
}
return result;
};
/**
* Verify block is not one of the last {BLOCK_SLOT_WINDOW} saved blocks.
*
* @private
* @func verifyAgainstLastNBlockIds
* @param {Object} block - Target block
* @param {Object} result - Verification results
* @returns {Object} result - Verification results
* @returns {boolean} result.verified - Indicator that verification passed
* @returns {Array} result.errors - Array of validation errors
*/
__private.verifyAgainstLastNBlockIds = function(block, result) {
if (__private.lastNBlockIds.indexOf(block.id) !== -1) {
result.errors.push('Block already exists in chain');
}
return result;
};
/**
* Verify block version.
*
* @private
* @func verifyVersion
* @param {Object} block - Target block
* @param {Object} result - Verification results
* @returns {Object} result - Verification results
* @returns {boolean} result.verified - Indicator that verification passed
* @returns {Array} result.errors - Array of validation errors
*/
__private.verifyVersion = function(block, result) {
if (!blockVersion.isValid(block.version, block.height)) {
result.errors.push('Invalid block version');
}
return result;
};
/**
* Verify block reward.
*
* @private
* @func verifyReward
* @param {Object} block - Target block
* @param {Object} result - Verification results
* @returns {Object} result - Verification results
* @returns {boolean} result.verified - Indicator that verification passed
* @returns {Array} result.errors - Array of validation errors
*/
__private.verifyReward = function(block, result) {
const expectedReward = __private.blockReward.calcReward(block.height);
if (
block.height !== 1 &&
!expectedReward.equals(block.reward) &&
exceptions.blockRewards.indexOf(block.id) === -1
) {
result.errors.push(
['Invalid block reward:', block.reward, 'expected:', expectedReward].join(
' '
)
);
}
return result;
};
/**
* Verify block id.
*
* @private
* @func verifyId
* @param {Object} block - Target block
* @param {Object} result - Verification results
* @returns {Object} result - Verification results
* @returns {boolean} result.verified - Indicator that verification passed
* @returns {Array} result.errors - Array of validation errors
*/
__private.verifyId = function(block, result) {
try {
// Get block ID
// FIXME: Why we don't have it?
block.id = library.logic.block.getId(block);
} catch (e) {
result.errors.push(e.toString());
}
return result;
};
/**
* Verify block payload (transactions).
*
* @private
* @func verifyPayload
* @param {Object} block - Target block
* @param {Object} result - Verification results
* @returns {Object} result - Verification results
* @returns {boolean} result.verified - Indicator that verification passed
* @returns {Array} result.errors - Array of validation errors
*/
__private.verifyPayload = function(block, result) {
if (block.payloadLength > MAX_PAYLOAD_LENGTH) {
result.errors.push('Payload length is too long');
}
if (block.transactions.length !== block.numberOfTransactions) {
result.errors.push(
'Included transactions do not match block transactions count'
);
}
if (block.transactions.length > MAX_TRANSACTIONS_PER_BLOCK) {
result.errors.push('Number of transactions exceeds maximum per block');
}
let totalAmount = new Bignum(0);
let totalFee = new Bignum(0);
const payloadHash = crypto.createHash('sha256');
const appliedTransactions = {};
for (const i in block.transactions) {
const transaction = block.transactions[i];
let bytes;
try {
bytes = library.logic.transaction.getBytes(transaction);
} catch (e) {
result.errors.push(e.toString());
}
if (appliedTransactions[transaction.id]) {
result.errors.push(
`Encountered duplicate transaction: ${transaction.id}`
);
}
appliedTransactions[transaction.id] = transaction;
if (bytes) {
payloadHash.update(bytes);
}
totalAmount = totalAmount.plus(transaction.amount);
totalFee = totalFee.plus(transaction.fee);
}
if (payloadHash.digest().toString('hex') !== block.payloadHash) {
result.errors.push('Invalid payload hash');
}
if (!totalAmount.equals(block.totalAmount)) {
result.errors.push('Invalid total amount');
}
if (!totalFee.equals(block.totalFee)) {
result.errors.push('Invalid total fee');
}
return result;
};
/**
* Verify block for fork cause one.
*
* @private
* @func verifyForkOne
* @param {Object} block - Target block
* @param {Object} lastBlock - Last block
* @param {Object} result - Verification results
* @returns {Object} result - Verification results
* @returns {boolean} result.verified - Indicator that verification passed
* @returns {Array} result.errors - Array of validation errors
*/
__private.verifyForkOne = function(block, lastBlock, result) {
if (block.previousBlock && block.previousBlock !== lastBlock.id) {
modules.delegates.fork(block, 1);
result.errors.push(
[
'Invalid previous block:',
block.previousBlock,
'expected:',
lastBlock.id,
].join(' ')
);
}
return result;
};
/**
* Verify block slot according to timestamp.
*
* @private
* @func verifyBlockSlot
* @param {Object} block - Target block
* @param {Object} lastBlock - Last block
* @param {Object} result - Verification results
* @returns {Object} result - Verification results
* @returns {boolean} result.verified - Indicator that verification passed
* @returns {Array} result.errors - Array of validation errors
*/
__private.verifyBlockSlot = function(block, lastBlock, result) {
const blockSlotNumber = slots.getSlotNumber(block.timestamp);
const lastBlockSlotNumber = slots.getSlotNumber(lastBlock.timestamp);
if (
blockSlotNumber > slots.getSlotNumber() ||
blockSlotNumber <= lastBlockSlotNumber
) {
result.errors.push('Invalid block timestamp');
}
return result;
};
/**
* Verify block slot window according to application time.
*
* @private
* @func verifyBlockSlotWindow
* @param {Object} block - Target block
* @returns {Object} result - Verification results
* @returns {boolean} result.verified - Indicator that verification passed
* @returns {Array} result.errors - Array of validation errors
*/
__private.verifyBlockSlotWindow = function(block, result) {
const currentApplicationSlot = slots.getSlotNumber();
const blockSlot = slots.getSlotNumber(block.timestamp);
// Reject block if it's slot is older than BLOCK_SLOT_WINDOW
if (currentApplicationSlot - blockSlot > BLOCK_SLOT_WINDOW) {
result.errors.push('Block slot is too old');
}
// Reject block if it's slot is in the future
if (currentApplicationSlot < blockSlot) {
result.errors.push('Block slot is in the future');
}
return result;
};
/**
* Verify block before fork detection and return all possible errors related to block.
*
* @param {Object} block - Full block
* @returns {Object} result - Verification results
* @returns {boolean} result.verified - Indicator that verification passed
* @returns {Array} result.errors - Array of validation errors
*/
Verify.prototype.verifyReceipt = function(block) {
const lastBlock = modules.blocks.lastBlock.get();
block = __private.setHeight(block, lastBlock);
let result = { verified: false, errors: [] };
result = __private.verifySignature(block, result);
result = __private.verifyPreviousBlock(block, result);
result = __private.verifyAgainstLastNBlockIds(block, result);
result = __private.verifyBlockSlotWindow(block, result);
result = __private.verifyVersion(block, result);
result = __private.verifyReward(block, result);
result = __private.verifyId(block, result);
result = __private.verifyPayload(block, result);
result.verified = result.errors.length === 0;
result.errors.reverse();
return result;
};
/**
* Loads last {BLOCK_SLOT_WINDOW} blocks from the database into memory. Called when application triggeres blockchainReady event.
*/
Verify.prototype.onBlockchainReady = function() {
return library.db.blocks
.loadLastNBlockIds(BLOCK_SLOT_WINDOW)
.then(blockIds => {
__private.lastNBlockIds = _.map(blockIds, 'id');
})
.catch(err => {
library.logger.error(
`Unable to load last ${BLOCK_SLOT_WINDOW} block ids`
);
library.logger.error(err);
});
};
/**
* Maintains __private.lastNBlock constiable - a queue of fixed length (BLOCK_SLOT_WINDOW). Called when application triggers newBlock event.
*
* @func onNewBlock
* @param {block} block
* @todo Add description for the params
*/
Verify.prototype.onNewBlock = function(block) {
__private.lastNBlockIds.push(block.id);
if (__private.lastNBlockIds.length > BLOCK_SLOT_WINDOW) {
__private.lastNBlockIds.shift();
}
};
/**
* Verify block before processing and return all possible errors related to block.
*
* @param {Object} block - Full block
* @returns {Object} result - Verification results
* @returns {boolean} result.verified - Indicator that verification passed
* @returns {Array} result.errors - Array of validation errors
*/
Verify.prototype.verifyBlock = function(block) {
const lastBlock = modules.blocks.lastBlock.get();
block = __private.setHeight(block, lastBlock);
let result = { verified: false, errors: [] };
result = __private.verifySignature(block, result);
result = __private.verifyPreviousBlock(block, result);
result = __private.verifyVersion(block, result);
result = __private.verifyReward(block, result);
result = __private.verifyId(block, result);
result = __private.verifyPayload(block, result);
result = __private.verifyForkOne(block, lastBlock, result);
result = __private.verifyBlockSlot(block, lastBlock, result);
result.verified = result.errors.length === 0;
result.errors.reverse();
if (result.verified) {
library.logger.info(
`Verify->verifyBlock succeeded for block ${block.id} at height ${
block.height
}.`
);
} else {
library.logger.error(
`Verify->verifyBlock failed for block ${block.id} at height ${
block.height
}.`,
result.errors
);
}
return result;
};
/**
* Adds default properties to block.
*
* @param {Object} block - Block object reduced
* @returns {Object} Block object completed
*/
Verify.prototype.addBlockProperties = function(block) {
block.totalAmount = new Bignum(block.totalAmount || 0);
block.totalFee = new Bignum(block.totalFee || 0);
block.reward = new Bignum(block.reward || 0);
if (block.version === undefined) {
block.version = 0;
}
if (block.numberOfTransactions === undefined) {
if (block.transactions === undefined) {
block.numberOfTransactions = 0;
} else {
block.numberOfTransactions = block.transactions.length;
}
}
if (block.payloadLength === undefined) {
block.payloadLength = 0;
}
if (block.transactions === undefined) {
block.transactions = [];
}
return block;
};
/**
* Deletes default properties from block.
*
* @param {Object} block - Block object completed
* @returns {Object} Block object reduced
*/
Verify.prototype.deleteBlockProperties = function(block) {
const reducedBlock = Object.assign({}, block);
if (reducedBlock.version === 0) {
delete reducedBlock.version;
}
// verifyBlock ensures numberOfTransactions is transactions.length
if (typeof reducedBlock.numberOfTransactions === 'number') {
delete reducedBlock.numberOfTransactions;
}
if (reducedBlock.totalAmount.equals(0)) {
delete reducedBlock.totalAmount;
}
if (reducedBlock.totalFee.equals(0)) {
delete reducedBlock.totalFee;
}
if (reducedBlock.payloadLength === 0) {
delete reducedBlock.payloadLength;
}
if (reducedBlock.reward.equals(0)) {
delete reducedBlock.reward;
}
if (reducedBlock.transactions && reducedBlock.transactions.length === 0) {
delete reducedBlock.transactions;
}
return reducedBlock;
};
/**
* Adds block properties.
*
* @private
* @func addBlockProperties
* @param {Object} block - Full block
* @param {boolean} broadcast - Indicator that block needs to be broadcasted
* @param {function} cb - Callback function
* @returns {function} cb - Callback function from params (through setImmediate)
* @returns {Object} cb.err - Error if occurred
*/
__private.addBlockProperties = function(block, broadcast, cb) {
if (!broadcast) {
try {
// Set default properties
block = self.addBlockProperties(block);
} catch (err) {
return setImmediate(cb, err);
}
}
return setImmediate(cb);
};
/**
* Validates block schema.
*
* @private
* @func normalizeBlock
* @param {Object} block - Full block
* @param {function} cb - Callback function
* @returns {function} cb - Callback function from params (through setImmediate)
* @returns {Object} cb.err - Error if occurred
*/
__private.normalizeBlock = function(block, cb) {
try {
block = library.logic.block.objectNormalize(block);
} catch (err) {
return setImmediate(cb, err);
}
return setImmediate(cb);
};
/**
* Verifies block.
*
* @private
* @func verifyBlock
* @param {Object} block - Full block
* @param {function} cb - Callback function
* @returns {function} cb - Callback function from params (through setImmediate)
* @returns {Object} cb.err - Error if occurred
*/
__private.verifyBlock = function(block, cb) {
// Sanity check of the block, if values are coherent
// No access to database
var result = self.verifyBlock(block);
if (!result.verified) {
library.logger.error(
['Block', block.id, 'verification failed'].join(' '),
result.errors[0]
);
return setImmediate(cb, result.errors[0]);
}
return setImmediate(cb);
};
/**
* Broadcasts block.
*
* @private
* @func broadcastBlock
* @param {Object} block - Full block
* @param {boolean} broadcast - Indicator that block needs to be broadcasted
* @param {function} cb - Callback function
* @returns {function} cb - Callback function from params (through setImmediate)
* @returns {Object} cb.err - Error if occurred
*/
__private.broadcastBlock = function(block, broadcast, cb) {
if (broadcast) {
try {
// Delete default properties
var reducedBlock = self.deleteBlockProperties(block);
modules.blocks.chain.broadcastReducedBlock(reducedBlock, broadcast);
} catch (err) {
return setImmediate(cb, err);
}
}
return setImmediate(cb);
};
/**
* Checks if block is in database.
*
* @private
* @func checkExists
* @param {Object} block - Full block
* @param {function} cb - Callback function
* @returns {function} cb - Callback function from params (through setImmediate)
* @returns {Object} cb.err - Error if occurred
*/
__private.checkExists = function(block, cb) {
// Check if block id is already in the database (very low probability of hash collision)
// TODO: In case of hash-collision, to me it would be a special autofork...
// DATABASE: read only
library.db.blocks
.blockExists(block.id)
.then(rows => {
if (rows) {
return setImmediate(
cb,
['Block', block.id, 'already exists'].join(' ')
);
}
return setImmediate(cb);
})
.catch(err => {
library.logger.error(err);
return setImmediate(cb, 'Block#blockExists error');
});
};
/**
* Checks if block was generated by the right active delagate.
*
* @private
* @func validateBlockSlot
* @param {Object} block - Full block
* @param {function} cb - Callback function
* @returns {function} cb - Callback function from params (through setImmediate)
* @returns {Object} cb.err - Error if occurred
*/
__private.validateBlockSlot = function(block, cb) {
// Check if block was generated by the right active delagate. Otherwise, fork 3
// DATABASE: Read only to mem_accounts to extract active delegate list
modules.delegates.validateBlockSlot(block, err => {
if (err) {
// Fork: Delegate does not match calculated slot
modules.delegates.fork(block, 3);
return setImmediate(cb, err);
}
return setImmediate(cb);
});
};
/**
* Checks transactions in block.
*
* @private
* @func checkTransactions
* @param {Object} block - Full block
* @param {boolean} checkExists - Check if transactions already exists in database
* @param {function} cb - Callback function
* @returns {function} cb - Callback function from params (through setImmediate)
* @returns {Object} cb.err - Error if occurred
*/
__private.checkTransactions = function(block, checkExists, cb) {
// Check against the mem_* tables that we can perform the transactions included in the block
async.eachSeries(
block.transactions,
(transaction, eachSeriesCb) => {
__private.checkTransaction(block, transaction, checkExists, eachSeriesCb);
},
err => setImmediate(cb, err)
);
};
/**
* Main function to process a block:
* - Verify the block looks ok
* - Verify the block is compatible with database state (DATABASE readonly)
* - Broadcast the block to remote peers
* - Apply the block to database if both verifications are ok
* - Update headers: broadhash and height
* - Notify remote peers about our new headers
*
* @param {Object} block - Full block
* @param {boolean} broadcast - Indicator that block needs to be broadcasted
* @param {function} cb - Callback function
* @param {boolean} saveBlock - Indicator that block needs to be saved to database
* @returns {function} cb - Callback function from params (through setImmediate)
* @returns {Object} cb.err - Error if occurred
*/
Verify.prototype.processBlock = function(block, broadcast, saveBlock, cb) {
if (modules.blocks.isCleaning.get()) {
// Break processing if node shutdown reqested
return setImmediate(cb, 'Cleaning up');
} else if (!__private.loaded) {
// Break processing if blockchain is not loaded
return setImmediate(cb, 'Blockchain is loading');
}
async.series(
{
addBlockProperties(seriesCb) {
__private.addBlockProperties(block, broadcast, seriesCb);
},
normalizeBlock(seriesCb) {
__private.normalizeBlock(block, seriesCb);
},
verifyBlock(seriesCb) {
__private.verifyBlock(block, seriesCb);
},
broadcastBlock(seriesCb) {
__private.broadcastBlock(block, broadcast, seriesCb);
},
checkExists(seriesCb) {
// Skip checking for existing block id if we don't need to save that block
if (!saveBlock) {
return setImmediate(seriesCb);
}
__private.checkExists(block, seriesCb);
},
validateBlockSlot(seriesCb) {
__private.validateBlockSlot(block, seriesCb);
},
checkTransactions(seriesCb) {
// checkTransactions should check for transactions to exists in database
// only if the block needed to be saved to database
__private.checkTransactions(block, saveBlock, seriesCb);
},
applyBlock(seriesCb) {
// The block and the transactions are OK i.e:
// * Block and transactions have valid values (signatures, block slots, etc...)
// * The check against database state passed (for instance sender has enough LSK, votes are under 101, etc...)
// We thus update the database with the transactions values, save the block and tick it.
// Also that function set new block as our last block
modules.blocks.chain.applyBlock(block, saveBlock, seriesCb);
},
// Perform next two steps only when 'broadcast' flag is set, it can be:
// 'true' if block comes from generation or receiving process
// 'false' if block comes from chain synchronisation process
updateSystemHeaders(seriesCb) {
// Update our own headers: broadhash and height
!library.config.loading.snapshotRound
? modules.system.update(seriesCb)
: seriesCb();
},
broadcastHeaders(seriesCb) {
// Notify all remote peers about our new headers
broadcast ? modules.transport.broadcastHeaders(seriesCb) : seriesCb();
},
},
err => setImmediate(cb, err)
);
};
/**
* Handle modules initialization:
* - accounts
* - blocks
* - delegates
* - transactions
* - system
* - transport
*
* @param {Object} scope - Exposed modules
*/
Verify.prototype.onBind = function(scope) {
library.logger.trace('Blocks->Verify: Shared modules bind.');
modules = {
accounts: scope.accounts,
blocks: scope.blocks,
delegates: scope.delegates,
transactions: scope.transactions,
system: scope.system,
transport: scope.transport,
};
// Set module as loaded
__private.loaded = true;
};
module.exports = Verify;