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 pathblock_header.spec.ts
297 lines (258 loc) · 10.1 KB
/
block_header.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
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
/*
* 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 { utils } from '@liskhq/lisk-cryptography';
import { BlockHeader } from '../../src/block_header';
import { EMPTY_BUFFER, EMPTY_HASH } from '../../src/constants';
import {
blockHeaderSchema,
blockHeaderSchemaWithId,
signingBlockHeaderSchema,
} from '../../src/schema';
const getBlockAttrs = () => ({
version: 1,
timestamp: 1009988,
height: 1009988,
previousBlockID: Buffer.from('4a462ea57a8c9f72d866c09770e5ec70cef18727', 'hex'),
stateRoot: Buffer.from('7f9d96a09a3fd17f3478eb7bef3a8bda00e1238b', 'hex'),
transactionRoot: Buffer.from('b27ca21f40d44113c2090ca8f05fb706c54e87dd', 'hex'),
assetRoot: Buffer.from('b27ca21f40d44113c2090ca8f05fb706c54e87dd', 'hex'),
eventRoot: Buffer.from('30dda4fbc395828e5a9f2f8824771e434fce4945a1e7820012440d09dd1e2b6d', 'hex'),
generatorAddress: Buffer.from('be63fb1c0426573352556f18b21efd5b6183c39c', 'hex'),
maxHeightPrevoted: 1000988,
maxHeightGenerated: 1000988,
impliesMaxPrevotes: true,
validatorsHash: utils.hash(Buffer.alloc(0)),
aggregateCommit: {
height: 0,
aggregationBits: Buffer.alloc(0),
certificateSignature: Buffer.alloc(0),
},
signature: Buffer.from('6da88e2fd4435e26e02682435f108002ccc3ddd5', 'hex'),
});
const getGenesisBlockAttrs = () => ({
version: 1,
timestamp: 1009988,
height: 1009988,
previousBlockID: utils.getRandomBytes(32),
stateRoot: Buffer.from('7f9d96a09a3fd17f3478eb7bef3a8bda00e1238b', 'hex'),
transactionRoot: EMPTY_HASH,
assetRoot: EMPTY_HASH,
eventRoot: EMPTY_HASH,
impliesMaxPrevotes: true,
generatorAddress: Buffer.alloc(20, 0),
maxHeightPrevoted: 1009988,
maxHeightGenerated: 0,
validatorsHash: utils.hash(Buffer.alloc(0)),
aggregateCommit: {
height: 0,
aggregationBits: Buffer.alloc(0),
certificateSignature: EMPTY_BUFFER,
},
signature: EMPTY_BUFFER,
});
const blockId = Buffer.from(
'926ef9e8f01294c9b1f068119002a3903f5a2820b957759402faefeb8fd6e97a',
'hex',
);
const blockHeaderBytes = Buffer.from(
'080110c4d23d18c4d23d22144a462ea57a8c9f72d866c09770e5ec70cef187272a14be63fb1c0426573352556f18b21efd5b6183c39c3214b27ca21f40d44113c2090ca8f05fb706c54e87dd3a14b27ca21f40d44113c2090ca8f05fb706c54e87dd422030dda4fbc395828e5a9f2f8824771e434fce4945a1e7820012440d09dd1e2b6d4a147f9d96a09a3fd17f3478eb7bef3a8bda00e1238b509c8c3d589c8c3d60016a20e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b8557206080012001a007a146da88e2fd4435e26e02682435f108002ccc3ddd5',
'hex',
);
const blockHeaderProps = [
'version',
'timestamp',
'height',
'previousBlockID',
'generatorAddress',
'transactionRoot',
'assetRoot',
'stateRoot',
'maxHeightPrevoted',
'maxHeightGenerated',
'validatorsHash',
'aggregateCommit',
];
describe('block_header', () => {
describe('signingBlockHeaderSchema', () => {
it('should be valid schema without signature and id', () => {
expect(signingBlockHeaderSchema).toMatchSnapshot();
expect(Object.keys(signingBlockHeaderSchema.properties)).toIncludeAllMembers([
...blockHeaderProps,
]);
expect(Object.keys(signingBlockHeaderSchema.properties)).not.toIncludeAllMembers([
'id',
'signature',
]);
});
});
describe('blockHeaderSchema', () => {
it('should be valid schema with signature but without id', () => {
expect(blockHeaderSchema).toMatchSnapshot();
expect(Object.keys(blockHeaderSchema.properties)).toIncludeAllMembers([
...blockHeaderProps,
'signature',
]);
expect(Object.keys(blockHeaderSchema.properties)).not.toIncludeAllMembers(['id']);
});
});
describe('blockHeaderSchemaWithId', () => {
it('should be valid schema with signature and id', () => {
expect(blockHeaderSchemaWithId).toMatchSnapshot();
expect(Object.keys(blockHeaderSchemaWithId.properties)).toIncludeAllMembers([
...blockHeaderProps,
'signature',
'id',
]);
});
});
describe('BlockHeader', () => {
describe('constructor', () => {
it('should initialize block header object', () => {
const data = getBlockAttrs();
const blockHeader = new BlockHeader(data);
expect(blockHeader).toBeInstanceOf(BlockHeader);
expect(blockHeader.version).toEqual(data.version);
expect(blockHeader.timestamp).toEqual(data.timestamp);
expect(blockHeader.height).toEqual(data.height);
expect(blockHeader.generatorAddress).toEqual(data.generatorAddress);
expect(blockHeader.previousBlockID).toEqual(data.previousBlockID);
expect(blockHeader.stateRoot).toEqual(data.stateRoot);
expect(blockHeader.eventRoot).toEqual(data.eventRoot);
expect(blockHeader.validatorsHash).toEqual(data.validatorsHash);
expect(blockHeader.aggregateCommit).toEqual(data.aggregateCommit);
expect(blockHeader.maxHeightPrevoted).toEqual(data.maxHeightPrevoted);
expect(blockHeader.maxHeightGenerated).toEqual(data.maxHeightGenerated);
expect(blockHeader.assetRoot).toEqual(data.assetRoot);
expect(blockHeader.transactionRoot).toEqual(data.transactionRoot);
expect(blockHeader.id).toEqual(blockId);
expect(blockHeader.signature).toEqual(data.signature);
});
});
describe('fromBytes', () => {
it('should load the block header from bytes', () => {
const blockHeader = BlockHeader.fromBytes(blockHeaderBytes);
expect(blockHeader.toObject()).toEqual({ ...getBlockAttrs(), id: blockId });
});
});
describe('validate', () => {
it('should validate the block header without error if all the properties are valid', () => {
const data = getBlockAttrs();
data.signature = Buffer.from(
'41f7d923c8957664923b49d7a893153476bc60e0392702cf111a26a92d74279a00c41f1999504c9faeee7b3d05393ca04d61a0768c3ae4d324f3097ab0b52201',
'hex',
);
const blockHeader = new BlockHeader(data);
expect(blockHeader.validate()).toBeUndefined();
});
it('should throw error if previous block id is not set', () => {
const data = getBlockAttrs();
const blockHeader = new BlockHeader({ ...data, previousBlockID: Buffer.alloc(0) });
expect(() => blockHeader.validate()).toThrow('Previous block id must not be empty.');
});
it('should throw error if signature length is not correct', () => {
const data = getBlockAttrs();
const blockHeader = new BlockHeader(data);
expect(() => blockHeader.validate()).toThrow('Signature length must be 64 bytes.');
});
});
describe('signature', () => {
it('should throw error if block header not signed', () => {
const { signature, ...rest } = getBlockAttrs();
const blockHeader = new BlockHeader(rest);
expect(() => blockHeader.signature).toThrow('Block header is not signed.');
});
});
describe('id', () => {
it('should throw error if block header not signed', () => {
const { signature, ...rest } = getBlockAttrs();
const blockHeader = new BlockHeader(rest);
expect(() => blockHeader.id).toThrow('Can not generate the id for unsigned block header.');
});
it('should not throw error if block header is signed', () => {
const data = getBlockAttrs();
const blockHeader = new BlockHeader(data);
expect(blockHeader.id).toEqual(blockId);
});
});
describe('validateGenesis', () => {
it('should throw error if previousBlockID is not 32 bytes', () => {
const block = getGenesisBlockAttrs();
const blockHeader = new BlockHeader({
...block,
previousBlockID: utils.getRandomBytes(31),
});
expect(() => blockHeader.validateGenesis()).toThrow(
'Genesis block header previousBlockID must be 32 bytes',
);
});
it('should throw error if transactionRoot is not empty hash', () => {
const block = getGenesisBlockAttrs();
const blockHeader = new BlockHeader({
...block,
transactionRoot: utils.getRandomBytes(32),
});
expect(() => blockHeader.validateGenesis()).toThrow(
'Genesis block header transaction root must be empty hash',
);
});
it('should throw error if maxHeightPrevoted is not equal to header.height', () => {
const block = getGenesisBlockAttrs();
const blockHeader = new BlockHeader({ ...block, maxHeightPrevoted: 10 });
expect(() => blockHeader.validateGenesis()).toThrow(
'Genesis block header maxHeightPrevoted must equal height',
);
});
it('should throw error if aggregateCommit.height is not equal to 0', () => {
const block = getGenesisBlockAttrs();
const blockHeader = new BlockHeader({
...block,
aggregateCommit: { ...block.aggregateCommit, height: 10 },
});
expect(() => blockHeader.validateGenesis()).toThrow(
'Genesis block header aggregateCommit.height must equal 0',
);
});
it('should throw error if aggregateCommit.certificateSignature is not empty buffer', () => {
const block = getGenesisBlockAttrs();
const blockHeader = new BlockHeader({
...block,
aggregateCommit: {
...block.aggregateCommit,
certificateSignature: utils.getRandomBytes(32),
},
});
expect(() => blockHeader.validateGenesis()).toThrow(
'Genesis block header aggregateCommit.certificateSignature must be empty bytes',
);
});
it('should throw error if aggregateCommit.aggregationBits is not empty buffer', () => {
const block = getGenesisBlockAttrs();
const blockHeader = new BlockHeader({
...block,
aggregateCommit: { ...block.aggregateCommit, aggregationBits: utils.getRandomBytes(32) },
});
expect(() => blockHeader.validateGenesis()).toThrow(
'Genesis block header aggregateCommit.aggregationBits must be empty bytes',
);
});
it('should throw error if signature is not empty buffer', () => {
const block = getGenesisBlockAttrs();
const blockHeader = new BlockHeader({ ...block, signature: utils.getRandomBytes(32) });
expect(() => blockHeader.validateGenesis()).toThrow(
'Genesis block header signature must be empty bytes',
);
});
});
});
});