@@ -53,12 +53,12 @@ class RegularSyncItSpec extends FlatSpecBase with Matchers with BeforeAndAfter {
53
53
54
54
it should " should sync blockchain with same best block" in customTestCaseResourceM(FakePeer .start2FakePeersRes()) {
55
55
case (peer1, peer2) =>
56
- val blockNumer : BigInt = 2000
56
+ val blockNumer : Int = 2000
57
57
for {
58
58
_ <- peer2.importBlocksUntil(blockNumer)(IdentityUpdate )
59
59
_ <- peer1.connectToPeers(Set (peer2.node))
60
60
_ <- peer1.startRegularSync().delayExecution(50 .milliseconds)
61
- _ <- peer2.broadcastBlock()(IdentityUpdate ).delayExecution(1 .seconds )
61
+ _ <- peer2.broadcastBlock()(IdentityUpdate ).delayExecution(500 .milliseconds )
62
62
_ <- peer1.waitForRegularSyncLoadLastBlock(blockNumer)
63
63
} yield {
64
64
assert(peer1.bl.getBestBlockNumber() == peer2.bl.getBestBlockNumber())
@@ -67,36 +67,42 @@ class RegularSyncItSpec extends FlatSpecBase with Matchers with BeforeAndAfter {
67
67
68
68
it should " should sync blockchain progressing forward in the same time" in customTestCaseResourceM(FakePeer .start2FakePeersRes()) {
69
69
case (peer1, peer2) =>
70
- val blockNumer : BigInt = 2000
71
- val blockNumerOnTop : BigInt = blockNumer + 1
70
+ val blockNumer : Int = 2000
72
71
for {
73
72
_ <- peer2.startRegularSync().delayExecution(50 .milliseconds)
74
73
_ <- peer2.importBlocksUntil(blockNumer)(IdentityUpdate )
75
74
_ <- peer1.connectToPeers(Set (peer2.node))
76
75
_ <- peer1.startRegularSync().delayExecution(500 .milliseconds)
77
76
_ <- peer2.mineNewBlock()(IdentityUpdate ).delayExecution(50 .milliseconds)
78
- _ <- peer1.waitForRegularSyncLoadLastBlock(blockNumerOnTop )
77
+ _ <- peer1.waitForRegularSyncLoadLastBlock(blockNumer + 1 )
79
78
} yield {
80
79
assert(peer1.bl.getBestBlockNumber() == peer2.bl.getBestBlockNumber())
81
80
}
82
81
}
83
82
84
83
it should " should sync peers with divergent chains will be forced to resolve branches" in customTestCaseResourceM(FakePeer .start2FakePeersRes()) {
85
84
case (peer1, peer2) =>
86
- val blockNumer : BigInt = 2000
85
+ val blockNumer : Int = 2000
87
86
for {
88
- _ <- peer2.startRegularSync().delayExecution(50 .milliseconds)
89
87
_ <- peer2.importBlocksUntil(blockNumer)(IdentityUpdate )
90
- _ <- peer1 .startRegularSync().delayExecution(50 .milliseconds)
88
+ _ <- peer2 .startRegularSync().delayExecution(50 .milliseconds)
91
89
_ <- peer1.importBlocksUntil(blockNumer)(IdentityUpdate )
92
- _ <- peer2.mineNewBlock()(IdentityUpdate ).delayExecution(50 .milliseconds)
93
- _ <- peer2.waitForRegularSyncLoadLastBlock(blockNumer + 1 )
94
- _ <- peer1.mineNewBlock()(IdentityUpdate ).delayExecution(50 .milliseconds)
90
+ _ <- peer1.startRegularSync().delayExecution(50 .milliseconds)
91
+ _ <- peer2.mineNewBlock(10 )(IdentityUpdate ).delayExecution(500 .milliseconds)
92
+ _ <- peer2.mineNewBlock(10 )(IdentityUpdate ).delayExecution(500 .milliseconds)
93
+ _ <- peer2.mineNewBlock(10 )(IdentityUpdate ).delayExecution(500 .milliseconds)
94
+ _ <- peer2.waitForRegularSyncLoadLastBlock(blockNumer + 3 )
95
+ _ <- peer1.mineNewBlock()(IdentityUpdate ).delayExecution(500 .milliseconds)
95
96
_ <- peer1.waitForRegularSyncLoadLastBlock(blockNumer + 1 )
96
- _ <- peer1.connectToPeers(Set (peer2.node)).delayExecution(50 .milliseconds)
97
- _ <- peer2.connectToPeers(Set (peer1.node)).delayExecution(50 .milliseconds)
97
+ _ <- peer1.connectToPeers(Set (peer2.node)).delayExecution(500 .milliseconds)
98
+ _ <- peer1.waitForRegularSyncLoadLastBlock(blockNumer + 3 )
99
+ _ <- peer2.waitForRegularSyncLoadLastBlock(blockNumer + 3 )
98
100
} yield {
99
- assert(peer1.bl.getBestBlockNumber() == peer2.bl.getBestBlockNumber())
101
+ assert(peer1.bl.getBestBlock().number == peer2.bl.getBestBlock().number)
102
+ (peer1.bl.getBlockByNumber(blockNumer + 1 ), peer1.bl.getBlockByNumber(blockNumer + 1 )) match {
103
+ case (Some (blockP1), Some (blockP2)) => assert(blockP1.header.difficulty == blockP2.header.difficulty)
104
+ case (_ , _) => fail(" invalid difficulty validation" )
105
+ }
100
106
}
101
107
}
102
108
@@ -378,11 +384,11 @@ object RegularSyncItSpec {
378
384
regularSync ! RegularSync .Start
379
385
}
380
386
381
- def mineNewBlock ()(updateWorldForBlock : (BigInt , InMemoryWorldStateProxy ) => InMemoryWorldStateProxy ): Task [Unit ] = Task {
387
+ def mineNewBlock (plusDifficulty : BigInt = 0 )(updateWorldForBlock : (BigInt , InMemoryWorldStateProxy ) => InMemoryWorldStateProxy ): Task [Unit ] = Task {
382
388
val block : Block = bl.getBestBlock()
383
389
val currentTd = bl.getTotalDifficultyByHash(block.hash).get
384
390
val currentWolrd = getMptForBlock(block)
385
- val (newBlock, newTd, newWorld) = createChildBlock(block, currentTd, currentWolrd)(updateWorldForBlock)
391
+ val (newBlock, newTd, newWorld) = createChildBlock(block, currentTd, currentWolrd, plusDifficulty )(updateWorldForBlock)
386
392
regularSync ! RegularSync .MinedBlock (newBlock)
387
393
}
388
394
@@ -391,13 +397,16 @@ object RegularSyncItSpec {
391
397
Task (bl.getBestBlockNumber() == blockNumer), 1 .second,90 ) { isDone => isDone }
392
398
}
393
399
394
- private def createChildBlock (parent : Block , parentTd : BigInt , parentWorld : InMemoryWorldStateProxy )(
400
+ private def createChildBlock (parent : Block , parentTd : BigInt , parentWorld : InMemoryWorldStateProxy , plusDifficulty : BigInt = 0 )(
395
401
updateWorldForBlock : (BigInt , InMemoryWorldStateProxy ) => InMemoryWorldStateProxy
396
402
): (Block , BigInt , InMemoryWorldStateProxy ) = {
397
403
val newBlockNumber = parent.header.number + 1
398
404
val newWorld = updateWorldForBlock(newBlockNumber, parentWorld)
405
+ // val difficulty = plusDifficulty + parent.header.difficulty//plusDifficulty.map(_ + parent.header.difficulty).getOrElse(parent.header.difficulty)
399
406
val newBlock = parent.copy(header =
400
- parent.header.copy(parentHash = parent.header.hash, number = newBlockNumber, stateRoot = newWorld.stateRootHash)
407
+ parent.header.copy(parentHash = parent.header.hash, number = newBlockNumber,
408
+ stateRoot = newWorld.stateRootHash,
409
+ difficulty = plusDifficulty + parent.header.difficulty)
401
410
)
402
411
val newTd = newBlock.header.difficulty + parentTd
403
412
(newBlock, newTd, parentWorld)
@@ -422,14 +431,16 @@ object RegularSyncItSpec {
422
431
}
423
432
}
424
433
425
- def broadcastBlock ()(updateWorldForBlock : (BigInt , InMemoryWorldStateProxy ) => InMemoryWorldStateProxy ): Task [Unit ] = {
426
- Task (bl.getBestBlock()).flatMap { block => Task {
427
- val currentTd = bl.getTotalDifficultyByHash(block.hash).get
428
- val currentWolrd = getMptForBlock(block)
429
- val (newBlock, newTd, newWorld) = createChildBlock(block, currentTd, currentWolrd)(updateWorldForBlock)
430
- broadcastBlock(newBlock, newTd)
431
- }
432
- }
434
+ def broadcastBlock (blockNumber : Option [Int ] = None )(updateWorldForBlock : (BigInt , InMemoryWorldStateProxy ) => InMemoryWorldStateProxy ): Task [Unit ] = {
435
+ Task ( blockNumber match {
436
+ case Some (bNumber) => bl.getBlockByNumber(bNumber).get
437
+ case None => bl.getBestBlock()
438
+ }).flatMap { block => Task {
439
+ val currentTd = bl.getTotalDifficultyByHash(block.hash).get
440
+ val currentWolrd = getMptForBlock(block)
441
+ val (newBlock, newTd, newWorld) = createChildBlock(block, currentTd, currentWolrd)(updateWorldForBlock)
442
+ broadcastBlock(newBlock, newTd)
443
+ }}
433
444
}
434
445
435
446
def startPeer (): Task [Unit ] = {
@@ -462,6 +473,7 @@ object RegularSyncItSpec {
462
473
val currentNodes = knownNodes.map(Node .fromUri).map(_.id)
463
474
requestedNodes.subsetOf(currentNodes)
464
475
}
476
+
465
477
} yield ()
466
478
}
467
479
}
0 commit comments