This repository was archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathcommit_pool.ts
562 lines (496 loc) · 18.5 KB
/
commit_pool.ts
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
/*
* Copyright © 2021 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.
*/
import { BlockHeader, Chain, StateStore } from '@liskhq/lisk-chain';
import { dataStructures, objects } from '@liskhq/lisk-utils';
import { bls } from '@liskhq/lisk-cryptography';
import { Database } from '@liskhq/lisk-db';
import { codec } from '@liskhq/lisk-codec';
import { EMPTY_BUFFER, NETWORK_EVENT_COMMIT_MESSAGES, COMMIT_RANGE_STORED } from './constants';
import { BFTParameterNotFoundError } from '../../bft/errors';
import { PkSigPair, AggregateCommit } from '../types';
import { Certificate, CommitPoolConfig, SingleCommit, ValidatorInfo } from './types';
import {
computeUnsignedCertificateFromBlockHeader,
verifyAggregateCertificateSignature,
signCertificate,
verifySingleCertificateSignature,
} from './utils';
import { Network } from '../../network';
import { singleCommitSchema, singleCommitsNetworkPacketSchema } from './schema';
import { CommitList, COMMIT_SORT } from './commit_list';
import { BFTMethod } from '../../bft';
import { defaultMetrics } from '../../metrics/metrics';
export class CommitPool {
private readonly _nonGossipedCommits: CommitList;
private readonly _nonGossipedCommitsLocal: CommitList;
private readonly _gossipedCommits: CommitList;
private readonly _blockTime: number;
private readonly _bftMethod: BFTMethod;
private readonly _minCertifyHeight: number;
private readonly _chain: Chain;
private readonly _network: Network;
private readonly _db: Database;
private _jobIntervalID!: NodeJS.Timeout;
private readonly _metrics = {
singleCommits: defaultMetrics.gauge('commitPool_numSingleCommits'),
nonGossippedCommits: defaultMetrics.gauge('commitPool_numNonGossippedCommits'),
nonGossippedCommitsLocal: defaultMetrics.gauge('commitPool_numNonGossippedCommitsLocal'),
gossippedCommits: defaultMetrics.gauge('commitPool_numGossippedCommits'),
job: defaultMetrics.histogram('commitPool_job', [0.01, 0.05, 0.1, 0.2, 0.5, 1, 5]),
};
public constructor(config: CommitPoolConfig) {
this._blockTime = config.blockTime;
this._bftMethod = config.bftMethod;
this._minCertifyHeight = config.minCertifyHeight;
this._chain = config.chain;
this._network = config.network;
this._db = config.db;
this._nonGossipedCommits = new CommitList();
this._nonGossipedCommitsLocal = new CommitList();
this._gossipedCommits = new CommitList();
}
public start() {
// Run job every BLOCK_TIME/2 interval
// eslint-disable-next-line @typescript-eslint/no-misused-promises
this._jobIntervalID = setInterval(async () => {
const stateStore = new StateStore(this._db);
const endTimer = this._metrics.job.startTimer();
await this._job(stateStore);
endTimer();
}, (this._blockTime / 2) * 1000);
}
public stop() {
clearInterval(this._jobIntervalID);
}
public addCommit(commit: SingleCommit, local = false): void {
if (!this._nonGossipedCommits.exists(commit) && !this._nonGossipedCommitsLocal.exists(commit)) {
if (local) {
this._nonGossipedCommitsLocal.add(commit);
} else {
this._nonGossipedCommits.add(commit);
}
this._metrics.singleCommits.inc();
}
}
public async validateCommit(methodContext: StateStore, commit: SingleCommit): Promise<boolean> {
// Validation step 1
const existsInNonGossiped = this._nonGossipedCommits.exists(commit);
const existsInNonGossipedLocal = this._nonGossipedCommitsLocal.exists(commit);
const existsInGossiped = this._gossipedCommits.exists(commit);
const doesCommitExist = existsInGossiped || existsInNonGossiped || existsInNonGossipedLocal;
if (doesCommitExist) {
return false;
}
// Validation Step 2
const maxRemovalHeight = await this._getMaxRemovalHeight();
if (commit.height <= maxRemovalHeight) {
return false;
}
// Validation Step 3
const currentHeight = this._chain.lastBlock.header.height;
const { maxHeightPrecommitted } = await this._bftMethod.getBFTHeights(methodContext);
const isCommitInRange =
commit.height >= maxHeightPrecommitted - COMMIT_RANGE_STORED &&
commit.height <= currentHeight;
const doesBFTParamExistForNextHeight = await this._bftMethod.existBFTParameters(
methodContext,
commit.height + 1,
);
if (!isCommitInRange && !doesBFTParamExistForNextHeight) {
return false;
}
// Validation step 4
const blockHeaderAtCommitHeight = await this._chain.dataAccess.getBlockHeaderByHeight(
commit.height,
);
if (!blockHeaderAtCommitHeight.id.equals(commit.blockID)) {
return false;
}
// Validation Step 5
const { validators } = await this._bftMethod.getBFTParametersActiveValidators(
methodContext,
commit.height,
);
const validator = validators.find(v => v.address.equals(commit.validatorAddress));
if (!validator) {
throw new Error('Commit validator was not active for its height.');
}
// Validation Step 6
const unsignedCertificate =
computeUnsignedCertificateFromBlockHeader(blockHeaderAtCommitHeight);
const { chainID } = this._chain;
const isSingleCertificateVerified = verifySingleCertificateSignature(
validator.blsKey,
commit.certificateSignature,
chainID,
unsignedCertificate,
);
if (!isSingleCertificateVerified) {
throw new Error('Certificate signature is not valid.');
}
return true;
}
public getCommitsByHeight(height: number): SingleCommit[] {
const nonGossipedCommits = this._nonGossipedCommits.getByHeight(height);
const nonGossipedCommitsLocal = this._nonGossipedCommitsLocal.getByHeight(height);
const gossipedCommits = this._gossipedCommits.getByHeight(height);
return [...nonGossipedCommits, ...nonGossipedCommitsLocal, ...gossipedCommits];
}
public createSingleCommit(
blockHeader: BlockHeader,
validatorInfo: ValidatorInfo,
chainID: Buffer,
): SingleCommit {
return {
blockID: blockHeader.id,
height: blockHeader.height,
validatorAddress: validatorInfo.address,
certificateSignature: signCertificate(
validatorInfo.blsSecretKey,
chainID,
computeUnsignedCertificateFromBlockHeader(blockHeader),
),
};
}
public async verifyAggregateCommit(
stateStore: StateStore,
aggregateCommit: AggregateCommit,
): Promise<boolean> {
const { maxHeightCertified, maxHeightPrecommitted } = await this._bftMethod.getBFTHeights(
stateStore,
);
if (
aggregateCommit.aggregationBits.equals(EMPTY_BUFFER) &&
aggregateCommit.certificateSignature.equals(EMPTY_BUFFER) &&
aggregateCommit.height === maxHeightCertified
) {
return true;
}
if (
aggregateCommit.aggregationBits.equals(EMPTY_BUFFER) ||
aggregateCommit.certificateSignature.equals(EMPTY_BUFFER)
) {
return false;
}
if (aggregateCommit.height <= maxHeightCertified) {
return false;
}
if (aggregateCommit.height > maxHeightPrecommitted) {
return false;
}
// The heights of aggregate commits must be greater than or equal to MIN_CERTIFY_HEIGHT.
if (aggregateCommit.height < this._minCertifyHeight) {
return false;
}
try {
let heightNextBFTParameters = await this._bftMethod.getNextHeightBFTParameters(
stateStore,
maxHeightCertified + 1,
);
heightNextBFTParameters = Math.max(heightNextBFTParameters, this._minCertifyHeight + 1);
if (aggregateCommit.height > heightNextBFTParameters - 1) {
return false;
}
} catch (err) {
if (!(err instanceof BFTParameterNotFoundError)) {
throw err;
}
}
const blockHeader = await this._chain.dataAccess.getBlockHeaderByHeight(aggregateCommit.height);
const certificate: Certificate = {
...computeUnsignedCertificateFromBlockHeader(blockHeader),
aggregationBits: aggregateCommit.aggregationBits,
signature: aggregateCommit.certificateSignature,
};
const { validators: activeValidators, certificateThreshold } =
await this._bftMethod.getBFTParametersActiveValidators(stateStore, aggregateCommit.height);
// Filter out all the standby validators with bftWeight === 0
const activeValidators = validators.filter(v => v.bftWeight > BigInt(0));
return verifyAggregateCertificateSignature(
activeValidators,
certificateThreshold,
this._chain.chainID,
certificate,
);
}
public async getAggregateCommit(methodContext: StateStore): Promise<AggregateCommit> {
return this._selectAggregateCommit(methodContext);
}
public async aggregateSingleCommits(
methodContext: StateStore,
singleCommits: SingleCommit[],
): Promise<AggregateCommit> {
if (singleCommits.length === 0) {
throw new Error('No single commit found');
}
const { height } = singleCommits[0];
// assuming this list of validators includes all validators corresponding to each singleCommit.validatorAddress
const { validators } = await this._bftMethod.getBFTParametersActiveValidators(
methodContext,
height,
);
const addressToBlsKey: dataStructures.BufferMap<Buffer> = new dataStructures.BufferMap();
const validatorKeys: Buffer[] = [];
// Filter out all the standby validators with bftWeight === 0
const addressesWithBFTWeightZero = validators
.filter(v => v.bftWeight === BigInt(0))
.map(v => v.address);
for (const validator of validators.filter(v => v.bftWeight > BigInt(0))) {
addressToBlsKey.set(validator.address, validator.blsKey);
validatorKeys.push(validator.blsKey);
}
const pubKeySignaturePairs: PkSigPair[] = [];
for (const commit of singleCommits) {
// Skip any standby validator
if (objects.bufferArrayIncludes(addressesWithBFTWeightZero, commit.validatorAddress)) {
continue;
}
const publicKey = addressToBlsKey.get(commit.validatorAddress);
if (!publicKey) {
throw new Error(
`No bls public key entry found for validatorAddress ${commit.validatorAddress.toString(
'hex',
)}`,
);
}
pubKeySignaturePairs.push({ publicKey, signature: commit.certificateSignature });
}
validatorKeys.sort((blsKeyA, blsKeyB) => blsKeyA.compare(blsKeyB));
const { aggregationBits, signature: aggregateSignature } = bls.createAggSig(
validatorKeys,
pubKeySignaturePairs,
);
return {
height,
aggregationBits,
certificateSignature: aggregateSignature,
};
}
private async _selectAggregateCommit(methodContext: StateStore): Promise<AggregateCommit> {
const { maxHeightCertified, maxHeightPrecommitted } = await this._bftMethod.getBFTHeights(
methodContext,
);
let heightNextBFTParameters: number;
let nextHeight: number;
try {
heightNextBFTParameters = await this._bftMethod.getNextHeightBFTParameters(
methodContext,
maxHeightCertified + 1,
);
heightNextBFTParameters = Math.max(heightNextBFTParameters, this._minCertifyHeight + 1);
nextHeight = Math.min(heightNextBFTParameters - 1, maxHeightPrecommitted);
} catch (err) {
if (!(err instanceof BFTParameterNotFoundError)) {
throw err;
}
nextHeight = maxHeightPrecommitted;
}
const certifyUptoHeight = Math.max(maxHeightCertified, this._minCertifyHeight - 1);
while (nextHeight > certifyUptoHeight) {
const singleCommits = [
...this._nonGossipedCommits.getByHeight(nextHeight),
...this._nonGossipedCommitsLocal.getByHeight(nextHeight),
...this._gossipedCommits.getByHeight(nextHeight),
];
let aggregateBFTWeight = BigInt(0);
// Assume BFT parameters exist for next height
const { validators: bftParamValidators, certificateThreshold } =
await this._bftMethod.getBFTParametersActiveValidators(methodContext, nextHeight);
const activeValidatorAddresses = bftParamValidators.map(v => v.address);
// Filter out any single commits from standby delegates
const singleCommitsByActiveValidators = singleCommits.filter(commit =>
objects.bufferArrayIncludes(activeValidatorAddresses, commit.validatorAddress),
);
const nextValidators = singleCommitsByActiveValidators.map(commit => commit.validatorAddress);
const filteredSingleCommits = singleCommits.filter(commit => {
const foundValidator = bftParamValidators.find(v =>
v.address.equals(commit.validatorAddress),
);
if (foundValidator && foundValidator.bftWeight === BigInt(0)) {
return false;
}
return true;
});
const nextValidators = filteredSingleCommits.map(commit => commit.validatorAddress);
for (const matchingAddress of nextValidators) {
const bftParamsValidatorInfo = bftParamValidators.find(bftParamValidator =>
bftParamValidator.address.equals(matchingAddress),
);
if (!bftParamsValidatorInfo) {
throw new Error('Validator address not found in commit pool');
}
// Skip validators with BFT Weight zero when someone is running node with standby validators
if (bftParamsValidatorInfo.bftWeight === BigInt(0)) {
continue;
}
aggregateBFTWeight += bftParamsValidatorInfo.bftWeight;
}
if (aggregateBFTWeight >= certificateThreshold) {
return this.aggregateSingleCommits(methodContext, singleCommitsByActiveValidators);
}
nextHeight -= 1;
}
return {
height: maxHeightCertified,
aggregationBits: EMPTY_BUFFER,
certificateSignature: EMPTY_BUFFER,
};
}
private async _job(methodContext: StateStore): Promise<void> {
const removalHeight = await this._getMaxRemovalHeight();
const currentHeight = this._chain.lastBlock.header.height;
const { maxHeightPrecommitted } = await this._bftMethod.getBFTHeights(methodContext);
// Clean up nonGossipedCommits
const deletedNonGossipedHeights = await this._getDeleteHeights(
methodContext,
this._nonGossipedCommits,
removalHeight,
maxHeightPrecommitted,
currentHeight,
);
for (const height of deletedNonGossipedHeights) {
this._nonGossipedCommits.deleteByHeight(height);
}
this._metrics.nonGossippedCommits.set(this._nonGossipedCommits.size());
// Clean up nonGossipedCommitsLocal
const deletedNonGossipedHeightsLocal = await this._getDeleteHeights(
methodContext,
this._nonGossipedCommitsLocal,
removalHeight,
maxHeightPrecommitted,
currentHeight,
);
for (const height of deletedNonGossipedHeightsLocal) {
this._nonGossipedCommitsLocal.deleteByHeight(height);
}
this._metrics.nonGossippedCommitsLocal.set(this._nonGossipedCommitsLocal.size());
// Clean up gossipedCommits
const deletedGossipedHeights = await this._getDeleteHeights(
methodContext,
this._gossipedCommits,
removalHeight,
maxHeightPrecommitted,
currentHeight,
);
for (const height of deletedGossipedHeights) {
this._gossipedCommits.deleteByHeight(height);
}
this._metrics.gossippedCommits.set(this._gossipedCommits.size());
// 2. Select commits to gossip
const nextHeight = this._chain.lastBlock.header.height + 1;
const { validators } = await this._bftMethod.getBFTParametersActiveValidators(
methodContext,
nextHeight,
);
const maxSelectedCommitsLength = 2 * validators.length;
// Get a list of commits sorted by ascending order of height
const allCommits = this._getAllCommits();
this._metrics.singleCommits.set(allCommits.length);
const selectedCommits = [];
for (const commit of allCommits) {
if (selectedCommits.length >= maxSelectedCommitsLength) {
break;
}
// 2.1 Choosing the commit with smaller height first
if (commit.height < maxHeightPrecommitted - COMMIT_RANGE_STORED) {
selectedCommits.push(commit);
}
}
// 2.2 Select newly created commits by generator
// Non gossiped commits with descending order of height by generator
const sortedNonGossipedCommitsLocal = this._nonGossipedCommitsLocal.getAll(COMMIT_SORT.DSC);
for (const commit of sortedNonGossipedCommitsLocal) {
if (selectedCommits.length >= maxSelectedCommitsLength) {
break;
}
selectedCommits.push(commit);
}
// 2.3 Select newly received commits by others
// Non gossiped commits with descending order of height
const sortedNonGossipedCommits = this._nonGossipedCommits.getAll(COMMIT_SORT.DSC);
for (const commit of sortedNonGossipedCommits) {
if (selectedCommits.length >= maxSelectedCommitsLength) {
break;
}
selectedCommits.push(commit);
}
const encodedCommitArray = selectedCommits.map(commit =>
codec.encode(singleCommitSchema, commit),
);
// 3. Gossip an array of up to 2*numActiveValidators commit messages to 16 randomly chosen connected peers with at least 8 of them being outgoing peers (same parameters as block propagation)
this._network.send({
event: NETWORK_EVENT_COMMIT_MESSAGES,
data: codec.encode(singleCommitsNetworkPacketSchema, { commits: encodedCommitArray }),
});
// 4. Move any gossiped commit message included in nonGossipedCommits, nonGossipedCommitsLocal to gossipedCommits.
for (const commit of selectedCommits) {
if (!this._gossipedCommits.exists(commit)) {
this._gossipedCommits.add(commit);
}
this._nonGossipedCommits.deleteSingle(commit);
this._nonGossipedCommitsLocal.deleteSingle(commit);
}
}
private async _getDeleteHeights(
methodContext: StateStore,
commitMap: CommitList,
removalHeight: number,
maxHeightPrecommitted: number,
currentHeight: number,
): Promise<number[]> {
const deleteHeights = [];
for (const height of commitMap.getHeights()) {
// 1. Remove any single commit message m from nonGossipedCommits
if (height <= removalHeight) {
deleteHeights.push(height);
continue;
}
// 2. For every commit message m in nonGossipedCommits or gossipedCommits one of the following two conditions has to hold, otherwise it is discarded
const nonGossipedCommits = commitMap.getByHeight(height);
for (const singleCommit of nonGossipedCommits) {
// Condition #1
if (
maxHeightPrecommitted - COMMIT_RANGE_STORED <= singleCommit.height &&
singleCommit.height <= currentHeight
) {
continue;
}
// Condition #2
const changeOfBFTParams = await this._bftMethod.existBFTParameters(
methodContext,
singleCommit.height + 1,
);
if (changeOfBFTParams) {
continue;
}
deleteHeights.push(height);
}
}
return deleteHeights;
}
private async _getMaxRemovalHeight() {
const blockHeader = await this._chain.dataAccess.getBlockHeaderByHeight(
this._chain.finalizedHeight,
);
return blockHeader.aggregateCommit.height;
}
private _getAllCommits(): SingleCommit[] {
// Flattened list of all the single commits from both gossiped and non gossiped list sorted by ascending order of height
return [
...this._nonGossipedCommits.getAll(),
...this._nonGossipedCommitsLocal.getAll(),
...this._gossipedCommits.getAll(),
].sort((a, b) => a.height - b.height);
}
}