@@ -51,20 +51,36 @@ import scala.concurrent.duration._
51
51
class RegularSyncItSpec extends FlatSpecBase with Matchers with BeforeAndAfter {
52
52
implicit val testScheduler = Scheduler .fixedPool(" test" , 16 )
53
53
54
-
55
- it should " should update target block" in customTestCaseResourceM(FakePeer .start2FakePeersRes()) {
54
+ it should " should sync blockchain with same best block" in customTestCaseResourceM(FakePeer .start2FakePeersRes()) {
56
55
case (peer1, peer2) =>
57
- val blockNumer : BigInt = 100
56
+ val blockNumer : BigInt = 2000
58
57
for {
59
58
_ <- peer2.importBlocksUntil(blockNumer)(IdentityUpdate )
60
59
_ <- peer1.connectToPeers(Set (peer2.node))
61
60
_ <- peer1.startRegularSync().delayExecution(50 .milliseconds)
61
+ _ <- peer2.broadcastBlock()(IdentityUpdate ).delayExecution(1 .seconds)
62
62
_ <- peer1.waitForRegularSyncLoadLastBlock(blockNumer)
63
63
} yield {
64
64
assert(peer1.bl.getBestBlockNumber() == peer2.bl.getBestBlockNumber())
65
65
}
66
66
}
67
67
68
+ it should " should sync blockchain progressing forward in the same time" in customTestCaseResourceM(FakePeer .start2FakePeersRes()) {
69
+ case (peer1, peer2) =>
70
+ val blockNumer : BigInt = 2000
71
+ val blockNumerOnTop : BigInt = blockNumer + 1
72
+ for {
73
+ _ <- peer2.startRegularSync().delayExecution(50 .milliseconds)
74
+ _ <- peer2.importBlocksUntil(blockNumer)(IdentityUpdate )
75
+ _ <- peer1.connectToPeers(Set (peer2.node))
76
+ _ <- peer1.startRegularSync().delayExecution(50 .milliseconds)
77
+ _ <- peer2.mineNewBlock()(IdentityUpdate ).delayExecution(50 .milliseconds)
78
+ _ <- peer1.waitForRegularSyncLoadLastBlock(blockNumerOnTop)
79
+ } yield {
80
+ assert(peer1.bl.getBestBlockNumber() == peer2.bl.getBestBlockNumber())
81
+ }
82
+ }
83
+
68
84
}
69
85
70
86
object RegularSyncItSpec {
@@ -343,6 +359,14 @@ object RegularSyncItSpec {
343
359
regularSync ! RegularSync .Start
344
360
}
345
361
362
+ def mineNewBlock ()(updateWorldForBlock : (BigInt , InMemoryWorldStateProxy ) => InMemoryWorldStateProxy ): Task [Unit ] = Task {
363
+ val block : Block = bl.getBestBlock()
364
+ val currentTd = bl.getTotalDifficultyByHash(block.hash).get
365
+ val currentWolrd = getMptForBlock(block)
366
+ val (newBlock, newTd, newWorld) = createChildBlock(block, currentTd, currentWolrd)(updateWorldForBlock)
367
+ regularSync ! RegularSync .MinedBlock (newBlock)
368
+ }
369
+
346
370
def waitForRegularSyncLoadLastBlock (blockNumer : BigInt ): Task [Boolean ] = {
347
371
retryUntilWithDelay(
348
372
Task (bl.getBestBlockNumber() == blockNumer), 1 .second,90 ) { isDone => isDone }
@@ -379,6 +403,16 @@ object RegularSyncItSpec {
379
403
}
380
404
}
381
405
406
+ def broadcastBlock ()(updateWorldForBlock : (BigInt , InMemoryWorldStateProxy ) => InMemoryWorldStateProxy ): Task [Unit ] = {
407
+ Task (bl.getBestBlock()).flatMap { block => Task {
408
+ val currentTd = bl.getTotalDifficultyByHash(block.hash).get
409
+ val currentWolrd = getMptForBlock(block)
410
+ val (newBlock, newTd, newWorld) = createChildBlock(block, currentTd, currentWolrd)(updateWorldForBlock)
411
+ broadcastBlock(newBlock, newTd)
412
+ }
413
+ }
414
+ }
415
+
382
416
def startPeer (): Task [Unit ] = {
383
417
for {
384
418
_ <- Task {
0 commit comments