Skip to content

Commit

Permalink
fixed node-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
phearnot committed Jan 29, 2025
1 parent 7f79678 commit 56801b9
Show file tree
Hide file tree
Showing 16 changed files with 205 additions and 249 deletions.
8 changes: 2 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,8 @@ lazy val `grpc-server` =
// lazy val `ride-runner` = project.dependsOn(node, `grpc-server`, `node-tests` % "test->test")
lazy val `node-it` = project.dependsOn(`repl-jvm`, `grpc-server`, `lang-testkit` % "test->test", `node-testkit`)

// TODO: [scala3] enable
// lazy val `node-generator` = project
// .dependsOn(node, `node-testkit`, `node-tests` % "compile->test")
// .settings(
// libraryDependencies += "com.iheart" %% "ficus" % "1.5.2"
// )
lazy val `node-generator` = project.dependsOn(node, `node-testkit`, `node-testkit`)

lazy val benchmark = project.dependsOn(node, `node-tests` % "test->test")

lazy val repl = crossProject(JSPlatform, JVMPlatform)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.wavesplatform.generator

import java.util.concurrent.atomic.AtomicReference

import cats.Show
import com.wavesplatform.account.KeyPair
import com.wavesplatform.generator.DynamicWideTransactionGenerator.Settings
import com.wavesplatform.generator.utils.Gen
import com.wavesplatform.transaction.Transaction
import pureconfig.ConfigReader

import java.util.concurrent.atomic.AtomicReference

