Skip to content

Commit

Permalink
refactoring: removed ScriptCostLimit constant
Browse files Browse the repository at this point in the history
  • Loading branch information
aslesarenko committed Aug 9, 2023
1 parent 8988532 commit cf8e3a3
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ object SigmaConstants {
"Max children count should not be greater than provided value") {
}

object ScriptCostLimit extends SizeConstant[Int](1000000, 12,
"Maximum execution cost of a script") {
}

object MaxLoopLevelInCostFunction extends SizeConstant[Int](1, 13,
"Maximum allowed loop level in a cost function") {
}
Expand All @@ -96,7 +92,6 @@ object SigmaConstants {
MaxTupleLength,
MaxHeaders,
MaxChildrenCountForAtLeastOp,
ScriptCostLimit,
MaxLoopLevelInCostFunction,
VotesArraySize,
AutolykosPowSolutionNonceArraySize
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sigmastate.interpreter

import org.ergoplatform.ErgoLikeContext
import org.ergoplatform.SigmaConstants.ScriptCostLimit
import sigmastate.{PerItemCost, VersionContext, TypeBasedCost, FixedCost, SType, JitCost}
import sigmastate.Values._
import sigmastate.eval.Profiler
Expand Down Expand Up @@ -42,7 +41,12 @@ case class EvalSettings(
* The default value is None, which means the version is defined by ErgoTree.version
* and Context.activatedScriptVersion.
*/
evaluationMode: Option[EvaluationMode] = None)
evaluationMode: Option[EvaluationMode] = None,
/** Maximum execution cost of a script used by profiler.
* @see ErgoTreeEvaluator
*/
scriptCostLimitInEvaluator: Int = 1000000,
)

