Skip to content

Commit

Permalink
Generate genesis each test task run
Browse files Browse the repository at this point in the history
  • Loading branch information
vsuharnikov committed Oct 30, 2024
1 parent 6c36762 commit c9eb84c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 24 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/check-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ jobs:
- uses: sbt/setup-sbt@v1
- name: Run tests
run: |
sbt --batch "\
test; \
consensus-client-it/Test/updateGenesis; \
docker; \
consensus-client-it/test"
sbt --batch "test;docker;consensus-client-it/test"
- name: Archive logs
uses: actions/upload-artifact@v4
if: always()
Expand Down
10 changes: 1 addition & 9 deletions consensus-client-it/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ libraryDependencies ++= Seq(
).map(_ % Test)

val logsDirectory = taskKey[File]("The directory for logs") // Evaluates every time, so it recreates the logs directory
val updateGenesis = taskKey[Unit]("Updates the genesis.json file in local-network")

inConfig(Test)(
Seq(
Expand Down Expand Up @@ -57,13 +56,6 @@ inConfig(Test)(
)
)
}
},
updateGenesis := Def.taskDyn {
val mainClass = "com.wavesplatform.GenesisBlockGenerator"
val wavesNodeConfigsDir = baseDirectory.value.getParentFile / "local-network" / "configs" / "wavesnode"
val templateFile = wavesNodeConfigsDir / "genesis-template.conf"
val outputFile = wavesNodeConfigsDir / "genesis.conf"
(Test / runMain).toTask(s" $mainClass $templateFile $outputFile")
}.value
}
)
)
38 changes: 33 additions & 5 deletions consensus-client-it/src/test/scala/units/BaseItTestSuite.scala
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
package units

import com.google.common.primitives.{Bytes, Ints}
import com.typesafe.config.ConfigFactory
import com.wavesplatform.account.{AddressScheme, KeyPair, SeedKeyPair}
import com.wavesplatform.api.HasRetry
import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.crypto
import com.wavesplatform.utils.ScorexLogging
import monix.execution.atomic.AtomicBoolean
import com.wavesplatform.{GenesisBlockGenerator, crypto}
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers
import org.scalatest.{BeforeAndAfterAll, EitherValues, OptionValues}
import org.web3j.crypto.Credentials
import units.client.contract.HasConsensusLayerDappTxHelpers
import units.docker.BaseContainer.ConfigsDir
import units.docker.Networks
import units.eth.{EthAddress, Gwei}
import units.test.CustomMatchers

import java.io.PrintStream
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import scala.concurrent.duration.DurationInt

trait BaseItTestSuite
extends AnyFreeSpec
Expand Down Expand Up @@ -72,11 +76,35 @@ trait BaseItTestSuite
}

object BaseItTestSuite {
private val initialized = AtomicBoolean(false)
private var initialized = false

def init(): Unit =
if (initialized.compareAndSet(expect = false, update = true))
def init(): Unit = synchronized {
if (!initialized) {
AddressScheme.current = new AddressScheme {
override val chainId: Byte = 'D'.toByte
}

val templateFile = ConfigsDir.resolve("wavesnode/genesis-template.conf").toAbsolutePath
val genesisFile = ConfigsDir.resolve("wavesnode/genesis.conf").toAbsolutePath

val origConfig = ConfigFactory.parseFile(templateFile.toFile)
val gap = 1.minute // To force node mining at start, otherwise it schedules
val overrides = ConfigFactory.parseString(
s"""genesis-generator {
| timestamp = ${System.currentTimeMillis() - gap.toMillis}
|}""".stripMargin
)

val genesisSettings = GenesisBlockGenerator.parseSettings(overrides.withFallback(origConfig))

val origOut = System.out
System.setOut(new PrintStream({ (_: Int) => }))
val config = GenesisBlockGenerator.createConfig(genesisSettings)
System.setOut(origOut)

Files.writeString(genesisFile, config)

initialized = true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class RewardTestSuite extends OneNodeTestSuite {
val epoch1Number = epoch1FirstContractBlock.epoch
val epoch2Number = epoch1Number + 1

log.info(s"Wait for next epoch #$epoch2Number")
waves1.api.waitForHeight(epoch2Number)

log.info(s"Wait for epoch #$epoch2Number data on chain contract")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ abstract class BaseContainer(val hostName: String) {
}

object BaseContainer {
val ConfigsDir: Path = Path.of(System.getProperty("cc.it.configs.dir"))
val DefaultLogsDir: Path = Path.of(System.getProperty("cc.it.logs.dir"))
val ConfigsDir: Path = Path.of(System.getProperty("cc.it.configs.dir"))
val DefaultLogsDir: Path = Path.of(System.getProperty("cc.it.logs.dir"))
val WavesDockerImage: String = System.getProperty("cc.it.docker.image")
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.testcontainers.containers.Network.NetworkImpl
import org.testcontainers.utility.DockerImageName
import sttp.client3.{HttpClientSyncBackend, UriContext}
import units.client.HttpChainContractClient
import units.docker.BaseContainer.{ConfigsDir, DefaultLogsDir}
import units.docker.BaseContainer.*
import units.docker.WavesNodeContainer.ApiPort

import java.io.File
Expand All @@ -27,7 +27,7 @@ class WavesNodeContainer(
private val logFile = new File(s"$DefaultLogsDir/waves-$number.log")
Files.touch(logFile)

protected override val container = new GenericContainer(DockerImageName.parse(System.getProperty("cc.it.docker.image")))
protected override val container = new GenericContainer(DockerImageName.parse(WavesDockerImage))
.withNetwork(network)
.withExposedPorts(ApiPort)
.withEnv(
Expand Down

0 comments on commit c9eb84c

Please sign in to comment.