class DynamicWideTransactionGenerator(settings: Settings, accounts: Seq[KeyPair]) extends TransactionGenerator {
require(accounts.nonEmpty)
Expand All @@ -28,13 +29,13 @@ class DynamicWideTransactionGenerator(settings: Settings, accounts: Seq[KeyPair]

object DynamicWideTransactionGenerator {

case class Settings(start: Int, growAdder: Double, maxTxsPerRequest: Option[Int], limitDestAccounts: Option[Int], minFee: Long, maxFee: Long) {
case class Settings(start: Int, growAdder: Double, maxTxsPerRequest: Option[Int], limitDestAccounts: Option[Int], minFee: Long, maxFee: Long)derives ConfigReader {
require(start >= 1)
}

object Settings {
implicit val toPrintable: Show[Settings] = { x =>
import x._
import x.*
s"""txs at start: $start
|grow adder: $growAdder
|max txs: $maxTxsPerRequest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,50 @@
package com.wavesplatform.generator

import java.net.{InetSocketAddress, URL}
import java.nio.charset.StandardCharsets
import cats.Show
import cats.implicits.showInterpolator
import com.google.common.primitives.{Bytes, Ints}
import com.wavesplatform.account.{KeyPair, SeedKeyPair}
import com.wavesplatform.generator.GeneratorSettings.NodeAddress
import com.wavesplatform.generator.config.FicusImplicits
import com.wavesplatform.settings.*
import pureconfig.ConfigReader
import pureconfig.generic.derivation.*

import java.net.{InetSocketAddress, URI, URL}
import java.nio.charset.StandardCharsets
import scala.util.Try

case class GeneratorSettings(
chainId: String,
accounts: Seq[String],
sendTo: Seq[NodeAddress],
worker: Worker.Settings,
mode: Mode.Value,
mode: Mode,
narrow: NarrowTransactionGenerator.Settings,
wide: WideTransactionGenerator.Settings,
dynWide: DynamicWideTransactionGenerator.Settings,
multisig: MultisigTransactionGenerator.Settings,
oracle: OracleTransactionGenerator.Settings,
swarm: SmartGenerator.Settings
) {
)derives ConfigReader {
val addressScheme: Char = chainId.head
val privateKeyAccounts: Seq[SeedKeyPair] = accounts.map(s => GeneratorSettings.toKeyPair(s))
}

object GeneratorSettings {
case class NodeAddress(networkAddress: InetSocketAddress, apiAddress: URL)
object GeneratorSettings extends FicusImplicits {
given ConfigReader[InetSocketAddress] = ConfigReader.fromStringTry(str =>
Try {
val url = new URI(s"my://$str")
new InetSocketAddress(url.getHost, url.getPort)
}
)

given ConfigReader[URL] = ConfigReader[String].map(str => new URL(str))

case class NodeAddress(networkAddress: InetSocketAddress, apiAddress: URL)derives ConfigReader

implicit val toPrintable: Show[GeneratorSettings] = { x =>
import x._
import x.*

val modeSettings: String = (mode: @unchecked) match {
case Mode.NARROW => show"$narrow"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.wavesplatform.generator

object Mode extends Enumeration {
type Mode = Value
val WIDE, NARROW, DYN_WIDE, MULTISIG, ORACLE, SWARM = Value
import pureconfig.generic.derivation.EnumConfigReader

enum Mode derives EnumConfigReader {
case WIDE, NARROW, DYN_WIDE, MULTISIG, ORACLE, SWARM
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import cats.Show
import com.wavesplatform.account.KeyPair
import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.common.utils.EitherExt2
import com.wavesplatform.common.utils.EitherExt2.explicitGet
import com.wavesplatform.crypto
import com.wavesplatform.generator.utils.Gen
import com.wavesplatform.generator.utils.Implicits.DoubleExt
Expand All @@ -13,6 +14,7 @@ import com.wavesplatform.transaction.Asset.Waves
import com.wavesplatform.transaction.smart.SetScriptTransaction
import com.wavesplatform.transaction.transfer.TransferTransaction
import com.wavesplatform.transaction.{Proofs, Transaction, TxPositiveAmount}
import pureconfig.ConfigReader

import scala.util.Random

Expand Down Expand Up @@ -67,7 +69,7 @@ class MultisigTransactionGenerator(settings: MultisigTransactionGenerator.Settin
}

object MultisigTransactionGenerator {
final case class Settings(transactions: Int, firstRun: Boolean)
final case class Settings(transactions: Int, firstRun: Boolean)derives ConfigReader

object Settings {
implicit val toPrintable: Show[Settings] = { x =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
package com.wavesplatform.generator

import java.nio.file.{Files, Paths}
import java.util.UUID
import java.util.concurrent.ThreadLocalRandom
import cats.Show
import com.wavesplatform.account.{KeyPair, SeedKeyPair}
import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.common.utils.EitherExt2.explicitGet
import com.wavesplatform.common.utils.{Base58, EitherExt2}
import com.wavesplatform.generator.config.FicusImplicits
import com.wavesplatform.generator.utils.{Gen, Universe}
import com.wavesplatform.lang.ValidationError
import com.wavesplatform.lang.v1.FunctionHeader
import com.wavesplatform.lang.v1.compiler.Terms
import com.wavesplatform.lang.v1.estimator.ScriptEstimator
import com.wavesplatform.state.DataEntry.{MaxValueSize, Type}
import com.wavesplatform.state.{BinaryDataEntry, BooleanDataEntry, IntegerDataEntry, StringDataEntry}
import com.wavesplatform.transaction.*
import com.wavesplatform.transaction.Asset.{IssuedAsset, Waves}
import com.wavesplatform.transaction.TransactionType.TransactionType
import com.wavesplatform.transaction.*
import com.wavesplatform.transaction.assets.*
import com.wavesplatform.transaction.assets.exchange.*
import com.wavesplatform.transaction.lease.{LeaseCancelTransaction, LeaseTransaction}
import com.wavesplatform.transaction.smart.script.ScriptCompiler
import com.wavesplatform.transaction.smart.{InvokeScriptTransaction, SetScriptTransaction}
import com.wavesplatform.transaction.transfer.MassTransferTransaction.ParsedTransfer
import com.wavesplatform.transaction.transfer.*
import com.wavesplatform.transaction.transfer.MassTransferTransaction.ParsedTransfer
import com.wavesplatform.transaction.utils.Signed
import com.wavesplatform.utils.{LoggerFacade, NTP}
import org.slf4j.LoggerFactory
import org.web3j.crypto.Bip32ECKeyPair
import pureconfig.ConfigReader

import java.nio.file.{Files, Paths}
import java.util.UUID
import java.util.concurrent.ThreadLocalRandom
import scala.concurrent.duration.*
import scala.reflect.ClassTag
import scala.util.Random
import scala.util.Random.*

Expand All @@ -44,16 +48,16 @@ class NarrowTransactionGenerator(
) extends TransactionGenerator {
import NarrowTransactionGenerator.*

private[this] val log = LoggerFacade(LoggerFactory.getLogger(getClass))
private[this] val typeGen = DistributedRandomGenerator(settings.probabilities)
private val log = LoggerFacade(LoggerFactory.getLogger(getClass))
private val typeGen = DistributedRandomGenerator(settings.probabilities)

private[this] def correctVersion(v: TxVersion): TxVersion =
private def correctVersion(v: TxVersion): TxVersion =
if (settings.protobuf) (v + 1).toByte
else v

override def next(): Iterator[Transaction] = generate(settings.transactions).iterator

private[this] def generate(n: Int): Seq[Transaction] = {
private def generate(n: Int): Seq[Transaction] = {
val now = System.currentTimeMillis()

val generated = (0 until (n * 1.2).toInt).foldLeft(
Expand Down Expand Up @@ -434,7 +438,7 @@ class NarrowTransactionGenerator(
else ByteStr(Array.fill(random.nextInt(100))(random.nextInt().toByte))
}

private[this] def logOption[T <: Transaction](txE: Either[ValidationError, T])(implicit m: Manifest[T]): Option[T] = {
private def logOption[T <: Transaction](txE: Either[ValidationError, T])(implicit m: ClassTag[T]): Option[T] = {
txE match {
case Left(e) =>
log.warn(s"${m.runtimeClass.getName}: ${e.toString}")
Expand All @@ -443,12 +447,12 @@ class NarrowTransactionGenerator(
}
}

private[this] def accountByAddress(address: String): Option[KeyPair] =
private def accountByAddress(address: String): Option[KeyPair] =
accounts
.find(_.toAddress.toString == address)
.orElse(Universe.Accounts.map(_.keyPair).find(_.toAddress.toString == address))

private[this] def randomSenderAndAsset(issueTxs: Seq[IssueTransaction]): Option[(KeyPair, Option[ByteStr])] =
private def randomSenderAndAsset(issueTxs: Seq[IssueTransaction]): Option[(KeyPair, Option[ByteStr])] =
if (random.nextBoolean()) {
(randomFrom(issueTxs) orElse randomFrom(Universe.IssuedAssets)).map { issue =>
val pk = (accounts ++ Universe.Accounts.map(_.keyPair)).find(_.publicKey == issue.sender).get
Expand All @@ -467,33 +471,34 @@ class NarrowTransactionGenerator(
}
}

object NarrowTransactionGenerator {
object NarrowTransactionGenerator extends FicusImplicits {

final case class ScriptSettings(
dappAccount: String,
paymentAssets: Set[String],
functions: Seq[ScriptSettings.Function],
scriptFile: Option[String]
) {
)derives ConfigReader {
def dappAccountKP = GeneratorSettings.toKeyPair(dappAccount)
def dappAddress = dappAccountKP.toAddress
}
object ScriptSettings {
final case class Function(name: String, args: Seq[Function.Arg])
final case class Function(name: String, args: Seq[Function.Arg])derives ConfigReader
object Function {
final case class Arg(`type`: String, value: String)
final case class Arg(`type`: String, value: String)derives ConfigReader
}
}

final case class SetScriptSettings(
richAccount: String,
accounts: SetScriptSettings.Accounts,
assets: SetScriptSettings.Assets
)
)derives ConfigReader

object SetScriptSettings {
final case class Accounts(balance: Long, scriptFile: String, repeat: Int)
final case class Accounts(balance: Long, scriptFile: String, repeat: Int)derives ConfigReader
final case class Assets(description: String, amount: Long, decimals: Int, reissuable: Boolean, scriptFile: String, repeat: Int)
derives ConfigReader
}

final case class Settings(
Expand All @@ -502,11 +507,11 @@ object NarrowTransactionGenerator {
scripts: Seq[ScriptSettings],
setScript: Option[SetScriptSettings],
protobuf: Boolean
)
)derives ConfigReader

object Settings {
implicit val toPrintable: Show[Settings] = { x =>
import x._
import x.*
s"""transactions per iteration: $transactions
|probabilities:
| ${probabilities.mkString("\n ")}""".stripMargin
Expand Down Expand Up @@ -556,7 +561,7 @@ object NarrowTransactionGenerator {
val (accountInitTxs, accountTailInitTxs, accounts) =
((1 to accountsSettings.repeat) foldLeft ((Seq.empty[Transaction], Seq.empty[Transaction], Seq.empty[KeyPair]))) {
case ((initTxs, tailInitTxs, accounts), _) =>
import accountsSettings._
import accountsSettings.*

val account = GeneratorSettings.toKeyPair(s"${UUID.randomUUID().toString}")

Expand All @@ -573,7 +578,7 @@ object NarrowTransactionGenerator {
val assetTailInitTxs =
if (settings.probabilities.keySet.contains(TransactionType.SetAssetScript))
((1 to assetsSettings.repeat) foldLeft Seq.empty[IssueTransaction]) { case (txs, i) =>
import assetsSettings._
import assetsSettings.*

val issuer = randomFrom(accounts).get
val script = ScriptCompiler.compile(new String(Files.readAllBytes(Paths.get(scriptFile))), estimator).explicitGet()._1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import cats.Show
import com.wavesplatform.account.KeyPair
import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.common.utils.EitherExt2
import com.wavesplatform.common.utils.EitherExt2.explicitGet
import com.wavesplatform.generator.OracleTransactionGenerator.Settings
import com.wavesplatform.generator.config.FicusImplicits
import com.wavesplatform.generator.utils.Gen
import com.wavesplatform.generator.utils.Implicits.DoubleExt
import com.wavesplatform.lang.v1.estimator.ScriptEstimator
import com.wavesplatform.state._
import com.wavesplatform.state.*
import com.wavesplatform.transaction.Asset.Waves
import com.wavesplatform.transaction.smart.SetScriptTransaction
import com.wavesplatform.transaction.transfer.TransferTransaction
import com.wavesplatform.transaction.{DataTransaction, Transaction}
import pureconfig.ConfigReader

class OracleTransactionGenerator(settings: Settings, val accounts: Seq[KeyPair], estimator: ScriptEstimator) extends TransactionGenerator {
override def next(): Iterator[Transaction] = generate(settings).iterator
Expand Down Expand Up @@ -46,8 +49,8 @@ class OracleTransactionGenerator(settings: Settings, val accounts: Seq[KeyPair],
}
}

object OracleTransactionGenerator {
final case class Settings(transactions: Int, requiredData: Set[DataEntry[_]])
object OracleTransactionGenerator extends FicusImplicits {
final case class Settings(transactions: Int, requiredData: Set[DataEntry[?]])derives ConfigReader

object Settings {
implicit val toPrintable: Show[Settings] = { x =>
Expand Down
Loading

0 comments on commit 56801b9

Please sign in to comment.