Skip to content

Commit 81f7b9b

Browse files
Maximiliano Biandrattibiandratti
authored andcommitted
fix it with divergent chains
1 parent 13389bb commit 81f7b9b

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

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

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ class RegularSyncItSpec extends FlatSpecBase with Matchers with BeforeAndAfter {
5353

5454
it should "should sync blockchain with same best block" in customTestCaseResourceM(FakePeer.start2FakePeersRes()) {
5555
case (peer1, peer2) =>
56-
val blockNumer: BigInt = 2000
56+
val blockNumer: Int = 2000
5757
for {
5858
_ <- peer2.importBlocksUntil(blockNumer)(IdentityUpdate)
5959
_ <- peer1.connectToPeers(Set(peer2.node))
6060
_ <- peer1.startRegularSync().delayExecution(50.milliseconds)
61-
_ <- peer2.broadcastBlock()(IdentityUpdate).delayExecution(1.seconds)
61+
_ <- peer2.broadcastBlock()(IdentityUpdate).delayExecution(500.milliseconds)
6262
_ <- peer1.waitForRegularSyncLoadLastBlock(blockNumer)
6363
} yield {
6464
assert(peer1.bl.getBestBlockNumber() == peer2.bl.getBestBlockNumber())
@@ -67,36 +67,42 @@ class RegularSyncItSpec extends FlatSpecBase with Matchers with BeforeAndAfter {
6767

6868
it should "should sync blockchain progressing forward in the same time" in customTestCaseResourceM(FakePeer.start2FakePeersRes()) {
6969
case (peer1, peer2) =>
70-
val blockNumer: BigInt = 2000
71-
val blockNumerOnTop: BigInt = blockNumer + 1
70+
val blockNumer: Int = 2000
7271
for {
7372
_ <- peer2.startRegularSync().delayExecution(50.milliseconds)
7473
_ <- peer2.importBlocksUntil(blockNumer)(IdentityUpdate)
7574
_ <- peer1.connectToPeers(Set(peer2.node))
7675
_ <- peer1.startRegularSync().delayExecution(500.milliseconds)
7776
_ <- peer2.mineNewBlock()(IdentityUpdate).delayExecution(50.milliseconds)
78-
_ <- peer1.waitForRegularSyncLoadLastBlock(blockNumerOnTop)
77+
_ <- peer1.waitForRegularSyncLoadLastBlock(blockNumer + 1)
7978
} yield {
8079
assert(peer1.bl.getBestBlockNumber() == peer2.bl.getBestBlockNumber())
8180
}
8281
}
8382

8483
it should "should sync peers with divergent chains will be forced to resolve branches"in customTestCaseResourceM(FakePeer.start2FakePeersRes()) {
8584
case (peer1, peer2) =>
86-
val blockNumer: BigInt = 2000
85+
val blockNumer: Int = 2000
8786
for {
88-
_ <- peer2.startRegularSync().delayExecution(50.milliseconds)
8987
_ <- peer2.importBlocksUntil(blockNumer)(IdentityUpdate)
90-
_ <- peer1.startRegularSync().delayExecution(50.milliseconds)
88+
_ <- peer2.startRegularSync().delayExecution(50.milliseconds)
9189
_ <- peer1.importBlocksUntil(blockNumer)(IdentityUpdate)
92-
_ <- peer2.mineNewBlock()(IdentityUpdate).delayExecution(50.milliseconds)
93-
_ <- peer2.waitForRegularSyncLoadLastBlock(blockNumer + 1)
94-
_ <- peer1.mineNewBlock()(IdentityUpdate).delayExecution(50.milliseconds)
90+
_ <- peer1.startRegularSync().delayExecution(50.milliseconds)
91+
_ <- peer2.mineNewBlock(10)(IdentityUpdate).delayExecution(500.milliseconds)
92+
_ <- peer2.mineNewBlock(10)(IdentityUpdate).delayExecution(500.milliseconds)
93+
_ <- peer2.mineNewBlock(10)(IdentityUpdate).delayExecution(500.milliseconds)
94+
_ <- peer2.waitForRegularSyncLoadLastBlock(blockNumer + 3)
95+
_ <- peer1.mineNewBlock()(IdentityUpdate).delayExecution(500.milliseconds)
9596
_ <- peer1.waitForRegularSyncLoadLastBlock(blockNumer + 1)
96-
_ <- peer1.connectToPeers(Set(peer2.node)).delayExecution(50.milliseconds)
97-
_ <- peer2.connectToPeers(Set(peer1.node)).delayExecution(50.milliseconds)
97+
_ <- peer1.connectToPeers(Set(peer2.node)).delayExecution(500.milliseconds)
98+
_ <- peer1.waitForRegularSyncLoadLastBlock(blockNumer + 3)
99+
_ <- peer2.waitForRegularSyncLoadLastBlock(blockNumer + 3)
98100
} yield {
99-
assert(peer1.bl.getBestBlockNumber() == peer2.bl.getBestBlockNumber())
101+
assert(peer1.bl.getBestBlock().number == peer2.bl.getBestBlock().number)
102+
(peer1.bl.getBlockByNumber(blockNumer + 1), peer1.bl.getBlockByNumber(blockNumer + 1)) match {
103+
case (Some(blockP1), Some(blockP2)) => assert(blockP1.header.difficulty == blockP2.header.difficulty)
104+
case (_ , _) => fail("invalid difficulty validation")
105+
}
100106
}
101107
}
102108

@@ -378,11 +384,11 @@ object RegularSyncItSpec {
378384
regularSync ! RegularSync.Start
379385
}
380386

381-
def mineNewBlock()(updateWorldForBlock: (BigInt, InMemoryWorldStateProxy) => InMemoryWorldStateProxy): Task[Unit] = Task {
387+
def mineNewBlock(plusDifficulty: BigInt = 0)(updateWorldForBlock: (BigInt, InMemoryWorldStateProxy) => InMemoryWorldStateProxy): Task[Unit] = Task {
382388
val block: Block = bl.getBestBlock()
383389
val currentTd = bl.getTotalDifficultyByHash(block.hash).get
384390
val currentWolrd = getMptForBlock(block)
385-
val (newBlock, newTd, newWorld) = createChildBlock(block, currentTd, currentWolrd)(updateWorldForBlock)
391+
val (newBlock, newTd, newWorld) = createChildBlock(block, currentTd, currentWolrd, plusDifficulty)(updateWorldForBlock)
386392
regularSync ! RegularSync.MinedBlock(newBlock)
387393
}
388394

@@ -391,13 +397,16 @@ object RegularSyncItSpec {
391397
Task(bl.getBestBlockNumber() == blockNumer), 1.second,90) { isDone => isDone }
392398
}
393399

394-
private def createChildBlock(parent: Block, parentTd: BigInt, parentWorld: InMemoryWorldStateProxy)(
400+
private def createChildBlock(parent: Block, parentTd: BigInt, parentWorld: InMemoryWorldStateProxy, plusDifficulty: BigInt = 0)(
395401
updateWorldForBlock: (BigInt, InMemoryWorldStateProxy) => InMemoryWorldStateProxy
396402
): (Block, BigInt, InMemoryWorldStateProxy) = {
397403
val newBlockNumber = parent.header.number + 1
398404
val newWorld = updateWorldForBlock(newBlockNumber, parentWorld)
405+
//val difficulty = plusDifficulty + parent.header.difficulty//plusDifficulty.map(_ + parent.header.difficulty).getOrElse(parent.header.difficulty)
399406
val newBlock = parent.copy(header =
400-
parent.header.copy(parentHash = parent.header.hash, number = newBlockNumber, stateRoot = newWorld.stateRootHash)
407+
parent.header.copy(parentHash = parent.header.hash, number = newBlockNumber,
408+
stateRoot = newWorld.stateRootHash,
409+
difficulty = plusDifficulty + parent.header.difficulty)
401410
)
402411
val newTd = newBlock.header.difficulty + parentTd
403412
(newBlock, newTd, parentWorld)
@@ -422,14 +431,16 @@ object RegularSyncItSpec {
422431
}
423432
}
424433

425-
def broadcastBlock()(updateWorldForBlock: (BigInt, InMemoryWorldStateProxy) => InMemoryWorldStateProxy): Task[Unit] = {
426-
Task(bl.getBestBlock()).flatMap { block => Task {
427-
val currentTd = bl.getTotalDifficultyByHash(block.hash).get
428-
val currentWolrd = getMptForBlock(block)
429-
val (newBlock, newTd, newWorld) = createChildBlock(block, currentTd, currentWolrd)(updateWorldForBlock)
430-
broadcastBlock(newBlock, newTd)
431-
}
432-
}
434+
def broadcastBlock(blockNumber: Option[Int] = None)(updateWorldForBlock: (BigInt, InMemoryWorldStateProxy) => InMemoryWorldStateProxy): Task[Unit] = {
435+
Task( blockNumber match {
436+
case Some(bNumber) => bl.getBlockByNumber(bNumber).get
437+
case None => bl.getBestBlock()
438+
}).flatMap { block => Task {
439+
val currentTd = bl.getTotalDifficultyByHash(block.hash).get
440+
val currentWolrd = getMptForBlock(block)
441+
val (newBlock, newTd, newWorld) = createChildBlock(block, currentTd, currentWolrd)(updateWorldForBlock)
442+
broadcastBlock(newBlock, newTd)
443+
}}
433444
}
434445

435446
def startPeer(): Task[Unit] = {
@@ -462,6 +473,7 @@ object RegularSyncItSpec {
462473
val currentNodes = knownNodes.map(Node.fromUri).map(_.id)
463474
requestedNodes.subsetOf(currentNodes)
464475
}
476+
465477
} yield ()
466478
}
467479
}

0 commit comments

Comments
 (0)