/
block_processor_v2.js
410 lines (358 loc) · 10.7 KB
/
block_processor_v2.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
/*
* Copyright © 2019 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 BigNum = require('@liskhq/bignum');
const { validator } = require('@liskhq/lisk-validator');
const {
BIG_ENDIAN,
hash,
signDataWithPrivateKey,
hexToBuffer,
intToBuffer,
LITTLE_ENDIAN,
} = require('@liskhq/lisk-cryptography');
const { baseBlockSchema } = require('./blocks');
const { BaseBlockProcessor } = require('./processor');
const FORGER_INFO_KEY_MAX_HEIGHT_PREVIOUSLY_FORGED =
'maxHeightPreviouslyForged';
const SIZE_INT32 = 4;
const SIZE_INT64 = 8;
const blockSchema = {
...baseBlockSchema,
properties: {
...baseBlockSchema.properties,
maxHeightPreviouslyForged: {
type: 'integer',
},
maxHeightPrevoted: {
type: 'integer',
},
},
required: [
...baseBlockSchema.required,
'maxHeightPreviouslyForged',
'maxHeightPrevoted',
'height',
],
};
const getBytes = block => {
const blockVersionBuffer = intToBuffer(
block.version,
SIZE_INT32,
LITTLE_ENDIAN,
);
const timestampBuffer = intToBuffer(
block.timestamp,
SIZE_INT32,
LITTLE_ENDIAN,
);
const previousBlockBuffer = block.previousBlockId
? intToBuffer(block.previousBlockId, SIZE_INT64, BIG_ENDIAN)
: Buffer.alloc(SIZE_INT64);
const heightBuffer = intToBuffer(block.height, SIZE_INT32, LITTLE_ENDIAN);
const maxHeightPreviouslyForgedBuffer = intToBuffer(
block.maxHeightPreviouslyForged,
SIZE_INT32,
LITTLE_ENDIAN,
);
const maxHeightPrevotedBuffer = intToBuffer(
block.maxHeightPrevoted,
SIZE_INT32,
LITTLE_ENDIAN,
);
const numTransactionsBuffer = intToBuffer(
block.numberOfTransactions,
SIZE_INT32,
LITTLE_ENDIAN,
);
const totalAmountBuffer = intToBuffer(
block.totalAmount.toString(),
SIZE_INT64,
LITTLE_ENDIAN,
);
const totalFeeBuffer = intToBuffer(
block.totalFee.toString(),
SIZE_INT64,
LITTLE_ENDIAN,
);
const rewardBuffer = intToBuffer(
block.reward.toString(),
SIZE_INT64,
LITTLE_ENDIAN,
);
const payloadLengthBuffer = intToBuffer(
block.payloadLength,
SIZE_INT32,
LITTLE_ENDIAN,
);
const payloadHashBuffer = hexToBuffer(block.payloadHash);
const generatorPublicKeyBuffer = hexToBuffer(block.generatorPublicKey);
const blockSignatureBuffer = block.blockSignature
? hexToBuffer(block.blockSignature)
: Buffer.alloc(0);
return Buffer.concat([
blockVersionBuffer,
timestampBuffer,
previousBlockBuffer,
heightBuffer,
maxHeightPreviouslyForgedBuffer,
maxHeightPrevotedBuffer,
numTransactionsBuffer,
totalAmountBuffer,
totalFeeBuffer,
rewardBuffer,
payloadLengthBuffer,
payloadHashBuffer,
generatorPublicKeyBuffer,
blockSignatureBuffer,
]);
};
const validateSchema = ({ block }) => {
const errors = validator.validate(blockSchema, block);
if (errors.length) {
throw errors;
}
};
class BlockProcessorV2 extends BaseBlockProcessor {
constructor({
blocksModule,
bftModule,
dposModule,
storage,
logger,
constants,
exceptions,
}) {
super();
this.blocksModule = blocksModule;
this.bftModule = bftModule;
this.dposModule = dposModule;
this.logger = logger;
this.storage = storage;
this.constants = constants;
this.exceptions = exceptions;
this.init.pipe([
async ({ stateStore }) => {
// minActiveHeightsOfDelegates will be used to load 202 blocks from the storage
// That's why we need to get the delegates who were active in the last 2 rounds.
const numberOfRounds = 2;
const minActiveHeightsOfDelegates = await this.dposModule.getMinActiveHeightsOfDelegates(
numberOfRounds,
);
this.bftModule.init(stateStore, minActiveHeightsOfDelegates);
},
]);
this.deserialize.pipe([
({ block }) => this.blocksModule.deserialize(block),
]);
this.serialize.pipe([
({ block }) => this.blocksModule.serialize(block),
(_, updatedBlock) => this.bftModule.serialize(updatedBlock),
]);
this.validate.pipe([
data => this._validateVersion(data),
data => validateSchema(data),
({ block }) => {
let expectedReward = this.blocksModule.blockReward.calculateReward(
block.height,
);
if (!this.bftModule.isBFTProtocolCompliant(block)) {
expectedReward *= 0.25;
}
this.blocksModule.validateBlockHeader(
block,
getBytes(block),
expectedReward,
);
},
({ block, lastBlock }) =>
this.blocksModule.verifyInMemory(block, lastBlock),
({ block }) => this.dposModule.verifyBlockForger(block),
({ block }) => this.bftModule.validateBlock(block),
]);
this.validateDetached.pipe([
data => this._validateVersion(data),
data => validateSchema(data),
({ block }) =>
this.blocksModule.validateBlockHeader(
block,
getBytes(block),
block.reward, // Because it cannot calculate BFT compliance, it assumes that reward is correct
),
]);
this.forkStatus.pipe([
({ block, lastBlock }) => this.bftModule.forkChoice(block, lastBlock), // validate common block header
]);
this.verify.pipe([({ block }) => this.bftModule.verifyNewBlock(block)]);
this.apply.pipe([
({ block, stateStore, skipExistingCheck }) =>
this.blocksModule.verify(block, stateStore, { skipExistingCheck }),
({ block, stateStore }) => this.blocksModule.apply(block, stateStore),
({ block, tx }) => this.dposModule.apply(block, { tx }),
async ({ block, tx, stateStore }) => {
// We only need activeMinHeight value of the delegate who is forging the block.
// Since the block is always the latest,
// fetching only the latest active delegate list would be enough.
const numberOfRounds = 1;
const minActiveHeightsOfDelegates = await this.dposModule.getMinActiveHeightsOfDelegates(
numberOfRounds,
{ tx },
);
const [delegateMinHeightActive] = minActiveHeightsOfDelegates[
block.generatorPublicKey
];
const blockHeader = {
...block,
// This parameter injected to block object to avoid big refactoring
// for the moment. `delegateMinHeightActive` will be removed from the block
// object with https://github.com/LiskHQ/lisk-sdk/issues/4413
delegateMinHeightActive,
};
return this.bftModule.addNewBlock(blockHeader, stateStore);
},
]);
this.applyGenesis.pipe([
({ block, stateStore }) =>
this.blocksModule.applyGenesis(block, stateStore),
({ block, tx }) => this.dposModule.apply(block, { tx }),
]);
this.undo.pipe([
({ block, stateStore }) => this.blocksModule.undo(block, stateStore),
({ block, tx }) => this.dposModule.undo(block, { tx }),
async ({ block, tx }) => {
// minActiveHeightsOfDelegates will be used to load 202 blocks from the storage
// That's why we need to get the delegates who were active in the last 2 rounds.
const numberOfRounds = 2;
const minActiveHeightsOfDelegates = await this.dposModule.getMinActiveHeightsOfDelegates(
numberOfRounds,
{ tx },
);
this.bftModule.deleteBlocks([block], minActiveHeightsOfDelegates);
},
]);
this.create.pipe([
// Create a block with with basic block and bft properties
async data => {
const previouslyForged = await this._getPreviouslyForgedMap();
const delegatePublicKey = data.keypair.publicKey.toString('hex');
const maxHeightPreviouslyForged =
previouslyForged[delegatePublicKey] || 0;
const block = this._create({
...data,
maxHeightPreviouslyForged,
maxHeightPrevoted: this.bftModule.maxHeightPrevoted,
});
await this._saveMaxHeightPreviouslyForged(
block.generatorPublicKey,
block.height,
);
return block;
},
]);
}
// eslint-disable-next-line class-methods-use-this
get version() {
return 2;
}
_create({
transactions,
previousBlock,
keypair,
timestamp,
maxHeightPreviouslyForged,
maxHeightPrevoted,
}) {
const nextHeight = previousBlock ? previousBlock.height + 1 : 1;
const reward = this.blocksModule.blockReward.calculateReward(nextHeight);
let totalFee = new BigNum(0);
let totalAmount = new BigNum(0);
let size = 0;
const blockTransactions = [];
const transactionsBytesArray = [];
// eslint-disable-next-line no-plusplus
for (let i = 0; i < transactions.length; i++) {
const transaction = transactions[i];
const transactionBytes = transaction.getBytes(transaction);
if (size + transactionBytes.length > this.constants.maxPayloadLength) {
break;
}
size += transactionBytes.length;
totalFee = totalFee.plus(transaction.fee);
totalAmount = totalAmount.plus(transaction.asset.amount || 0);
blockTransactions.push(transaction);
transactionsBytesArray.push(transactionBytes);
}
const transactionsBuffer = Buffer.concat(transactionsBytesArray);
const payloadHash = hash(transactionsBuffer).toString('hex');
const block = {
version: this.version,
totalAmount,
totalFee,
reward,
payloadHash,
timestamp,
numberOfTransactions: blockTransactions.length,
payloadLength: size,
previousBlockId: previousBlock.id,
generatorPublicKey: keypair.publicKey.toString('hex'),
transactions: blockTransactions,
height: nextHeight,
maxHeightPreviouslyForged,
maxHeightPrevoted,
};
// Reduce reward based on BFT rules
if (!this.bftModule.isBFTProtocolCompliant(block)) {
block.reward = block.reward.times(0.25);
}
return {
...block,
blockSignature: signDataWithPrivateKey(
hash(getBytes(block)),
keypair.privateKey,
),
};
}
async _getPreviouslyForgedMap() {
const previouslyForgedStr = await this.storage.entities.ForgerInfo.getKey(
FORGER_INFO_KEY_MAX_HEIGHT_PREVIOUSLY_FORGED,
);
return previouslyForgedStr ? JSON.parse(previouslyForgedStr) : {};
}
/**
* Saving a height which delegate last forged. this needs to be saved before broadcasting
* so it needs to be outside of the DB transaction
*/
async _saveMaxHeightPreviouslyForged(delegatePublicKey, height) {
const previouslyForgedMap = await this._getPreviouslyForgedMap();
const previouslyForgedHeightByDelegate =
previouslyForgedMap[delegatePublicKey] || 0;
// previously forged height only saves maximum forged height
if (height <= previouslyForgedHeightByDelegate) {
return;
}
const updatedPreviouslyForged = {
...previouslyForgedMap,
[delegatePublicKey]: height,
};
const previouslyForgedStr = JSON.stringify(updatedPreviouslyForged);
await this.storage.entities.ForgerInfo.setKey(
FORGER_INFO_KEY_MAX_HEIGHT_PREVIOUSLY_FORGED,
previouslyForgedStr,
);
}
}
module.exports = {
BlockProcessorV2,
getBytes,
};