object EvalSettings {
/** Enumeration type of evaluation modes of [[Interpreter]].
Expand Down Expand Up @@ -123,7 +127,7 @@ class ErgoTreeEvaluator(
protected val coster: CostAccumulator,
val profiler: Profiler,
val settings: EvalSettings) {

/** Evaluates the given expression in the given data environment. */
def eval(env: DataEnv, exp: SValue): Any = {
VersionContext.checkVersions(context.activatedScriptVersion, context.currentErgoTreeVersion)
Expand Down Expand Up @@ -392,7 +396,7 @@ object ErgoTreeEvaluator {
def forProfiling(profiler: Profiler, evalSettings: EvalSettings): ErgoTreeEvaluator = {
val acc = new CostAccumulator(
initialCost = JitCost(0),
costLimit = Some(JitCost.fromBlockCost(ScriptCostLimit.value)))
costLimit = Some(JitCost.fromBlockCost(evalSettings.scriptCostLimitInEvaluator)))
new ErgoTreeEvaluator(
context = null,
constants = ArraySeq.empty,
Expand Down
3 changes: 3 additions & 0 deletions interpreter/shared/src/test/scala/sigmastate/TestsBase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ trait TestsBase extends Matchers with VersionTesting {
/** Transform sigma proposition into [[ErgoTree]] using current ergoTreeHeaderInTests. */
def mkTestErgoTree(prop: SigmaBoolean): ErgoTree =
ErgoTree.fromSigmaBoolean(ergoTreeHeaderInTests, prop)

/** Max cost of script execution in tests. */
val scriptCostLimitInTests: Int = 1000000
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package sigmastate.helpers

import org.ergoplatform.SigmaConstants.ScriptCostLimit
import org.ergoplatform.ErgoLikeContext.Height
import org.ergoplatform._
import org.ergoplatform.validation.{ValidationRules, SigmaValidationSettings}
import org.ergoplatform.validation.{SigmaValidationSettings, ValidationRules}
import sigmastate.AvlTreeData
import sigmastate.basics.CryptoConstants
import sigmastate.eval._
import sigmastate.interpreter.ContextExtension
import sigmastate.serialization.{SigmaSerializer, GroupElementSerializer}
import sigmastate.interpreter.ErgoTreeEvaluator.DefaultEvalSettings
import sigmastate.serialization.{GroupElementSerializer, SigmaSerializer}
import special.collection.Coll
import special.sigma.{Box, PreHeader, Header}
import special.sigma.{Box, Header, PreHeader}

object ErgoLikeContextTesting {
/* NO HF PROOF:
Expand Down Expand Up @@ -46,7 +46,8 @@ object ErgoLikeContextTesting {
new ErgoLikeContext(
lastBlockUtxoRoot, noHeaders, dummyPreHeader(currentHeight, minerPubkey), noBoxes,
boxesToSpend, spendingTransaction, boxesToSpend.indexOf(self), extension, vs,
ScriptCostLimit.value, initCost = 0L, activatedVersion)
DefaultEvalSettings.scriptCostLimitInEvaluator,
initCost = 0L, activatedVersion)

def apply(currentHeight: Height,
lastBlockUtxoRoot: AvlTreeData,
Expand All @@ -59,7 +60,7 @@ object ErgoLikeContextTesting {
new ErgoLikeContext(
lastBlockUtxoRoot, noHeaders, dummyPreHeader(currentHeight, minerPubkey),
dataBoxes, boxesToSpend, spendingTransaction, selfIndex, ContextExtension.empty,
ValidationRules.currentSettings, ScriptCostLimit.value,
ValidationRules.currentSettings, DefaultEvalSettings.scriptCostLimitInEvaluator,
initCost = 0L, activatedVersion)


Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package sigmastate.serialization

import java.math.BigInteger

import org.ergoplatform._
import org.scalacheck.Arbitrary._
import scalan.RType
import sigmastate.SCollection.SByteArray
import sigmastate.Values.{LongConstant, FalseLeaf, Constant, SValue, TrueLeaf, BigIntConstant, GroupGenerator, ByteArrayConstant}
import sigmastate.Values.{BigIntConstant, ByteArrayConstant, Constant, FalseLeaf, GroupGenerator, LongConstant, SValue, TrueLeaf}
import sigmastate.basics.CryptoConstants.EcPointType
import sigmastate._
import sigmastate.eval._
Expand All @@ -19,6 +18,8 @@ import scorex.util.encode.Base16
import sigmastate.exceptions.SerializerException
import sigmastate.lang.DeserializationSigmaBuilder

import scala.annotation.nowarn

class ConstantSerializerSpecification extends TableSerializationSpecification {

private def testCollection[T <: SType](tpe: T) = {
Expand Down Expand Up @@ -47,8 +48,8 @@ class ConstantSerializerSpecification extends TableSerializationSpecification {
def testTuples[T <: SType](tpe: T) = {
implicit val wWrapped = wrappedTypeGen(tpe)
implicit val tT = Evaluation.stypeToRType(tpe)
implicit val tag = tT.classTag
implicit val tAny = RType.AnyType
@nowarn implicit val tag = tT.classTag
implicit val tAny: RType[Any] = RType.AnyType
forAll { in: (T#WrappedType, T#WrappedType) =>
val (x,y) = (in._1, in._2)
roundTripTest(Constant[SType]((x, y).asWrappedType, STuple(tpe, tpe)))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package sigmastate.serialization

import java.math.BigInteger

import org.ergoplatform.ErgoBox
import org.scalacheck.Arbitrary._
import scalan.RType
import sigmastate.SCollection.SByteArray
import sigmastate.Values.{SigmaBoolean, ErgoTree}
import sigmastate.Values.{ErgoTree, SigmaBoolean}
import sigmastate._
import sigmastate.eval.Evaluation
import sigmastate.eval._
import sigmastate.eval.Extensions._
import sigmastate.basics.CryptoConstants.EcPointType
import special.sigma.AvlTree
import SType.AnyOps
import org.ergoplatform.SigmaConstants.ScriptCostLimit
import sigmastate.exceptions.SerializerException
import sigmastate.interpreter.{CostAccumulator, ErgoTreeEvaluator}
import sigmastate.interpreter.ErgoTreeEvaluator.DefaultProfiler
import sigmastate.utils.Helpers

import scala.annotation.nowarn

class DataSerializerSpecification extends SerializationSpecification {

def roundtrip[T <: SType](obj: T#WrappedType, tpe: T) = {
Expand All @@ -30,13 +30,14 @@ class DataSerializerSpecification extends SerializationSpecification {
val res = DataSerializer.deserialize(tpe, r)
res shouldBe obj

val es = ErgoTreeEvaluator.DefaultEvalSettings
val accumulator = new CostAccumulator(
initialCost = JitCost(0),
costLimit = Some(JitCost.fromBlockCost(ScriptCostLimit.value)))
costLimit = Some(JitCost.fromBlockCost(es.scriptCostLimitInEvaluator)))
val evaluator = new ErgoTreeEvaluator(
context = null,
constants = ErgoTree.EmptyConstants,
coster = accumulator, DefaultProfiler, ErgoTreeEvaluator.DefaultEvalSettings)
coster = accumulator, DefaultProfiler, es)
val ok = DataValueComparer.equalDataValues(res, obj)(evaluator)
ok shouldBe true

Expand Down Expand Up @@ -76,8 +77,8 @@ class DataSerializerSpecification extends SerializationSpecification {

def testTuples[T <: SType](tpe: T) = {
implicit val wWrapped = wrappedTypeGen(tpe)
implicit val tag = tpe.classTag[T#WrappedType]
implicit val tAny = RType.AnyType
@nowarn implicit val tag = tpe.classTag[T#WrappedType]
implicit val tAny: RType[Any] = RType.AnyType
forAll { in: (T#WrappedType, T#WrappedType) =>
val (x,y) = (in._1, in._2)
roundtrip[SType]((x, y).asWrappedType, STuple(tpe, tpe))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ class SigSerializerSpecification extends TestingCommons
)
)

cases.zipWithIndex.foreach { case (c, iCase) =>
cases.zipWithIndex.foreach { case (c, _) =>
val sigBytes = SigSerializer.toProofBytes(c.uncheckedTree)
sigBytes shouldBe c.proof
val uncheckedTree = SigSerializer.parseAndComputeChallenges(c.prop, c.proof)(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import sigmastate.{AvlTreeData, Values}
import sigmastate.eval._
import sigmastate.eval.Extensions._
import sigmastate.helpers.TestingHelpers._
import scalan._ // imports implicit ClassTag
import scalan._

import scala.annotation.nowarn // imports implicit ClassTag

trait ContractsTestkit {
val R0 = 0.toByte;
Expand Down Expand Up @@ -59,7 +61,7 @@ trait ContractsTestkit {

val AliceId = Array[Byte](1) // 0x0001

def newAliceBox(id: Byte, value: Long): Box = {
def newAliceBox(@nowarn id: Byte, value: Long): Box = {
val ergoBox = testBox(value,
ErgoTree.fromProposition(Values.TrueSigmaProp),
creationHeight = 0, additionalTokens = Seq(), additionalRegisters = Map())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.scalacheck.Gen.containerOfN
import org.scalacheck.util.Buildable
import org.scalacheck.{Arbitrary, Gen}
import scalan.RType
import scorex.crypto.authds.{ADDigest, ADKey, ADValue}
import scorex.crypto.authds.{ADKey, ADValue}
import scorex.crypto.hash.{Blake2b256, Digest32}
import scorex.util.ModifierId
import sigmastate.Values.{ByteArrayConstant, ConcreteCollection, ConstantPlaceholder, ErgoTree, FalseLeaf, IntConstant, LongConstant, SigmaPropConstant, TrueLeaf}
Expand Down Expand Up @@ -43,11 +43,11 @@ trait SigmaTestingData extends TestingCommons with ObjectGenerators {
len <- Gen.choose(0, 100)
arr <- containerOfN[Array, Byte](len, Arbitrary.arbByte.arbitrary)
} yield arr
val bytesCollGen = bytesGen.map(Colls.fromArray(_))
val intsCollGen = arrayGen[Int].map(Colls.fromArray(_))
implicit val arbBytes = Arbitrary(bytesCollGen)
implicit val arbInts = Arbitrary(intsCollGen)
val keyCollGen = collOfN[Byte](32, arbitrary[Byte])
val bytesCollGen: Gen[Coll[Byte]] = bytesGen.map(Colls.fromArray(_))
val intsCollGen: Gen[Coll[Int]] = arrayGen[Int].map(Colls.fromArray(_))
implicit val arbBytes: Arbitrary[Coll[Byte]] = Arbitrary(bytesCollGen)
implicit val arbInts: Arbitrary[Coll[Int]] = Arbitrary(intsCollGen)
val keyCollGen: Gen[Coll[Byte]] = collOfN[Byte](32, arbitrary[Byte])
import org.ergoplatform.dsl.AvlTreeHelpers._

def createAvlTreeAndProver(entries: (Coll[Byte], Coll[Byte])*) = {
Expand Down
1 change: 0 additions & 1 deletion parsers/shared/src/main/scala/sigmastate/lang/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package sigmastate.lang
import fastparse._
import ScalaWhitespace._
import sigmastate._
import sigmastate.SCollection.SByteArray
import Values._
import sigmastate.lang.Terms.Ident
import sigmastate.lang.syntax.Core
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.ergoplatform

import org.ergoplatform.ErgoAddressEncoder.{MainnetNetworkPrefix, TestnetNetworkPrefix, hash256}
import org.ergoplatform.SigmaConstants.ScriptCostLimit
import org.ergoplatform.validation.{ValidationException, ValidationRules}
import org.scalatest.{Assertion, TryValues}
import scorex.crypto.hash.Blake2b256
Expand Down Expand Up @@ -260,7 +259,7 @@ class ErgoAddressSpecification extends SigmaDslTesting
property("negative cases: deserialized script + costing exceptions") {
implicit lazy val IR = new TestingIRContext

def testPay2SHAddress(address: Pay2SHAddress, script: VarBinding, costLimit: Int = ScriptCostLimit.value): CostedProverResult = {
def testPay2SHAddress(address: Pay2SHAddress, script: VarBinding, costLimit: Int = scriptCostLimitInTests): CostedProverResult = {
val boxToSpend = testBox(10, address.script, creationHeight = 5)
val ctx = copyContext(ErgoLikeContextTesting.dummy(boxToSpend, activatedVersionInTests)
.withExtension(ContextExtension(Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class ErgoTreePredefSpec extends CompilerTestingCommons with CompilerCrossVersio
boxesToSpend = inputBoxes,
spendingTransaction,
self = inputBoxes.head,
activatedVersionInTests).withCostLimit(SigmaConstants.ScriptCostLimit.value * 10)
activatedVersionInTests).withCostLimit(scriptCostLimitInTests * 10)

val pr = prover.prove(emptyEnv + (ScriptNameProp -> "tokenThresholdScript_prove"), prop, ctx, fakeMessage).getOrThrow
verifier.verify(emptyEnv + (ScriptNameProp -> "tokenThresholdScript_verify"), prop, ctx, pr, fakeMessage).getOrThrow._1 shouldBe true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package sigmastate.helpers

import org.ergoplatform.SigmaConstants.ScriptCostLimit
import org.ergoplatform._
import org.ergoplatform.validation.ValidationRules.CheckSerializableTypeCode
import org.ergoplatform.validation.{ValidationException, ValidationSpecification}
Expand Down Expand Up @@ -137,7 +136,7 @@ trait CompilerTestingCommons extends TestingCommons
val sigmaCtx = createContexts(in, bindings)
val accumulator = new CostAccumulator(
initialCost = JitCost(0),
costLimit = Some(JitCost.fromBlockCost(ScriptCostLimit.value)))
costLimit = Some(JitCost.fromBlockCost(evalSettings.scriptCostLimitInEvaluator)))
val evaluator = new ErgoTreeEvaluator(
context = sigmaCtx,
constants = ErgoTree.EmptyConstants,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package special.sigma

import org.ergoplatform.SigmaConstants.ScriptCostLimit
import org.scalatest.BeforeAndAfterAll
import scalan.RType
import scalan.util.BenchmarkUtil
Expand All @@ -23,7 +22,8 @@ class DataValueComparerSpecification extends SigmaDslTesting
isMeasureOperationTime = true,
isMeasureScriptTime = true,
isLogEnabled = false, // don't commit the `true` value (CI log is too high)
costTracingEnabled = true // should always be enabled in tests (and false by default)
costTracingEnabled = true, // should always be enabled in tests (and false by default)
scriptCostLimitInEvaluator = scriptCostLimitInTests
)
override val nBenchmarkIters = 10

Expand All @@ -36,7 +36,7 @@ class DataValueComparerSpecification extends SigmaDslTesting
def createEvaluator(settings: EvalSettings, profiler: Profiler): ErgoTreeEvaluator = {
val accumulator = new CostAccumulator(
initialCost = JitCost(0),
costLimit = Some(JitCost.fromBlockCost(ScriptCostLimit.value)))
costLimit = Some(JitCost.fromBlockCost(settings.scriptCostLimitInEvaluator)))
val evaluator = new ErgoTreeEvaluator(
context = null,
constants = ErgoTree.EmptyConstants,
Expand Down
3 changes: 1 addition & 2 deletions sc/shared/src/test/scala/special/sigma/SigmaDslTesting.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package special.sigma

import debox.cfor
import org.ergoplatform.SigmaConstants.ScriptCostLimit
import org.ergoplatform._
import org.ergoplatform.dsl.{ContractSpec, SigmaContractSyntax, TestContractSpec}
import org.ergoplatform.validation.ValidationRules.CheckSerializableTypeCode
Expand Down Expand Up @@ -366,7 +365,7 @@ class SigmaDslTesting extends AnyPropSpec
createErgoLikeContext(
newCtx,
ValidationRules.currentSettings,
ScriptCostLimit.value,
evalSettings.scriptCostLimitInEvaluator,
initCost = initialCostInTests.value
)

Expand Down

0 comments on commit cf8e3a3

Please sign in to comment.