@@ -100,11 +100,7 @@ describe('CommitPool', () => {
100
100
} ) ;
101
101
} ) ;
102
102
103
- describe ( 'constructor' , ( ) => {
104
- it . todo ( '' ) ;
105
- } ) ;
106
-
107
- describe ( 'job' , ( ) => {
103
+ describe ( '_job' , ( ) => {
108
104
const dbMock = {
109
105
get : jest . fn ( ) ,
110
106
put : jest . fn ( ) ,
@@ -158,9 +154,7 @@ describe('CommitPool', () => {
158
154
159
155
gossipedCommits . forEach ( commit => commitPool [ '_gossipedCommits' ] . add ( commit ) ) ;
160
156
commitPool [ '_gossipedCommits' ] . add ( staleGossipedCommit ) ;
161
- // commitPool['_gossipedCommits'].set(staleGossipedCommit.height, [staleGossipedCommit]);
162
157
nonGossipedCommits . forEach ( commit => commitPool [ '_nonGossipedCommits' ] . add ( commit ) ) ;
163
- // commitPool['_nonGossipedCommits'].set(height, nonGossipedCommits);
164
158
commitPool [ '_nonGossipedCommits' ] . add ( staleNonGossipedCommit ) ;
165
159
( commitPool [ '_chain' ] as any ) . finalizedHeight = maxHeightCertified ;
166
160
@@ -204,7 +198,7 @@ describe('CommitPool', () => {
204
198
expect ( commitPool [ '_gossipedCommits' ] . exists ( staleGossipedCommit ) ) . toBeFalse ( ) ;
205
199
} ) ;
206
200
207
- it ( 'should clean all the commits from nonGossipedCommit that does not have bftParams change and height is in the future' , async ( ) => {
201
+ it ( 'should clean all the commits from nonGossipedCommit that does not have bftParams change and in the future height ' , async ( ) => {
208
202
// Update current height so that commits will always be in the future
209
203
chain . lastBlock = { header : { height : 1019 } } ;
210
204
// it should not be deleted by the height
@@ -241,7 +235,93 @@ describe('CommitPool', () => {
241
235
expect ( commitPool [ '_gossipedCommits' ] . getByHeight ( 1070 ) ) . toBeArrayOfSize ( 0 ) ;
242
236
} ) ;
243
237
244
- it ( 'should select non gossiped commits that are created by the generator of the engine' , async ( ) => {
238
+ it ( `should not clean commits for the heights ${ maxHeightPrecommitted } - ${ COMMIT_RANGE_STORED } until currentHeight and bftParams does not exist for height` , async ( ) => {
239
+ const testHeight = maxHeightPrecommitted - COMMIT_RANGE_STORED ;
240
+ // // Update current height so that commits will always be in the future
241
+ chain . lastBlock = { header : { height : maxHeightPrecommitted + 19 } } ;
242
+
243
+ // it should not be deleted by the height
244
+ commitPool [ '_nonGossipedCommits' ] . add ( {
245
+ blockID : utils . getRandomBytes ( 32 ) ,
246
+ certificateSignature : utils . getRandomBytes ( 96 ) ,
247
+ height : testHeight ,
248
+ validatorAddress : utils . getRandomBytes ( 20 ) ,
249
+ } ) ;
250
+ commitPool [ '_gossipedCommits' ] . add ( {
251
+ blockID : utils . getRandomBytes ( 32 ) ,
252
+ certificateSignature : utils . getRandomBytes ( 96 ) ,
253
+ height : testHeight ,
254
+ validatorAddress : utils . getRandomBytes ( 20 ) ,
255
+ } ) ;
256
+ // Assert
257
+ expect ( commitPool [ '_nonGossipedCommits' ] . getAll ( ) ) . toHaveLength ( 7 ) ;
258
+ // Arrange
259
+ const bftParamsMock = jest . fn ( ) ;
260
+ commitPool [ '_bftMethod' ] . existBFTParameters = bftParamsMock ;
261
+ const context = new StateStore ( new InMemoryDatabase ( ) ) ;
262
+ commitPool [ '_bftMethod' ] . getBFTHeights = jest
263
+ . fn ( )
264
+ . mockResolvedValue ( { maxHeightPrecommitted } ) ;
265
+ when ( bftParamsMock ) . calledWith ( context , 1071 ) . mockResolvedValue ( false ) ;
266
+ when ( bftParamsMock ) . calledWith ( context , maxHeightCertified ) . mockResolvedValue ( true ) ;
267
+ when ( bftParamsMock )
268
+ . calledWith ( context , height + 1 )
269
+ . mockResolvedValue ( true ) ;
270
+ // Act
271
+ await commitPool [ '_job' ] ( context ) ;
272
+ // Assert
273
+ // nonGossiped commits are moved to gossiped commits after posting to network
274
+ expect ( commitPool [ '_nonGossipedCommits' ] . getAll ( ) ) . toHaveLength ( 0 ) ;
275
+ expect ( commitPool [ '_gossipedCommits' ] . getAll ( ) ) . toHaveLength ( 10 ) ;
276
+ } ) ;
277
+
278
+ it ( `should not clean commits for the heights lower than ${ maxHeightPrecommitted } - ${ COMMIT_RANGE_STORED } until currentHeight and bftParams does not exist for height` , async ( ) => {
279
+ const testHeight = maxHeightPrecommitted - COMMIT_RANGE_STORED - 1 ;
280
+ // // Update current height so that commits will always be in the future
281
+ chain . lastBlock = { header : { height : maxHeightPrecommitted + 19 } } ;
282
+
283
+ // it should not be deleted by the height
284
+ commitPool [ '_nonGossipedCommits' ] . add ( {
285
+ blockID : utils . getRandomBytes ( 32 ) ,
286
+ certificateSignature : utils . getRandomBytes ( 96 ) ,
287
+ height : testHeight ,
288
+ validatorAddress : utils . getRandomBytes ( 20 ) ,
289
+ } ) ;
290
+ commitPool [ '_nonGossipedCommits' ] . add ( {
291
+ blockID : utils . getRandomBytes ( 32 ) ,
292
+ certificateSignature : utils . getRandomBytes ( 96 ) ,
293
+ height : maxHeightPrecommitted - 1 ,
294
+ validatorAddress : utils . getRandomBytes ( 20 ) ,
295
+ } ) ;
296
+ commitPool [ '_gossipedCommits' ] . add ( {
297
+ blockID : utils . getRandomBytes ( 32 ) ,
298
+ certificateSignature : utils . getRandomBytes ( 96 ) ,
299
+ height : testHeight ,
300
+ validatorAddress : utils . getRandomBytes ( 20 ) ,
301
+ } ) ;
302
+ // Assert
303
+ expect ( commitPool [ '_nonGossipedCommits' ] . getAll ( ) ) . toHaveLength ( 8 ) ;
304
+ // Arrange
305
+ const bftParamsMock = jest . fn ( ) ;
306
+ commitPool [ '_bftMethod' ] . existBFTParameters = bftParamsMock ;
307
+ const context = new StateStore ( new InMemoryDatabase ( ) ) ;
308
+ commitPool [ '_bftMethod' ] . getBFTHeights = jest
309
+ . fn ( )
310
+ . mockResolvedValue ( { maxHeightPrecommitted } ) ;
311
+ when ( bftParamsMock ) . calledWith ( context , 1071 ) . mockResolvedValue ( false ) ;
312
+ when ( bftParamsMock ) . calledWith ( context , maxHeightCertified ) . mockResolvedValue ( true ) ;
313
+ when ( bftParamsMock )
314
+ . calledWith ( context , height + 1 )
315
+ . mockResolvedValue ( false ) ;
316
+ // Act
317
+ await commitPool [ '_job' ] ( context ) ;
318
+ // Assert
319
+ // nonGossiped commits at maxHeightPrecommitted - 1 is not deleted and broadcasted and moved to gossiped commits
320
+ expect ( commitPool [ '_nonGossipedCommits' ] . getAll ( ) ) . toHaveLength ( 0 ) ;
321
+ expect ( commitPool [ '_gossipedCommits' ] . getAll ( ) ) . toHaveLength ( 1 ) ;
322
+ } ) ;
323
+
324
+ it ( 'should select non gossiped local commits' , async ( ) => {
245
325
// Arrange
246
326
const generatorAddress = utils . getRandomBytes ( 20 ) ;
247
327
commitPool . addCommit (
@@ -968,7 +1048,7 @@ describe('CommitPool', () => {
968
1048
expect ( isCommitVerified ) . toBeTrue ( ) ;
969
1049
} ) ;
970
1050
971
- it ( 'should return false when aggregate commit is not signed at height maxHeightCertified' , async ( ) => {
1051
+ it ( 'should return false when aggregate commit <= maxHeightCertified' , async ( ) => {
972
1052
bftMethod . getBFTHeights . mockReturnValue ( {
973
1053
maxHeightCertified : 1080 ,
974
1054
maxHeightPrecommitted : 1100 ,
@@ -1003,6 +1083,30 @@ describe('CommitPool', () => {
1003
1083
expect ( isCommitVerified ) . toBeFalse ( ) ;
1004
1084
} ) ;
1005
1085
1086
+ it ( 'should return true when aggregationBits empty and certificateSignature is empty and aggregateCommit.height === getBFTHeights().maxHeightCertified' , async ( ) => {
1087
+ aggregateCommit = {
1088
+ aggregationBits : Buffer . alloc ( 0 ) ,
1089
+ certificateSignature : Buffer . alloc ( 0 ) ,
1090
+ height : maxHeightCertified ,
1091
+ } ;
1092
+
1093
+ const isCommitVerified = await commitPool . verifyAggregateCommit ( stateStore , aggregateCommit ) ;
1094
+
1095
+ expect ( isCommitVerified ) . toBeTrue ( ) ;
1096
+ } ) ;
1097
+
1098
+ it ( 'should return false when aggregationBits empty and certificateSignature is empty and aggregateCommit.height != getBFTHeights().maxHeightCertified' , async ( ) => {
1099
+ aggregateCommit = {
1100
+ aggregationBits : Buffer . alloc ( 0 ) ,
1101
+ certificateSignature : Buffer . alloc ( 0 ) ,
1102
+ height : maxHeightCertified - 1 ,
1103
+ } ;
1104
+
1105
+ const isCommitVerified = await commitPool . verifyAggregateCommit ( stateStore , aggregateCommit ) ;
1106
+
1107
+ expect ( isCommitVerified ) . toBeFalse ( ) ;
1108
+ } ) ;
1109
+
1006
1110
it ( 'should return false when aggregateCommit height is lesser than equal to maxHeightCertified' , async ( ) => {
1007
1111
aggregateCommit = {
1008
1112
aggregationBits,
@@ -1058,19 +1162,15 @@ describe('CommitPool', () => {
1058
1162
} ) ;
1059
1163
} ) ;
1060
1164
1061
- describe ( 'getAggregateCommit' , ( ) => {
1062
- it . todo ( '' ) ;
1063
- } ) ;
1064
-
1065
- describe ( 'getMaxRemovalHeight' , ( ) => {
1165
+ describe ( '_getMaxRemovalHeight' , ( ) => {
1066
1166
let blockHeader : BlockHeader ;
1067
1167
const finalizedHeight = 1010 ;
1068
1168
1069
1169
beforeEach ( ( ) => {
1070
1170
chain . finalizedHeight = finalizedHeight ;
1071
1171
1072
1172
blockHeader = createFakeBlockHeader ( {
1073
- height : finalizedHeight ,
1173
+ height : finalizedHeight - 1 ,
1074
1174
timestamp : finalizedHeight * 10 ,
1075
1175
aggregateCommit : {
1076
1176
aggregationBits : Buffer . alloc ( 0 ) ,
@@ -1136,10 +1236,6 @@ describe('CommitPool', () => {
1136
1236
} ) ;
1137
1237
} ) ;
1138
1238
1139
- describe ( '_aggregateSingleCommits' , ( ) => {
1140
- it . todo ( '' ) ;
1141
- } ) ;
1142
-
1143
1239
describe ( 'aggregateSingleCommits' , ( ) => {
1144
1240
const height = 45678 ;
1145
1241
const blockHeader1 = createFakeBlockHeader ( { height } ) ;
0 commit comments