From e501a8bbcaff2142266e279b4254e4f0aa904130 Mon Sep 17 00:00:00 2001 From: Alexander Chepurnoy Date: Fri, 26 Jul 2024 18:23:19 +0300 Subject: [PATCH] polishing, LSV6 test --- .../src/main/scala/sigma/ast/methods.scala | 19 ++++++------ .../scala/sigma/eval/ErgoTreeEvaluator.scala | 1 + .../interpreter/CErgoTreeEvaluator.scala | 1 + .../scala/sigma/LanguageSpecificationV6.scala | 30 ++++++++++++++++++- 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/data/shared/src/main/scala/sigma/ast/methods.scala b/data/shared/src/main/scala/sigma/ast/methods.scala index 71ac0a1e1f..487665aa2c 100644 --- a/data/shared/src/main/scala/sigma/ast/methods.scala +++ b/data/shared/src/main/scala/sigma/ast/methods.scala @@ -1467,18 +1467,19 @@ case object SHeaderMethods extends MonoTypeMethods { E.checkPow_eval(mc, header) } + private lazy val v5Methods = super.getMethods() ++ Seq( + idMethod, versionMethod, parentIdMethod, ADProofsRootMethod, stateRootMethod, transactionsRootMethod, + timestampMethod, nBitsMethod, heightMethod, extensionRootMethod, minerPkMethod, powOnetimePkMethod, + powNonceMethod, powDistanceMethod, votesMethod) + + // 6.0 : checkPow method added + private lazy val v6Methods = v5Methods ++ Seq(checkPowMethod) + protected override def getMethods() = { if (VersionContext.current.isV6SoftForkActivated) { - // 6.0 : checkPow method added - super.getMethods() ++ Seq( - idMethod, versionMethod, parentIdMethod, ADProofsRootMethod, stateRootMethod, transactionsRootMethod, - timestampMethod, nBitsMethod, heightMethod, extensionRootMethod, minerPkMethod, powOnetimePkMethod, - powNonceMethod, powDistanceMethod, votesMethod, checkPowMethod) + v6Methods } else { - super.getMethods() ++ Seq( - idMethod, versionMethod, parentIdMethod, ADProofsRootMethod, stateRootMethod, transactionsRootMethod, - timestampMethod, nBitsMethod, heightMethod, extensionRootMethod, minerPkMethod, powOnetimePkMethod, - powNonceMethod, powDistanceMethod, votesMethod) + v5Methods } } } diff --git a/data/shared/src/main/scala/sigma/eval/ErgoTreeEvaluator.scala b/data/shared/src/main/scala/sigma/eval/ErgoTreeEvaluator.scala index 610be08c9c..b95c034bd2 100644 --- a/data/shared/src/main/scala/sigma/eval/ErgoTreeEvaluator.scala +++ b/data/shared/src/main/scala/sigma/eval/ErgoTreeEvaluator.scala @@ -139,6 +139,7 @@ abstract class ErgoTreeEvaluator { mc: MethodCall, tree: AvlTree, operations: Coll[Coll[Byte]], proof: Coll[Byte]): Option[AvlTree] + /** Implements evaluation of Header.checkPow method call ErgoTree node. */ def checkPow_eval(mc: MethodCall, header: Header): Boolean } diff --git a/interpreter/shared/src/main/scala/sigmastate/interpreter/CErgoTreeEvaluator.scala b/interpreter/shared/src/main/scala/sigmastate/interpreter/CErgoTreeEvaluator.scala index 1c58e00a97..c0759f4a5c 100644 --- a/interpreter/shared/src/main/scala/sigmastate/interpreter/CErgoTreeEvaluator.scala +++ b/interpreter/shared/src/main/scala/sigmastate/interpreter/CErgoTreeEvaluator.scala @@ -218,6 +218,7 @@ class CErgoTreeEvaluator( } } + /** Implements evaluation of Header.checkPow method call ErgoTree node. */ override def checkPow_eval(mc: MethodCall, header: Header): Boolean = { val checkPowCostInfo = OperationCostInfo(checkPowMethod.costKind.asInstanceOf[FixedCost], NamedDesc("Header.checkPow")) fixedCostOp(checkPowCostInfo){ diff --git a/sc/shared/src/test/scala/sigma/LanguageSpecificationV6.scala b/sc/shared/src/test/scala/sigma/LanguageSpecificationV6.scala index 3b3de407b5..62425f747e 100644 --- a/sc/shared/src/test/scala/sigma/LanguageSpecificationV6.scala +++ b/sc/shared/src/test/scala/sigma/LanguageSpecificationV6.scala @@ -1,6 +1,6 @@ package sigma -import sigma.ast.{Apply, Downcast, FixedCost, FixedCostItem, FuncValue, GetVar, JitCost, OptionGet, SBigInt, SByte, SInt, SLong, SShort, ValUse} +import sigma.ast.{Apply, Downcast, FixedCost, FixedCostItem, FuncValue, GetVar, JitCost, MethodCall, OptionGet, SBigInt, SBoolean, SByte, SHeader, SHeaderMethods, SInt, SLong, SShort, ValUse, Value} import sigma.data.{CBigInt, ExactNumeric} import sigma.eval.SigmaDsl import sigma.util.Extensions.{BooleanOps, ByteOps, IntOps, LongOps} @@ -338,6 +338,34 @@ class LanguageSpecificationV6 extends LanguageSpecificationBase { suite => } } + property("Header new methods") { + def checkPoW = newFeature({ (x: Header) => x.checkPow}, + "{ (x: Header) => x.checkPow }", + FuncValue( + Array((1, SHeader)), + MethodCall.typed[Value[SBoolean.type]]( + ValUse(1, SHeader), + SHeaderMethods.getMethodByName("checkPow"), + IndexedSeq(), + Map() + ) + ), + sinceVersion = VersionContext.V6SoftForkVersion) + + if (VersionContext.current.isV6SoftForkActivated) { + forAll { x: Header => + Seq(checkPoW).map(_.checkEquality(x)) + } + } else { + an[Exception] shouldBe thrownBy { + forAll { x: Header => + Seq(checkPoW).map(_.checkEquality(x)) + } + } + } + + } + // TODO v6.0: implement Option.fold (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/479) property("Option new methods") { val n = ExactNumeric.LongIsExactNumeric