Skip to content

Commit

Permalink
eventually
Browse files Browse the repository at this point in the history
  • Loading branch information
vsuharnikov committed Nov 15, 2024
1 parent 6486b23 commit 462da10
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import units.client.contract.HasConsensusLayerDappTxHelpers.EmptyE2CTransfersRoo
import units.docker.WavesNodeContainer

class AlternativeChainTestSuite extends BaseDockerTestSuite {
private val waitFiveBlocksAtMax = patienceConfig.copy(timeout = WavesNodeContainer.AverageBlockDelay * 5)
private val fiveBlocks = WavesNodeContainer.AverageBlockDelay * 5

"L2-383 Start an alternative chain after not getting an EL-block" in {
step("Wait miner #1 forge at least one block")
def getLastContractBlock = chainContract.getLastBlockMeta(0).getOrElse(fail("Can't get last block"))
retry {
eventually {
getLastContractBlock.height should be > 0L
}

Expand Down Expand Up @@ -44,16 +44,16 @@ class AlternativeChainTestSuite extends BaseDockerTestSuite {
waitMinerEpoch(miner11Account)

step("Checking an alternative chain started")
retry {
chainContract.getChainInfo(1L).getOrElse(fail("Can't get an alternative chain info"))
eventually {
chainContract.getChainInfo(1L) shouldBe defined
}
}

private def waitMinerEpoch(minerAccount: KeyPair): Unit = {
val expectedGenerator = minerAccount.toAddress
retry {
eventually(timeout(fiveBlocks)) {
val actualGenerator = chainContract.computedGenerator
if (actualGenerator != expectedGenerator) fail(s"Expected $expectedGenerator generator, got $actualGenerator")
}(waitFiveBlocksAtMax)
actualGenerator shouldBe expectedGenerator
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.common.utils.EitherExt2
import com.wavesplatform.utils.ScorexLogging
import monix.execution.atomic.AtomicBoolean
import org.scalatest.concurrent.Eventually
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers
import org.scalatest.{BeforeAndAfterAll, EitherValues, OptionValues}
Expand All @@ -16,7 +17,9 @@ import units.docker.*
import units.docker.WavesNodeContainer.generateWavesGenesisConfig
import units.el.ElBridgeClient
import units.eth.Gwei
import units.test.{CustomMatchers, HasRetry, TestEnvironment}
import units.test.{CustomMatchers, TestEnvironment}

import scala.concurrent.duration.DurationInt

trait BaseDockerTestSuite
extends AnyFreeSpec
Expand All @@ -27,9 +30,11 @@ trait BaseDockerTestSuite
with EitherValues
with OptionValues
with ReportingTestName
with HasRetry
with Eventually
with Accounts
with HasConsensusLayerDappTxHelpers {
implicit override def patienceConfig: PatienceConfig = PatienceConfig(timeout = 30.seconds, interval = 1.second)

override val currentHitSource: ByteStr = ByteStr.empty
protected val rewardAmount: Gwei = Gwei.ofRawGwei(2_000_000_000L)

Expand Down Expand Up @@ -81,7 +86,7 @@ trait BaseDockerTestSuite
waves1.api.broadcastAndWait(ChainContract.setScript())

log.info("Setup chain contract")
val genesisBlock = ec1.engineApi.getBlockByNumber(BlockNumber.Number(0)).explicitGet().getOrElse(failRetry("No EL genesis block"))
val genesisBlock = ec1.engineApi.getBlockByNumber(BlockNumber.Number(0)).explicitGet().getOrElse(fail("No EL genesis block"))
waves1.api.broadcastAndWait(
ChainContract.setup(
genesisBlock = genesisBlock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class BridgeC2ETestSuite extends BaseDockerTestSuite {
val withdrawal = Iterator
.from(elCurrHeight + 1)
.map { h =>
retry {
ec1.engineApi.getBlockByNumber(BlockNumber.Number(h)).toOption.flatten.getOrElse(fail(s"Can't get EL block #$h"))
eventually {
ec1.engineApi.getBlockByNumber(BlockNumber.Number(h)).toOption.flatten.value
}
}
.flatMap(_.withdrawals)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ class BridgeE2CTestSuite extends BaseDockerTestSuite {
val transferProofs = Bridge.mkTransferProofs(transferEvents, sendTxnLogIndex).reverse

step(s"Wait block $blockHash on contract")
val blockConfirmationHeight = retry {
chainContract.getBlock(blockHash).get.height
val blockConfirmationHeight = eventually {
chainContract.getBlock(blockHash).value.height
}

step(s"Wait block $blockHash ($blockConfirmationHeight) finalization")
retry {
eventually {
val currFinalizedHeight = chainContract.getFinalizedBlock.height
step(s"Current finalized height: $currFinalizedHeight")
if (currFinalizedHeight < blockConfirmationHeight) fail("Not yet finalized")
currFinalizedHeight should be >= blockConfirmationHeight
}

withClue("3. Tokens received: ") {
Expand Down
17 changes: 8 additions & 9 deletions consensus-client-it/src/test/scala/units/RewardTestSuite.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package units

import com.wavesplatform.common.utils.EitherExt2
import units.client.engine.model.BlockNumber

class RewardTestSuite extends BaseDockerTestSuite {
"L2-234 The reward for a previous epoch is in the first block withdrawals" in {
val epoch1FirstEcBlock = retry {
ec1.engineApi.getBlockByNumber(BlockNumber.Number(1)).explicitGet().get
val epoch1FirstEcBlock = eventually {
ec1.engineApi.getBlockByNumber(BlockNumber.Number(1)).value.value
}

withClue("No reward for genesis block: ") {
epoch1FirstEcBlock.withdrawals shouldBe empty
}

val epoch1FirstContractBlock = retry {
chainContract.getBlock(epoch1FirstEcBlock.hash).getOrElse(failRetry(s"No first block ${epoch1FirstEcBlock.hash} confirmation"))
val epoch1FirstContractBlock = eventually {
chainContract.getBlock(epoch1FirstEcBlock.hash).value
}

val epoch1Number = epoch1FirstContractBlock.epoch
Expand All @@ -23,14 +22,14 @@ class RewardTestSuite extends BaseDockerTestSuite {
waves1.api.waitForHeight(epoch2Number)

step(s"Wait for epoch #$epoch2Number data on chain contract")
val epoch2FirstContractBlock = retry {
chainContract.getEpochFirstBlock(epoch2Number).get
val epoch2FirstContractBlock = eventually {
chainContract.getEpochFirstBlock(epoch2Number).value
}

val epoch2FirstEcBlock = ec1.engineApi
.getBlockByHash(epoch2FirstContractBlock.hash)
.explicitGet()
.getOrElse(failRetry(s"Can't find ${epoch2FirstContractBlock.hash}"))
.value
.value

epoch2FirstEcBlock.withdrawals should have length 1

Expand Down
15 changes: 8 additions & 7 deletions consensus-client-it/src/test/scala/units/SyncingTestSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.web3j.utils.Convert
import units.docker.EcContainer

import java.math.BigInteger
import scala.jdk.OptionConverters.RichOptional

class SyncingTestSuite extends BaseDockerTestSuite {
private val elSender = elRichAccount1
Expand All @@ -34,8 +35,8 @@ class SyncingTestSuite extends BaseDockerTestSuite {
val blocksWithTxns = List(txn2Receipt, txn3Receipt).map(x => x.getBlockNumber -> x.getBlockHash).toMap
step(s"Waiting blocks ${blocksWithTxns.mkString(", ")} on contract")
blocksWithTxns.foreach { case (_, blockHash) =>
retry {
chainContract.getBlock(BlockHash(blockHash)).get
eventually {
chainContract.getBlock(BlockHash(blockHash)).value
}
}

Expand All @@ -47,13 +48,13 @@ class SyncingTestSuite extends BaseDockerTestSuite {

step(s"Waiting blocks ${blocksWithTxns.mkString(", ")} disappear")
blocksWithTxns.foreach { case (_, blockHash) =>
retry {
if (chainContract.getBlock(BlockHash(blockHash)).nonEmpty) throw new RuntimeException(s"Expected $blockHash to disappear")
eventually {
chainContract.getBlock(BlockHash(blockHash)) shouldBe empty
}
}

blocksWithTxns.foreach { case (height, blockHash) =>
retry {
eventually {
val block = ec1.web3j.ethGetBlockByNumber(DefaultBlockParameter.valueOf(height), false).send().getBlock
block.getHash shouldNot be(blockHash)
}
Expand All @@ -78,7 +79,7 @@ class SyncingTestSuite extends BaseDockerTestSuite {
ec1.web3j.ethSendRawTransaction(signedTransaction).send()
}

private def waitForTxn(txnResult: EthSendTransaction): TransactionReceipt = retry {
ec1.web3j.ethGetTransactionReceipt(txnResult.getTransactionHash).send().getTransactionReceipt.get()
private def waitForTxn(txnResult: EthSendTransaction): TransactionReceipt = eventually {
ec1.web3j.ethGetTransactionReceipt(txnResult.getTransactionHash).send().getTransactionReceipt.toScala.value
}
}

0 comments on commit 462da10

Please sign in to comment.