Skip to content

Commit

Permalink
fixing JS reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Oct 4, 2024
1 parent 6b77c25 commit ce11553
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion data/shared/src/main/scala/sigma/SigmaDataReflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
4 changes: 2 additions & 2 deletions data/shared/src/main/scala/sigma/ast/SMethod.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]])
}
Expand Down

0 comments on commit ce11553

Please sign in to comment.