Skip to content

Commit

Permalink
NODE-2624 /utils/script/evaluate: do not parse bytes in expr (#3907)
Browse files Browse the repository at this point in the history
  • Loading branch information
vsuharnikov authored Oct 26, 2023
1 parent 82f19d8 commit 89f9c51
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 26 deletions.
2 changes: 1 addition & 1 deletion node/src/main/resources/swagger-ui/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5402,7 +5402,7 @@ components:
expr:
type: string
example: "default()"
description: "Ride expression either as a Ride code, or as compiled in base64 representation"
description: "Ride expression"
state:
$ref: '#/components/schemas/BlockchainOverrides'
RideInvocationRequest:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import com.wavesplatform.lang.v1.compiler.Terms.EXPR
import com.wavesplatform.lang.v1.compiler.{ExpressionCompiler, Terms}
import com.wavesplatform.lang.v1.evaluator.ContractEvaluator.Invocation
import com.wavesplatform.lang.v1.parser.Parser.LibrariesOffset.NoLibraries
import com.wavesplatform.lang.v1.serialization.SerdeV1
import com.wavesplatform.lang.v1.traits.domain.Recipient.Address as RideAddress
import com.wavesplatform.lang.{ValidationError, utils}
import com.wavesplatform.state.diffs.FeeValidation.{FeeConstants, FeeUnit}
Expand Down Expand Up @@ -44,13 +43,10 @@ object UtilsEvaluationRequest {
}

case class UtilsExprRequest(
expr: Either[ByteStr, String],
expr: String,
state: Option[BlockchainOverrides] = None
) extends UtilsEvaluationRequest {
def parseCall(version: StdLibVersion): Either[GenericError, Terms.EXPR] = expr match {
case Left(binaryCall) => SerdeV1.deserialize(binaryCall.arr).bimap(GenericError(_), _._1)
case Right(textCall) => compile(version, textCall)
}
def parseCall(version: StdLibVersion): Either[GenericError, Terms.EXPR] = compile(version, expr)

private def compile(version: StdLibVersion, str: String): Either[GenericError, EXPR] =
ExpressionCompiler
Expand All @@ -59,8 +55,6 @@ case class UtilsExprRequest(
}

object UtilsExprRequest {
implicit val byteStrReads: Reads[ByteStr] = com.wavesplatform.utils.byteStrFormat
implicit val exprReads: Reads[Either[ByteStr, String]] = com.wavesplatform.api.http.eitherReads[ByteStr, String]
implicit val utilsExprRequestReads: Reads[UtilsExprRequest] = Json.using[Json.WithDefaultValues].reads[UtilsExprRequest]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ import com.wavesplatform.db.WithState.AddrWithBalance
import com.wavesplatform.history.DefaultBlockchainSettings
import com.wavesplatform.lang.directives.values.V6
import com.wavesplatform.lang.script.Script
import com.wavesplatform.lang.v1.FunctionHeader
import com.wavesplatform.lang.v1.compiler.Terms.{CONST_LONG, EXPR, FUNCTION_CALL}
import com.wavesplatform.lang.v1.compiler.TestCompiler
import com.wavesplatform.lang.v1.estimator.v3.ScriptEstimatorV3
import com.wavesplatform.lang.v1.evaluator.ctx.impl.PureContext
import com.wavesplatform.lang.v1.serialization.SerdeV1
import com.wavesplatform.state.{Blockchain, IntegerDataEntry, LeaseBalance}
import com.wavesplatform.test.DomainPresets.{RideV5, RideV6}
import com.wavesplatform.test.NumericExt
Expand Down Expand Up @@ -64,10 +61,10 @@ class UtilsRouteEvaluateSpec
}

routePath("/script/evaluate/{address}") - {
val letFromContract = 1000
val xFromContract = 1000
val testScript = TxHelpers.scriptV5(
s"""
|let letFromContract = $letFromContract
|let x = $xFromContract
|
|func any(value: Any) = value
|func test(i: Int) = i * 10
Expand Down Expand Up @@ -117,11 +114,6 @@ class UtilsRouteEvaluateSpec
def evalScript(text: String, address: Address = dAppAddress) =
Post(routePath(s"/script/evaluate/$address"), Json.obj("expr" -> text))

def evalBin(expr: EXPR) = {
val serialized = ByteStr(SerdeV1.serialize(expr))
Post(routePath(s"/script/evaluate/$dAppAddress"), Json.obj("expr" -> serialized.toString))
}

def responseJson: JsObject = {
val fullJson = responseAs[JsObject]
(fullJson \ "address").as[String] shouldBe dAppAddress.toString
Expand Down Expand Up @@ -219,10 +211,6 @@ class UtilsRouteEvaluateSpec
responseJson shouldBe Json.obj("type" -> "Int", "value" -> 1230)
}

evalBin(FUNCTION_CALL(FunctionHeader.User("test"), List(CONST_LONG(123)))) ~> route ~> check {
responseJson shouldBe Json.obj("type" -> "Int", "value" -> 1230)
}

evalScript("testS()") ~> route ~> check {
responseJson shouldBe Json.obj("type" -> "String", "value" -> "Test")
}
Expand Down Expand Up @@ -255,8 +243,17 @@ class UtilsRouteEvaluateSpec
responseJson shouldBe Json.obj("type" -> "Boolean", "value" -> true)
}

evalScript("letFromContract - 1") ~> route ~> check {
responseJson shouldBe Json.obj("type" -> "Int", "value" -> (letFromContract - 1))
evalScript("x") ~> route ~> check {
responseJson.validate[JsObject] shouldBe JsSuccess(
Json.obj(
"type" -> "Int",
"value" -> xFromContract
)
)
}

evalScript("x - 1") ~> route ~> check {
responseJson shouldBe Json.obj("type" -> "Int", "value" -> (xFromContract - 1))
}

(() => utilsApi.blockchain.settings)
Expand Down
2 changes: 1 addition & 1 deletion ride-runner/src/main/resources/swagger-ui/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ components:
expr:
type: string
example: "default()"
description: "Ride expression either as a Ride code, or as compiled in base64 representation"
description: "Ride expression"
state:
$ref: '#/components/schemas/BlockchainOverrides'
RideInvocationRequest:
Expand Down

0 comments on commit 89f9c51

Please sign in to comment.