Skip to content

Commit

Permalink
Generate waves genesis block for each test suite to have a stable min…
Browse files Browse the repository at this point in the history
…ing from the start
  • Loading branch information
vsuharnikov committed Oct 31, 2024
1 parent 1f103a9 commit 2131a80
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 30 deletions.
49 changes: 26 additions & 23 deletions consensus-client-it/src/test/scala/units/BaseItTestSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ import com.wavesplatform.api.HasRetry
import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.utils.ScorexLogging
import com.wavesplatform.{GenesisBlockGenerator, crypto}
import monix.execution.atomic.AtomicBoolean
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers
import org.scalatest.{BeforeAndAfterAll, EitherValues, OptionValues}
import org.web3j.crypto.Credentials
import units.BaseItTestSuite.generateWavesGenesisConfig
import units.client.contract.HasConsensusLayerDappTxHelpers
import units.docker.BaseContainer.ConfigsDir
import units.docker.BaseContainer.{ConfigsDir, DefaultLogsDir}
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 java.nio.file.{Files, Path}
import scala.concurrent.duration.DurationInt

trait BaseItTestSuite
Expand Down Expand Up @@ -50,6 +52,8 @@ trait BaseItTestSuite
protected val elRichAccount1 = Credentials.create("8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63")
protected val elRichAccount2 = Credentials.create("ae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f")

protected lazy val wavesGenesisConfigPath = generateWavesGenesisConfig()

protected def startNodes(): Unit

protected def stopNodes(): Unit
Expand All @@ -76,35 +80,34 @@ trait BaseItTestSuite
}

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

def init(): Unit = synchronized {
if (!initialized) {
def init(): Unit =
if (initialized.compareAndSet(expect = false, update = true))
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
)
def generateWavesGenesisConfig(): Path = {
val templateFile = ConfigsDir.resolve("wavesnode/genesis-template.conf").toAbsolutePath

val genesisSettings = GenesisBlockGenerator.parseSettings(overrides.withFallback(origConfig))
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 origOut = System.out
System.setOut(new PrintStream({ (_: Int) => }))
val config = GenesisBlockGenerator.createConfig(genesisSettings)
System.setOut(origOut)
val genesisSettings = GenesisBlockGenerator.parseSettings(overrides.withFallback(origConfig))

Files.writeString(genesisFile, config)
val origOut = System.out
System.setOut(new PrintStream({ (_: Int) => })) // We don't use System.out in tests, so it should not be an issue
val config = GenesisBlockGenerator.createConfig(genesisSettings)
System.setOut(origOut)

initialized = true
}
val dest = DefaultLogsDir.resolve("genesis.conf").toAbsolutePath
Files.writeString(dest, config)
dest
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ trait OneNodeTestSuite extends BaseItTestSuite {
ip = Networks.ipForNode(3),
baseSeed = "devnet-1",
chainContractAddress = chainContractAddress,
ecEngineApiUrl = s"http://${ec1.hostName}:${EcContainer.EnginePort}"
ecEngineApiUrl = s"http://${ec1.hostName}:${EcContainer.EnginePort}",
genesisConfigPath = wavesGenesisConfigPath
)

override protected def startNodes(): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ trait TwoNodesTestSuite extends BaseItTestSuite {
ip = Networks.ipForNode(4),
baseSeed = "devnet-1",
chainContractAddress = chainContractAddress,
ecEngineApiUrl = s"http://${ec1.hostName}:${EcContainer.EnginePort}"
ecEngineApiUrl = s"http://${ec1.hostName}:${EcContainer.EnginePort}",
genesisConfigPath = wavesGenesisConfigPath
)

protected lazy val waves2: WavesNodeContainer = new WavesNodeContainer(
Expand All @@ -32,7 +33,8 @@ trait TwoNodesTestSuite extends BaseItTestSuite {
ip = Networks.ipForNode(5),
baseSeed = "devnet-2",
chainContractAddress = chainContractAddress,
ecEngineApiUrl = s"http://${ec2.hostName}:${EcContainer.EnginePort}"
ecEngineApiUrl = s"http://${ec2.hostName}:${EcContainer.EnginePort}",
genesisConfigPath = wavesGenesisConfigPath
)

override protected def startNodes(): Unit = {
Expand Down Expand Up @@ -110,4 +112,5 @@ trait TwoNodesTestSuite extends BaseItTestSuite {
log.info(s"Wait for #$epoch1Number epoch")
waves1.api.waitForHeight(epoch1Number)
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package units.docker

import com.google.common.io.Files
import com.google.common.io.Files as GFiles
import com.wavesplatform.account.Address
import com.wavesplatform.api.{LoggingBackend, NodeHttpApi}
import com.wavesplatform.common.utils.Base58
Expand All @@ -10,10 +10,11 @@ import org.testcontainers.utility.DockerImageName
import sttp.client3.{HttpClientSyncBackend, UriContext}
import units.client.HttpChainContractClient
import units.docker.BaseContainer.*
import units.docker.WavesNodeContainer.ApiPort
import units.docker.WavesNodeContainer.*

import java.io.File
import java.nio.charset.StandardCharsets
import java.nio.file.Path
import scala.jdk.CollectionConverters.MapHasAsJava

class WavesNodeContainer(
Expand All @@ -22,10 +23,11 @@ class WavesNodeContainer(
ip: String,
baseSeed: String,
chainContractAddress: Address,
ecEngineApiUrl: String
ecEngineApiUrl: String,
genesisConfigPath: Path
) extends BaseContainer(s"wavesnode-$number") {
private val logFile = new File(s"$DefaultLogsDir/waves-$number.log")
Files.touch(logFile)
GFiles.touch(logFile)

protected override val container = new GenericContainer(DockerImageName.parse(WavesDockerImage))
.withNetwork(network)
Expand All @@ -46,6 +48,7 @@ class WavesNodeContainer(
)
.withFileSystemBind(s"$ConfigsDir/wavesnode", "/etc/waves", BindMode.READ_ONLY)
.withFileSystemBind(s"$ConfigsDir/ec-common", "/etc/secrets", BindMode.READ_ONLY)
.withFileSystemBind(s"$genesisConfigPath", "/etc/it/genesis.conf", BindMode.READ_ONLY)
.withFileSystemBind(s"$logFile", "/var/log/waves/waves.log", BindMode.READ_WRITE)
.withCreateContainerCmdModifier { cmd =>
cmd
Expand Down Expand Up @@ -74,4 +77,5 @@ class WavesNodeContainer(

object WavesNodeContainer {
val ApiPort = 6869

}
1 change: 1 addition & 0 deletions local-network/configs/wavesnode/waves.conf
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ waves {
voting-interval = 1
}
include "genesis.conf"
include "/etc/it/genesis.conf"
}
}

Expand Down

0 comments on commit 2131a80

Please sign in to comment.