Skip to content

Commit

Permalink
LSV6 test
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Aug 20, 2024
1 parent 20b583b commit a07b67b
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 34 deletions.
6 changes: 5 additions & 1 deletion data/shared/src/main/scala/sigma/ast/methods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,11 @@ case object SBoxMethods extends MonoTypeMethods {
lazy val getRegMethodV6 = SMethod(this, "getReg",
SFunc(Array(SBox, SInt), SOption(tT), Array(paramT)), 7, ExtractRegisterAs.costKind, Seq(tT))
.withIRInfo(MethodCallIrBuilder)
.withInfo(MethodCall, "todo: desc")
.withInfo(MethodCall, """ Extracts register by id and type.
| Type param \lst{T} expected type of the register.
| Returns \lst{Some(value)} if the register is defined and has given type and \lst{None} otherwise
""".stripMargin,
ArgInfo("regId", "zero-based identifier of the register."))

lazy val commonBoxMethods = Array(
ValueMethod, // see ExtractAmount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import sigma.data.{CSigmaDslBuilder, ExactIntegral, ExactNumeric, ExactOrdering,
import sigma.exceptions.GraphBuildingException
import sigma.serialization.OpCodes
import sigma.util.Extensions.ByteOps
import sigma.{SigmaException, ast}
import sigma.{SigmaException, VersionContext, ast}
import sigmastate.interpreter.Interpreter.ScriptEnv

import scala.collection.mutable.ArrayBuffer
Expand Down Expand Up @@ -518,7 +518,7 @@ trait GraphBuilding extends Base with DefRewriting { IR: IRContext =>
error(s"Invalid register name $regName in expression $sel", sel.sourceContext.toOption))
eval(mkExtractRegisterAs(box.asBox, reg, SOption(valType)).asValue[SOption[valType.type]])

case sel @ Select(obj, field, _) if obj.tpe == SBox && field != SBoxMethods.getRegMethodV6.name =>
case sel @ Select(obj, field, _) if obj.tpe == SBox =>
(obj.asValue[SBox.type], field) match {
case (box, SBoxMethods.Value) => eval(mkExtractAmount(box))
case (box, SBoxMethods.PropositionBytes) => eval(mkExtractScriptBytes(box))
Expand Down Expand Up @@ -1018,7 +1018,7 @@ trait GraphBuilding extends Base with DefRewriting { IR: IRContext =>
case (box: Ref[Box]@unchecked, SBoxMethods) => method.name match {
case SBoxMethods.tokensMethod.name =>
box.tokens
case SBoxMethods.getRegMethodV6.name =>
case SBoxMethods.getRegMethodV6.name if VersionContext.current.isV6SoftForkActivated =>
val c1 = asRep[Int](argsV(0))
val c3 = stypeToElem(typeSubst.apply(tT))
box.getReg(c1)(c3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class SigmaTyper(val builder: SigmaBuilder,
case Apply(ApplyTypes(sel @ Select(obj, n, _), Seq(rangeTpe)), args) =>
val newObj = assignType(env, obj)
val newArgs = args.map(assignType(env, _))
obj.tpe match {
newObj.tpe match {
case p: SProduct =>
MethodsContainer.getMethod(p, n) match {
case Some(method: SMethod) =>
Expand All @@ -160,10 +160,10 @@ class SigmaTyper(val builder: SigmaBuilder,
case Some(method) =>
error(s"Don't know how to handle method $method in obj $p", sel.sourceContext)
case None =>
throw new MethodNotFound(s"Cannot find method '$n' in in the object $obj of Product type $p", obj.sourceContext.toOption)
throw new MethodNotFound(s"Cannot find method '$n' in in the object $newObj of Product type $p", newObj.sourceContext.toOption)
}
case _ =>
error(s"Cannot get field '$n' in in the object $obj of non-product type ${obj.tpe}", sel.sourceContext)
error(s"Cannot get field '$n' in in the object $newObj of non-product type ${newObj.tpe}", sel.sourceContext)
}

case app @ Apply(sel @ Select(obj, n, _), args) =>
Expand Down
26 changes: 22 additions & 4 deletions sc/shared/src/test/scala/sigma/LanguageSpecificationV6.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package sigma

import org.ergoplatform.ErgoBox
import org.ergoplatform.ErgoBox.Token
import scorex.util.ModifierId
import scorex.util.encode.Base16
import sigma.ast.ErgoTree.ZeroHeader
import sigma.ast.SCollection.SByteArray
import sigma.ast.syntax.TrueSigmaProp
import sigma.ast._
import sigma.data.{CBigInt, ExactNumeric}
import sigma.data.{CBigInt, CBox, ExactNumeric}
import sigma.eval.{CostDetails, SigmaDsl, TracedCost}
import sigma.util.Extensions.{BooleanOps, ByteOps, IntOps, LongOps}
import sigmastate.exceptions.MethodNotFound
Expand Down Expand Up @@ -265,9 +269,13 @@ class LanguageSpecificationV6 extends LanguageSpecificationBase { suite =>
}

property("Box properties equivalence (new features)") {
// TODO v6.0: related to https://github.com/ScorexFoundation/sigmastate-interpreter/issues/416
val getReg = newFeature((x: Box) => x.getReg[Int](1).get,
"{ (x: Box) => x.getReg[Int](1).get }",
// related to https://github.com/ScorexFoundation/sigmastate-interpreter/issues/416
def getReg = newFeature((x: Box) => x.getReg[Long](0).get,
"{ (x: Box) => x.getReg[Long](0).get }",
FuncValue(
Array((1, SBox)),
OptionGet(ExtractRegisterAs(ValUse(1, SBox), ErgoBox.R0, SOption(SLong)))
),
sinceVersion = VersionContext.V6SoftForkVersion)

if (activatedVersionInTests < VersionContext.V6SoftForkVersion) {
Expand All @@ -277,6 +285,16 @@ class LanguageSpecificationV6 extends LanguageSpecificationBase { suite =>
forAll { box: Box =>
Seq(getReg).foreach(_.checkEquality(box))
}
} else {
val value = 10L
val box = CBox(new ErgoBox(value, TrueTree, Colls.emptyColl[Token], Map.empty,
ModifierId @@ Base16.encode(Array.fill(32)(0)), 0, 0))
verifyCases(
Seq(
box -> new Expected(ExpectedResult(Success(value), None))
),
getReg
)
}
}

Expand Down
18 changes: 14 additions & 4 deletions sc/shared/src/test/scala/sigma/SigmaDslTesting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -872,10 +872,14 @@ class SigmaDslTesting extends AnyPropSpec
extends Feature[A, B] {

override def isSupportedIn(vc: VersionContext): Boolean =
vc.activatedVersion >= sinceVersion && vc.ergoTreeVersion >= sinceVersion
vc.activatedVersion >= sinceVersion

override def scalaFunc: A => B = { x =>
sys.error(s"Semantic Scala function is not defined for old implementation: $this")
if (isSupportedIn(VersionContext.current)) {
scalaFuncNew(x)
} else {
sys.error(s"Semantic Scala function is not defined for old implementation: $this")
}
}
implicit val cs = compilerSettingsInTests

Expand Down Expand Up @@ -925,8 +929,14 @@ class SigmaDslTesting extends AnyPropSpec
printTestCases: Boolean,
failOnTestVectors: Boolean): Unit = {
val funcRes = checkEquality(input, printTestCases)
funcRes.isFailure shouldBe true
Try(scalaFunc(input)) shouldBe expected.value
if(!isSupportedIn(VersionContext.current)) {
funcRes.isFailure shouldBe true
}
if(isSupportedIn(VersionContext.current)) {
Try(scalaFunc(input)) shouldBe expected.value
} else {
Try(scalaFunc(input)).isFailure shouldBe true
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,25 +161,6 @@ class BasicOpsSpecification extends CompilerTestingCommons
)
}

property("Box.getReg") {
def getRegTest(): Assertion = {
test("R1", env, ext,
"""{ SELF.getReg[Long](0).get == SELF.value &&
| SELF.getReg[Coll[(Coll[Byte], Long)]](2).get == SELF.tokens &&
| SELF.getReg[Int](9).isEmpty
|}""".stripMargin,
null
)
}

if (VersionContext.current.isV6SoftForkActivated) {
getRegTest()
} else {
an[Exception] should be thrownBy getRegTest()
}

}

property("Relation operations") {
test("R1", env, ext,
"{ allOf(Coll(getVar[Boolean](trueVar).get, true, true)) }",
Expand Down Expand Up @@ -786,4 +767,25 @@ class BasicOpsSpecification extends CompilerTestingCommons
test("subst", env, ext, hostScript, null)
}

property("Box.getReg") {
def getRegTest(): Assertion = {
test("Box.getReg", env, ext,
"""{
| val x = SELF
| x.getReg[Long](0).get == SELF.value &&
| x.getReg[Coll[(Coll[Byte], Long)]](2).get == SELF.tokens &&
| x.getReg[Int](9).isEmpty
|}""".stripMargin,
null
)
}

if (VersionContext.current.isV6SoftForkActivated) {
getRegTest()
} else {
an[Exception] should be thrownBy getRegTest()
}

}

}

0 comments on commit a07b67b

Please sign in to comment.