Skip to content

Commit

Permalink
Merge branch 'v6.0.0' of github.com:ScorexFoundation/sigmastate-inter…
Browse files Browse the repository at this point in the history
…preter into i675-2
  • Loading branch information
kushti committed May 26, 2024
2 parents 1016323 + f0c34e6 commit c8d75cd
Show file tree
Hide file tree
Showing 86 changed files with 583 additions and 714 deletions.
4 changes: 2 additions & 2 deletions core/js/src/main/scala/sigma/js/GroupElement.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package sigma.js

import sigma.Extensions.CoreArrayByteOps
import scorex.util.encode.Base16
import sigma.crypto.{CryptoFacade, CryptoFacadeJs, Ecp, Platform}

import scala.scalajs.js
Expand All @@ -13,7 +13,7 @@ class GroupElement(val point: Ecp) extends js.Object {
* @see CryptoFacade.getASN1Encoding
*/
def toPointHex(): String = {
CryptoFacade.getASN1Encoding(point, true).toHex
Base16.encode(CryptoFacade.getASN1Encoding(point, true))
}
}

Expand Down
3 changes: 3 additions & 0 deletions core/js/src/main/scala/sigma/js/Isos.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import scala.reflect.ClassTag
import scala.scalajs.js
import scala.scalajs.js.JSConverters.JSRichOption

