From ce115538ba3affb08200290c0468ec7440a93722 Mon Sep 17 00:00:00 2001 From: Alexander Chepurnoy Date: Fri, 4 Oct 2024 21:50:00 +0300 Subject: [PATCH] fixing JS reflection --- .../scala/sigma/SigmaDataReflection.scala | 2 +- .../src/main/scala/sigma/ast/SMethod.scala | 4 +-- .../scala/sigmastate/eval/BasicOpsTests.scala | 27 ++++++++++++++++++- .../sigma/compiler/ir/GraphIRReflection.scala | 6 +++++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/data/shared/src/main/scala/sigma/SigmaDataReflection.scala b/data/shared/src/main/scala/sigma/SigmaDataReflection.scala index 2e7d5d0595..fe72f364fa 100644 --- a/data/shared/src/main/scala/sigma/SigmaDataReflection.scala +++ b/data/shared/src/main/scala/sigma/SigmaDataReflection.scala @@ -336,7 +336,7 @@ object SigmaDataReflection { args(1).asInstanceOf[SigmaDslBuilder], args(2).asInstanceOf[SType#WrappedType])(args(3).asInstanceOf[ErgoTreeEvaluator]) }, - mkMethod(clazz, "powHit_eval", Array[Class[_]](classOf[MethodCall], classOf[SigmaDslBuilder], classOf[Object], classOf[Object], classOf[Object], classOf[Object], classOf[Object], classOf[ErgoTreeEvaluator])) { (obj, args) => + mkMethod(clazz, "powHit_eval", Array[Class[_]](classOf[MethodCall], classOf[SigmaDslBuilder], classOf[Int], classOf[Coll[_]], classOf[Coll[_]], classOf[Coll[_]], classOf[Int], classOf[ErgoTreeEvaluator])) { (obj, args) => obj.asInstanceOf[SGlobalMethods.type].powHit_eval(args(0).asInstanceOf[MethodCall], args(1).asInstanceOf[SigmaDslBuilder], args(2).asInstanceOf[Int], diff --git a/data/shared/src/main/scala/sigma/ast/SMethod.scala b/data/shared/src/main/scala/sigma/ast/SMethod.scala index e428b3babe..0ab4581b56 100644 --- a/data/shared/src/main/scala/sigma/ast/SMethod.scala +++ b/data/shared/src/main/scala/sigma/ast/SMethod.scala @@ -155,8 +155,8 @@ case class SMethod( val methodName = name + "_eval" val m = try { objType.thisRClass.getMethod(methodName, paramTypes:_*) - } - catch { case e: NoSuchMethodException => + } catch { case e: NoSuchMethodException => + println("dm: " + objType.thisRClass.getDeclaredMethods().mkString(", ")) throw new RuntimeException(s"Cannot find eval method def $methodName(${Seq(paramTypes:_*)})", e) } m diff --git a/interpreter/shared/src/test/scala/sigmastate/eval/BasicOpsTests.scala b/interpreter/shared/src/test/scala/sigmastate/eval/BasicOpsTests.scala index fbbe24d8d7..a0d1b344d4 100644 --- a/interpreter/shared/src/test/scala/sigmastate/eval/BasicOpsTests.scala +++ b/interpreter/shared/src/test/scala/sigmastate/eval/BasicOpsTests.scala @@ -67,6 +67,32 @@ class BasicOpsTests extends AnyFunSuite with ContractsTestkit with Matchers { box.creationInfo._1 shouldBe a [Integer] } + test("xor evaluation") { + val es = CErgoTreeEvaluator.DefaultEvalSettings + val accumulator = new CostAccumulator( + initialCost = JitCost(0), + costLimit = Some(JitCost.fromBlockCost(es.scriptCostLimitInEvaluator))) + + val context = new CContext( + noInputs.toColl, noHeaders, dummyPreHeader, + Array[Box]().toColl, Array[Box]().toColl, 0, null, 0, null, + dummyPubkey.toColl, Colls.emptyColl, VersionContext.V6SoftForkVersion, VersionContext.V6SoftForkVersion) + + val evaluator = new CErgoTreeEvaluator( + context = context, + constants = ErgoTree.EmptyConstants, + coster = accumulator, DefaultProfiler, es) + + val msg = Colls.fromArray(Base16.decode("0a101b8c6a4f2e").get) + VersionContext.withVersions(VersionContext.V6SoftForkVersion, VersionContext.V6SoftForkVersion) { + val res = MethodCall(Global, SGlobalMethods.xorMethod, + IndexedSeq(ByteArrayConstant(msg), ByteArrayConstant(msg)), Map.empty) + .evalTo[sigma.Coll[Byte]](Map.empty)(evaluator) + + res should be(Colls.fromArray(Base16.decode("00000000000000").get)) + } + } + /** * Checks BigInt.nbits evaluation for SigmaDSL as well as AST interpreter (MethodCall) layers */ @@ -94,7 +120,6 @@ class BasicOpsTests extends AnyFunSuite with ContractsTestkit with Matchers { constants = ErgoTree.EmptyConstants, coster = accumulator, DefaultProfiler, es) - VersionContext.withVersions(VersionContext.V6SoftForkVersion, VersionContext.V6SoftForkVersion) { val res = MethodCall(Global, SGlobalMethods.powHitMethod, IndexedSeq(IntConstant(k), ByteArrayConstant(msg), ByteArrayConstant(nonce), diff --git a/sc/shared/src/main/scala/sigma/compiler/ir/GraphIRReflection.scala b/sc/shared/src/main/scala/sigma/compiler/ir/GraphIRReflection.scala index 79fba9bb96..99b325e688 100644 --- a/sc/shared/src/main/scala/sigma/compiler/ir/GraphIRReflection.scala +++ b/sc/shared/src/main/scala/sigma/compiler/ir/GraphIRReflection.scala @@ -1,5 +1,6 @@ package sigma.compiler.ir +import sigma.Coll import sigma.ast.SType import sigma.compiler.ir.primitives.Thunks import sigma.data.RType @@ -512,6 +513,11 @@ object GraphIRReflection { mkMethod(clazz, "serialize", Array[Class[_]](classOf[Base#Ref[_]])) { (obj, args) => obj.asInstanceOf[ctx.SigmaDslBuilder].serialize(args(0).asInstanceOf[ctx.Ref[Any]]) }, + mkMethod(clazz, "powHit", Array[Class[_]](classOf[Base#Ref[_]])) { (obj, args) => + obj.asInstanceOf[ctx.SigmaDslBuilder].powHit(args(0).asInstanceOf[ctx.Ref[Int]], + args(1).asInstanceOf[ctx.Ref[Coll[Byte]]], args(2).asInstanceOf[ctx.Ref[Coll[Byte]]], + args(3).asInstanceOf[ctx.Ref[Coll[Byte]]], args(4).asInstanceOf[ctx.Ref[Int]]) + }, mkMethod(clazz, "fromBigEndianBytes", Array[Class[_]](classOf[Base#Ref[_]], classOf[TypeDescs#Elem[_]])) { (obj, args) => obj.asInstanceOf[ctx.SigmaDslBuilder].fromBigEndianBytes(args(0).asInstanceOf[ctx.Ref[ctx.Coll[Byte]]])(args(1).asInstanceOf[ctx.Elem[SType]]) }