@@ -223,7 +223,9 @@ abstract class CommonFakePeer(peerName: String, fakePeerCustomConfig: FakePeerCu
223
223
nodesPerRequest = 200 ,
224
224
maxTargetDifference = 1 ,
225
225
syncRetryInterval = 50 .milliseconds,
226
- blacklistDuration = 100 .seconds
226
+ blacklistDuration = 100 .seconds,
227
+ fastSyncMaxBatchRetries = 2 ,
228
+ fastSyncBlockValidationN = 200
227
229
)
228
230
229
231
lazy val broadcaster = new BlockBroadcast (etcPeerManager)
@@ -298,21 +300,90 @@ abstract class CommonFakePeer(peerName: String, fakePeerCustomConfig: FakePeerCu
298
300
(newBlock, newWeight, parentWorld)
299
301
}
300
302
303
+ private def generateInvalidBlock (
304
+ currentBestBlock : Block
305
+ )(updateWorldForBlock : (BigInt , InMemoryWorldStateProxy ) => InMemoryWorldStateProxy ): Task [Unit ] = {
306
+ Task {
307
+ val currentWorld = getMptForBlock(currentBestBlock)
308
+
309
+ val newBlockNumber = currentBestBlock.header.number + 1
310
+ val newWorld = updateWorldForBlock(newBlockNumber, currentWorld)
311
+
312
+ // The child block is made invalid by not properly updating its parent hash.
313
+ val childBlock =
314
+ currentBestBlock.copy(header =
315
+ currentBestBlock.header.copy(
316
+ number = newBlockNumber,
317
+ stateRoot = newWorld.stateRootHash
318
+ )
319
+ )
320
+ val newWeight = ChainWeight .totalDifficultyOnly(1 )
321
+
322
+ broadcastBlock(childBlock, newWeight)
323
+ bl.save(childBlock, Seq (), newWeight, saveAsBestBlock = true )
324
+ }
325
+ }
326
+
327
+ private def generateValidBlock (
328
+ currentBestBlock : Block
329
+ )(updateWorldForBlock : (BigInt , InMemoryWorldStateProxy ) => InMemoryWorldStateProxy ): Task [Unit ] = {
330
+ Task {
331
+ val currentWeight = bl.getChainWeightByHash(currentBestBlock.hash).get
332
+ val currentWorld = getMptForBlock(currentBestBlock)
333
+ val (newBlock, newWeight, _) =
334
+ createChildBlock(currentBestBlock, currentWeight, currentWorld)(updateWorldForBlock)
335
+ bl.save(newBlock, Seq (), newWeight, saveAsBestBlock = true )
336
+ broadcastBlock(newBlock, newWeight)
337
+ }
338
+ }
339
+
301
340
def importBlocksUntil (
302
341
n : BigInt
303
342
)(updateWorldForBlock : (BigInt , InMemoryWorldStateProxy ) => InMemoryWorldStateProxy ): Task [Unit ] = {
304
343
Task (bl.getBestBlock()).flatMap { block =>
305
344
if (block.get.number >= n) {
306
345
Task (())
307
346
} else {
308
- Task {
309
- val currentWeight = bl.getChainWeightByHash(block.get.hash).get
310
- val currentWolrd = getMptForBlock(block.get)
311
- val (newBlock, newWeight, _) = createChildBlock(block.get, currentWeight, currentWolrd)(updateWorldForBlock)
312
- bl.save(newBlock, Seq (), newWeight, saveAsBestBlock = true )
313
- broadcastBlock(newBlock, newWeight)
314
- }.flatMap(_ => importBlocksUntil(n)(updateWorldForBlock))
347
+ generateValidBlock(block.get)(updateWorldForBlock).flatMap(_ => importBlocksUntil(n)(updateWorldForBlock))
348
+ }
349
+ }
350
+ }
351
+
352
+ def importInvalidBlocks (
353
+ from : BigInt ,
354
+ to : BigInt
355
+ )(updateWorldForBlock : (BigInt , InMemoryWorldStateProxy ) => InMemoryWorldStateProxy ): Task [Unit ] = {
356
+ Task (bl.getBestBlock()).flatMap { block =>
357
+ if (block.get.number >= to) {
358
+ Task (())
359
+ } else if (block.get.number >= from) {
360
+ generateInvalidBlock(block.get)(updateWorldForBlock).flatMap(_ =>
361
+ importInvalidBlocks(from, to)(updateWorldForBlock)
362
+ )
363
+ } else {
364
+ generateValidBlock(block.get)(updateWorldForBlock).flatMap(_ =>
365
+ importInvalidBlocks(from, to)(updateWorldForBlock)
366
+ )
315
367
}
368
+
369
+ }
370
+ }
371
+
372
+ def importInvalidBlockNumbers (
373
+ from : BigInt ,
374
+ to : BigInt
375
+ )(updateWorldForBlock : (BigInt , InMemoryWorldStateProxy ) => InMemoryWorldStateProxy ): Task [Unit ] = {
376
+ Task (bl.getBestBlock()).flatMap { block =>
377
+ if (block.get.number >= to) {
378
+ Task (())
379
+ } else if (block.get.number >= from) {
380
+ generateInvalidBlock(block.get)(updateWorldForBlock).flatMap(_ =>
381
+ importInvalidBlockNumbers(from, to)(updateWorldForBlock)
382
+ )
383
+ } else {
384
+ importBlocksUntil(from)(updateWorldForBlock)
385
+ }
386
+
316
387
}
317
388
}
318
389
0 commit comments