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

Preparation refactoring towards v5.0 (part 5) #734

Merged
merged 13 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions common/src/main/scala/scalan/AnyVals.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,14 @@ class AVHashMap[K,V](val hashMap: HashMap[K,V]) extends AnyVal {
object AVHashMap {
/** Helper method to create a new map with the given capacity. */
def apply[K,V](initialCapacity: Int) = new AVHashMap[K,V](new HashMap[K,V](initialCapacity))

/** Helper method to create a new map from the sequence of K, V pairs. */
def fromSeq[K,V](items: Seq[(K, V)]): AVHashMap[K,V] = {
val map = new AVHashMap[K,V](new HashMap[K,V](items.length))
items.foreach { case (k, v) =>
map.put(k, v)
}
map
}
}

55 changes: 54 additions & 1 deletion sigmastate/src/main/scala/org/ergoplatform/ErgoLikeContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import sigmastate.Values._
import sigmastate._
import sigmastate.eval.Extensions._
import sigmastate.eval._
import sigmastate.interpreter.{ContextExtension, InterpreterContext}
import sigmastate.interpreter.ErgoTreeEvaluator.DataEnv
import sigmastate.interpreter.{ContextExtension, InterpreterContext, ErgoTreeEvaluator}
import sigmastate.serialization.OpCodes
import sigmastate.serialization.OpCodes.OpCode
import special.collection.Coll
Expand Down Expand Up @@ -228,46 +229,84 @@ object ErgoLikeContext {
/** When interpreted evaluates to a ByteArrayConstant built from Context.minerPubkey */
case object MinerPubkey extends NotReadyValueByteArray with ValueCompanion {
override def opCode: OpCode = OpCodes.MinerPubkeyCode
/** Cost of calling Context.minerPubkey Scala method. */
override val costKind = FixedCost(20)
override val opType = SFunc(SContext, SCollection.SByteArray)
override def companion = this
protected final override def eval(env: DataEnv)(implicit E: ErgoTreeEvaluator): Any = {
addCost(this.costKind)
E.context.minerPubKey
}
}

/** When interpreted evaluates to a IntConstant built from Context.currentHeight */
case object Height extends NotReadyValueInt with ValueCompanion {
override def companion = this
override def opCode: OpCode = OpCodes.HeightCode
/** Cost of: 1) Calling Context.HEIGHT Scala method. */
override val costKind = FixedCost(26)
aslesarenko marked this conversation as resolved.
Show resolved Hide resolved
override val opType = SFunc(SContext, SInt)
protected final override def eval(env: DataEnv)(implicit E: ErgoTreeEvaluator): Any = {
addCost(this.costKind)
aslesarenko marked this conversation as resolved.
Show resolved Hide resolved
E.context.HEIGHT
}
}

/** When interpreted evaluates to a collection of BoxConstant built from Context.boxesToSpend */
case object Inputs extends LazyCollection[SBox.type] with ValueCompanion {
override def companion = this
override def opCode: OpCode = OpCodes.InputsCode
/** Cost of: 1) Calling Context.INPUTS Scala method. */
override val costKind = FixedCost(10)
override def tpe = SCollection.SBoxArray
override val opType = SFunc(SContext, tpe)
protected final override def eval(env: DataEnv)(implicit E: ErgoTreeEvaluator): Any = {
addCost(this.costKind)
E.context.INPUTS
}
}

/** When interpreted evaluates to a collection of BoxConstant built from Context.spendingTransaction.outputs */
case object Outputs extends LazyCollection[SBox.type] with ValueCompanion {
override def companion = this
override def opCode: OpCode = OpCodes.OutputsCode
/** Cost of: 1) Calling Context.OUTPUTS Scala method. */
override val costKind = FixedCost(10)
override def tpe = SCollection.SBoxArray
override val opType = SFunc(SContext, tpe)
protected final override def eval(env: DataEnv)(implicit E: ErgoTreeEvaluator): Any = {
addCost(this.costKind)
E.context.OUTPUTS
}
}

/** When interpreted evaluates to a AvlTreeConstant built from Context.lastBlockUtxoRoot */
case object LastBlockUtxoRootHash extends NotReadyValueAvlTree with ValueCompanion {
override def companion = this
override def opCode: OpCode = OpCodes.LastBlockUtxoRootHashCode

/** Cost of: 1) Calling Context.LastBlockUtxoRootHash Scala method. */
override val costKind = FixedCost(15)

override val opType = SFunc(SContext, tpe)
protected final override def eval(env: DataEnv)(implicit E: ErgoTreeEvaluator): Any = {
addCost(this.costKind)
E.context.LastBlockUtxoRootHash
}
}


/** When interpreted evaluates to a BoxConstant built from context.boxesToSpend(context.selfIndex) */
case object Self extends NotReadyValueBox with ValueCompanion {
override def companion = this
override def opCode: OpCode = OpCodes.SelfCode
/** Cost of: 1) Calling Context.SELF Scala method. */
override val costKind = FixedCost(10)
override val opType = SFunc(SContext, SBox)
protected final override def eval(env: DataEnv)(implicit E: ErgoTreeEvaluator): Any = {
addCost(this.costKind)
E.context.SELF
}
}

/** When interpreted evaluates to the singleton instance of [[special.sigma.Context]].
Expand All @@ -276,8 +315,16 @@ case object Self extends NotReadyValueBox with ValueCompanion {
case object Context extends NotReadyValue[SContext.type] with ValueCompanion {
override def companion = this
override def opCode: OpCode = OpCodes.ContextCode

/** Cost of: 1) accessing global Context instance. */
override val costKind = FixedCost(1)

override def tpe: SContext.type = SContext
override val opType: SFunc = SFunc(SUnit, SContext)
protected final override def eval(env: DataEnv)(implicit E: ErgoTreeEvaluator): Any = {
addCost(this.costKind)
E.context
}
}

/** When interpreted evaluates to the singleton instance of [[special.sigma.SigmaDslBuilder]].
Expand All @@ -286,6 +333,12 @@ case object Context extends NotReadyValue[SContext.type] with ValueCompanion {
case object Global extends NotReadyValue[SGlobal.type] with ValueCompanion {
override def companion = this
override def opCode: OpCode = OpCodes.GlobalCode
/** Cost of: 1) accessing Global instance. */
override val costKind = FixedCost(5)
override def tpe: SGlobal.type = SGlobal
override val opType: SFunc = SFunc(SUnit, SGlobal)
protected final override def eval(env: DataEnv)(implicit E: ErgoTreeEvaluator): Any = {
addCost(this.costKind)
CostingSigmaDslBuilder
}
}
Loading