Skip to content

Commit 6b3202f

Browse files
Leonor BogaAurélien Richez
Leonor Boga
authored and
Aurélien Richez
committed
[cherry pick from ETCM-1045] Fix Blockchain test about rolling back blocks
1 parent 2d1d1f3 commit 6b3202f

File tree

2 files changed

+5
-31
lines changed

2 files changed

+5
-31
lines changed

src/test/scala/io/iohk/ethereum/ObjectGenerators.scala

-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ trait ObjectGenerators {
3333

3434
def intGen(min: Int, max: Int): Gen[Int] = Gen.choose(min, max)
3535

36-
def posIntGen(min: Int, max: Int): Gen[Int] = Gen.choose(min, max).suchThat(_ > 0)
37-
3836
def intGen: Gen[Int] = Gen.choose(Int.MinValue, Int.MaxValue)
3937

4038
def longGen: Gen[Long] = Gen.choose(Long.MinValue, Long.MaxValue)

src/test/scala/io/iohk/ethereum/domain/BlockchainSpec.scala

+5-29
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ class BlockchainSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyCh
214214
}
215215

216216
it should "return correct best block number after saving and rolling back blocks" in new TestSetup {
217-
forAll(posIntGen(min = 1, max = maxNumberBlocksToImport)) { numberBlocksToImport =>
217+
forAll(intGen(min = 1, max = maxNumberBlocksToImport)) { numberBlocksToImport =>
218218
val testSetup = newSetup()
219219
import testSetup._
220220

@@ -237,8 +237,9 @@ class BlockchainSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyCh
237237
blockchainReaderWithStubPersisting.getBestBlockNumber() shouldBe blocksToImport.last.number
238238

239239
// Rollback blocks
240-
val numberBlocksToRollback = intGen(0, numberBlocksToImport).sample.get
241-
val (_, blocksToRollback) = blocksToImport.splitAt(numberBlocksToRollback)
240+
val numberBlocksToKeep = intGen(0, numberBlocksToImport).sample.get
241+
242+
val (_, blocksToRollback) = blocksToImport.splitAt(numberBlocksToKeep)
242243

243244
// Randomly select the block rollback to persist (empty means no persistence)
244245
val blockRollbackToPersist =
@@ -254,38 +255,13 @@ class BlockchainSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyCh
254255
blockchainWithStubPersisting.removeBlock(block.hash)
255256
}
256257

257-
val expectedPersistedBestBlock = calculatePersistedBestBlock(
258-
blockImportToPersist.map(_.number),
259-
blockRollbackToPersist.map(_.number),
260-
blocksToRollback.map(_.number)
261-
)
262-
blockchainReaderWithStubPersisting.getBestBlockNumber() shouldBe expectedPersistedBestBlock
258+
blockchainReaderWithStubPersisting.getBestBlockNumber() shouldBe numberBlocksToKeep
263259
}
264260
}
265261

266262
trait TestSetup extends MockFactory {
267263
val maxNumberBlocksToImport: Int = 30
268264

269-
def calculatePersistedBestBlock(
270-
blockImportPersisted: Option[BigInt],
271-
blockRollbackPersisted: Option[BigInt],
272-
blocksRolledback: Seq[BigInt]
273-
): BigInt =
274-
(blocksRolledback, blockImportPersisted) match {
275-
case (Nil, Some(bi)) =>
276-
// No blocks rolledback, last persist was the persist during import
277-
bi
278-
case (nonEmptyRolledbackBlocks, Some(bi)) =>
279-
// Last forced persist during apply/rollback
280-
val maxForcedPersist = blockRollbackPersisted.fold(bi)(br => (br - 1).max(bi))
281-
282-
// The above number would have been decreased by any rollbacked blocks
283-
(nonEmptyRolledbackBlocks.head - 1).min(maxForcedPersist)
284-
case (_, None) =>
285-
// If persisted rollback, then it was decreased by the future rollbacks, if not no persistance was ever done
286-
blockRollbackPersisted.fold(0: BigInt)(_ => blocksRolledback.head - 1)
287-
}
288-
289265
trait StubPersistingBlockchainSetup {
290266
def stubStateStorage: StateStorage
291267
def blockchainStoragesWithStubPersisting: BlockchainStorages

0 commit comments

Comments
 (0)