Skip to content

Commit 4953406

Browse files
Maximiliano Biandrattibiandratti
authored andcommitted
add it - should sync blockchain progressing forward in the same time
1 parent 01e4bb5 commit 4953406

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

src/it/scala/io/iohk/ethereum/sync/RegularSyncItSpec.scala

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,36 @@ import scala.concurrent.duration._
5151
class RegularSyncItSpec extends FlatSpecBase with Matchers with BeforeAndAfter {
5252
implicit val testScheduler = Scheduler.fixedPool("test", 16)
5353

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()) {
5655
case (peer1, peer2) =>
57-
val blockNumer: BigInt = 100
56+
val blockNumer: BigInt = 2000
5857
for {
5958
_ <- peer2.importBlocksUntil(blockNumer)(IdentityUpdate)
6059
_ <- peer1.connectToPeers(Set(peer2.node))
6160
_ <- peer1.startRegularSync().delayExecution(50.milliseconds)
61+
_ <- peer2.broadcastBlock()(IdentityUpdate).delayExecution(1.seconds)
6262
_ <- peer1.waitForRegularSyncLoadLastBlock(blockNumer)
6363
} yield {
6464
assert(peer1.bl.getBestBlockNumber() == peer2.bl.getBestBlockNumber())
6565
}
6666
}
6767

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+
6884
}
6985

7086
object RegularSyncItSpec {
@@ -343,6 +359,14 @@ object RegularSyncItSpec {
343359
regularSync ! RegularSync.Start
344360
}
345361

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+
346370
def waitForRegularSyncLoadLastBlock(blockNumer: BigInt): Task[Boolean] = {
347371
retryUntilWithDelay(
348372
Task(bl.getBestBlockNumber() == blockNumer), 1.second,90) { isDone => isDone }
@@ -379,6 +403,16 @@ object RegularSyncItSpec {
379403
}
380404
}
381405

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+
382416
def startPeer(): Task[Unit] = {
383417
for {
384418
_ <- Task {

0 commit comments

Comments
 (0)