Skip to content

Commit

Permalink
Merge branch 'version-1.5.x' into node-2630-improved-caches-script-es…
Browse files Browse the repository at this point in the history
…timation
  • Loading branch information
phearnot committed Dec 6, 2023
2 parents 6f15255 + a518c18 commit fb6093f
Show file tree
Hide file tree
Showing 28 changed files with 299 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: '8'
java-version: '11'
- name: Cache SBT
uses: actions/cache@v3
with:
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ A quick introduction of the minimal setup you need to get a running node.
Linux systems:
```bash
sudo apt-get update
sudo apt-get install openjdk-8-jre
sudo apt-get install openjdk-11-jre
java -jar node/target/waves-all*.jar path/to/config/waves-{network}.conf
```

Mac systems (assuming already installed homebrew):
```bash
brew cask install adoptopenjdk/openjdk/adoptopenjdk8
brew cask install adoptopenjdk/openjdk/adoptopenjdk11
java -jar node/target/waves-all*.jar path/to/config/waves-{network}.conf
```

Windows systems (assuming already installed OpenJDK 8):
Windows systems (assuming already installed OpenJDK 11):
```bash
java -jar node/target/waves-all*.jar path/to/config/waves-{network}.conf
```
Expand Down Expand Up @@ -97,9 +97,9 @@ To build and test this project, you will have to follow these steps:

```bash
sudo apt-get update
sudo apt-get install openjdk-8-jre # Ubuntu
sudo apt-get install openjdk-11-jre # Ubuntu
# or
# brew cask install adoptopenjdk/openjdk/adoptopenjdk8 # Mac
# brew cask install adoptopenjdk/openjdk/adoptopenjdk11 # Mac
```

- Install SBT (Scala Build Tool)
Expand Down
2 changes: 1 addition & 1 deletion benchmark/build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
enablePlugins(JmhPlugin)

Jmh / version := "1.33"
Jmh / version := "1.37"

