@@ -19,6 +19,7 @@ import io.iohk.ethereum.ledger.Ledger.BlockResult
1919import monix .execution .Scheduler
2020import org .scalamock .scalatest .MockFactory
2121import org .scalatest .BeforeAndAfterAll
22+ import org .scalatest .concurrent .Eventually .eventually
2223import org .scalatest .flatspec .AsyncFlatSpecLike
2324import org .scalatest .matchers .should .Matchers
2425
@@ -150,9 +151,8 @@ class BlockImporterItSpec
150151 blockImporter ! BlockImporter .Start
151152 blockImporter ! BlockFetcher .PickedBlocks (NonEmptyList .fromListUnsafe(newBranch))
152153
153- Thread .sleep(1000 )
154154 // because the blocks are not valid, we shouldn't reorganise, but at least stay with a current chain, and the best block of the current chain is oldBlock4
155- blockchain.getBestBlock().get shouldEqual oldBlock4
155+ eventually { blockchain.getBestBlock().get shouldEqual oldBlock4 }
156156 }
157157
158158 it should " return a correct new best block after reorganising longer chain to a shorter one if its weight is bigger" in {
@@ -164,17 +164,16 @@ class BlockImporterItSpec
164164
165165 blockImporter ! BlockFetcher .PickedBlocks (NonEmptyList .fromListUnsafe(newBranch))
166166
167- Thread .sleep(200 )
168- blockchain.getBestBlock().get shouldEqual newBlock3
167+ eventually { Thread .sleep(200 ); blockchain.getBestBlock().get shouldEqual newBlock3 }
169168 }
170169
171170 it should " return Unknown branch, in case of PickedBlocks with block that has a parent that's not in the chain" in {
172- val newcomerBlock4 : Block =
171+ val newBlock4ParentOldBlock3 : Block =
173172 getBlock(genesisBlock.number + 4 , difficulty = 104 , parent = oldBlock3.header.hash)
174- val newcomerWeight4Duplicate = oldWeight3.increase(newcomerBlock4 .header)
173+ val newBlock4WeightParentOldBlock3 = oldWeight3.increase(newBlock4ParentOldBlock3 .header)
175174
176175 // Block n5 with oldBlock4 as parent
177- val newComerBlock5WithOldBlock4Parent : Block =
176+ val newBlock5ParentOldBlock4 : Block =
178177 getBlock(
179178 genesisBlock.number + 5 ,
180179 difficulty = 108 ,
@@ -187,13 +186,12 @@ class BlockImporterItSpec
187186 blockchain.save(oldBlock4, Nil , oldWeight4, saveAsBestBlock = true )
188187 // simulation of node restart
189188 blockchain.saveBestKnownBlocks(blockchain.getBestBlockNumber() - 1 )
190- blockchain.save(newcomerBlock4 , Nil , newcomerWeight4Duplicate , saveAsBestBlock = true )
189+ blockchain.save(newBlock4ParentOldBlock3 , Nil , newBlock4WeightParentOldBlock3 , saveAsBestBlock = true )
191190
192191 // not reorganising anymore until oldBlock4(not part of the chain anymore), no block/ommer validation when not part of the chain, resolveBranch is returning UnknownBranch
193- blockImporter ! BlockFetcher .PickedBlocks (NonEmptyList .fromListUnsafe(List (newComerBlock5WithOldBlock4Parent )))
192+ blockImporter ! BlockFetcher .PickedBlocks (NonEmptyList .fromListUnsafe(List (newBlock5ParentOldBlock4 )))
194193
195- Thread .sleep(200 )
196- blockchain.getBestBlock().get shouldEqual newcomerBlock4
194+ eventually { blockchain.getBestBlock().get shouldEqual newBlock4ParentOldBlock3 }
197195 }
198196
199197 it should " switch to a branch with a checkpoint" in {
@@ -206,9 +204,8 @@ class BlockImporterItSpec
206204
207205 blockImporter ! BlockFetcher .PickedBlocks (NonEmptyList .fromListUnsafe(newBranch))
208206
209- Thread .sleep(200 )
210- blockchain.getBestBlock().get shouldEqual oldBlock5WithCheckpoint
211- blockchain.getLatestCheckpointBlockNumber() shouldEqual oldBlock5WithCheckpoint.header.number
207+ eventually { blockchain.getBestBlock().get shouldEqual oldBlock5WithCheckpoint }
208+ eventually { blockchain.getLatestCheckpointBlockNumber() shouldEqual oldBlock5WithCheckpoint.header.number }
212209 }
213210
214211 it should " switch to a branch with a newer checkpoint" in {
@@ -221,9 +218,8 @@ class BlockImporterItSpec
221218
222219 blockImporter ! BlockFetcher .PickedBlocks (NonEmptyList .fromListUnsafe(newBranch))
223220
224- Thread .sleep(200 )
225- blockchain.getBestBlock().get shouldEqual newBlock4WithCheckpoint
226- blockchain.getLatestCheckpointBlockNumber() shouldEqual newBlock4WithCheckpoint.header.number
221+ eventually { blockchain.getBestBlock().get shouldEqual newBlock4WithCheckpoint }
222+ eventually { blockchain.getLatestCheckpointBlockNumber() shouldEqual newBlock4WithCheckpoint.header.number }
227223 }
228224
229225 it should " return a correct checkpointed block after receiving a request for generating a new checkpoint" in {
@@ -242,8 +238,9 @@ class BlockImporterItSpec
242238
243239 val checkpointBlock = checkpointBlockGenerator.generate(newBlock5, Checkpoint (signatures))
244240
245- Thread .sleep(1000 )
246- blockchain.getBestBlock().get shouldEqual checkpointBlock
247- blockchain.getLatestCheckpointBlockNumber() shouldEqual newBlock5.header.number + 1
241+ eventually { Thread .sleep(1000 ); blockchain.getBestBlock().get shouldEqual checkpointBlock }
242+ eventually {
243+ Thread .sleep(1000 ); blockchain.getLatestCheckpointBlockNumber() shouldEqual newBlock5.header.number + 1
244+ }
248245 }
249246}
0 commit comments