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 458
/
Copy pathbft_processing.spec.ts
188 lines (178 loc) · 6.73 KB
/
bft_processing.spec.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
/*
* 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.
*/
/* eslint-disable no-loop-func */
import { BlockHeader, StateStore } from '@liskhq/lisk-chain';
import { utils, address } from '@liskhq/lisk-cryptography';
import { InMemoryDatabase } from '@liskhq/lisk-db';
import { BFTModule } from '../../../../src/engine/bft';
import {
EMPTY_KEY,
MODULE_STORE_PREFIX_BFT,
STORE_PREFIX_BFT_PARAMETERS,
STORE_PREFIX_BFT_VOTES,
} from '../../../../src/engine/bft/constants';
import { bftParametersSchema, BFTVotes, bftVotesSchema } from '../../../../src/engine/bft/schemas';
import * as scenario4ValidatorsMissedSlots from './bft_processing/4_validators_missed_slots.json';
import * as scenario4ValidatorsSimple from './bft_processing/4_validators_simple.json';
import * as scenario5ValidatorsSwitchedCompletely from './bft_processing/5_validators_switched_completely.json';
import * as scenario7ValidatorsPartialSwitch from './bft_processing/7_validators_partial_switch.json';
import * as scenario11ValidatorsPartialSwitch from './bft_processing/11_validators_partial_switch.json';
import { Validator } from '../../../../src/abi';
describe('BFT processing', () => {
const bftScenarios = [
scenario4ValidatorsMissedSlots,
scenario4ValidatorsSimple,
scenario5ValidatorsSwitchedCompletely,
scenario7ValidatorsPartialSwitch,
scenario11ValidatorsPartialSwitch,
];
const blockTime = 10;
for (const scenario of bftScenarios) {
// eslint-disable-next-line no-loop-func
describe(`when running scenario "${scenario.handler}"`, () => {
let bftModule: BFTModule;
let db: InMemoryDatabase;
let stateStore: StateStore;
beforeAll(async () => {
bftModule = new BFTModule();
await bftModule.init(scenario.config.activeValidators, blockTime);
db = new InMemoryDatabase();
stateStore = new StateStore(db);
const paramsStore = stateStore.getStore(
MODULE_STORE_PREFIX_BFT,
STORE_PREFIX_BFT_PARAMETERS,
);
const threshold = Math.floor((scenario.config.activeValidators * 2) / 3) + 1;
const validators: (Validator & { minHeightActive: number })[] = [];
for (const testCase of scenario.testCases) {
const generatorAddress = address.getAddressFromPublicKey(
Buffer.from(testCase.input.blockHeader.generatorPublicKey, 'hex'),
);
if (validators.find(v => v.address.equals(generatorAddress)) === undefined) {
validators.push({
address: generatorAddress,
minHeightActive: testCase.input.blockHeader.validatorMinHeightActive,
bftWeight: BigInt(1),
generatorKey: Buffer.from(testCase.input.blockHeader.generatorPublicKey, 'hex'),
blsKey: utils.getRandomBytes(42),
});
}
}
await paramsStore.setWithSchema(
utils.intToBuffer(1, 4),
{
prevoteThreshold: BigInt(threshold),
precommitThreshold: BigInt(threshold),
certificateThreshold: BigInt(threshold),
validators,
validatorsHash: utils.getRandomBytes(32),
},
bftParametersSchema,
);
const votesStore = stateStore.getStore(MODULE_STORE_PREFIX_BFT, STORE_PREFIX_BFT_VOTES);
await votesStore.setWithSchema(
EMPTY_KEY,
{
maxHeightPrevoted: 0,
maxHeightPrecommitted: 0,
maxHeightCertified: 0,
blockBFTInfos: [],
activeValidatorsVoteInfo: validators.map(v => ({
address: v.address,
minActiveHeight: v.minHeightActive,
largestHeightPrecommit: 0,
})),
},
bftVotesSchema,
);
});
for (const testCase of scenario.testCases) {
it(`should have accurate information when ${testCase.input.validatorName} forge block at height = ${testCase.input.blockHeader.height}`, async () => {
// Arrange
const generatorAddress = address.getAddressFromPublicKey(
Buffer.from(testCase.input.blockHeader.generatorPublicKey, 'hex'),
);
const header = new BlockHeader({
version: 2,
impliesMaxPrevotes: false,
aggregateCommit: {
height: 0,
aggregationBits: Buffer.alloc(0),
certificateSignature: Buffer.alloc(0),
},
generatorAddress,
height: testCase.input.blockHeader.height,
maxHeightGenerated: testCase.input.blockHeader.maxHeightPreviouslyForged,
maxHeightPrevoted: testCase.input.blockHeader.maxHeightPrevoted,
previousBlockID: utils.getRandomBytes(32),
timestamp: 0,
});
// Update minActiveHeight which is written in input block header
const beforeVotesStore = stateStore.getStore(
MODULE_STORE_PREFIX_BFT,
STORE_PREFIX_BFT_VOTES,
);
const beforeVotes = await beforeVotesStore.getWithSchema<BFTVotes>(
EMPTY_KEY,
bftVotesSchema,
);
const validators = beforeVotes.activeValidatorsVoteInfo.map(v => {
if (v.address.equals(generatorAddress)) {
return {
...v,
minActiveHeight: testCase.input.blockHeader.validatorMinHeightActive,
};
}
return v;
});
// Act
await beforeVotesStore.setWithSchema(
EMPTY_KEY,
{
...beforeVotes,
activeValidatorsVoteInfo: validators,
},
bftVotesSchema,
);
await bftModule.beforeTransactionsExecute(stateStore, header, 0);
const votesStore = stateStore.getStore(MODULE_STORE_PREFIX_BFT, STORE_PREFIX_BFT_VOTES);
const result = await votesStore.getWithSchema<BFTVotes>(EMPTY_KEY, bftVotesSchema);
// Assert
expect(result.maxHeightPrevoted).toEqual(testCase.output.preVotedConfirmedHeight);
expect(result.maxHeightPrecommitted).toEqual(testCase.output.finalizedHeight);
const minHeight =
testCase.input.blockHeader.height - bftModule['_maxLengthBlockBFTInfos'];
for (const [heightStr, val] of Object.entries(testCase.output.preVotes)) {
const height = Number(heightStr);
const bftInfo = result.blockBFTInfos.find(b => b.height === height);
if (height > minHeight) {
expect(bftInfo?.prevoteWeight).toEqual(BigInt(val));
} else {
expect(bftInfo?.prevoteWeight).toBeUndefined();
}
}
for (const [heightStr, val] of Object.entries(testCase.output.preCommits)) {
const height = Number(heightStr);
const bftInfo = result.blockBFTInfos.find(b => b.height === height);
if (height > minHeight) {
expect(bftInfo?.precommitWeight).toEqual(BigInt(val));
} else {
expect(bftInfo?.precommitWeight).toBeUndefined();
}
}
});
}
});
}
});