/** Definitions of isomorphisms for sigma-core module.
* @see sigma.data.Iso
*/
object Isos {

implicit def isoUndefOr[A, B](implicit iso: Iso[A, B]): Iso[js.UndefOr[A], Option[B]] = new Iso[js.UndefOr[A], Option[B]] {
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/sigma/Environment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ sealed abstract class Environment {

object Environment {
/** Current runtime environment. */
implicit val current: Environment = new Environment {
val current: Environment = new Environment {
override def isJVM: Boolean = runtimePlatform == RuntimePlatform.JVM
override def isJS: Boolean = runtimePlatform == RuntimePlatform.JS
override def runtimePlatform: RuntimePlatform = sigma.reflection.Platform.runtimePlatform
Expand Down
3 changes: 1 addition & 2 deletions core/shared/src/main/scala/sigma/Evaluation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ object Evaluation {
case HeaderRType => SHeader
case PreHeaderRType => SPreHeader
case SigmaPropRType => SSigmaProp
// TODO remove commented code below after full sync test
// case SigmaBooleanRType => SSigmaProp // this is not used in consensus code
case SigmaBooleanRType => SSigmaProp // TODO remove: this is not used in consensus code
case tup: TupleType => STuple(tup.items.map(t => rtypeToSType(t)))
case at: ArrayType[_] => SCollection(rtypeToSType(at.tA))
case ct: CollType[_] => SCollection(rtypeToSType(ct.tItem))
Expand Down
11 changes: 6 additions & 5 deletions core/shared/src/main/scala/sigma/Extensions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import scorex.util.encode.Base16
import scorex.util.{ModifierId, bytesToId}
import sigma.data.RType

/** Declaration of extension methods introduced in `sigma-core` module.
* See `implicit class ...` wrappers below.
*/
object Extensions {
/** Extension methods for `Array[Byte]` not available for generic `Array[T]`. */
implicit class CoreArrayByteOps(val arr: Array[Byte]) extends AnyVal {
/** Encodes array into hex string */
@inline def toHex: String = Base16.encode(arr)
}

/** Extension methods for `Array[T]` where implicit descriptor `RType[T]` is also
* required.
*/
implicit class ArrayOps[T: RType](arr: Array[T]) {
/** Wraps array into Coll instance. The source array in not cloned. */
@inline def toColl: Coll[T] = Colls.fromArray(arr)
Expand Down
6 changes: 3 additions & 3 deletions core/shared/src/main/scala/sigma/VersionContext.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package sigma

import VersionContext.{EvolutionVersion, JitActivationVersion}
import VersionContext.{JitActivationVersion, V6SoftForkVersion}

import scala.util.DynamicVariable

Expand All @@ -24,7 +24,7 @@ case class VersionContext(activatedVersion: Byte, ergoTreeVersion: Byte) {

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

object VersionContext {
Expand All @@ -48,7 +48,7 @@ object VersionContext {
/**
* The version of ErgoTree corresponding to "evolution" (6.0) soft-fork
*/
val EvolutionVersion: Byte = 3
val V6SoftForkVersion: Byte = 3

private val _defaultContext = VersionContext(
activatedVersion = 1 /* v4.x */,
Expand Down
3 changes: 1 addition & 2 deletions core/shared/src/main/scala/sigma/ast/SType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ object SType {
val paramOV = STypeParam(tOV)
val paramIVSeq: Seq[STypeParam] = Array(paramIV)

val IndexedSeqOfT1: IndexedSeq[SType] = Array(SType.tT)
val IndexedSeqOfT2: IndexedSeq[SType] = Array(SType.tT, SType.tT)

/** Immutable empty array, can be used to avoid repeated allocations. */
Expand Down Expand Up @@ -146,7 +145,7 @@ object SType {
* 2) `isValueOfType == true` for each tree leaf
* 3) `isValueOfType == true` for each sub-expression
*
* @param value value to check type
* @param x value to check type
* @param tpe type descriptor to check value against
* @return true if the given `value` is of type tpe`
*/
Expand Down
10 changes: 8 additions & 2 deletions core/shared/src/main/scala/sigma/data/Iso.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ import sigma.{Coll, Colls}
/** Type-class of isomorphisms between types.
* Isomorphism between two types `A` and `B` essentially say that both types
* represents the same information (entity) but in a different way.
*
* Each isomorphism defined by two functions:
* - `to: A => B` - conversion from `A` to `B`
* - `from: B => A` - conversion from `B` to `A`
*
* <p>
* The information is not lost so that both are true:
* such that the information is not lost during conversion, so that both are true:
* 1) a == from(to(a))
* 2) b == to(from(b))
* <p>
* It is used to define type-full conversions:
* - different conversions between Java and Scala data types.
* - conversion between Ergo representations and generated API representations
* - conversion between internal Ergo representations and API representations
* - conversions between exported JS classes and internal Ergo representations
*/
abstract class Iso[A, B] {
def to(a: A): B
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/sigma/data/SigmaPropCodes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sigma.data
import sigma.ast.TypeCodes.LastConstantCode
import supertagged.TaggedType

/** Encoding of sigma proposition nodes.
/** Opcodes of sigma proposition nodes.
*
* @see SigmaBoolean.opCode
*/
Expand Down
1 change: 1 addition & 0 deletions core/shared/src/main/scala/sigma/data/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package object data {
val BigIntClassTag = classTag[BigInt]
val GroupElementClassTag = classTag[GroupElement]
val SigmaPropClassTag = classTag[SigmaProp]
val SigmaBooleanClassTag = classTag[SigmaBoolean]
val AvlTreeClassTag = classTag[AvlTree]
val BoxClassTag = classTag[Box]
val ContextClassTag = classTag[Context]
Expand Down
7 changes: 5 additions & 2 deletions core/shared/src/main/scala/sigma/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ package object sigma {

implicit val StringType : RType[String] = GeneralType(StringClassTag)

implicit val BigIntRType: RType[BigInt] = GeneralType(BigIntClassTag)
implicit val BigIntRType : RType[BigInt] = GeneralType(BigIntClassTag)
implicit val GroupElementRType: RType[GroupElement] = GeneralType(GroupElementClassTag)
implicit val SigmaPropRType: RType[SigmaProp] = GeneralType(SigmaPropClassTag)
implicit val SigmaPropRType : RType[SigmaProp] = GeneralType(SigmaPropClassTag)
implicit val SigmaBooleanRType: RType[SigmaBoolean] = GeneralType(SigmaBooleanClassTag)


implicit val AvlTreeRType: RType[AvlTree] = GeneralType(AvlTreeClassTag)

implicit val BoxRType: RType[Box] = GeneralType(BoxClassTag)
Expand Down
10 changes: 8 additions & 2 deletions core/shared/src/main/scala/sigma/reflection/ReflectionData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ import scala.collection.mutable
import scala.collection.immutable

/** Reflection metadata and global dictionaries to access it.
* For each class of this module that needs reflection metadata,
* we register a class entry with the necessary information.
* Such metadata is only used on JS platform to support reflection-like interfaces of
* RClass, RMethod, RConstructor. These interfaces implemented on JVM using Java
* reflection.
*
* For each class that needs reflection metadata, we register a class entry using
* `registerClassEntry` method with the necessary information such as constructors and
* methods.
* Only information that is needed at runtime is registered.
* @see mkConstructor, mkMethod
*/
object ReflectionData {
/** Descriptors of classes. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import sigma.validation.ValidationRules.CheckPositionLimit
* methods.
*
* @param r the underlying reader this reader reads from
* @param constantStore the store of constants which is used to resolve
* [[sigma.ast.ConstantPlaceholder]]
* @param resolvePlaceholdersToConstants if true then resolved constants will be
* substituted in the tree instead of the placeholder.
* @param maxTreeDepth limit on the tree depth (recursive invocations)
* of the deserializer
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import scorex.util.serialization.{VLQByteBufferWriter, Writer}
import sigma.ast.SType
import sigma.serialization.CoreByteWriter.{Bits, DataInfo, U, Vlq, ZigZag}

/** Implementation of [[Writer]] provided by `sigma-core` module.
* @param w destination [[Writer]] to which all the call got delegated.
*/
class CoreByteWriter(val w: Writer) extends Writer {
type CH = w.CH

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,31 @@ import sigma.data.SigmaConstants

import java.nio.ByteBuffer

/** Implementation of [[Serializer]] provided by `sigma-core` module. */
abstract class CoreSerializer[TFamily, T <: TFamily] extends Serializer[TFamily, T, CoreByteReader, CoreByteWriter] {

def error(msg: String) = throw new SerializerException(msg, None)
def error(msg: String) = throw SerializerException(msg, None)

/** Serializes the given 'obj' to a new array of bytes using this serializer. */
final def toBytes(obj: T): Array[Byte] = {
val w = CoreSerializer.startWriter()
serialize(obj, w)
w.toBytes
}

/** Deserializes `bytes` to an object of this [[TFamily]] using this serializer.
* The actual class of the returned object is expected to be descendant of [[TFamily]].
*/
final def fromBytes(bytes: Array[Byte]): TFamily = {
parse(CoreSerializer.startReader(bytes))
}
}

object CoreSerializer {
type Position = Int

/** Max length of Box.propositionBytes collection */
val MaxPropositionSize: Int = SigmaConstants.MaxPropositionBytes.value

/** Max tree depth should not be greater then provided value */
val MaxTreeDepth: Int = SigmaConstants.MaxTreeDepth.value

/** Helper function to be use in serializers.
Expand Down
4 changes: 1 addition & 3 deletions core/shared/src/test/scala/sigma/VersionTesting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ trait VersionTesting {
_ + 1) { j =>
val treeVersion = ergoTreeVers(j)
// for each tree version up to currently activated, set it up and execute block
_currErgoTreeVersion.withValue(treeVersion) {
VersionContext.withVersions(activatedVersion, treeVersion)(block)
}
_currErgoTreeVersion.withValue(treeVersion)(block)
}
}
}
Expand Down
Loading

0 comments on commit c8d75cd

Please sign in to comment.