Skip to content

Commit

Permalink
add it - should sync blockchain progressing forward in the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximiliano Biandratti authored and biandratti committed Oct 20, 2020
1 parent 267eb60 commit f9a4a25
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/it/scala/io/iohk/ethereum/sync/RegularSyncItSpec.scala
Expand Up @@ -51,20 +51,36 @@ import scala.concurrent.duration._
class RegularSyncItSpec extends FlatSpecBase with Matchers with BeforeAndAfter {
implicit val testScheduler = Scheduler.fixedPool("test", 16)


it should "should update target block" in customTestCaseResourceM(FakePeer.start2FakePeersRes()) {
it should "should sync blockchain with same best block" in customTestCaseResourceM(FakePeer.start2FakePeersRes()) {
case (peer1, peer2) =>
val blockNumer: BigInt = 100
val blockNumer: BigInt = 2000
for {
_ <- peer2.importBlocksUntil(blockNumer)(IdentityUpdate)
_ <- peer1.connectToPeers(Set(peer2.node))
_ <- peer1.startRegularSync().delayExecution(50.milliseconds)
_ <- peer2.broadcastBlock()(IdentityUpdate).delayExecution(1.seconds)
_ <- peer1.waitForRegularSyncLoadLastBlock(blockNumer)
} yield {
assert(peer1.bl.getBestBlockNumber() == peer2.bl.getBestBlockNumber())
}
}

it should "should sync blockchain progressing forward in the same time" in customTestCaseResourceM(FakePeer.start2FakePeersRes()) {
case (peer1, peer2) =>
val blockNumer: BigInt = 2000
val blockNumerOnTop: BigInt = blockNumer + 1
for {
_ <- peer2.startRegularSync().delayExecution(50.milliseconds)
_ <- peer2.importBlocksUntil(blockNumer)(IdentityUpdate)
_ <- peer1.connectToPeers(Set(peer2.node))
_ <- peer1.startRegularSync().delayExecution(50.milliseconds)
_ <- peer2.mineNewBlock()(IdentityUpdate).delayExecution(50.milliseconds)
_ <- peer1.waitForRegularSyncLoadLastBlock(blockNumerOnTop)
} yield {
assert(peer1.bl.getBestBlockNumber() == peer2.bl.getBestBlockNumber())
}
}

}

object RegularSyncItSpec {
Expand Down Expand Up @@ -343,6 +359,14 @@ object RegularSyncItSpec {
regularSync ! RegularSync.Start
}

def mineNewBlock()(updateWorldForBlock: (BigInt, InMemoryWorldStateProxy) => InMemoryWorldStateProxy): Task[Unit] = Task {
val block: Block = bl.getBestBlock()
val currentTd = bl.getTotalDifficultyByHash(block.hash).get
val currentWolrd = getMptForBlock(block)
val (newBlock, newTd, newWorld) = createChildBlock(block, currentTd, currentWolrd)(updateWorldForBlock)
regularSync ! RegularSync.MinedBlock(newBlock)
}

def waitForRegularSyncLoadLastBlock(blockNumer: BigInt): Task[Boolean] = {
retryUntilWithDelay(
Task(bl.getBestBlockNumber() == blockNumer), 1.second,90) { isDone => isDone }
Expand Down Expand Up @@ -379,6 +403,16 @@ object RegularSyncItSpec {
}
}

def broadcastBlock()(updateWorldForBlock: (BigInt, InMemoryWorldStateProxy) => InMemoryWorldStateProxy): Task[Unit] = {
Task(bl.getBestBlock()).flatMap { block => Task {
val currentTd = bl.getTotalDifficultyByHash(block.hash).get
val currentWolrd = getMptForBlock(block)
val (newBlock, newTd, newWorld) = createChildBlock(block, currentTd, currentWolrd)(updateWorldForBlock)
broadcastBlock(newBlock, newTd)
}
}
}

def startPeer(): Task[Unit] = {
for {
_ <- Task {
Expand Down

0 comments on commit f9a4a25

Please sign in to comment.