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

Resolve all TODO v5.x #911

Merged
merged 10 commits into from
Aug 31, 2023
7 changes: 0 additions & 7 deletions common/shared/src/main/scala/scalan/TypeDesc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ object RType {
case ClassTag.Short => ShortType
case ClassTag.Int => IntType
case ClassTag.Long => LongType
case ClassTag.Char => CharType
case ClassTag.Float => FloatType
case ClassTag.Double => DoubleType
case ClassTag.Unit => UnitType
case _ => GeneralType[A](ctA)
}).asInstanceOf[RType[A]]
Expand Down Expand Up @@ -72,10 +69,6 @@ object RType {
implicit val ShortType : RType[Short] = PrimitiveType[Short] (ClassTag.Short, Array.emptyShortArray)
implicit val IntType : RType[Int] = PrimitiveType[Int] (ClassTag.Int, Array.emptyIntArray)
implicit val LongType : RType[Long] = PrimitiveType[Long] (ClassTag.Long, Array.emptyLongArray)
// TODO v5.x: optimize: remove Char, Float, Double types, they are not supported and will never be
implicit val CharType : RType[Char] = PrimitiveType[Char] (ClassTag.Char, Array.emptyCharArray)
implicit val FloatType : RType[Float] = PrimitiveType[Float] (ClassTag.Float, Array.emptyFloatArray)
implicit val DoubleType : RType[Double] = PrimitiveType[Double] (ClassTag.Double, Array.emptyDoubleArray)
implicit val UnitType : RType[Unit] = PrimitiveType[Unit] (ClassTag.Unit, Array[Unit]()(ClassTag.Unit))

implicit case object StringType extends RType[String] {
Expand Down
4 changes: 2 additions & 2 deletions core-lib/shared/src/test/scala/special/TypesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class TypesTests extends BaseTests {
def test[A](t: RType[A], n: String) = {
t.name shouldBe n
}
test(tupleRType(Array(IntType, LongType, RType[(String, Double)], RType[Option[Boolean]])),
"(Int, Long, (String, Double), Option[Boolean])")
test(tupleRType(Array(IntType, LongType, RType[(Byte, special.sigma.BigInt)], RType[Option[Boolean]])),
"(Int, Long, (Byte, BigInt), Option[Boolean])")
}

test("RType implements equality") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,24 +433,6 @@ class CollsTests extends AnyPropSpec with ScalaCheckPropertyChecks with Matchers

checkColls(repl, coll)
}
forAll(charGen, indexGen, minSuccess) { (x, n) =>
val repl = builder.replicate(n, x)
val coll = builder.fromArray(Array.fill(n)(x))

checkColls(repl, coll)
}
forAll(floatGen, indexGen, minSuccess) { (x, n) =>
val repl = builder.replicate(n, x)
val coll = builder.fromArray(Array.fill(n)(x))

checkColls(repl, coll)
}
forAll (doubleGen, indexGen, minSuccess) { (x, n) =>
val repl = builder.replicate(n, x)
val coll = builder.fromArray(Array.fill(n)(x))

checkColls(repl, coll)
}
forAll (indexGen, minSuccess) { (n) =>
val replTrue = builder.replicate(n, true)
val collTrue = builder.fromArray(Array.fill(n)(true))
Expand Down Expand Up @@ -479,13 +461,13 @@ class CollsTests extends AnyPropSpec with ScalaCheckPropertyChecks with Matchers

checkColls(repl, coll)
}
forAll (byteGen, doubleGen, intGen, indexGen, minSuccess) { (b, d, i, n) =>
forAll (byteGen, longGen, intGen, indexGen, minSuccess) { (b, d, i, n) =>
val repl = builder.replicate(n, (b, (i, (d, b))))
val coll = builder.fromArray(Array.fill[(Byte, (Int, (Double, Byte)))](n)((b, (i, (d, b)))))
val coll = builder.fromArray(Array.fill[(Byte, (Int, (Long, Byte)))](n)((b, (i, (d, b)))))

checkColls(repl, coll)
}
forAll (byteGen, doubleGen, intGen, indexGen, indexGen, minSuccess) { (b, d, i, n, m) =>
forAll (byteGen, longGen, intGen, indexGen, indexGen, minSuccess) { (b, d, i, n, m) =>
val repl = builder.replicate(n, (b, ((i, (("string", builder.replicate(m, n)), Array(1, 2, 3, 4))), (d, b))))
val coll = builder.fromArray(Array.fill(n)((b, ((i, (("string", builder.fromArray(Array.fill(m)(n))), Array(1, 2, 3, 4))), (d, b)))))

Expand Down
17 changes: 7 additions & 10 deletions graph-ir/shared/src/main/scala/scalan/Base.scala
Original file line number Diff line number Diff line change
Expand Up @@ -301,16 +301,13 @@ abstract class Base { scalan: Scalan =>
def lift(srcF: SA => SB): Ref[A => B] = FuncConst[SA,SB,A,B](srcF)
}

implicit lazy val BooleanIsLiftable = asLiftable[Boolean,Boolean](BooleanElement.liftable)
implicit lazy val ByteIsLiftable = asLiftable[Byte,Byte](ByteElement.liftable)
implicit lazy val ShortIsLiftable = asLiftable[Short,Short](ShortElement.liftable)
implicit lazy val IntIsLiftable = asLiftable[Int,Int](IntElement.liftable)
implicit lazy val LongIsLiftable = asLiftable[Long,Long](LongElement.liftable)
implicit lazy val StringIsLiftable = asLiftable[String,String](StringElement.liftable)
implicit lazy val FloatIsLiftable = asLiftable[Float,Float](FloatElement.liftable)
implicit lazy val DoubleIsLiftable = asLiftable[Double,Double](DoubleElement.liftable)
implicit lazy val UnitIsLiftable = asLiftable[Unit,Unit](UnitElement.liftable)
implicit lazy val CharIsLiftable = asLiftable[Char,Char](CharElement.liftable)
implicit lazy val BooleanIsLiftable: Liftable[Boolean, Boolean] = asLiftable[Boolean,Boolean](BooleanElement.liftable)
implicit lazy val ByteIsLiftable: Liftable[Byte, Byte] = asLiftable[Byte,Byte](ByteElement.liftable)
implicit lazy val ShortIsLiftable: Liftable[Short, Short] = asLiftable[Short,Short](ShortElement.liftable)
implicit lazy val IntIsLiftable: Liftable[Int, Int] = asLiftable[Int,Int](IntElement.liftable)
implicit lazy val LongIsLiftable: Liftable[Long, Long] = asLiftable[Long,Long](LongElement.liftable)
implicit lazy val StringIsLiftable: Liftable[String, String] = asLiftable[String,String](StringElement.liftable)
implicit lazy val UnitIsLiftable: Liftable[Unit, Unit] = asLiftable[Unit,Unit](UnitElement.liftable)

implicit def PairIsLiftable[SA,SB,A,B]
(implicit lA: Liftable[SA, A], lB: Liftable[SB, B]): Liftable[(SA, SB), (A, B)] =
Expand Down
3 changes: 0 additions & 3 deletions graph-ir/shared/src/main/scala/scalan/TypeDescs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,8 @@ abstract class TypeDescs extends Base { self: Scalan =>
implicit val ShortElement: Elem[Short] = new BaseElemLiftable(0.toShort, ShortType)
implicit val IntElement: Elem[Int] = new BaseElemLiftable(0, IntType)
implicit val LongElement: Elem[Long] = new BaseElemLiftable(0L, LongType)
implicit val FloatElement: Elem[Float] = new BaseElemLiftable(0.0F, FloatType)
implicit val DoubleElement: Elem[Double] = new BaseElemLiftable(0.0, DoubleType)
implicit val UnitElement: Elem[Unit] = new BaseElemLiftable((), UnitType)
implicit val StringElement: Elem[String] = new BaseElemLiftable("", StringType)
implicit val CharElement: Elem[Char] = new BaseElemLiftable('\u0000', CharType)

/** Implicitly defines element type for pairs. */
implicit final def pairElement[A, B](implicit ea: Elem[A], eb: Elem[B]): Elem[(A, B)] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ object Platform {
case _: Int => tpe == SInt
case _: Long => tpe == SLong
case _: BigInt => tpe == SBigInt
case _: String => tpe == SString // TODO v6.0: remove this case
case _: String => tpe == SString // TODO v6.0: remove this case (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/905)
case _: GroupElement => tpe.isGroupElement
case _: SigmaProp => tpe.isSigmaProp
case _: AvlTree => tpe.isAvlTree
Expand Down
39 changes: 31 additions & 8 deletions interpreter/shared/src/main/scala/org/ergoplatform/ErgoBox.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import special.collection._
* A transaction is unsealing a box. As a box can not be open twice, any further valid transaction can not be linked
* to the same box.
*
* Note, private constructor can only be used from within the ErgoBox companion object, e.g. by deserializer.
*
* @param value - amount of money associated with the box
* @param ergoTree - guarding script, which should be evaluated to true in order to open this box
* @param additionalTokens - secondary tokens the box contains
Expand All @@ -44,17 +46,28 @@ import special.collection._
* @param creationHeight - height when a transaction containing the box was created.
* This height is declared by user and should not exceed height of the block,
* containing the transaction with this box.
* @param _bytes - serialized bytes of the box when not `null`
* HOTSPOT: don't beautify the code of this class
*/
class ErgoBox(
class ErgoBox private (
override val value: Long,
override val ergoTree: ErgoTree,
override val additionalTokens: Coll[Token] = Colls.emptyColl[Token],
override val additionalRegisters: AdditionalRegisters = Map.empty,
override val additionalTokens: Coll[Token],
override val additionalRegisters: AdditionalRegisters,
val transactionId: ModifierId,
val index: Short,
override val creationHeight: Int
override val creationHeight: Int,
_bytes: Array[Byte]
) extends ErgoBoxCandidate(value, ergoTree, creationHeight, additionalTokens, additionalRegisters) {
/** This is public constructor has the same parameters as the private primary constructor, except bytes. */
def this(value: Long,
ergoTree: ErgoTree,
additionalTokens: Coll[Token] = Colls.emptyColl[Token],
additionalRegisters: AdditionalRegisters = Map.empty,
transactionId: ModifierId,
index: Short,
creationHeight: Int) =
this(value, ergoTree, additionalTokens, additionalRegisters, transactionId, index, creationHeight, null)

import ErgoBox._

Expand All @@ -70,11 +83,15 @@ class ErgoBox(
}
}

// TODO optimize: avoid serialization by implementing lazy box deserialization
/** Serialized content of this box.
* @see [[ErgoBox.sigmaSerializer]]
*/
lazy val bytes: Array[Byte] = ErgoBox.sigmaSerializer.toBytes(this)
lazy val bytes: Array[Byte] = {
if (_bytes != null)
_bytes // bytes provided by deserializer
else
ErgoBox.sigmaSerializer.toBytes(this)
}

override def equals(arg: Any): Boolean = arg match {
case x: ErgoBox => java.util.Arrays.equals(id, x.id)
Expand Down Expand Up @@ -197,10 +214,16 @@ object ErgoBox {
}

override def parse(r: SigmaByteReader): ErgoBox = {
val ergoBoxCandidate = ErgoBoxCandidate.serializer.parse(r)
val start = r.position
val c = ErgoBoxCandidate.serializer.parse(r)
val transactionId = r.getBytes(ErgoLikeTransaction.TransactionIdBytesSize).toModifierId
val index = r.getUShort()
ergoBoxCandidate.toBox(transactionId, index.toShort)
val end = r.position
val len = end - start
r.position = start
val boxBytes = r.getBytes(len) // also moves position back to end
new ErgoBox(c.value, c.ergoTree, c.additionalTokens, c.additionalRegisters,
transactionId, index.toShort, c.creationHeight, boxBytes)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter}
import scalan.util.Extensions.{IntOps, LongOps}
import sigmastate.exceptions.SerializerException

// TODO v5.x: remove unused class and related json encoders
/** The rules are serialized ordered by ruleId.
* This serializer preserves roundtrip identity `deserialize(serialize(_)) = identity`
* however it may not preserve `serialize(deserialize(_)) = identity` */
Expand Down
26 changes: 0 additions & 26 deletions interpreter/shared/src/main/scala/sigmastate/Values.scala
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,6 @@ object Values {
val dhtSerializer = ProveDHTupleSerializer(ProveDHTuple.apply)
val dlogSerializer = ProveDlogSerializer(ProveDlog.apply)

// TODO v5.x: control maxTreeDepth same as in deserialize
override def serialize(data: SigmaBoolean, w: SigmaByteWriter): Unit = {
w.put(data.opCode)
data match {
Expand Down Expand Up @@ -878,31 +877,6 @@ object Values {
def apply(items: Value[SType]*): Tuple = Tuple(items.toIndexedSeq)
}

trait OptionValue[T <: SType] extends Value[SOption[T]] {
}

// TODO v6.0 (4h): SomeValue and NoneValue are not used in ErgoTree and can be
// either removed or implemented in v6.0
case class SomeValue[T <: SType](x: Value[T]) extends OptionValue[T] {
override def companion = SomeValue
val tpe = SOption(x.tpe)
def opType = SFunc(x.tpe, tpe)
}
object SomeValue extends ValueCompanion {
override val opCode = SomeValueCode
override def costKind: CostKind = Constant.costKind
}

case class NoneValue[T <: SType](elemType: T) extends OptionValue[T] {
override def companion = NoneValue
val tpe = SOption(elemType)
def opType = SFunc(elemType, tpe)
}
object NoneValue extends ValueCompanion {
override val opCode = NoneValueCode
override def costKind: CostKind = Constant.costKind
}

/** ErgoTree node which converts a collection of expressions into a collection of data
* values. Each data value of the resulting collection is obtained by evaluating the
* corresponding expression in `items`. All items must have the same type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ case class CSigmaProp(sigmaTree: SigmaBoolean) extends SigmaProp with WrapperOf[
override def propBytes: Coll[Byte] = {
// in order to have comparisons like `box.propositionBytes == pk.propBytes` we need to make sure
// the same serialization method is used in both cases
// TODO v6.0: add `pk.propBytes(version)`
// TODO v6.0: add `pk.propBytes(version)` (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/903)
val root = sigmaTree.toSigmaProp
val ergoTree = new ErgoTree(ErgoTree.DefaultHeader, EmptyConstants, Right(root), 0, null, None)
val bytes = DefaultSerializer.serializeErgoTree(ergoTree)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,6 @@ object Evaluation {
case _: Short => ShortType
case _: Int => IntType
case _: Long => LongType
case _: Char => CharType
case _: Float => FloatType
case _: Double => DoubleType
case _: String => StringType
case _: Unit => UnitType

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ class TyperException(message: String, source: Option[SourceContext] = None)
class BuilderException(message: String, source: Option[SourceContext] = None)
extends CompilerException(message, source)

// TODO v5.x: remove this exception
class CosterException(message: String, source: Option[SourceContext], cause: Option[Throwable] = None)
class GraphBuildingException(message: String, source: Option[SourceContext], cause: Option[Throwable] = None)
extends CompilerException(message, source, cause)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ trait Interpreter {
* @throws InterpreterException when cannot proceed and no activation yet.
*/
protected def checkSoftForkCondition(ergoTree: ErgoTree, context: CTX): Option[VerificationResult] = {
// TODO v6.0: the condition below should be revised if necessary
// TODO v6.0: the condition below should be revised if necessary (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/904)
// The following conditions define behavior which depend on the version of ergoTree
// This works in addition to more fine-grained soft-forkability mechanism implemented
// using ValidationRules (see trySoftForkable method call here and in reduceToCrypto).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,6 @@ abstract class SigmaBuilder {

def mkTaggedVariable[T <: SType](varId: Byte, tpe: T): TaggedVariable[T]

def mkSomeValue[T <: SType](x: Value[T]): Value[SOption[T]]
def mkNoneValue[T <: SType](elemType: T): Value[SOption[T]]

def mkBlock(bindings: Seq[Val], result: Value[SType]): Value[SType]
def mkBlockValue(items: IndexedSeq[BlockItem], result: Value[SType]): Value[SType]
def mkValUse(valId: Int, tpe: SType): Value[SType]
Expand Down Expand Up @@ -527,11 +524,6 @@ class StdSigmaBuilder extends SigmaBuilder {
override def mkTaggedVariable[T <: SType](varId: Byte, tpe: T): TaggedVariable[T] =
TaggedVariableNode(varId, tpe).withSrcCtx(currentSrcCtx.value).asInstanceOf[TaggedVariable[T]]

override def mkSomeValue[T <: SType](x: Value[T]): Value[SOption[T]] =
SomeValue(x).withSrcCtx(currentSrcCtx.value)
override def mkNoneValue[T <: SType](elemType: T): Value[SOption[T]] =
NoneValue(elemType).withSrcCtx(currentSrcCtx.value)

override def mkBlock(bindings: Seq[Val], result: Value[SType]): Value[SType] =
Block(bindings, result).withSrcCtx(currentSrcCtx.value)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import scala.collection.mutable
/** This works in tandem with ConstantSerializer, if you change one make sure to check the other.*/
object DataSerializer {

// TODO v5.x: control maxTreeDepth same as in deserialize
/** Use type descriptor `tpe` to deconstruct type structure and recursively serialize subcomponents.
* Primitive types are leaves of the type tree, and they are served as basis of recursion.
* The data value `v` is expected to conform to the type described by `tpe`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import sigmastate.utils.SigmaByteWriter.DataInfo
import sigmastate.utils.{SigmaByteReader, SigmaByteWriter}
import sigmastate.{ModQArithOpCompanion, SType, ModQArithOp}

// TODO v6.0 (2h): make sure it is covered with tests
// TODO v6.0: make sure it is covered with tests (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/327)
case class ModQArithOpSerializer(override val opDesc: ModQArithOpCompanion, cons: (BigIntValue, BigIntValue) => BigIntValue)
extends ValueSerializer[ModQArithOp] {
val leftInfo: DataInfo[SValue] = opDesc.argInfos(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import sigmastate.lang.Terms._
import sigmastate.utils.{SigmaByteReader, SigmaByteWriter}
import sigmastate.{ModQ, SType}

// TODO v6.0 (2h): make sure it is covered with tests
// TODO v6.0: make sure it is covered with tests (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/327)
object ModQSerializer extends ValueSerializer[ModQ] {
override def opDesc = ModQ

Expand Down
Loading
Loading