From cf8e3a39200fb61f60f64512a650c60d68a636a1 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Wed, 9 Aug 2023 17:09:56 +0200 Subject: [PATCH] refactoring: removed ScriptCostLimit constant --- .../scala/org/ergoplatform/SigmaConstants.scala | 5 ----- .../interpreter/ErgoTreeEvaluator.scala | 12 ++++++++---- .../src/test/scala/sigmastate/TestsBase.scala | 3 +++ .../helpers/ErgoLikeContextTesting.scala | 13 +++++++------ .../ConstantSerializerSpecification.scala | 9 +++++---- .../DataSerializerSpecification.scala | 15 ++++++++------- .../SigSerializerSpecification.scala | 2 +- .../scala/special/sigma/ContractsTestkit.scala | 6 ++++-- .../scala/special/sigma/SigmaTestingData.scala | 12 ++++++------ .../src/main/scala/sigmastate/lang/Types.scala | 1 - .../ergoplatform/ErgoAddressSpecification.scala | 3 +-- .../org/ergoplatform/ErgoTreePredefSpec.scala | 2 +- .../helpers/CompilerTestingCommons.scala | 3 +-- .../sigma/DataValueComparerSpecification.scala | 6 +++--- .../scala/special/sigma/SigmaDslTesting.scala | 3 +-- 15 files changed, 49 insertions(+), 46 deletions(-) diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/SigmaConstants.scala b/interpreter/shared/src/main/scala/org/ergoplatform/SigmaConstants.scala index ff1251ae10..be745d8d7d 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/SigmaConstants.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/SigmaConstants.scala @@ -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") { } @@ -96,7 +92,6 @@ object SigmaConstants { MaxTupleLength, MaxHeaders, MaxChildrenCountForAtLeastOp, - ScriptCostLimit, MaxLoopLevelInCostFunction, VotesArraySize, AutolykosPowSolutionNonceArraySize diff --git a/interpreter/shared/src/main/scala/sigmastate/interpreter/ErgoTreeEvaluator.scala b/interpreter/shared/src/main/scala/sigmastate/interpreter/ErgoTreeEvaluator.scala index bdd59eb850..e118665533 100644 --- a/interpreter/shared/src/main/scala/sigmastate/interpreter/ErgoTreeEvaluator.scala +++ b/interpreter/shared/src/main/scala/sigmastate/interpreter/ErgoTreeEvaluator.scala @@ -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 @@ -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]]. @@ -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) @@ -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, diff --git a/interpreter/shared/src/test/scala/sigmastate/TestsBase.scala b/interpreter/shared/src/test/scala/sigmastate/TestsBase.scala index 2ac254da5b..4e40d91c23 100644 --- a/interpreter/shared/src/test/scala/sigmastate/TestsBase.scala +++ b/interpreter/shared/src/test/scala/sigmastate/TestsBase.scala @@ -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 } diff --git a/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala b/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala index 2945192700..702d890147 100644 --- a/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala +++ b/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala @@ -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: @@ -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, @@ -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) diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/ConstantSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/ConstantSerializerSpecification.scala index 8b261df880..42df295d53 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/ConstantSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/ConstantSerializerSpecification.scala @@ -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._ @@ -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) = { @@ -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))) diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala index 61f6fc85e3..d50f943d7a 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala @@ -1,12 +1,11 @@ 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._ @@ -14,12 +13,13 @@ 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) = { @@ -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 @@ -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)) diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala index 884b8d7150..a66c56f2f2 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala @@ -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) diff --git a/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala b/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala index 4c073d3dc9..a544361d98 100644 --- a/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala +++ b/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala @@ -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; @@ -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()) diff --git a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala index 3838ac87d2..db2f8501c5 100644 --- a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala +++ b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala @@ -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} @@ -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])*) = { diff --git a/parsers/shared/src/main/scala/sigmastate/lang/Types.scala b/parsers/shared/src/main/scala/sigmastate/lang/Types.scala index 8a45d4d66b..e4869e4190 100644 --- a/parsers/shared/src/main/scala/sigmastate/lang/Types.scala +++ b/parsers/shared/src/main/scala/sigmastate/lang/Types.scala @@ -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 diff --git a/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala b/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala index 6b3c219aaa..22359c1606 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala @@ -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 @@ -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( diff --git a/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala b/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala index 36c5e78067..89247a968a 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala @@ -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 diff --git a/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala b/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala index 8981ace5df..b2b497e423 100644 --- a/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala +++ b/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala @@ -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} @@ -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, diff --git a/sc/shared/src/test/scala/special/sigma/DataValueComparerSpecification.scala b/sc/shared/src/test/scala/special/sigma/DataValueComparerSpecification.scala index b264d52144..eaca756182 100644 --- a/sc/shared/src/test/scala/special/sigma/DataValueComparerSpecification.scala +++ b/sc/shared/src/test/scala/special/sigma/DataValueComparerSpecification.scala @@ -1,6 +1,5 @@ package special.sigma -import org.ergoplatform.SigmaConstants.ScriptCostLimit import org.scalatest.BeforeAndAfterAll import scalan.RType import scalan.util.BenchmarkUtil @@ -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 @@ -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, diff --git a/sc/shared/src/test/scala/special/sigma/SigmaDslTesting.scala b/sc/shared/src/test/scala/special/sigma/SigmaDslTesting.scala index 53c4cdddac..884e257ab4 100644 --- a/sc/shared/src/test/scala/special/sigma/SigmaDslTesting.scala +++ b/sc/shared/src/test/scala/special/sigma/SigmaDslTesting.scala @@ -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 @@ -366,7 +365,7 @@ class SigmaDslTesting extends AnyPropSpec createErgoLikeContext( newCtx, ValidationRules.currentSettings, - ScriptCostLimit.value, + evalSettings.scriptCostLimitInEvaluator, initCost = initialCostInTests.value )