Skip to content

Commit

Permalink
LSV6 test, roundtrip test, deserializeTo predefined function
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Sep 23, 2024
1 parent 2bd556e commit dfc448f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
22 changes: 21 additions & 1 deletion data/shared/src/main/scala/sigma/ast/SigmaPredef.scala
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,25 @@ object SigmaPredef {
)
)

val DeserializeToFunc = PredefinedFunc("deserializeTo",
Lambda(Seq(paramT), Array("bytes" -> SByteArray), tT, None),
irInfo = PredefFuncInfo(
irBuilder = { case (u, args) =>
val resType = u.opType.tRange.asInstanceOf[SFunc].tRange
MethodCall(
Global,
SGlobalMethods.deserializeToMethod.withConcreteTypes(Map(tT -> resType)),
args.toIndexedSeq,
Map(tT -> resType)
)
}),
docInfo = OperationInfo(MethodCall,
"""Deserializes provided bytes into a value of given type using the default serialization format.
""".stripMargin,
Seq(ArgInfo("bytes", "bytes to deserialize"))
)
)

val globalFuncs: Map[String, PredefinedFunc] = Seq(
AllOfFunc,
AnyOfFunc,
Expand Down Expand Up @@ -448,7 +467,8 @@ object SigmaPredef {
SubstConstantsFunc,
ExecuteFromVarFunc,
ExecuteFromSelfRegFunc,
SerializeFunc
SerializeFunc,
DeserializeToFunc
).map(f => f.name -> f).toMap

def comparisonOp(symbolName: String, opDesc: ValueCompanion, desc: String, args: Seq[ArgInfo]) = {
Expand Down
9 changes: 5 additions & 4 deletions sc/shared/src/test/scala/sigma/LanguageSpecificationV6.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import scorex.util.ModifierId
import scorex.utils.{Ints, Longs, Shorts}
import sigma.ast.ErgoTree.ZeroHeader
import sigma.ast.SCollection.SByteArray
import sigma.ast.SType.tT
import sigma.ast.syntax.TrueSigmaProp
import sigma.ast.{SInt, _}
import sigma.crypto.SecP256K1Group
Expand Down Expand Up @@ -1524,8 +1525,8 @@ class LanguageSpecificationV6 extends LanguageSpecificationBase { suite =>
)
}

property("Global.deserializeTo") {
def checkPoW: Feature[GroupElement, Boolean] = {
property("Global.deserializeTo - group element") {
def deserializeTo: Feature[GroupElement, Boolean] = {
newFeature(
{ (x: GroupElement) => CSigmaDslBuilder.deserializeTo[GroupElement](SGroupElement, x.getEncoded) == x},
"{ (x: GroupElement) => Global.deserializeTo[GroupElement](x.getEncoded) == x }",
Expand All @@ -1534,7 +1535,7 @@ class LanguageSpecificationV6 extends LanguageSpecificationBase { suite =>
EQ(
MethodCall.typed[Value[SGroupElement.type]](
Global,
SGlobalMethods.deserializeToMethod,
SGlobalMethods.deserializeToMethod.withConcreteTypes(Map(tT -> SGroupElement)),
Vector(
MethodCall.typed[Value[SCollection[SByte.type]]](
ValUse(1, SGroupElement),
Expand All @@ -1556,7 +1557,7 @@ class LanguageSpecificationV6 extends LanguageSpecificationBase { suite =>
Seq(
CGroupElement(SecP256K1Group.generator) -> new Expected(ExpectedResult(Success(true), None))
),
checkPoW
deserializeTo
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,6 @@ class BasicOpsSpecification extends CompilerTestingCommons
}
}

// todo: failing, needs for Header (de)serialization support from https://github.com/ScorexFoundation/sigmastate-interpreter/pull/972
property("serialize - collection of collection of headers") {
val td = new SigmaTestingData {}
val h1 = td.TestData.h1
Expand All @@ -775,9 +774,6 @@ class BasicOpsSpecification extends CompilerTestingCommons
}
}

// todo: roundtrip tests with deserializeTo from https://github.com/ScorexFoundation/sigmastate-interpreter/pull/979

// todo: move spam tests to dedicated test suite?
property("serialize - not spam") {
val customExt = Seq(21.toByte -> ShortArrayConstant((1 to Short.MaxValue).map(_.toShort).toArray),
22.toByte -> ByteArrayConstant(Array.fill(1)(1.toByte)))
Expand Down Expand Up @@ -823,6 +819,26 @@ class BasicOpsSpecification extends CompilerTestingCommons
}
}

property("serialize - deserialize roundtrip") {
val customExt = Seq(21.toByte -> ShortArrayConstant((1 to 10).map(_.toShort).toArray))
def deserTest() = test("serialize", env, customExt,
s"""{
val src = getVar[Coll[Short]](21).get
val ba = serialize(src)
val restored = deserializeTo[Coll[Short]](ba)
src == restored
}""",
null,
true
)

if (activatedVersionInTests < V6SoftForkVersion) {
an[Exception] should be thrownBy deserTest()
} else {
deserTest()
}
}

property("deserializeTo - int") {
val value = -109253
val w = new VLQByteBufferWriter(new ByteArrayBuilder()).putInt(value)
Expand Down

0 comments on commit dfc448f

Please sign in to comment.