Skip to content

Commit

Permalink
NODE-2403: Forbid temporary negative balance between invoke script ac…
Browse files Browse the repository at this point in the history
  • Loading branch information
Karasiq authored Jan 12, 2022
1 parent eff08bb commit 38707d0
Show file tree
Hide file tree
Showing 121 changed files with 1,093 additions and 1,204 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import org.openjdk.jmh.infra.Blackhole
import scala.annotation.tailrec

object EvaluatorV2Benchmark {
val pureContext: CTX[Environment] = PureContext.build(V1, fixUnicodeFunctions = true, useNewPowPrecision = true).withEnvironment[Environment]
val pureContext: CTX[Environment] = PureContext.build(V1, useNewPowPrecision = true).withEnvironment[Environment]
val pureEvalContext: EvaluationContext[Environment, Id] = pureContext.evaluationContext(Common.emptyBlockchainEnvironment())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import scala.util.Random
object ScriptEvaluatorBenchmark {
val version = V1
val pureEvalContext: EvaluationContext[NoContext, Id] =
PureContext.build(V1, fixUnicodeFunctions = true, useNewPowPrecision = true).evaluationContext
PureContext.build(V1, useNewPowPrecision = true).evaluationContext
val evaluatorV1: EvaluatorV1[Id, NoContext] = new EvaluatorV1[Id, NoContext]()
}

Expand Down Expand Up @@ -218,7 +218,7 @@ class Concat {

@State(Scope.Benchmark)
class Median {
val context: EvaluationContext[NoContext, Id] = PureContext.build(V4, fixUnicodeFunctions = true, useNewPowPrecision = true).evaluationContext
val context: EvaluationContext[NoContext, Id] = PureContext.build(V4, useNewPowPrecision = true).evaluationContext

val randomElements: Array[EXPR] =
(1 to 10000).map { _ =>
Expand Down Expand Up @@ -261,7 +261,7 @@ class Median {
@State(Scope.Benchmark)
class SigVerify32Kb {
val context: EvaluationContext[NoContext, Id] =
Monoid.combine(PureContext.build(V4, fixUnicodeFunctions = true, useNewPowPrecision = true).evaluationContext,
Monoid.combine(PureContext.build(V4, useNewPowPrecision = true).evaluationContext,
CryptoContext.build(Global, V4).evaluationContext)

val expr: EXPR = {
Expand All @@ -284,7 +284,7 @@ class SigVerify32Kb {
class ListRemoveByIndex {
val context: EvaluationContext[NoContext, Id] =
Monoid.combine(
PureContext.build(V4, fixUnicodeFunctions = true, useNewPowPrecision = true).evaluationContext,
PureContext.build(V4, useNewPowPrecision = true).evaluationContext,
CryptoContext.build(Global, V4).evaluationContext
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ package object v1 {
expr: EXPR,
stdLibVersion: StdLibVersion
): (Log[Id], Int, Either[ExecutionError, Terms.EVALUATED]) =
EvaluatorV2.applyCompleted(ctx, expr, stdLibVersion, newMode = true)
EvaluatorV2.applyCompleted(ctx, expr, stdLibVersion, newMode = true, correctFunctionCallScope = true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class BloomFilterBenchmark {

@Benchmark
def verifyExchangeTxSign(bh: Blackhole, st: St): Unit = {
bh.consume(Verifier.verifyAsEllipticCurveSignature(st.exchangeTransactions(Random.nextInt(1000))))
bh.consume(Verifier.verifyAsEllipticCurveSignature(st.exchangeTransactions(Random.nextInt(1000)), checkWeakPk = false))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object RideFullContext {
def build(ds: DirectiveSet): CTX[Environment] = {
val wavesCtx = WavesContext.build(Global,ds)
val cryptoCtx = CryptoContext.build(Global, ds.stdLibVersion).withEnvironment[Environment]
val pureCtx = PureContext.build(ds.stdLibVersion).withEnvironment[Environment]
val pureCtx = PureContext.build().withEnvironment[Environment]
pureCtx |+| cryptoCtx |+| wavesCtx
}
}
2 changes: 1 addition & 1 deletion lang/js/src/main/scala/JsAPI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object JsAPI {
)

private def cryptoContext(version: StdLibVersion) = CryptoContext.build(Global, version).withEnvironment[Environment]
private def pureContext(version: StdLibVersion) = PureContext.build(version, fixUnicodeFunctions = true, useNewPowPrecision = true).withEnvironment[Environment]
private def pureContext(version: StdLibVersion) = PureContext.build(version, useNewPowPrecision = true).withEnvironment[Environment]

private val fullDAppContext: Map[StdLibVersion, CTX[Environment]] =
DirectiveDictionary[StdLibVersion].all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object JavaAdapter {
Seq(
CryptoContext.compilerContext(Global, version),
WavesContext.build(Global, ???).compilerContext,
PureContext.build(version, fixUnicodeFunctions = true, useNewPowPrecision = true).compilerContext
PureContext.build(version, useNewPowPrecision = true).compilerContext
))

def compile(input: String): EXPR = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ package object utils {
val ctx = Coeval.evalOnce(
Monoid.combineAll(
Seq(
PureContext.build(version, fixUnicodeFunctions = true, useNewPowPrecision = true).withEnvironment[Environment],
PureContext.build(version, useNewPowPrecision = true).withEnvironment[Environment],
CryptoContext.build(Global, version).withEnvironment[Environment],
WavesContext.build(Global, ds)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,12 @@ class EvaluatorV2(
case _ => Coeval.now(unused)
}
}
.onErrorHandle { e =>
val error = if (e.getMessage != null) e.getMessage else e.toString
.onErrorHandle { throwable =>
val error = if (throwable.getMessage != null) throwable.getMessage else throwable.toString
ctx.log(let, Left(error))
throw e match {
case _: EvaluationException | _: RejectException => e
case _ => EvaluationException(e.getMessage, limit)
throw throwable match {
case evalExc: EvaluationException => evalExc
case other => EvaluationException(other.getMessage, limit)
}
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.wavesplatform.lang.v1.traits.domain._
sealed trait ScriptResult {
def returnedValue: EVALUATED = unit
def invokes: Seq[(Address, String, Seq[EVALUATED], Seq[CaseObj], ScriptResult)] = Nil
def unusedComplexity: Int
}
case class ScriptResultV3(ds: List[DataItem[_]], ts: List[AssetTransfer], unusedComplexity: Int) extends ScriptResult
case class ScriptResultV4(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,9 @@ object PureContext {
}

private val dropBytesBeforeV6 = dropBytes(checkLimits = false)
private val dropBytesFromV6 = dropBytes(checkLimits = true)
private val dropBytesFromV6 = dropBytes(checkLimits = true)
private val takeBytesBeforeV6 = takeBytes(checkLimits = false)
private val takeBytesFromV6 = takeBytes(checkLimits = true)
private val takeBytesFromV6 = takeBytes(checkLimits = true)

private val dropRightBytesBeforeV6: BaseFunction[NoContext] =
UserFunction(
Expand Down Expand Up @@ -839,14 +839,14 @@ object PureContext {
) {
case CONST_STRING(xs) :: CONST_LONG(number) :: Nil =>
val limit = Terms.DataEntryValueMax
if (number < 0)
Left(s"Unexpected negative number = $number passed to takeRight()")
else if (number > limit)
Left(s"Number = $number passed to takeRight() exceeds String limit = $limit")
else {
val correctedNumber = number.max(0).min(xs.codePointCount(0, xs.length))
CONST_STRING(xs.takeRight(xs.offsetByCodePoints(0, trimLongToInt(correctedNumber))))
}
if (number < 0)
Left(s"Unexpected negative number = $number passed to takeRight()")
else if (number > limit)
Left(s"Number = $number passed to takeRight() exceeds String limit = $limit")
else {
val correctedNumber = number.max(0).min(xs.codePointCount(0, xs.length))
CONST_STRING(xs.takeRight(xs.offsetByCodePoints(0, trimLongToInt(correctedNumber))))
}
case xs =>
notImplemented[Id, EVALUATED]("takeRight(xs: String, number: Int)", xs)
}
Expand Down Expand Up @@ -920,7 +920,6 @@ object PureContext {
notImplemented[Id, EVALUATED]("dropRight(xs: String, number: Int)", xs)
}


val UTF8Decoder = UTF_8.newDecoder

def toUtf8String(reduceLimit: Boolean): BaseFunction[NoContext] =
Expand Down Expand Up @@ -1492,7 +1491,7 @@ object PureContext {
1,
(CREATE_TUPLE + resultSize - 2).toShort,
PARAMETERIZEDTUPLE(typeParams),
typeParams.mapWithIndex { case (typeParam, i) => (s"element${i + 1}", typeParam) }*
typeParams.mapWithIndex { case (typeParam, i) => (s"element${i + 1}", typeParam) } *
) {
case elements if elements.length == resultSize =>
val fields = elements.mapWithIndex { case (element, i) => (s"_${i + 1}", element) }.toMap
Expand All @@ -1513,16 +1512,18 @@ object PureContext {
}

def pow(roundTypes: UNION, useNewPrecision: Boolean): BaseFunction[NoContext] = {
NativeFunction("pow",
Map(V3 -> 100L, V4 -> 100L, V5 -> 100L, V6 -> 28L),
POW,
LONG,
("base", LONG),
("bp", LONG),
("exponent", LONG),
("ep", LONG),
("rp", LONG),
("round", roundTypes)) {
NativeFunction(
"pow",
Map(V3 -> 100L, V4 -> 100L, V5 -> 100L, V6 -> 28L),
POW,
LONG,
("base", LONG),
("bp", LONG),
("exponent", LONG),
("ep", LONG),
("rp", LONG),
("round", roundTypes)
) {
case CONST_LONG(b) :: CONST_LONG(bp) :: CONST_LONG(e) :: CONST_LONG(ep) :: CONST_LONG(rp) :: round :: Nil =>
if (bp < 0
|| bp > 8
Expand Down Expand Up @@ -1716,7 +1717,7 @@ object PureContext {
m.flatMap(result) {
case (Right(value), complexity) => evaluateUserFunction(function, List(value, element), complexity)
case (error, complexity) => Coeval((error, complexity).pure[F])
}
}
)
case xs =>
val err = notImplemented[F, EVALUATED](s"fold_$limit(list: List[A], accumulator: B, function: String)", xs)
Expand Down Expand Up @@ -1834,7 +1835,7 @@ object PureContext {
Array(
extract,
fraction(fixLimitCheck = false),
sizeStringFixed,
sizeStringFixed
)

private val v1V2V3CommonFunctionsUnfixed =
Expand Down Expand Up @@ -1884,14 +1885,14 @@ object PureContext {
splitStrFixed
)

private def v3Functions(fixUnicodeFunctions: Boolean, useNewPowPrecision: Boolean) =
v1V2V3CommonFunctions(fixUnicodeFunctions) ++
private def v3Functions(useNewPowPrecision: Boolean) =
v1V2V3CommonFunctions(useNewPowPrecision) ++
fromV3Functions ++
v3V4Functions(useNewPowPrecision) ++
Array(
toUtf8String(reduceLimit = false),
listConstructor(checkSize = false)
) ++ (if (fixUnicodeFunctions) v3FunctionFixed else v3FunctionsUnfixed)
) ++ (if (useNewPowPrecision) v3FunctionFixed else v3FunctionsUnfixed)

private val fromV4Functions =
commonFunctions ++
Expand All @@ -1913,11 +1914,11 @@ object PureContext {
makeString
) ++ (MinTupleSize to MaxTupleSize).map(i => createTupleN(i))

private def v4FunctionsUnfixed(useNewPowPrecision: Boolean) =
private def v4FunctionsUnfixed =
fromV4Functions ++
takeDropBytesBeforeV6 ++
takeDropStringUnfixedBeforeV6 ++
v3V4Functions(useNewPowPrecision) ++
v3V4Functions(useNewPowPrecision = false) ++
Array(
indexOf,
indexOfN,
Expand All @@ -1928,11 +1929,11 @@ object PureContext {
fraction(fixLimitCheck = false)
)

private def v4FunctionsFixed(useNewPowPrecision: Boolean) =
private def v4FunctionsFixed =
fromV4Functions ++
takeDropBytesBeforeV6 ++
takeDropStringFixedBeforeV6 ++
v3V4Functions(useNewPowPrecision) ++
v3V4Functions(useNewPowPrecision = true) ++
Array(
indexOfFixed,
indexOfNFixed,
Expand All @@ -1943,8 +1944,8 @@ object PureContext {
fraction(fixLimitCheck = false)
)

private def v4Functions(fixUnicodeFunctions: Boolean, useNewPowPrecision: Boolean) =
if (fixUnicodeFunctions) v4FunctionsFixed(useNewPowPrecision) else v4FunctionsUnfixed(useNewPowPrecision)
private def v4Functions(useNewPowPrecision: Boolean) =
if (useNewPowPrecision) v4FunctionsFixed else v4FunctionsUnfixed

private def fromV5Functions(useNewPowPrecision: Boolean) =
fromV4Functions ++
Expand Down Expand Up @@ -1980,7 +1981,7 @@ object PureContext {
logBigInt(UNION(fromV5RoundTypes)),
pow(UNION(fromV5RoundTypes), useNewPowPrecision),
log(UNION(fromV5RoundTypes)),
fraction(fixLimitCheck = true),
fraction(fixLimitCheck = true)
)

private def v5Functions(useNewPowPrecision: Boolean) =
Expand Down Expand Up @@ -2015,51 +2016,40 @@ object PureContext {
v1V2V3CommonFunctions(fixUnicodeFunctions)
)

private def v3Ctx(fixUnicodeFunctions: Boolean, useNewPowPrecision: Boolean) =
private def v3Ctx(useNewPowPrecision: Boolean) =
CTX[NoContext](
v1v2v3v4Types,
v3V4Vars,
v3Functions(fixUnicodeFunctions, useNewPowPrecision)
v3Functions(useNewPowPrecision)
)

private def v4Ctx(fixUnicodeFunctions: Boolean, useNewPowPrecision: Boolean) =
private def v4Ctx(useNewPowPrecision: Boolean) =
CTX[NoContext](
v1v2v3v4Types,
v3V4Vars,
v4Functions(fixUnicodeFunctions, useNewPowPrecision)
v4Functions(useNewPowPrecision)
)

private val v1V2CtxFixed = v1V2Ctx(true)
private def v3CtxFixed(useNewPowPrecision: Boolean) = v3Ctx(true, useNewPowPrecision)
private def v4CtxFixed(useNewPowPrecision: Boolean) = v4Ctx(true, useNewPowPrecision)

private val v1V2CtxUnfixed = v1V2Ctx(false)
private def v3CtxUnfixed(useNewPowPrecision: Boolean) = v3Ctx(false, useNewPowPrecision)
private def v4CtxUnfixed(useNewPowPrecision: Boolean) = v4Ctx(false, useNewPowPrecision)

private def v5Ctx(useNewPowPrecision: Boolean) =
CTX[NoContext](
v5Types,
v5Vars,
v5Functions(useNewPowPrecision)
)

private val v6Ctx =
private[this] val v6Ctx =
CTX[NoContext](
v5Types,
v5Vars,
v6Functions
)

def build(version: StdLibVersion, fixUnicodeFunctions: Boolean, useNewPowPrecision: Boolean): CTX[NoContext] =
def build(version: StdLibVersion, useNewPowPrecision: Boolean): CTX[NoContext] =
version match {
case V1 | V2 if fixUnicodeFunctions => v1V2CtxFixed
case V3 if fixUnicodeFunctions => v3CtxFixed(useNewPowPrecision)
case V4 if fixUnicodeFunctions => v4CtxFixed(useNewPowPrecision)
case V1 | V2 => v1V2CtxUnfixed
case V3 => v3CtxUnfixed(useNewPowPrecision)
case V4 => v4CtxUnfixed(useNewPowPrecision)
case V5 => v5Ctx(useNewPowPrecision)
case V6 => v6Ctx
case V1 | V2 => v1V2Ctx(useNewPowPrecision)
case V3 => v3Ctx(useNewPowPrecision)
case V4 => v4Ctx(useNewPowPrecision)
case V5 => v5Ctx(useNewPowPrecision)
case V6 => v6Ctx
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object Common {

def ev[T <: EVALUATED](
context: EvaluationContext[NoContext, Id] =
Monoid.combine(PureContext.build(V1, fixUnicodeFunctions = true, useNewPowPrecision = true).evaluationContext, addCtx.evaluationContext),
Monoid.combine(PureContext.build(V1, useNewPowPrecision = true).evaluationContext, addCtx.evaluationContext),
expr: EXPR
): Either[ExecutionError, T] =
new EvaluatorV1[Id, NoContext]().apply[T](context, expr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import scala.collection.mutable

class TestCompiler(version: StdLibVersion) {
private lazy val baseCompilerContext =
PureContext.build(version, fixUnicodeFunctions = true, useNewPowPrecision = true).withEnvironment[Environment] |+|
PureContext.build(version, useNewPowPrecision = true).withEnvironment[Environment] |+|
CryptoContext.build(Global, version).withEnvironment[Environment]

private lazy val compilerContext =
Expand All @@ -39,7 +39,7 @@ class TestCompiler(version: StdLibVersion) {
def compileContract(script: String, allowIllFormedStrings: Boolean = false): Script =
ContractScript(version, compile(script, allowIllFormedStrings).explicitGet()).explicitGet()

def compileExpression(script: String, allowIllFormedStrings: Boolean = false, checkSize: Boolean = true): Script =
def compileExpression(script: String, allowIllFormedStrings: Boolean = false, checkSize: Boolean = true): ExprScript =
ExprScript(
version,
ExpressionCompiler.compile(script, expressionCompilerContext, allowIllFormedStrings).explicitGet()._1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import org.scalatest.Inside
class ContractIntegrationTest extends PropSpec with Inside {

private val ctx: CTX[Environment] =
PureContext.build(V3, fixUnicodeFunctions = true, useNewPowPrecision = true).withEnvironment[Environment] |+|
PureContext.build(V3, useNewPowPrecision = true).withEnvironment[Environment] |+|
CTX[Environment](sampleTypes, Map.empty, Array.empty) |+|
WavesContext.build(
Global,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.wavesplatform.test.PropSpec
class EvaluatorV1CaseObjField extends PropSpec {

def context(p: CaseObj): EvaluationContext[NoContext, Id] =
Monoid.combine(PureContext.build(V1, fixUnicodeFunctions = true, useNewPowPrecision = true).evaluationContext, sampleUnionContext(p))
Monoid.combine(PureContext.build(V1, useNewPowPrecision = true).evaluationContext, sampleUnionContext(p))

property("case custom type field access") {
ev[CONST_LONG](
Expand Down
Loading

0 comments on commit 38707d0

Please sign in to comment.