Skip to content

Commit

Permalink
unify behavior between regular and fast sync
Browse files Browse the repository at this point in the history
  • Loading branch information
biandratti committed Oct 16, 2020
1 parent 9fc4a64 commit fd9339f
Show file tree
Hide file tree
Showing 10 changed files with 568 additions and 647 deletions.
20 changes: 4 additions & 16 deletions src/it/scala/io/iohk/ethereum/sync/FastSyncItSpec.scala
@@ -1,20 +1,20 @@
package io.iohk.ethereum.sync

import java.net.{InetSocketAddress, ServerSocket}

import akka.util.ByteString
import io.iohk.ethereum.FlatSpecBase
import io.iohk.ethereum.domain._
import io.iohk.ethereum.ledger.InMemoryWorldStateProxy
import io.iohk.ethereum.sync.FastSyncItSpec._
import io.iohk.ethereum.sync.FastSyncItSpecUtils.{FakePeer, FakePeerCustomConfig, HostConfig}
import io.iohk.ethereum.sync.util.FastSyncItSpecUtils.FakePeer
import io.iohk.ethereum.sync.util.SyncCommonItSpec._
import io.iohk.ethereum.sync.util.SyncCommonItSpecUtils._
import monix.execution.Scheduler
import org.scalatest.BeforeAndAfter
import org.scalatest.matchers.should.Matchers

import scala.concurrent.duration._

class FastSyncItSpec extends FlatSpecBase with Matchers with BeforeAndAfter {
class FastSyncItSpec extends FlatSpecBase with Matchers with BeforeAndAfter {
implicit val testScheduler = Scheduler.fixedPool("test", 16)

"FastSync" should "should sync blockchain without state nodes" in customTestCaseResourceM(
Expand Down Expand Up @@ -132,18 +132,6 @@ class FastSyncItSpec extends FlatSpecBase with Matchers with BeforeAndAfter {
}

object FastSyncItSpec {
def randomAddress(): InetSocketAddress = {
val s = new ServerSocket(0)
try {
new InetSocketAddress("localhost", s.getLocalPort)
} finally {
s.close()
}
}

final case class BlockchainState(bestBlock: Block, currentWorldState: InMemoryWorldStateProxy, currentTd: BigInt)

val IdentityUpdate: (BigInt, InMemoryWorldStateProxy) => InMemoryWorldStateProxy = (_, world) => world

def updateWorldWithNAccounts(n: Int, world: InMemoryWorldStateProxy): InMemoryWorldStateProxy = {
val resultWorld = (0 until n).foldLeft(world) { (world, num) =>
Expand Down
40 changes: 20 additions & 20 deletions src/it/scala/io/iohk/ethereum/sync/RegularSyncItSpec.scala
@@ -1,8 +1,8 @@
package io.iohk.ethereum.sync

import io.iohk.ethereum.FlatSpecBase
import io.iohk.ethereum.sync.util.FakePeerRegularSync
import io.iohk.ethereum.sync.util.SyncUtils.identityUpdate
import io.iohk.ethereum.sync.util.RegularSyncItSpecUtils.FakePeer
import io.iohk.ethereum.sync.util.SyncCommonItSpec._
import monix.execution.Scheduler
import org.scalatest.BeforeAndAfter
import org.scalatest.matchers.should.Matchers
Expand All @@ -12,48 +12,48 @@ import scala.concurrent.duration._
class RegularSyncItSpec extends FlatSpecBase with Matchers with BeforeAndAfter {
implicit val testScheduler = Scheduler.fixedPool("test", 16)

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

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

it should "should sync peers with divergent chains will be forced to resolve branches"in customTestCaseResourceM(FakePeerRegularSync.start2FakePeersRes()) {
it should "should sync peers with divergent chains will be forced to resolve branches"in customTestCaseResourceM(FakePeer.start2FakePeersRes()) {
case (peer1, peer2) =>
val blockNumer: Int = 2000
for {
_ <- peer2.importBlocksUntil(blockNumer)(identityUpdate)
_ <- peer2.start.delayExecution(50.milliseconds)
_ <- peer1.importBlocksUntil(blockNumer)(identityUpdate)
_ <- peer1.start.delayExecution(50.milliseconds)
_ <- peer2.mineNewBlock(10)(identityUpdate).delayExecution(500.milliseconds)
_ <- peer2.mineNewBlock(10)(identityUpdate).delayExecution(500.milliseconds)
_ <- peer2.mineNewBlock(10)(identityUpdate).delayExecution(500.milliseconds)
_ <- peer2.importBlocksUntil(blockNumer)(IdentityUpdate)
_ <- peer2.startRegularSync().delayExecution(50.milliseconds)
_ <- peer1.importBlocksUntil(blockNumer)(IdentityUpdate)
_ <- peer1.startRegularSync().delayExecution(50.milliseconds)
_ <- peer2.mineNewBlock(10)(IdentityUpdate).delayExecution(500.milliseconds)
_ <- peer2.mineNewBlock(10)(IdentityUpdate).delayExecution(500.milliseconds)
_ <- peer2.mineNewBlock(10)(IdentityUpdate).delayExecution(500.milliseconds)
_ <- peer2.waitForRegularSyncLoadLastBlock(blockNumer + 3)
_ <- peer1.mineNewBlock()(identityUpdate).delayExecution(500.milliseconds)
_ <- peer1.mineNewBlock()(IdentityUpdate).delayExecution(500.milliseconds)
_ <- peer1.waitForRegularSyncLoadLastBlock(blockNumer + 1)
_ <- peer1.connectToPeers(Set(peer2.node)).delayExecution(500.milliseconds)
_ <- peer1.waitForRegularSyncLoadLastBlock(blockNumer + 3)
Expand Down

0 comments on commit fd9339f

Please sign in to comment.