Skip to content

Commit

Permalink
tree version based versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Dec 13, 2024
1 parent e2720be commit 59225d3
Show file tree
Hide file tree
Showing 22 changed files with 285 additions and 247 deletions.
6 changes: 1 addition & 5 deletions core/shared/src/main/scala/sigma/VersionContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ case class VersionContext(activatedVersion: Byte, ergoTreeVersion: Byte) {

/** @return true, if the activated script version of Ergo protocol on the network is
* including v6.0 update. */
def isV6SoftForkActivated: Boolean = activatedVersion >= V6SoftForkVersion

def isV6SoftForkActivatedAndTreeV3: Boolean = {
activatedVersion >= V6SoftForkVersion && ergoTreeVersion >= V6SoftForkVersion
}
def isV3OrLaterErgoTreeVersion: Boolean = ergoTreeVersion >= V6SoftForkVersion

}

Expand Down
26 changes: 13 additions & 13 deletions core/shared/src/main/scala/sigma/ast/SType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ object SType {
/** All pre-defined types should be listed here. Note, NoType is not listed.
* Should be in sync with sigmastate.lang.Types.predefTypes. */
def allPredefTypes: Seq[SType] = {
if(VersionContext.current.isV6SoftForkActivated) {
if(VersionContext.current.isV3OrLaterErgoTreeVersion) {
v6PredefTypes
} else {
v5PredefTypes
Expand Down Expand Up @@ -164,7 +164,7 @@ object SType {

private val v6TypesMap = v6Types.map { t => (t.typeId, t) }.toMap

def types: Map[Byte, STypeCompanion] = if (VersionContext.current.isV6SoftForkActivated) {
def types: Map[Byte, STypeCompanion] = if (VersionContext.current.isV3OrLaterErgoTreeVersion) {
v6TypesMap
} else {
v5TypesMap
Expand All @@ -191,7 +191,7 @@ object SType {
case SInt => x.isInstanceOf[Int]
case SLong => x.isInstanceOf[Long]
case SBigInt => x.isInstanceOf[BigInt]
case SUnsignedBigInt if VersionContext.current.isV6SoftForkActivated => x.isInstanceOf[UnsignedBigInt]
case SUnsignedBigInt if VersionContext.current.isV3OrLaterErgoTreeVersion => x.isInstanceOf[UnsignedBigInt]
case SGroupElement => x.isInstanceOf[GroupElement]
case SSigmaProp => x.isInstanceOf[SigmaProp]
case SBox => x.isInstanceOf[Box]
Expand Down Expand Up @@ -409,8 +409,8 @@ case object SByte extends SPrimType with SEmbeddable with SNumericType with SMon
case s: Short => s.toByteExact
case i: Int => i.toByteExact
case l: Long => l.toByteExact
case bi: BigInt if VersionContext.current.isV6SoftForkActivated => bi.toByte // toByteExact from int is called under the hood
case ubi: UnsignedBigInt if VersionContext.current.isV6SoftForkActivated => ubi.toByte // toByteExact from int is called under the hood
case bi: BigInt if VersionContext.current.isV3OrLaterErgoTreeVersion => bi.toByte // toByteExact from int is called under the hood
case ubi: UnsignedBigInt if VersionContext.current.isV3OrLaterErgoTreeVersion => ubi.toByte // toByteExact from int is called under the hood
case _ => sys.error(s"Cannot downcast value $v to the type $this")
}
}
Expand All @@ -432,8 +432,8 @@ case object SShort extends SPrimType with SEmbeddable with SNumericType with SMo
case s: Short => s
case i: Int => i.toShortExact
case l: Long => l.toShortExact
case bi: BigInt if VersionContext.current.isV6SoftForkActivated => bi.toShort // toShortExact from int is called under the hood
case ubi: UnsignedBigInt if VersionContext.current.isV6SoftForkActivated => ubi.toShort // toShortExact from int is called under the hood
case bi: BigInt if VersionContext.current.isV3OrLaterErgoTreeVersion => bi.toShort // toShortExact from int is called under the hood
case ubi: UnsignedBigInt if VersionContext.current.isV3OrLaterErgoTreeVersion => ubi.toShort // toShortExact from int is called under the hood
case _ => sys.error(s"Cannot downcast value $v to the type $this")
}
}
Expand All @@ -457,8 +457,8 @@ case object SInt extends SPrimType with SEmbeddable with SNumericType with SMono
case s: Short => s.toInt
case i: Int => i
case l: Long => l.toIntExact
case bi: BigInt if VersionContext.current.isV6SoftForkActivated => bi.toInt
case ubi: UnsignedBigInt if VersionContext.current.isV6SoftForkActivated => ubi.toInt
case bi: BigInt if VersionContext.current.isV3OrLaterErgoTreeVersion => bi.toInt
case ubi: UnsignedBigInt if VersionContext.current.isV3OrLaterErgoTreeVersion => ubi.toInt
case _ => sys.error(s"Cannot downcast value $v to the type $this")
}
}
Expand All @@ -484,8 +484,8 @@ case object SLong extends SPrimType with SEmbeddable with SNumericType with SMon
case s: Short => s.toLong
case i: Int => i.toLong
case l: Long => l
case bi: BigInt if VersionContext.current.isV6SoftForkActivated => bi.toLong
case ubi: UnsignedBigInt if VersionContext.current.isV6SoftForkActivated => ubi.toLong
case bi: BigInt if VersionContext.current.isV3OrLaterErgoTreeVersion => bi.toLong
case ubi: UnsignedBigInt if VersionContext.current.isV3OrLaterErgoTreeVersion => ubi.toLong
case _ => sys.error(s"Cannot downcast value $v to the type $this")
}
}
Expand All @@ -512,7 +512,7 @@ case object SBigInt extends SPrimType with SEmbeddable with SNumericType with SM
case x: Short => CBigInt(BigInteger.valueOf(x.toLong))
case x: Int => CBigInt(BigInteger.valueOf(x.toLong))
case x: Long => CBigInt(BigInteger.valueOf(x))
case x: BigInt if VersionContext.current.isV6SoftForkActivated => x
case x: BigInt if VersionContext.current.isV3OrLaterErgoTreeVersion => x
case _ => sys.error(s"Cannot upcast value $v to the type $this")
}
}
Expand All @@ -524,7 +524,7 @@ case object SBigInt extends SPrimType with SEmbeddable with SNumericType with SM
case x: Short => CBigInt(BigInteger.valueOf(x.toLong))
case x: Int => CBigInt(BigInteger.valueOf(x.toLong))
case x: Long => CBigInt(BigInteger.valueOf(x))
case x: BigInt if VersionContext.current.isV6SoftForkActivated => x
case x: BigInt if VersionContext.current.isV3OrLaterErgoTreeVersion => x
case _ => sys.error(s"Cannot downcast value $v to the type $this")
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/shared/src/main/scala/sigma/data/CollsOverArrays.scala
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class CollOverArray[@specialized A](val toArray: Array[A], val builder: CollBuil
case obj: CollOverArray[_] if obj.tItem == this.tItem =>
java.util.Objects.deepEquals(obj.toArray, this.toArray)
case obj: PairColl[Any, Any] if obj.tItem == this.tItem =>
if (VersionContext.current.isV6SoftForkActivated) {
if (VersionContext.current.isV3OrLaterErgoTreeVersion) {
equalsPairCollWithCollOverArray(obj, this.asInstanceOf[CollOverArray[Any]])
} else {
false
Expand Down Expand Up @@ -282,7 +282,7 @@ class PairOfCols[@specialized L, @specialized R](val ls: Coll[L], val rs: Coll[R
case that: PairColl[_, _] if that.tItem == this.tItem =>
ls == that.ls && rs == that.rs
case that: CollOverArray[Any] if that.tItem == this.tItem =>
if (VersionContext.current.isV6SoftForkActivated) {
if (VersionContext.current.isV3OrLaterErgoTreeVersion) {
equalsPairCollWithCollOverArray(this.asInstanceOf[PairColl[Any, Any]], that)
} else {
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CoreDataSerializer {
val data = v.asInstanceOf[BigInt].toBigInteger.toByteArray
w.putUShort(data.length)
w.putBytes(data)
case SUnsignedBigInt if VersionContext.current.isV6SoftForkActivated =>
case SUnsignedBigInt if VersionContext.current.isV3OrLaterErgoTreeVersion =>
val data = BigIntegers.asUnsignedByteArray(v.asInstanceOf[CUnsignedBigInt].wrappedValue)
w.putUShort(data.length)
w.putBytes(data)
Expand Down Expand Up @@ -73,7 +73,7 @@ class CoreDataSerializer {
i += 1
}

case SOption(elemType) if VersionContext.current.isV6SoftForkActivated =>
case SOption(elemType) if VersionContext.current.isV3OrLaterErgoTreeVersion =>
val o = v.asInstanceOf[Option[elemType.WrappedType]]
w.putOption(o){case (w, v) =>
serialize(v, elemType, w)
Expand Down Expand Up @@ -113,7 +113,7 @@ class CoreDataSerializer {
}
val valueBytes = r.getBytes(size)
CBigInt(new BigInteger(valueBytes))
case SUnsignedBigInt if VersionContext.current.isV6SoftForkActivated =>
case SUnsignedBigInt if VersionContext.current.isV3OrLaterErgoTreeVersion =>
val size: Short = r.getUShort().toShort
if (size > SBigInt.MaxSizeInBytes) {
throw SerializerException(s"BigInt value doesn't not fit into ${SBigInt.MaxSizeInBytes} bytes: $size")
Expand All @@ -135,7 +135,7 @@ class CoreDataSerializer {
}.toArray[Any]
val coll = Colls.fromArray(arr)(sigma.AnyType)
Evaluation.toDslTuple(coll, tuple)
case tOption: SOption[_] if VersionContext.current.isV6SoftForkActivated =>
case tOption: SOption[_] if VersionContext.current.isV3OrLaterErgoTreeVersion =>
r.getOption[tOption.ElemWrappedType] {
deserialize(tOption.elemType, r).asInstanceOf[tOption.ElemWrappedType]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class TypeSerializer {
case SHeader.typeCode => SHeader
case SPreHeader.typeCode => SPreHeader
case SGlobal.typeCode => SGlobal
case SFunc.FuncTypeCode if VersionContext.current.isV6SoftForkActivated =>
case SFunc.FuncTypeCode if VersionContext.current.isV3OrLaterErgoTreeVersion =>
val tdLength = r.getUByte()

val tDom = (1 to tdLength).map { _ =>
Expand Down Expand Up @@ -243,7 +243,7 @@ object TypeSerializer extends TypeSerializer {
/** The list of embeddable types, i.e. types that can be combined with type constructor for optimized encoding.
* For each embeddable type `T`, and type constructor `C`, the type `C[T]` can be represented by single byte. */
def embeddableIdToType = {
if (VersionContext.current.isV6SoftForkActivated) {
if (VersionContext.current.isV3OrLaterErgoTreeVersion) {
embeddableV6
} else {
embeddableV5
Expand Down
26 changes: 13 additions & 13 deletions data/js/src/main/scala/sigma/Platform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,33 @@ object Platform {
private[sigma] def liftToConstant(obj: Any, builder: SigmaBuilder): Nullable[Constant[SType]] = {
import builder._
obj match {
case arr: Array[Boolean] if !VersionContext.current.isV6SoftForkActivated => Nullable(mkCollectionConstant[SBoolean.type](arr, SBoolean))
case arr: Array[Byte] if !VersionContext.current.isV6SoftForkActivated => Nullable(mkCollectionConstant[SByte.type](arr, SByte))
case arr: Array[Short] if !VersionContext.current.isV6SoftForkActivated => Nullable(mkCollectionConstant[SShort.type](arr, SShort))
case arr: Array[Int] if !VersionContext.current.isV6SoftForkActivated => Nullable(mkCollectionConstant[SInt.type](arr, SInt))
case arr: Array[Long] if !VersionContext.current.isV6SoftForkActivated => Nullable(mkCollectionConstant[SLong.type](arr, SLong))
case arr: Array[BigInteger] if !VersionContext.current.isV6SoftForkActivated => Nullable(mkCollectionConstant[SBigInt.type](arr.map[BigInt](n => CBigInt(n)), SBigInt))
case arr: Array[String] if !VersionContext.current.isV6SoftForkActivated => Nullable(mkCollectionConstant[SString.type](arr, SString))
case arr: Array[Boolean] if !VersionContext.current.isV3OrLaterErgoTreeVersion => Nullable(mkCollectionConstant[SBoolean.type](arr, SBoolean))
case arr: Array[Byte] if !VersionContext.current.isV3OrLaterErgoTreeVersion => Nullable(mkCollectionConstant[SByte.type](arr, SByte))
case arr: Array[Short] if !VersionContext.current.isV3OrLaterErgoTreeVersion => Nullable(mkCollectionConstant[SShort.type](arr, SShort))
case arr: Array[Int] if !VersionContext.current.isV3OrLaterErgoTreeVersion => Nullable(mkCollectionConstant[SInt.type](arr, SInt))
case arr: Array[Long] if !VersionContext.current.isV3OrLaterErgoTreeVersion => Nullable(mkCollectionConstant[SLong.type](arr, SLong))
case arr: Array[BigInteger] if !VersionContext.current.isV3OrLaterErgoTreeVersion => Nullable(mkCollectionConstant[SBigInt.type](arr.map[BigInt](n => CBigInt(n)), SBigInt))
case arr: Array[String] if !VersionContext.current.isV3OrLaterErgoTreeVersion => Nullable(mkCollectionConstant[SString.type](arr, SString))
case v: AnyValue =>
val tpe = Evaluation.rtypeToSType(v.tVal)
Nullable(mkConstant[tpe.type](v.value.asInstanceOf[tpe.WrappedType], tpe))
case v: Int => Nullable(mkConstant[SInt.type](v, SInt))
case v: Long => Nullable(mkConstant[SLong.type](v, SLong))
case v: BigInteger if !VersionContext.current.isV6SoftForkActivated => Nullable(mkConstant[SBigInt.type](CBigInt(v), SBigInt))
case v: BigInteger if !VersionContext.current.isV3OrLaterErgoTreeVersion => Nullable(mkConstant[SBigInt.type](CBigInt(v), SBigInt))
case n: sigma.BigInt => Nullable(mkConstant[SBigInt.type](n, SBigInt))
case n: sigma.UnsignedBigInt => Nullable(mkConstant[SUnsignedBigInt.type](n, SUnsignedBigInt))
case ge: GroupElement => Nullable(mkConstant[SGroupElement.type](ge, SGroupElement))
case b: Boolean => Nullable(if (b) TrueLeaf else FalseLeaf)
case v: String if !VersionContext.current.isV6SoftForkActivated => Nullable(mkConstant[SString.type](v, SString))
case h: Header if VersionContext.current.isV6SoftForkActivated => Nullable(mkConstant[SHeader.type](h, SHeader))
case h: PreHeader if VersionContext.current.isV6SoftForkActivated => Nullable(mkConstant[SPreHeader.type](h, SPreHeader))
case v: String if !VersionContext.current.isV3OrLaterErgoTreeVersion => Nullable(mkConstant[SString.type](v, SString))
case h: Header if VersionContext.current.isV3OrLaterErgoTreeVersion => Nullable(mkConstant[SHeader.type](h, SHeader))
case h: PreHeader if VersionContext.current.isV3OrLaterErgoTreeVersion => Nullable(mkConstant[SPreHeader.type](h, SPreHeader))

// The Box lifting was broken in v4.x. `SigmaDsl.Box(b)` was missing which means the
// isCorrectType requirement would fail in ConstantNode constructor.
// This method is used as part of consensus in SubstConstants operation, however
// ErgoBox cannot be passed as argument as it is never valid value during evaluation.
// Thus we can use activation-based versioning and fix this code when v5.0 is activated.
case b: ErgoBox if !VersionContext.current.isV6SoftForkActivated =>
case b: ErgoBox if !VersionContext.current.isV3OrLaterErgoTreeVersion =>
Nullable(mkConstant[SBox.type](CBox(b), SBox)) // fixed in v5.0

// this case is added in v5.0 and it can be useful when the box value comes from a
Expand All @@ -50,7 +50,7 @@ object Platform {
Nullable(mkConstant[SBox.type](b, SBox))
else
Nullable.None // return the same result as in v4.x when there was no this case
case avl: AvlTreeData if !VersionContext.current.isV6SoftForkActivated => Nullable(mkConstant[SAvlTree.type](CAvlTree(avl), SAvlTree))
case avl: AvlTreeData if !VersionContext.current.isV3OrLaterErgoTreeVersion => Nullable(mkConstant[SAvlTree.type](CAvlTree(avl), SAvlTree))
case avl: AvlTree => Nullable(mkConstant[SAvlTree.type](avl, SAvlTree))
case sb: SigmaBoolean => Nullable(mkConstant[SSigmaProp.type](CSigmaProp(sb), SSigmaProp))
case p: SigmaProp => Nullable(mkConstant[SSigmaProp.type](p, SSigmaProp))
Expand Down
Loading

0 comments on commit 59225d3

Please sign in to comment.