-
Notifications
You must be signed in to change notification settings - Fork 76
/
RegularSyncItSpec.scala
70 lines (63 loc) · 3.34 KB
/
RegularSyncItSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package io.iohk.ethereum.sync
import io.iohk.ethereum.FlatSpecBase
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
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(FakePeer.start2FakePeersRes()) {
case (peer1, peer2) =>
val blockNumer: Int = 2000
for {
_ <- peer2.importBlocksUntil(blockNumer)(IdentityUpdate)
_ <- peer1.connectToPeers(Set(peer2.node))
_ <- 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(FakePeer.start2FakePeersRes()) {
case (peer1, peer2) =>
val blockNumer: Int = 2000
for {
_ <- peer2.startRegularSync().delayExecution(50.milliseconds)
_ <- peer2.importBlocksUntil(blockNumer)(IdentityUpdate)
_ <- peer1.connectToPeers(Set(peer2.node))
_ <- 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(FakePeer.start2FakePeersRes()) {
case (peer1, peer2) =>
val blockNumer: Int = 2000
for {
_ <- 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.waitForRegularSyncLoadLastBlock(blockNumer + 1)
_ <- peer1.connectToPeers(Set(peer2.node)).delayExecution(500.milliseconds)
_ <- peer1.waitForRegularSyncLoadLastBlock(blockNumer + 3)
_ <- peer2.waitForRegularSyncLoadLastBlock(blockNumer + 3)
} yield {
assert(peer1.bl.getBestBlock().number == peer2.bl.getBestBlock().number)
(peer1.bl.getBlockByNumber(blockNumer + 1), peer1.bl.getBlockByNumber(blockNumer + 1)) match {
case (Some(blockP1), Some(blockP2)) => assert(blockP1.header.difficulty == blockP2.header.difficulty)
case (_ , _) => fail("invalid difficulty validation")
}
}
}
}