Skip to content

Commit

Permalink
[cherry pick from ETCM-1045] Fix Blockchain test about rolling back b…
Browse files Browse the repository at this point in the history
…locks
  • Loading branch information
Leonor Boga authored and Aurélien Richez committed Aug 11, 2021
1 parent 2d1d1f3 commit 6b3202f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 31 deletions.
2 changes: 0 additions & 2 deletions src/test/scala/io/iohk/ethereum/ObjectGenerators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ trait ObjectGenerators {

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

def posIntGen(min: Int, max: Int): Gen[Int] = Gen.choose(min, max).suchThat(_ > 0)

def intGen: Gen[Int] = Gen.choose(Int.MinValue, Int.MaxValue)

def longGen: Gen[Long] = Gen.choose(Long.MinValue, Long.MaxValue)
Expand Down
34 changes: 5 additions & 29 deletions src/test/scala/io/iohk/ethereum/domain/BlockchainSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class BlockchainSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyCh
}

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

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

// Rollback blocks
val numberBlocksToRollback = intGen(0, numberBlocksToImport).sample.get
val (_, blocksToRollback) = blocksToImport.splitAt(numberBlocksToRollback)
val numberBlocksToKeep = intGen(0, numberBlocksToImport).sample.get

val (_, blocksToRollback) = blocksToImport.splitAt(numberBlocksToKeep)

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

val expectedPersistedBestBlock = calculatePersistedBestBlock(
blockImportToPersist.map(_.number),
blockRollbackToPersist.map(_.number),
blocksToRollback.map(_.number)
)
blockchainReaderWithStubPersisting.getBestBlockNumber() shouldBe expectedPersistedBestBlock
blockchainReaderWithStubPersisting.getBestBlockNumber() shouldBe numberBlocksToKeep
}
}

trait TestSetup extends MockFactory {
val maxNumberBlocksToImport: Int = 30

def calculatePersistedBestBlock(
blockImportPersisted: Option[BigInt],
blockRollbackPersisted: Option[BigInt],
blocksRolledback: Seq[BigInt]
): BigInt =
(blocksRolledback, blockImportPersisted) match {
case (Nil, Some(bi)) =>
// No blocks rolledback, last persist was the persist during import
bi
case (nonEmptyRolledbackBlocks, Some(bi)) =>
// Last forced persist during apply/rollback
val maxForcedPersist = blockRollbackPersisted.fold(bi)(br => (br - 1).max(bi))

// The above number would have been decreased by any rollbacked blocks
(nonEmptyRolledbackBlocks.head - 1).min(maxForcedPersist)
case (_, None) =>
// If persisted rollback, then it was decreased by the future rollbacks, if not no persistance was ever done
blockRollbackPersisted.fold(0: BigInt)(_ => blocksRolledback.head - 1)
}

trait StubPersistingBlockchainSetup {
def stubStateStorage: StateStorage
def blockchainStoragesWithStubPersisting: BlockchainStorages
Expand Down

0 comments on commit 6b3202f

Please sign in to comment.