Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement missing ops and predefined functions for 2.0 #400

Merged
merged 30 commits into from
Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
49dbe17
add byteArrayToLong;
greenhat Feb 19, 2019
0bc2c2b
add xorOf;
greenhat Feb 19, 2019
60fde65
add (de)serialization roundtrip into compilation pipeline in tests;
greenhat Feb 20, 2019
6f3f4e4
fix numeric casts test (got reduced to constants);
greenhat Feb 20, 2019
9cc74ba
add MethodCall.typeSubst (map of concrete types for each generic type…
greenhat Feb 20, 2019
699d55c
fix SOption.method to have generic types (don't substitute for SOptio…
greenhat Feb 21, 2019
3899508
fix build after rebase;
greenhat Feb 21, 2019
4a2f89b
enable tests for Coll.patch, .updated, .updateMany;
greenhat Feb 21, 2019
cd0d4e8
add BitOp serialization;
greenhat Feb 21, 2019
6fbeb9b
add BinXor serialization;
greenhat Feb 21, 2019
a8625a0
add LogicalNotSerializer;
greenhat Feb 21, 2019
be490bf
add SigmaDslBuilder.byteArrayToLong;
greenhat Feb 21, 2019
3745d2e
add SigmaDslBuilder.byteArrayToLong implementation (codegen);
greenhat Feb 21, 2019
9668338
disable (de)serialization roundtrip for ZProofBlock test;
greenhat Feb 22, 2019
2f3cd15
add sigma API for xorOf, logicalNot, negation, logicalXor;
greenhat Feb 22, 2019
edcf2c0
add codegen for sigma API xorOf, logicalNot, negation, logicalXor;
greenhat Feb 22, 2019
24de39f
add xorOf handling in costing;
greenhat Feb 22, 2019
fcf3058
add logicalNot full implementation;
greenhat Feb 22, 2019
2abd8ec
add numeric negate full implementation;
greenhat Feb 22, 2019
f4e6ba6
fix SigmaCompilerTest;
greenhat Feb 22, 2019
823e0cd
disable SigmaDslTest xor test;
greenhat Feb 22, 2019
1daf285
build fix after rebase;
greenhat Feb 22, 2019
a81ffea
remove adaptSigmaBoolean in XorOf costing;
greenhat Feb 22, 2019
55eb2d1
add special.sigma.BigInt test (failing);
greenhat Feb 23, 2019
237de80
fix byteArrayToLong and xorOf tests to test against SigmaDsl;
greenhat Feb 23, 2019
391cec0
make SOption a SGenericType;
greenhat Feb 24, 2019
eb4b3c1
remove type param index in MethodCall serialization (only concrete ty…
greenhat Feb 25, 2019
37804f6
add Box.getReg handling via MethodCall throughout the whole pipeline
greenhat Feb 26, 2019
772a030
fix build;
greenhat Feb 26, 2019
2a5524a
fix and enable special.sigma.BigInt negation SigmaDsl test;
greenhat Feb 26, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions src/main/scala/sigmastate/serialization/MethodCallSerializer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sigmastate.serialization
import sigmastate.Values._
import sigmastate._
import sigmastate.lang.SigmaTyper.STypeSubst
import sigmastate.lang.Terms.MethodCall
import sigmastate.lang.Terms.{MethodCall, STypeParam}
import sigmastate.utils.{SigmaByteReader, SigmaByteWriter}

case class MethodCallSerializer(opCode: Byte, cons: (Value[SType], SMethod, IndexedSeq[Value[SType]], STypeSubst) => Value[SType])
Expand All @@ -17,25 +17,38 @@ case class MethodCallSerializer(opCode: Byte, cons: (Value[SType], SMethod, Inde
assert(mc.args.nonEmpty)
w.putValues(mc.args)
}
w.putUByte(mc.typeSubst.size)
mc.typeSubst.foreach { case (typeIdent, tpe) => w.putType(typeIdent).putType(tpe) }
mc.method.stype match {
case genType: SGenericType =>
w.putUByte(mc.typeSubst.size)
mc.typeSubst.foreach { case (ti, tpe) =>
val tpIndex = genType.substitutedTypeParams.indexOf(STypeParam(ti))
w.putUByte(tpIndex)
greenhat marked this conversation as resolved.
Show resolved Hide resolved
.putType(tpe)
}
case _ => w.putUByte(0)
}
}

override def parse(r: SigmaByteReader): Value[SType] = {
val typeId = r.getByte()
val methodId = r.getByte()
val obj = r.getValue()
val args = if (opCode == OpCodes.MethodCallCode) {
r.getValues()
}
else IndexedSeq()
val args = if (opCode == OpCodes.MethodCallCode) r.getValues() else IndexedSeq()
val method = MethodCall.fromIds(typeId, methodId)
val typeSubstSize = r.getUByte()
val xs = new Array[(STypeIdent, SType)](typeSubstSize)
for (i <- 0 until typeSubstSize) {
xs(i) = (r.getType().asInstanceOf[STypeIdent], r.getType())
val typeSubst: STypeSubst = method.stype match {
case genType: SGenericType =>
val typeSubstSize = r.getUByte()
val xs = new Array[(STypeIdent, SType)](typeSubstSize)
for (i <- 0 until typeSubstSize) {
val tpIndex = r.getUByte()
val ti = genType.substitutedTypeParams.apply(tpIndex).ident
xs(i) = (ti, r.getType())
}
xs.toMap
case _ =>
r.getUByte() // read 0
Map()
}
val typeSubst = xs.toMap
cons(obj, method, args, typeSubst)
}
}
26 changes: 20 additions & 6 deletions src/main/scala/sigmastate/types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,16 @@ trait SProduct extends SType {
* e.g. Array[T], Option[T], etc.)*/
trait SGenericType {
def typeParams: Seq[STypeParam]
def tparamSubst: Map[String, SType]
def tparamSubst: Map[STypeIdent, SType]

lazy val substitutedTypeParams: Seq[STypeParam] =
typeParams.map { tp =>
tparamSubst.getOrElse(tp.ident, tp.ident) match {
case v: STypeIdent => STypeParam(v)
case _ => tp
}
}

}

/** Method info including name, arg type and result type.
Expand Down Expand Up @@ -565,7 +574,8 @@ case object SUnit extends SPrimType {

/** Type description of optional values. Instances of `Option`
* are either constructed by `Some` or by `None` constructors. */
case class SOption[ElemType <: SType](elemType: ElemType) extends SProduct {
case class SOption[ElemType <: SType](elemType: ElemType) extends SProduct with SGenericType {
import SOption._
override type WrappedType = Option[ElemType#WrappedType]
override val typeCode: TypeCode = SOption.OptionTypeCode
override def dataSize(v: SType#WrappedType) = {
Expand All @@ -576,6 +586,8 @@ case class SOption[ElemType <: SType](elemType: ElemType) extends SProduct {
def ancestors = Nil
override def methods: Seq[SMethod] = SOption.methods
override def toString = s"Option[$elemType]"
val typeParams: Seq[STypeParam] = Seq(STypeParam(tT))
def tparamSubst: Map[STypeIdent, SType] = Map(tT -> elemType)
}

object SOption extends STypeCompanion {
Expand Down Expand Up @@ -662,8 +674,8 @@ case class SCollectionType[T <: SType](elemType: T) extends SCollection[T] {
arr.map(x => elemType.dataSize(x)).sum
res
}
def typeParams = SCollectionType.typeParams
def tparamSubst = Map(tIV.name -> elemType)
def typeParams: Seq[STypeParam] = SCollectionType.typeParams
def tparamSubst: Map[STypeIdent, SType] = Map(tIV -> elemType)
override def methods = SCollection.methods
override def toString = s"Coll[$elemType]"
}
Expand Down Expand Up @@ -921,10 +933,12 @@ case class SFunc(tDom: IndexedSeq[SType], tRange: SType, tpeParams: Seq[STypePa
}
override def dataSize(v: SType#WrappedType) = 8L
import SFunc._
val typeParams: Seq[STypeParam] = (tDom.zipWithIndex.map { case (t, i) => STypeParam(tD.name + (i + 1)) }) :+ STypeParam(tR.name)
val tparamSubst = typeParams.zip(tDom).map { case (p, t) => p.ident.name -> t }.toMap + (tR.name -> tRange)
val typeParams: Seq[STypeParam] = tpeParams
val tparamSubst: Map[STypeIdent, SType] = Map() // defined in MethodCall.typeSubst

def getGenericType: SFunc = {
val typeParams: Seq[STypeParam] = tDom.zipWithIndex
.map { case (t, i) => STypeParam(tD.name + (i + 1)) } :+ STypeParam(tR.name)
val ts = typeParams.map(_.ident)
SFunc(ts.init.toIndexedSeq, ts.last, Nil)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.ergoplatform.Outputs
import sigmastate.Values.{FuncValue, ValUse}
import sigmastate.lang.Terms.MethodCall
import sigmastate.utxo.ExtractScriptBytes
import sigmastate.{SBox, SByte, SCollection}
import sigmastate._

class MethodCallSerializerSpecification extends SerializationSpecification {

Expand All @@ -17,4 +17,12 @@ class MethodCallSerializerSpecification extends SerializationSpecification {
roundTripTest(expr)
}

property("MethodCall deserialization round trip (non-generic method)") {
val expr = MethodCall(Outputs,
SMethod(SCollection, "size", SInt, 1),
Vector(),
Map()
)
roundTripTest(expr)
}
}