libraryDependencies ++= Seq(
"org.scodec" %% "scodec-core" % "1.11.10",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ object EnvironmentFunctionsBenchmark {

override def accountScript(addressOrAlias: Recipient): Option[Script] = ???

override def calculateDelay(hitSource: ByteStr, baseTarget: Long, generator: ByteStr, balance: Long): Long = ???
override def calculateDelay(generator: ByteStr, balance: Long): Long = ???

def callScript(
dApp: Address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@ class CalculateDelayBenchmark {

@Benchmark
def calculateDelay1(bh: Blackhole, st: St): Unit =
bh.consume(st.environment.calculateDelay(ByteStr.empty, 0, ByteStr.empty, 0))
bh.consume(st.environment.calculateDelay(ByteStr.empty, 0))

@Benchmark
def calculateDelay2(bh: Blackhole, st: St): Unit =
bh.consume(
st.environment.calculateDelay(ByteStr.fill(96)(127), Long.MaxValue, ByteStr.fill(26)(127), Long.MaxValue)
st.environment.calculateDelay(ByteStr.fill(26)(127), Long.MaxValue)
)

@Benchmark
def calculateDelay3(bh: Blackhole, st: St): Unit =
bh.consume(
st.environment.calculateDelay(ByteStr.fill(96)(-128), Long.MinValue, ByteStr.fill(26)(-128), Long.MinValue)
st.environment.calculateDelay(ByteStr.fill(26)(-128), Long.MinValue)
)

@Benchmark
def calculateDelay4(bh: Blackhole, st: St): Unit =
bh.consume(
st.environment.calculateDelay(ByteStr.fill(32)(32), 123456, ByteStr.fill(26)(32), 100_000_000)
st.environment.calculateDelay(ByteStr.fill(26)(32), 100_000_000)
)
}

Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ enablePlugins(GitVersioning)
git.uncommittedSignifier := Some("DIRTY")
git.useGitDescribe := true
ThisBuild / git.useGitDescribe := true
ThisBuild / PB.protocVersion := "3.24.4" // https://protobuf.dev/support/version-support/#java
ThisBuild / PB.protocVersion := "3.25.1" // https://protobuf.dev/support/version-support/#java

lazy val lang =
crossProject(JSPlatform, JVMPlatform)
Expand Down
2 changes: 1 addition & 1 deletion docker/node-sbt-builder.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM eclipse-temurin:8-jdk-jammy
FROM eclipse-temurin:11-jdk-jammy

ARG WAVES_VERSION
ARG SBT_VERSION
Expand Down
4 changes: 2 additions & 2 deletions lang/doc/v8/funcs/blockchain-functions.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@
}
{
name: "calculateDelay"
params: [ "ByteVector", "Int", "Address", "Int" ]
params: [ "Address", "Int" ]
doc: "Calculates mining delay using Fair PoS calculator."
paramsDoc: [ "hit source", "base target", "generator address", "generator balance" ]
paramsDoc: [ "generator address", "generator balance" ]
complexity: 1
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ package object utils {
override def addressFromString(address: String): Either[String, Recipient.Address] = ???
override def addressFromPublicKey(publicKey: ByteStr): Either[String, Address] = ???
override def accountScript(addressOrAlias: Recipient): Option[Script] = ???
override def calculateDelay(hs: ByteStr, bt: Long, gt: ByteStr, b: Long): Long = ???
override def calculateDelay(gt: ByteStr, b: Long): Long = ???
override def callScript(
dApp: Address,
func: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.wavesplatform.lang.v1.estimator.v3

import cats.implicits.{toBifunctorOps, toFoldableOps, toTraverseOps}
import cats.{Id, Monad}
import cats.syntax.functor.*
import com.wavesplatform.lang.v1.FunctionHeader
import com.wavesplatform.lang.v1.FunctionHeader.User
import com.wavesplatform.lang.v1.compiler.Terms.*
Expand Down Expand Up @@ -63,7 +64,7 @@ case class ScriptEstimatorV3(fixOverflow: Boolean, overhead: Boolean, letFixes:
for {
_ <- if (globalDeclarationsMode) saveGlobalLetCost(let, activeFuncArgs) else doNothing
startCtx <- get[Id, EstimatorContext, EstimationError]
letEval = evalHoldingFuncs(let.value, activeFuncArgs)
letEval = evalHoldingFuncs(let.value, activeFuncArgs, Some(startCtx.funcs))
_ <- beforeNextExprEval(let, letEval)
nextExprCost <- evalExpr(nextExpr, activeFuncArgs, globalDeclarationsMode)
nextExprCtx <- get[Id, EstimatorContext, EstimationError]
Expand All @@ -75,9 +76,17 @@ case class ScriptEstimatorV3(fixOverflow: Boolean, overhead: Boolean, letFixes:
private def saveGlobalLetCost(let: LET, activeFuncArgs: Set[String]): EvalM[Unit] = {
val costEvaluation =
for {
startCtx <- get[Id, EstimatorContext, EstimationError]
(bodyCost, usedRefs) <- withUsedRefs(evalExpr(let.value, activeFuncArgs))
ctx <- get[Id, EstimatorContext, EstimationError]
letCosts <- usedRefs.toSeq.traverse(ctx.globalLetEvals.getOrElse(_, zero))
letCosts <- usedRefs.toSeq.traverse { ref =>
local {
for {
_ <- update(funcs.set(_)(startCtx.funcs))
cost <- ctx.globalLetEvals.getOrElse(ref, zero)
} yield cost
}
}
} yield bodyCost + letCosts.sum
for {
cost <- local(costEvaluation)
Expand Down Expand Up @@ -166,15 +175,15 @@ case class ScriptEstimatorV3(fixOverflow: Boolean, overhead: Boolean, letFixes:

private def evalFuncCall(header: FunctionHeader, args: List[EXPR], activeFuncArgs: Set[String]): EvalM[Long] =
for {
ctx <- get[Id, EstimatorContext, EstimationError]
(bodyCost, bodyUsedRefs) <- getFuncCost(header, ctx)
_ <- setFuncToCtx(header, bodyCost, bodyUsedRefs)
(argsCosts, argsUsedRefs) <- withUsedRefs(args.traverse(evalHoldingFuncs(_, activeFuncArgs)))
argsCostsSum <- argsCosts.foldM(0L)(sum)
ctx <- get[Id, EstimatorContext, EstimationError]
(bodyCost, bodyUsedRefs) <- getFuncCost(header, ctx)
_ <- setFuncToCtx(header, bodyCost, bodyUsedRefs)
(argsCosts, _) <- withUsedRefs(args.traverse(evalHoldingFuncs(_, activeFuncArgs)))
argsCostsSum <- argsCosts.foldM(0L)(sum)
bodyCostV = bodyCost.value()
correctedBodyCost =
if (!overhead && !letFixes && bodyCostV == 0) 1
else if (letFixes && bodyCostV == 0 && isBlankFunc(bodyUsedRefs ++ argsUsedRefs, ctx.refsCosts)) 1
else if (letFixes && bodyCostV == 0 && isBlankFunc(bodyUsedRefs, ctx.refsCosts)) 1
else bodyCostV
result <- sum(argsCostsSum, correctedBodyCost)
} yield result
Expand All @@ -201,9 +210,14 @@ case class ScriptEstimatorV3(fixOverflow: Boolean, overhead: Boolean, letFixes:
private def isBlankFunc(usedRefs: Set[String], refsCosts: Map[String, Long]): Boolean =
!usedRefs.exists(refsCosts.get(_).exists(_ > 0))

private def evalHoldingFuncs(expr: EXPR, activeFuncArgs: Set[String]): EvalM[Long] =
private def evalHoldingFuncs(
expr: EXPR,
activeFuncArgs: Set[String],
ctxFuncsOpt: Option[Map[FunctionHeader, (Coeval[Long], Set[String])]] = None
): EvalM[Long] =
for {
startCtx <- get[Id, EstimatorContext, EstimationError]
_ <- ctxFuncsOpt.fold(doNothing.void)(ctxFuncs => update(funcs.set(_)(ctxFuncs)))
cost <- evalExpr(expr, activeFuncArgs)
_ <- update(funcs.set(_)(startCtx.funcs))
} yield cost
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1002,8 +1002,6 @@ object Functions {
val calculateDelay: NativeFunction[Environment] = {
val args =
Seq(
("hit source", BYTESTR),
("base target", LONG),
("generator", addressType),
("balance", LONG)
)
Expand All @@ -1014,24 +1012,23 @@ object Functions {
LONG,
args*
) {
val MaxHitSourceLength = 96
new ContextfulNativeFunction.Simple[Environment]("calculateDelay", LONG, args) {
override def evaluate[F[_]: Monad](env: Environment[F], args: List[EVALUATED]): F[Either[ExecutionError, EVALUATED]] =
args match {
case CONST_BYTESTR(hitSource) :: CONST_LONG(baseTarget) :: CaseObj(`addressType`, fields) :: CONST_LONG(balance) :: Nil =>
case CaseObj(`addressType`, fields) :: CONST_LONG(balance) :: Nil =>
val addressBytes = fields("bytes").asInstanceOf[CONST_BYTESTR].bs
if (addressBytes.size > AddressLength) {
val error = CommonError(s"Address bytes length = ${addressBytes.size} exceeds limit = $AddressLength")
(error: ExecutionError).asLeft[EVALUATED].pure[F]
} else if (hitSource.size > MaxHitSourceLength) {
val error = CommonError(s"Hit source bytes length = ${hitSource.size} exceeds limit = $MaxHitSourceLength")
} else if (balance <= 0) {
val error = CommonError(s"Unexpected non-positive balance = $balance")
(error: ExecutionError).asLeft[EVALUATED].pure[F]
} else {
val delay = env.calculateDelay(hitSource, baseTarget, addressBytes, balance)
val delay = env.calculateDelay(addressBytes, balance)
(CONST_LONG(delay): EVALUATED).asRight[ExecutionError].pure[F]
}
case xs =>
notImplemented[Id, EVALUATED]("calculateDelay(hitSource: ByteVector, baseTarget: ByteVector, generator: Address, balance: Long)", xs)
notImplemented[Id, EVALUATED]("calculateDelay(generator: Address, balance: Long)", xs)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.wavesplatform.lang.v1.parser

import com.wavesplatform.lang.v1.parser.Expressions.*
import fastparse.*
import fastparse.MultiLineWhitespace.*

sealed abstract class BinaryOperation {
val func: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ class Parser(stdLibVersion: StdLibVersion)(implicit offset: LibrariesOffset) {
}

def invalid[A: P]: P[INVALID] = {
import fastparse.NoWhitespace.*
P(Index ~~ CharPred(_ != '\n').rep(1) ~~ Index)
P(Index ~~ CharPred(_ != '\n').repX(1) ~~ Index)
.map { case (start, end) =>
INVALID(Pos(start, end), "can't parse the expression")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.wavesplatform.lang.v1.parser
import com.wavesplatform.lang.v1.parser.Expressions.*
import com.wavesplatform.lang.v1.parser.Parser.LibrariesOffset
import fastparse.*
import fastparse.MultiLineWhitespace.*

sealed abstract class UnaryOperation {
val func: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ trait Environment[F[_]] {
availableComplexity: Int,
reentrant: Boolean
): Coeval[F[(Either[ValidationError, (EVALUATED, Log[F])], Int)]]
def calculateDelay(hitSource: ByteStr, baseTarget: Long, generator: ByteStr, balance: Long): Long
def calculateDelay(generator: ByteStr, balance: Long): Long
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import com.wavesplatform.lang.v1.CTX
import com.wavesplatform.lang.v1.compiler.Terms.*
import com.wavesplatform.lang.v1.compiler.Types.*
import com.wavesplatform.lang.v1.evaluator.Contextful.NoContext
import com.wavesplatform.lang.v1.evaluator.{EvaluatorV1, Log}
import com.wavesplatform.lang.v1.evaluator.EvaluatorV1.*
import com.wavesplatform.lang.v1.evaluator.ctx.*
import com.wavesplatform.lang.v1.evaluator.ctx.impl.{EnvironmentFunctions, PureContext, *}
import com.wavesplatform.lang.v1.evaluator.ctx.impl.*
import com.wavesplatform.lang.v1.evaluator.{EvaluatorV1, Log}
import com.wavesplatform.lang.v1.traits.domain.Recipient.Address
import com.wavesplatform.lang.v1.traits.domain.{BlockInfo, Recipient, ScriptAssetInfo, Tx}
import com.wavesplatform.lang.v1.traits.{DataType, Environment}
Expand Down Expand Up @@ -96,7 +96,7 @@ object Common {
def addressFromString(address: String): Either[String, Recipient.Address] = ???
def addressFromPublicKey(publicKey: ByteStr): Either[String, Address] = ???
def accountScript(addressOrAlias: Recipient): Option[Script] = ???
def calculateDelay(hs: ByteStr, bt: Long, gt: ByteStr, b: Long): Long = ???
def calculateDelay(gt: ByteStr, b: Long): Long = ???
def callScript(
dApp: Address,
func: String,
Expand Down
Loading

0 comments on commit fb6093f

Please sign in to comment.