From 7602213d66e1483c4c606bc059621b9eb0978319 Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Thu, 17 Mar 2022 22:30:55 +0100 Subject: [PATCH 01/44] v5.0-jit-costing: Enhance all tests with expected jit costing --- .../special/sigma/SigmaDslSpecification.scala | 307 ++++++++++++++---- .../scala/special/sigma/SigmaDslTesting.scala | 13 +- 2 files changed, 244 insertions(+), 76 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 92af9b3ce8..f731012d54 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -196,19 +196,32 @@ class SigmaDslSpecification extends SigmaDslTesting Vector((1, STuple(Vector(SBoolean, SBoolean)))), BinXor( SelectField.typed[BoolValue](ValUse(1, STuple(Vector(SBoolean, SBoolean))), 1.toByte), - SelectField.typed[BoolValue](ValUse(1, STuple(Vector(SBoolean, SBoolean))), 2.toByte) - ) - )) + SelectField.typed[BoolValue](ValUse(1, STuple(Vector(SBoolean, SBoolean))), 2.toByte)))) + val expectedCostDetails = CostDetails( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(BinXor) + ) + ) val expectedCost = 36518 + val newCost = 1788 val cases = Seq( - (true, true) -> Expected(Success(false), expectedCost) + (true, true) -> Expected(Success(false), expectedCost, expectedCostDetails, newCost) ) verifyCases(cases, feature) val initCost = 100 initialCostInTests.withValue(initCost) { val cases = Seq( - (true, true) -> Expected(Success(false), expectedCost + initCost) + (true, true) -> Expected(Success(false), expectedCost + initCost, expectedCostDetails, newCost + initCost) ) verifyCases(cases, feature) } @@ -227,7 +240,8 @@ class SigmaDslSpecification extends SigmaDslTesting SelectField.typed[BoolValue](ValUse(1, STuple(Vector(SInt, SBoolean))), 2.toByte) ) )) - val newCost = CostDetails( + val newCost = 36865 + val expectedCostDetails = CostDetails( Array( FixedCostItem(Apply), FixedCostItem(FuncValue), @@ -244,12 +258,12 @@ class SigmaDslSpecification extends SigmaDslTesting ) ) val cases = Seq( - (1095564593, true) -> Expected(Success(true), 36865, newCost), - (-901834021, true) -> Expected(Success(true), 36865, newCost), - (595045530, false) -> Expected(Success(false), 36865, newCost), - (-1157998227, false) -> Expected(Success(false), 36865, newCost), - (0, true) -> Expected(Success(false), 36865, newCost), - (0, false) -> Expected(Success(true), 36865, newCost) + (1095564593, true) -> Expected(Success(true), newCost, expectedCostDetails), + (-901834021, true) -> Expected(Success(true), newCost, expectedCostDetails), + (595045530, false) -> Expected(Success(false), newCost, expectedCostDetails), + (-1157998227, false) -> Expected(Success(false), newCost, expectedCostDetails), + (0, true) -> Expected(Success(false), newCost, expectedCostDetails), + (0, false) -> Expected(Success(true), newCost, expectedCostDetails) ) verifyCases(cases, xor) } @@ -288,11 +302,12 @@ class SigmaDslSpecification extends SigmaDslTesting FixedCostItem(SelectField) ) ) + val newCost = 38241 val cases = Seq( - (false, true) -> Expected(Success(false), 38241, cost1), - (false, false) -> Expected(Success(false), 38241, cost1), - (true, true) -> Expected(Success(true), 38241, cost2), - (true, false) -> Expected(Success(false), 38241, cost2) + (false, true) -> Expected(Success(false), newCost, cost1), + (false, false) -> Expected(Success(false), newCost, cost1), + (true, true) -> Expected(Success(true), newCost, cost2), + (true, false) -> Expected(Success(false), newCost, cost2) ) verifyCases(cases, eq) } @@ -331,11 +346,12 @@ class SigmaDslSpecification extends SigmaDslTesting FixedCostItem(SelectField) ) ) + val newCost = 38241 val cases = Seq( - (true, false) -> Expected(Success(true), 38241, cost1), - (true, true) -> Expected(Success(true), 38241, cost1), - (false, false) -> Expected(Success(false), 38241, cost2), - (false, true) -> Expected(Success(true), 38241, cost2) + (true, false) -> Expected(Success(true), newCost, cost1), + (true, true) -> Expected(Success(true), newCost, cost1), + (false, false) -> Expected(Success(false), newCost, cost2), + (false, true) -> Expected(Success(true), newCost, cost2) ) verifyCases(cases, eq) } @@ -346,7 +362,8 @@ class SigmaDslSpecification extends SigmaDslTesting FixedCostItem(Apply), FixedCostItem(FuncValue), FixedCostItem(GetVar), - FixedCostItem(OptionGet), FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), FixedCostItem(ValUse), FixedCostItem(BinOr) ) @@ -356,7 +373,8 @@ class SigmaDslSpecification extends SigmaDslTesting FixedCostItem(Apply), FixedCostItem(FuncValue), FixedCostItem(GetVar), - FixedCostItem(OptionGet), FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), FixedCostItem(ValUse), FixedCostItem(BinAnd) ) @@ -366,7 +384,8 @@ class SigmaDslSpecification extends SigmaDslTesting FixedCostItem(Apply), FixedCostItem(FuncValue), FixedCostItem(GetVar), - FixedCostItem(OptionGet), FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), FixedCostItem(ValUse), FixedCostItem(BinAnd), FixedCostItem(ValUse), @@ -378,7 +397,8 @@ class SigmaDslSpecification extends SigmaDslTesting FixedCostItem(Apply), FixedCostItem(FuncValue), FixedCostItem(GetVar), - FixedCostItem(OptionGet), FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), FixedCostItem(ValUse), FixedCostItem(BinAnd), FixedCostItem(ValUse), @@ -392,7 +412,8 @@ class SigmaDslSpecification extends SigmaDslTesting FixedCostItem(Apply), FixedCostItem(FuncValue), FixedCostItem(GetVar), - FixedCostItem(OptionGet), FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), FixedCostItem(ValUse), FixedCostItem(BinAnd), FixedCostItem(ValUse), @@ -716,6 +737,7 @@ class SigmaDslSpecification extends SigmaDslTesting FixedCostItem(ValUse) ) ) + // jozi: not needed - same as costIdentity above, is identity == upcast? val costUpcast = CostDetails( Array( FixedCostItem(Apply), @@ -726,6 +748,10 @@ class SigmaDslSpecification extends SigmaDslTesting FixedCostItem(ValUse) ) ) + val costShortUpcast = CostDetails(costIdentity.trace :+ TypeBasedCostItem(Upcast, SShort)) + val costIntUpcast = CostDetails(costIdentity.trace :+ TypeBasedCostItem(Upcast, SInt)) + val costLongUpcast = CostDetails(costIdentity.trace :+ TypeBasedCostItem(Upcast, SLong)) + val costBigIntUpcast = CostDetails(costIdentity.trace :+ TypeBasedCostItem(Upcast, SBigInt)) val costDowncast = CostDetails( Array( FixedCostItem(Apply), @@ -736,6 +762,8 @@ class SigmaDslSpecification extends SigmaDslTesting FixedCostItem(ValUse) ) ) + val costByteDowncast = CostDetails(costIdentity.trace :+ TypeBasedCostItem(Downcast, SByte)) + val costShortDowncast = CostDetails(costIdentity.trace :+ TypeBasedCostItem(Downcast, SShort)) def costArithOps(tpe: SType) = CostDetails( Array( FixedCostItem(Apply), @@ -743,10 +771,13 @@ class SigmaDslSpecification extends SigmaDslTesting FixedCostItem(GetVar), FixedCostItem(OptionGet), FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), FixedCostItem(ValUse), FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), FixedCostItem(ValUse), FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), FixedCostItem(ValUse), FixedCostItem(ValUse), TypeBasedCostItem(ArithOp.Plus, tpe), @@ -792,7 +823,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def expected(v: Short) = Expected(Success(v), 35902, costUpcast) + def expected(v: Short) = Expected(Success(v), 35902, costShortUpcast) Seq( (0.toByte, expected(0.toShort)), (1.toByte, expected(1.toShort)), @@ -809,7 +840,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def expected(v: Int) = Expected(Success(v), 35902, costUpcast) + def expected(v: Int) = Expected(Success(v), 35902, costIntUpcast) Seq( (0.toByte, expected(0)), (1.toByte, expected(1)), @@ -826,7 +857,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def expected(v: Long) = Expected(Success(v), 35902, costUpcast) + def expected(v: Long) = Expected(Success(v), 35902, costLongUpcast) Seq( (0.toByte, expected(0L)), (1.toByte, expected(1L)), @@ -843,7 +874,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def expected(v: BigInt) = Expected(Success(v), 35932, costUpcast) + def expected(v: BigInt) = Expected(Success(v), 35932, costBigIntUpcast) Seq( (0.toByte, expected(CBigInt(new BigInteger("0", 16)))), (1.toByte, expected(CBigInt(new BigInteger("1", 16)))), @@ -1039,7 +1070,9 @@ class SigmaDslSpecification extends SigmaDslTesting ) ++ neqCost CostDetails(trace) } - val constNeqCost: Seq[CostItem] = Array[CostItem]() + + // jozi: see where it is used and make more compact + val constNeqCost: Seq[CostItem] = Array[CostItem](FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3)))) def costLE(tpe: SType) = CostDetails( Array( @@ -1199,7 +1232,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 35976, costDowncast) + def success[T](v: T) = Expected(Success(v), 35976, costByteDowncast) Seq( (Short.MinValue, Expected(new ArithmeticException("Byte overflow"))), (-21626.toShort, Expected(new ArithmeticException("Byte overflow"))), @@ -1235,7 +1268,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 35902, costUpcast) + def success[T](v: T) = Expected(Success(v), 35902, costIntUpcast) Seq( (-32768.toShort, success(-32768)), (-21064.toShort, success(-21064)), @@ -1252,7 +1285,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 35902, costUpcast) + def success[T](v: T) = Expected(Success(v), 35902, costLongUpcast) Seq( (-32768.toShort, success(-32768L)), (-23408.toShort, success(-23408L)), @@ -1269,7 +1302,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success(v: BigInt) = Expected(Success(v), 35932, costUpcast) + def success(v: BigInt) = Expected(Success(v), 35932, costBigIntUpcast) Seq( (-32768.toShort, success(CBigInt(new BigInteger("-8000", 16)))), (-26248.toShort, success(CBigInt(new BigInteger("-6688", 16)))), @@ -1503,7 +1536,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 35976, costDowncast) + def success[T](v: T) = Expected(Success(v), 35976, costByteDowncast) Seq( (Int.MinValue, Expected(new ArithmeticException("Byte overflow"))), (-2014394379, Expected(new ArithmeticException("Byte overflow"))), @@ -1522,7 +1555,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 35976, costDowncast) + def success[T](v: T) = Expected(Success(v), 35976, costShortDowncast) Seq( (Int.MinValue, Expected(new ArithmeticException("Short overflow"))), (Short.MinValue - 1, Expected(new ArithmeticException("Short overflow"))), @@ -1556,7 +1589,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 35902, costUpcast) + def success[T](v: T) = Expected(Success(v), 35902, costLongUpcast) Seq( (Int.MinValue, success(Int.MinValue.toLong)), (-1, success(-1L)), @@ -1571,7 +1604,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success(v: BigInt) = Expected(Success(v), 35932, costUpcast) + def success(v: BigInt) = Expected(Success(v), 35932, costBigIntUpcast) Seq( (Int.MinValue, success(CBigInt(new BigInteger("-80000000", 16)))), (-1937187314, success(CBigInt(new BigInteger("-737721f2", 16)))), @@ -2138,10 +2171,13 @@ class SigmaDslSpecification extends SigmaDslTesting FixedCostItem(GetVar), FixedCostItem(OptionGet), FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), FixedCostItem(ValUse), FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), FixedCostItem(ValUse), FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), FixedCostItem(ValUse), FixedCostItem(ValUse), TypeBasedCostItem(ArithOp.Plus, SBigInt), @@ -2335,7 +2371,8 @@ class SigmaDslSpecification extends SigmaDslTesting swapArgs(LT_cases, cost = 36342, newCost = costGT(SBigInt)), ">", GT.apply)(o.gt(_, _)) - val neqCases = newCasesFrom2(LT_cases.map(_._1))(_ != _, cost = 36337, newCost = costNEQ(constNeqCost)) + val constBigIntCost = Array[CostItem](FixedCostItem(NamedDesc("EQ_BigInt"), FixedCost(JitCost(5)))) + val neqCases = newCasesFrom2(LT_cases.map(_._1))(_ != _, cost = 36337, newCost = costNEQ(constBigIntCost)) verifyOp(neqCases, "!=", NEQ.apply)(_ != _) } @@ -2471,48 +2508,158 @@ class SigmaDslSpecification extends SigmaDslTesting } property("NEQ of pre-defined types") { - verifyNeq(ge1, ge2, 36337, constNeqCost)(_.asInstanceOf[CGroupElement].copy()) - verifyNeq(t1, t2, 36337, constNeqCost)(_.asInstanceOf[CAvlTree].copy()) - verifyNeq(b1, b2, 36417, - Array[CostItem]())(_.asInstanceOf[CostingBox].copy()) - verifyNeq(preH1, preH2, 36337, constNeqCost)(_.asInstanceOf[CPreHeader].copy()) - verifyNeq(h1, h2, 36337, constNeqCost)(_.asInstanceOf[CHeader].copy()) - } - + verifyNeq(ge1, ge2, 36337, Array[CostItem](FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172)))))(_.asInstanceOf[CGroupElement].copy()) + verifyNeq(t1, t2, 36337, Array[CostItem](FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6)))))(_.asInstanceOf[CAvlTree].copy()) + verifyNeq(b1, b2, 36417, Array[CostItem]())(_.asInstanceOf[CostingBox].copy()) + verifyNeq(preH1, preH2, 36337, Array[CostItem](FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4)))))(_.asInstanceOf[CPreHeader].copy()) + verifyNeq(h1, h2, 36337, Array[CostItem](FixedCostItem(NamedDesc("EQ_Header"), FixedCost(JitCost(6)))))(_.asInstanceOf[CHeader].copy()) + } + + // jozi: Test fails, last item is(n't) part of the output + val tuplesNeqCost = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))) + ) property("NEQ of tuples of numerics") { - verifyNeq((0.toByte, 1.toByte), (1.toByte, 1.toByte), 36337, constNeqCost)(_.copy()) - verifyNeq((0.toShort, 1.toByte), (1.toShort, 1.toByte), 36337, constNeqCost)(_.copy()) - verifyNeq((0, 1.toByte), (1, 1.toByte), 36337, constNeqCost)(_.copy()) - verifyNeq((0.toLong, 1.toByte), (1.toLong, 1.toByte), 36337, constNeqCost)(_.copy()) - verifyNeq((0.toBigInt, 1.toByte), (1.toBigInt, 1.toByte), 36337, constNeqCost)(_.copy()) + verifyNeq((0.toByte, 1.toByte), (1.toByte, 1.toByte), 36337, tuplesNeqCost)(_.copy()) + // verifyNeq((0.toShort, 1.toByte), (1.toShort, 1.toByte), 36337, tuplesNeqCost)(_.copy()) + // verifyNeq((0, 1.toByte), (1, 1.toByte), 36337, tuplesNeqCost)(_.copy()) + // verifyNeq((0.toLong, 1.toByte), (1.toLong, 1.toByte), 36337, tuplesNeqCost)(_.copy()) + // verifyNeq((0.toBigInt, 1.toByte), (1.toBigInt, 1.toByte), 36337, tuplesNeqCost)(_.copy()) } + val groupNeqCost = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), + FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))) + ) + val treeNeqCost = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))) + ) + val preHeaderNeqCost = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))) + ) + val headerNeqCost = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Header"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Header"), FixedCost(JitCost(6))) + ) + property("NEQ of tuples of pre-defined types") { - verifyNeq((ge1, ge1), (ge1, ge2), 36337, constNeqCost)(_.copy()) - verifyNeq((t1, t1), (t1, t2), 36337, constNeqCost)(_.copy()) + verifyNeq((ge1, ge1), (ge1, ge2), 36337, groupNeqCost)(_.copy()) + verifyNeq((t1, t1), (t1, t2), 36337, treeNeqCost)(_.copy()) verifyNeq((b1, b1), (b1, b2), cost = 36497, neqCost = Array[CostItem]() - )(_.copy()) - verifyNeq((preH1, preH1), (preH1, preH2), 36337, constNeqCost)(_.copy()) - verifyNeq((h1, h1), (h1, h2), 36337, constNeqCost)(_.copy()) + )(_.copy()) + verifyNeq((preH1, preH1), (preH1, preH2), 36337, preHeaderNeqCost)(_.copy()) + verifyNeq((h1, h1), (h1, h2), 36337, headerNeqCost)(_.copy()) } + val nestedTuplesNeqCost1 = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))) + ) + val nestedTuplesNeqCost2 = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))) + ) + val nestedTuplesNeqCost3 = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))) + ) + val nestedTuplesNeqCost4 = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Header"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Header"), FixedCost(JitCost(6))) + ) + val nestedTuplesNeqCost5 = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))) + ) + val nestedTuplesNeqCost6 = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))) + ) + val nestedTuplesNeqCost7 = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))) + ) + val nestedTuplesNeqCost8 = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Header"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Header"), FixedCost(JitCost(6))) + ) property("NEQ of nested tuples") { - verifyNeq((ge1, (t1, t1)), (ge1, (t1, t2)), 36337)(_.copy()) - verifyNeq((ge1, (t1, (b1, b1))), (ge1, (t1, (b1, b2))), 36497)(_.copy()) - verifyNeq((ge1, (t1, (b1, (preH1, preH1)))), (ge1, (t1, (b1, (preH1, preH2)))), 36417)(_.copy()) - verifyNeq((ge1, (t1, (b1, (preH1, (h1, h1))))), (ge1, (t1, (b1, (preH1, (h1, h2))))), 36427)(_.copy()) - - verifyNeq(((ge1, t1), t1), ((ge1, t1), t2), 36337)(_.copy()) - verifyNeq((((ge1, t1), b1), b1), (((ge1, t1), b1), b2), 36497)(_.copy()) - verifyNeq((((ge1, t1), b1), (preH1, preH1)), (((ge1, t1), b1), (preH1, preH2)), 36417)(_.copy()) - verifyNeq((((ge1, t1), b1), (preH1, (h1, h1))), (((ge1, t1), b1), (preH1, (h1, h2))), 36427)(_.copy()) - } - + verifyNeq((ge1, (t1, t1)), (ge1, (t1, t2)), 36337, nestedTuplesNeqCost1)(_.copy()) + verifyNeq((ge1, (t1, (b1, b1))), (ge1, (t1, (b1, b2))), 36497, nestedTuplesNeqCost2)(_.copy()) + verifyNeq((ge1, (t1, (b1, (preH1, preH1)))), (ge1, (t1, (b1, (preH1, preH2)))), 36417, nestedTuplesNeqCost3)(_.copy()) + verifyNeq((ge1, (t1, (b1, (preH1, (h1, h1))))), (ge1, (t1, (b1, (preH1, (h1, h2))))), 36427, nestedTuplesNeqCost4)(_.copy()) + + verifyNeq(((ge1, t1), t1), ((ge1, t1), t2), 36337, nestedTuplesNeqCost5)(_.copy()) + verifyNeq((((ge1, t1), b1), b1), (((ge1, t1), b1), b2), 36497, nestedTuplesNeqCost6)(_.copy()) + verifyNeq((((ge1, t1), b1), (preH1, preH1)), (((ge1, t1), b1), (preH1, preH2)), 36417, nestedTuplesNeqCost7)(_.copy()) + verifyNeq((((ge1, t1), b1), (preH1, (h1, h1))), (((ge1, t1), b1), (preH1, (h1, h2))), 36427, nestedTuplesNeqCost8)(_.copy()) + } + // jozi: Cyclic fail again. + val collNeqCost1 = Array( + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + SeqCostItem(NamedDesc("EQ_COA_Byte"), PerItemCost(JitCost(15), JitCost(2), 128), 0) + ) property("NEQ of collections of pre-defined types") { implicit val evalSettings = suite.evalSettings.copy(isMeasureOperationTime = false) - verifyNeq(Coll[Byte](), Coll(1.toByte), 36337)(cloneColl(_)) + verifyNeq(Coll[Byte](), Coll(1.toByte), 36337, collNeqCost1)(cloneColl(_)) verifyNeq(Coll[Byte](0, 1), Coll(1.toByte, 1.toByte), 36337)(cloneColl(_)) verifyNeq(Coll[Short](), Coll(1.toShort), 36337)(cloneColl(_)) @@ -2924,6 +3071,7 @@ class SigmaDslSpecification extends SigmaDslTesting } verifyCases( { + // jozi: here def success[T](v: T) = Expected(Success(v), 36182) Seq( (t1, success(Helpers.decodeBytes("000183807f66b301530120ff7fc6bd6601ff01ff7f7d2bedbbffff00187fe89094"))), @@ -2937,6 +3085,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { + // jozi: And here def success[T](v: T) = Expected(Success(v), 36260) Seq( (t1, success(6.toByte)), @@ -2950,6 +3099,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { + // jozi: And here def success[T](v: T) = Expected(Success(v), 36136) Seq( (t1, success(1)), @@ -2963,6 +3113,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { + // jozi: also here def success[T](v: T) = Expected(Success(v), 37151) Seq( (t1, success(Some(1))), @@ -3002,6 +3153,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { + // jozi: here def success[T](v: T) = Expected(Success(v), 36502) Seq( (t1, success(true)), @@ -3202,6 +3354,7 @@ class SigmaDslSpecification extends SigmaDslTesting ) )) + // jozi: also here def success[T](v: T) = Expected(Success(v), 0) { // getMany with a bunch of existing keys @@ -3242,6 +3395,7 @@ class SigmaDslSpecification extends SigmaDslTesting contains.checkExpected(input, success(okContains)) get.checkExpected(input, success(valueOpt)) + // jozi: And here contains.checkVerify(input, Expected(value = Success(okContains), cost = 37850)) get.checkVerify(input, Expected(value = Success(valueOpt), cost = 38372)) } @@ -3252,6 +3406,7 @@ class SigmaDslSpecification extends SigmaDslTesting { val input = (tree, (keys, proof)) getMany.checkExpected(input, success(expRes)) + // jozi: also here getMany.checkVerify(input, Expected(value = Success(expRes), cost = 38991)) } @@ -3259,6 +3414,7 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (tree, digest) val (res, _) = updateDigest.checkEquality(input).getOrThrow res.digest shouldBe digest + // jozi: also here updateDigest.checkVerify(input, Expected(value = Success(res), cost = 36341)) } @@ -3268,6 +3424,7 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (tree, newOps) val (res,_) = updateOperations.checkEquality(input).getOrThrow res.enabledOperations shouldBe newOps + // jozi: also here updateOperations.checkVerify(input, Expected(value = Success(res), cost = 36341)) } @@ -3278,6 +3435,7 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (tree, (key, invalidProof)) val (res, _) = contains.checkEquality(input).getOrThrow res shouldBe false + // jozi: also here contains.checkVerify(input, Expected(value = Success(res), cost = 37850)) } @@ -3403,6 +3561,7 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (preInsertTree, (kvs, insertProof)) val (res, _) = insert.checkEquality(input).getOrThrow res.isDefined shouldBe true + // jozi: also here insert.checkVerify(input, Expected(value = Success(res), cost = 38501)) } @@ -3411,6 +3570,7 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (readonlyTree, (kvs, insertProof)) val (res, _) = insert.checkEquality(input).getOrThrow res.isDefined shouldBe false + // jozi: also here insert.checkVerify(input, Expected(value = Success(res), cost = 38501)) } @@ -3421,6 +3581,7 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (tree, (invalidKvs, insertProof)) val (res, _) = insert.checkEquality(input).getOrThrow res.isDefined shouldBe true // TODO v6.0: should it really be true? (looks like a bug) + // jozi: also here insert.checkVerify(input, Expected(value = Success(res), cost = 38501)) } @@ -3503,6 +3664,7 @@ class SigmaDslSpecification extends SigmaDslTesting )) val cost = 40952 + // jozi: also here def success[T](v: T) = Expected(Success(v), 0) forAll(keyCollGen, bytesCollGen) { (key, value) => @@ -3519,6 +3681,7 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (preUpdateTree, (kvs, updateProof)) val res = Some(endTree) update.checkExpected(input, success(res)) + // jozi: also here update.checkVerify(input, Expected(value = Success(res), cost = cost)) } @@ -3528,6 +3691,7 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (tree, (keys, updateProof)) val res = Some(tree) update.checkExpected(input, success(res)) + // jozi: also here update.checkVerify(input, Expected(value = Success(res), cost = cost)) } @@ -3535,6 +3699,7 @@ class SigmaDslSpecification extends SigmaDslTesting val readonlyTree = createTree(preUpdateDigest) val input = (readonlyTree, (kvs, updateProof)) update.checkExpected(input, success(None)) + // jozi: also here update.checkVerify(input, Expected(value = Success(None), cost = cost)) } @@ -3544,6 +3709,7 @@ class SigmaDslSpecification extends SigmaDslTesting val invalidKvs = Colls.fromItems((invalidKey -> newValue)) val input = (tree, (invalidKvs, updateProof)) update.checkExpected(input, success(None)) + // jozi: also here update.checkVerify(input, Expected(value = Success(None), cost = cost)) } @@ -3554,6 +3720,7 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (tree, (invalidKvs, updateProof)) val (res, _) = update.checkEquality(input).getOrThrow res.isDefined shouldBe true // TODO v6.0: should it really be true? (looks like a bug) + // jozi: also here update.checkVerify(input, Expected(value = Success(res), cost = cost)) } @@ -3562,6 +3729,7 @@ class SigmaDslSpecification extends SigmaDslTesting val invalidProof = updateProof.map(x => (-x).toByte) // any other different from proof val input = (tree, (kvs, invalidProof)) update.checkExpected(input, success(None)) + // jozi: also here update.checkVerify(input, Expected(value = Success(None), cost = cost)) } } @@ -3604,6 +3772,7 @@ class SigmaDslSpecification extends SigmaDslTesting ) )) + // jozi: also here def success[T](v: T) = Expected(Success(v), 0) { // positive test with many keys in the tree and to remove diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala index cd544b7183..c30ffe0073 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala @@ -602,13 +602,12 @@ class SigmaDslTesting extends PropSpec } else { // new cost expectation is specified, compare it with the actual result -// TODO mainnet v5.0: uncomment to enable test vectors -// funcRes.foreach { case (_, newCost) => -// if (newCost.trace != expectedTrace) { -// printCostDetails(script, newCost) -// newCost.trace shouldBe expectedTrace -// } -// } + funcRes.foreach { case (_, newCost) => + if (newCost.trace != expectedTrace) { + printCostDetails(script, newCost) + newCost.trace shouldBe expectedTrace + } + } } checkVerify(input, expected) From 553263538f0df574eaa71d0173869a1f089198e5 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Sat, 19 Mar 2022 21:14:42 +0100 Subject: [PATCH 02/44] v5.0-add-jit-costing-for-tests: Long methods --- .../special/sigma/SigmaDslSpecification.scala | 54 +++++++++++-------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index f731012d54..8362aef63a 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -737,31 +737,24 @@ class SigmaDslSpecification extends SigmaDslTesting FixedCostItem(ValUse) ) ) - // jozi: not needed - same as costIdentity above, is identity == upcast? - val costUpcast = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse) - ) - ) + val costShortUpcast = CostDetails(costIdentity.trace :+ TypeBasedCostItem(Upcast, SShort)) val costIntUpcast = CostDetails(costIdentity.trace :+ TypeBasedCostItem(Upcast, SInt)) val costLongUpcast = CostDetails(costIdentity.trace :+ TypeBasedCostItem(Upcast, SLong)) val costBigIntUpcast = CostDetails(costIdentity.trace :+ TypeBasedCostItem(Upcast, SBigInt)) - val costDowncast = CostDetails( + + def costDowncast(tpe: SType) = TracedCost( Array( FixedCostItem(Apply), FixedCostItem(FuncValue), FixedCostItem(GetVar), FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse) - ) - ) + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + TypeBasedCostItem(CompanionDesc(Downcast), Downcast.costKind, tpe) + ), + None) + val costByteDowncast = CostDetails(costIdentity.trace :+ TypeBasedCostItem(Downcast, SByte)) val costShortDowncast = CostDetails(costIdentity.trace :+ TypeBasedCostItem(Downcast, SShort)) def costArithOps(tpe: SType) = CostDetails( @@ -1833,13 +1826,17 @@ class SigmaDslSpecification extends SigmaDslTesting } } - property("Long methods equivalence") { - SLong.upcast(0L) shouldBe 0L // boundary test case - SLong.downcast(0L) shouldBe 0L // boundary test case + property("Long downcast and upcast identity") { + forAll { x: Long => + SLong.upcast(x) shouldBe x // boundary test case + SLong.downcast(x) shouldBe x // boundary test case + } + } + property("Long.toByte method") { verifyCases( { - def success[T](v: T) = Expected(Success(v), 35976, costDowncast) + def success[T](v: T) = Expected(Success(v), 35976, costDowncast(SByte)) Seq( (Long.MinValue, Expected(new ArithmeticException("Byte overflow"))), (Byte.MinValue.toLong - 1, Expected(new ArithmeticException("Byte overflow"))), @@ -1855,10 +1852,12 @@ class SigmaDslSpecification extends SigmaDslTesting existingFeature((x: Long) => x.toByteExact, "{ (x: Long) => x.toByte }", FuncValue(Vector((1, SLong)), Downcast(ValUse(1, SLong), SByte)))) + } + property("Long.toShort method") { verifyCases( { - def success[T](v: T) = Expected(Success(v), 35976, costDowncast) + def success[T](v: T) = Expected(Success(v), 35976, costDowncast(SShort)) Seq( (Long.MinValue, Expected(new ArithmeticException("Short overflow"))), (Short.MinValue.toLong - 1, Expected(new ArithmeticException("Short overflow"))), @@ -1874,10 +1873,12 @@ class SigmaDslSpecification extends SigmaDslTesting existingFeature((x: Long) => x.toShortExact, "{ (x: Long) => x.toShort }", FuncValue(Vector((1, SLong)), Downcast(ValUse(1, SLong), SShort)))) + } + property("Long.toInt method") { verifyCases( { - def success[T](v: T) = Expected(Success(v), 35976, costDowncast) + def success[T](v: T) = Expected(Success(v), 35976, costDowncast(SInt)) Seq( (Long.MinValue, Expected(new ArithmeticException("Int overflow"))), (Int.MinValue.toLong - 1, Expected(new ArithmeticException("Int overflow"))), @@ -1893,7 +1894,9 @@ class SigmaDslSpecification extends SigmaDslTesting existingFeature((x: Long) => x.toIntExact, "{ (x: Long) => x.toInt }", FuncValue(Vector((1, SLong)), Downcast(ValUse(1, SLong), SInt)))) + } + property("Long.toLong method") { verifyCases( { def success[T](v: T) = Expected(Success(v), 35798, costIdentity) @@ -1908,10 +1911,12 @@ class SigmaDslSpecification extends SigmaDslTesting existingFeature((x: Long) => x.toLong, "{ (x: Long) => x.toLong }", FuncValue(Vector((1, SLong)), ValUse(1, SLong)))) + } + property("Long.toBigInt method") { verifyCases( { - def success(v: BigInt) = Expected(Success(v), 35932, costUpcast) + def success(v: BigInt) = Expected(Success(v), 35932, costBigIntUpcast) Seq( (Long.MinValue, success(CBigInt(new BigInteger("-8000000000000000", 16)))), (-1074651039980347209L, success(CBigInt(new BigInteger("-ee9ed6d57885f49", 16)))), @@ -1925,6 +1930,9 @@ class SigmaDslSpecification extends SigmaDslTesting existingFeature((x: Long) => x.toBigInt, "{ (x: Long) => x.toBigInt }", FuncValue(Vector((1, SLong)), Upcast(ValUse(1, SLong), SBigInt)))) + } + + property("Long methods equivalence") { val n = ExactNumeric.LongIsExactNumeric verifyCases( From 47c11385ae472b554988a9269a087c3f3d57dac3 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Mon, 21 Mar 2022 14:55:27 +0100 Subject: [PATCH 03/44] v5.0-add-jit-costing-for-tests: fixed property("NEQ of tuples of numerics") --- .../special/sigma/SigmaDslSpecification.scala | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 8362aef63a..30e70338d2 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -2505,12 +2505,13 @@ class SigmaDslSpecification extends SigmaDslTesting val copied_x = copy(x) val newCost = if (neqCost.isEmpty) CostDetails.ZeroCost else costNEQ(neqCost) def expected(v: Boolean) = Expected(Success(v), cost, newCost) + def expectedNoCost(v: Boolean) = Expected(Success(v), cost, CostDetails.ZeroCost) verifyOp(Seq( - (x, x) -> expected(false), - (x, copied_x) -> expected(false), - (copied_x, x) -> expected(false), - (x, y) -> expected(true), - (y, x) -> expected(true) + (x, y) -> expected(true), // check cost only for this test case, because the trace depends in x and y + (x, x) -> expectedNoCost(false), // and don't check for others + (x, copied_x) -> expectedNoCost(false), + (copied_x, x) -> expectedNoCost(false), + (y, x) -> expectedNoCost(true) ), "!=", NEQ.apply)(_ != _, generateCases) } @@ -2527,14 +2528,16 @@ class SigmaDslSpecification extends SigmaDslTesting val tuplesNeqCost = Array( FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), - FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))) ) property("NEQ of tuples of numerics") { verifyNeq((0.toByte, 1.toByte), (1.toByte, 1.toByte), 36337, tuplesNeqCost)(_.copy()) - // verifyNeq((0.toShort, 1.toByte), (1.toShort, 1.toByte), 36337, tuplesNeqCost)(_.copy()) - // verifyNeq((0, 1.toByte), (1, 1.toByte), 36337, tuplesNeqCost)(_.copy()) - // verifyNeq((0.toLong, 1.toByte), (1.toLong, 1.toByte), 36337, tuplesNeqCost)(_.copy()) - // verifyNeq((0.toBigInt, 1.toByte), (1.toBigInt, 1.toByte), 36337, tuplesNeqCost)(_.copy()) + verifyNeq((0.toShort, 1.toByte), (1.toShort, 1.toByte), 36337, tuplesNeqCost)(_.copy()) + verifyNeq((0, 1.toByte), (1, 1.toByte), 36337, tuplesNeqCost)(_.copy()) + verifyNeq((0.toLong, 1.toByte), (1.toLong, 1.toByte), 36337, tuplesNeqCost)(_.copy()) + verifyNeq((0.toBigInt, 1.toByte), (1.toBigInt, 1.toByte), 36337, Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_BigInt"), FixedCost(JitCost(5))), + ))(_.copy()) } val groupNeqCost = Array( From 516e80e900b1c38e9c577ff6c6acd711b6e6152e Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Mon, 21 Mar 2022 16:13:22 +0100 Subject: [PATCH 04/44] v5.0-add-jit-costing-for-tests: fixed property("NEQ of collections of pre-defined types") --- .../special/sigma/SigmaDslSpecification.scala | 79 ++++++++++++++----- 1 file changed, 59 insertions(+), 20 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 30e70338d2..ca2115f92f 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -2666,33 +2666,60 @@ class SigmaDslSpecification extends SigmaDslTesting // jozi: Cyclic fail again. val collNeqCost1 = Array( FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), - SeqCostItem(NamedDesc("EQ_COA_Byte"), PerItemCost(JitCost(15), JitCost(2), 128), 0) ) property("NEQ of collections of pre-defined types") { implicit val evalSettings = suite.evalSettings.copy(isMeasureOperationTime = false) verifyNeq(Coll[Byte](), Coll(1.toByte), 36337, collNeqCost1)(cloneColl(_)) - verifyNeq(Coll[Byte](0, 1), Coll(1.toByte, 1.toByte), 36337)(cloneColl(_)) + verifyNeq(Coll[Byte](0, 1), Coll(1.toByte, 1.toByte), 36337, + Array( + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + SeqCostItem(NamedDesc("EQ_COA_Byte"), PerItemCost(JitCost(15), JitCost(2), 128), 1)) + )(cloneColl(_)) - verifyNeq(Coll[Short](), Coll(1.toShort), 36337)(cloneColl(_)) - verifyNeq(Coll[Short](0), Coll(1.toShort), 36337)(cloneColl(_)) + verifyNeq(Coll[Short](), Coll(1.toShort), 36337, collNeqCost1)(cloneColl(_)) + verifyNeq(Coll[Short](0), Coll(1.toShort), 36337, + Array( + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + SeqCostItem(NamedDesc("EQ_COA_Short"), PerItemCost(JitCost(15), JitCost(2), 96), 1)) + )(cloneColl(_)) - verifyNeq(Coll[Int](), Coll(1), 36337)(cloneColl(_)) - verifyNeq(Coll[Int](0), Coll(1), 36337)(cloneColl(_)) + verifyNeq(Coll[Int](), Coll(1), 36337, collNeqCost1)(cloneColl(_)) + verifyNeq(Coll[Int](0), Coll(1), 36337, + Array( + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + SeqCostItem(NamedDesc("EQ_COA_Int"), PerItemCost(JitCost(15), JitCost(2), 64), 1)) + )(cloneColl(_)) - verifyNeq(Coll[Long](), Coll(1.toLong), 36337)(cloneColl(_)) - verifyNeq(Coll[Long](0), Coll(1.toLong), 36337)(cloneColl(_)) + verifyNeq(Coll[Long](), Coll(1.toLong), 36337, collNeqCost1)(cloneColl(_)) + verifyNeq(Coll[Long](0), Coll(1.toLong), 36337, + Array( + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + SeqCostItem(NamedDesc("EQ_COA_Long"), PerItemCost(JitCost(15), JitCost(2), 48), 1)) + )(cloneColl(_)) prepareSamples[Coll[BigInt]] - verifyNeq(Coll[BigInt](), Coll(1.toBigInt), 36337)(cloneColl(_)) - verifyNeq(Coll[BigInt](0.toBigInt), Coll(1.toBigInt), 36337)(cloneColl(_)) + verifyNeq(Coll[BigInt](), Coll(1.toBigInt), 36337, collNeqCost1)(cloneColl(_)) + verifyNeq(Coll[BigInt](0.toBigInt), Coll(1.toBigInt), 36337, + Array( + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + SeqCostItem(NamedDesc("EQ_COA_BigInt"), PerItemCost(JitCost(15), JitCost(7), 5), 1)) + )(cloneColl(_)) prepareSamples[Coll[GroupElement]] - verifyNeq(Coll[GroupElement](), Coll(ge1), 36337)(cloneColl(_)) - verifyNeq(Coll[GroupElement](ge1), Coll(ge2), 36337)(cloneColl(_)) + verifyNeq(Coll[GroupElement](), Coll(ge1), 36337, collNeqCost1)(cloneColl(_)) + verifyNeq(Coll[GroupElement](ge1), Coll(ge2), 36337, + Array( + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + SeqCostItem(NamedDesc("EQ_COA_GroupElement"), PerItemCost(JitCost(15), JitCost(5), 1), 1)) + )(cloneColl(_)) prepareSamples[Coll[AvlTree]] - verifyNeq(Coll[AvlTree](), Coll(t1), 36337)(cloneColl(_)) - verifyNeq(Coll[AvlTree](t1), Coll(t2), 36337)(cloneColl(_)) + verifyNeq(Coll[AvlTree](), Coll(t1), 36337, collNeqCost1)(cloneColl(_)) + verifyNeq(Coll[AvlTree](t1), Coll(t2), 36337, + Array( + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + SeqCostItem(NamedDesc("EQ_COA_AvlTree"), PerItemCost(JitCost(15), JitCost(5), 2), 1)) + )(cloneColl(_)) { // since SBox.isConstantSize = false, the cost is different among cases prepareSamples[Coll[AvlTree]] @@ -2703,21 +2730,33 @@ class SigmaDslSpecification extends SigmaDslTesting (x, x) -> Expected(Success(false), 36337), (x, copied_x) -> Expected(Success(false), 36337), (copied_x, x) -> Expected(Success(false), 36337), - (x, y) -> Expected(Success(true), 36377), + (x, y) -> Expected(Success(true), 36377, costNEQ(collNeqCost1)), (y, x) -> Expected(Success(true), 36377) ), "!=", NEQ.apply)(_ != _, generateCases = false) - verifyNeq(Coll[Box](b1), Coll(b2), 36417)(cloneColl(_), generateCases = false) + verifyNeq(Coll[Box](b1), Coll(b2), 36417, + Array( + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + SeqCostItem(NamedDesc("EQ_COA_Box"), PerItemCost(JitCost(15), JitCost(5), 1), 1)) + )(cloneColl(_), generateCases = false) } prepareSamples[Coll[PreHeader]] - verifyNeq(Coll[PreHeader](), Coll(preH1), 36337)(cloneColl(_)) - verifyNeq(Coll[PreHeader](preH1), Coll(preH2), 36337)(cloneColl(_)) + verifyNeq(Coll[PreHeader](), Coll(preH1), 36337, collNeqCost1)(cloneColl(_)) + verifyNeq(Coll[PreHeader](preH1), Coll(preH2), 36337, + Array( + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + SeqCostItem(NamedDesc("EQ_COA_PreHeader"), PerItemCost(JitCost(15), JitCost(3), 1), 1)) + )(cloneColl(_)) prepareSamples[Coll[Header]] - verifyNeq(Coll[Header](), Coll(h1), 36337)(cloneColl(_)) - verifyNeq(Coll[Header](h1), Coll(h2), 36337)(cloneColl(_)) + verifyNeq(Coll[Header](), Coll(h1), 36337, collNeqCost1)(cloneColl(_)) + verifyNeq(Coll[Header](h1), Coll(h2), 36337, + Array( + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + SeqCostItem(NamedDesc("EQ_COA_Header"), PerItemCost(JitCost(15), JitCost(5), 1), 1)) + )(cloneColl(_)) } property("NEQ of nested collections and tuples") { From 9e96cf4f3815b1a8a711c1e4969158b86f3857ee Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Tue, 29 Mar 2022 21:04:37 +0200 Subject: [PATCH 05/44] v5.0-jit-costing: Another bunch of tests uses jit costing --- .../org/ergoplatform/ErgoLikeContext.scala | 10 +- .../main/scala/sigmastate/lang/Terms.scala | 2 +- .../src/main/scala/sigmastate/trees.scala | 10 +- .../scala/sigmastate/utxo/transformers.scala | 14 +- .../special/sigma/SigmaDslSpecification.scala | 3746 ++++++++++++----- .../scala/special/sigma/SigmaDslTesting.scala | 2 +- 6 files changed, 2704 insertions(+), 1080 deletions(-) diff --git a/sigmastate/src/main/scala/org/ergoplatform/ErgoLikeContext.scala b/sigmastate/src/main/scala/org/ergoplatform/ErgoLikeContext.scala index 6f12d8e0f9..8f5f72c931 100644 --- a/sigmastate/src/main/scala/org/ergoplatform/ErgoLikeContext.scala +++ b/sigmastate/src/main/scala/org/ergoplatform/ErgoLikeContext.scala @@ -259,7 +259,7 @@ case object MinerPubkey extends NotReadyValueByteArray with ValueCompanion { } /** When interpreted evaluates to a IntConstant built from Context.currentHeight */ -case object Height extends NotReadyValueInt with ValueCompanion { +case object Height extends NotReadyValueInt with FixedCostValueCompanion { override def companion = this override def opCode: OpCode = OpCodes.HeightCode /** Cost of: 1) Calling Context.HEIGHT Scala method. */ @@ -272,7 +272,7 @@ case object Height extends NotReadyValueInt with ValueCompanion { } /** When interpreted evaluates to a collection of BoxConstant built from Context.boxesToSpend */ -case object Inputs extends LazyCollection[SBox.type] with ValueCompanion { +case object Inputs extends LazyCollection[SBox.type] with FixedCostValueCompanion { override def companion = this override def opCode: OpCode = OpCodes.InputsCode /** Cost of: 1) Calling Context.INPUTS Scala method. */ @@ -286,7 +286,7 @@ case object Inputs extends LazyCollection[SBox.type] with ValueCompanion { } /** When interpreted evaluates to a collection of BoxConstant built from Context.spendingTransaction.outputs */ -case object Outputs extends LazyCollection[SBox.type] with ValueCompanion { +case object Outputs extends LazyCollection[SBox.type] with FixedCostValueCompanion { override def companion = this override def opCode: OpCode = OpCodes.OutputsCode /** Cost of: 1) Calling Context.OUTPUTS Scala method. */ @@ -316,7 +316,7 @@ case object LastBlockUtxoRootHash extends NotReadyValueAvlTree with ValueCompani /** When interpreted evaluates to a BoxConstant built from context.boxesToSpend(context.selfIndex) */ -case object Self extends NotReadyValueBox with ValueCompanion { +case object Self extends NotReadyValueBox with FixedCostValueCompanion { override def companion = this override def opCode: OpCode = OpCodes.SelfCode /** Cost of: 1) Calling Context.SELF Scala method. */ @@ -349,7 +349,7 @@ case object Context extends NotReadyValue[SContext.type] with ValueCompanion { /** When interpreted evaluates to the singleton instance of [[special.sigma.SigmaDslBuilder]]. * Corresponds to `Global` variable in ErgoScript which can be used like `Global.groupGenerator`. */ -case object Global extends NotReadyValue[SGlobal.type] with ValueCompanion { +case object Global extends NotReadyValue[SGlobal.type] with FixedCostValueCompanion { override def companion = this override def opCode: OpCode = OpCodes.GlobalCode /** Cost of: 1) accessing Global instance. */ diff --git a/sigmastate/src/main/scala/sigmastate/lang/Terms.scala b/sigmastate/src/main/scala/sigmastate/lang/Terms.scala index b3d5fb3680..e8a1df33cd 100644 --- a/sigmastate/src/main/scala/sigmastate/lang/Terms.scala +++ b/sigmastate/src/main/scala/sigmastate/lang/Terms.scala @@ -248,7 +248,7 @@ object Terms { } } - object MethodCall extends ValueCompanion { + object MethodCall extends FixedCostValueCompanion { override def opCode: OpCode = OpCodes.MethodCallCode /** Cost of: 1) packing args into Array 2) java.lang.reflect.Method.invoke */ override val costKind = FixedCost(JitCost(4)) diff --git a/sigmastate/src/main/scala/sigmastate/trees.scala b/sigmastate/src/main/scala/sigmastate/trees.scala index 0759f13b52..35f88bd956 100644 --- a/sigmastate/src/main/scala/sigmastate/trees.scala +++ b/sigmastate/src/main/scala/sigmastate/trees.scala @@ -168,7 +168,7 @@ case class BoolToSigmaProp(value: BoolValue) extends SigmaPropValue { SigmaDsl.sigmaProp(v) } } -object BoolToSigmaProp extends ValueCompanion { +object BoolToSigmaProp extends FixedCostValueCompanion { override def opCode: OpCode = OpCodes.BoolToSigmaPropCode override val costKind = FixedCost(JitCost(15)) val OpType = SFunc(SBoolean, SSigmaProp) @@ -186,7 +186,7 @@ case class CreateProveDlog(value: Value[SGroupElement.type]) extends SigmaPropVa SigmaDsl.proveDlog(v) } } -object CreateProveDlog extends ValueCompanion { +object CreateProveDlog extends FixedCostValueCompanion { override def opCode: OpCode = OpCodes.ProveDlogCode override val costKind = FixedCost(JitCost(10)) val OpType = SFunc(SGroupElement, SSigmaProp) @@ -227,7 +227,7 @@ case class CreateProveDHTuple(gv: Value[SGroupElement.type], SigmaDsl.proveDHTuple(g, h, u, v) } } -object CreateProveDHTuple extends ValueCompanion { +object CreateProveDHTuple extends FixedCostValueCompanion { override def opCode: OpCode = OpCodes.ProveDiffieHellmanTupleCode override val costKind = FixedCost(JitCost(20)) } @@ -1019,7 +1019,7 @@ case class Negation[T <: SType](input: Value[T]) extends OneArgumentOperation[T, i.negate(inputV) } } -object Negation extends OneArgumentOperationCompanion { +object Negation extends OneArgumentOperationCompanion with FixedCostValueCompanion { override def opCode: OpCode = OpCodes.NegationCode override val costKind = FixedCost(JitCost(30)) override def argInfos: Seq[ArgInfo] = NegationInfo.argInfos @@ -1498,7 +1498,7 @@ case class If[T <: SType](condition: Value[SBoolean.type], trueBranch: Value[T], } } } -object If extends QuadrupleCompanion { +object If extends QuadrupleCompanion with FixedCostValueCompanion { override def opCode: OpCode = OpCodes.IfCode /** Cost of: conditional switching to the right branch (excluding the cost both * condition itself and the branches) */ diff --git a/sigmastate/src/main/scala/sigmastate/utxo/transformers.scala b/sigmastate/src/main/scala/sigmastate/utxo/transformers.scala index c9022c4b1b..fe6a5cfd9e 100644 --- a/sigmastate/src/main/scala/sigmastate/utxo/transformers.scala +++ b/sigmastate/src/main/scala/sigmastate/utxo/transformers.scala @@ -280,7 +280,7 @@ case class ByIndex[V <: SType](input: Value[SCollection[V]], } } } -object ByIndex extends ValueCompanion { +object ByIndex extends FixedCostValueCompanion { override def opCode: OpCode = OpCodes.ByIndexCode override val costKind = FixedCost(JitCost(30)) } @@ -364,7 +364,7 @@ case class SizeOf[V <: SType](input: Value[SCollection[V]]) inputV.length } } -object SizeOf extends SimpleTransformerCompanion { +object SizeOf extends SimpleTransformerCompanion with FixedCostValueCompanion { val OpType = SFunc(SCollection(SType.tIV), SInt) override def opCode: OpCode = OpCodes.SizeOfCode /** Cost of: 1) calling Coll.length method (guaranteed to be O(1)) @@ -387,7 +387,7 @@ case class ExtractAmount(input: Value[SBox.type]) extends Extract[SLong.type] wi inputV.value } } -object ExtractAmount extends SimpleTransformerCompanion { +object ExtractAmount extends SimpleTransformerCompanion with FixedCostValueCompanion { val OpType = SFunc(SBox, SLong) override def opCode: OpCode = OpCodes.ExtractAmountCode /** Cost of: 1) access `value` property of a [[special.sigma.Box]] */ @@ -408,7 +408,7 @@ case class ExtractScriptBytes(input: Value[SBox.type]) extends Extract[SByteArra inputV.propositionBytes } } -object ExtractScriptBytes extends SimpleTransformerCompanion { +object ExtractScriptBytes extends SimpleTransformerCompanion with FixedCostValueCompanion { val OpType = SFunc(SBox, SByteArray) override def opCode: OpCode = OpCodes.ExtractScriptBytesCode @@ -495,7 +495,7 @@ case class ExtractRegisterAs[V <: SType]( input: Value[SBox.type], inputV.getReg(registerId.number)(tV) } } -object ExtractRegisterAs extends ValueCompanion { +object ExtractRegisterAs extends FixedCostValueCompanion { override def opCode: OpCode = OpCodes.ExtractRegisterAs /** CostOf: 1) accessing `registers` collection 2) comparing types 3) allocating Some()*/ override val costKind = FixedCost(JitCost(50)) @@ -625,7 +625,7 @@ case class OptionGetOrElse[V <: SType](input: Value[SOption[V]], default: Value[ inputV.getOrElse(dV) } } -object OptionGetOrElse extends ValueCompanion { +object OptionGetOrElse extends ValueCompanion with FixedCostValueCompanion { override def opCode: OpCode = OpCodes.OptionGetOrElseCode /** Cost of: 1) Calling Option.getOrElse Scala method. */ override val costKind = FixedCost(JitCost(20)) @@ -643,7 +643,7 @@ case class OptionIsDefined[V <: SType](input: Value[SOption[V]]) inputV.isDefined } } -object OptionIsDefined extends SimpleTransformerCompanion { +object OptionIsDefined extends SimpleTransformerCompanion with FixedCostValueCompanion { override def opCode: OpCode = OpCodes.OptionIsDefinedCode /** Cost of: 1) Calling Option.isDefined Scala method. */ override val costKind = FixedCost(JitCost(10)) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index ca2115f92f..5d586eea09 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -140,6 +140,113 @@ class SigmaDslSpecification extends SigmaDslTesting prepareSamples[(Header, Header)] } + ///===================================================== + /// CostDetails shared among test cases + ///----------------------------------------------------- + val traceBase = Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), + FixedCostItem(ValUse) + ) + def upcastCostDetails(tpe: SType) = TracedCost(traceBase :+ TypeBasedCostItem(Upcast, tpe)) + def downcastCostDetails(tpe: SType) = TracedCost(traceBase :+ TypeBasedCostItem(Downcast, tpe)) + def arithOpsCostDetails(tpe: SType) = CostDetails( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), + FixedCostItem(ValUse), + FixedCostItem(ValUse), + TypeBasedCostItem(ArithOp.Plus, tpe), + FixedCostItem(ValUse), + FixedCostItem(ValUse), + TypeBasedCostItem(ArithOp.Minus, tpe), + FixedCostItem(ValUse), + FixedCostItem(ValUse), + TypeBasedCostItem(ArithOp.Multiply, tpe), + FixedCostItem(ValUse), + FixedCostItem(ValUse), + TypeBasedCostItem(ArithOp.Division, tpe), + FixedCostItem(ValUse), + FixedCostItem(ValUse), + TypeBasedCostItem(ArithOp.Modulo, tpe), + FixedCostItem(Tuple), + FixedCostItem(Tuple), + FixedCostItem(Tuple), + FixedCostItem(Tuple) + ) + ) + + def binaryRelationCostDetails(rel: RelationCompanion, tpe: SType) = CostDetails( + traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + TypeBasedCostItem(rel, tpe) + ) + ) + // rewrite with binaryOpCostDetails + def costLT(tpe: SType) = CostDetails( + traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + TypeBasedCostItem(LT, tpe) + ) + ) + def costGT(tpe: SType) = CostDetails( + traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + TypeBasedCostItem(GT, tpe) + ) + ) + def costLE(tpe: SType) = CostDetails( + traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + TypeBasedCostItem(LE, tpe) + ) + ) + def costGE(tpe: SType) = CostDetails( + traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + TypeBasedCostItem(GE, tpe) + ) + ) + def costNEQ(neqCost: Seq[CostItem]) = CostDetails( + traceBase ++ + Array( + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField) + ) ++ + neqCost + ) + + def methodCostDetails(sMethod: SMethod, methodCost: Int) = TracedCost( + traceBase ++ Array( + FixedCostItem(PropertyCall), + FixedCostItem(sMethod, FixedCost(JitCost(methodCost))) + ) + ) + ///===================================================== /// Boolean type operations ///----------------------------------------------------- @@ -165,26 +272,21 @@ class SigmaDslSpecification extends SigmaDslTesting SelectField.typed[BoolValue](ValUse(1, STuple(Vector(SBoolean, SBoolean))), 2.toByte) ) )) - val trace = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), + val newDetails = CostDetails( + traceBase ++ Array( FixedCostItem(SelectField), FixedCostItem(ValUse), FixedCostItem(SelectField), FixedCostItem(BinXor) ) ) - val newCost = 1788 + val newCost = 1788 + def success(b: Boolean) = Expected(Success(b), 36518, newDetails, newCost) val cases = Seq( - (true, true) -> Expected(Success(false), 36518, trace, newCost), - (true, false) -> Expected(Success(true), 36518, trace, newCost), - (false, false) -> Expected(Success(false), 36518, trace, newCost), - (false, true) -> Expected(Success(true), 36518, trace, newCost) + (true, true) -> success(false), + (true, false) -> success(true), + (false, false) -> success(false), + (false, true) -> success(true) ) verifyCases(cases, binXor) } @@ -197,14 +299,8 @@ class SigmaDslSpecification extends SigmaDslTesting BinXor( SelectField.typed[BoolValue](ValUse(1, STuple(Vector(SBoolean, SBoolean))), 1.toByte), SelectField.typed[BoolValue](ValUse(1, STuple(Vector(SBoolean, SBoolean))), 2.toByte)))) - val expectedCostDetails = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), + val newDetails = CostDetails( + traceBase ++ Array( FixedCostItem(SelectField), FixedCostItem(ValUse), FixedCostItem(SelectField), @@ -214,14 +310,14 @@ class SigmaDslSpecification extends SigmaDslTesting val expectedCost = 36518 val newCost = 1788 val cases = Seq( - (true, true) -> Expected(Success(false), expectedCost, expectedCostDetails, newCost) + (true, true) -> Expected(Success(false), expectedCost, newDetails, newCost) ) verifyCases(cases, feature) val initCost = 100 initialCostInTests.withValue(initCost) { val cases = Seq( - (true, true) -> Expected(Success(false), expectedCost + initCost, expectedCostDetails, newCost + initCost) + (true, true) -> Expected(Success(false), expectedCost + initCost, newDetails, newCost + initCost) ) verifyCases(cases, feature) } @@ -240,15 +336,8 @@ class SigmaDslSpecification extends SigmaDslTesting SelectField.typed[BoolValue](ValUse(1, STuple(Vector(SInt, SBoolean))), 2.toByte) ) )) - val newCost = 36865 - val expectedCostDetails = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), - FixedCostItem(ValUse), + val newDetails = CostDetails( + traceBase ++ Array( FixedCostItem(SelectField), FixedCostItem(Constant), FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), @@ -257,13 +346,14 @@ class SigmaDslSpecification extends SigmaDslTesting FixedCostItem(BinXor) ) ) + def success(b: Boolean) = Expected(Success(b), 36865, newDetails, 1789) val cases = Seq( - (1095564593, true) -> Expected(Success(true), newCost, expectedCostDetails), - (-901834021, true) -> Expected(Success(true), newCost, expectedCostDetails), - (595045530, false) -> Expected(Success(false), newCost, expectedCostDetails), - (-1157998227, false) -> Expected(Success(false), newCost, expectedCostDetails), - (0, true) -> Expected(Success(false), newCost, expectedCostDetails), - (0, false) -> Expected(Success(true), newCost, expectedCostDetails) + (1095564593, true) -> success(true), + (-901834021, true) -> success(true), + (595045530, false) -> success(false), + (-1157998227, false) -> success(false), + (0, true) -> success(false), + (0, false) -> success(true) ) verifyCases(cases, xor) } @@ -278,36 +368,25 @@ class SigmaDslSpecification extends SigmaDslTesting SelectField.typed[BoolValue](ValUse(1, STuple(Vector(SBoolean, SBoolean))), 2.toByte) ) )) - val cost1 = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), + val newDetails1 = CostDetails( + traceBase ++ Array( FixedCostItem(SelectField), FixedCostItem(BinAnd) ) ) - val cost2 = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), + val newDetails2 = CostDetails( + traceBase ++ Array( FixedCostItem(SelectField), FixedCostItem(BinAnd), FixedCostItem(ValUse), FixedCostItem(SelectField) ) ) - val newCost = 38241 val cases = Seq( - (false, true) -> Expected(Success(false), newCost, cost1), - (false, false) -> Expected(Success(false), newCost, cost1), - (true, true) -> Expected(Success(true), newCost, cost2), - (true, false) -> Expected(Success(false), newCost, cost2) + (false, true) -> Expected(Success(false), 38241, newDetails1, 1786), + (false, false) -> Expected(Success(false), 38241, newDetails1, 1786), + (true, true) -> Expected(Success(true), 38241, newDetails2, 1788), + (true, false) -> Expected(Success(false), 38241, newDetails2, 1788) ) verifyCases(cases, eq) } @@ -322,24 +401,14 @@ class SigmaDslSpecification extends SigmaDslTesting SelectField.typed[BoolValue](ValUse(1, STuple(Vector(SBoolean, SBoolean))), 2.toByte) ) )) - val cost1 = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), + val newDetails1 = CostDetails( + traceBase ++ Array( FixedCostItem(SelectField), FixedCostItem(BinOr) ) ) - val cost2 = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), + val newDetails2 = CostDetails( + traceBase ++ Array( FixedCostItem(SelectField), FixedCostItem(BinOr), FixedCostItem(ValUse), @@ -348,58 +417,34 @@ class SigmaDslSpecification extends SigmaDslTesting ) val newCost = 38241 val cases = Seq( - (true, false) -> Expected(Success(true), newCost, cost1), - (true, true) -> Expected(Success(true), newCost, cost1), - (false, false) -> Expected(Success(false), newCost, cost2), - (false, true) -> Expected(Success(true), newCost, cost2) + (true, false) -> Expected(Success(true), newCost, newDetails1, 1786), + (true, true) -> Expected(Success(true), newCost, newDetails1, 1786), + (false, false) -> Expected(Success(false), newCost, newDetails2, 1788), + (false, true) -> Expected(Success(true), newCost, newDetails2, 1788) ) verifyCases(cases, eq) } def runLazy_And_Or_BooleanEquivalence(implicit evalSettings: EvalSettings) = { - val cost1 = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), + val newDetails1 = CostDetails( + traceBase ++ Array( FixedCostItem(BinOr) ) ) - val cost2 = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), + val newDetails2 = CostDetails( + traceBase ++ Array( FixedCostItem(BinAnd) ) ) - val cost3 = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), + val newDetails3 = CostDetails( + traceBase ++ Array( FixedCostItem(BinAnd), FixedCostItem(ValUse), FixedCostItem(BinOr) ) ) - val cost4 = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), + val newDetails4 = CostDetails( + traceBase ++ Array( FixedCostItem(BinAnd), FixedCostItem(ValUse), FixedCostItem(BinAnd), @@ -407,14 +452,8 @@ class SigmaDslSpecification extends SigmaDslTesting FixedCostItem(BinOr) ) ) - val cost5 = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), + val newDetails5 = CostDetails( + traceBase ++ Array( FixedCostItem(BinAnd), FixedCostItem(ValUse), FixedCostItem(BinAnd), @@ -426,7 +465,7 @@ class SigmaDslSpecification extends SigmaDslTesting ) verifyCases( Seq( - (true, Expected(Success(true), 38467, cost1)), + (true, Expected(Success(true), 38467, newDetails1, 1785)), (false, Expected(new ArithmeticException("/ by zero"))) ), existingFeature((x: Boolean) => x || (1 / 0 == 1), @@ -442,7 +481,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( Seq( (true, Expected(new ArithmeticException("/ by zero"))), - (false, Expected(Success(false), 38467, cost2)) + (false, Expected(Success(false), 38467, newDetails2, 1785)) ), existingFeature((x: Boolean) => x && (1 / 0 == 1), "{ (x: Boolean) => x && (1 / 0 == 1) }", @@ -456,8 +495,8 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( Seq( - (false, Expected(Success(false), 40480, cost2)), - (true, Expected(Success(true), 40480, cost3)) + (false, Expected(Success(false), 40480, newDetails2, 1785)), + (true, Expected(Success(true), 40480, newDetails3, 1788)) ), existingFeature((x: Boolean) => x && (x || (1 / 0 == 1)), "{ (x: Boolean) => x && (x || (1 / 0 == 1)) }", @@ -474,8 +513,8 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( Seq( - (false, Expected(Success(false), 42493, cost2)), - (true, Expected(Success(true), 42493, cost4)) + (false, Expected(Success(false), 42493, newDetails2, 1785)), + (true, Expected(Success(true), 42493, newDetails4, 1790)) ), existingFeature((x: Boolean) => x && (x && (x || (1 / 0 == 1))), "{ (x: Boolean) => x && (x && (x || (1 / 0 == 1))) }", @@ -495,8 +534,8 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( Seq( - (false, Expected(Success(false), 44506, cost2)), - (true, Expected(Success(true), 44506, cost5)) + (false, Expected(Success(false), 44506, newDetails2, 1785)), + (true, Expected(Success(true), 44506, newDetails5, 1793)) ), existingFeature((x: Boolean) => x && (x && (x && (x || (1 / 0 == 1)))), "{ (x: Boolean) => x && (x && (x && (x || (1 / 0 == 1)))) }", @@ -517,13 +556,8 @@ class SigmaDslSpecification extends SigmaDslTesting ) ))) - val cost6 = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), + val newDetails6 = CostDetails( + traceBase ++ Array( FixedCostItem(LogicalNot), FixedCostItem(BinAnd), FixedCostItem(LogicalNot), @@ -535,7 +569,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( Seq( (false, Expected(new ArithmeticException("/ by zero"))), - (true, Expected(Success(true), 43281, cost6)) + (true, Expected(Success(true), 43281, newDetails6, 1793)) ), existingFeature((x: Boolean) => !(!x && (1 / 0 == 1)) && (x || (1 / 0 == 1)), "{ (x: Boolean) => !(!x && (1 / 0 == 1)) && (x || (1 / 0 == 1)) }", @@ -555,14 +589,8 @@ class SigmaDslSpecification extends SigmaDslTesting ) ))) - val cost7 = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), + val newDetails7 = CostDetails( + traceBase ++ Array( FixedCostItem(BinOr), FixedCostItem(BinAnd), FixedCostItem(ValUse) @@ -570,7 +598,7 @@ class SigmaDslSpecification extends SigmaDslTesting ) verifyCases( Seq( - (true, Expected(Success(true), 40480, cost7)), + (true, Expected(Success(true), 40480, newDetails7, 1788)), (false, Expected(new ArithmeticException("/ by zero"))) ), existingFeature((x: Boolean) => (x || (1 / 0 == 1)) && x, @@ -586,14 +614,8 @@ class SigmaDslSpecification extends SigmaDslTesting ) ))) - val cost8 = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), + val newDetails8 = CostDetails( + traceBase ++ Array( FixedCostItem(BinOr), FixedCostItem(BinAnd), FixedCostItem(ValUse), @@ -602,7 +624,7 @@ class SigmaDslSpecification extends SigmaDslTesting ) verifyCases( Seq( - (true, Expected(Success(true), 43149, cost8)), + (true, Expected(Success(true), 43149, newDetails8, 1790)), (false, Expected(new ArithmeticException("/ by zero"))) ), existingFeature((x: Boolean) => (x || (1 / 0 == 1)) && (x || (1 / 0 == 1)), @@ -621,14 +643,8 @@ class SigmaDslSpecification extends SigmaDslTesting ) ))) - val cost9 = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), + val newDetails9 = CostDetails( + traceBase ++ Array( FixedCostItem(LogicalNot), FixedCostItem(BinAnd), FixedCostItem(LogicalNot), @@ -640,7 +656,7 @@ class SigmaDslSpecification extends SigmaDslTesting ) verifyCases( Seq( - (true, Expected(Success(true), 45950, cost9)), + (true, Expected(Success(true), 45950, newDetails9, 1795)), (false, Expected(new ArithmeticException("/ by zero"))) ), existingFeature( @@ -665,7 +681,7 @@ class SigmaDslSpecification extends SigmaDslTesting ) ))) - val cost10 = CostDetails( + val newDetails10 = CostDetails( Array( FixedCostItem(Apply), FixedCostItem(FuncValue), @@ -690,7 +706,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( Seq( (false, Expected(new ArithmeticException("/ by zero"))), - (true, Expected(Success(true), 48862, cost10)) + (true, Expected(Success(true), 48862, newDetails10, 1800)) ), existingFeature( (x: Boolean) => (!(!x && (1 / 0 == 1)) || (1 / 0 == 0)) && (!(!x && (1 / 0 == 1)) || (1 / 0 == 1)), @@ -725,73 +741,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("lazy || and && boolean equivalence") { runLazy_And_Or_BooleanEquivalence(evalSettings) - } - - val costIdentity = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse) - ) - ) - - val costShortUpcast = CostDetails(costIdentity.trace :+ TypeBasedCostItem(Upcast, SShort)) - val costIntUpcast = CostDetails(costIdentity.trace :+ TypeBasedCostItem(Upcast, SInt)) - val costLongUpcast = CostDetails(costIdentity.trace :+ TypeBasedCostItem(Upcast, SLong)) - val costBigIntUpcast = CostDetails(costIdentity.trace :+ TypeBasedCostItem(Upcast, SBigInt)) - - def costDowncast(tpe: SType) = TracedCost( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), - FixedCostItem(ValUse), - TypeBasedCostItem(CompanionDesc(Downcast), Downcast.costKind, tpe) - ), - None) - - val costByteDowncast = CostDetails(costIdentity.trace :+ TypeBasedCostItem(Downcast, SByte)) - val costShortDowncast = CostDetails(costIdentity.trace :+ TypeBasedCostItem(Downcast, SShort)) - def costArithOps(tpe: SType) = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), - FixedCostItem(ValUse), - FixedCostItem(SelectField), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), - FixedCostItem(SelectField), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), - FixedCostItem(ValUse), - TypeBasedCostItem(ArithOp.Plus, tpe), - FixedCostItem(ValUse), - FixedCostItem(ValUse), - TypeBasedCostItem(ArithOp.Minus, tpe), - FixedCostItem(ValUse), - FixedCostItem(ValUse), - TypeBasedCostItem(ArithOp.Multiply, tpe), - FixedCostItem(ValUse), - FixedCostItem(ValUse), - TypeBasedCostItem(ArithOp.Division, tpe), - FixedCostItem(ValUse), - FixedCostItem(ValUse), - TypeBasedCostItem(ArithOp.Modulo, tpe), - FixedCostItem(Tuple), - FixedCostItem(Tuple), - FixedCostItem(Tuple), - FixedCostItem(Tuple) - ) - ) + } property("Byte methods equivalence") { SByte.upcast(0.toByte) shouldBe 0.toByte // boundary test case @@ -799,7 +749,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def expect(v: Byte) = Expected(Success(v), 35798, costIdentity) + def expect(v: Byte) = Expected(Success(v), 35798, TracedCost(traceBase), 1783) Seq( (0.toByte, expect(0.toByte)), (1.toByte, expect(1.toByte)), @@ -816,7 +766,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def expected(v: Short) = Expected(Success(v), 35902, costShortUpcast) + def expected(v: Short) = Expected(Success(v), 35902, upcastCostDetails(SShort), 1784) Seq( (0.toByte, expected(0.toShort)), (1.toByte, expected(1.toShort)), @@ -833,7 +783,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def expected(v: Int) = Expected(Success(v), 35902, costIntUpcast) + def expected(v: Int) = Expected(Success(v), 35902, upcastCostDetails(SInt), 1784) Seq( (0.toByte, expected(0)), (1.toByte, expected(1)), @@ -850,7 +800,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def expected(v: Long) = Expected(Success(v), 35902, costLongUpcast) + def expected(v: Long) = Expected(Success(v), 35902, upcastCostDetails(SLong), 1784) Seq( (0.toByte, expected(0L)), (1.toByte, expected(1L)), @@ -867,7 +817,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def expected(v: BigInt) = Expected(Success(v), 35932, costBigIntUpcast) + def expected(v: BigInt) = Expected(Success(v), 35932, upcastCostDetails(SBigInt), 1787) Seq( (0.toByte, expected(CBigInt(new BigInteger("0", 16)))), (1.toByte, expected(CBigInt(new BigInteger("1", 16)))), @@ -885,7 +835,7 @@ class SigmaDslSpecification extends SigmaDslTesting val n = ExactIntegral.ByteIsExactIntegral verifyCases( { - def success[T](v: (T, (T, (T, (T, T))))) = Expected(Success(v), 39654, costArithOps(SByte)) + def success[T](v: (T, (T, (T, (T, T))))) = Expected(Success(v), 39654, arithOpsCostDetails(SByte), 1808) Seq( ((-128.toByte, -128.toByte), Expected(new ArithmeticException("Byte overflow"))), ((-128.toByte, 0.toByte), Expected(new ArithmeticException("/ by zero"))), @@ -979,20 +929,31 @@ class SigmaDslSpecification extends SigmaDslTesting )) } - def swapArgs[A](cases: Seq[((A, A), Expected[Boolean])], cost: Int, newCost: CostDetails) = + def swapArgs1[A]( + cases: Seq[((A, A), Expected[Boolean])], + cost: Int, + newCostDetails: CostDetails + ) = cases.map { case ((x, y), res) => - ((y, x), Expected(res.value, cost, newCost)) + ((y, x), Expected(res.value, cost, newCostDetails, 1788)) } - def newCasesFrom[A, R](cases: Seq[(A, A)])(getExpectedRes: (A, A) => R, cost: Int) = + def newCasesFrom[A, R]( + cases: Seq[(A, A)] + )( + getExpectedRes: (A, A) => R, + cost: Int, + newDetails: CostDetails, + newCost: Int + ) = cases.map { case (x, y) => - ((x, y), Expected(Success(getExpectedRes(x, y)), cost = cost)) + ((x, y), Expected(Success(getExpectedRes(x, y)), cost = cost, newDetails, newCost)) } def newCasesFrom2[A, R](cases: Seq[(A, A)]) - (getExpectedRes: (A, A) => R, cost: Int, newCost: CostDetails) = + (getExpectedRes: (A, A) => R, cost: Int, newCostDetails: CostDetails) = cases.map { case (x, y) => - ((x, y), Expected(Success(getExpectedRes(x, y)), cost = cost, expectedDetails = newCost)) + ((x, y), Expected(Success(getExpectedRes(x, y)), cost = cost, expectedDetails = newCostDetails, expectedNewCost = 1786)) } def verifyOp[A: Ordering: Arbitrary] @@ -1021,84 +982,11 @@ class SigmaDslSpecification extends SigmaDslTesting preGeneratedSamples = Some(sampled.samples)) } - def costLT(tpe: SType) = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), - FixedCostItem(SelectField), - FixedCostItem(ValUse), - FixedCostItem(SelectField), - TypeBasedCostItem(LT, tpe) - ) - ) - def costGT(tpe: SType) = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), - FixedCostItem(SelectField), - FixedCostItem(ValUse), - FixedCostItem(SelectField), - TypeBasedCostItem(GT, tpe) - ) - ) - def costNEQ(neqCost: Seq[CostItem]) = { - val trace = Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), - FixedCostItem(SelectField), - FixedCostItem(ValUse), - FixedCostItem(SelectField) - ) ++ neqCost - CostDetails(trace) - } - - // jozi: see where it is used and make more compact - val constNeqCost: Seq[CostItem] = Array[CostItem](FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3)))) - - def costLE(tpe: SType) = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), - FixedCostItem(SelectField), - FixedCostItem(ValUse), - FixedCostItem(SelectField), - TypeBasedCostItem(LE, tpe) - ) - ) - def costGE(tpe: SType) = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), - FixedCostItem(SelectField), - FixedCostItem(ValUse), - FixedCostItem(SelectField), - TypeBasedCostItem(GE, tpe) - ) - ) + val constNeqCost: Seq[CostItem] = Array[CostItem](FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3)))) property("Byte LT, GT, NEQ") { val o = ExactOrdering.ByteIsExactOrdering - def expect(v: Boolean) = Expected(Success(v), 36328, costLT(SByte)) + def expect(v: Boolean) = Expected(Success(v), 36328, costLT(SByte), 1788) val LT_cases: Seq[((Byte, Byte), Expected[Boolean])] = Seq( (-128.toByte, -128.toByte) -> expect(false), (-128.toByte, -127.toByte) -> expect(true), @@ -1139,16 +1027,16 @@ class SigmaDslSpecification extends SigmaDslTesting verifyOp(LT_cases, "<", LT.apply)(_ < _) verifyOp( - swapArgs(LT_cases, cost = 36342, newCost = costGT(SByte)), + swapArgs1(LT_cases, cost = 36342, newCostDetails = costGT(SByte)), ">", GT.apply)(_ > _) - val neqCases = newCasesFrom2(LT_cases.map(_._1))(_ != _, cost = 36337, newCost = costNEQ(constNeqCost)) + val neqCases = newCasesFrom2(LT_cases.map(_._1))(_ != _, cost = 36337, newCostDetails = costNEQ(constNeqCost)) verifyOp(neqCases, "!=", NEQ.apply)(_ != _) } property("Byte LE, GE") { val o = ExactOrdering.ByteIsExactOrdering - def expect(v: Boolean) = Expected(Success(v), 36337, costLE(SByte)) + def expect(v: Boolean) = Expected(Success(v), 36337, costLE(SByte), 1788) val LE_cases: Seq[((Byte, Byte), Expected[Boolean])] = Seq( (-128.toByte, -128.toByte) -> expect(true), (-128.toByte, -127.toByte) -> expect(true), @@ -1190,7 +1078,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyOp(LE_cases, "<=", LE.apply)(_ <= _) verifyOp( - swapArgs(LE_cases, cost = 36336, newCost = costGE(SByte)), + swapArgs1(LE_cases, cost = 36336, newCostDetails = costGE(SByte)), ">=", GE.apply)(_ >= _) } @@ -1225,7 +1113,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 35976, costByteDowncast) + def success[T](v: T) = Expected(Success(v), 35976, downcastCostDetails(SByte), 1784) Seq( (Short.MinValue, Expected(new ArithmeticException("Byte overflow"))), (-21626.toShort, Expected(new ArithmeticException("Byte overflow"))), @@ -1244,7 +1132,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 35798, costIdentity) + def success[T](v: T) = Expected(Success(v), 35798, TracedCost(traceBase), 1783) Seq( (-32768.toShort, success(-32768.toShort)), (-27798.toShort, success(-27798.toShort)), @@ -1261,7 +1149,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 35902, costIntUpcast) + def success[T](v: T) = Expected(Success(v), 35902, upcastCostDetails(SInt), 1784) Seq( (-32768.toShort, success(-32768)), (-21064.toShort, success(-21064)), @@ -1278,7 +1166,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 35902, costLongUpcast) + def success[T](v: T) = Expected(Success(v), 35902, upcastCostDetails(SLong), 1784) Seq( (-32768.toShort, success(-32768L)), (-23408.toShort, success(-23408L)), @@ -1295,7 +1183,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success(v: BigInt) = Expected(Success(v), 35932, costBigIntUpcast) + def success(v: BigInt) = Expected(Success(v), 35932, upcastCostDetails(SBigInt), 1787) Seq( (-32768.toShort, success(CBigInt(new BigInteger("-8000", 16)))), (-26248.toShort, success(CBigInt(new BigInteger("-6688", 16)))), @@ -1313,7 +1201,7 @@ class SigmaDslSpecification extends SigmaDslTesting val n = ExactIntegral.ShortIsExactIntegral verifyCases( { - def success[T](v: T) = Expected(Success(v), 39654, costArithOps(SShort)) + def success[T](v: T) = Expected(Success(v), 39654, arithOpsCostDetails(SShort), 1808) Seq( ((-32768.toShort, 1.toShort), Expected(new ArithmeticException("Short overflow"))), ((-32768.toShort, 4006.toShort), Expected(new ArithmeticException("Short overflow"))), @@ -1405,7 +1293,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("Short LT, GT, NEQ") { val o = ExactOrdering.ShortIsExactOrdering - def expect(v: Boolean) = Expected(Success(v), 36328, costLT(SShort)) + def expect(v: Boolean) = Expected(Success(v), 36328, costLT(SShort), 1788) val LT_cases: Seq[((Short, Short), Expected[Boolean])] = Seq( (Short.MinValue, Short.MinValue) -> expect(false), (Short.MinValue, (Short.MinValue + 1).toShort) -> expect(true), @@ -1445,15 +1333,15 @@ class SigmaDslSpecification extends SigmaDslTesting verifyOp(LT_cases, "<", LT.apply)(_ < _) - verifyOp(swapArgs(LT_cases, cost = 36342, costGT(SShort)), ">", GT.apply)(_ > _) + verifyOp(swapArgs1(LT_cases, cost = 36342, newCostDetails = costGT(SShort)), ">", GT.apply)(_ > _) - val neqCases = newCasesFrom2(LT_cases.map(_._1))(_ != _, cost = 36337, newCost = costNEQ(constNeqCost)) + val neqCases = newCasesFrom2(LT_cases.map(_._1))(_ != _, cost = 36337, newCostDetails = costNEQ(constNeqCost)) verifyOp(neqCases, "!=", NEQ.apply)(_ != _) } property("Short LE, GE") { val o = ExactOrdering.ShortIsExactOrdering - def expect(v: Boolean) = Expected(Success(v), 36337, costLE(SShort)) + def expect(v: Boolean) = Expected(Success(v), 36337, costLE(SShort), 1788) val LE_cases: Seq[((Short, Short), Expected[Boolean])] = Seq( (Short.MinValue, Short.MinValue) -> expect(true), (Short.MinValue, (Short.MinValue + 1).toShort) -> expect(true), @@ -1495,7 +1383,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyOp(LE_cases, "<=", LE.apply)(_ <= _) verifyOp( - swapArgs(LE_cases, cost = 36336, newCost = costGE(SShort)), + swapArgs1(LE_cases, cost = 36336, newCostDetails = costGE(SShort)), ">=", GE.apply)(_ >= _) } @@ -1529,7 +1417,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 35976, costByteDowncast) + def success[T](v: T) = Expected(Success(v), 35976, downcastCostDetails(SByte), 1784) Seq( (Int.MinValue, Expected(new ArithmeticException("Byte overflow"))), (-2014394379, Expected(new ArithmeticException("Byte overflow"))), @@ -1548,7 +1436,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 35976, costShortDowncast) + def success[T](v: T) = Expected(Success(v), 35976, downcastCostDetails(SShort), 1784) Seq( (Int.MinValue, Expected(new ArithmeticException("Short overflow"))), (Short.MinValue - 1, Expected(new ArithmeticException("Short overflow"))), @@ -1567,7 +1455,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 35798, costIdentity) + def success[T](v: T) = Expected(Success(v), 35798, TracedCost(traceBase), 1783) Seq( (Int.MinValue, success(Int.MinValue)), (-1, success(-1)), @@ -1582,7 +1470,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 35902, costLongUpcast) + def success[T](v: T) = Expected(Success(v), 35902, upcastCostDetails(SLong), 1784) Seq( (Int.MinValue, success(Int.MinValue.toLong)), (-1, success(-1L)), @@ -1597,7 +1485,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success(v: BigInt) = Expected(Success(v), 35932, costBigIntUpcast) + def success(v: BigInt) = Expected(Success(v), 35932, upcastCostDetails(SBigInt), 1787) Seq( (Int.MinValue, success(CBigInt(new BigInteger("-80000000", 16)))), (-1937187314, success(CBigInt(new BigInteger("-737721f2", 16)))), @@ -1615,7 +1503,7 @@ class SigmaDslSpecification extends SigmaDslTesting val n = ExactNumeric.IntIsExactNumeric verifyCases( { - def success[T](v: T) = Expected(Success(v), 39654, costArithOps(SInt)) + def success[T](v: T) = Expected(Success(v), 39654, arithOpsCostDetails(SInt), 1808) Seq( ((Int.MinValue, 449583993), Expected(new ArithmeticException("integer overflow"))), ((-1589633733, 2147483647), Expected(new ArithmeticException("integer overflow"))), @@ -1707,7 +1595,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("Int LT, GT, NEQ") { val o = ExactOrdering.IntIsExactOrdering - def expect(v: Boolean) = Expected(Success(v), 36328, costLT(SInt)) + def expect(v: Boolean) = Expected(Success(v), 36328, costLT(SInt), 1788) val LT_cases: Seq[((Int, Int), Expected[Boolean])] = Seq( (Int.MinValue, Int.MinValue) -> expect(false), (Int.MinValue, (Int.MinValue + 1).toInt) -> expect(true), @@ -1748,16 +1636,16 @@ class SigmaDslSpecification extends SigmaDslTesting verifyOp(LT_cases, "<", LT.apply)(_ < _) verifyOp( - swapArgs(LT_cases, cost = 36342, newCost = costGT(SInt)), + swapArgs1(LT_cases, cost = 36342, newCostDetails = costGT(SInt)), ">", GT.apply)(_ > _) - val neqCases = newCasesFrom2(LT_cases.map(_._1))(_ != _, cost = 36337, newCost = costNEQ(constNeqCost)) + val neqCases = newCasesFrom2(LT_cases.map(_._1))(_ != _, cost = 36337, newCostDetails = costNEQ(constNeqCost)) verifyOp(neqCases, "!=", NEQ.apply)(_ != _) } property("Int LE, GE") { val o = ExactOrdering.IntIsExactOrdering - def expect(v: Boolean) = Expected(Success(v), 36337, costLE(SInt)) + def expect(v: Boolean) = Expected(Success(v), 36337, costLE(SInt), 1788) val LE_cases: Seq[((Int, Int), Expected[Boolean])] = Seq( (Int.MinValue, Int.MinValue) -> expect(true), (Int.MinValue, (Int.MinValue + 1).toInt) -> expect(true), @@ -1799,7 +1687,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyOp(LE_cases, "<=", LE.apply)(_ <= _) verifyOp( - swapArgs(LE_cases, cost = 36336, newCost = costGE(SInt)), + swapArgs1(LE_cases, cost = 36336, newCostDetails = costGE(SInt)), ">=", GE.apply)(_ >= _) } @@ -1836,7 +1724,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("Long.toByte method") { verifyCases( { - def success[T](v: T) = Expected(Success(v), 35976, costDowncast(SByte)) + def success[T](v: T) = Expected(Success(v), 35976, downcastCostDetails(SByte), 1784) Seq( (Long.MinValue, Expected(new ArithmeticException("Byte overflow"))), (Byte.MinValue.toLong - 1, Expected(new ArithmeticException("Byte overflow"))), @@ -1857,7 +1745,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("Long.toShort method") { verifyCases( { - def success[T](v: T) = Expected(Success(v), 35976, costDowncast(SShort)) + def success[T](v: T) = Expected(Success(v), 35976, downcastCostDetails(SShort), 1784) Seq( (Long.MinValue, Expected(new ArithmeticException("Short overflow"))), (Short.MinValue.toLong - 1, Expected(new ArithmeticException("Short overflow"))), @@ -1878,7 +1766,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("Long.toInt method") { verifyCases( { - def success[T](v: T) = Expected(Success(v), 35976, costDowncast(SInt)) + def success[T](v: T) = Expected(Success(v), 35976, downcastCostDetails(SInt), 1784) Seq( (Long.MinValue, Expected(new ArithmeticException("Int overflow"))), (Int.MinValue.toLong - 1, Expected(new ArithmeticException("Int overflow"))), @@ -1899,7 +1787,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("Long.toLong method") { verifyCases( { - def success[T](v: T) = Expected(Success(v), 35798, costIdentity) + def success[T](v: T) = Expected(Success(v), 35798, TracedCost(traceBase), 1783) Seq( (Long.MinValue, success(Long.MinValue)), (-1L, success(-1L)), @@ -1916,7 +1804,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("Long.toBigInt method") { verifyCases( { - def success(v: BigInt) = Expected(Success(v), 35932, costBigIntUpcast) + def success(v: BigInt) = Expected(Success(v), 35932, upcastCostDetails(SBigInt), 1787) Seq( (Long.MinValue, success(CBigInt(new BigInteger("-8000000000000000", 16)))), (-1074651039980347209L, success(CBigInt(new BigInteger("-ee9ed6d57885f49", 16)))), @@ -1937,7 +1825,7 @@ class SigmaDslSpecification extends SigmaDslTesting val n = ExactNumeric.LongIsExactNumeric verifyCases( { - def success[T](v: T) = Expected(Success(v), 39654, costArithOps(SLong)) + def success[T](v: T) = Expected(Success(v), 39654, arithOpsCostDetails(SLong), 1808) Seq( ((Long.MinValue, -4677100190307931395L), Expected(new ArithmeticException("long overflow"))), ((Long.MinValue, -1L), Expected(new ArithmeticException("long overflow"))), @@ -2027,7 +1915,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("Long LT, GT, NEQ") { val o = ExactOrdering.LongIsExactOrdering - def expect(v: Boolean) = Expected(Success(v), 36328, costLT(SLong)) + def expect(v: Boolean) = Expected(Success(v), 36328, costLT(SLong), 1788) val LT_cases: Seq[((Long, Long), Expected[Boolean])] = Seq( (Long.MinValue, Long.MinValue) -> expect(false), (Long.MinValue, (Long.MinValue + 1).toLong) -> expect(true), @@ -2068,16 +1956,16 @@ class SigmaDslSpecification extends SigmaDslTesting verifyOp(LT_cases, "<", LT.apply)(_ < _) verifyOp( - swapArgs(LT_cases, cost = 36342, newCost = costGT(SLong)), + swapArgs1(LT_cases, cost = 36342, newCostDetails = costGT(SLong)), ">", GT.apply)(_ > _) - val neqCases = newCasesFrom2(LT_cases.map(_._1))(_ != _, cost = 36337, newCost = costNEQ(constNeqCost)) + val neqCases = newCasesFrom2(LT_cases.map(_._1))(_ != _, cost = 36337, newCostDetails = costNEQ(constNeqCost)) verifyOp(neqCases, "!=", NEQ.apply)(_ != _) } property("Long LE, GE") { val o = ExactOrdering.LongIsExactOrdering - def expect(v: Boolean) = Expected(Success(v), 36337, costLE(SLong)) + def expect(v: Boolean) = Expected(Success(v), 36337, costLE(SLong), 1788) val LE_cases: Seq[((Long, Long), Expected[Boolean])] = Seq( (Long.MinValue, Long.MinValue) -> expect(true), (Long.MinValue, (Long.MinValue + 1).toLong) -> expect(true), @@ -2119,7 +2007,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyOp(LE_cases, "<=", LE.apply)(_ <= _) verifyOp( - swapArgs(LE_cases, cost = 36336, newCost = costGE(SLong)), + swapArgs1(LE_cases, cost = 36336, newCostDetails = costGE(SLong)), ">=", GE.apply)(_ >= _) } @@ -2150,7 +2038,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("BigInt methods equivalence") { verifyCases( { - def success(v: BigInt) = Expected(Success(v), 35798, costIdentity) + def success(v: BigInt) = Expected(Success(v), 35798, TracedCost(traceBase), 1784) Seq( (CBigInt(new BigInteger("-85102d7f884ca0e8f56193b46133acaf7e4681e1757d03f191ae4f445c8e0", 16)), success( CBigInt(new BigInteger("-85102d7f884ca0e8f56193b46133acaf7e4681e1757d03f191ae4f445c8e0", 16)) @@ -2172,43 +2060,8 @@ class SigmaDslSpecification extends SigmaDslTesting val n = NumericOps.BigIntIsExactIntegral verifyCases( { - val costArithOps = CostDetails( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), - FixedCostItem(ValUse), - FixedCostItem(SelectField), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), - FixedCostItem(SelectField), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), - FixedCostItem(ValUse), - TypeBasedCostItem(ArithOp.Plus, SBigInt), - FixedCostItem(ValUse), - FixedCostItem(ValUse), - TypeBasedCostItem(ArithOp.Minus, SBigInt), - FixedCostItem(ValUse), - FixedCostItem(ValUse), - TypeBasedCostItem(ArithOp.Multiply, SBigInt), - FixedCostItem(ValUse), - FixedCostItem(ValUse), - TypeBasedCostItem(ArithOp.Division, SBigInt), - FixedCostItem(ValUse), - FixedCostItem(ValUse), - TypeBasedCostItem(ArithOp.Modulo, SBigInt), - FixedCostItem(Tuple), - FixedCostItem(Tuple), - FixedCostItem(Tuple), - FixedCostItem(Tuple) - ) - ) def success(v: (BigInt, (BigInt, (BigInt, (BigInt, BigInt))))) = - Expected(Success(v), 39774, costArithOps) + Expected(Success(v), 39774, arithOpsCostDetails(SBigInt), 1813) Seq( ((CBigInt(new BigInteger("-8683d1cd99d5fcf0e6eff6295c285c36526190e13dbde008c49e5ae6fddc1c", 16)), CBigInt(new BigInteger("-2ef55db3f245feddacf0182e299dd", 16))), @@ -2332,7 +2185,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("BigInt LT, GT, NEQ") { val o = NumericOps.BigIntIsExactOrdering - def expect(v: Boolean) = Expected(Success(v), 36328, costLT(SBigInt)) + def expect(v: Boolean) = Expected(Success(v), 36328, costLT(SBigInt), 1788) val LT_cases: Seq[((BigInt, BigInt), Expected[Boolean])] = Seq( (BigIntMinValue, BigIntMinValue) -> expect(false), @@ -2376,11 +2229,11 @@ class SigmaDslSpecification extends SigmaDslTesting verifyOp(LT_cases, "<", LT.apply)(o.lt(_, _)) verifyOp( - swapArgs(LT_cases, cost = 36342, newCost = costGT(SBigInt)), + swapArgs1(LT_cases, cost = 36342, newCostDetails = costGT(SBigInt)), ">", GT.apply)(o.gt(_, _)) val constBigIntCost = Array[CostItem](FixedCostItem(NamedDesc("EQ_BigInt"), FixedCost(JitCost(5)))) - val neqCases = newCasesFrom2(LT_cases.map(_._1))(_ != _, cost = 36337, newCost = costNEQ(constBigIntCost)) + val neqCases = newCasesFrom2(LT_cases.map(_._1))(_ != _, cost = 36337, newCostDetails = costNEQ(constBigIntCost)) verifyOp(neqCases, "!=", NEQ.apply)(_ != _) } @@ -2391,7 +2244,7 @@ class SigmaDslSpecification extends SigmaDslTesting val BigIntMaxValue = CBigInt(new BigInteger("7F" + "ff" * 31, 16)) val BigIntOverlimit = CBigInt(new BigInteger("7F" + "ff" * 33, 16)) - def expect(v: Boolean) = Expected(Success(v), 36337, costLE(SBigInt)) + def expect(v: Boolean) = Expected(Success(v), 36337, costLE(SBigInt), 1788) val LE_cases: Seq[((BigInt, BigInt), Expected[Boolean])] = Seq( (BigIntMinValue, BigIntMinValue) -> expect(true), @@ -2436,7 +2289,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyOp(LE_cases, "<=", LE.apply)(o.lteq(_, _)) verifyOp( - swapArgs(LE_cases, cost = 36336, newCost = costGE(SBigInt)), + swapArgs1(LE_cases, cost = 36336, newCostDetails = costGE(SBigInt)), ">=", GE.apply)(o.gteq(_, _)) } @@ -2499,12 +2352,12 @@ class SigmaDslSpecification extends SigmaDslTesting * @param cost the expected cost of `verify` (the same for all cases) */ def verifyNeq[A: Ordering: Arbitrary: RType] - (x: A, y: A, cost: Int, neqCost: Seq[CostItem] = mutable.WrappedArray.empty) + (x: A, y: A, cost: Int, neqCost: Seq[CostItem] = mutable.WrappedArray.empty, newCost: Int) (copy: A => A, generateCases: Boolean = true) (implicit sampled: Sampled[(A, A)], evalSettings: EvalSettings) = { val copied_x = copy(x) - val newCost = if (neqCost.isEmpty) CostDetails.ZeroCost else costNEQ(neqCost) - def expected(v: Boolean) = Expected(Success(v), cost, newCost) + val newCostDetails = if (neqCost.isEmpty) CostDetails.ZeroCost else costNEQ(neqCost) + def expected(v: Boolean) = Expected(Success(v), cost, newCostDetails, newCost) def expectedNoCost(v: Boolean) = Expected(Success(v), cost, CostDetails.ZeroCost) verifyOp(Seq( (x, y) -> expected(true), // check cost only for this test case, because the trace depends in x and y @@ -2517,208 +2370,218 @@ class SigmaDslSpecification extends SigmaDslTesting } property("NEQ of pre-defined types") { - verifyNeq(ge1, ge2, 36337, Array[CostItem](FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172)))))(_.asInstanceOf[CGroupElement].copy()) - verifyNeq(t1, t2, 36337, Array[CostItem](FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6)))))(_.asInstanceOf[CAvlTree].copy()) - verifyNeq(b1, b2, 36417, Array[CostItem]())(_.asInstanceOf[CostingBox].copy()) - verifyNeq(preH1, preH2, 36337, Array[CostItem](FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4)))))(_.asInstanceOf[CPreHeader].copy()) - verifyNeq(h1, h2, 36337, Array[CostItem](FixedCostItem(NamedDesc("EQ_Header"), FixedCost(JitCost(6)))))(_.asInstanceOf[CHeader].copy()) + verifyNeq(ge1, ge2, 36337, Array[CostItem](FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172)))), 1803)(_.asInstanceOf[CGroupElement].copy()) + verifyNeq(t1, t2, 36337, Array[CostItem](FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6)))), 1787)(_.asInstanceOf[CAvlTree].copy()) + verifyNeq(b1, b2, 36417, Array[CostItem](), 1787)(_.asInstanceOf[CostingBox].copy()) + verifyNeq(preH1, preH2, 36337, Array[CostItem](FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4)))), 1786)(_.asInstanceOf[CPreHeader].copy()) + verifyNeq(h1, h2, 36337, Array[CostItem](FixedCostItem(NamedDesc("EQ_Header"), FixedCost(JitCost(6)))), 1787)(_.asInstanceOf[CHeader].copy()) } - // jozi: Test fails, last item is(n't) part of the output - val tuplesNeqCost = Array( - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), - ) property("NEQ of tuples of numerics") { - verifyNeq((0.toByte, 1.toByte), (1.toByte, 1.toByte), 36337, tuplesNeqCost)(_.copy()) - verifyNeq((0.toShort, 1.toByte), (1.toShort, 1.toByte), 36337, tuplesNeqCost)(_.copy()) - verifyNeq((0, 1.toByte), (1, 1.toByte), 36337, tuplesNeqCost)(_.copy()) - verifyNeq((0.toLong, 1.toByte), (1.toLong, 1.toByte), 36337, tuplesNeqCost)(_.copy()) + val tuplesNeqCost = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + ) + verifyNeq((0.toByte, 1.toByte), (1.toByte, 1.toByte), 36337, tuplesNeqCost, 1787)(_.copy()) + verifyNeq((0.toShort, 1.toByte), (1.toShort, 1.toByte), 36337, tuplesNeqCost, 1787)(_.copy()) + verifyNeq((0, 1.toByte), (1, 1.toByte), 36337, tuplesNeqCost, 1787)(_.copy()) + verifyNeq((0.toLong, 1.toByte), (1.toLong, 1.toByte), 36337, tuplesNeqCost, 1787)(_.copy()) verifyNeq((0.toBigInt, 1.toByte), (1.toBigInt, 1.toByte), 36337, Array( FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), FixedCostItem(NamedDesc("EQ_BigInt"), FixedCost(JitCost(5))), - ))(_.copy()) + ), 1787)(_.copy()) } - val groupNeqCost = Array( - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), - FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))) - ) - val treeNeqCost = Array( - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), - FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))) - ) - val preHeaderNeqCost = Array( - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))) - ) - val headerNeqCost = Array( - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_Header"), FixedCost(JitCost(6))), - FixedCostItem(NamedDesc("EQ_Header"), FixedCost(JitCost(6))) - ) - property("NEQ of tuples of pre-defined types") { - verifyNeq((ge1, ge1), (ge1, ge2), 36337, groupNeqCost)(_.copy()) - verifyNeq((t1, t1), (t1, t2), 36337, treeNeqCost)(_.copy()) - verifyNeq((b1, b1), (b1, b2), - cost = 36497, - neqCost = Array[CostItem]() - )(_.copy()) - verifyNeq((preH1, preH1), (preH1, preH2), 36337, preHeaderNeqCost)(_.copy()) - verifyNeq((h1, h1), (h1, h2), 36337, headerNeqCost)(_.copy()) - } - - val nestedTuplesNeqCost1 = Array( - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), - FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))) - ) - val nestedTuplesNeqCost2 = Array( - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))), - FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))) - ) - val nestedTuplesNeqCost3 = Array( - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))), - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))) - ) - val nestedTuplesNeqCost4 = Array( - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))), - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_Header"), FixedCost(JitCost(6))), - FixedCostItem(NamedDesc("EQ_Header"), FixedCost(JitCost(6))) - ) - val nestedTuplesNeqCost5 = Array( - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), - FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), - FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))) - ) - val nestedTuplesNeqCost6 = Array( - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), - FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), - FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))), - FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))) - ) - val nestedTuplesNeqCost7 = Array( - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), - FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), - FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))), - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))) - ) - val nestedTuplesNeqCost8 = Array( - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), - FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), - FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))), - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_Header"), FixedCost(JitCost(6))), - FixedCostItem(NamedDesc("EQ_Header"), FixedCost(JitCost(6))) - ) + val groupNeqCost = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), + FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))) + ) + verifyNeq((ge1, ge1), (ge1, ge2), 36337, groupNeqCost, 1821)(_.copy()) + + val treeNeqCost = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))) + ) + verifyNeq((t1, t1), (t1, t2), 36337, treeNeqCost, 1788)(_.copy()) + + verifyNeq((b1, b1), (b1, b2), 36497, Array[CostItem](), 1788)(_.copy()) + + val preHeaderNeqCost = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))) + ) + verifyNeq((preH1, preH1), (preH1, preH2), 36337, preHeaderNeqCost, 1787)(_.copy()) + + val headerNeqCost = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Header"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Header"), FixedCost(JitCost(6))) + ) + verifyNeq((h1, h1), (h1, h2), 36337, headerNeqCost, 1788)(_.copy()) + } + property("NEQ of nested tuples") { - verifyNeq((ge1, (t1, t1)), (ge1, (t1, t2)), 36337, nestedTuplesNeqCost1)(_.copy()) - verifyNeq((ge1, (t1, (b1, b1))), (ge1, (t1, (b1, b2))), 36497, nestedTuplesNeqCost2)(_.copy()) - verifyNeq((ge1, (t1, (b1, (preH1, preH1)))), (ge1, (t1, (b1, (preH1, preH2)))), 36417, nestedTuplesNeqCost3)(_.copy()) - verifyNeq((ge1, (t1, (b1, (preH1, (h1, h1))))), (ge1, (t1, (b1, (preH1, (h1, h2))))), 36427, nestedTuplesNeqCost4)(_.copy()) - - verifyNeq(((ge1, t1), t1), ((ge1, t1), t2), 36337, nestedTuplesNeqCost5)(_.copy()) - verifyNeq((((ge1, t1), b1), b1), (((ge1, t1), b1), b2), 36497, nestedTuplesNeqCost6)(_.copy()) - verifyNeq((((ge1, t1), b1), (preH1, preH1)), (((ge1, t1), b1), (preH1, preH2)), 36417, nestedTuplesNeqCost7)(_.copy()) - verifyNeq((((ge1, t1), b1), (preH1, (h1, h1))), (((ge1, t1), b1), (preH1, (h1, h2))), 36427, nestedTuplesNeqCost8)(_.copy()) - } - // jozi: Cyclic fail again. - val collNeqCost1 = Array( - FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), - ) + val nestedTuplesNeqCost1 = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))) + ) + val nestedTuplesNeqCost2 = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))) + ) + val nestedTuplesNeqCost3 = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))) + ) + val nestedTuplesNeqCost4 = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Header"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Header"), FixedCost(JitCost(6))) + ) + val nestedTuplesNeqCost5 = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))) + ) + val nestedTuplesNeqCost6 = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))) + ) + val nestedTuplesNeqCost7 = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))) + ) + val nestedTuplesNeqCost8 = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Box"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Header"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("EQ_Header"), FixedCost(JitCost(6))) + ) + verifyNeq((ge1, (t1, t1)), (ge1, (t1, t2)), 36337, nestedTuplesNeqCost1, 1805)(_.copy()) + verifyNeq((ge1, (t1, (b1, b1))), (ge1, (t1, (b1, b2))), 36497, nestedTuplesNeqCost2, 1806)(_.copy()) + verifyNeq((ge1, (t1, (b1, (preH1, preH1)))), (ge1, (t1, (b1, (preH1, preH2)))), 36417, nestedTuplesNeqCost3, 1807)(_.copy()) + verifyNeq((ge1, (t1, (b1, (preH1, (h1, h1))))), (ge1, (t1, (b1, (preH1, (h1, h2))))), 36427, nestedTuplesNeqCost4, 1808)(_.copy()) + + verifyNeq(((ge1, t1), t1), ((ge1, t1), t2), 36337, nestedTuplesNeqCost5, 1805)(_.copy()) + verifyNeq((((ge1, t1), b1), b1), (((ge1, t1), b1), b2), 36497, nestedTuplesNeqCost6, 1806)(_.copy()) + verifyNeq((((ge1, t1), b1), (preH1, preH1)), (((ge1, t1), b1), (preH1, preH2)), 36417, nestedTuplesNeqCost7, 1807)(_.copy()) + verifyNeq((((ge1, t1), b1), (preH1, (h1, h1))), (((ge1, t1), b1), (preH1, (h1, h2))), 36427, nestedTuplesNeqCost8, 1808)(_.copy()) + } + property("NEQ of collections of pre-defined types") { + val collNeqCost1 = Array( + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + ) + val collNeqCost2 = Array( + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + SeqCostItem(NamedDesc("EQ_COA_Box"), PerItemCost(JitCost(15), JitCost(5), 1), 0) + ) implicit val evalSettings = suite.evalSettings.copy(isMeasureOperationTime = false) - verifyNeq(Coll[Byte](), Coll(1.toByte), 36337, collNeqCost1)(cloneColl(_)) + verifyNeq(Coll[Byte](), Coll(1.toByte), 36337, collNeqCost1, 1786)(cloneColl(_)) verifyNeq(Coll[Byte](0, 1), Coll(1.toByte, 1.toByte), 36337, Array( FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), - SeqCostItem(NamedDesc("EQ_COA_Byte"), PerItemCost(JitCost(15), JitCost(2), 128), 1)) + SeqCostItem(NamedDesc("EQ_COA_Byte"), PerItemCost(JitCost(15), JitCost(2), 128), 1)), + 1788 )(cloneColl(_)) - verifyNeq(Coll[Short](), Coll(1.toShort), 36337, collNeqCost1)(cloneColl(_)) + verifyNeq(Coll[Short](), Coll(1.toShort), 36337, collNeqCost1, 1786)(cloneColl(_)) verifyNeq(Coll[Short](0), Coll(1.toShort), 36337, Array( FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), - SeqCostItem(NamedDesc("EQ_COA_Short"), PerItemCost(JitCost(15), JitCost(2), 96), 1)) + SeqCostItem(NamedDesc("EQ_COA_Short"), PerItemCost(JitCost(15), JitCost(2), 96), 1)), + 1788 )(cloneColl(_)) - verifyNeq(Coll[Int](), Coll(1), 36337, collNeqCost1)(cloneColl(_)) + verifyNeq(Coll[Int](), Coll(1), 36337, collNeqCost1, 1786)(cloneColl(_)) verifyNeq(Coll[Int](0), Coll(1), 36337, Array( FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), - SeqCostItem(NamedDesc("EQ_COA_Int"), PerItemCost(JitCost(15), JitCost(2), 64), 1)) + SeqCostItem(NamedDesc("EQ_COA_Int"), PerItemCost(JitCost(15), JitCost(2), 64), 1)), + 1788 )(cloneColl(_)) - verifyNeq(Coll[Long](), Coll(1.toLong), 36337, collNeqCost1)(cloneColl(_)) + verifyNeq(Coll[Long](), Coll(1.toLong), 36337, collNeqCost1, 1786)(cloneColl(_)) verifyNeq(Coll[Long](0), Coll(1.toLong), 36337, Array( FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), - SeqCostItem(NamedDesc("EQ_COA_Long"), PerItemCost(JitCost(15), JitCost(2), 48), 1)) + SeqCostItem(NamedDesc("EQ_COA_Long"), PerItemCost(JitCost(15), JitCost(2), 48), 1)), + 1788 )(cloneColl(_)) prepareSamples[Coll[BigInt]] - verifyNeq(Coll[BigInt](), Coll(1.toBigInt), 36337, collNeqCost1)(cloneColl(_)) + verifyNeq(Coll[BigInt](), Coll(1.toBigInt), 36337, collNeqCost1, 1786)(cloneColl(_)) verifyNeq(Coll[BigInt](0.toBigInt), Coll(1.toBigInt), 36337, Array( FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), - SeqCostItem(NamedDesc("EQ_COA_BigInt"), PerItemCost(JitCost(15), JitCost(7), 5), 1)) + SeqCostItem(NamedDesc("EQ_COA_BigInt"), PerItemCost(JitCost(15), JitCost(7), 5), 1)), + 1788 )(cloneColl(_)) prepareSamples[Coll[GroupElement]] - verifyNeq(Coll[GroupElement](), Coll(ge1), 36337, collNeqCost1)(cloneColl(_)) + verifyNeq(Coll[GroupElement](), Coll(ge1), 36337, collNeqCost1, 1786)(cloneColl(_)) verifyNeq(Coll[GroupElement](ge1), Coll(ge2), 36337, Array( FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), - SeqCostItem(NamedDesc("EQ_COA_GroupElement"), PerItemCost(JitCost(15), JitCost(5), 1), 1)) + SeqCostItem(NamedDesc("EQ_COA_GroupElement"), PerItemCost(JitCost(15), JitCost(5), 1), 1)), + 1788 )(cloneColl(_)) prepareSamples[Coll[AvlTree]] - verifyNeq(Coll[AvlTree](), Coll(t1), 36337, collNeqCost1)(cloneColl(_)) + verifyNeq(Coll[AvlTree](), Coll(t1), 36337, collNeqCost1, 1786)(cloneColl(_)) verifyNeq(Coll[AvlTree](t1), Coll(t2), 36337, Array( FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), - SeqCostItem(NamedDesc("EQ_COA_AvlTree"), PerItemCost(JitCost(15), JitCost(5), 2), 1)) + SeqCostItem(NamedDesc("EQ_COA_AvlTree"), PerItemCost(JitCost(15), JitCost(5), 2), 1)), + 1788 )(cloneColl(_)) { // since SBox.isConstantSize = false, the cost is different among cases @@ -2727,35 +2590,38 @@ class SigmaDslSpecification extends SigmaDslTesting val y = Coll(b1) val copied_x = cloneColl(x) verifyOp(Seq( - (x, x) -> Expected(Success(false), 36337), - (x, copied_x) -> Expected(Success(false), 36337), - (copied_x, x) -> Expected(Success(false), 36337), - (x, y) -> Expected(Success(true), 36377, costNEQ(collNeqCost1)), - (y, x) -> Expected(Success(true), 36377) + (x, x) -> Expected(Success(false), 36337, costNEQ(collNeqCost2), 1788), + (x, copied_x) -> Expected(Success(false), 36337, costNEQ(collNeqCost2), 1788), + (copied_x, x) -> Expected(Success(false), 36337, costNEQ(collNeqCost2), 1788), + (x, y) -> Expected(Success(true), 36377, costNEQ(collNeqCost1), 1786), + (y, x) -> Expected(Success(true), 36377, costNEQ(collNeqCost1), 1786) ), "!=", NEQ.apply)(_ != _, generateCases = false) verifyNeq(Coll[Box](b1), Coll(b2), 36417, Array( FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), - SeqCostItem(NamedDesc("EQ_COA_Box"), PerItemCost(JitCost(15), JitCost(5), 1), 1)) + SeqCostItem(NamedDesc("EQ_COA_Box"), PerItemCost(JitCost(15), JitCost(5), 1), 1)), + 1788 )(cloneColl(_), generateCases = false) } prepareSamples[Coll[PreHeader]] - verifyNeq(Coll[PreHeader](), Coll(preH1), 36337, collNeqCost1)(cloneColl(_)) + verifyNeq(Coll[PreHeader](), Coll(preH1), 36337, collNeqCost1, 1786)(cloneColl(_)) verifyNeq(Coll[PreHeader](preH1), Coll(preH2), 36337, Array( FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), - SeqCostItem(NamedDesc("EQ_COA_PreHeader"), PerItemCost(JitCost(15), JitCost(3), 1), 1)) + SeqCostItem(NamedDesc("EQ_COA_PreHeader"), PerItemCost(JitCost(15), JitCost(3), 1), 1)), + 1788 )(cloneColl(_)) prepareSamples[Coll[Header]] - verifyNeq(Coll[Header](), Coll(h1), 36337, collNeqCost1)(cloneColl(_)) + verifyNeq(Coll[Header](), Coll(h1), 36337, collNeqCost1, 1786)(cloneColl(_)) verifyNeq(Coll[Header](h1), Coll(h2), 36337, Array( FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), - SeqCostItem(NamedDesc("EQ_COA_Header"), PerItemCost(JitCost(15), JitCost(5), 1), 1)) + SeqCostItem(NamedDesc("EQ_COA_Header"), PerItemCost(JitCost(15), JitCost(5), 1), 1)), + 1788 )(cloneColl(_)) } @@ -2765,16 +2631,68 @@ class SigmaDslSpecification extends SigmaDslTesting prepareSamples[Coll[Coll[Int]]] prepareSamples[Coll[Coll[Int]]] - verifyNeq(Coll[Coll[Int]](), Coll(Coll[Int]()), 36337)(cloneColl(_)) - verifyNeq(Coll(Coll[Int]()), Coll(Coll[Int](1)), 36337)(cloneColl(_)) - verifyNeq(Coll(Coll[Int](1)), Coll(Coll[Int](2)), 36337)(cloneColl(_)) - verifyNeq(Coll(Coll[Int](1)), Coll(Coll[Int](1, 2)), 36337)(cloneColl(_)) + val nestedNeq1 = Array( + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + ) + val nestedNeq2 = Array( + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + SeqCostItem(NamedDesc("EQ_Coll"), PerItemCost(JitCost(10), JitCost(2), 1), 1) + ) + val nestedNeq3 = Array( + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + SeqCostItem(NamedDesc("EQ_COA_Int"), PerItemCost(JitCost(15), JitCost(2), 64), 1), + SeqCostItem(NamedDesc("EQ_Coll"), PerItemCost(JitCost(10), JitCost(2), 1), 1) + ) + verifyNeq(Coll[Coll[Int]](), Coll(Coll[Int]()), 36337, nestedNeq1, 1786)(cloneColl(_)) + verifyNeq(Coll(Coll[Int]()), Coll(Coll[Int](1)), 36337, nestedNeq2, 1787)(cloneColl(_)) + verifyNeq(Coll(Coll[Int](1)), Coll(Coll[Int](2)), 36337, nestedNeq3, 1789)(cloneColl(_)) + verifyNeq(Coll(Coll[Int](1)), Coll(Coll[Int](1, 2)), 36337, nestedNeq2, 1787)(cloneColl(_)) prepareSamples[Coll[(Int, BigInt)]] prepareSamples[Coll[Coll[(Int, BigInt)]]] - verifyNeq(Coll(Coll((1, 10.toBigInt))), Coll(Coll((1, 11.toBigInt))), 36337)(cloneColl(_)) - verifyNeq(Coll(Coll(Coll((1, 10.toBigInt)))), Coll(Coll(Coll((1, 11.toBigInt)))), 36337)(cloneColl(_)) + val nestedNeq4 = Array( + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(NamedDesc("EQ_BigInt"), FixedCost(JitCost(5))), + SeqCostItem(NamedDesc("EQ_Coll"), PerItemCost(JitCost(10), JitCost(2), 1), 1), + SeqCostItem(NamedDesc("EQ_Coll"), PerItemCost(JitCost(10), JitCost(2), 1), 1) + ) + val nestedNeq5 = Array( + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(NamedDesc("EQ_BigInt"), FixedCost(JitCost(5))), + SeqCostItem(NamedDesc("EQ_Coll"), PerItemCost(JitCost(10), JitCost(2), 1), 1), + SeqCostItem(NamedDesc("EQ_Coll"), PerItemCost(JitCost(10), JitCost(2), 1), 1), + SeqCostItem(NamedDesc("EQ_Coll"), PerItemCost(JitCost(10), JitCost(2), 1), 1) + ) + val nestedNeq6 = Array( + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6))), + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(NamedDesc("EQ_BigInt"), FixedCost(JitCost(5))), + FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(NamedDesc("EQ_BigInt"), FixedCost(JitCost(5))), + SeqCostItem(NamedDesc("EQ_Coll"), PerItemCost(JitCost(10), JitCost(2), 1), 2), + SeqCostItem(NamedDesc("EQ_Coll"), PerItemCost(JitCost(10), JitCost(2), 1), 1), + SeqCostItem(NamedDesc("EQ_Coll"), PerItemCost(JitCost(10), JitCost(2), 1), 1) + ) + verifyNeq(Coll(Coll((1, 10.toBigInt))), Coll(Coll((1, 11.toBigInt))), 36337, nestedNeq4, 1790)(cloneColl(_)) + verifyNeq(Coll(Coll(Coll((1, 10.toBigInt)))), Coll(Coll(Coll((1, 11.toBigInt)))), 36337, nestedNeq5, 1791)(cloneColl(_)) verifyNeq( (Coll( (Coll( @@ -2786,25 +2704,22 @@ class SigmaDslSpecification extends SigmaDslTesting (t1, Coll((1, 10.toBigInt), (1, 11.toBigInt))) ), ge1) ), preH1), - 36337)(x => (cloneColl(x._1), x._2)) + 36337, + nestedNeq6, + 1794 + )(x => (cloneColl(x._1), x._2)) } property("GroupElement.getEncoded equivalence") { verifyCases( { - val cost = TracedCost( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), + val costDetails = TracedCost( + traceBase ++ Array( FixedCostItem(PropertyCall), - MethodCallCostItem(TracedCost(Array(FixedCostItem(SGroupElement.GetEncodedMethod, FixedCost(JitCost(3)))))) + FixedCostItem(SGroupElement.GetEncodedMethod, FixedCost(JitCost(250))) ) ) - def success[T](v: T) = Expected(Success(v), 37905, cost) + def success[T](v: T) = Expected(Success(v), 37905, costDetails, 1810) Seq( (ge1, success(Helpers.decodeBytes(ge1str))), (ge2, success(Helpers.decodeBytes(ge2str))), @@ -2826,21 +2741,16 @@ class SigmaDslSpecification extends SigmaDslTesting property("decodePoint(GroupElement.getEncoded) equivalence") { verifyCases( { - val cost = TracedCost( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), + val costDetails = TracedCost( + traceBase ++ Array( FixedCostItem(PropertyCall), - MethodCallCostItem(TracedCost(Array(FixedCostItem(MethodDesc(SGroupElement.GetEncodedMethod), FixedCost(JitCost(3)))))), + FixedCostItem(MethodDesc(SGroupElement.GetEncodedMethod), FixedCost(JitCost(250))), FixedCostItem(DecodePoint), - FixedCostItem(ValUse) + FixedCostItem(ValUse), + FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172))) ) ) - def success[T](v: T) = Expected(Success(v), 38340, cost) + def success[T](v: T) = Expected(Success(v), 38340, costDetails, 1857) Seq( (ge1, success(true)), (ge2, success(true)), @@ -2870,19 +2780,13 @@ class SigmaDslSpecification extends SigmaDslTesting property("GroupElement.negate equivalence") { verifyCases( { - val cost = TracedCost( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), + val costDetails = TracedCost( + traceBase ++ Array( FixedCostItem(PropertyCall), - MethodCallCostItem(TracedCost(Array(FixedCostItem(MethodDesc(SGroupElement.NegateMethod), FixedCost(JitCost(1)))))) + FixedCostItem(MethodDesc(SGroupElement.NegateMethod), FixedCost(JitCost(45))) ) ) - def success[T](v: T) = Expected(Success(v), 36292, cost) + def success[T](v: T) = Expected(Success(v), 36292, costDetails, 1805) Seq( (ge1, success(Helpers.decodeGroupElement("02358d53f01276211f92d0aefbd278805121d4ff6eb534b777af1ee8abae5b2056"))), (ge2, success(Helpers.decodeGroupElement("03dba7b94b111f3894e2f9120b577da595ec7d58d488485adf73bf4e153af63575"))), @@ -2901,21 +2805,15 @@ class SigmaDslSpecification extends SigmaDslTesting property("GroupElement.exp equivalence") { val cases = { - val cost = TracedCost( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), + val costDetails = TracedCost( + traceBase ++ Array( FixedCostItem(SelectField), FixedCostItem(ValUse), FixedCostItem(SelectField), FixedCostItem(Exponentiate) ) ) - def success[T](v: T) = Expected(Success(v), 41484, cost) + def success[T](v: T) = Expected(Success(v), 41484, costDetails, 1893) Seq( ((ge1, CBigInt(new BigInteger("-25c80b560dd7844e2efd10f80f7ee57d", 16))), success(Helpers.decodeGroupElement("023a850181b7b73f92a5bbfa0bfc78f5bbb6ff00645ddde501037017e1a2251e2e"))), @@ -3005,21 +2903,15 @@ class SigmaDslSpecification extends SigmaDslTesting if (lowerMethodCallsInTests) { verifyCases( { - val cost = TracedCost( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), + val costDetails = TracedCost( + traceBase ++ Array( FixedCostItem(SelectField), FixedCostItem(ValUse), FixedCostItem(SelectField), FixedCostItem(MultiplyGroup) ) ) - def success[T](v: T) = Expected(Success(v), 36457, cost) + def success[T](v: T) = Expected(Success(v), 36457, costDetails, 1807) Seq( ((ge1, Helpers.decodeGroupElement("03e132ca090614bd6c9f811e91f6daae61f16968a1e6c694ed65aacd1b1092320e")), success(Helpers.decodeGroupElement("02bc48937b4a66f249a32dfb4d2efd0743dc88d46d770b8c5d39fd03325ba211df"))), @@ -3121,8 +3013,7 @@ class SigmaDslSpecification extends SigmaDslTesting } verifyCases( { - // jozi: here - def success[T](v: T) = Expected(Success(v), 36182) + def success[T](v: T) = Expected(Success(v), 36182, methodCostDetails(SAvlTree.digestMethod, 15), 1787) Seq( (t1, success(Helpers.decodeBytes("000183807f66b301530120ff7fc6bd6601ff01ff7f7d2bedbbffff00187fe89094"))), (t2, success(Helpers.decodeBytes("ff000d937f80ffd731ed802d24358001ff8080ff71007f00ad37e0a7ae43fff95b"))), @@ -3135,8 +3026,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - // jozi: And here - def success[T](v: T) = Expected(Success(v), 36260) + def success[T](v: T) = Expected(Success(v), 36260, methodCostDetails(SAvlTree.enabledOperationsMethod, 15), 1785) Seq( (t1, success(6.toByte)), (t2, success(0.toByte)), @@ -3149,8 +3039,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - // jozi: And here - def success[T](v: T) = Expected(Success(v), 36136) + def success[T](v: T) = Expected(Success(v), 36136, methodCostDetails(SAvlTree.keyLengthMethod, 15), 1785) Seq( (t1, success(1)), (t2, success(32)), @@ -3161,10 +3050,16 @@ class SigmaDslSpecification extends SigmaDslTesting "{ (t: AvlTree) => t.keyLength }", expectedExprFor("keyLength"))) + val newCostValueLength = TracedCost( + traceBase ++ Array( + FixedCostItem(PropertyCall), + FixedCostItem(MethodDesc(SAvlTree.valueLengthOptMethod), FixedCost(JitCost(15))), + ) + ) verifyCases( { - // jozi: also here - def success[T](v: T) = Expected(Success(v), 37151) + // TODO mainnet v5.0: This test isn't using JITC. Cyclic failing for newCost: 1785 vs 1786 + def success[T](v: T) = Expected(Success(v), 37151)//, newCostValueLength, 1786) Seq( (t1, success(Some(1))), (t2, success(Some(64))), @@ -3177,7 +3072,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 36479) + def success[T](v: T) = Expected(Success(v), 36479, methodCostDetails(SAvlTree.isInsertAllowedMethod, 15), 1785) Seq( (t1, success(false)), (t2, success(false)), @@ -3190,7 +3085,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 36096) + def success[T](v: T) = Expected(Success(v), 36096, methodCostDetails(SAvlTree.isUpdateAllowedMethod, 15), 1785) Seq( (t1, success(true)), (t2, success(false)), @@ -3203,8 +3098,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - // jozi: here - def success[T](v: T) = Expected(Success(v), 36502) + def success[T](v: T) = Expected(Success(v), 36502, methodCostDetails(SAvlTree.isRemoveAllowedMethod, 15), 1785) Seq( (t1, success(true)), (t2, success(false)), @@ -3404,7 +3298,6 @@ class SigmaDslSpecification extends SigmaDslTesting ) )) - // jozi: also here def success[T](v: T) = Expected(Success(v), 0) { // getMany with a bunch of existing keys @@ -3420,7 +3313,8 @@ class SigmaDslSpecification extends SigmaDslTesting val tree = SigmaDsl.avlTree(AvlTreeFlags.ReadOnly.serializeToByte, digest, 32, None) val input = (tree, (keys, proof)) - getMany.checkExpected(input, success(expRes)) + // TODO mainnet v5.0: checkExpected is ignoring newCost/costDetails. Is that correct behaviour? + getMany.checkExpected(input, Expected(Success(expRes), 0))//, TracedCost(traceBase), 1700)) } val (keysArr, valuesArr, _, avlProver) = sampleAvlProver @@ -3445,8 +3339,8 @@ class SigmaDslSpecification extends SigmaDslTesting contains.checkExpected(input, success(okContains)) get.checkExpected(input, success(valueOpt)) - // jozi: And here - contains.checkVerify(input, Expected(value = Success(okContains), cost = 37850)) + // TODO mainnet v5.0: Cyclic newCost here (1823/1825), also costDetails are ignored + contains.checkVerify(input, Expected(Success(okContains), 37850))//, TracedCost(traceBase), 1823)) get.checkVerify(input, Expected(value = Success(valueOpt), cost = 38372)) } @@ -3456,7 +3350,6 @@ class SigmaDslSpecification extends SigmaDslTesting { val input = (tree, (keys, proof)) getMany.checkExpected(input, success(expRes)) - // jozi: also here getMany.checkVerify(input, Expected(value = Success(expRes), cost = 38991)) } @@ -3464,7 +3357,6 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (tree, digest) val (res, _) = updateDigest.checkEquality(input).getOrThrow res.digest shouldBe digest - // jozi: also here updateDigest.checkVerify(input, Expected(value = Success(res), cost = 36341)) } @@ -3474,7 +3366,6 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (tree, newOps) val (res,_) = updateOperations.checkEquality(input).getOrThrow res.enabledOperations shouldBe newOps - // jozi: also here updateOperations.checkVerify(input, Expected(value = Success(res), cost = 36341)) } @@ -3485,7 +3376,6 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (tree, (key, invalidProof)) val (res, _) = contains.checkEquality(input).getOrThrow res shouldBe false - // jozi: also here contains.checkVerify(input, Expected(value = Success(res), cost = 37850)) } @@ -3600,6 +3490,12 @@ class SigmaDslSpecification extends SigmaDslTesting ) )) + val newCostDetails = TracedCost( + traceBase ++ Array( + FixedCostItem(PropertyCall) + ) + ) + forAll(keyCollGen, bytesCollGen) { (key, value) => val (tree, avlProver) = createAvlTreeAndProver() val preInsertDigest = avlProver.digest.toColl @@ -3611,8 +3507,7 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (preInsertTree, (kvs, insertProof)) val (res, _) = insert.checkEquality(input).getOrThrow res.isDefined shouldBe true - // jozi: also here - insert.checkVerify(input, Expected(value = Success(res), cost = 38501)) + insert.checkVerify(input, Expected(Success(res), 38501, newCostDetails, 1816)) } { // negative: readonly tree @@ -3620,7 +3515,6 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (readonlyTree, (kvs, insertProof)) val (res, _) = insert.checkEquality(input).getOrThrow res.isDefined shouldBe false - // jozi: also here insert.checkVerify(input, Expected(value = Success(res), cost = 38501)) } @@ -3631,7 +3525,6 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (tree, (invalidKvs, insertProof)) val (res, _) = insert.checkEquality(input).getOrThrow res.isDefined shouldBe true // TODO v6.0: should it really be true? (looks like a bug) - // jozi: also here insert.checkVerify(input, Expected(value = Success(res), cost = 38501)) } @@ -3714,7 +3607,6 @@ class SigmaDslSpecification extends SigmaDslTesting )) val cost = 40952 - // jozi: also here def success[T](v: T) = Expected(Success(v), 0) forAll(keyCollGen, bytesCollGen) { (key, value) => @@ -3731,8 +3623,7 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (preUpdateTree, (kvs, updateProof)) val res = Some(endTree) update.checkExpected(input, success(res)) - // jozi: also here - update.checkVerify(input, Expected(value = Success(res), cost = cost)) + update.checkVerify(input, Expected(Success(res), cost)) } { // positive: update to the same value (identity operation) @@ -3741,7 +3632,6 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (tree, (keys, updateProof)) val res = Some(tree) update.checkExpected(input, success(res)) - // jozi: also here update.checkVerify(input, Expected(value = Success(res), cost = cost)) } @@ -3749,7 +3639,6 @@ class SigmaDslSpecification extends SigmaDslTesting val readonlyTree = createTree(preUpdateDigest) val input = (readonlyTree, (kvs, updateProof)) update.checkExpected(input, success(None)) - // jozi: also here update.checkVerify(input, Expected(value = Success(None), cost = cost)) } @@ -3759,7 +3648,6 @@ class SigmaDslSpecification extends SigmaDslTesting val invalidKvs = Colls.fromItems((invalidKey -> newValue)) val input = (tree, (invalidKvs, updateProof)) update.checkExpected(input, success(None)) - // jozi: also here update.checkVerify(input, Expected(value = Success(None), cost = cost)) } @@ -3770,7 +3658,6 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (tree, (invalidKvs, updateProof)) val (res, _) = update.checkEquality(input).getOrThrow res.isDefined shouldBe true // TODO v6.0: should it really be true? (looks like a bug) - // jozi: also here update.checkVerify(input, Expected(value = Success(res), cost = cost)) } @@ -3779,7 +3666,6 @@ class SigmaDslSpecification extends SigmaDslTesting val invalidProof = updateProof.map(x => (-x).toByte) // any other different from proof val input = (tree, (kvs, invalidProof)) update.checkExpected(input, success(None)) - // jozi: also here update.checkVerify(input, Expected(value = Success(None), cost = cost)) } } @@ -3822,7 +3708,6 @@ class SigmaDslSpecification extends SigmaDslTesting ) )) - // jozi: also here def success[T](v: T) = Expected(Success(v), 0) { // positive test with many keys in the tree and to remove @@ -3885,9 +3770,10 @@ class SigmaDslSpecification extends SigmaDslTesting } property("longToByteArray equivalence") { + val costDetails = CostDetails(traceBase :+ FixedCostItem(CompanionDesc(LongToByteArray), FixedCost(JitCost(17)))) verifyCases( { - def success[T](v: T) = Expected(Success(v), 36007) + def success[T](v: T) = Expected(Success(v), 36007, costDetails, 1787) Seq( (-9223372036854775808L, success(Helpers.decodeBytes("8000000000000000"))), (-1148502660425090565L, success(Helpers.decodeBytes("f00fb2ea55c579fb"))), @@ -3904,9 +3790,10 @@ class SigmaDslSpecification extends SigmaDslTesting } property("byteArrayToBigInt equivalence") { + val costDetails = CostDetails(traceBase :+ FixedCostItem(CompanionDesc(ByteArrayToBigInt), FixedCost(JitCost(30)))) verifyCases( { - def success[T](v: T) = Expected(Success(v), 36536) + def success[T](v: T) = Expected(Success(v), 36536, costDetails, 1787) Seq( (Helpers.decodeBytes(""), Expected(new NumberFormatException("Zero length BigInteger"))), @@ -3917,9 +3804,9 @@ class SigmaDslSpecification extends SigmaDslTesting (Helpers.decodeBytes("ff"), success(CBigInt(new BigInteger("-1", 16)))), (Helpers.decodeBytes("80d6c201"), - Expected(Success(CBigInt(new BigInteger("-7f293dff", 16))), 36539)), + Expected(Success(CBigInt(new BigInteger("-7f293dff", 16))), 36539, costDetails, 1787)), (Helpers.decodeBytes("70d6c20170d6c201"), - Expected(Success(CBigInt(new BigInteger("70d6c20170d6c201", 16))), 36543)), + Expected(Success(CBigInt(new BigInteger("70d6c20170d6c201", 16))), 36543, costDetails, 1787)), (Helpers.decodeBytes( "80e0ff7f02807fff72807f0a00ff7fb7c57f75c11ba2802970fd250052807fc37f6480ffff007fff18eeba44" ), Expected(new ArithmeticException("BigInteger out of 256 bit range"))) @@ -3931,9 +3818,10 @@ class SigmaDslSpecification extends SigmaDslTesting } property("byteArrayToLong equivalence") { + val costDetails = CostDetails(traceBase :+ FixedCostItem(CompanionDesc(ByteArrayToLong), FixedCost(JitCost(16)))) verifyCases( { - def success[T](v: T) = Expected(Success(v), 36092) + def success[T](v: T) = Expected(Success(v), 36092, costDetails, 1785) Seq( (Helpers.decodeBytes(""), Expected(new IllegalArgumentException("array too small: 0 < 8"))), (Helpers.decodeBytes("81"), Expected(new IllegalArgumentException("array too small: 1 < 8"))), @@ -3963,8 +3851,9 @@ class SigmaDslSpecification extends SigmaDslTesting property("Box properties equivalence") { verifyCases( { - def success[T](v: T) = Expected(Success(v), 35984) - Seq( + val costDetails = CostDetails(traceBase :+ FixedCostItem(CompanionDesc(ExtractId), FixedCost(JitCost(12)))) + def success[T](v: T) = Expected(Success(v), 35984, costDetails, 1786) + Seq( (b1, success(Helpers.decodeBytes("5ee78f30ae4e770e44900a46854e9fecb6b12e8112556ef1cd19aef633b4421e"))), (b2, success(Helpers.decodeBytes("3a0089be265460e29ca47d26e5b55a6f3e3ffaf5b4aed941410a2437913848ad"))) ) @@ -3975,7 +3864,8 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 35882) + val costDetails = CostDetails(traceBase :+ FixedCostItem(CompanionDesc(ExtractAmount), FixedCost(JitCost(8)))) + def success[T](v: T) = Expected(Success(v), 35882, costDetails, 1784) Seq( (b1, success(9223372036854775807L)), (b2, success(12345L)) @@ -3987,7 +3877,8 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 35938) + val costDetails = CostDetails(traceBase :+ FixedCostItem(CompanionDesc(ExtractScriptBytes), FixedCost(JitCost(10)))) + def success[T](v: T) = Expected(Success(v), 35938, costDetails, 1786) Seq( (b1, success(Helpers.decodeBytes( "100108cd0297c44a12f4eb99a85d298fa3ba829b5b42b9f63798c980ece801cc663cc5fc9e7300" @@ -4001,7 +3892,8 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 36012) + val costDetails = CostDetails(traceBase :+ FixedCostItem(CompanionDesc(ExtractBytes), FixedCost(JitCost(12)))) + def success[T](v: T) = Expected(Success(v), 36012, costDetails, 1786) Seq( (b1, success(Helpers.decodeBytes( "ffffffffffffffff7f100108cd0297c44a12f4eb99a85d298fa3ba829b5b42b9f63798c980ece801cc663cc5fc9e73009fac29026e789ab7b2fffff12280a6cd01557f6fb22b7f80ff7aff8e1f7f15973d7f000180ade204a3ff007f00057600808001ff8f8000019000ffdb806fff7cc0b6015eb37fa600f4030201000e067fc87f7f01ff218301ae8000018008637f0021fb9e00018055486f0b514121016a00ff718080bcb001" @@ -4017,7 +3909,8 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 35954) + val costDetails = CostDetails(traceBase :+ FixedCostItem(CompanionDesc(ExtractBytesWithNoRef), FixedCost(JitCost(12)))) + def success[T](v: T) = Expected(Success(v), 35954, costDetails, 1786) Seq( (b1, success(Helpers.decodeBytes( "ffffffffffffffff7f100108cd0297c44a12f4eb99a85d298fa3ba829b5b42b9f63798c980ece801cc663cc5fc9e73009fac29026e789ab7b2fffff12280a6cd01557f6fb22b7f80ff7aff8e1f7f15973d7f000180ade204a3ff007f00057600808001ff8f8000019000ffdb806fff7cc0b6015eb37fa600f4030201000e067fc87f7f01ff" @@ -4033,7 +3926,8 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 36074) + val costDetails = CostDetails(traceBase :+ FixedCostItem(CompanionDesc(ExtractCreationInfo), FixedCost(JitCost(16)))) + def success[T](v: T) = Expected(Success(v), 36074, costDetails, 1787) Seq( (b1, success(( 677407, @@ -4049,6 +3943,12 @@ class SigmaDslSpecification extends SigmaDslTesting "{ (x: Box) => x.creationInfo }", FuncValue(Vector((1, SBox)), ExtractCreationInfo(ValUse(1, SBox))))) + val costDetails = CostDetails( + traceBase ++ Array( + FixedCostItem(PropertyCall), + FixedCostItem(MethodDesc(SBox.tokensMethod), FixedCost(JitCost(15))), + ) + ) // TODO v6.0 (2h): fix collections equality and remove map(identity) // (PairOfColl should be equal CollOverArray) verifyCases( @@ -4057,7 +3957,7 @@ class SigmaDslSpecification extends SigmaDslTesting (Helpers.decodeBytes("6e789ab7b2fffff12280a6cd01557f6fb22b7f80ff7aff8e1f7f15973d7f0001"), 10000000L), (Helpers.decodeBytes("a3ff007f00057600808001ff8f8000019000ffdb806fff7cc0b6015eb37fa600"), 500L) ).map(identity)), 36167), - b2 -> Expected(Success(Coll[(Coll[Byte], Long)]().map(identity)), 36157) + b2 -> Expected(Success(Coll[(Coll[Byte], Long)]().map(identity)), 36157, costDetails, 1786) ), existingFeature({ (x: Box) => x.tokens }, "{ (x: Box) => x.tokens }", @@ -4098,14 +3998,49 @@ class SigmaDslSpecification extends SigmaDslTesting ErgoBox.R4 -> ByteConstant(2.toByte) )) val box4 = boxWithRegisters(Map.empty) + val expCostDetails1 = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(CompanionDesc(ExtractRegisterAs), FixedCost(JitCost(50))), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(CompanionDesc(OptionIsDefined), FixedCost(JitCost(10))), + FixedCostItem(CompanionDesc(If), FixedCost(JitCost(10))), + FixedCostItem(ValUse), + FixedCostItem(OptionGet) + ) + ) + val expCostDetails2 = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(CompanionDesc(ExtractRegisterAs), FixedCost(JitCost(50))), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(CompanionDesc(OptionIsDefined), FixedCost(JitCost(10))), + FixedCostItem(CompanionDesc(If), FixedCost(JitCost(10))), + FixedCostItem(Constant) + ) + ) verifyCases( Seq( - (box1, Expected(Success(1024.toShort), cost = 37190)), + (box1, Expected(Success(1024.toShort), 37190, expCostDetails1, 1794)), (box2, Expected( new InvalidType("Cannot getReg[Short](5): invalid type of value TestValue(1048576) at id=5") )), - (box3, Expected(Success(0.toShort), cost = 37190)) + (box3, Expected(Success(0.toShort), 37190, expCostDetails2, 1792)) ), existingFeature( { (x: Box) => @@ -4136,69 +4071,161 @@ class SigmaDslSpecification extends SigmaDslTesting ) ))) - verifyCases( - Seq( - (box1, Expected(Success(1024), cost = 39782)), - (box2, Expected(Success(1024 * 1024), cost = 39782)), - (box3, Expected(Success(0), cost = 39782)), - (box4, Expected(Success(-1), cost = 39782)) - ), - existingFeature( - { (x: Box) => - val tagOpt = x.R4[Byte] - if (tagOpt.isDefined) { - val tag = tagOpt.get - if (tag == 0.toByte) { - val short = x.R5[Short].get // access Short in the register - short.toInt - } else { - if (tag == 1.toByte) { - x.R5[Int].get // access Int in the register - } - else 0 - } - } else { - -1 - } - }, - """{ - | (x: Box) => - | val tagOpt = x.R4[Byte] - | if (tagOpt.isDefined) { - | val tag = tagOpt.get - | if (tag == 0.toByte) { - | val short = x.R5[Short].get // access Short in the register - | short.toInt - | } else { - | if (tag == 1.toByte) { - | x.R5[Int].get // access Int in the register - | } - | else 0 - | } - | } else { - | -1 - | } - |}""".stripMargin, - FuncValue( - Array((1, SBox)), - BlockValue( - Array(ValDef(3, List(), ExtractRegisterAs(ValUse(1, SBox), ErgoBox.R4, SOption(SByte)))), - If( - OptionIsDefined(ValUse(3, SOption(SByte))), - BlockValue( - Array(ValDef(4, List(), OptionGet(ValUse(3, SOption(SByte))))), - If( - EQ(ValUse(4, SByte), ByteConstant(0.toByte)), - Upcast(OptionGet(ExtractRegisterAs(ValUse(1, SBox), ErgoBox.R5, SOption(SShort))), SInt), - If( - EQ(ValUse(4, SByte), ByteConstant(1.toByte)), - OptionGet(ExtractRegisterAs(ValUse(1, SBox), ErgoBox.R5, SOption(SInt))), - IntConstant(0) - ) - ) - ), - IntConstant(-1) - ) + val expCostDetails3 = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(CompanionDesc(ExtractRegisterAs), FixedCost(JitCost(50))), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(CompanionDesc(OptionIsDefined), FixedCost(JitCost(10))), + FixedCostItem(CompanionDesc(If), FixedCost(JitCost(10))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(CompanionDesc(If), FixedCost(JitCost(10))), + FixedCostItem(ValUse), + FixedCostItem(CompanionDesc(ExtractRegisterAs), FixedCost(JitCost(50))), + FixedCostItem(OptionGet), + TypeBasedCostItem(Upcast, SInt) + ) + ) + + val expCostDetails4 = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(CompanionDesc(ExtractRegisterAs), FixedCost(JitCost(50))), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(CompanionDesc(OptionIsDefined), FixedCost(JitCost(10))), + FixedCostItem(CompanionDesc(If), FixedCost(JitCost(10))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(CompanionDesc(If), FixedCost(JitCost(10))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(CompanionDesc(If), FixedCost(JitCost(10))), + FixedCostItem(ValUse), + FixedCostItem(CompanionDesc(ExtractRegisterAs), FixedCost(JitCost(50))), + FixedCostItem(OptionGet) + ) + ) + + val expCostDetails5 = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(CompanionDesc(ExtractRegisterAs), FixedCost(JitCost(50))), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(CompanionDesc(OptionIsDefined), FixedCost(JitCost(10))), + FixedCostItem(CompanionDesc(If), FixedCost(JitCost(10))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(CompanionDesc(If), FixedCost(JitCost(10))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(CompanionDesc(If), FixedCost(JitCost(10))), + FixedCostItem(Constant) + ) + ) + + + verifyCases( + Seq( + (box1, Expected(Success(1024), cost = 39782, expCostDetails3, 1805)), + (box2, Expected(Success(1024 * 1024), cost = 39782, expCostDetails4, 1806)), + (box3, Expected(Success(0), cost = 39782, expCostDetails5, 1799)), + (box4, Expected(Success(-1), cost = 39782, expCostDetails2, 1792)) + ), + existingFeature( + { (x: Box) => + val tagOpt = x.R4[Byte] + if (tagOpt.isDefined) { + val tag = tagOpt.get + if (tag == 0.toByte) { + val short = x.R5[Short].get // access Short in the register + short.toInt + } else { + if (tag == 1.toByte) { + x.R5[Int].get // access Int in the register + } + else 0 + } + } else { + -1 + } + }, + """{ + | (x: Box) => + | val tagOpt = x.R4[Byte] + | if (tagOpt.isDefined) { + | val tag = tagOpt.get + | if (tag == 0.toByte) { + | val short = x.R5[Short].get // access Short in the register + | short.toInt + | } else { + | if (tag == 1.toByte) { + | x.R5[Int].get // access Int in the register + | } + | else 0 + | } + | } else { + | -1 + | } + |}""".stripMargin, + FuncValue( + Array((1, SBox)), + BlockValue( + Array(ValDef(3, List(), ExtractRegisterAs(ValUse(1, SBox), ErgoBox.R4, SOption(SByte)))), + If( + OptionIsDefined(ValUse(3, SOption(SByte))), + BlockValue( + Array(ValDef(4, List(), OptionGet(ValUse(3, SOption(SByte))))), + If( + EQ(ValUse(4, SByte), ByteConstant(0.toByte)), + Upcast(OptionGet(ExtractRegisterAs(ValUse(1, SBox), ErgoBox.R5, SOption(SShort))), SInt), + If( + EQ(ValUse(4, SByte), ByteConstant(1.toByte)), + OptionGet(ExtractRegisterAs(ValUse(1, SBox), ErgoBox.R5, SOption(SInt))), + IntConstant(0) + ) + ) + ), + IntConstant(-1) + ) ) ) )) @@ -4227,9 +4254,15 @@ class SigmaDslSpecification extends SigmaDslTesting // ErgoBox.R6 -> Constant[SOption[SInt.type]](Option(10), SOption(SInt)), ))) + val expCostDetails = TracedCost( + traceBase ++ Array( + FixedCostItem(CompanionDesc(ExtractRegisterAs), FixedCost(JitCost(50))), + FixedCostItem(OptionGet) + ) + ) verifyCases( Seq( - (box1, Expected(Success(1.toByte), cost = 36253)), + (box1, Expected(Success(1.toByte), cost = 36253, expCostDetails, 1790)), (box2, Expected(new InvalidType("Cannot getReg[Byte](4): invalid type of value Value(Coll(1)) at id=4"))) ), existingFeature((x: Box) => x.R4[Byte].get, @@ -4241,7 +4274,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( Seq( - (box1, Expected(Success(1024.toShort), cost = 36253)), + (box1, Expected(Success(1024.toShort), cost = 36253, expCostDetails, 1790)), (box2, Expected(new NoSuchElementException("None.get"))) ), existingFeature((x: Box) => x.R5[Short].get, @@ -4253,7 +4286,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( Seq( - (box1, Expected(Success(1024 * 1024), cost = 36253)) + (box1, Expected(Success(1024 * 1024), cost = 36253, expCostDetails, 1790)) ), existingFeature((x: Box) => x.R6[Int].get, "{ (x: Box) => x.R6[Int].get }", @@ -4264,7 +4297,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( Seq( - (box1, Expected(Success(1024.toLong), cost = 36253)) + (box1, Expected(Success(1024.toLong), cost = 36253, expCostDetails, 1790)) ), existingFeature((x: Box) => x.R7[Long].get, "{ (x: Box) => x.R7[Long].get }", @@ -4275,7 +4308,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( Seq( - (box1, Expected(Success(CBigInt(BigInteger.valueOf(222L))), cost = 36253)) + (box1, Expected(Success(CBigInt(BigInteger.valueOf(222L))), cost = 36253, expCostDetails, 1790)) ), existingFeature((x: Box) => x.R8[BigInt].get, "{ (x: Box) => x.R8[BigInt].get }", @@ -4295,7 +4328,10 @@ class SigmaDslSpecification extends SigmaDslTesting 32, None ) - )), cost = 36253) + )), + cost = 36253, + expCostDetails, + 1790) )), existingFeature((x: Box) => x.R9[AvlTree].get, "{ (x: Box) => x.R9[AvlTree].get }", @@ -4306,7 +4342,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( Seq( - (box3, Expected(Success(10), cost = 36468)) + (box3, Expected(Success(10), cost = 36468))//, expCostDetails, 1790)) ), existingFeature((x: Box) => x.R4[(Int, Long)].get._1, "{ (x: Box) => x.R4[(Int, Long)].get._1 }", @@ -4320,7 +4356,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( Seq( - (box3, Expected(Success(10), cost = 36468)) + (box3, Expected(Success(10), cost = 36468))//, expCostDetails, 1790)) ), existingFeature((x: Box) => x.R5[(Int, Option[Long])].get._1, "{ (x: Box) => x.R5[(Int, Option[Long])].get._1 }", @@ -4364,36 +4400,38 @@ class SigmaDslSpecification extends SigmaDslTesting } property("PreHeader properties equivalence") { + + verifyCases( - Seq((preH1, Expected(Success(0.toByte), cost = 37022))), + Seq((preH1, Expected(Success(0.toByte), cost = 37022, methodCostDetails(SPreHeader.versionMethod, 10), 1785))), existingPropTest("version", { (x: PreHeader) => x.version })) verifyCases( Seq((preH1, Expected(Success( Helpers.decodeBytes("7fff7fdd6f62018bae0001006d9ca888ff7f56ff8006573700a167f17f2c9f40")), - cost = 36121))), + cost = 36121, methodCostDetails(SPreHeader.parentIdMethod, 10), 1786))), existingPropTest("parentId", { (x: PreHeader) => x.parentId })) verifyCases( - Seq((preH1, Expected(Success(6306290372572472443L), cost = 36147))), + Seq((preH1, Expected(Success(6306290372572472443L), cost = 36147, methodCostDetails(SPreHeader.timestampMethod, 10), 1785))), existingPropTest("timestamp", { (x: PreHeader) => x.timestamp })) verifyCases( - Seq((preH1, Expected(Success(-3683306095029417063L), cost = 36127))), + Seq((preH1, Expected(Success(-3683306095029417063L), cost = 36127, methodCostDetails(SPreHeader.nBitsMethod, 10), 1785))), existingPropTest("nBits", { (x: PreHeader) => x.nBits })) verifyCases( - Seq((preH1, Expected(Success(1), cost = 36136))), + Seq((preH1, Expected(Success(1), cost = 36136, methodCostDetails(SPreHeader.heightMethod, 10), 1785))), existingPropTest("height", { (x: PreHeader) => x.height })) verifyCases( Seq((preH1, Expected(Success( Helpers.decodeGroupElement("026930cb9972e01534918a6f6d6b8e35bc398f57140d13eb3623ea31fbd069939b")), - cost = 36255))), + cost = 36255, methodCostDetails(SPreHeader.minerPkMethod, 10), 1802))), existingPropTest("minerPk", { (x: PreHeader) => x.minerPk })) verifyCases( - Seq((preH1, Expected(Success(Helpers.decodeBytes("ff8087")), cost = 36249))), + Seq((preH1, Expected(Success(Helpers.decodeBytes("ff8087")), cost = 36249, methodCostDetails(SPreHeader.votesMethod,10), 1786))), existingPropTest("votes", { (x: PreHeader) => x.votes })) } @@ -4401,81 +4439,81 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( Seq((h1, Expected(Success( Helpers.decodeBytes("957f008001808080ffe4ffffc8f3802401df40006aa05e017fa8d3f6004c804a")), - cost = 36955))), + cost = 36955, methodCostDetails(SHeader.idMethod, 10), 1786))), existingPropTest("id", { (x: Header) => x.id })) verifyCases( - Seq((h1, Expected(Success(0.toByte), cost = 36124))), + Seq((h1, Expected(Success(0.toByte), cost = 36124, methodCostDetails(SHeader.versionMethod, 10), 1785))), existingPropTest("version", { (x: Header) => x.version })) verifyCases( Seq((h1, Expected(Success( Helpers.decodeBytes("0180dd805b0000ff5400b997fd7f0b9b00de00fb03c47e37806a8186b94f07ff")), - cost = 36097))), + cost = 36097, methodCostDetails(SHeader.parentIdMethod, 10), 1786))), existingPropTest("parentId", { (x: Header) => x.parentId })) verifyCases( Seq((h1, Expected(Success( Helpers.decodeBytes("01f07f60d100ffb970c3007f60ff7f24d4070bb8fffa7fca7f34c10001ffe39d")), - cost = 36111))), + cost = 36111, methodCostDetails(SHeader.ADProofsRootMethod, 10), 1786))), existingPropTest("ADProofsRoot", { (x: Header) => x.ADProofsRoot})) verifyCases( - Seq((h1, Expected(Success(CAvlTree(createAvlTreeData())), cost = 36092))), + Seq((h1, Expected(Success(CAvlTree(createAvlTreeData())), cost = 36092, methodCostDetails(SHeader.stateRootMethod, 10), 1785))), existingPropTest("stateRoot", { (x: Header) => x.stateRoot })) verifyCases( Seq((h1, Expected(Success( Helpers.decodeBytes("804101ff01000080a3ffbd006ac080098df132a7017f00649311ec0e00000100")), - cost = 36094))), + cost = 36094, methodCostDetails(SHeader.transactionsRootMethod, 10), 1786))), existingPropTest("transactionsRoot", { (x: Header) => x.transactionsRoot })) verifyCases( - Seq((h1, Expected(Success(1L), cost = 36151))), + Seq((h1, Expected(Success(1L), cost = 36151, methodCostDetails(SHeader.timestampMethod, 10), 1785))), existingPropTest("timestamp", { (x: Header) => x.timestamp })) verifyCases( - Seq((h1, Expected(Success(-1L), cost = 36125))), + Seq((h1, Expected(Success(-1L), cost = 36125, methodCostDetails(SHeader.nBitsMethod, 10), 1785))), existingPropTest("nBits", { (x: Header) => x.nBits })) verifyCases( - Seq((h1, Expected(Success(1), cost = 36134))), + Seq((h1, Expected(Success(1), cost = 36134, methodCostDetails(SHeader.heightMethod, 10), 1785))), existingPropTest("height", { (x: Header) => x.height })) verifyCases( Seq((h1, Expected(Success( Helpers.decodeBytes("e57f80885601b8ff348e01808000bcfc767f2dd37f0d01015030ec018080bc62")), - cost = 36133))), + cost = 36133, methodCostDetails(SHeader.extensionRootMethod, 10), 1786))), existingPropTest("extensionRoot", { (x: Header) => x.extensionRoot })) verifyCases( Seq((h1, Expected(Success( Helpers.decodeGroupElement("039bdbfa0b49cc6bef58297a85feff45f7bbeb500a9d2283004c74fcedd4bd2904")), - cost = 36111))), + cost = 36111, methodCostDetails(SHeader.minerPkMethod, 10), 1802))), existingPropTest("minerPk", { (x: Header) => x.minerPk })) verifyCases( Seq((h1, Expected(Success( Helpers.decodeGroupElement("0361299207fa392231e23666f6945ae3e867b978e021d8d702872bde454e9abe9c")), - cost = 36111))), + cost = 36111, methodCostDetails(SHeader.powOnetimePkMethod, 10), 1802))), existingPropTest("powOnetimePk", { (x: Header) => x.powOnetimePk })) verifyCases( Seq((h1, Expected(Success( Helpers.decodeBytes("7f4f09012a807f01")), - cost = 36176))), + cost = 36176, methodCostDetails(SHeader.powNonceMethod, 10), 1786))), existingPropTest("powNonce", { (x: Header) => x.powNonce })) verifyCases( Seq((h1, Expected(Success( CBigInt(new BigInteger("-e24990c47e15ed4d0178c44f1790cc72155d516c43c3e8684e75db3800a288", 16))), - cost = 36220))), + cost = 36220, methodCostDetails(SHeader.powDistanceMethod, 10), 1785))), existingPropTest("powDistance", { (x: Header) => x.powDistance })) verifyCases( Seq((h1, Expected(Success( Helpers.decodeBytes("7f0180")), - cost = 36100))), + cost = 36100, methodCostDetails(SHeader.votesMethod, 10), 1786))), existingPropTest("votes", { (x: Header) => x.votes })) } @@ -4684,9 +4722,17 @@ class SigmaDslSpecification extends SigmaDslTesting test(samples, existingPropTest("dataInputs", { (x: Context) => x.dataInputs })) + val testTraceBase = traceBase ++ Array( + FixedCostItem(PropertyCall), + FixedCostItem(SContext.dataInputsMethod, FixedCost(JitCost(15))), + FixedCostItem(Constant), + FixedCostItem(ByIndex) + ) + + val costDetails = TracedCost(testTraceBase) verifyCases( Seq( - (ctx, Expected(Success(dataBox), cost = 37087)), + (ctx, Expected(Success(dataBox), cost = 37087, costDetails, 1789)), (ctx.copy(_dataInputs = Coll()), Expected(new ArrayIndexOutOfBoundsException("0"))) ), existingFeature({ (x: Context) => x.dataInputs(0) }, @@ -4706,11 +4752,12 @@ class SigmaDslSpecification extends SigmaDslTesting )), preGeneratedSamples = Some(samples)) + val idCostDetails = TracedCost(testTraceBase :+ FixedCostItem(CompanionDesc(ExtractId), FixedCost(JitCost(12)))) verifyCases( Seq( (ctx, Expected(Success( Helpers.decodeBytes("7da4b55971f19a78d007638464580f91a020ab468c0dbe608deb1f619e245bc3")), - cost = 37193)) + cost = 37193, idCostDetails, 1792)) ), existingFeature({ (x: Context) => x.dataInputs(0).id }, "{ (x: Context) => x.dataInputs(0).id }", @@ -4760,16 +4807,39 @@ class SigmaDslSpecification extends SigmaDslTesting test(samples, existingFeature({ (x: Context) => x.SELF }, "{ (x: Context) => x.SELF }", FuncValue(Vector((1, SContext)), Self))) + val heightCostDetails = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(Height) + ) + ) verifyCases( - Seq(ctx -> Expected(Success(ctx.HEIGHT), cost = 35885)), + Seq(ctx -> Expected(Success(ctx.HEIGHT), cost = 35885, heightCostDetails, 1786)), existingFeature( { (x: Context) => x.HEIGHT }, "{ (x: Context) => x.HEIGHT }", FuncValue(Vector((1, SContext)), Height)), preGeneratedSamples = Some(samples)) + val inputsCostDetails1 = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(Inputs), + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(MapCollection), PerItemCost(JitCost(20), JitCost(1), 10), 1), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractAmount))) verifyCases( - Seq((ctx, Expected(Success(Coll[Long](80946L)), cost = 39152))), + Seq((ctx, Expected(Success(Coll[Long](80946L)), cost = 39152, inputsCostDetails1, 1790))), existingFeature( { (x: Context) => x.INPUTS.map { (b: Box) => b.value } }, "{ (x: Context) => x.INPUTS.map { (b: Box) => b.value } }", @@ -4779,8 +4849,26 @@ class SigmaDslSpecification extends SigmaDslTesting )), preGeneratedSamples = Some(samples)) + val inputsCostDetails2 = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(Inputs), + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(MapCollection), PerItemCost(JitCost(20), JitCost(1), 10), 1), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(ExtractAmount), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ValUse), + FixedCostItem(Tuple))) verifyCases( - Seq((ctx, Expected(Success(Coll((80946L, 80946L))), cost = 39959))), + Seq((ctx, Expected(Success(Coll((80946L, 80946L))), cost = 39959, inputsCostDetails2, 1794))), existingFeature( { (x: Context) => x.INPUTS.map { (b: Box) => (b.value, b.value) } }, """{ (x: Context) => @@ -4839,6 +4927,7 @@ class SigmaDslSpecification extends SigmaDslTesting Seq( (ctx, Expected( Success(-1), cost = 36318, + // TODO mainnet v5.0: Change to concrete details does not change test result newDetails = CostDetails.ZeroCost, newCost = 1786, newVersionedResults = { @@ -4871,13 +4960,27 @@ class SigmaDslSpecification extends SigmaDslTesting } } + val lastBlockCostDetails = TracedCost( + traceBase ++ Array( + FixedCostItem(PropertyCall), + FixedCostItem(SContext.lastBlockUtxoRootHashMethod, FixedCost(JitCost(15))) + ) + ) verifyCases( - Seq(ctx -> Expected(Success(ctx.LastBlockUtxoRootHash), cost = 35990)), + Seq(ctx -> Expected(Success(ctx.LastBlockUtxoRootHash), cost = 35990, lastBlockCostDetails, 1786)), existingPropTest("LastBlockUtxoRootHash", { (x: Context) => x.LastBlockUtxoRootHash }), preGeneratedSamples = Some(samples)) + val isUpdateAllowedCostDetails = TracedCost( + traceBase ++ Array( + FixedCostItem(PropertyCall), + FixedCostItem(SContext.lastBlockUtxoRootHashMethod, FixedCost(JitCost(15))), + FixedCostItem(PropertyCall), + FixedCostItem(SAvlTree.isUpdateAllowedMethod, FixedCost(JitCost(15))) + ) + ) verifyCases( - Seq(ctx -> Expected(Success(ctx.LastBlockUtxoRootHash.isUpdateAllowed), cost = 36288)), + Seq(ctx -> Expected(Success(ctx.LastBlockUtxoRootHash.isUpdateAllowed), cost = 36288, isUpdateAllowedCostDetails, 1787)), existingFeature( { (x: Context) => x.LastBlockUtxoRootHash.isUpdateAllowed }, "{ (x: Context) => x.LastBlockUtxoRootHash.isUpdateAllowed }", @@ -4897,8 +5000,14 @@ class SigmaDslSpecification extends SigmaDslTesting )), preGeneratedSamples = Some(samples)) + val minerCostDetails = TracedCost( + traceBase ++ Array( + FixedCostItem(PropertyCall), + FixedCostItem(SContext.minerPubKeyMethod, FixedCost(JitCost(20))) + ) + ) verifyCases( - Seq(ctx -> Expected(Success(ctx.minerPubKey), cost = 36047)), + Seq(ctx -> Expected(Success(ctx.minerPubKey), cost = 36047, minerCostDetails, 1787)), existingPropTest("minerPubKey", { (x: Context) => x.minerPubKey }), preGeneratedSamples = Some(samples)) @@ -4920,7 +5029,7 @@ class SigmaDslSpecification extends SigmaDslTesting "{ (x: Context) => getVar[Boolean](11) }", FuncValue(Vector((1, SContext)), GetVar(11.toByte, SOption(SBoolean)))), preGeneratedSamples = Some(samples)) - + verifyCases( Seq((ctx, Expected(new InvalidType("Cannot getVar[Int](11): invalid type of value Value(true) at id=2")))), existingFeature((x: Context) => x.getVar[Int](11).get, @@ -4928,8 +5037,17 @@ class SigmaDslSpecification extends SigmaDslTesting FuncValue(Vector((1, SContext)), OptionGet(GetVar(11.toByte, SOption(SInt))))), preGeneratedSamples = Some(samples)) + val getVarCostDetails = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(GetVar), + FixedCostItem(OptionGet))) verifyCases( - Seq((ctx, Expected(Success(true), cost = 36750))), + Seq((ctx, Expected(Success(true), cost = 36750, getVarCostDetails, 1785))), existingFeature((x: Context) => x.getVar[Boolean](11).get, "{ (x: Context) => getVar[Boolean](11).get }", FuncValue(Vector((1, SContext)), OptionGet(GetVar(11.toByte, SOption(SBoolean))))), @@ -4939,9 +5057,31 @@ class SigmaDslSpecification extends SigmaDslTesting property("Conditional access to data box register using isDefined") { val (_, _, _, ctx, _, _) = contextData() + val registerIsDefinedCostDetails = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(PropertyCall), + FixedCostItem(SContext.dataInputsMethod, FixedCost(JitCost(15))), + FixedCostItem(Constant), + FixedCostItem(ByIndex), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(OptionIsDefined), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(OptionGet) + ) + ) verifyCases( Seq( - ctx -> Expected(Success(-135729055492651903L), 38399) + ctx -> Expected(Success(-135729055492651903L), 38399, registerIsDefinedCostDetails, 1799) ), existingFeature( { (x: Context) => @@ -5074,14 +5214,103 @@ class SigmaDslSpecification extends SigmaDslTesting property("Conditional access OUTPUTS(0).R4 using tag in R5") { val (_, _, _, ctx, _, _) = contextData() + val registerTagCostDetails1 = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(Outputs), + FixedCostItem(Constant), + FixedCostItem(ByIndex), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(OptionIsDefined), + FixedCostItem(If), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(OptionGet) + ) + ) + val registerTagCostDetails2 = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(Outputs), + FixedCostItem(Constant), + FixedCostItem(ByIndex), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(OptionIsDefined), + FixedCostItem(If), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(OptionGet), + TypeBasedCostItem(Upcast, SLong) + ) + ) + + val registerTagCostDetails3 = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(Outputs), + FixedCostItem(Constant), + FixedCostItem(ByIndex), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(OptionIsDefined), + FixedCostItem(If), + FixedCostItem(Constant) + ) + ) verifyCases( Seq( - ctx -> Expected(Success(5008366408131208436L), 40406), + ctx -> Expected(Success(5008366408131208436L), 40406, registerTagCostDetails1, 1811), ctxWithRegsInOutput(ctx, Map( ErgoBox.R5 -> LongConstant(0L), - ErgoBox.R4 -> ShortConstant(10))) -> Expected(Success(10L), 40396), + ErgoBox.R4 -> ShortConstant(10))) -> Expected(Success(10L), 40396, registerTagCostDetails2, 1810), ctxWithRegsInOutput(ctx, Map( - ErgoBox.R4 -> ShortConstant(10))) -> Expected(Success(-1L), 40396) + ErgoBox.R4 -> ShortConstant(10))) -> Expected(Success(-1L), 40396, registerTagCostDetails3, 1797) ), existingFeature( { (x: Context) => @@ -5154,25 +5383,152 @@ class SigmaDslSpecification extends SigmaDslTesting property("Conditional access OUTPUTS(0).R4 using tag in R5 (plus action)") { val (_, _, _, ctx, _, _) = contextData() - verifyCases( - Seq( - // case 1L - ctx -> Expected(Success(5008366408131289382L), 41016), + val tagRegisterCostDetails1 = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(Outputs), + FixedCostItem(Constant), + FixedCostItem(ByIndex), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(OptionIsDefined), + FixedCostItem(If), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(OptionGet), + FixedCostItem(Self), + FixedCostItem(ExtractAmount), + TypeBasedCostItem(ArithOp.Plus, SLong) + ) + ) + val tagRegisterCostDetails2 = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(Outputs), + FixedCostItem(Constant), + FixedCostItem(ByIndex), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(OptionIsDefined), + FixedCostItem(If), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(OptionGet), + TypeBasedCostItem(Upcast, SLong), + FixedCostItem(Self), + FixedCostItem(ExtractAmount), + TypeBasedCostItem(ArithOp.Plus, SLong) + ) + ) + val tagRegisterCostDetails3 = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(Outputs), + FixedCostItem(Constant), + FixedCostItem(ByIndex), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(OptionIsDefined), + FixedCostItem(If), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(If), + FixedCostItem(Constant) + ) + ) + val tagRegisterCostDetails4 = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(Outputs), + FixedCostItem(Constant), + FixedCostItem(ByIndex), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(OptionIsDefined), + FixedCostItem(If), + FixedCostItem(Constant) + ) + ) + verifyCases( + Seq( + // case 1L + ctx -> Expected(Success(5008366408131289382L), 41016, tagRegisterCostDetails1, 1814), // case 0L ctxWithRegsInOutput(ctx, Map( ErgoBox.R5 -> LongConstant(0L), - ErgoBox.R4 -> ShortConstant(10))) -> Expected(Success(80956L), 41006), + ErgoBox.R4 -> ShortConstant(10))) -> Expected(Success(80956L), 41006, tagRegisterCostDetails2, 1813), // case returning 0L ctxWithRegsInOutput(ctx, Map( ErgoBox.R5 -> LongConstant(2L), // note R4 is required to avoid // "RuntimeException: Set of non-mandatory indexes is not densely packed" - ErgoBox.R4 -> ShortConstant(10))) -> Expected(Success(0L), 41006), + ErgoBox.R4 -> ShortConstant(10))) -> Expected(Success(0L), 41006, tagRegisterCostDetails3, 1804), // case returning -1L ctxWithRegsInOutput(ctx, Map( - ErgoBox.R4 -> ShortConstant(10))) -> Expected(Success(-1L), 41006) + ErgoBox.R4 -> ShortConstant(10))) -> Expected(Success(-1L), 41006, tagRegisterCostDetails4, 1797) ), existingFeature( { (x: Context) => @@ -5256,19 +5612,149 @@ class SigmaDslSpecification extends SigmaDslTesting property("Conditional access dataInputs(0).R4 using tag in R5") { val (_, _, _, ctx, _, _) = contextData() + val tagRegisterCostDetails1 = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(ValUse), + FixedCostItem(PropertyCall), + FixedCostItem(SContext.dataInputsMethod, FixedCost(JitCost(15))), + FixedCostItem(Constant), + FixedCostItem(ByIndex), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(OptionIsDefined), + FixedCostItem(If), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(OptionGet) + ) + ) + val tagRegisterCostDetails2 = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(ValUse), + FixedCostItem(PropertyCall), + FixedCostItem(SContext.dataInputsMethod, FixedCost(JitCost(15))), + FixedCostItem(Constant), + FixedCostItem(ByIndex), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(OptionIsDefined), + FixedCostItem(If), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(OptionGet), + TypeBasedCostItem(Upcast, SLong) + ) + ) + val tagRegisterCostDetails3 = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(ValUse), + FixedCostItem(PropertyCall), + FixedCostItem(SContext.dataInputsMethod, FixedCost(JitCost(15))), + FixedCostItem(Constant), + FixedCostItem(ByIndex), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(OptionIsDefined), + FixedCostItem(If), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(If), + FixedCostItem(Constant) + ) + ) + val tagRegisterCostDetails4 = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(ValUse), + FixedCostItem(PropertyCall), + FixedCostItem(SContext.dataInputsMethod, FixedCost(JitCost(15))), + FixedCostItem(Constant), + FixedCostItem(ByIndex), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(OptionIsDefined), + FixedCostItem(If), + FixedCostItem(Constant) + ) + ) + verifyCases( Seq( ctxWithRegsInDataInput(ctx, Map( ErgoBox.R5 -> LongConstant(1L), - ErgoBox.R4 -> LongConstant(10))) -> Expected(Success(10L), 41084), + ErgoBox.R4 -> LongConstant(10))) -> Expected(Success(10L), 41084, tagRegisterCostDetails1, 1812), ctxWithRegsInDataInput(ctx, Map( ErgoBox.R5 -> LongConstant(0L), - ErgoBox.R4 -> ShortConstant(10))) -> Expected(Success(10L), 41084), + ErgoBox.R4 -> ShortConstant(10))) -> Expected(Success(10L), 41084, tagRegisterCostDetails2, 1811), ctxWithRegsInDataInput(ctx, Map( ErgoBox.R5 -> LongConstant(2L), - ErgoBox.R4 -> ShortConstant(10))) -> Expected(Success(0L), 41084), + ErgoBox.R4 -> ShortConstant(10))) -> Expected(Success(0L), 41084, tagRegisterCostDetails3, 1806), ctxWithRegsInDataInput(ctx, Map( - ErgoBox.R4 -> ShortConstant(10))) -> Expected(Success(-1L), 41084) + ErgoBox.R4 -> ShortConstant(10))) -> Expected(Success(-1L), 41084, tagRegisterCostDetails4, 1799) ), existingFeature( { (x: Context) => @@ -5354,19 +5840,154 @@ class SigmaDslSpecification extends SigmaDslTesting property("Conditional access dataInputs(0).R4 using tag in R5 (plus action)") { val (_, _, _, ctx, _, _) = contextData() + val costDetails1 = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(ValUse), + FixedCostItem(PropertyCall), + FixedCostItem(SContext.dataInputsMethod, FixedCost(JitCost(15))), + FixedCostItem(Constant), + FixedCostItem(ByIndex), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(OptionIsDefined), + FixedCostItem(If), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(OptionGet), + FixedCostItem(Self), + FixedCostItem(ExtractAmount), + TypeBasedCostItem(ArithOp.Plus, SLong) + ) + ) + val costDetails2 = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(ValUse), + FixedCostItem(PropertyCall), + FixedCostItem(SContext.dataInputsMethod, FixedCost(JitCost(15))), + FixedCostItem(Constant), + FixedCostItem(ByIndex), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(OptionIsDefined), + FixedCostItem(If), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(OptionGet), + TypeBasedCostItem(Upcast, SLong), + FixedCostItem(Self), + FixedCostItem(ExtractAmount), + TypeBasedCostItem(ArithOp.Plus, SLong) + ) + ) + val costDetails3 = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(ValUse), + FixedCostItem(PropertyCall), + FixedCostItem(SContext.dataInputsMethod, FixedCost(JitCost(15))), + FixedCostItem(Constant), + FixedCostItem(ByIndex), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(OptionIsDefined), + FixedCostItem(If), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(If), + FixedCostItem(Constant) + ) + ) + val costDetails4 = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(ValUse), + FixedCostItem(PropertyCall), + FixedCostItem(SContext.dataInputsMethod, FixedCost(JitCost(15))), + FixedCostItem(Constant), + FixedCostItem(ByIndex), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractRegisterAs), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(OptionIsDefined), + FixedCostItem(If), + FixedCostItem(Constant) + ) + ) verifyCases( Seq( ctxWithRegsInDataInput(ctx, Map( ErgoBox.R5 -> LongConstant(1L), - ErgoBox.R4 -> LongConstant(10))) -> Expected(Success(80956L), 41694), + ErgoBox.R4 -> LongConstant(10))) -> Expected(Success(80956L), 41694, costDetails1, 1816), ctxWithRegsInDataInput(ctx, Map( ErgoBox.R5 -> LongConstant(0L), - ErgoBox.R4 -> ShortConstant(10))) -> Expected(Success(80956L), 41694), + ErgoBox.R4 -> ShortConstant(10))) -> Expected(Success(80956L), 41694, costDetails2, 1814), ctxWithRegsInDataInput(ctx, Map( ErgoBox.R5 -> LongConstant(2L), - ErgoBox.R4 -> ShortConstant(10))) -> Expected(Success(0L), 41694), + ErgoBox.R4 -> ShortConstant(10))) -> Expected(Success(0L), 41694, costDetails3, 1806), ctxWithRegsInDataInput(ctx, Map( - ErgoBox.R4 -> ShortConstant(10))) -> Expected(Success(-1L), 41694) + ErgoBox.R4 -> ShortConstant(10))) -> Expected(Success(-1L), 41694, costDetails4, 1799) ), existingFeature( { (x: Context) => @@ -5465,7 +6086,6 @@ class SigmaDslSpecification extends SigmaDslTesting property("xorOf equivalence") { verifyCases( { - def success[T](v: T, c: Int) = Expected(Success(v), c) def successNew[T](v: T, c: Int, newV: T, newC: Int) = Expected( value = Success(v), cost = c, @@ -5507,27 +6127,32 @@ class SigmaDslSpecification extends SigmaDslTesting } property("LogicalNot equivalence") { + val costDetails = TracedCost(traceBase :+ FixedCostItem(LogicalNot)) verifyCases( Seq( - (true, Expected(Success(false), 35864)), - (false, Expected(Success(true), 35864))), + (true, Expected(Success(false), 35864, costDetails, 1785)), + (false, Expected(Success(true), 35864, costDetails, 1785))), existingFeature((x: Boolean) => !x, "{ (x: Boolean) => !x }", FuncValue(Vector((1, SBoolean)), LogicalNot(ValUse(1, SBoolean))))) } property("Numeric Negation equivalence") { + val costDetails = TracedCost(traceBase :+ FixedCostItem(Negation)) verifyCases( { - def success[T](v: T) = Expected(Success(v), 36136) + def success[T](v: T) = Expected(Success(v), 36136, costDetails, 1786) Seq( (Byte.MinValue, success(Byte.MinValue)), // !!! + ((Byte.MinValue + 1).toByte, success(Byte.MaxValue)), (-40.toByte, success(40.toByte)), (-1.toByte, success(1.toByte)), (0.toByte, success(0.toByte)), (1.toByte, success(-1.toByte)), (45.toByte, success(-45.toByte)), - (127.toByte, success(-127.toByte))) + (127.toByte, success(-127.toByte)), + (Byte.MaxValue, success(-127.toByte)) + ) }, existingFeature((x: Byte) => (-x).toByte, "{ (x: Byte) => -x }", @@ -5535,15 +6160,16 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 36136) + def success[T](v: T) = Expected(Success(v), 36136, costDetails, 1786) Seq( (Short.MinValue, success(Short.MinValue)), // special case! - ((Short.MinValue + 1).toShort, success(32767.toShort)), + ((Short.MinValue + 1).toShort, success(Short.MaxValue)), (-1528.toShort, success(1528.toShort)), (-1.toShort, success(1.toShort)), (0.toShort, success(0.toShort)), (1.toShort, success(-1.toShort)), (7586.toShort, success(-7586.toShort)), + (32767.toShort, success(-32767.toShort)), (Short.MaxValue, success(-32767.toShort))) }, existingFeature((x: Short) => (-x).toShort, @@ -5552,10 +6178,10 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 36136) + def success[T](v: T) = Expected(Success(v), 36136, costDetails, 1786) Seq( (Int.MinValue, success(Int.MinValue)), // special case! - (Int.MinValue + 1, success(2147483647)), + (Int.MinValue + 1, success(Int.MaxValue)), (-63509744, success(63509744)), (-1, success(1)), (0, success(0)), @@ -5569,16 +6195,16 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 36136) + def success[T](v: T) = Expected(Success(v), 36136, costDetails, 1786) Seq( (Long.MinValue, success(Long.MinValue)), // special case! - (Long.MinValue + 1, success(9223372036854775807L)), + (Long.MinValue + 1, success(Long.MaxValue)), (-957264171003115006L, success(957264171003115006L)), (-1L, success(1L)), (0L, success(0L)), (1L, success(-1L)), (340835904095777627L, success(-340835904095777627L)), - (9223372036854775807L, success(-9223372036854775807L))) + (Long.MaxValue, success(-9223372036854775807L))) }, existingFeature((x: Long) => -x, "{ (x: Long) => -x }", @@ -5586,7 +6212,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 36136) + def success[T](v: T) = Expected(Success(v), 36136, costDetails, 1787) Seq( (CBigInt(new BigInteger("-1655a05845a6ad363ac88ea21e88b97e436a1f02c548537e12e2d9667bf0680", 16)), success(CBigInt(new BigInteger("1655a05845a6ad363ac88ea21e88b97e436a1f02c548537e12e2d9667bf0680", 16)))), (CBigInt(new BigInteger("-1b24ba8badba8abf347cce054d9b9f14f229321507245b8", 16)), success(CBigInt(new BigInteger("1b24ba8badba8abf347cce054d9b9f14f229321507245b8", 16)))), @@ -5610,9 +6236,21 @@ class SigmaDslSpecification extends SigmaDslTesting } property("groupGenerator equivalence") { + val costDetails = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(Global), + FixedCostItem(PropertyCall), + FixedCostItem(SGlobal.groupGeneratorMethod, FixedCost(JitCost(10))) + ) + ) verifyCases( { - def success[T](v: T) = Expected(Success(v), 35981) + def success[T](v: T) = Expected(Success(v), 35981, costDetails, 1802) Seq( (-1, success(Helpers.decodeGroupElement("0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"))), (1, success(Helpers.decodeGroupElement("0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798")))) @@ -5631,7 +6269,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 35981) + def success[T](v: T) = Expected(Success(v), 35981, costDetails, 1802) Seq( (-1, success(Helpers.decodeGroupElement("0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"))), (1, success(Helpers.decodeGroupElement("0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798")))) @@ -5649,9 +6287,15 @@ class SigmaDslSpecification extends SigmaDslTesting ))) if (lowerMethodCallsInTests) { + val expCostDetails = TracedCost( + costDetails.trace ++ Array( + FixedCostItem(ValUse), + FixedCostItem(Exponentiate) + ) + ) verifyCases( { - def success[T](v: T) = Expected(Success(v), 41237) + def success[T](v: T) = Expected(Success(v), 41237, expCostDetails, 1892) Seq( (CBigInt(new BigInteger("-e5c1a54694c85d644fa30a6fc5f3aa209ed304d57f72683a0ebf21038b6a9d", 16)), success(Helpers.decodeGroupElement("023395bcba3d7cf21d73c50f8af79d09a8c404c15ce9d04f067d672823bae91a54"))), (CBigInt(new BigInteger("-bc2d08f935259e0eebf272c66c6e1dbd484c6706390215", 16)), success(Helpers.decodeGroupElement("02ddcf4c48105faf3c16f7399b5dbedd82ab0bb50ae292d8f88f49a3f86e78974e"))), @@ -5685,7 +6329,6 @@ class SigmaDslSpecification extends SigmaDslTesting } property("Global.xor equivalence") { - if (lowerMethodCallsInTests) { verifyCases( { @@ -5844,13 +6487,46 @@ class SigmaDslSpecification extends SigmaDslTesting val b1 = create_b1 val b2 = create_b2 + val costDetails = TracedCost( + traceBase ++ Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Filter), PerItemCost(JitCost(20), JitCost(1), 10), 0) + ) + ) + val costDetails2 = TracedCost( + traceBase ++ Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Filter), PerItemCost(JitCost(20), JitCost(1), 10), 1), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractAmount), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SLong) + ) + ) + val costDetails3 = TracedCost( + traceBase ++ Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Filter), PerItemCost(JitCost(20), JitCost(1), 10), 2), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractAmount), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SLong), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractAmount), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SLong) + ) + ) verifyCases( { - def success[T](v: T, c: Int) = Expected(Success(v), c) + def success[T](v: T, c: Int, costDetails: CostDetails, newCost: Int) = Expected(Success(v), c, costDetails, newCost) Seq( - (Coll[Box](), success(Coll[Box](), 37297)), - (Coll[Box](b1), success(Coll[Box](), 37397)), - (Coll[Box](b1, b2), success(Coll[Box](b2), 37537)) + (Coll[Box](), success(Coll[Box](), 37297, costDetails, 1787)), + (Coll[Box](b1), success(Coll[Box](), 37397, costDetails2, 1792)), + (Coll[Box](b1, b2), success(Coll[Box](b2), 37537, costDetails3, 1796)) ) }, existingFeature({ (x: Coll[Box]) => x.filter({ (b: Box) => b.value > 1 }) }, @@ -5870,6 +6546,35 @@ class SigmaDslSpecification extends SigmaDslTesting val b1 = create_b1 val b2 = create_b2 + val costDetails1 = TracedCost( + traceBase ++ Array( + FixedCostItem(MethodCall), + FixedCostItem(FuncValue), + SeqCostItem(MethodDesc(SCollection.FlatMapMethod), PerItemCost(JitCost(60), JitCost(10), 8), 0) + ) + ) + val costDetails2 = TracedCost( + traceBase ++ Array( + FixedCostItem(MethodCall), + FixedCostItem(FuncValue), + FixedCostItem(NamedDesc("MatchSingleArgMethodCall"), FixedCost(JitCost(30))), + SeqCostItem(NamedDesc("CheckFlatmapBody"), PerItemCost(JitCost(20), JitCost(20), 1), 2), + SeqCostItem(MethodDesc(SCollection.FlatMapMethod), PerItemCost(JitCost(60), JitCost(10), 8), 0) + ), + ) + val costDetails3 = TracedCost( + traceBase ++ Array( + FixedCostItem(MethodCall), + FixedCostItem(FuncValue), + FixedCostItem(NamedDesc("MatchSingleArgMethodCall"), FixedCost(JitCost(30))), + SeqCostItem(NamedDesc("CheckFlatmapBody"), PerItemCost(JitCost(20), JitCost(20), 1), 2), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractScriptBytes), + SeqCostItem(MethodDesc(SCollection.FlatMapMethod), PerItemCost(JitCost(60), JitCost(10), 8), 135) + ) + ) + // TODO mainnet v5.0: Failing test with cyclic results verifyCases( { def success[T](v: T, c: Int) = Expected(Success(v), c) @@ -5903,14 +6608,21 @@ class SigmaDslSpecification extends SigmaDslTesting val samples = sampleCollBoxes val b1 = create_b1 val b2 = create_b2 + def costDetails(zipElements: Int) = TracedCost( + traceBase ++ Array( + FixedCostItem(MethodCall), + FixedCostItem(ValUse), + SeqCostItem(MethodDesc(SCollection.ZipMethod), PerItemCost(JitCost(10), JitCost(1), 10), zipElements) + ) + ) verifyCases( { - def success[T](v: T, c: Int) = Expected(Success(v), c) + def success[T](v: T, c: Int, cd: CostDetails, nc: Int) = Expected(Success(v), c, cd, nc) Seq( - (Coll[Box](), success(Coll[(Box, Box)](), 37399)), - (Coll[Box](b1), success(Coll[(Box, Box)]((b1, b1)), 37559)), - (Coll[Box](b1, b2), success(Coll[(Box, Box)]((b1, b1), (b2, b2)), 37719)) + (Coll[Box](), success(Coll[(Box, Box)](), 37399, costDetails(0), 1786)), + (Coll[Box](b1), success(Coll[(Box, Box)]((b1, b1)), 37559, costDetails(1), 1788)), + (Coll[Box](b1, b2), success(Coll[(Box, Box)]((b1, b1), (b2, b2)), 37719, costDetails(2), 1790)) ) }, existingFeature({ (x: Coll[Box]) => x.zip(x) }, @@ -5933,9 +6645,10 @@ class SigmaDslSpecification extends SigmaDslTesting val samples = sampleCollBoxes val b1 = create_b1 val b2 = create_b2 + val costDetails = TracedCost(traceBase :+ FixedCostItem(SizeOf)) verifyCases( { - def success[T](v: T) = Expected(Success(v), 35954) + def success[T](v: T) = Expected(Success(v), 35954, costDetails, 1785) Seq( (Coll[Box](), success(0)), (Coll[Box](b1), success(1)), @@ -5952,13 +6665,19 @@ class SigmaDslSpecification extends SigmaDslTesting val samples = sampleCollBoxes val b1 = create_b1 val b2 = create_b2 + def costDetails(indicesCount: Int) = TracedCost( + traceBase ++ Array( + FixedCostItem(PropertyCall), + SeqCostItem(MethodDesc(SCollection.IndicesMethod), PerItemCost(JitCost(20), JitCost(2), 16), indicesCount) + ) + ) verifyCases( { - def success[T](v: T) = Expected(Success(v), 36036) + def success[T](v: T, i: Int) = Expected(Success(v), 36036, costDetails(i), 1788) Seq( - (Coll[Box](), success(Coll[Int]())), - (Coll[Box](b1), success(Coll[Int](0))), - (Coll[Box](b1, b2), success(Coll[Int](0, 1))) + (Coll[Box](), success(Coll[Int](), 0)), + (Coll[Box](b1), success(Coll[Int](0), 1)), + (Coll[Box](b1, b2), success(Coll[Int](0, 1), 2)) ) }, existingFeature({ (x: Coll[Box]) => x.indices }, @@ -5980,15 +6699,43 @@ class SigmaDslSpecification extends SigmaDslTesting val samples = sampleCollBoxes val b1 = create_b1 val b2 = create_b2 + val costDetails1 = TracedCost( + traceBase ++ Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(ForAll), PerItemCost(JitCost(3), JitCost(1), 10), 0) + ) + ) + val costDetails2 = TracedCost( + traceBase ++ Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(ForAll), PerItemCost(JitCost(3), JitCost(1), 10), 1), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractAmount), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SLong) + ) + ) + val costDetails3 = TracedCost( + traceBase ++ Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(ForAll), PerItemCost(JitCost(3), JitCost(1), 10), 2), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractAmount), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SLong) + ) + ) def success[T](v: T, c: Int) = Expected(Success(v), c) if (lowerMethodCallsInTests) { verifyCases( { Seq( - (Coll[Box](), success(true, 37909)), - (Coll[Box](b1), success(false, 37969)), - (Coll[Box](b1, b2), success(false, 38029)) + (Coll[Box](), Expected(Success(true), 37909, costDetails1, 1784)), + (Coll[Box](b1), Expected(Success(false), 37969, costDetails2, 1789)), + (Coll[Box](b1, b2), Expected(Success(false), 38029, costDetails3, 1789)) ) }, existingFeature({ (x: Coll[Box]) => x.forall({ (b: Box) => b.value > 1 }) }, @@ -6004,7 +6751,8 @@ class SigmaDslSpecification extends SigmaDslTesting } else { assertExceptionThrown( verifyCases( - Seq( (Coll[Box](), success(true, 37909)) ), + // TODO mainnet v5.0: Correct behaviour (cost/details are ignored) when exception is thrown? + Seq( (Coll[Box](), Expected(Success(true), 37909, TracedCost(traceBase), 1789))), existingFeature( { (x: Coll[Box]) => x.forall({ (b: Box) => b.value > 1 }) }, "{ (x: Coll[Box]) => x.forall({(b: Box) => b.value > 1 }) }" @@ -6018,15 +6766,48 @@ class SigmaDslSpecification extends SigmaDslTesting val samples = sampleCollBoxes val b1 = create_b1 val b2 = create_b2 + val costDetails1 = TracedCost( + traceBase ++ Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Exists), PerItemCost(JitCost(3), JitCost(1), 10), 0) + ) + ) + val costDetails2 = TracedCost( + traceBase ++ Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Exists), PerItemCost(JitCost(3), JitCost(1), 10), 1), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractAmount), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SLong) + ) + ) + val costDetails3 = TracedCost( + traceBase ++ Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Exists), PerItemCost(JitCost(3), JitCost(1), 10), 2), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractAmount), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SLong), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractAmount), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SLong) + ) + ) def success[T](v: T, c: Int) = Expected(Success(v), c) if (lowerMethodCallsInTests) { verifyCases( { Seq( - (Coll[Box](), success(false, 38455)), - (Coll[Box](b1), success(false, 38515)), - (Coll[Box](b1, b2), success(true, 38575)) + (Coll[Box](), Expected(Success(false), 38455, costDetails1, 1784)), + (Coll[Box](b1), Expected(Success(false), 38515, costDetails2, 1789)), + (Coll[Box](b1, b2), Expected(Success(true), 38575, costDetails3, 1793)) ) }, existingFeature({ (x: Coll[Box]) => x.exists({ (b: Box) => b.value > 1 }) }, @@ -6042,6 +6823,7 @@ class SigmaDslSpecification extends SigmaDslTesting } else { assertExceptionThrown( verifyCases( + // TODO mainnet v5.0: Correct behaviour (cost/details are ignored) when exception is thrown? Seq( (Coll[Box](), success(false, 38455)) ), existingFeature( { (x: Coll[Box]) => x.exists({ (b: Box) => b.value > 1 }) }, @@ -6054,16 +6836,69 @@ class SigmaDslSpecification extends SigmaDslTesting property("Coll exists with nested If") { val o = NumericOps.BigIntIsExactOrdering + val costDetails1 = TracedCost( + traceBase ++ Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Exists), PerItemCost(JitCost(3), JitCost(1), 10), 0) + ) + ) + val costDetails2 = TracedCost( + traceBase ++ Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Exists), PerItemCost(JitCost(3), JitCost(1), 10), 1), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SBigInt), + FixedCostItem(If), + FixedCostItem(Constant) + ) + ) + val costDetails3 = TracedCost( + traceBase ++ Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Exists), PerItemCost(JitCost(3), JitCost(1), 10), 1), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SBigInt), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(LT, SBigInt) + ) + ) + val costDetails4 = TracedCost( + traceBase ++ Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Exists), PerItemCost(JitCost(3), JitCost(1), 10), 2), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SBigInt), + FixedCostItem(If), + FixedCostItem(Constant), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SBigInt), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(LT, SBigInt) + ) + ) + def success[T](v: T, c: Int) = Expected(Success(v), c) if (lowerMethodCallsInTests) { verifyCases( { Seq( - (Coll[BigInt](), success(false, 38955)), - (Coll[BigInt](BigIntZero), success(false, 39045)), - (Coll[BigInt](BigIntOne), success(true, 39045)), - (Coll[BigInt](BigIntZero, BigIntOne), success(true, 39135)), - (Coll[BigInt](BigIntZero, BigInt10), success(false, 39135)) + (Coll[BigInt](), Expected(Success(false), 38955, costDetails1, 1784)), + (Coll[BigInt](BigIntZero), Expected(Success(false), 39045, costDetails2, 1789)), + (Coll[BigInt](BigIntOne), Expected(Success(true), 39045, costDetails3, 1792)), + (Coll[BigInt](BigIntZero, BigIntOne), Expected(Success(true), 39135, costDetails4, 1797)), + (Coll[BigInt](BigIntZero, BigInt10), Expected(Success(false), 39135, costDetails4, 1797)) ) }, existingFeature( @@ -6089,6 +6924,7 @@ class SigmaDslSpecification extends SigmaDslTesting } else { assertExceptionThrown( verifyCases( + // TODO mainnet v5.0: Correct behaviour (cost/details are ignored) when exception is thrown? Seq( (Coll[BigInt](), success(false, 38955)) ), existingFeature( { (x: Coll[BigInt]) => x.exists({ (b: BigInt) => @@ -6099,21 +6935,74 @@ class SigmaDslSpecification extends SigmaDslTesting )), rootCauseLike[NoSuchMethodException]("sigmastate.eval.CostingRules$CollCoster.exists(scalan.Base$Ref)") ) - } - } - - property("Coll forall with nested If") { - val o = NumericOps.BigIntIsExactOrdering - def success[T](v: T, c: Int) = Expected(Success(v), c) + } + } + + property("Coll forall with nested If") { + val o = NumericOps.BigIntIsExactOrdering + val costDetails1 = TracedCost( + traceBase ++ Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(ForAll), PerItemCost(JitCost(3), JitCost(1), 10), 0) + ) + ) + val costDetails2 = TracedCost( + traceBase ++ Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(ForAll), PerItemCost(JitCost(3), JitCost(1), 10), 1), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GE, SBigInt), + FixedCostItem(If), + FixedCostItem(Constant) + ) + ) + val costDetails3 = TracedCost( + traceBase ++ Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(ForAll), PerItemCost(JitCost(3), JitCost(1), 10), 1), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GE, SBigInt), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(LE, SBigInt) + ) + ) + val costDetails4 = TracedCost( + traceBase ++ Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(ForAll), PerItemCost(JitCost(3), JitCost(1), 10), 2), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GE, SBigInt), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(LE, SBigInt), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GE, SBigInt), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(LE, SBigInt) + ) + ) if (lowerMethodCallsInTests) { verifyCases( { Seq( - (Coll[BigInt](), success(true, 38412)), - (Coll[BigInt](BigIntMinusOne), success(false, 38502)), - (Coll[BigInt](BigIntOne), success(true, 38502)), - (Coll[BigInt](BigIntZero, BigIntOne), success(true, 38592)), - (Coll[BigInt](BigIntZero, BigInt11), success(false, 38592)) + (Coll[BigInt](), Expected(Success(true), 38412, costDetails1, 1784)), + (Coll[BigInt](BigIntMinusOne), Expected(Success(false), 38502, costDetails2, 1789)), + (Coll[BigInt](BigIntOne), Expected(Success(true), 38502, costDetails3, 1792)), + (Coll[BigInt](BigIntZero, BigIntOne), Expected(Success(true), 38592, costDetails4, 1799)), + (Coll[BigInt](BigIntZero, BigInt11), Expected(Success(false), 38592, costDetails4, 1799)) ) }, existingFeature( @@ -6139,7 +7028,8 @@ class SigmaDslSpecification extends SigmaDslTesting } else { assertExceptionThrown( verifyCases( - Seq( (Coll[BigInt](), success(true, 38412)) ), + // ignored here also + Seq( (Coll[BigInt](), Expected(Success(true), 38412)) ), existingFeature( { (x: Coll[BigInt]) => x.forall({ (b: BigInt) => if (o.gteq(b, BigIntZero)) o.lteq(b, BigInt10) else false @@ -6159,11 +7049,29 @@ class SigmaDslSpecification extends SigmaDslTesting r <- Gen.choose(l, arr.length - 1) } yield (arr, (l, r)) property("Coll flatMap method equivalence") { + val costDetails1 = TracedCost( + traceBase ++ Array( + FixedCostItem(MethodCall), + FixedCostItem(FuncValue), + FixedCostItem(NamedDesc("MatchSingleArgMethodCall"), FixedCost(JitCost(30))), + SeqCostItem(NamedDesc("CheckFlatmapBody"), PerItemCost(JitCost(20), JitCost(20), 1), 1), + SeqCostItem(MethodDesc(SCollection.FlatMapMethod), PerItemCost(JitCost(60), JitCost(10), 8), 0) + ) + ) + val costDetails1Copy = TracedCost( + traceBase ++ Array( + FixedCostItem(MethodCall), + FixedCostItem(FuncValue), + SeqCostItem(MethodDesc(SCollection.FlatMapMethod), PerItemCost(JitCost(60), JitCost(10), 8), 0) + ) + ) verifyCases( { def success[T](v: T, c: Int) = Expected(Success(v), c) Seq( - Coll[GroupElement]() -> success(Coll[Byte](), 40133), + // TODO mainnet v5.0: Correct behaviour (cost/details are ignored) when exception is thrown? + // handle multiple versions combination resulting in different jit costing - costDetails1(Copy) and cost 1793/1800 (problematic version 2/2) + // Coll[GroupElement]() -> Expected(Success(Coll[Byte]()), 40133, CostDetails.ZeroCost, 1793), Coll[GroupElement]( Helpers.decodeGroupElement("02d65904820f8330218cf7318b3810d0c9ab9df86f1ee6100882683f23c0aee587"), Helpers.decodeGroupElement("0390e9daa9916f30d0bc61a8e381c6005edfb7938aee5bb4fc9e8a759c7748ffaa")) -> @@ -6290,22 +7198,47 @@ class SigmaDslSpecification extends SigmaDslTesting property("Coll patch method equivalence") { val samples = genSamples(collWithRangeGen, MinSuccessful(50)) + def costDetails(i: Int) = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(MethodCall), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + SeqCostItem(MethodDesc(SCollection.PatchMethod), PerItemCost(JitCost(30), JitCost(2), 10), i) + ) + ) + verifyCases( { - def success[T](v: T) = Expected(Success(v), 37514) + def success[T](v: T, cd: CostDetails) = Expected(Success(v), 37514, cd, 1796) Seq( - ((Coll[Int](), (0, 0)), success(Coll[Int]())), - ((Coll[Int](1), (0, 0)), success(Coll[Int](1, 1))), - ((Coll[Int](1), (0, 1)), success(Coll[Int](1))), - ((Coll[Int](1, 2), (0, 0)), success(Coll[Int](1, 2, 1, 2))), - ((Coll[Int](1, 2), (1, 0)), success(Coll[Int](1, 1, 2, 2))), - ((Coll[Int](1, 2), (0, 2)), success(Coll[Int](1, 2))), - ((Coll[Int](1, 2), (0, 3)), success(Coll[Int](1, 2))), - ((Coll[Int](1, 2), (1, 2)), success(Coll[Int](1, 1, 2))), - ((Coll[Int](1, 2), (2, 0)), success(Coll[Int](1, 2, 1, 2))), - ((Coll[Int](1, 2), (3, 0)), success(Coll[Int](1, 2, 1, 2))), - ((Coll[Int](1, 2), (3, 1)), success(Coll[Int](1, 2, 1, 2))), - ((Coll[Int](1, 2), (-1, 1)), success(Coll[Int](1, 2, 2))) + ((Coll[Int](), (0, 0)), success(Coll[Int](), costDetails(0))), + ((Coll[Int](1), (0, 0)), success(Coll[Int](1, 1), costDetails(2))), + ((Coll[Int](1), (0, 1)), success(Coll[Int](1), costDetails(2))), + ((Coll[Int](1, 2), (0, 0)), success(Coll[Int](1, 2, 1, 2), costDetails(4))), + ((Coll[Int](1, 2), (1, 0)), success(Coll[Int](1, 1, 2, 2), costDetails(4))), + ((Coll[Int](1, 2), (0, 2)), success(Coll[Int](1, 2), costDetails(4))), + ((Coll[Int](1, 2), (0, 3)), success(Coll[Int](1, 2), costDetails(4))), + ((Coll[Int](1, 2), (1, 2)), success(Coll[Int](1, 1, 2), costDetails(4))), + ((Coll[Int](1, 2), (2, 0)), success(Coll[Int](1, 2, 1, 2), costDetails(4))), + ((Coll[Int](1, 2), (3, 0)), success(Coll[Int](1, 2, 1, 2), costDetails(4))), + ((Coll[Int](1, 2), (3, 1)), success(Coll[Int](1, 2, 1, 2), costDetails(4))), + ((Coll[Int](1, 2), (-1, 1)), success(Coll[Int](1, 2, 2), costDetails(4))) ) }, existingFeature( @@ -6357,16 +7290,37 @@ class SigmaDslSpecification extends SigmaDslTesting } property("Coll updated method equivalence") { + def costDetails(i: Int) = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(MethodCall), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + SeqCostItem(MethodDesc(SCollection.UpdatedMethod), PerItemCost(JitCost(20), JitCost(1), 10), i) + ) + ) verifyCases( // (coll, (index, elem)) { - def success[T](v: T) = Expected(Success(v), 37180) + def success[T](v: T, cd: CostDetails) = Expected(Success(v), 37180, cd, 1794) Seq( ((Coll[Int](), (0, 0)), Expected(new IndexOutOfBoundsException("0"))), - ((Coll[Int](1), (0, 0)), success(Coll[Int](0))), - ((Coll[Int](1, 2), (0, 0)), success(Coll[Int](0, 2))), - ((Coll[Int](1, 2), (1, 0)), success(Coll[Int](1, 0))), - ((Coll[Int](1, 2, 3), (2, 0)), success(Coll[Int](1, 2, 0))), + ((Coll[Int](1), (0, 0)), success(Coll[Int](0), costDetails(1))), + ((Coll[Int](1, 2), (0, 0)), success(Coll[Int](0, 2), costDetails(2))), + ((Coll[Int](1, 2), (1, 0)), success(Coll[Int](1, 0), costDetails(2))), + ((Coll[Int](1, 2, 3), (2, 0)), success(Coll[Int](1, 2, 0), costDetails(3))), ((Coll[Int](1, 2), (2, 0)), Expected(new IndexOutOfBoundsException("2"))), ((Coll[Int](1, 2), (3, 0)), Expected(new IndexOutOfBoundsException("3"))), ((Coll[Int](1, 2), (-1, 0)), Expected(new IndexOutOfBoundsException("-1"))) @@ -6413,27 +7367,48 @@ class SigmaDslSpecification extends SigmaDslTesting } yield (coll, (is.toColl, vs)), MinSuccessful(20)) + def costDetails(i: Int) = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(MethodCall), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + SeqCostItem(MethodDesc(SCollection.UpdateManyMethod), PerItemCost(JitCost(20), JitCost(2), 10), i) + ) + ) verifyCases( // (coll, (indexes, values)) { - def success[T](v: T) = Expected(Success(v), 37817) + def success[T](v: T, i: Int) = Expected(Success(v), 37817, costDetails(i), 1794) Seq( ((Coll[Int](), (Coll(0), Coll(0))), Expected(new IndexOutOfBoundsException("0"))), ((Coll[Int](), (Coll(0, 1), Coll(0, 0))), Expected(new IndexOutOfBoundsException("0"))), ((Coll[Int](), (Coll(0, 1), Coll(0))), Expected(new IllegalArgumentException("requirement failed: Collections should have same length but was 2 and 1:\n xs=Coll(0,1);\n ys=Coll(0)"))), - ((Coll[Int](1), (Coll(0), Coll(0))), success(Coll[Int](0))), + ((Coll[Int](1), (Coll(0), Coll(0))), success(Coll[Int](0), 1)), ((Coll[Int](1), (Coll(0, 1), Coll(0, 0))), Expected(new IndexOutOfBoundsException("1"))), - ((Coll[Int](1, 2), (Coll(0), Coll(0))), success(Coll[Int](0, 2))), - ((Coll[Int](1, 2), (Coll(0, 1), Coll(0, 0))), success(Coll[Int](0, 0))), + ((Coll[Int](1, 2), (Coll(0), Coll(0))), success(Coll[Int](0, 2), 2)), + ((Coll[Int](1, 2), (Coll(0, 1), Coll(0, 0))), success(Coll[Int](0, 0), 2)), ((Coll[Int](1, 2), (Coll(0, 1, 2), Coll(0, 0, 0))), Expected(new IndexOutOfBoundsException("2"))), - ((Coll[Int](1, 2), (Coll(1), Coll(0))), success(Coll[Int](1, 0))), - ((Coll[Int](1, 2, 3), (Coll(2), Coll(0))), success(Coll[Int](1, 2, 0))), + ((Coll[Int](1, 2), (Coll(1), Coll(0))), success(Coll[Int](1, 0), 2)), + ((Coll[Int](1, 2, 3), (Coll(2), Coll(0))), success(Coll[Int](1, 2, 0), 3)), ((Coll[Int](1, 2), (Coll(2), Coll(0))), Expected(new IndexOutOfBoundsException("2"))), ((Coll[Int](1, 2), (Coll(3), Coll(0))), Expected(new IndexOutOfBoundsException("3"))), ((Coll[Int](1, 2), (Coll(-1), Coll(0))), Expected(new IndexOutOfBoundsException("-1"))), ((Coll[Int](10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140), (Coll[Int](12, 12, 4, 11, 1, 8, 0, 1), Coll[Int](-10, -20, -30, -40, -50, -60, -70, -80))), - success(Coll[Int](-70, -80, 30, 40, -30, 60, 70, 80, -60, 100, 110, -40, -20, 140))) + success(Coll[Int](-70, -80, 30, 40, -30, 60, 70, 80, -60, 100, 110, -40, -20, 140), 14)) ) }, existingFeature( @@ -6510,21 +7485,98 @@ class SigmaDslSpecification extends SigmaDslTesting property("Coll fold method equivalence") { val n = ExactNumeric.IntIsExactNumeric - def success[T](v: T, c: Int) = Expected(Success(v), c) + val costDetails1 = TracedCost( + traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Fold), PerItemCost(JitCost(3), JitCost(1), 10), 0) + ) + ) + val costDetails2 = TracedCost( + traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Fold), PerItemCost(JitCost(3), JitCost(1), 10), 1), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + TypeBasedCostItem(Upcast, SInt), + TypeBasedCostItem(ArithOp.Plus, SInt) + ) + ) + val costDetails3 = TracedCost( + traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Fold), PerItemCost(JitCost(3), JitCost(1), 10), 2), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + TypeBasedCostItem(Upcast, SInt), + TypeBasedCostItem(ArithOp.Plus, SInt), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + TypeBasedCostItem(Upcast, SInt), + TypeBasedCostItem(ArithOp.Plus, SInt) + ) + ) + val costDetails4 = TracedCost( + traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Fold), PerItemCost(JitCost(3), JitCost(1), 10), 3), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + TypeBasedCostItem(Upcast, SInt), + TypeBasedCostItem(ArithOp.Plus, SInt), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + TypeBasedCostItem(Upcast, SInt), + TypeBasedCostItem(ArithOp.Plus, SInt), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + TypeBasedCostItem(Upcast, SInt), + TypeBasedCostItem(ArithOp.Plus, SInt) + ) + ) if (lowerMethodCallsInTests) { verifyCases( // (coll, initState) { Seq( - ((Coll[Byte](), 0), success(0, 41266)), - ((Coll[Byte](), Int.MaxValue), success(Int.MaxValue, 41266)), - ((Coll[Byte](1), Int.MaxValue - 1), success(Int.MaxValue, 41396)), + ((Coll[Byte](), 0), Expected(Success(0), 41266, costDetails1, 1787)), + ((Coll[Byte](), Int.MaxValue), Expected(Success(Int.MaxValue), 41266, costDetails1, 1787)), + ((Coll[Byte](1), Int.MaxValue - 1), Expected(Success(Int.MaxValue), 41396, costDetails2, 1793)), ((Coll[Byte](1), Int.MaxValue), Expected(new ArithmeticException("integer overflow"))), - ((Coll[Byte](-1), Int.MinValue + 1), success(Int.MinValue, 41396)), + ((Coll[Byte](-1), Int.MinValue + 1), Expected(Success(Int.MinValue), 41396, costDetails2, 1793)), ((Coll[Byte](-1), Int.MinValue), Expected(new ArithmeticException("integer overflow"))), - ((Coll[Byte](1, 2), 0), success(3, 41526)), - ((Coll[Byte](1, -1), 0), success(0, 41526)), - ((Coll[Byte](1, -1, 1), 0), success(1, 41656)) + ((Coll[Byte](1, 2), 0), Expected(Success(3), 41526, costDetails3, 1799)), + ((Coll[Byte](1, -1), 0), Expected(Success(0), 41526, costDetails3, 1799)), + ((Coll[Byte](1, -1, 1), 0), Expected(Success(1), 41656, costDetails4, 1805)) ) }, existingFeature( @@ -6555,26 +7607,219 @@ class SigmaDslSpecification extends SigmaDslTesting )), rootCauseLike[CosterException]("Don't know how to evalNode(Lambda(List()") ) - } - } - - property("Coll fold with nested If") { - val n = ExactNumeric.IntIsExactNumeric - def success[T](v: T, c: Int) = Expected(Success(v), c) + } + } + + property("Coll fold with nested If") { + val n = ExactNumeric.IntIsExactNumeric + val costDetails1 = TracedCost( + traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Fold), PerItemCost(JitCost(3), JitCost(1), 10), 0) + ) + ) + val costDetails2 = TracedCost( + traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Fold), PerItemCost(JitCost(3), JitCost(1), 10), 1), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + TypeBasedCostItem(Upcast, SInt), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SInt), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(ValUse), + TypeBasedCostItem(ArithOp.Plus, SInt) + ) + ) + val costDetails3 = TracedCost( + traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Fold), PerItemCost(JitCost(3), JitCost(1), 10), 1), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + TypeBasedCostItem(Upcast, SInt), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SInt), + FixedCostItem(If), + FixedCostItem(ValUse) + ) + ) + val costDetails4 = TracedCost( + traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Fold), PerItemCost(JitCost(3), JitCost(1), 10), 2), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + TypeBasedCostItem(Upcast, SInt), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SInt), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(ValUse), + TypeBasedCostItem(ArithOp.Plus, SInt), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + TypeBasedCostItem(Upcast, SInt), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SInt), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(ValUse), + TypeBasedCostItem(ArithOp.Plus, SInt) + ), + Some(232417L) + ) + val costDetails5 = TracedCost( + traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Fold), PerItemCost(JitCost(3), JitCost(1), 10), 2), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + TypeBasedCostItem(Upcast, SInt), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SInt), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(ValUse), + TypeBasedCostItem(ArithOp.Plus, SInt), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + TypeBasedCostItem(Upcast, SInt), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SInt), + FixedCostItem(If), + FixedCostItem(ValUse) + ) + ) + val costDetails6 = TracedCost( + traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Fold), PerItemCost(JitCost(3), JitCost(1), 10), 3), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + TypeBasedCostItem(Upcast, SInt), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SInt), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(ValUse), + TypeBasedCostItem(ArithOp.Plus, SInt), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + TypeBasedCostItem(Upcast, SInt), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SInt), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 2), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + TypeBasedCostItem(Upcast, SInt), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SInt), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(ValUse), + TypeBasedCostItem(ArithOp.Plus, SInt) + ) + ) if (lowerMethodCallsInTests) { verifyCases( // (coll, initState) { + def success[T](v: T, c: Int) = Expected(Success(v), c) Seq( - ((Coll[Byte](), 0), success(0, 42037)), - ((Coll[Byte](), Int.MaxValue), success(Int.MaxValue, 42037)), - ((Coll[Byte](1), Int.MaxValue - 1), success(Int.MaxValue, 42197)), + ((Coll[Byte](), 0), Expected(Success(0), 42037, costDetails1, 1787)), + ((Coll[Byte](), Int.MaxValue), Expected(Success(Int.MaxValue), 42037, costDetails1, 1787)), + ((Coll[Byte](1), Int.MaxValue - 1), Expected(Success(Int.MaxValue), 42197, costDetails2, 1799)), ((Coll[Byte](1), Int.MaxValue), Expected(new ArithmeticException("integer overflow"))), - ((Coll[Byte](-1), Int.MinValue + 1), success(Int.MinValue + 1, 42197)), - ((Coll[Byte](-1), Int.MinValue), success(Int.MinValue, 42197)), - ((Coll[Byte](1, 2), 0), success(3, 42357)), - ((Coll[Byte](1, -1), 0), success(1, 42357)), - ((Coll[Byte](1, -1, 1), 0), success(2, 42517)) + ((Coll[Byte](-1), Int.MinValue + 1), Expected(Success(Int.MinValue + 1), 42197, costDetails3, 1797)), + ((Coll[Byte](-1), Int.MinValue), Expected(Success(Int.MinValue), 42197, costDetails3, 1797)), + ((Coll[Byte](1, 2), 0), Expected(Success(3), 42357, costDetails4, 1811)), + ((Coll[Byte](1, -1), 0), Expected(Success(1), 42357, costDetails5, 1809)), + ((Coll[Byte](1, -1, 1), 0), Expected(Success(2), 42517, costDetails6, 1821)) ) }, existingFeature( @@ -6596,11 +7841,6 @@ class SigmaDslSpecification extends SigmaDslTesting SelectField.typed[Value[SByte.type]](ValUse(3, SPair(SInt, SByte)), 2.toByte), SInt ) - ), - ValDef( - 6, - List(), - SelectField.typed[Value[SInt.type]](ValUse(3, SPair(SInt, SByte)), 1.toByte) ) ), If( @@ -6626,29 +7866,55 @@ class SigmaDslSpecification extends SigmaDslTesting } property("Coll indexOf method equivalence") { + def costDetails(i: Int) = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(MethodCall), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField) + ) + ++ Array.fill(i)(FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3)))) + :+ SeqCostItem(MethodDesc(SCollection.IndexOfMethod), PerItemCost(JitCost(20), JitCost(10), 2), i) + ) verifyCases( // (coll, (elem: Byte, from: Int)) { - def success[T](v: T) = Expected(Success(v), 37649) + def success0[T](v: T) = Expected(Success(v), 37649, costDetails(0), 1793) + def success1[T](v: T) = Expected(Success(v), 37649, costDetails(1), 1793) + def success2[T](v: T) = Expected(Success(v), 37649, costDetails(2), 1794) + def success3[T](v: T) = Expected(Success(v), 37649, costDetails(3), 1795) + def success12[T](v: T) = Expected(Success(v), 37649, costDetails(12), 1802) Seq( - ((Coll[Byte](), (0.toByte, 0)), success(-1)), - ((Coll[Byte](), (0.toByte, -1)), success(-1)), - ((Coll[Byte](), (0.toByte, 1)), success(-1)), - ((Coll[Byte](1), (0.toByte, 0)), success(-1)), - ((Coll[Byte](1), (1.toByte, 0)), success(0)), - ((Coll[Byte](1), (1.toByte, 1)), success(-1)), - ((Coll[Byte](1, 1), (0.toByte, -1)), success(-1)), - ((Coll[Byte](1, 1), (0.toByte, 0)), success(-1)), - ((Coll[Byte](1, 1), (1.toByte, -1)), success(0)), - ((Coll[Byte](1, 1), (1.toByte, 0)), success(0)), - ((Coll[Byte](1, 1), (1.toByte, 1)), success(1)), - ((Coll[Byte](1, 1), (1.toByte, 2)), success(-1)), - ((Coll[Byte](1, 1), (1.toByte, 3)), success(-1)), - ((Coll[Byte](1, 2, 3), (3.toByte, 0)), success(2)), - ((Coll[Byte](1, 2, 3), (3.toByte, 1)), success(2)), - ((Coll[Byte](1, 2, 3), (3.toByte, 2)), success(2)), - ((Coll[Byte](1, 2, 3), (3.toByte, 3)), success(-1)), - ((Helpers.decodeBytes("8085623fb7cd6b7f01801f00800100"), (0.toByte, -1)), success(11)) + ((Coll[Byte](), (0.toByte, 0)), success0(-1)), + ((Coll[Byte](), (0.toByte, -1)), success0(-1)), + ((Coll[Byte](), (0.toByte, 1)), success0(-1)), + ((Coll[Byte](1), (0.toByte, 0)), success1(-1)), + ((Coll[Byte](1), (1.toByte, 0)), success1(0)), + ((Coll[Byte](1), (1.toByte, 1)), success0(-1)), + ((Coll[Byte](1, 1), (0.toByte, -1)), success2(-1)), + ((Coll[Byte](1, 1), (0.toByte, 0)), success2(-1)), + ((Coll[Byte](1, 1), (1.toByte, -1)), success1(0)), + ((Coll[Byte](1, 1), (1.toByte, 0)), success1(0)), + ((Coll[Byte](1, 1), (1.toByte, 1)), success1(1)), + ((Coll[Byte](1, 1), (1.toByte, 2)), success0(-1)), + ((Coll[Byte](1, 1), (1.toByte, 3)), success0(-1)), + ((Coll[Byte](1, 2, 3), (3.toByte, 0)), success3(2)), + ((Coll[Byte](1, 2, 3), (3.toByte, 1)), success2(2)), + ((Coll[Byte](1, 2, 3), (3.toByte, 2)), success1(2)), + ((Coll[Byte](1, 2, 3), (3.toByte, 3)), success0(-1)), + ((Helpers.decodeBytes("8085623fb7cd6b7f01801f00800100"), (0.toByte, -1)), success12(11)) ) }, existingFeature( @@ -6681,9 +7947,17 @@ class SigmaDslSpecification extends SigmaDslTesting } property("Coll apply method equivalence") { + val costDetails = TracedCost( + traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ByIndex) + ) + ) verifyCases( { - def success[T](v: T) = Expected(Success(v), 36410) + def success[T](v: T) = Expected(Success(v), 36410, costDetails, 1789) Seq( ((Coll[Int](), 0), Expected(new ArrayIndexOutOfBoundsException("0"))), ((Coll[Int](), -1), Expected(new ArrayIndexOutOfBoundsException("-1"))), @@ -6712,11 +7986,31 @@ class SigmaDslSpecification extends SigmaDslTesting property("Coll getOrElse method equivalence") { val default = 10 + val costDetails = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ByIndex) + ) + ) if (lowerMethodCallsInTests) { verifyCases( // (coll, (index, default)) { - def success[T](v: T) = Expected(Success(v), 37020) + def success[T](v: T) = Expected(Success(v), 37020, costDetails, 1793) Seq( ((Coll[Int](), (0, default)), success(default)), ((Coll[Int](), (-1, default)), success(default)), @@ -6819,9 +8113,19 @@ class SigmaDslSpecification extends SigmaDslTesting } property("Tuple size method equivalence") { + val costDetails = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(Constant) + ) + ) verifyCases( { - def success[T](v: T) = Expected(Success(v), 35905) + def success[T](v: T) = Expected(Success(v), 35905, costDetails, 1783) Seq( ((0, 0), success(2)), ((1, 2), success(2)) @@ -6834,8 +8138,9 @@ class SigmaDslSpecification extends SigmaDslTesting property("Tuple apply method equivalence") { val samples = genSamples[(Int, Int)](DefaultMinSuccessful) + val costDetails = TracedCost(traceBase :+ FixedCostItem(SelectField)) verifyCases( - Seq(((1, 2), Expected(Success(1), cost = 36013))), + Seq(((1, 2), Expected(Success(1), cost = 36013, costDetails, 1784))), existingFeature((x: (Int, Int)) => x._1, "{ (x: (Int, Int)) => x(0) }", FuncValue( @@ -6844,7 +8149,7 @@ class SigmaDslSpecification extends SigmaDslTesting )), preGeneratedSamples = Some(samples)) verifyCases( - Seq(((1, 2), Expected(Success(2), cost = 36013))), + Seq(((1, 2), Expected(Success(2), cost = 36013, costDetails, 1784))), existingFeature((x: (Int, Int)) => x._2, "{ (x: (Int, Int)) => x(1) }", FuncValue( @@ -6855,14 +8160,31 @@ class SigmaDslSpecification extends SigmaDslTesting } property("Coll map method equivalence") { + def repeatPlusChunk(i: Int): Array[CostItem] = Array.fill(i){ + Array( + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(ArithOp.Plus, SInt) + ) + }.flatten + def costDetails(i: Int) = TracedCost( + traceBase + ++ Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(MapCollection), PerItemCost(JitCost(20), JitCost(1), 10), i) + ) + ++ repeatPlusChunk(i) + ) + val n = ExactNumeric.IntIsExactNumeric verifyCases( { def success[T](v: T, c: Int) = Expected(Success(v), c) Seq( - (Coll[Int](), success(Coll[Int](), 38886)), - (Coll[Int](1), success(Coll[Int](2), 38936)), - (Coll[Int](1, 2), success(Coll[Int](2, 3), 38986)), + (Coll[Int](), Expected(Success(Coll[Int]()), 38886, costDetails(0), 1788)), + (Coll[Int](1), Expected(Success(Coll[Int](2)), 38936, costDetails(1), 1791)), + (Coll[Int](1, 2), Expected(Success(Coll[Int](2, 3)), 38986, costDetails(2), 1794)), (Coll[Int](1, 2, Int.MaxValue), Expected(new ArithmeticException("integer overflow"))) ) }, @@ -6878,15 +8200,71 @@ class SigmaDslSpecification extends SigmaDslTesting } property("Coll map with nested if") { + val costDetails1 = TracedCost( + traceBase ++ Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(MapCollection), PerItemCost(JitCost(20), JitCost(1), 10), 0) + ) + ) + val costDetails2 = TracedCost( + traceBase ++ Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(MapCollection), PerItemCost(JitCost(20), JitCost(1), 10), 1), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SInt), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(ArithOp.Plus, SInt) + ) + ) + val costDetails3 = TracedCost( + traceBase ++ Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(MapCollection), PerItemCost(JitCost(20), JitCost(1), 10), 1), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SInt), + FixedCostItem(If), + FixedCostItem(Constant), + FixedCostItem(ValUse), + TypeBasedCostItem(ArithOp.Multiply, SInt) + ) + ) + val costDetails4 = TracedCost( + traceBase ++ Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(MapCollection), PerItemCost(JitCost(20), JitCost(1), 10), 2), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SInt), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(ArithOp.Plus, SInt), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SInt), + FixedCostItem(If), + FixedCostItem(Constant), + FixedCostItem(ValUse), + TypeBasedCostItem(ArithOp.Multiply, SInt) + ) + ) val n = ExactNumeric.IntIsExactNumeric verifyCases( { def success[T](v: T, c: Int) = Expected(Success(v), c) Seq( - (Coll[Int](), success(Coll[Int](), 39571)), - (Coll[Int](1), success(Coll[Int](2), 39671)), - (Coll[Int](-1), success(Coll[Int](1), 39671)), - (Coll[Int](1, -2), success(Coll[Int](2, 2), 39771)), + (Coll[Int](), Expected(Success(Coll[Int]()), 39571, costDetails1, 1788)), + (Coll[Int](1), Expected(Success(Coll[Int](2)), 39671, costDetails2, 1795)), + (Coll[Int](-1), Expected(Success(Coll[Int](1)), 39671, costDetails3, 1795)), + (Coll[Int](1, -2), Expected(Success(Coll[Int](2, 2)), 39771, costDetails4, 1802)), (Coll[Int](1, 2, Int.MaxValue), Expected(new ArithmeticException("integer overflow"))), (Coll[Int](1, 2, Int.MinValue), Expected(new ArithmeticException("integer overflow"))) ) @@ -6911,16 +8289,35 @@ class SigmaDslSpecification extends SigmaDslTesting } property("Coll filter") { + def costDetails(i: Int) = { + val gtChunk = Array.fill(i)( + Array( + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SInt) + ) + ).flatten + + TracedCost( + traceBase ++ + Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Filter), PerItemCost(JitCost(20), JitCost(1), 10), i) + ) ++ + gtChunk + ) + } + val o = ExactOrdering.IntIsExactOrdering verifyCases( { - def success[T](v: T, c: Int) = Expected(Success(v), c) Seq( - (Coll[Int](), success(Coll[Int](), 37223)), - (Coll[Int](1), success(Coll[Int](1), 37273)), - (Coll[Int](1, 2), success(Coll[Int](1, 2), 37323)), - (Coll[Int](1, 2, -1), success(Coll[Int](1, 2), 37373)), - (Coll[Int](1, -1, 2, -2), success(Coll[Int](1, 2), 37423)) + (Coll[Int](), Expected(Success(Coll[Int]()), 37223, costDetails(0), 1788)), + (Coll[Int](1), Expected(Success(Coll[Int](1)), 37273, costDetails(1), 1791)), + (Coll[Int](1, 2), Expected(Success(Coll[Int](1, 2)), 37323, costDetails(2), 1795)), + (Coll[Int](1, 2, -1), Expected(Success(Coll[Int](1, 2)), 37373, costDetails(3), 1798)), + (Coll[Int](1, -1, 2, -2), Expected(Success(Coll[Int](1, 2)), 37423, costDetails(4), 1802)) ) }, existingFeature((x: Coll[Int]) => x.filter({ (v: Int) => o.gt(v, 0) }), @@ -6935,17 +8332,69 @@ class SigmaDslSpecification extends SigmaDslTesting } property("Coll filter with nested If") { + val leftBranch = Array( + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SInt), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(LT, SInt) + ) + val rightBranch = Array( + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SInt), + FixedCostItem(If), + FixedCostItem(Constant) + ) + def repeatLeftBranch(i: Int) = Array.fill(i)(leftBranch).flatten + + def costDetails(i: Int) = { + TracedCost( + traceBase ++ + Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Filter), PerItemCost(JitCost(20), JitCost(1), 10), i) + ) ++ + repeatLeftBranch(i) + ) + } + val costDetails3 = TracedCost( + traceBase ++ + Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Filter), PerItemCost(JitCost(20), JitCost(1), 10), 3) + ) ++ + repeatLeftBranch(2) ++ + rightBranch + ) + val costDetails5 = TracedCost( + traceBase ++ + Array( + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(Filter), PerItemCost(JitCost(20), JitCost(1), 10), 5) + ) ++ + leftBranch ++ + rightBranch ++ + leftBranch ++ + rightBranch ++ + leftBranch + ) + val o = ExactOrdering.IntIsExactOrdering verifyCases( { def success[T](v: T, c: Int) = Expected(Success(v), c) Seq( - (Coll[Int](), success(Coll[Int](), 37797)), - (Coll[Int](1), success(Coll[Int](1), 37887)), - (Coll[Int](10), success(Coll[Int](), 37887)), - (Coll[Int](1, 2), success(Coll[Int](1, 2), 37977)), - (Coll[Int](1, 2, 0), success(Coll[Int](1, 2), 38067)), - (Coll[Int](1, -1, 2, -2, 11), success(Coll[Int](1, 2), 38247)) + (Coll[Int](), Expected(Success(Coll[Int]()), 37797, costDetails(0), 1788)), + (Coll[Int](1), Expected(Success(Coll[Int](1)), 37887, costDetails(1), 1795)), + (Coll[Int](10), Expected(Success(Coll[Int]()), 37887, costDetails(1), 1795)), + (Coll[Int](1, 2), Expected(Success(Coll[Int](1, 2)), 37977, costDetails(2), 1803)), + (Coll[Int](1, 2, 0), Expected(Success(Coll[Int](1, 2)), 38067, costDetails3, 1808)), + (Coll[Int](1, -1, 2, -2, 11), Expected(Success(Coll[Int](1, 2)), 38247, costDetails5, 1820)) ) }, existingFeature((x: Coll[Int]) => x.filter({ (v: Int) => if (o.gt(v, 0)) v < 10 else false }), @@ -6962,6 +8411,7 @@ class SigmaDslSpecification extends SigmaDslTesting ))) } + // TODO mainnet v5.0: NoSuchMethodException thrown inside the test. property("Coll slice method equivalence") { val samples = genSamples(collWithRangeGen, DefaultMinSuccessful) if (lowerMethodCallsInTests) { @@ -7021,21 +8471,29 @@ class SigmaDslSpecification extends SigmaDslTesting } property("Coll.append equivalence") { + def costDetails(i: Int) = TracedCost( + traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + SeqCostItem(CompanionDesc(Append), PerItemCost(JitCost(20), JitCost(2), 100), i) + ) + ) if (lowerMethodCallsInTests) { verifyCases( { - def success[T](v: T) = Expected(Success(v), 37765) + def success[T](v: T, size: Int) = Expected(Success(v), 37765, costDetails(size), 1790) val arr1 = Gen.listOfN(100, arbitrary[Int]).map(_.toArray).sample.get val arr2 = Gen.listOfN(200, arbitrary[Int]).map(_.toArray).sample.get Seq( - (Coll[Int](), Coll[Int]()) -> success(Coll[Int]()), - (Coll[Int](), Coll[Int](1)) -> success(Coll[Int](1)), - (Coll[Int](1), Coll[Int]()) -> success(Coll[Int](1)), - (Coll[Int](1), Coll[Int](2)) -> success(Coll[Int](1, 2)), - (Coll[Int](1), Coll[Int](2, 3)) -> success(Coll[Int](1, 2, 3)), - (Coll[Int](1, 2), Coll[Int](3)) -> success(Coll[Int](1, 2, 3)), - (Coll[Int](1, 2), Coll[Int](3, 4)) -> success(Coll[Int](1, 2, 3, 4)), - (Coll[Int](arr1:_*), Coll[Int](arr2:_*)) -> Expected(Success(Coll[Int](arr1 ++ arr2:_*)), 37785) + (Coll[Int](), Coll[Int]()) -> success(Coll[Int](), 0), + (Coll[Int](), Coll[Int](1)) -> success(Coll[Int](1), 1), + (Coll[Int](1), Coll[Int]()) -> success(Coll[Int](1), 1), + (Coll[Int](1), Coll[Int](2)) -> success(Coll[Int](1, 2), 2), + (Coll[Int](1), Coll[Int](2, 3)) -> success(Coll[Int](1, 2, 3), 3), + (Coll[Int](1, 2), Coll[Int](3)) -> success(Coll[Int](1, 2, 3), 3), + (Coll[Int](1, 2), Coll[Int](3, 4)) -> success(Coll[Int](1, 2, 3, 4), 4), + (Coll[Int](arr1:_*), Coll[Int](arr2:_*)) -> Expected(Success(Coll[Int](arr1 ++ arr2:_*)), 37785, costDetails(300), 1791) ) }, existingFeature( @@ -7068,37 +8526,80 @@ class SigmaDslSpecification extends SigmaDslTesting } property("Option methods equivalence") { - def success[T](v: T, c: Int) = Expected(Success(v), c) + val costDetails1 = TracedCost(traceBase :+ FixedCostItem(OptionGet)) + val costDetails2 = TracedCost(traceBase :+ FixedCostItem(OptionIsDefined)) + val costDetails3 = TracedCost( + traceBase ++ Array( + FixedCostItem(Constant), + FixedCostItem(OptionGetOrElse) + ) + ) + val costDetails4 = TracedCost( + traceBase ++ Array( + FixedCostItem(MethodCall), + FixedCostItem(FuncValue), + FixedCostItem(SOption.FilterMethod, FixedCost(JitCost(20))) + ) + ) + val costDetails5 = TracedCost( + traceBase ++ Array( + FixedCostItem(MethodCall), + FixedCostItem(FuncValue), + FixedCostItem(SOption.FilterMethod, FixedCost(JitCost(20))), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))) + ) + ) + val costDetails6 = TracedCost( + traceBase ++ Array( + FixedCostItem(MethodCall), + FixedCostItem(FuncValue), + FixedCostItem(SOption.MapMethod, FixedCost(JitCost(20))) + ) + ) + val costDetails7 = TracedCost( + traceBase ++ Array( + FixedCostItem(MethodCall), + FixedCostItem(FuncValue), + FixedCostItem(SOption.MapMethod, FixedCost(JitCost(20))), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(ArithOp.Plus, SLong) + ) + ) verifyCases( Seq( (None -> Expected(new NoSuchElementException("None.get"))), - (Some(10L) -> success(10L, 36046))), + (Some(10L) -> Expected(Success(10L), 36046, costDetails1, 1785))), existingFeature({ (x: Option[Long]) => x.get }, "{ (x: Option[Long]) => x.get }", FuncValue(Vector((1, SOption(SLong))), OptionGet(ValUse(1, SOption(SLong)))))) verifyCases( Seq( - (None -> success(false, 36151)), - (Some(10L) -> success(true, 36151))), + (None -> Expected(Success(false), 36151, costDetails2, 1784)), + (Some(10L) -> Expected(Success(true), 36151, costDetails2, 1784))), existingFeature({ (x: Option[Long]) => x.isDefined }, "{ (x: Option[Long]) => x.isDefined }", FuncValue(Vector((1, SOption(SLong))), OptionIsDefined(ValUse(1, SOption(SLong)))))) verifyCases( Seq( - (None -> success(1L, 36367)), - (Some(10L) -> success(10L, 36367))), + (None -> Expected(Success(1L), 36367, costDetails3, 1786)), + (Some(10L) -> Expected(Success(10L), 36367, costDetails3, 1786))), existingFeature({ (x: Option[Long]) => x.getOrElse(1L) }, "{ (x: Option[Long]) => x.getOrElse(1L) }", FuncValue(Vector((1, SOption(SLong))), OptionGetOrElse(ValUse(1, SOption(SLong)), LongConstant(1L))))) verifyCases( Seq( - (None -> success(None, 38239)), - (Some(10L) -> success(None, 38239)), - (Some(1L) -> success(Some(1L), 38239))), + (None -> Expected(Success(None), 38239, costDetails4, 1786)), + (Some(10L) -> Expected(Success(None), 38239, costDetails5, 1788)), + (Some(1L) -> Expected(Success(Some(1L)), 38239, costDetails5, 1789))), existingFeature({ (x: Option[Long]) => x.filter({ (v: Long) => v == 1} ) }, "{ (x: Option[Long]) => x.filter({ (v: Long) => v == 1 }) }", FuncValue( @@ -7114,8 +8615,8 @@ class SigmaDslSpecification extends SigmaDslTesting val n = ExactNumeric.LongIsExactNumeric verifyCases( Seq( - (None -> success(None, 38575)), - (Some(10L) -> success(Some(11L), 38575)), + (None -> Expected(Success(None), 38575, costDetails6, 1786)), + (Some(10L) -> Expected(Success(Some(11L)), 38575, costDetails7, 1790)), (Some(Long.MaxValue) -> Expected(new ArithmeticException("long overflow")))), existingFeature({ (x: Option[Long]) => x.map( (v: Long) => n.plus(v, 1) ) }, "{ (x: Option[Long]) => x.map({ (v: Long) => v + 1 }) }", @@ -7138,15 +8639,49 @@ class SigmaDslSpecification extends SigmaDslTesting } property("Option filter,map with nested If") { - def success[T](v: T, c: Int) = Expected(Success(v), c) + val costDetails1 = TracedCost( + traceBase ++ Array( + FixedCostItem(MethodCall), + FixedCostItem(FuncValue), + FixedCostItem(SOption.FilterMethod, FixedCost(JitCost(20))) + ) + ) + val costDetails2 = TracedCost( + traceBase ++ Array( + FixedCostItem(MethodCall), + FixedCostItem(FuncValue), + FixedCostItem(SOption.FilterMethod, FixedCost(JitCost(20))), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SLong), + FixedCostItem(If), + FixedCostItem(Constant) + ) + ) + val costDetails3 = TracedCost( + traceBase ++ Array( + FixedCostItem(MethodCall), + FixedCostItem(FuncValue), + FixedCostItem(SOption.FilterMethod, FixedCost(JitCost(20))), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(GT, SLong), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(LE, SLong) + ) + ) val o = ExactOrdering.LongIsExactOrdering verifyCases( Seq( - (None -> success(None, 38736)), - (Some(0L) -> success(None, 38736)), - (Some(10L) -> success(Some(10L), 38736)), - (Some(11L) -> success(None, 38736))), + (None -> Expected(Success(None), 38736, costDetails1, 1786)), + (Some(0L) -> Expected(Success(None), 38736, costDetails2, 1791)), + (Some(10L) -> Expected(Success(Some(10L)), 38736, costDetails3, 1794)), + (Some(11L) -> Expected(Success(None), 38736, costDetails3, 1794))), existingFeature( { (x: Option[Long]) => x.filter({ (v: Long) => if (o.gt(v, 0L)) v <= 10 else false } ) }, "{ (x: Option[Long]) => x.filter({ (v: Long) => if (v > 0) v <= 10 else false }) }", @@ -7169,13 +8704,48 @@ class SigmaDslSpecification extends SigmaDslTesting ) ))) + val costDetails4 = TracedCost( + traceBase ++ Array( + FixedCostItem(MethodCall), + FixedCostItem(FuncValue), + FixedCostItem(SOption.MapMethod, FixedCost(JitCost(20))) + ) + ) + val costDetails5 = TracedCost( + traceBase ++ Array( + FixedCostItem(MethodCall), + FixedCostItem(FuncValue), + FixedCostItem(SOption.MapMethod, FixedCost(JitCost(20))), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(LT, SLong), + FixedCostItem(If), + FixedCostItem(ValUse) + ) + ) + val costDetails6 = TracedCost( + traceBase ++ Array( + FixedCostItem(MethodCall), + FixedCostItem(FuncValue), + FixedCostItem(SOption.MapMethod, FixedCost(JitCost(20))), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(LT, SLong), + FixedCostItem(If), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(ArithOp.Minus, SLong) + ) + ) val n = ExactNumeric.LongIsExactNumeric verifyCases( Seq( - (None -> success(None, 39077)), - (Some(0L) -> success(Some(0L), 39077)), - (Some(10L) -> success(Some(10L), 39077)), - (Some(-1L) -> success(Some(-2L), 39077)), + (None -> Expected(Success(None), 39077, costDetails4, 1786)), + (Some(0L) -> Expected(Success(Some(0L)), 39077, costDetails5, 1792)), + (Some(10L) -> Expected(Success(Some(10L)), 39077, costDetails5, 1792)), + (Some(-1L) -> Expected(Success(Some(-2L)), 39077, costDetails6, 1794)), (Some(Long.MinValue) -> Expected(new ArithmeticException("long overflow")))), existingFeature( { (x: Option[Long]) => x.map( (v: Long) => if (o.lt(v, 0)) n.minus(v, 1) else v ) }, @@ -7217,6 +8787,13 @@ class SigmaDslSpecification extends SigmaDslTesting } property("Option fold workaround method") { + val costDetails1 = TracedCost( + traceBase ++ Array( + FixedCostItem(MethodCall), + FixedCostItem(FuncValue), + FixedCostItem(SOption.FilterMethod, FixedCost(JitCost(20))) + ) + ) val n = ExactNumeric.LongIsExactNumeric verifyCases( Seq( @@ -7289,22 +8866,29 @@ class SigmaDslSpecification extends SigmaDslTesting } property("blake2b256, sha256 equivalence") { - def success[T](v: T, c: Int) = Expected(Success(v), c) + def costDetailsBlake(i: Int) = TracedCost(traceBase :+ SeqCostItem(CompanionDesc(CalcBlake2b256), PerItemCost(JitCost(20), JitCost(7), 128), i)) + def costDetailsSha(i: Int) = TracedCost(traceBase :+ SeqCostItem(CompanionDesc(CalcSha256), PerItemCost(JitCost(80), JitCost(8), 64), i)) verifyCases( Seq( - Coll[Byte]() -> - success( - Helpers.decodeBytes("0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8"), - 36269), - Helpers.decodeBytes("e0ff0105ffffac31010017ff33") -> - success( - Helpers.decodeBytes("33707eed9aab64874ff2daa6d6a378f61e7da36398fb36c194c7562c9ff846b5"), - 36269), - Colls.replicate(1024, 1.toByte) -> - success( - Helpers.decodeBytes("45d8456fc5d41d1ec1124cb92e41192c1c3ec88f0bf7ae2dc6e9cf75bec22045"), - 36369) + Coll[Byte]() -> Expected( + Success(Helpers.decodeBytes("0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8")), + 36269, + costDetailsBlake(0), + 1788 + ), + Helpers.decodeBytes("e0ff0105ffffac31010017ff33") -> Expected( + Success(Helpers.decodeBytes("33707eed9aab64874ff2daa6d6a378f61e7da36398fb36c194c7562c9ff846b5")), + 36269, + costDetailsBlake(13), + 1788 + ), + Colls.replicate(1024, 1.toByte) -> Expected( + Success(Helpers.decodeBytes("45d8456fc5d41d1ec1124cb92e41192c1c3ec88f0bf7ae2dc6e9cf75bec22045")), + 36369, + costDetailsBlake(1024), + 1793 + ) ), existingFeature((x: Coll[Byte]) => SigmaDsl.blake2b256(x), "{ (x: Coll[Byte]) => blake2b256(x) }", @@ -7312,18 +8896,24 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( Seq( - Coll[Byte]() -> - success( - Helpers.decodeBytes("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"), - 36393), - Helpers.decodeBytes("e0ff0105ffffac31010017ff33") -> - success( - Helpers.decodeBytes("367d0ec2cdc14aac29d5beb60c2bfc86d5a44a246308659af61c1b85fa2ca2cc"), - 36393), - Colls.replicate(1024, 1.toByte) -> - success( - Helpers.decodeBytes("5a648d8015900d89664e00e125df179636301a2d8fa191c1aa2bd9358ea53a69"), - 36493) + Coll[Byte]() -> Expected( + Success(Helpers.decodeBytes("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")), + 36393, + costDetailsSha(0), + 1794 + ), + Helpers.decodeBytes("e0ff0105ffffac31010017ff33") -> Expected( + Success(Helpers.decodeBytes("367d0ec2cdc14aac29d5beb60c2bfc86d5a44a246308659af61c1b85fa2ca2cc")), + 36393, + costDetailsSha(13), + 1794 + ), + Colls.replicate(1024, 1.toByte) -> Expected( + Success(Helpers.decodeBytes("5a648d8015900d89664e00e125df179636301a2d8fa191c1aa2bd9358ea53a69")), + 36493, + costDetailsSha(1024), + 1806 + ) ), existingFeature((x: Coll[Byte]) => SigmaDsl.sha256(x), "{ (x: Coll[Byte]) => sha256(x) }", @@ -7331,17 +8921,26 @@ class SigmaDslSpecification extends SigmaDslTesting } property("sigmaProp equivalence") { + val costDetails = TracedCost(traceBase :+ FixedCostItem(BoolToSigmaProp)) verifyCases( Seq( - (false, Expected(Success(CSigmaProp(TrivialProp.FalseProp)), 35892)), - (true, Expected(Success(CSigmaProp(TrivialProp.TrueProp)), 35892))), + (false, Expected(Success(CSigmaProp(TrivialProp.FalseProp)), 35892, costDetails, 1785)), + (true , Expected(Success(CSigmaProp(TrivialProp.TrueProp )), 35892, costDetails, 1785))), existingFeature((x: Boolean) => sigmaProp(x), "{ (x: Boolean) => sigmaProp(x) }", FuncValue(Vector((1, SBoolean)), BoolToSigmaProp(ValUse(1, SBoolean))))) } property("atLeast equivalence") { - def success[T](v: T) = Expected(Success(v), 36462) + def costDetails(i: Int) = TracedCost( + traceBase ++ Array( + FixedCostItem(SizeOf), + FixedCostItem(Constant), + TypeBasedCostItem(ArithOp.Minus, SInt), + FixedCostItem(ValUse), + SeqCostItem(CompanionDesc(AtLeast), PerItemCost(JitCost(20), JitCost(3), 5), i) + ) + ) verifyCases( Seq( @@ -7353,7 +8952,7 @@ class SigmaDslSpecification extends SigmaDslTesting Helpers.decodeECPoint("02614b14a8c6c6b4b7ce017d72fbca7f9218b72c16bdd88f170ffb300b106b9014"), Helpers.decodeECPoint("034cc5572276adfa3e283a3f1b0f0028afaadeaa362618c5ec43262d8cefe7f004") ) - )) -> success(CSigmaProp(TrivialProp.TrueProp)), + )) -> Expected(Success(CSigmaProp(TrivialProp.TrueProp)), 36462, costDetails(1), 1790), Coll[SigmaProp]( CSigmaProp( ProveDHTuple( @@ -7365,7 +8964,7 @@ class SigmaDslSpecification extends SigmaDslTesting ), CSigmaProp(ProveDlog(Helpers.decodeECPoint("03f7eacae7476a9ef082513a6a70ed6b208aafad0ade5f614ac6cfa2176edd0d69"))), CSigmaProp(ProveDlog(Helpers.decodeECPoint("023bddd50b917388cd2c4f478f3ea9281bf03a252ee1fefe9c79f800afaa8d86ad"))) - ) -> success( + ) -> Expected(Success( CSigmaProp( CTHRESHOLD( 2, @@ -7381,7 +8980,7 @@ class SigmaDslSpecification extends SigmaDslTesting ) ) ) - ), + ), 36462, costDetails(3), 1893), Colls.replicate[SigmaProp](AtLeast.MaxChildrenCount + 1, CSigmaProp(TrivialProp.TrueProp)) -> Expected(new IllegalArgumentException("Expected input elements count should not exceed 255, actual: 256")) ), @@ -7397,10 +8996,23 @@ class SigmaDslSpecification extends SigmaDslTesting } property("&& sigma equivalence") { + val testTraceBase = traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField) + ) + + val costDetails1 = TracedCost(testTraceBase :+ SeqCostItem(CompanionDesc(SigmaAnd), PerItemCost(JitCost(10), JitCost(2), 1), 2)) + val costDetails2 = TracedCost( + testTraceBase ++ Array( + FixedCostItem(BoolToSigmaProp), + SeqCostItem(CompanionDesc(SigmaAnd), PerItemCost(JitCost(10), JitCost(2), 1), 2) + ) + ) verifyCases( { - def success[T](v: T) = Expected(Success(v), 36428) + def success[T](v: T, newCost: Int) = Expected(Success(v), 36428, costDetails1, newCost) Seq( (CSigmaProp(ProveDlog(Helpers.decodeECPoint("02ea9bf6da7f512386c6ca509d40f8c5e7e0ffb3eea5dc3c398443ea17f4510798"))), CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606")))) -> @@ -7412,20 +9024,19 @@ class SigmaDslSpecification extends SigmaDslTesting ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606")) ) ) - ) - ), + ), 1822), (CSigmaProp(TrivialProp.TrueProp), CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606")))) -> - success(CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606")))), + success(CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606"))), 1804), (CSigmaProp(TrivialProp.FalseProp), CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606")))) -> - success(CSigmaProp(TrivialProp.FalseProp)), + success(CSigmaProp(TrivialProp.FalseProp), 1787), (CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606"))), CSigmaProp(TrivialProp.TrueProp)) -> - success(CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606")))), + success(CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606"))), 1804), (CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606"))), CSigmaProp(TrivialProp.FalseProp)) -> - success(CSigmaProp(TrivialProp.FalseProp)) + success(CSigmaProp(TrivialProp.FalseProp), 1787) ) }, existingFeature( @@ -7443,12 +9054,11 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - def success[T](v: T) = Expected(Success(v), 36522) Seq( (CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606"))), true) -> - success(CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606")))), + Expected(Success(CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606")))), 36522, costDetails2, 1806), (CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606"))), false) -> - success(CSigmaProp(TrivialProp.FalseProp)) + Expected(Success(CSigmaProp(TrivialProp.FalseProp)), 36522, costDetails2, 1789) ) }, existingFeature( @@ -7468,9 +9078,16 @@ class SigmaDslSpecification extends SigmaDslTesting } property("|| sigma equivalence") { + val testTraceBase = traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField) + ) + + val costDetails1 = TracedCost(testTraceBase :+ SeqCostItem(CompanionDesc(SigmaOr), PerItemCost(JitCost(10), JitCost(2), 1), 2)) verifyCases( { - def success[T](v: T) = Expected(Success(v), 36494) + def success[T](v: T, newCost: Int) = Expected(Success(v), 36494, costDetails1, newCost) Seq( (CSigmaProp(ProveDlog(Helpers.decodeECPoint("02ea9bf6da7f512386c6ca509d40f8c5e7e0ffb3eea5dc3c398443ea17f4510798"))), CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606")))) -> @@ -7482,20 +9099,20 @@ class SigmaDslSpecification extends SigmaDslTesting ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606")) ) ) - ) - ), + ), + 1822), (CSigmaProp(TrivialProp.FalseProp), CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606")))) -> - success(CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606")))), + success(CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606"))), 1804), (CSigmaProp(TrivialProp.TrueProp), CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606")))) -> - success(CSigmaProp(TrivialProp.TrueProp)), + success(CSigmaProp(TrivialProp.TrueProp), 1787), (CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606"))), CSigmaProp(TrivialProp.FalseProp)) -> - success(CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606")))), + success(CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606"))), 1804), (CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606"))), CSigmaProp(TrivialProp.TrueProp)) -> - success(CSigmaProp(TrivialProp.TrueProp)) + success(CSigmaProp(TrivialProp.TrueProp), 1787) ) }, existingFeature( @@ -7511,14 +9128,20 @@ class SigmaDslSpecification extends SigmaDslTesting ) ))) + val costDetails2 = TracedCost( + testTraceBase ++ Array( + FixedCostItem(BoolToSigmaProp), + SeqCostItem(CompanionDesc(SigmaOr), PerItemCost(JitCost(10), JitCost(2), 1), 2) + ) + ) verifyCases( { - def success[T](v: T) = Expected(Success(v), 36588) + def success[T](v: T, newCost: Int) = Expected(Success(v), 36588, costDetails2, newCost) Seq( (CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606"))), false) -> - success(CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606")))), + success(CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606"))), 1806), (CSigmaProp(ProveDlog(Helpers.decodeECPoint("03a426a66fc1af2792b35d9583904c3fb877b49ae5cea45b7a2aa105ffa4c68606"))), true) -> - success(CSigmaProp(TrivialProp.TrueProp)) + success(CSigmaProp(TrivialProp.TrueProp), 1789) ) }, existingFeature( @@ -7540,17 +9163,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("SigmaProp.propBytes equivalence") { verifyCases( { - def newCost(nItems: Int) = TracedCost( - Array( - FixedCostItem(Apply), - FixedCostItem(FuncValue), - FixedCostItem(GetVar), - FixedCostItem(OptionGet), - FixedCostItem(FuncValue.AddToEnvironmentDesc, FuncValue.AddToEnvironmentDesc_CostKind), - FixedCostItem(ValUse), - SeqCostItem(SigmaPropBytes, nItems) - ) - ) + def newDetails(nItems: Int) = TracedCost(traceBase :+ SeqCostItem(SigmaPropBytes, nItems)) def pk = ProveDlog(Helpers.decodeECPoint("039d0b1e46c21540d033143440d2fb7dd5d650cf89981c99ee53c6e0374d2b1b6f")) def dht = ProveDHTuple( Helpers.decodeECPoint("03c046fccb95549910767d0543f5e8ce41d66ae6a8720a46f4049cac3b3d26dafb"), @@ -7566,25 +9179,25 @@ class SigmaDslSpecification extends SigmaDslTesting Helpers.decodeBytes( "0008ce03c046fccb95549910767d0543f5e8ce41d66ae6a8720a46f4049cac3b3d26dafb023479c9c3b86a0d3c8be3db0a2d186788e9af1db76d55f3dad127d15185d83d0303d7898641cb6653585a8e1dabfa7f665e61e0498963e329e6e3744bd764db2d72037ae057d89ec0b46ff8e9ff4c37e85c12acddb611c3f636421bef1542c11b0441" ) - ), cost = 35902, newCost(4)), + ), cost = 35902, newDetails(4), expectedNewCost = 1791), CSigmaProp(pk) -> Expected(Success( Helpers.decodeBytes("0008cd039d0b1e46c21540d033143440d2fb7dd5d650cf89981c99ee53c6e0374d2b1b6f")), - cost = 35902, newCost(1)), + cost = 35902, newDetails(1), expectedNewCost = 1789), CSigmaProp(and) -> Expected(Success( Helpers.decodeBytes( "00089602cd039d0b1e46c21540d033143440d2fb7dd5d650cf89981c99ee53c6e0374d2b1b6fce03c046fccb95549910767d0543f5e8ce41d66ae6a8720a46f4049cac3b3d26dafb023479c9c3b86a0d3c8be3db0a2d186788e9af1db76d55f3dad127d15185d83d0303d7898641cb6653585a8e1dabfa7f665e61e0498963e329e6e3744bd764db2d72037ae057d89ec0b46ff8e9ff4c37e85c12acddb611c3f636421bef1542c11b0441" ) - ), cost = 35902, newCost(6)), + ), cost = 35902, newDetails(6), expectedNewCost = 1792), CSigmaProp(threshold) -> Expected(Success( Helpers.decodeBytes( "0008980204cd039d0b1e46c21540d033143440d2fb7dd5d650cf89981c99ee53c6e0374d2b1b6fce03c046fccb95549910767d0543f5e8ce41d66ae6a8720a46f4049cac3b3d26dafb023479c9c3b86a0d3c8be3db0a2d186788e9af1db76d55f3dad127d15185d83d0303d7898641cb6653585a8e1dabfa7f665e61e0498963e329e6e3744bd764db2d72037ae057d89ec0b46ff8e9ff4c37e85c12acddb611c3f636421bef1542c11b04419702cd039d0b1e46c21540d033143440d2fb7dd5d650cf89981c99ee53c6e0374d2b1b6fce03c046fccb95549910767d0543f5e8ce41d66ae6a8720a46f4049cac3b3d26dafb023479c9c3b86a0d3c8be3db0a2d186788e9af1db76d55f3dad127d15185d83d0303d7898641cb6653585a8e1dabfa7f665e61e0498963e329e6e3744bd764db2d72037ae057d89ec0b46ff8e9ff4c37e85c12acddb611c3f636421bef1542c11b04419602cd039d0b1e46c21540d033143440d2fb7dd5d650cf89981c99ee53c6e0374d2b1b6fce03c046fccb95549910767d0543f5e8ce41d66ae6a8720a46f4049cac3b3d26dafb023479c9c3b86a0d3c8be3db0a2d186788e9af1db76d55f3dad127d15185d83d0303d7898641cb6653585a8e1dabfa7f665e61e0498963e329e6e3744bd764db2d72037ae057d89ec0b46ff8e9ff4c37e85c12acddb611c3f636421bef1542c11b0441" ) - ), cost = 35902, newCost(18)), + ), cost = 35902, newDetails(18), expectedNewCost = 1800), CSigmaProp(COR(Array(pk, dht, and, or, threshold))) -> Expected(Success( Helpers.decodeBytes( "00089705cd039d0b1e46c21540d033143440d2fb7dd5d650cf89981c99ee53c6e0374d2b1b6fce03c046fccb95549910767d0543f5e8ce41d66ae6a8720a46f4049cac3b3d26dafb023479c9c3b86a0d3c8be3db0a2d186788e9af1db76d55f3dad127d15185d83d0303d7898641cb6653585a8e1dabfa7f665e61e0498963e329e6e3744bd764db2d72037ae057d89ec0b46ff8e9ff4c37e85c12acddb611c3f636421bef1542c11b04419602cd039d0b1e46c21540d033143440d2fb7dd5d650cf89981c99ee53c6e0374d2b1b6fce03c046fccb95549910767d0543f5e8ce41d66ae6a8720a46f4049cac3b3d26dafb023479c9c3b86a0d3c8be3db0a2d186788e9af1db76d55f3dad127d15185d83d0303d7898641cb6653585a8e1dabfa7f665e61e0498963e329e6e3744bd764db2d72037ae057d89ec0b46ff8e9ff4c37e85c12acddb611c3f636421bef1542c11b04419702cd039d0b1e46c21540d033143440d2fb7dd5d650cf89981c99ee53c6e0374d2b1b6fce03c046fccb95549910767d0543f5e8ce41d66ae6a8720a46f4049cac3b3d26dafb023479c9c3b86a0d3c8be3db0a2d186788e9af1db76d55f3dad127d15185d83d0303d7898641cb6653585a8e1dabfa7f665e61e0498963e329e6e3744bd764db2d72037ae057d89ec0b46ff8e9ff4c37e85c12acddb611c3f636421bef1542c11b0441980204cd039d0b1e46c21540d033143440d2fb7dd5d650cf89981c99ee53c6e0374d2b1b6fce03c046fccb95549910767d0543f5e8ce41d66ae6a8720a46f4049cac3b3d26dafb023479c9c3b86a0d3c8be3db0a2d186788e9af1db76d55f3dad127d15185d83d0303d7898641cb6653585a8e1dabfa7f665e61e0498963e329e6e3744bd764db2d72037ae057d89ec0b46ff8e9ff4c37e85c12acddb611c3f636421bef1542c11b04419702cd039d0b1e46c21540d033143440d2fb7dd5d650cf89981c99ee53c6e0374d2b1b6fce03c046fccb95549910767d0543f5e8ce41d66ae6a8720a46f4049cac3b3d26dafb023479c9c3b86a0d3c8be3db0a2d186788e9af1db76d55f3dad127d15185d83d0303d7898641cb6653585a8e1dabfa7f665e61e0498963e329e6e3744bd764db2d72037ae057d89ec0b46ff8e9ff4c37e85c12acddb611c3f636421bef1542c11b04419602cd039d0b1e46c21540d033143440d2fb7dd5d650cf89981c99ee53c6e0374d2b1b6fce03c046fccb95549910767d0543f5e8ce41d66ae6a8720a46f4049cac3b3d26dafb023479c9c3b86a0d3c8be3db0a2d186788e9af1db76d55f3dad127d15185d83d0303d7898641cb6653585a8e1dabfa7f665e61e0498963e329e6e3744bd764db2d72037ae057d89ec0b46ff8e9ff4c37e85c12acddb611c3f636421bef1542c11b0441" ) - ), cost = 35902, newCost(36)) + ), cost = 35902, newDetails(36), expectedNewCost = 1811) ) }, existingFeature((x: SigmaProp) => x.propBytes, @@ -7612,21 +9225,20 @@ class SigmaDslSpecification extends SigmaDslTesting } property("allOf equivalence") { - def success[T](v: T, c: Int) = Expected(Success(v), c) - + def costDetails(i: Int) = TracedCost(traceBase :+ SeqCostItem(CompanionDesc(AND), PerItemCost(JitCost(10), JitCost(5), 32), i)) verifyCases( Seq( - (Coll[Boolean]() -> success(true, 36018)), - (Coll[Boolean](true) -> success(true, 36028)), - (Coll[Boolean](false) -> success(false, 36028)), - (Coll[Boolean](false, false) -> success(false, 36038)), - (Coll[Boolean](false, true) -> success(false, 36038)), - (Coll[Boolean](true, false) -> success(false, 36038)), - (Coll[Boolean](true, true) -> success(true, 36038)), - (Coll[Boolean](true, false, false) -> success(false, 36048)), - (Coll[Boolean](true, false, true) -> success(false, 36048)), - (Coll[Boolean](true, true, false) -> success(false, 36048)), - (Coll[Boolean](true, true, true) -> success(true, 36048)) + (Coll[Boolean]() -> Expected(Success(true), 36018, costDetails(0), 1785)), + (Coll[Boolean](true) -> Expected(Success(true), 36028, costDetails(1), 1785)), + (Coll[Boolean](false) -> Expected(Success(false), 36028, costDetails(1), 1785)), + (Coll[Boolean](false, false) -> Expected(Success(false), 36038, costDetails(1), 1785)), + (Coll[Boolean](false, true) -> Expected(Success(false), 36038, costDetails(1), 1785)), + (Coll[Boolean](true, false) -> Expected(Success(false), 36038, costDetails(2), 1785)), + (Coll[Boolean](true, true) -> Expected(Success(true), 36038, costDetails(2), 1785)), + (Coll[Boolean](true, false, false) -> Expected(Success(false), 36048, costDetails(2), 1785)), + (Coll[Boolean](true, false, true) -> Expected(Success(false), 36048, costDetails(2), 1785)), + (Coll[Boolean](true, true, false) -> Expected(Success(false), 36048, costDetails(3), 1785)), + (Coll[Boolean](true, true, true) -> Expected(Success(true), 36048, costDetails(3), 1785)) ), existingFeature((x: Coll[Boolean]) => SigmaDsl.allOf(x), "{ (x: Coll[Boolean]) => allOf(x) }", @@ -7634,21 +9246,20 @@ class SigmaDslSpecification extends SigmaDslTesting } property("anyOf equivalence") { - def success[T](v: T, c: Int) = Expected(Success(v), c) - + def costDetails(i: Int) = TracedCost(traceBase :+ SeqCostItem(CompanionDesc(OR), PerItemCost(JitCost(5), JitCost(5), 64), i)) verifyCases( Seq( - (Coll[Boolean]() -> success(false, 36062)), - (Coll[Boolean](true) -> success(true, 36072)), - (Coll[Boolean](false) -> success(false, 36072)), - (Coll[Boolean](false, false) -> success(false, 36082)), - (Coll[Boolean](false, true) -> success(true, 36082)), - (Coll[Boolean](true, false) -> success(true, 36082)), - (Coll[Boolean](true, true) -> success(true, 36082)), - (Coll[Boolean](true, false, false) -> success(true, 36092)), - (Coll[Boolean](true, false, true) -> success(true, 36092)), - (Coll[Boolean](true, true, false) -> success(true, 36092)), - (Coll[Boolean](true, true, true) -> success(true, 36092)) + (Coll[Boolean]() -> Expected(Success(false), 36062, costDetails(0), 1784)), + (Coll[Boolean](true) -> Expected(Success(true), 36072, costDetails(1), 1784)), + (Coll[Boolean](false) -> Expected(Success(false), 36072, costDetails(1), 1784)), + (Coll[Boolean](false, false) -> Expected(Success(false), 36082, costDetails(2), 1784)), + (Coll[Boolean](false, true) -> Expected(Success(true), 36082, costDetails(2), 1784)), + (Coll[Boolean](true, false) -> Expected(Success(true), 36082, costDetails(1), 1784)), + (Coll[Boolean](true, true) -> Expected(Success(true), 36082, costDetails(1), 1784)), + (Coll[Boolean](true, false, false) -> Expected(Success(true), 36092, costDetails(1), 1784)), + (Coll[Boolean](true, false, true) -> Expected(Success(true), 36092, costDetails(1), 1784)), + (Coll[Boolean](true, true, false) -> Expected(Success(true), 36092, costDetails(1), 1784)), + (Coll[Boolean](true, true, true) -> Expected(Success(true), 36092, costDetails(1), 1784)) ), existingFeature((x: Coll[Boolean]) => SigmaDsl.anyOf(x), "{ (x: Coll[Boolean]) => anyOf(x) }", @@ -7656,12 +9267,15 @@ class SigmaDslSpecification extends SigmaDslTesting } property("proveDlog equivalence") { + val costDetails = TracedCost(traceBase :+ FixedCostItem(CreateProveDlog)) verifyCases( Seq( (Helpers.decodeGroupElement("02288f0e55610c3355c89ed6c5de43cf20da145b8c54f03a29f481e540d94e9a69") -> Expected(Success( CSigmaProp(ProveDlog(Helpers.decodeECPoint("02288f0e55610c3355c89ed6c5de43cf20da145b8c54f03a29f481e540d94e9a69")))), - cost = 45935)) + cost = 45935, + costDetails, + 1802)) ), existingFeature({ (x: GroupElement) => SigmaDsl.proveDlog(x) }, "{ (x: GroupElement) => proveDlog(x) }", @@ -7669,6 +9283,14 @@ class SigmaDslSpecification extends SigmaDslTesting } property("proveDHTuple equivalence") { + val costDetails = TracedCost( + traceBase ++ Array( + FixedCostItem(ValUse), + FixedCostItem(ValUse), + FixedCostItem(ValUse), + FixedCostItem(CreateProveDHTuple) + ) + ) verifyCases( Seq( (Helpers.decodeGroupElement("039c15221a318d27c186eba84fa8d986c1f63bbd9f8060380c9bfc2ef455d8346a") @@ -7681,7 +9303,9 @@ class SigmaDslSpecification extends SigmaDslTesting Helpers.decodeECPoint("039c15221a318d27c186eba84fa8d986c1f63bbd9f8060380c9bfc2ef455d8346a") ) )), - cost = 76215 + cost = 76215, + costDetails, + 1856 )) ), existingFeature({ (x: GroupElement) => SigmaDsl.proveDHTuple(x, x, x, x) }, @@ -7709,7 +9333,7 @@ class SigmaDslSpecification extends SigmaDslTesting ErgoTree.ConstantSegregationHeader, Vector(IntConstant(10)), BoolToSigmaProp(EQ(ConstantPlaceholder(0, SInt), IntConstant(20)))) - + verifyCases( { def success[T](v: T) = Expected(Success(v), 37694) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala index c30ffe0073..3e9391a619 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala @@ -676,7 +676,7 @@ class SigmaDslTesting extends PropSpec } } - def checkEquality(input: A, logInputOutput: Boolean = false): Try[(B, CostDetails)] = { + override def checkEquality(input: A, logInputOutput: Boolean = false): Try[(B, CostDetails)] = { // check the old implementation against Scala semantic function var oldRes: Try[(B, CostDetails)] = null if (ergoTreeVersionInTests < VersionContext.JitActivationVersion) From 2e0ec2033672fa8cae8d4fc8a2a7ecd9bf6f7921 Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Wed, 30 Mar 2022 10:50:20 +0200 Subject: [PATCH 06/44] v5.0-jit-costing: binary and method cost details abstraction. --- .../special/sigma/SigmaDslSpecification.scala | 152 ++++-------------- 1 file changed, 35 insertions(+), 117 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 5d586eea09..9fb8f79762 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -197,39 +197,6 @@ class SigmaDslSpecification extends SigmaDslTesting TypeBasedCostItem(rel, tpe) ) ) - // rewrite with binaryOpCostDetails - def costLT(tpe: SType) = CostDetails( - traceBase ++ Array( - FixedCostItem(SelectField), - FixedCostItem(ValUse), - FixedCostItem(SelectField), - TypeBasedCostItem(LT, tpe) - ) - ) - def costGT(tpe: SType) = CostDetails( - traceBase ++ Array( - FixedCostItem(SelectField), - FixedCostItem(ValUse), - FixedCostItem(SelectField), - TypeBasedCostItem(GT, tpe) - ) - ) - def costLE(tpe: SType) = CostDetails( - traceBase ++ Array( - FixedCostItem(SelectField), - FixedCostItem(ValUse), - FixedCostItem(SelectField), - TypeBasedCostItem(LE, tpe) - ) - ) - def costGE(tpe: SType) = CostDetails( - traceBase ++ Array( - FixedCostItem(SelectField), - FixedCostItem(ValUse), - FixedCostItem(SelectField), - TypeBasedCostItem(GE, tpe) - ) - ) def costNEQ(neqCost: Seq[CostItem]) = CostDetails( traceBase ++ Array( @@ -929,11 +896,7 @@ class SigmaDslSpecification extends SigmaDslTesting )) } - def swapArgs1[A]( - cases: Seq[((A, A), Expected[Boolean])], - cost: Int, - newCostDetails: CostDetails - ) = + def swapArgs[A](cases: Seq[((A, A), Expected[Boolean])], cost: Int, newCostDetails: CostDetails) = cases.map { case ((x, y), res) => ((y, x), Expected(res.value, cost, newCostDetails, 1788)) } @@ -986,7 +949,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("Byte LT, GT, NEQ") { val o = ExactOrdering.ByteIsExactOrdering - def expect(v: Boolean) = Expected(Success(v), 36328, costLT(SByte), 1788) + def expect(v: Boolean) = Expected(Success(v), 36328, binaryRelationCostDetails(LT, SByte), 1788) val LT_cases: Seq[((Byte, Byte), Expected[Boolean])] = Seq( (-128.toByte, -128.toByte) -> expect(false), (-128.toByte, -127.toByte) -> expect(true), @@ -1027,7 +990,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyOp(LT_cases, "<", LT.apply)(_ < _) verifyOp( - swapArgs1(LT_cases, cost = 36342, newCostDetails = costGT(SByte)), + swapArgs(LT_cases, cost = 36342, newCostDetails = binaryRelationCostDetails(GT, SByte)), ">", GT.apply)(_ > _) val neqCases = newCasesFrom2(LT_cases.map(_._1))(_ != _, cost = 36337, newCostDetails = costNEQ(constNeqCost)) @@ -1036,7 +999,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("Byte LE, GE") { val o = ExactOrdering.ByteIsExactOrdering - def expect(v: Boolean) = Expected(Success(v), 36337, costLE(SByte), 1788) + def expect(v: Boolean) = Expected(Success(v), 36337, binaryRelationCostDetails(LE, SByte), 1788) val LE_cases: Seq[((Byte, Byte), Expected[Boolean])] = Seq( (-128.toByte, -128.toByte) -> expect(true), (-128.toByte, -127.toByte) -> expect(true), @@ -1078,7 +1041,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyOp(LE_cases, "<=", LE.apply)(_ <= _) verifyOp( - swapArgs1(LE_cases, cost = 36336, newCostDetails = costGE(SByte)), + swapArgs(LE_cases, cost = 36336, newCostDetails = binaryRelationCostDetails(GE, SByte)), ">=", GE.apply)(_ >= _) } @@ -1293,7 +1256,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("Short LT, GT, NEQ") { val o = ExactOrdering.ShortIsExactOrdering - def expect(v: Boolean) = Expected(Success(v), 36328, costLT(SShort), 1788) + def expect(v: Boolean) = Expected(Success(v), 36328, binaryRelationCostDetails(LT, SShort), 1788) val LT_cases: Seq[((Short, Short), Expected[Boolean])] = Seq( (Short.MinValue, Short.MinValue) -> expect(false), (Short.MinValue, (Short.MinValue + 1).toShort) -> expect(true), @@ -1333,7 +1296,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyOp(LT_cases, "<", LT.apply)(_ < _) - verifyOp(swapArgs1(LT_cases, cost = 36342, newCostDetails = costGT(SShort)), ">", GT.apply)(_ > _) + verifyOp(swapArgs(LT_cases, cost = 36342, newCostDetails = binaryRelationCostDetails(GT, SShort)), ">", GT.apply)(_ > _) val neqCases = newCasesFrom2(LT_cases.map(_._1))(_ != _, cost = 36337, newCostDetails = costNEQ(constNeqCost)) verifyOp(neqCases, "!=", NEQ.apply)(_ != _) @@ -1341,7 +1304,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("Short LE, GE") { val o = ExactOrdering.ShortIsExactOrdering - def expect(v: Boolean) = Expected(Success(v), 36337, costLE(SShort), 1788) + def expect(v: Boolean) = Expected(Success(v), 36337, binaryRelationCostDetails(LE, SShort), 1788) val LE_cases: Seq[((Short, Short), Expected[Boolean])] = Seq( (Short.MinValue, Short.MinValue) -> expect(true), (Short.MinValue, (Short.MinValue + 1).toShort) -> expect(true), @@ -1383,7 +1346,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyOp(LE_cases, "<=", LE.apply)(_ <= _) verifyOp( - swapArgs1(LE_cases, cost = 36336, newCostDetails = costGE(SShort)), + swapArgs(LE_cases, cost = 36336, newCostDetails = binaryRelationCostDetails(GE, SShort)), ">=", GE.apply)(_ >= _) } @@ -1595,7 +1558,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("Int LT, GT, NEQ") { val o = ExactOrdering.IntIsExactOrdering - def expect(v: Boolean) = Expected(Success(v), 36328, costLT(SInt), 1788) + def expect(v: Boolean) = Expected(Success(v), 36328, binaryRelationCostDetails(LT, SInt), 1788) val LT_cases: Seq[((Int, Int), Expected[Boolean])] = Seq( (Int.MinValue, Int.MinValue) -> expect(false), (Int.MinValue, (Int.MinValue + 1).toInt) -> expect(true), @@ -1636,7 +1599,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyOp(LT_cases, "<", LT.apply)(_ < _) verifyOp( - swapArgs1(LT_cases, cost = 36342, newCostDetails = costGT(SInt)), + swapArgs(LT_cases, cost = 36342, newCostDetails = binaryRelationCostDetails(GT, SInt)), ">", GT.apply)(_ > _) val neqCases = newCasesFrom2(LT_cases.map(_._1))(_ != _, cost = 36337, newCostDetails = costNEQ(constNeqCost)) @@ -1645,7 +1608,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("Int LE, GE") { val o = ExactOrdering.IntIsExactOrdering - def expect(v: Boolean) = Expected(Success(v), 36337, costLE(SInt), 1788) + def expect(v: Boolean) = Expected(Success(v), 36337, binaryRelationCostDetails(LE, SInt), 1788) val LE_cases: Seq[((Int, Int), Expected[Boolean])] = Seq( (Int.MinValue, Int.MinValue) -> expect(true), (Int.MinValue, (Int.MinValue + 1).toInt) -> expect(true), @@ -1687,7 +1650,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyOp(LE_cases, "<=", LE.apply)(_ <= _) verifyOp( - swapArgs1(LE_cases, cost = 36336, newCostDetails = costGE(SInt)), + swapArgs(LE_cases, cost = 36336, newCostDetails = binaryRelationCostDetails(GE, SInt)), ">=", GE.apply)(_ >= _) } @@ -1915,7 +1878,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("Long LT, GT, NEQ") { val o = ExactOrdering.LongIsExactOrdering - def expect(v: Boolean) = Expected(Success(v), 36328, costLT(SLong), 1788) + def expect(v: Boolean) = Expected(Success(v), 36328, binaryRelationCostDetails(LT, SLong), 1788) val LT_cases: Seq[((Long, Long), Expected[Boolean])] = Seq( (Long.MinValue, Long.MinValue) -> expect(false), (Long.MinValue, (Long.MinValue + 1).toLong) -> expect(true), @@ -1956,7 +1919,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyOp(LT_cases, "<", LT.apply)(_ < _) verifyOp( - swapArgs1(LT_cases, cost = 36342, newCostDetails = costGT(SLong)), + swapArgs(LT_cases, cost = 36342, newCostDetails = binaryRelationCostDetails(GT, SLong)), ">", GT.apply)(_ > _) val neqCases = newCasesFrom2(LT_cases.map(_._1))(_ != _, cost = 36337, newCostDetails = costNEQ(constNeqCost)) @@ -1965,7 +1928,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("Long LE, GE") { val o = ExactOrdering.LongIsExactOrdering - def expect(v: Boolean) = Expected(Success(v), 36337, costLE(SLong), 1788) + def expect(v: Boolean) = Expected(Success(v), 36337, binaryRelationCostDetails(LE, SLong), 1788) val LE_cases: Seq[((Long, Long), Expected[Boolean])] = Seq( (Long.MinValue, Long.MinValue) -> expect(true), (Long.MinValue, (Long.MinValue + 1).toLong) -> expect(true), @@ -2007,7 +1970,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyOp(LE_cases, "<=", LE.apply)(_ <= _) verifyOp( - swapArgs1(LE_cases, cost = 36336, newCostDetails = costGE(SLong)), + swapArgs(LE_cases, cost = 36336, newCostDetails = binaryRelationCostDetails(GE, SLong)), ">=", GE.apply)(_ >= _) } @@ -2185,7 +2148,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("BigInt LT, GT, NEQ") { val o = NumericOps.BigIntIsExactOrdering - def expect(v: Boolean) = Expected(Success(v), 36328, costLT(SBigInt), 1788) + def expect(v: Boolean) = Expected(Success(v), 36328, binaryRelationCostDetails(LT, SBigInt), 1788) val LT_cases: Seq[((BigInt, BigInt), Expected[Boolean])] = Seq( (BigIntMinValue, BigIntMinValue) -> expect(false), @@ -2229,7 +2192,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyOp(LT_cases, "<", LT.apply)(o.lt(_, _)) verifyOp( - swapArgs1(LT_cases, cost = 36342, newCostDetails = costGT(SBigInt)), + swapArgs(LT_cases, cost = 36342, newCostDetails = binaryRelationCostDetails(GT, SBigInt)), ">", GT.apply)(o.gt(_, _)) val constBigIntCost = Array[CostItem](FixedCostItem(NamedDesc("EQ_BigInt"), FixedCost(JitCost(5)))) @@ -2244,7 +2207,7 @@ class SigmaDslSpecification extends SigmaDslTesting val BigIntMaxValue = CBigInt(new BigInteger("7F" + "ff" * 31, 16)) val BigIntOverlimit = CBigInt(new BigInteger("7F" + "ff" * 33, 16)) - def expect(v: Boolean) = Expected(Success(v), 36337, costLE(SBigInt), 1788) + def expect(v: Boolean) = Expected(Success(v), 36337, binaryRelationCostDetails(LE, SBigInt), 1788) val LE_cases: Seq[((BigInt, BigInt), Expected[Boolean])] = Seq( (BigIntMinValue, BigIntMinValue) -> expect(true), @@ -2289,7 +2252,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyOp(LE_cases, "<=", LE.apply)(o.lteq(_, _)) verifyOp( - swapArgs1(LE_cases, cost = 36336, newCostDetails = costGE(SBigInt)), + swapArgs(LE_cases, cost = 36336, newCostDetails = binaryRelationCostDetails(GE, SBigInt)), ">=", GE.apply)(o.gteq(_, _)) } @@ -2713,13 +2676,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("GroupElement.getEncoded equivalence") { verifyCases( { - val costDetails = TracedCost( - traceBase ++ Array( - FixedCostItem(PropertyCall), - FixedCostItem(SGroupElement.GetEncodedMethod, FixedCost(JitCost(250))) - ) - ) - def success[T](v: T) = Expected(Success(v), 37905, costDetails, 1810) + def success[T](v: T) = Expected(Success(v), 37905, methodCostDetails(SGroupElement.GetEncodedMethod, 250), 1810) Seq( (ge1, success(Helpers.decodeBytes(ge1str))), (ge2, success(Helpers.decodeBytes(ge2str))), @@ -2780,13 +2737,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("GroupElement.negate equivalence") { verifyCases( { - val costDetails = TracedCost( - traceBase ++ Array( - FixedCostItem(PropertyCall), - FixedCostItem(MethodDesc(SGroupElement.NegateMethod), FixedCost(JitCost(45))) - ) - ) - def success[T](v: T) = Expected(Success(v), 36292, costDetails, 1805) + def success[T](v: T) = Expected(Success(v), 36292, methodCostDetails(SGroupElement.NegateMethod, 45), 1805) Seq( (ge1, success(Helpers.decodeGroupElement("02358d53f01276211f92d0aefbd278805121d4ff6eb534b777af1ee8abae5b2056"))), (ge2, success(Helpers.decodeGroupElement("03dba7b94b111f3894e2f9120b577da595ec7d58d488485adf73bf4e153af63575"))), @@ -3050,16 +3001,10 @@ class SigmaDslSpecification extends SigmaDslTesting "{ (t: AvlTree) => t.keyLength }", expectedExprFor("keyLength"))) - val newCostValueLength = TracedCost( - traceBase ++ Array( - FixedCostItem(PropertyCall), - FixedCostItem(MethodDesc(SAvlTree.valueLengthOptMethod), FixedCost(JitCost(15))), - ) - ) verifyCases( { // TODO mainnet v5.0: This test isn't using JITC. Cyclic failing for newCost: 1785 vs 1786 - def success[T](v: T) = Expected(Success(v), 37151)//, newCostValueLength, 1786) + def success[T](v: T) = Expected(Success(v), 37151)//, methodCostDetails(SAvlTree.valueLengthOptMethod, 15), 1786) Seq( (t1, success(Some(1))), (t2, success(Some(64))), @@ -3943,12 +3888,6 @@ class SigmaDslSpecification extends SigmaDslTesting "{ (x: Box) => x.creationInfo }", FuncValue(Vector((1, SBox)), ExtractCreationInfo(ValUse(1, SBox))))) - val costDetails = CostDetails( - traceBase ++ Array( - FixedCostItem(PropertyCall), - FixedCostItem(MethodDesc(SBox.tokensMethod), FixedCost(JitCost(15))), - ) - ) // TODO v6.0 (2h): fix collections equality and remove map(identity) // (PairOfColl should be equal CollOverArray) verifyCases( @@ -3956,8 +3895,8 @@ class SigmaDslSpecification extends SigmaDslTesting b1 -> Expected(Success(Coll[(Coll[Byte], Long)]( (Helpers.decodeBytes("6e789ab7b2fffff12280a6cd01557f6fb22b7f80ff7aff8e1f7f15973d7f0001"), 10000000L), (Helpers.decodeBytes("a3ff007f00057600808001ff8f8000019000ffdb806fff7cc0b6015eb37fa600"), 500L) - ).map(identity)), 36167), - b2 -> Expected(Success(Coll[(Coll[Byte], Long)]().map(identity)), 36157, costDetails, 1786) + ).map(identity)), 36167, methodCostDetails(SBox.tokensMethod, 15), 1792), + b2 -> Expected(Success(Coll[(Coll[Byte], Long)]().map(identity)), 36157, methodCostDetails(SBox.tokensMethod, 15), 1786) ), existingFeature({ (x: Box) => x.tokens }, "{ (x: Box) => x.tokens }", @@ -4960,14 +4899,8 @@ class SigmaDslSpecification extends SigmaDslTesting } } - val lastBlockCostDetails = TracedCost( - traceBase ++ Array( - FixedCostItem(PropertyCall), - FixedCostItem(SContext.lastBlockUtxoRootHashMethod, FixedCost(JitCost(15))) - ) - ) verifyCases( - Seq(ctx -> Expected(Success(ctx.LastBlockUtxoRootHash), cost = 35990, lastBlockCostDetails, 1786)), + Seq(ctx -> Expected(Success(ctx.LastBlockUtxoRootHash), cost = 35990, methodCostDetails(SContext.lastBlockUtxoRootHashMethod, 15), 1786)), existingPropTest("LastBlockUtxoRootHash", { (x: Context) => x.LastBlockUtxoRootHash }), preGeneratedSamples = Some(samples)) @@ -5000,14 +4933,8 @@ class SigmaDslSpecification extends SigmaDslTesting )), preGeneratedSamples = Some(samples)) - val minerCostDetails = TracedCost( - traceBase ++ Array( - FixedCostItem(PropertyCall), - FixedCostItem(SContext.minerPubKeyMethod, FixedCost(JitCost(20))) - ) - ) verifyCases( - Seq(ctx -> Expected(Success(ctx.minerPubKey), cost = 36047, minerCostDetails, 1787)), + Seq(ctx -> Expected(Success(ctx.minerPubKey), cost = 36047, methodCostDetails(SContext.minerPubKeyMethod, 20), 1787)), existingPropTest("minerPubKey", { (x: Context) => x.minerPubKey }), preGeneratedSamples = Some(samples)) @@ -6728,7 +6655,6 @@ class SigmaDslSpecification extends SigmaDslTesting ) ) - def success[T](v: T, c: Int) = Expected(Success(v), c) if (lowerMethodCallsInTests) { verifyCases( { @@ -6751,8 +6677,7 @@ class SigmaDslSpecification extends SigmaDslTesting } else { assertExceptionThrown( verifyCases( - // TODO mainnet v5.0: Correct behaviour (cost/details are ignored) when exception is thrown? - Seq( (Coll[Box](), Expected(Success(true), 37909, TracedCost(traceBase), 1789))), + Seq( (Coll[Box](), Expected(Success(true), 37909)) ), existingFeature( { (x: Coll[Box]) => x.forall({ (b: Box) => b.value > 1 }) }, "{ (x: Coll[Box]) => x.forall({(b: Box) => b.value > 1 }) }" @@ -6800,7 +6725,6 @@ class SigmaDslSpecification extends SigmaDslTesting ) ) - def success[T](v: T, c: Int) = Expected(Success(v), c) if (lowerMethodCallsInTests) { verifyCases( { @@ -6823,8 +6747,7 @@ class SigmaDslSpecification extends SigmaDslTesting } else { assertExceptionThrown( verifyCases( - // TODO mainnet v5.0: Correct behaviour (cost/details are ignored) when exception is thrown? - Seq( (Coll[Box](), success(false, 38455)) ), + Seq( (Coll[Box](), Expected(Success(false), 38455)) ), existingFeature( { (x: Coll[Box]) => x.exists({ (b: Box) => b.value > 1 }) }, "{ (x: Coll[Box]) => x.exists({(b: Box) => b.value > 1 }) }" @@ -6889,7 +6812,6 @@ class SigmaDslSpecification extends SigmaDslTesting ) ) - def success[T](v: T, c: Int) = Expected(Success(v), c) if (lowerMethodCallsInTests) { verifyCases( { @@ -6924,8 +6846,7 @@ class SigmaDslSpecification extends SigmaDslTesting } else { assertExceptionThrown( verifyCases( - // TODO mainnet v5.0: Correct behaviour (cost/details are ignored) when exception is thrown? - Seq( (Coll[BigInt](), success(false, 38955)) ), + Seq( (Coll[BigInt](), Expected(Success(false), 38955)) ), existingFeature( { (x: Coll[BigInt]) => x.exists({ (b: BigInt) => if (o.gt(b, BigIntZero)) o.lt(b, BigInt10) else false @@ -7028,7 +6949,6 @@ class SigmaDslSpecification extends SigmaDslTesting } else { assertExceptionThrown( verifyCases( - // ignored here also Seq( (Coll[BigInt](), Expected(Success(true), 38412)) ), existingFeature( { (x: Coll[BigInt]) => x.forall({ (b: BigInt) => @@ -7069,8 +6989,7 @@ class SigmaDslSpecification extends SigmaDslTesting { def success[T](v: T, c: Int) = Expected(Success(v), c) Seq( - // TODO mainnet v5.0: Correct behaviour (cost/details are ignored) when exception is thrown? - // handle multiple versions combination resulting in different jit costing - costDetails1(Copy) and cost 1793/1800 (problematic version 2/2) + // TODO mainnet v5.0: different results - costDetails1(Copy) and cost 1793/1800 (problematic version 2/2 ?) // Coll[GroupElement]() -> Expected(Success(Coll[Byte]()), 40133, CostDetails.ZeroCost, 1793), Coll[GroupElement]( Helpers.decodeGroupElement("02d65904820f8330218cf7318b3810d0c9ab9df86f1ee6100882683f23c0aee587"), @@ -7600,7 +7519,7 @@ class SigmaDslSpecification extends SigmaDslTesting } else { assertExceptionThrown( verifyCases( - Seq( ((Coll[Byte](), 0), success(0, 41266)) ), + Seq( ((Coll[Byte](), 0), Expected(Success(0), 41266)) ), existingFeature( { (x: (Coll[Byte], Int)) => x._1.foldLeft(x._2, { i: (Int, Byte) => n.plus(i._1, i._2) }) }, "{ (x: (Coll[Byte], Int)) => x._1.fold(x._2, { (i1: Int, i2: Byte) => i1 + i2 }) }" @@ -7809,7 +7728,6 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( // (coll, initState) { - def success[T](v: T, c: Int) = Expected(Success(v), c) Seq( ((Coll[Byte](), 0), Expected(Success(0), 42037, costDetails1, 1787)), ((Coll[Byte](), Int.MaxValue), Expected(Success(Int.MaxValue), 42037, costDetails1, 1787)), @@ -7855,7 +7773,7 @@ class SigmaDslSpecification extends SigmaDslTesting } else { assertExceptionThrown( verifyCases( - Seq( ((Coll[Byte](), 0), success(0, 42037)) ), + Seq( ((Coll[Byte](), 0), Expected(Success(0), 42037)) ), existingFeature( { (x: (Coll[Byte], Int)) => x._1.foldLeft(x._2, { i: (Int, Byte) => if (i._2 > 0) n.plus(i._1, i._2) else i._1 }) }, "{ (x: (Coll[Byte], Int)) => x._1.fold(x._2, { (i1: Int, i2: Byte) => if (i2 > 0) i1 + i2 else i1 }) }" @@ -8925,7 +8843,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( Seq( (false, Expected(Success(CSigmaProp(TrivialProp.FalseProp)), 35892, costDetails, 1785)), - (true , Expected(Success(CSigmaProp(TrivialProp.TrueProp )), 35892, costDetails, 1785))), + (true, Expected(Success(CSigmaProp(TrivialProp.TrueProp)), 35892, costDetails, 1785))), existingFeature((x: Boolean) => sigmaProp(x), "{ (x: Boolean) => sigmaProp(x) }", FuncValue(Vector((1, SBoolean)), BoolToSigmaProp(ValUse(1, SBoolean))))) From 51a69774f587e4a53565b824d333337b5ef486a0 Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Wed, 30 Mar 2022 11:07:09 +0200 Subject: [PATCH 07/44] v5.0-jit-costing: fix test for SAvlTree.valueLengthOptMethod --- .../special/sigma/SigmaDslSpecification.scala | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 9fb8f79762..8cf2c42fd5 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -2343,7 +2343,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("NEQ of tuples of numerics") { val tuplesNeqCost = Array( FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))), + FixedCostItem(NamedDesc("EQ_Prim"), FixedCost(JitCost(3))) ) verifyNeq((0.toByte, 1.toByte), (1.toByte, 1.toByte), 36337, tuplesNeqCost, 1787)(_.copy()) verifyNeq((0.toShort, 1.toByte), (1.toShort, 1.toByte), 36337, tuplesNeqCost, 1787)(_.copy()) @@ -2351,7 +2351,7 @@ class SigmaDslSpecification extends SigmaDslTesting verifyNeq((0.toLong, 1.toByte), (1.toLong, 1.toByte), 36337, tuplesNeqCost, 1787)(_.copy()) verifyNeq((0.toBigInt, 1.toByte), (1.toBigInt, 1.toByte), 36337, Array( FixedCostItem(NamedDesc("EQ_Tuple"), FixedCost(JitCost(4))), - FixedCostItem(NamedDesc("EQ_BigInt"), FixedCost(JitCost(5))), + FixedCostItem(NamedDesc("EQ_BigInt"), FixedCost(JitCost(5))) ), 1787)(_.copy()) } @@ -2481,7 +2481,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("NEQ of collections of pre-defined types") { val collNeqCost1 = Array( - FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))) ) val collNeqCost2 = Array( FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), @@ -2595,7 +2595,7 @@ class SigmaDslSpecification extends SigmaDslTesting prepareSamples[Coll[Coll[Int]]] val nestedNeq1 = Array( - FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), + FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))) ) val nestedNeq2 = Array( FixedCostItem(NamedDesc("MatchType"), FixedCost(JitCost(1))), @@ -3003,12 +3003,11 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( { - // TODO mainnet v5.0: This test isn't using JITC. Cyclic failing for newCost: 1785 vs 1786 - def success[T](v: T) = Expected(Success(v), 37151)//, methodCostDetails(SAvlTree.valueLengthOptMethod, 15), 1786) + def success[T](v: T, newCost: Int) = Expected(Success(v), 37151, methodCostDetails(SAvlTree.valueLengthOptMethod, 15), newCost) Seq( - (t1, success(Some(1))), - (t2, success(Some(64))), - (t3, success(None)) + (t1, success(Some(1), 1786)), + (t2, success(Some(64), 1786)), + (t3, success(None, 1785)) ) }, existingFeature((t: AvlTree) => t.valueLengthOpt, @@ -6487,7 +6486,7 @@ class SigmaDslSpecification extends SigmaDslTesting FixedCostItem(NamedDesc("MatchSingleArgMethodCall"), FixedCost(JitCost(30))), SeqCostItem(NamedDesc("CheckFlatmapBody"), PerItemCost(JitCost(20), JitCost(20), 1), 2), SeqCostItem(MethodDesc(SCollection.FlatMapMethod), PerItemCost(JitCost(60), JitCost(10), 8), 0) - ), + ) ) val costDetails3 = TracedCost( traceBase ++ Array( From 6f834ac47988dd2a9aed362c52040a9793124091 Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Wed, 30 Mar 2022 12:26:53 +0200 Subject: [PATCH 08/44] v5.0-jit-costing: Correst expectedExpr for 'Coll fold with nested If' test. --- .../src/test/scala/special/sigma/SigmaDslSpecification.scala | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 8cf2c42fd5..d005f0216a 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -7758,6 +7758,11 @@ class SigmaDslSpecification extends SigmaDslTesting SelectField.typed[Value[SByte.type]](ValUse(3, SPair(SInt, SByte)), 2.toByte), SInt ) + ), + ValDef( + 6, + List(), + SelectField.typed[Value[SInt.type]](ValUse(3, SPair(SInt, SByte)), 1.toByte) ) ), If( From a7d23217da3f6650b3cb01cb334b18497b32dd97 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Fri, 1 Apr 2022 17:57:54 +0200 Subject: [PATCH 09/44] v5.0-add-jit-costing-for-tests: renamings --- .../special/sigma/SigmaDslSpecification.scala | 28 +++++++++---------- .../scala/special/sigma/SigmaDslTesting.scala | 6 ++-- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index d005f0216a..0980767c13 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -382,12 +382,12 @@ class SigmaDslSpecification extends SigmaDslTesting FixedCostItem(SelectField) ) ) - val newCost = 38241 + val cost = 38241 val cases = Seq( - (true, false) -> Expected(Success(true), newCost, newDetails1, 1786), - (true, true) -> Expected(Success(true), newCost, newDetails1, 1786), - (false, false) -> Expected(Success(false), newCost, newDetails2, 1788), - (false, true) -> Expected(Success(true), newCost, newDetails2, 1788) + (true, false) -> Expected(Success(true), cost, newDetails1, 1786), + (true, true) -> Expected(Success(true), cost, newDetails1, 1786), + (false, false) -> Expected(Success(false), cost, newDetails2, 1788), + (false, true) -> Expected(Success(true), cost, newDetails2, 1788) ) verifyCases(cases, eq) } @@ -4866,7 +4866,7 @@ class SigmaDslSpecification extends SigmaDslTesting (ctx, Expected( Success(-1), cost = 36318, // TODO mainnet v5.0: Change to concrete details does not change test result - newDetails = CostDetails.ZeroCost, + expectedDetails = CostDetails.ZeroCost, newCost = 1786, newVersionedResults = { val res = (ExpectedResult(Success(0), Some(1786)) -> None) @@ -5382,7 +5382,7 @@ class SigmaDslSpecification extends SigmaDslTesting TypeBasedCostItem(ArithOp.Plus, SLong) ) ) - val tagRegisterCostDetails3 = TracedCost( + val tagRegisterCostDetails3 = TracedCost( Array( FixedCostItem(Apply), FixedCostItem(FuncValue), @@ -6015,7 +6015,7 @@ class SigmaDslSpecification extends SigmaDslTesting def successNew[T](v: T, c: Int, newV: T, newC: Int) = Expected( value = Success(v), cost = c, - newDetails = CostDetails.ZeroCost, + expectedDetails = CostDetails.ZeroCost, newCost = newC, newVersionedResults = Seq( 0 -> (ExpectedResult(Success(newV), Some(newC)) -> None), @@ -6267,7 +6267,7 @@ class SigmaDslSpecification extends SigmaDslTesting ((Helpers.decodeBytes("0100"), Helpers.decodeBytes("01")) -> Expected(Failure(new ArrayIndexOutOfBoundsException("1")), cost = 0, - newDetails = CostDetails.ZeroCost, + expectedDetails = CostDetails.ZeroCost, newCost = 1789, newVersionedResults = { val res = (ExpectedResult(Success(Helpers.decodeBytes("00")), Some(1789)), None) @@ -7036,7 +7036,7 @@ class SigmaDslSpecification extends SigmaDslTesting Helpers.decodeGroupElement("02d65904820f8330218cf7318b3810d0c9ab9df86f1ee6100882683f23c0aee587"), Helpers.decodeGroupElement("0390e9daa9916f30d0bc61a8e381c6005edfb7938aee5bb4fc9e8a759c7748ffaa") ) -> Expected(Try(SCollection.throwInvalidFlatmap(null)), 0, - newDetails = CostDetails.ZeroCost, + expectedDetails = CostDetails.ZeroCost, newCost = 0, newVersionedResults = (0 to 2).map(version => // successful result for each version @@ -8722,7 +8722,7 @@ class SigmaDslSpecification extends SigmaDslTesting (None -> Expected( value = Failure(new NoSuchElementException("None.get")), cost = 0, - newDetails = CostDetails.ZeroCost, + expectedDetails = CostDetails.ZeroCost, newCost = 1786, newVersionedResults = Seq( 2 -> (ExpectedResult(Success(5L), Some(1786)) -> None) @@ -9267,7 +9267,7 @@ class SigmaDslSpecification extends SigmaDslTesting (Coll(t1.bytes:_*), 0) -> Expected( Success(Helpers.decodeBytes("000008d3")), cost = 37694, - newDetails = CostDetails.ZeroCost, + expectedDetails = CostDetails.ZeroCost, newCost = 1803, newVersionedResults = { val res = (ExpectedResult(Success(Helpers.decodeBytes("0008d3")), Some(1803)) -> None) @@ -9277,7 +9277,7 @@ class SigmaDslSpecification extends SigmaDslTesting (Helpers.decodeBytes("000008d3"), 0) -> Expected( Success(Helpers.decodeBytes("00000008d3")), cost = 37694, - newDetails = CostDetails.ZeroCost, + expectedDetails = CostDetails.ZeroCost, newCost = 1803, newVersionedResults = { // since the tree without constant segregation, substitution has no effect @@ -9332,7 +9332,7 @@ class SigmaDslSpecification extends SigmaDslTesting ctx -> Expected( Failure(new NoSuchElementException("None.get")), cost = 37694, - newDetails = CostDetails.ZeroCost, + expectedDetails = CostDetails.ZeroCost, newCost = 1796, newVersionedResults = Seq( 0 -> (ExpectedResult(Success(true), Some(1796)) -> None), diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala index 3e9391a619..8f90dab653 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala @@ -908,19 +908,19 @@ class SigmaDslTesting extends PropSpec * * @param value value returned by feature function v4.x * @param cost expected cost value of the verification execution (v4.x) - * @param newDetails expected cost details for all versions + * @param expectedDetails expected cost details for all versions * @param newCost expected new verification cost for all versions * @param newVersionedResults new results returned by each changed feature function in * v5.+ for each ErgoTree version. */ def apply[A](value: Try[A], cost: Int, - newDetails: CostDetails, newCost: Int, + expectedDetails: CostDetails, newCost: Int, newVersionedResults: Seq[(Int, (ExpectedResult[A], Option[CostDetails]))]): Expected[A] = new Expected[A](ExpectedResult(value, Some(cost))) { override val newResults = { val commonNewResults = defaultNewResults.map { case (res, _) => - (ExpectedResult(res.value, Some(newCost)), Option(newDetails)) + (ExpectedResult(res.value, Some(newCost)), Option(expectedDetails)) } commonNewResults.updateMany(newVersionedResults) } From c84aa3595e5946c83050884083e404e4e79a0ced Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Mon, 4 Apr 2022 17:28:22 +0200 Subject: [PATCH 10/44] v5.0-fix-flatmap: remove checkValidFlatmap from flatMap_eval (which is used by v5.0 interpreter) --- sigmastate/src/main/scala/sigmastate/types.scala | 3 --- .../src/test/scala/special/sigma/SigmaDslSpecification.scala | 3 --- 2 files changed, 6 deletions(-) diff --git a/sigmastate/src/main/scala/sigmastate/types.scala b/sigmastate/src/main/scala/sigmastate/types.scala index dcfdadf5fc..d173d4d4ff 100644 --- a/sigmastate/src/main/scala/sigmastate/types.scala +++ b/sigmastate/src/main/scala/sigmastate/types.scala @@ -1688,9 +1688,6 @@ object SCollection extends STypeCompanion with MethodByNameUnapply { */ def flatMap_eval[A, B](mc: MethodCall, xs: Coll[A], f: A => Coll[B]) (implicit E: ErgoTreeEvaluator): Coll[B] = { - if (!VersionContext.current.isJitActivated) { - checkValidFlatmap(mc) - } val m = mc.method var res: Coll[B] = null E.addSeqCost(m.costKind.asInstanceOf[PerItemCost], m.opDesc) { () => diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 2a161923ee..946a4a64f7 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -6005,10 +6005,7 @@ class SigmaDslSpecification extends SigmaDslTesting val f = changedFeature( { (x: Coll[GroupElement]) => SCollection.throwInvalidFlatmap(null) }, { (x: Coll[GroupElement]) => - if (VersionContext.current.isJitActivated) x.flatMap({ (b: GroupElement) => b.getEncoded.indices }) - else - SCollection.throwInvalidFlatmap(null) }, "", // NOTE, the script for this test case cannot be compiled FuncValue( From bee7396ead3d9cd3113b5e984c30d178cbfbbe39 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Mon, 4 Apr 2022 17:35:16 +0200 Subject: [PATCH 11/44] v5.0-fix-flatmap: comment test code --- .../test/scala/special/sigma/SigmaDslSpecification.scala | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 946a4a64f7..663955540a 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -6003,9 +6003,10 @@ class SigmaDslSpecification extends SigmaDslTesting )) ) val f = changedFeature( - { (x: Coll[GroupElement]) => SCollection.throwInvalidFlatmap(null) }, - { (x: Coll[GroupElement]) => - x.flatMap({ (b: GroupElement) => b.getEncoded.indices }) + scalaFunc = { (x: Coll[GroupElement]) => SCollection.throwInvalidFlatmap(null) }, + scalaFuncNew = { (x: Coll[GroupElement]) => + // NOTE, v5.0 interpreter accepts any lambda in flatMap + x.flatMap({ (b: GroupElement) => b.getEncoded.indices }) }, "", // NOTE, the script for this test case cannot be compiled FuncValue( From d25924a06eebbea0ad3a96ec4605f7a0419e8b04 Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Wed, 30 Mar 2022 11:32:01 +0200 Subject: [PATCH 12/44] v5.0-jit-costing: Coll.flatMap equivalence failing test due to costDetails. --- .../src/test/scala/special/sigma/SigmaDslSpecification.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 158c7e6987..17d41072be 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -6500,12 +6500,12 @@ class SigmaDslSpecification extends SigmaDslTesting SeqCostItem(MethodDesc(SCollection.FlatMapMethod), PerItemCost(JitCost(60), JitCost(10), 8), 135) ) ) - // TODO mainnet v5.0: Failing test with cyclic results verifyCases( { def success[T](v: T, c: Int) = Expected(Success(v), c) Seq( - (Coll[Box](), success(Coll[Byte](), 38126)), + // TODO mainnet v5.0: ActivatedVersion = 2; ErgoTree version = 0 requres costDetails1. + (Coll[Box](), Expected(Success(Coll[Byte]()), 38126, costDetails2, 1798)), (Coll[Box](b1), success(Helpers.decodeBytes( "0008ce02c1a9311ecf1e76c787ba4b1c0e10157b4f6d1e4db3ef0d84f411c99f2d4d2c5b027d1bd9a437e73726ceddecc162e5c85f79aee4798505bc826b8ad1813148e4190257cff6d06fe15d1004596eeb97a7f67755188501e36adc49bd807fe65e9d8281033c6021cff6ba5fdfc4f1742486030d2ebbffd9c9c09e488792f3102b2dcdabd5" ), 38206)), From e50a12cb97e82f09df9419f4a498c5aaad0cfecc Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Mon, 4 Apr 2022 18:09:29 +0200 Subject: [PATCH 13/44] v5.0-jitc-failing-Coll-flatmap-equivalence: fix tests --- .../special/sigma/SigmaDslSpecification.scala | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 17d41072be..ff338367d2 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -6483,21 +6483,23 @@ class SigmaDslSpecification extends SigmaDslTesting traceBase ++ Array( FixedCostItem(MethodCall), FixedCostItem(FuncValue), - FixedCostItem(NamedDesc("MatchSingleArgMethodCall"), FixedCost(JitCost(30))), - SeqCostItem(NamedDesc("CheckFlatmapBody"), PerItemCost(JitCost(20), JitCost(20), 1), 2), - SeqCostItem(MethodDesc(SCollection.FlatMapMethod), PerItemCost(JitCost(60), JitCost(10), 8), 0) + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractScriptBytes), + SeqCostItem(MethodDesc(SCollection.FlatMapMethod), PerItemCost(JitCost(60), JitCost(10), 8), 135) ) ) val costDetails3 = TracedCost( traceBase ++ Array( FixedCostItem(MethodCall), FixedCostItem(FuncValue), - FixedCostItem(NamedDesc("MatchSingleArgMethodCall"), FixedCost(JitCost(30))), - SeqCostItem(NamedDesc("CheckFlatmapBody"), PerItemCost(JitCost(20), JitCost(20), 1), 2), FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), FixedCostItem(ValUse), FixedCostItem(ExtractScriptBytes), - SeqCostItem(MethodDesc(SCollection.FlatMapMethod), PerItemCost(JitCost(60), JitCost(10), 8), 135) + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(ExtractScriptBytes), + SeqCostItem(MethodDesc(SCollection.FlatMapMethod), PerItemCost(JitCost(60), JitCost(10), 8), 147) ) ) verifyCases( @@ -6505,13 +6507,13 @@ class SigmaDslSpecification extends SigmaDslTesting def success[T](v: T, c: Int) = Expected(Success(v), c) Seq( // TODO mainnet v5.0: ActivatedVersion = 2; ErgoTree version = 0 requres costDetails1. - (Coll[Box](), Expected(Success(Coll[Byte]()), 38126, costDetails2, 1798)), - (Coll[Box](b1), success(Helpers.decodeBytes( + (Coll[Box](), Expected(Success(Coll[Byte]()), 38126, costDetails1, 1793)), + (Coll[Box](b1), Expected(Success(Helpers.decodeBytes( "0008ce02c1a9311ecf1e76c787ba4b1c0e10157b4f6d1e4db3ef0d84f411c99f2d4d2c5b027d1bd9a437e73726ceddecc162e5c85f79aee4798505bc826b8ad1813148e4190257cff6d06fe15d1004596eeb97a7f67755188501e36adc49bd807fe65e9d8281033c6021cff6ba5fdfc4f1742486030d2ebbffd9c9c09e488792f3102b2dcdabd5" - ), 38206)), - (Coll[Box](b1, b2), success(Helpers.decodeBytes( + )), 38206, costDetails2, 1811)), + (Coll[Box](b1, b2), Expected(Success(Helpers.decodeBytes( "0008ce02c1a9311ecf1e76c787ba4b1c0e10157b4f6d1e4db3ef0d84f411c99f2d4d2c5b027d1bd9a437e73726ceddecc162e5c85f79aee4798505bc826b8ad1813148e4190257cff6d06fe15d1004596eeb97a7f67755188501e36adc49bd807fe65e9d8281033c6021cff6ba5fdfc4f1742486030d2ebbffd9c9c09e488792f3102b2dcdabd500d197830201010096850200" - ), 38286)) + )), 38286, costDetails3, 1815)) ) }, existingFeature({ (x: Coll[Box]) => x.flatMap({ (b: Box) => b.propositionBytes }) }, From b5ee2071ca50f83a710511aa5da5a820794679bb Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Wed, 30 Mar 2022 11:52:27 +0200 Subject: [PATCH 14/44] v5.0-jit-costing: AvlTree.contains failing test due to expectedCost. --- .../src/test/scala/special/sigma/SigmaDslSpecification.scala | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 158c7e6987..bcc493bfca 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -3283,8 +3283,9 @@ class SigmaDslSpecification extends SigmaDslTesting contains.checkExpected(input, success(okContains)) get.checkExpected(input, success(valueOpt)) - // TODO mainnet v5.0: Cyclic newCost here (1823/1825), also costDetails are ignored - contains.checkVerify(input, Expected(Success(okContains), 37850))//, TracedCost(traceBase), 1823)) + // TODO mainnet v5.0: Cyclic newCost here (1823/1825/1827), even for same Activated/ErgoTree version combination + // Also, costDetails are ignored. + contains.checkVerify(input, Expected(Success(okContains), 37850, TracedCost(traceBase), 1823)) get.checkVerify(input, Expected(value = Success(valueOpt), cost = 38372)) } From 894903cb767484d073824ed637c9762971d59537 Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Wed, 6 Apr 2022 09:33:42 +0200 Subject: [PATCH 15/44] v5.0-jitc-for-changed-feature-testing: Enable jitc for ChangedFeature --- .../scala/special/sigma/SigmaDslTesting.scala | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala index 8f90dab653..cabb2c0f3a 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala @@ -752,7 +752,27 @@ class SigmaDslTesting extends PropSpec expected: Expected[B], printTestCases: Boolean, failOnTestVectors: Boolean): Unit = { - checkEquality(input, printTestCases) + val funcRes = checkEquality(input, printTestCases) + val newRes = expected.newResults(ergoTreeVersionInTests) + val expectedTrace = newRes._2.fold(Seq.empty[CostItem])(_.trace) + println(s"expected trace: $expectedTrace") + if (expectedTrace.isEmpty) { + // new cost expectation is missing, print out actual cost results + if (evalSettings.printTestVectors) { + funcRes.foreach { case (_, newCost) => + printCostDetails(script, newCost) + } + } + } + else { + // new cost expectation is specified, compare it with the actual result + funcRes.foreach { case (_, newCost) => + if (newCost.trace != expectedTrace) { + printCostDetails(script, newCost) + newCost.trace shouldBe expectedTrace + } + } + } checkVerify(input, expected) } } From b109f8169a196a34e024a204085a8cee41f75978 Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Wed, 6 Apr 2022 09:40:54 +0200 Subject: [PATCH 16/44] v5.0-jitc-for-changed-feature-testing: Add jitc to 'Random headers access and comparison' test --- .../special/sigma/SigmaDslSpecification.scala | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 158c7e6987..2f876a8814 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -9323,6 +9323,31 @@ class SigmaDslSpecification extends SigmaDslTesting */ property("Random headers access and comparison (originaly from spam tests)") { val (_, _, _, ctx, _, _) = contextData() + val costDetails = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(PropertyCall), + FixedCostItem(SContext.headersMethod, FixedCost(JitCost(15))), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(PropertyCall), + SeqCostItem(MethodDesc(SCollection.IndicesMethod), PerItemCost(JitCost(20), JitCost(2), 16), 1), + FixedCostItem(Constant), + FixedCostItem(ValUse), + FixedCostItem(SizeOf), + FixedCostItem(Constant), + TypeBasedCostItem(ArithOp.Minus, SInt), + SeqCostItem(CompanionDesc(Slice), PerItemCost(JitCost(10), JitCost(2), 100), 0), + FixedCostItem(FuncValue), + SeqCostItem(CompanionDesc(ForAll), PerItemCost(JitCost(3), JitCost(1), 10), 0) + ) + ) if (lowerMethodCallsInTests) { verifyCases( @@ -9332,11 +9357,7 @@ class SigmaDslSpecification extends SigmaDslTesting cost = 37694, expectedDetails = CostDetails.ZeroCost, newCost = 1796, - newVersionedResults = Seq( - 0 -> (ExpectedResult(Success(true), Some(1796)) -> None), - 1 -> (ExpectedResult(Success(true), Some(1796)) -> None), - 2 -> (ExpectedResult(Success(true), Some(1796)) -> None) - ) + newVersionedResults = (0 to 2).map(i => i -> (ExpectedResult(Success(true), Some(1796)) -> Some(costDetails))) ) ), changedFeature( From 24917d30026f250a0a8c186d774936cc31621fa5 Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Wed, 6 Apr 2022 10:53:25 +0200 Subject: [PATCH 17/44] v5.0-jitc-for-changed-feature-testing: Add jitc to 'Context properties equivalence' test --- .../test/scala/special/sigma/SigmaDslSpecification.scala | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 2f876a8814..1ec71f93f5 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -4861,6 +4861,12 @@ class SigmaDslSpecification extends SigmaDslTesting )), preGeneratedSamples = Some(samples)) + val selfCostDetails = TracedCost( + traceBase ++ Array( + FixedCostItem(PropertyCall), + FixedCostItem(SContext.selfBoxIndexMethod, FixedCost(JitCost(20))) + ) + ) verifyCases( Seq( (ctx, Expected( @@ -4869,7 +4875,7 @@ class SigmaDslSpecification extends SigmaDslTesting expectedDetails = CostDetails.ZeroCost, newCost = 1786, newVersionedResults = { - val res = (ExpectedResult(Success(0), Some(1786)) -> None) + val res = (ExpectedResult(Success(0), Some(1786)) -> Some(selfCostDetails)) Seq(0, 1, 2).map(version => version -> res) })) ), From cb3d771dad7b5467084a35d537f1119ef744ca60 Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Wed, 6 Apr 2022 15:25:50 +0200 Subject: [PATCH 18/44] v5.0-jitc-for-changed-feature-testing: Add jitc to 'xorOf equivalence' test --- .../special/sigma/SigmaDslSpecification.scala | 51 +++++++++---------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 1ec71f93f5..a5dcd5e7fd 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -6016,39 +6016,36 @@ class SigmaDslSpecification extends SigmaDslTesting } property("xorOf equivalence") { + def costDetails(i: Int) = TracedCost(traceBase :+ SeqCostItem(CompanionDesc(XorOf), PerItemCost(JitCost(20), JitCost(5), 32), i)) verifyCases( { - def successNew[T](v: T, c: Int, newV: T, newC: Int) = Expected( + def successNew[T](v: T, c: Int, newV: T, cd: CostDetails) = Expected( value = Success(v), cost = c, expectedDetails = CostDetails.ZeroCost, - newCost = newC, - newVersionedResults = Seq( - 0 -> (ExpectedResult(Success(newV), Some(newC)) -> None), - 1 -> (ExpectedResult(Success(newV), Some(newC)) -> None), - 2 -> (ExpectedResult(Success(newV), Some(newC)) -> None) - )) - val newCost = 1786 + newCost = 1786, + newVersionedResults = Seq(0, 1, 2).map(i => i -> (ExpectedResult(Success(newV), Some(1786)) -> Some(cd))) + ) Seq( - (Coll[Boolean](), successNew(false, 37061, newV = false, newCost)), - (Coll[Boolean](false), successNew(false, 37071, newV = false, newCost)), - (Coll[Boolean](true), successNew(false, 37071, newV = true, newCost)), - (Coll[Boolean](false, false), successNew(false, 37081, newV = false, newCost)), - (Coll[Boolean](false, true), successNew(true, 37081, newV = true, newCost)), - (Coll[Boolean](true, false), successNew(true, 37081, newV = true, newCost)), - (Coll[Boolean](true, true), successNew(false, 37081, newV = false, newCost)), - (Coll[Boolean](false, false, false), successNew(false, 37091, newV = false, newCost)), - (Coll[Boolean](false, false, true), successNew(true, 37091, newV = true, newCost)), - (Coll[Boolean](false, true, false), successNew(true, 37091, newV = true, newCost)), - (Coll[Boolean](false, true, true), successNew(true, 37091, newV = false, newCost)), - (Coll[Boolean](true, false, false), successNew(true, 37091, newV = true, newCost)), - (Coll[Boolean](true, false, true), successNew(true, 37091, newV = false, newCost)), - (Coll[Boolean](true, true, false), successNew(true, 37091, newV = false, newCost)), - (Coll[Boolean](true, true, true), successNew(false, 37091, newV = true, newCost)), - (Coll[Boolean](false, false, false, false), successNew(false, 37101, newV = false, newCost)), - (Coll[Boolean](false, false, false, true), successNew(true, 37101, newV = true, newCost)), - (Coll[Boolean](false, false, true, false), successNew(true, 37101, newV = true, newCost)), - (Coll[Boolean](false, false, true, true), successNew(true, 37101, newV = false, newCost)) + (Coll[Boolean](), successNew(false, 37061, newV = false, costDetails(0))), + (Coll[Boolean](false), successNew(false, 37071, newV = false, costDetails(1))), + (Coll[Boolean](true), successNew(false, 37071, newV = true, costDetails(1))), + (Coll[Boolean](false, false), successNew(false, 37081, newV = false, costDetails(2))), + (Coll[Boolean](false, true), successNew(true, 37081, newV = true, costDetails(2))), + (Coll[Boolean](true, false), successNew(true, 37081, newV = true, costDetails(2))), + (Coll[Boolean](true, true), successNew(false, 37081, newV = false, costDetails(2))), + (Coll[Boolean](false, false, false), successNew(false, 37091, newV = false, costDetails(3))), + (Coll[Boolean](false, false, true), successNew(true, 37091, newV = true, costDetails(3))), + (Coll[Boolean](false, true, false), successNew(true, 37091, newV = true, costDetails(3))), + (Coll[Boolean](false, true, true), successNew(true, 37091, newV = false, costDetails(3))), + (Coll[Boolean](true, false, false), successNew(true, 37091, newV = true, costDetails(3))), + (Coll[Boolean](true, false, true), successNew(true, 37091, newV = false, costDetails(3))), + (Coll[Boolean](true, true, false), successNew(true, 37091, newV = false, costDetails(3))), + (Coll[Boolean](true, true, true), successNew(false, 37091, newV = true, costDetails(3))), + (Coll[Boolean](false, false, false, false), successNew(false, 37101, newV = false, costDetails(4))), + (Coll[Boolean](false, false, false, true), successNew(true, 37101, newV = true, costDetails(4))), + (Coll[Boolean](false, false, true, false), successNew(true, 37101, newV = true, costDetails(4))), + (Coll[Boolean](false, false, true, true), successNew(true, 37101, newV = false, costDetails(4))) ) }, changedFeature( From 8a8e9c066247ae4616c62184199267189be99968 Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Wed, 6 Apr 2022 15:43:14 +0200 Subject: [PATCH 19/44] v5.0-jitc-for-changed-feature-testing: Add jitc to 'Global.xor equivalence' test --- .../scala/special/sigma/SigmaDslSpecification.scala | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index a5dcd5e7fd..b73db6a237 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -6259,6 +6259,14 @@ class SigmaDslSpecification extends SigmaDslTesting property("Global.xor equivalence") { if (lowerMethodCallsInTests) { + val costDetails = TracedCost( + traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + SeqCostItem(CompanionDesc(Xor), PerItemCost(JitCost(10), JitCost(2), 128), 1) + ) + ) verifyCases( { def success[T](v: T) = Expected(Success(v), 36903) @@ -6273,7 +6281,7 @@ class SigmaDslSpecification extends SigmaDslTesting expectedDetails = CostDetails.ZeroCost, newCost = 1789, newVersionedResults = { - val res = (ExpectedResult(Success(Helpers.decodeBytes("00")), Some(1789)), None) + val res = (ExpectedResult(Success(Helpers.decodeBytes("00")), Some(1789)), Some(costDetails)) Seq(0, 1, 2).map(version => version -> res) } )), From f616d68cfc7fe9d6c46d04ff08c681b2a4a0073e Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Wed, 6 Apr 2022 15:43:49 +0200 Subject: [PATCH 20/44] v5.0-jitc-for-changed-feature-testing: Add jitc to 'Option fold workaround method' test --- .../special/sigma/SigmaDslSpecification.scala | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index b73db6a237..45b9665de9 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -8720,9 +8720,23 @@ class SigmaDslSpecification extends SigmaDslTesting property("Option fold workaround method") { val costDetails1 = TracedCost( traceBase ++ Array( - FixedCostItem(MethodCall), + FixedCostItem(OptionIsDefined), + FixedCostItem(If), + FixedCostItem(Constant) + ) + ) + val costDetails2 = TracedCost( + traceBase ++ Array( + FixedCostItem(OptionIsDefined), + FixedCostItem(If), + FixedCostItem(Apply), FixedCostItem(FuncValue), - FixedCostItem(SOption.FilterMethod, FixedCost(JitCost(20))) + FixedCostItem(ValUse), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(Constant), + TypeBasedCostItem(ArithOp.Plus, SLong) ) ) val n = ExactNumeric.LongIsExactNumeric @@ -8734,11 +8748,11 @@ class SigmaDslSpecification extends SigmaDslTesting expectedDetails = CostDetails.ZeroCost, newCost = 1786, newVersionedResults = Seq( - 2 -> (ExpectedResult(Success(5L), Some(1786)) -> None) + 2 -> (ExpectedResult(Success(5L), Some(1786)) -> Some(costDetails1)) ) )), (Some(0L) -> Expected(Success(1L), cost = 39012, - expectedDetails = CostDetails.ZeroCost, + expectedDetails = costDetails2, expectedNewCost = 1794)), (Some(Long.MaxValue) -> Expected(new ArithmeticException("long overflow"))) ), From fc950855a5a079476381b2bcda715bf1f97bf7ea Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Wed, 6 Apr 2022 16:08:11 +0200 Subject: [PATCH 21/44] v5.0-jitc-for-changed-feature-testing: Add jitc to 'substConstants equivalence' test --- .../src/main/scala/sigmastate/Values.scala | 2 +- .../special/sigma/SigmaDslSpecification.scala | 29 +++++++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/sigmastate/src/main/scala/sigmastate/Values.scala b/sigmastate/src/main/scala/sigmastate/Values.scala index 5e015d2ceb..fe177ecb68 100644 --- a/sigmastate/src/main/scala/sigmastate/Values.scala +++ b/sigmastate/src/main/scala/sigmastate/Values.scala @@ -998,7 +998,7 @@ object Values { Colls.fromArray(is) } } - object ConcreteCollection extends ValueCompanion { + object ConcreteCollection extends FixedCostValueCompanion { override def opCode: OpCode = ConcreteCollectionCode /** Cost of: allocating new collection * @see ConcreteCollection_PerItem */ diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 45b9665de9..0cdb3e3d1c 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -9278,10 +9278,21 @@ class SigmaDslSpecification extends SigmaDslTesting ErgoTree.ConstantSegregationHeader, Vector(IntConstant(10)), BoolToSigmaProp(EQ(ConstantPlaceholder(0, SInt), IntConstant(20)))) - + def costDetails(i: Int) = TracedCost( + traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(ConcreteCollection), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ConcreteCollection), + FixedCostItem(Constant), + FixedCostItem(BoolToSigmaProp), + SeqCostItem(CompanionDesc(SubstConstants), PerItemCost(JitCost(100), JitCost(100), 1), i) + ) + ) verifyCases( { - def success[T](v: T) = Expected(Success(v), 37694) + def success[T](v: T, cd: CostDetails, cost: Int) = Expected(Success(v), 37694, cd, cost) Seq( (Helpers.decodeBytes(""), 0) -> Expected(new java.nio.BufferUnderflowException()), @@ -9293,7 +9304,7 @@ class SigmaDslSpecification extends SigmaDslTesting expectedDetails = CostDetails.ZeroCost, newCost = 1803, newVersionedResults = { - val res = (ExpectedResult(Success(Helpers.decodeBytes("0008d3")), Some(1803)) -> None) + val res = (ExpectedResult(Success(Helpers.decodeBytes("0008d3")), Some(1803)) -> Some(costDetails(0))) Seq(0, 1, 2).map(version => version -> res) }), @@ -9304,16 +9315,16 @@ class SigmaDslSpecification extends SigmaDslTesting newCost = 1803, newVersionedResults = { // since the tree without constant segregation, substitution has no effect - val res = (ExpectedResult(Success(Helpers.decodeBytes("000008d3")), Some(1803)) -> None) + val res = (ExpectedResult(Success(Helpers.decodeBytes("000008d3")), Some(1803)) -> Some(costDetails(0))) Seq(0, 1, 2).map(version => version -> res) }), // tree with segregation flag, empty constants array - (Coll(t2.bytes:_*), 0) -> success(Helpers.decodeBytes("100008d3")), - (Helpers.decodeBytes("100008d3"), 0) -> success(Helpers.decodeBytes("100008d3")), + (Coll(t2.bytes:_*), 0) -> success(Helpers.decodeBytes("100008d3"), costDetails(0), 1803), + (Helpers.decodeBytes("100008d3"), 0) -> success(Helpers.decodeBytes("100008d3"), costDetails(0), 1803), // tree with one segregated constant - (Coll(t3.bytes:_*), 0) -> success(Helpers.decodeBytes("100108d27300")), - (Helpers.decodeBytes("100108d37300"), 0) -> success(Helpers.decodeBytes("100108d27300")), - (Coll(t3.bytes:_*), 1) -> success(Helpers.decodeBytes("100108d37300")), + (Coll(t3.bytes:_*), 0) -> success(Helpers.decodeBytes("100108d27300"), costDetails(1), 1813), + (Helpers.decodeBytes("100108d37300"), 0) -> success(Helpers.decodeBytes("100108d27300"), costDetails(1), 1813), + (Coll(t3.bytes:_*), 1) -> success(Helpers.decodeBytes("100108d37300"), costDetails(1), 1813), (Coll(t4.bytes:_*), 0) -> Expected(new IllegalArgumentException("requirement failed: expected new constant to have the same SInt$ tpe, got SSigmaProp")) ) }, From 325dfda8fa57605c4eafadc1dcde4aaf08e6dda8 Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Wed, 6 Apr 2022 16:13:07 +0200 Subject: [PATCH 22/44] v5.0-jitc-for-changed-feature-testing: Comments for tests which ignore jitc comparison --- .../special/sigma/SigmaDslSpecification.scala | 36 ++++++++++--------- .../scala/special/sigma/SigmaDslTesting.scala | 1 - 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 0cdb3e3d1c..ee6cfb30ae 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -2755,15 +2755,15 @@ class SigmaDslSpecification extends SigmaDslTesting } property("GroupElement.exp equivalence") { - val cases = { - val costDetails = TracedCost( - traceBase ++ Array( - FixedCostItem(SelectField), - FixedCostItem(ValUse), - FixedCostItem(SelectField), - FixedCostItem(Exponentiate) - ) + val costDetails = TracedCost( + traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(Exponentiate) ) + ) + val cases = { def success[T](v: T) = Expected(Success(v), 41484, costDetails, 1893) Seq( ((ge1, CBigInt(new BigInteger("-25c80b560dd7844e2efd10f80f7ee57d", 16))), @@ -2781,7 +2781,8 @@ class SigmaDslSpecification extends SigmaDslTesting if (lowerMethodCallsInTests) { verifyCases(cases, existingFeature( - scalaFunc, script, + scalaFunc, + script, FuncValue( Vector((1, STuple(Vector(SGroupElement, SBigInt)))), Exponentiate( @@ -2813,9 +2814,10 @@ class SigmaDslSpecification extends SigmaDslTesting Some(999) ) Seq( // expected result for each version - 0 -> ( res -> None ), - 1 -> ( res -> None ), - 2 -> ( res -> None ) + // TODO mainnet v5: costDetails comparison is ignored + 0 -> ( res -> Some(costDetails) ), + 1 -> ( res -> Some(TracedCost(traceBase)) ), + 2 -> ( res -> Some(TracedCost(traceBase)) ) ) } ) @@ -2904,8 +2906,9 @@ class SigmaDslSpecification extends SigmaDslTesting Success(Helpers.decodeGroupElement("02bc48937b4a66f249a32dfb4d2efd0743dc88d46d770b8c5d39fd03325ba211df")), Some(139) ) + // TODO mainnet v5: costDetails are not compared, is that ok? Seq( // expected result for each version - 0 -> ( res -> None ), + 0 -> ( res -> Some(TracedCost(traceBase)) ), 1 -> ( res -> None ), 2 -> ( res -> None ) ) @@ -4871,7 +4874,6 @@ class SigmaDslSpecification extends SigmaDslTesting Seq( (ctx, Expected( Success(-1), cost = 36318, - // TODO mainnet v5.0: Change to concrete details does not change test result expectedDetails = CostDetails.ZeroCost, newCost = 1786, newVersionedResults = { @@ -6323,8 +6325,9 @@ class SigmaDslSpecification extends SigmaDslTesting Success(Helpers.decodeBytes("00")), Some(116) ) + // TODO mainnet v5: costDetails are not compared, is that ok? Seq( // expected result for each version - 0 -> ( res -> None ), + 0 -> ( res -> Some(TracedCost(traceBase)) ), 1 -> ( res -> None ), 2 -> ( res -> None ) ) @@ -7993,8 +7996,9 @@ class SigmaDslSpecification extends SigmaDslTesting newVersionedResults = { // in v5.0 MethodCall ErgoTree node is allowed val res = ExpectedResult( Success(1), Some(166) ) + // TODO mainnet v5: costDetails are not compared, is that ok? Seq( // expected result for each version - 0 -> ( res -> None ), + 0 -> ( res -> Some(TracedCost(traceBase)) ), 1 -> ( res -> None ), 2 -> ( res -> None ) ) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala index cabb2c0f3a..bc682902c0 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala @@ -755,7 +755,6 @@ class SigmaDslTesting extends PropSpec val funcRes = checkEquality(input, printTestCases) val newRes = expected.newResults(ergoTreeVersionInTests) val expectedTrace = newRes._2.fold(Seq.empty[CostItem])(_.trace) - println(s"expected trace: $expectedTrace") if (expectedTrace.isEmpty) { // new cost expectation is missing, print out actual cost results if (evalSettings.printTestVectors) { From 261bc3cabd4a181cba53afd2c7d3883db075d137 Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Wed, 6 Apr 2022 16:19:11 +0200 Subject: [PATCH 23/44] v5.0-jitc-for-changed-feature-testing: Add jitc to 'Global.xor equivalence' test --- .../special/sigma/SigmaDslSpecification.scala | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index ee6cfb30ae..59720ab3f2 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -6261,35 +6261,35 @@ class SigmaDslSpecification extends SigmaDslTesting property("Global.xor equivalence") { if (lowerMethodCallsInTests) { - val costDetails = TracedCost( + def costDetails(i: Int) = TracedCost( traceBase ++ Array( FixedCostItem(SelectField), FixedCostItem(ValUse), FixedCostItem(SelectField), - SeqCostItem(CompanionDesc(Xor), PerItemCost(JitCost(10), JitCost(2), 128), 1) + SeqCostItem(CompanionDesc(Xor), PerItemCost(JitCost(10), JitCost(2), 128), i) ) ) verifyCases( { - def success[T](v: T) = Expected(Success(v), 36903) + def success[T](v: T, cd: CostDetails) = Expected(Success(v), 36903, cd, 1789) Seq( - ((Helpers.decodeBytes(""), Helpers.decodeBytes("")), success(Helpers.decodeBytes(""))), - ((Helpers.decodeBytes("01"), Helpers.decodeBytes("01")), success(Helpers.decodeBytes("00"))), - ((Helpers.decodeBytes("0100"), Helpers.decodeBytes("0101")), success(Helpers.decodeBytes("0001"))), - ((Helpers.decodeBytes("01"), Helpers.decodeBytes("0101")), success(Helpers.decodeBytes("00"))), + ((Helpers.decodeBytes(""), Helpers.decodeBytes("")), success(Helpers.decodeBytes(""), costDetails(0))), + ((Helpers.decodeBytes("01"), Helpers.decodeBytes("01")), success(Helpers.decodeBytes("00"), costDetails(1))), + ((Helpers.decodeBytes("0100"), Helpers.decodeBytes("0101")), success(Helpers.decodeBytes("0001"), costDetails(2))), + ((Helpers.decodeBytes("01"), Helpers.decodeBytes("0101")), success(Helpers.decodeBytes("00"), costDetails(1))), ((Helpers.decodeBytes("0100"), Helpers.decodeBytes("01")) -> Expected(Failure(new ArrayIndexOutOfBoundsException("1")), cost = 0, expectedDetails = CostDetails.ZeroCost, newCost = 1789, newVersionedResults = { - val res = (ExpectedResult(Success(Helpers.decodeBytes("00")), Some(1789)), Some(costDetails)) + val res = (ExpectedResult(Success(Helpers.decodeBytes("00")), Some(1789)), Some(costDetails(1))) Seq(0, 1, 2).map(version => version -> res) } )), ((Helpers.decodeBytes("800136fe89afff802acea67128a0ff007fffe3498c8001806080012b"), Helpers.decodeBytes("648018010a5d5800f5b400a580e7b4809b0cd273ff1230bfa800017f7fdb002749b3ac2b86ff")), - success(Helpers.decodeBytes("e4812eff83f2a780df7aa6d4a8474b80e4f3313a7392313fc8800054"))) + success(Helpers.decodeBytes("e4812eff83f2a780df7aa6d4a8474b80e4f3313a7392313fc8800054"), costDetails(28))) ) }, changedFeature( From 365479817cb04b82921d9ecc0e6121cd565bb013 Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Thu, 7 Apr 2022 01:24:51 +0200 Subject: [PATCH 24/44] v5.0-jitc-for-changed-feature-testing: Comment --- sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala index bc682902c0..d3da80f7ad 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala @@ -753,6 +753,9 @@ class SigmaDslTesting extends PropSpec printTestCases: Boolean, failOnTestVectors: Boolean): Unit = { val funcRes = checkEquality(input, printTestCases) + // TODO mainnet v5: How to handle different results (for features that changed behaviour) + // uncomment to see failing tests + // checkResult(funcRes.map(_._1), expected.value, failOnTestVectors) val newRes = expected.newResults(ergoTreeVersionInTests) val expectedTrace = newRes._2.fold(Seq.empty[CostItem])(_.trace) if (expectedTrace.isEmpty) { From 2bbc2564ce945d904413fd8020f07458ad433881 Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Thu, 7 Apr 2022 09:41:23 +0200 Subject: [PATCH 25/44] v5.0-jitc-failing-Coll-flatmap-equivalence: Remove unused method and comment. --- .../src/test/scala/special/sigma/SigmaDslSpecification.scala | 2 -- 1 file changed, 2 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index ff338367d2..f026505c92 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -6504,9 +6504,7 @@ class SigmaDslSpecification extends SigmaDslTesting ) verifyCases( { - def success[T](v: T, c: Int) = Expected(Success(v), c) Seq( - // TODO mainnet v5.0: ActivatedVersion = 2; ErgoTree version = 0 requres costDetails1. (Coll[Box](), Expected(Success(Coll[Byte]()), 38126, costDetails1, 1793)), (Coll[Box](b1), Expected(Success(Helpers.decodeBytes( "0008ce02c1a9311ecf1e76c787ba4b1c0e10157b4f6d1e4db3ef0d84f411c99f2d4d2c5b027d1bd9a437e73726ceddecc162e5c85f79aee4798505bc826b8ad1813148e4190257cff6d06fe15d1004596eeb97a7f67755188501e36adc49bd807fe65e9d8281033c6021cff6ba5fdfc4f1742486030d2ebbffd9c9c09e488792f3102b2dcdabd5" From 599a0e0bd17fc76ef082da3c23bfba0db3c7b812 Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Wed, 13 Apr 2022 14:24:09 +0200 Subject: [PATCH 26/44] v5.0-jitc-failing-avltree-contains: Hardcoded scenario for some avltree tests. --- .../special/sigma/SigmaDslSpecification.scala | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index bcc493bfca..2166198ac3 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -3261,17 +3261,18 @@ class SigmaDslSpecification extends SigmaDslTesting getMany.checkExpected(input, Expected(Success(expRes), 0))//, TracedCost(traceBase), 1700)) } - val (keysArr, valuesArr, _, avlProver) = sampleAvlProver - val key = keysArr(0) - val value = valuesArr(0) + val key = Colls.fromArray(Array[Byte](-16,-128,99,86,1,-128,-36,-83,109,72,-124,-114,1,-32,15,127,-30,125,127,1,-102,-53,-53,-128,-107,0,64,8,1,127,22,1)) + val value = Colls.fromArray(Array[Byte](0,41,-78,1,113,-128,0,-128,1,-92,0,1,1,34,127,-1,56,-1,-60,89,1,-20,-92)) + val (tree, avlProver) = createAvlTreeAndProver(key -> value) val otherKey = key.map(x => (-x).toByte) // any other different from key - val table = Table(("key", "contains", "valueOpt"), - (key, true, Some(value)), - (otherKey, false, None) + // Final cost is baseCost + additionalCost, baseCost is specified in test scenario below + val table = Table(("key", "contains", "valueOpt", "additionalCost"), + (key, true, Some(value), 2), + (otherKey, false, None, 0) ) - forAll(table) { (key, okContains, valueOpt) => + forAll(table) { (key, okContains, valueOpt, additionalCost) => avlProver.performOneOperation(Lookup(ADKey @@ key.toArray)) val proof = avlProver.generateProof().toColl val digest = avlProver.digest.toColl @@ -3283,10 +3284,8 @@ class SigmaDslSpecification extends SigmaDslTesting contains.checkExpected(input, success(okContains)) get.checkExpected(input, success(valueOpt)) - // TODO mainnet v5.0: Cyclic newCost here (1823/1825/1827), even for same Activated/ErgoTree version combination - // Also, costDetails are ignored. - contains.checkVerify(input, Expected(Success(okContains), 37850, TracedCost(traceBase), 1823)) - get.checkVerify(input, Expected(value = Success(valueOpt), cost = 38372)) + contains.checkVerify(input, Expected(Success(okContains), 37850, CostDetails.ZeroCost, 1810)) + get.checkVerify(input, Expected(value = Success(valueOpt), cost = 38372, CostDetails.ZeroCost, 1810 + additionalCost)) } val keys = Colls.fromItems(key) @@ -3295,14 +3294,14 @@ class SigmaDslSpecification extends SigmaDslTesting { val input = (tree, (keys, proof)) getMany.checkExpected(input, success(expRes)) - getMany.checkVerify(input, Expected(value = Success(expRes), cost = 38991)) + getMany.checkVerify(input, Expected(value = Success(expRes), cost = 38991, CostDetails.ZeroCost, 1811 + additionalCost)) } { val input = (tree, digest) val (res, _) = updateDigest.checkEquality(input).getOrThrow res.digest shouldBe digest - updateDigest.checkVerify(input, Expected(value = Success(res), cost = 36341)) + updateDigest.checkVerify(input, Expected(value = Success(res), cost = 36341, CostDetails.ZeroCost, 1791)) } val newOps = 1.toByte @@ -3311,7 +3310,7 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (tree, newOps) val (res,_) = updateOperations.checkEquality(input).getOrThrow res.enabledOperations shouldBe newOps - updateOperations.checkVerify(input, Expected(value = Success(res), cost = 36341)) + updateOperations.checkVerify(input, Expected(value = Success(res), cost = 36341, CostDetails.ZeroCost, 1791)) } // negative tests: invalid proof @@ -3321,7 +3320,7 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (tree, (key, invalidProof)) val (res, _) = contains.checkEquality(input).getOrThrow res shouldBe false - contains.checkVerify(input, Expected(value = Success(res), cost = 37850)) + contains.checkVerify(input, Expected(value = Success(res), cost = 37850, CostDetails.ZeroCost, 1810)) } { From 28da0296380125bbf6e13d6aca15f9e03613fca4 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Fri, 15 Apr 2022 16:51:03 +0200 Subject: [PATCH 27/44] v5.0-jitc-for-changed-feature-testing: checking CostDetails --- .../special/sigma/SigmaDslSpecification.scala | 90 ++++++++++++++----- .../scala/special/sigma/SigmaDslTesting.scala | 9 +- 2 files changed, 75 insertions(+), 24 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 59720ab3f2..d56b5e5db3 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -115,10 +115,17 @@ class SigmaDslSpecification extends SigmaDslTesting forAll(table) { (x, expectedRes) => val res = f.checkEquality(x) val resValue = res.map(_._1) - val expected = expectedRes.newResults(ergoTreeVersionInTests)._1 + val (expected, expDetailsOpt) = expectedRes.newResults(ergoTreeVersionInTests) checkResult(resValue, expected.value, failOnTestVectors = true) - if (res.isSuccess) { - res.get._2.cost shouldBe JitCost(expected.verificationCost.get) + res match { + case Success((value, details)) => + details.cost shouldBe JitCost(expected.verificationCost.get) + expDetailsOpt.foreach(expDetails => + if (details.trace != expDetails.trace) { + printCostDetails(f.script, details) + details.trace shouldBe expDetails.trace + } + ) } } } @@ -2813,11 +2820,19 @@ class SigmaDslSpecification extends SigmaDslTesting Success(Helpers.decodeGroupElement("023a850181b7b73f92a5bbfa0bfc78f5bbb6ff00645ddde501037017e1a2251e2e")), Some(999) ) + val details = TracedCost( + traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(MethodCall), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(SGroupElement.ExponentiateMethod, FixedCost(JitCost(900))) + ) + ) Seq( // expected result for each version - // TODO mainnet v5: costDetails comparison is ignored - 0 -> ( res -> Some(costDetails) ), - 1 -> ( res -> Some(TracedCost(traceBase)) ), - 2 -> ( res -> Some(TracedCost(traceBase)) ) + 0 -> ( res -> Some(details) ), + 1 -> ( res -> Some(details) ), + 2 -> ( res -> Some(details) ) ) } ) @@ -2906,12 +2921,14 @@ class SigmaDslSpecification extends SigmaDslTesting Success(Helpers.decodeGroupElement("02bc48937b4a66f249a32dfb4d2efd0743dc88d46d770b8c5d39fd03325ba211df")), Some(139) ) - // TODO mainnet v5: costDetails are not compared, is that ok? - Seq( // expected result for each version - 0 -> ( res -> Some(TracedCost(traceBase)) ), - 1 -> ( res -> None ), - 2 -> ( res -> None ) - ) + val details = TracedCost(traceBase ++ Array( + FixedCostItem(SelectField), + FixedCostItem(MethodCall), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(SGroupElement.MultiplyMethod, FixedCost(JitCost(40))) + )) + Seq.tabulate(3)(_ -> ( res -> Some(details) )) // expected result for each version } ) ) @@ -6325,12 +6342,23 @@ class SigmaDslSpecification extends SigmaDslTesting Success(Helpers.decodeBytes("00")), Some(116) ) - // TODO mainnet v5: costDetails are not compared, is that ok? - Seq( // expected result for each version - 0 -> ( res -> Some(TracedCost(traceBase)) ), - 1 -> ( res -> None ), - 2 -> ( res -> None ) + val details = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(Global), + FixedCostItem(MethodCall), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + SeqCostItem(CompanionDesc(Xor), PerItemCost(JitCost(10), JitCost(2), 128), 1) + ) ) + Seq.tabulate(3)(v => v -> ( res -> Some(details) )) // expected result for each version } ) ) @@ -7996,12 +8024,28 @@ class SigmaDslSpecification extends SigmaDslTesting newVersionedResults = { // in v5.0 MethodCall ErgoTree node is allowed val res = ExpectedResult( Success(1), Some(166) ) - // TODO mainnet v5: costDetails are not compared, is that ok? - Seq( // expected result for each version - 0 -> ( res -> Some(TracedCost(traceBase)) ), - 1 -> ( res -> None ), - 2 -> ( res -> None ) + val details = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(MethodCall), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(SCollection.GetOrElseMethod, FixedCost(JitCost(30))) + ) ) + Seq.tabulate(3)(v => v -> ( res -> Some(details) )) // expected result for each version } ) ) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala index d3da80f7ad..e0ed9c4bbe 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala @@ -732,11 +732,18 @@ class SigmaDslTesting extends PropSpec if (!(newImpl eq oldImpl)) { // check the new implementation with Scala semantic - val (newRes, _) = VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) { + val (newRes, newDetails) = VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) { checkEq(scalaFuncNew)(newF)(input).get } val newExpectedRes = expected.newResults(ergoTreeVersionInTests) newRes shouldBe newExpectedRes._1.value.get +// newExpectedRes._2.foreach(expDetails => +// if (newDetails != expDetails) { +// printCostDetails(script, newCost) +// newCost.trace shouldBe expectedTrace + + // } +// ) } } From 4bcc60545454427a0d375d7b196d7e9a919ce670 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Fri, 15 Apr 2022 19:00:18 +0200 Subject: [PATCH 28/44] v5.0-jitc-for-changed-feature-testing: checking CostDetails (part 2) --- .../special/sigma/SigmaDslSpecification.scala | 6 +++--- .../scala/special/sigma/SigmaDslTesting.scala | 21 ++++++++++++------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index d56b5e5db3..40af920a4b 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -3277,8 +3277,8 @@ class SigmaDslSpecification extends SigmaDslTesting val tree = SigmaDsl.avlTree(AvlTreeFlags.ReadOnly.serializeToByte, digest, 32, None) val input = (tree, (keys, proof)) - // TODO mainnet v5.0: checkExpected is ignoring newCost/costDetails. Is that correct behaviour? - getMany.checkExpected(input, Expected(Success(expRes), 0))//, TracedCost(traceBase), 1700)) + // TODO mainnet v5.0: use stable (not random) input + getMany.checkExpected(input, Expected(Success(expRes), 0/*, costTrace, 1700*/)) } val (keysArr, valuesArr, _, avlProver) = sampleAvlProver @@ -3303,7 +3303,7 @@ class SigmaDslSpecification extends SigmaDslTesting contains.checkExpected(input, success(okContains)) get.checkExpected(input, success(valueOpt)) - // TODO mainnet v5.0: Cyclic newCost here (1823/1825), also costDetails are ignored + // TODO mainnet v5.0: use stable (not random) input contains.checkVerify(input, Expected(Success(okContains), 37850))//, TracedCost(traceBase), 1823)) get.checkVerify(input, Expected(value = Success(valueOpt), cost = 38372)) } diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala index e0ed9c4bbe..d74afc63d1 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala @@ -567,10 +567,16 @@ class SigmaDslTesting extends PropSpec if (!(newImpl eq oldImpl)) { // check the new implementation with Scala semantic - val (newRes, _) = VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) { + val (newRes, newDetails) = VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) { checkEq(scalaFunc)(newF)(input).get } newRes shouldBe expected.value.get + expected.newResults(ergoTreeVersionInTests)._2.foreach { expDetails => + if (newDetails.trace != expDetails.trace) { + printCostDetails(script, newDetails) + newDetails.trace shouldBe expDetails.trace + } + } } } @@ -737,13 +743,12 @@ class SigmaDslTesting extends PropSpec } val newExpectedRes = expected.newResults(ergoTreeVersionInTests) newRes shouldBe newExpectedRes._1.value.get -// newExpectedRes._2.foreach(expDetails => -// if (newDetails != expDetails) { -// printCostDetails(script, newCost) -// newCost.trace shouldBe expectedTrace - - // } -// ) + newExpectedRes._2.foreach { expDetails => + if (newDetails.trace != expDetails.trace) { + printCostDetails(script, newDetails) + newDetails.trace shouldBe expDetails.trace + } + } } } From fc7de985eaf65b6b3127f1d049e16e75435d5ead Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Fri, 15 Apr 2022 20:06:39 +0200 Subject: [PATCH 29/44] v5.0-jitc-for-changed-feature-testing: checking CostDetails (part 3) --- .../scala/special/sigma/SigmaDslSpecification.scala | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 40af920a4b..d1196b8362 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -7030,8 +7030,17 @@ class SigmaDslSpecification extends SigmaDslTesting { def success[T](v: T, c: Int) = Expected(Success(v), c) Seq( - // TODO mainnet v5.0: different results - costDetails1(Copy) and cost 1793/1800 (problematic version 2/2 ?) - // Coll[GroupElement]() -> Expected(Success(Coll[Byte]()), 40133, CostDetails.ZeroCost, 1793), + Coll[GroupElement]() -> Expected(Success(Coll[Byte]()), 40133, CostDetails.ZeroCost, 1794, + newVersionedResults = { + val res = ExpectedResult(Success(Coll[Byte]()), Some(1793)) + Seq.tabulate(3)(v => + v -> (res -> Some(TracedCost(traceBase ++ Array( + FixedCostItem(MethodCall), + FixedCostItem(FuncValue), + SeqCostItem(MethodDesc(SCollection.FlatMapMethod), PerItemCost(JitCost(60), JitCost(10), 8), 0) + )))) + ) + }), Coll[GroupElement]( Helpers.decodeGroupElement("02d65904820f8330218cf7318b3810d0c9ab9df86f1ee6100882683f23c0aee587"), Helpers.decodeGroupElement("0390e9daa9916f30d0bc61a8e381c6005edfb7938aee5bb4fc9e8a759c7748ffaa")) -> From f6d9991565d008bbe70db57f037bb5f26d952ff4 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Fri, 15 Apr 2022 21:46:12 +0200 Subject: [PATCH 30/44] v5.0-jitc-for-changed-feature-testing: checking CostDetails (part 4) --- .../test/scala/special/sigma/SigmaDslSpecification.scala | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index d1196b8362..322f8bcf8f 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -5092,7 +5092,11 @@ class SigmaDslSpecification extends SigmaDslTesting verifyCases( Seq( - ctx -> Expected(expectedError) + ctx -> Expected(Failure(expectedError), 0, CostDetails.ZeroCost, 1813, + newVersionedResults = { + Seq.tabulate(3)(v => v -> (ExpectedResult(Success(true), Some(1813)) -> None)) + } + ) ), changedFeature( scalaFunc = { (x: Context) => From 3f940146a7da86fea9a6a7a4e1d12413f1a9e890 Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Tue, 19 Apr 2022 15:14:28 +0200 Subject: [PATCH 31/44] v5.0-jitc-for-changed-feature-testing: Add jitc to 'Coll flatMap method equivalence' test --- .../special/sigma/SigmaDslSpecification.scala | 54 +++++++++++++++---- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index e6a6801a5d..1528fa0dd8 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -7014,6 +7014,13 @@ class SigmaDslSpecification extends SigmaDslTesting r <- Gen.choose(l, arr.length - 1) } yield (arr, (l, r)) property("Coll flatMap method equivalence") { + val costDetails0 = TracedCost( + traceBase ++ Array( + FixedCostItem(MethodCall), + FixedCostItem(FuncValue), + SeqCostItem(MethodDesc(SCollection.FlatMapMethod), PerItemCost(JitCost(60), JitCost(10), 8), 0) + ) + ) val costDetails1 = TracedCost( traceBase ++ Array( FixedCostItem(MethodCall), @@ -7023,26 +7030,50 @@ class SigmaDslSpecification extends SigmaDslTesting SeqCostItem(MethodDesc(SCollection.FlatMapMethod), PerItemCost(JitCost(60), JitCost(10), 8), 0) ) ) - val costDetails1Copy = TracedCost( + val costDetails2 = TracedCost( traceBase ++ Array( FixedCostItem(MethodCall), FixedCostItem(FuncValue), - SeqCostItem(MethodDesc(SCollection.FlatMapMethod), PerItemCost(JitCost(60), JitCost(10), 8), 0) + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(PropertyCall), + FixedCostItem(SGroupElement.GetEncodedMethod, FixedCost(JitCost(250))), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(PropertyCall), + FixedCostItem(SGroupElement.GetEncodedMethod, FixedCost(JitCost(250))), + SeqCostItem(MethodDesc(SCollection.FlatMapMethod), PerItemCost(JitCost(60), JitCost(10), 8), 66) ) ) + val costDetails3 = TracedCost( + traceBase ++ Array( + FixedCostItem(MethodCall), + FixedCostItem(FuncValue), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(PropertyCall), + FixedCostItem(SGroupElement.GetEncodedMethod, FixedCost(JitCost(250))), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(PropertyCall), + FixedCostItem(SGroupElement.GetEncodedMethod, FixedCost(JitCost(250))), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(PropertyCall), + FixedCostItem(SGroupElement.GetEncodedMethod, FixedCost(JitCost(250))), + SeqCostItem(MethodDesc(SCollection.FlatMapMethod), PerItemCost(JitCost(60), JitCost(10), 8), 99) + ) + ) + verifyCases( { - def success[T](v: T, c: Int) = Expected(Success(v), c) + def success[T](v: T, c: Int, cd: CostDetails, newCost: Int) = Expected(Success(v), c, cd, newCost) Seq( Coll[GroupElement]() -> Expected(Success(Coll[Byte]()), 40133, CostDetails.ZeroCost, 1794, newVersionedResults = { val res = ExpectedResult(Success(Coll[Byte]()), Some(1793)) Seq.tabulate(3)(v => - v -> (res -> Some(TracedCost(traceBase ++ Array( - FixedCostItem(MethodCall), - FixedCostItem(FuncValue), - SeqCostItem(MethodDesc(SCollection.FlatMapMethod), PerItemCost(JitCost(60), JitCost(10), 8), 0) - )))) + v -> (res -> Some(costDetails0)) ) }), Coll[GroupElement]( @@ -7050,14 +7081,14 @@ class SigmaDslSpecification extends SigmaDslTesting Helpers.decodeGroupElement("0390e9daa9916f30d0bc61a8e381c6005edfb7938aee5bb4fc9e8a759c7748ffaa")) -> success(Helpers.decodeBytes( "02d65904820f8330218cf7318b3810d0c9ab9df86f1ee6100882683f23c0aee5870390e9daa9916f30d0bc61a8e381c6005edfb7938aee5bb4fc9e8a759c7748ffaa" - ), 40213), + ), 40213, costDetails2, 1854), Coll[GroupElement]( Helpers.decodeGroupElement("02d65904820f8330218cf7318b3810d0c9ab9df86f1ee6100882683f23c0aee587"), Helpers.decodeGroupElement("0390e9daa9916f30d0bc61a8e381c6005edfb7938aee5bb4fc9e8a759c7748ffaa"), Helpers.decodeGroupElement("03bd839b969b02d218fd1192f2c80cbda9c6ce9c7ddb765f31b748f4666203df85")) -> success(Helpers.decodeBytes( "02d65904820f8330218cf7318b3810d0c9ab9df86f1ee6100882683f23c0aee5870390e9daa9916f30d0bc61a8e381c6005edfb7938aee5bb4fc9e8a759c7748ffaa03bd839b969b02d218fd1192f2c80cbda9c6ce9c7ddb765f31b748f4666203df85" - ), 40253) + ), 40253, costDetails3, 1884) ) }, existingFeature( @@ -7099,7 +7130,8 @@ class SigmaDslSpecification extends SigmaDslTesting val is = Coll((0 to 32):_*) is.append(is) }), - verificationCost = Some(817)) -> None) + // TODO mainnet v5.0: Ignored cost details comparison. + verificationCost = Some(817)) -> Some(TracedCost(traceBase))) )) ) val f = changedFeature( From be20e3a26f2e625f08ca6cf5a75dfb556ec64b3f Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Tue, 19 Apr 2022 15:15:30 +0200 Subject: [PATCH 32/44] v5.0-jitc-for-changed-feature-testing: Add jitc to 'Option fold workaround method' test --- .../test/scala/special/sigma/SigmaDslSpecification.scala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 1528fa0dd8..877371cd2f 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -8840,11 +8840,11 @@ class SigmaDslSpecification extends SigmaDslTesting cost = 0, expectedDetails = CostDetails.ZeroCost, newCost = 1786, - newVersionedResults = Seq( - 2 -> (ExpectedResult(Success(5L), Some(1786)) -> Some(costDetails1)) - ) + newVersionedResults = Seq.tabulate(3)(v => v -> (ExpectedResult(Success(5L), Some(1786)) -> Some(costDetails1))) )), - (Some(0L) -> Expected(Success(1L), cost = 39012, + (Some(0L) -> Expected( + Success(1L), + cost = 39012, expectedDetails = costDetails2, expectedNewCost = 1794)), (Some(Long.MaxValue) -> Expected(new ArithmeticException("long overflow"))) From 91cd31bacb1b05b6aeb87962a1b483c04826c916 Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Tue, 19 Apr 2022 18:48:10 +0200 Subject: [PATCH 33/44] v5.0-jitc-for-changed-feature-testing: Add jitc to 'Coll flatMap method equivalence' test --- .../special/sigma/SigmaDslSpecification.scala | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 877371cd2f..48bc3b7f1c 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -7064,6 +7064,25 @@ class SigmaDslSpecification extends SigmaDslTesting SeqCostItem(MethodDesc(SCollection.FlatMapMethod), PerItemCost(JitCost(60), JitCost(10), 8), 99) ) ) + val costDetails4 = TracedCost( + traceBase ++ Array( + FixedCostItem(MethodCall), + FixedCostItem(FuncValue), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(PropertyCall), + FixedCostItem(SGroupElement.GetEncodedMethod, FixedCost(JitCost(250))), + FixedCostItem(PropertyCall), + SeqCostItem(MethodDesc(SCollection.IndicesMethod), PerItemCost(JitCost(20), JitCost(2), 16), 33), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(PropertyCall), + FixedCostItem(SGroupElement.GetEncodedMethod, FixedCost(JitCost(250))), + FixedCostItem(PropertyCall), + SeqCostItem(MethodDesc(SCollection.IndicesMethod), PerItemCost(JitCost(20), JitCost(2), 16), 33), + SeqCostItem(MethodDesc(SCollection.FlatMapMethod), PerItemCost(JitCost(60), JitCost(10), 8), 66) + ) + ) verifyCases( { @@ -7130,8 +7149,7 @@ class SigmaDslSpecification extends SigmaDslTesting val is = Coll((0 to 32):_*) is.append(is) }), - // TODO mainnet v5.0: Ignored cost details comparison. - verificationCost = Some(817)) -> Some(TracedCost(traceBase))) + verificationCost = Some(817)) -> Some(costDetails4)) )) ) val f = changedFeature( @@ -7173,18 +7191,7 @@ class SigmaDslSpecification extends SigmaDslTesting allowDifferentErrors = true ) - val table = Table(("x", "y"), cases:_*) - forAll(table) { (x, expectedRes) => - val res = f.checkEquality(x) - val resValue = res.map(_._1) - if (activatedVersionInTests >= VersionContext.JitActivationVersion) { - val expected = expectedRes.newResults(ergoTreeVersionInTests)._1 - checkResult(resValue, expected.value, failOnTestVectors = true) - if (res.isSuccess) { - res.get._2.cost shouldBe JitCost(expected.verificationCost.get) - } - } - } + testCases(cases, f) } val f = existingFeature( From d91406dbbf48f51e62806a569172ad7d8c98d91f Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Tue, 19 Apr 2022 23:32:08 +0200 Subject: [PATCH 34/44] v5.0-jitc-for-changed-feature-testing: Add jitc to 'AlvTree.{get,getMany,contains}' test --- .../special/sigma/SigmaDslSpecification.scala | 94 +++++++++++++++---- .../scala/special/sigma/SigmaDslTesting.scala | 1 + 2 files changed, 79 insertions(+), 16 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 48bc3b7f1c..1b5deaa1c0 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -3262,13 +3262,26 @@ class SigmaDslSpecification extends SigmaDslTesting ) )) - def success[T](v: T) = Expected(Success(v), 0) - - { // getMany with a bunch of existing keys - val (keysArr, valuesArr, _, avlProver) = sampleAvlProver - val keys = Colls.fromArray(keysArr.slice(0, 20)) - val expRes = Colls.fromArray(valuesArr.slice(0, 20).map(Option(_))) - + { + val keysArr = + Array( + Colls.fromArray(Array[Byte](0,14,54,-1,-128,-61,-89,72,27,34,1,-1,62,-48,0,8,74,1,82,-48,-41,100,-66,64,127,45,1,-1,0,-128,-16,-57)), + Colls.fromArray(Array[Byte](127,-128,-1,127,-96,-1,-63,1,25,43,127,-122,127,-43,0,-1,-57,113,-6,1,1,0,15,1,-104,-1,-55,101,115,-94,-128,94)), + Colls.fromArray(Array[Byte](-60,-26,117,124,-92,104,106,-1,-1,-1,-29,121,-98,54,10,-105,127,13,-1,0,-90,0,-125,-13,-1,-79,-87,127,0,127,0,0)), + Colls.fromArray(Array[Byte](84,127,-44,-65,1,-77,39,-128,113,14,1,-128,127,48,-53,60,-5,-19,123,0,-128,127,-28,0,104,-1,0,1,-51,124,-78,67)), + Colls.fromArray(Array[Byte](12,56,-25,-128,1,-128,-128,70,19,37,0,-1,1,87,-20,-128,80,127,-66,-1,83,-1,111,0,-34,0,49,-77,1,0,61,-1)) + ) + val valuesArr = + Array( + Colls.fromArray(Array[Byte](-87,0,-102,88,-128,-54,66,1,-128,-16,0,1,-44,0,35,-32,-23,40,127,-97,49,-1,127,1,-84,115,127,61,-84,-63,-104,-9,-116,-26,9,-93,-128,0,11,127,0,-128,-1,-128,-1,127,-110,-128,0,0,90,-126,28,0,42,-71,-1,37,-26,0,124,-72,68,26,14)), + Colls.fromArray(Array[Byte](84,106,-48,-17,-1,44,127,-128,0,86,-1)), + Colls.fromArray(Array[Byte](127,-128,-60,1,118,-32,-72,-9,101,0,0,-68,-51,8,95,127)), + Colls.fromArray(Array[Byte](127,-88,127,-101,-128,77,-25,-72,-86,127,127,127,-88,0,-128)), + Colls.fromArray(Array[Byte](2,5,-128,127,46,0,1,127,-9,64,-13,0,19,-112,124,1,20,52,65,-31,-112,114,0,18,-1,-88,-128,118,-126,-1,0,112,119,-1,20,84,11,-23,113,-1,71,-77,127,11,1,-128,63,-23,0,127,-55,42,8,127,126,115,59,70,127,102,-109,-128,127,41,-128,127,127,15,1,127,80,-29,0,8,-127,-96,-1,37,106,76,0,-128,-128,-128,-102,52,0,-11,1,-1,71)) + ) + val (_, avlProver) = createAvlTreeAndProver(keysArr.zip(valuesArr):_*) + val keys = Colls.fromArray(keysArr) + val expRes = Colls.fromArray(valuesArr).map(Option(_)) keys.foreach { key => avlProver.performOneOperation(Lookup(ADKey @@ key.toArray)) } @@ -3277,8 +3290,34 @@ class SigmaDslSpecification extends SigmaDslTesting val tree = SigmaDsl.avlTree(AvlTreeFlags.ReadOnly.serializeToByte, digest, 32, None) val input = (tree, (keys, proof)) - // TODO mainnet v5.0: use stable (not random) input - getMany.checkExpected(input, Expected(Success(expRes), 0/*, costTrace, 1700*/)) + val costDetails = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(MethodCall), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + SeqCostItem(NamedDesc("CreateAvlVerifier"), PerItemCost(JitCost(110), JitCost(20), 64), 456), + SeqCostItem(NamedDesc("LookupAvlTree"), PerItemCost(JitCost(40), JitCost(10), 1), 3), + SeqCostItem(NamedDesc("LookupAvlTree"), PerItemCost(JitCost(40), JitCost(10), 1), 3), + SeqCostItem(NamedDesc("LookupAvlTree"), PerItemCost(JitCost(40), JitCost(10), 1), 3), + SeqCostItem(NamedDesc("LookupAvlTree"), PerItemCost(JitCost(40), JitCost(10), 1), 3), + SeqCostItem(NamedDesc("LookupAvlTree"), PerItemCost(JitCost(40), JitCost(10), 1), 3) + ) + ) + + getMany.checkExpected(input, Expected(Success(expRes), 38991, costDetails, 1865)) } val key = Colls.fromArray(Array[Byte](-16,-128,99,86,1,-128,-36,-83,109,72,-124,-114,1,-32,15,127,-30,125,127,1,-102,-53,-53,-128,-107,0,64,8,1,127,22,1)) @@ -3287,22 +3326,45 @@ class SigmaDslSpecification extends SigmaDslTesting val otherKey = key.map(x => (-x).toByte) // any other different from key // Final cost is baseCost + additionalCost, baseCost is specified in test scenario below - val table = Table(("key", "contains", "valueOpt", "additionalCost"), - (key, true, Some(value), 2), - (otherKey, false, None, 0) + val table = Table(("key", "contains", "valueOpt", "additionalCost", "additionalCostDetails"), + (key, true, Some(value), 2, 23), + (otherKey, false, None, 0, 0) ) - forAll(table) { (key, okContains, valueOpt, additionalCost) => + forAll(table) { (key, okContains, valueOpt, additionalCost, additionalDetails) => avlProver.performOneOperation(Lookup(ADKey @@ key.toArray)) val proof = avlProver.generateProof().toColl val digest = avlProver.digest.toColl val tree = SigmaDsl.avlTree(AvlTreeFlags.ReadOnly.serializeToByte, digest, 32, None) + def costDetails(i: Int) = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(MethodCall), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + SeqCostItem(NamedDesc("CreateAvlVerifier"), PerItemCost(JitCost(110), JitCost(20), 64), i), + SeqCostItem(NamedDesc("LookupAvlTree"), PerItemCost(JitCost(40), JitCost(10), 1), 1) + ) + ) + // positive test { val input = (tree, (key, proof)) - contains.checkExpected(input, success(okContains)) - get.checkExpected(input, success(valueOpt)) + contains.checkExpected(input, Expected(Success(okContains), 37850, costDetails(105 + additionalDetails), 1810)) + get.checkExpected(input, Expected(Success(valueOpt), 38372, costDetails(105 + additionalDetails), 1810 + additionalCost)) contains.checkVerify(input, Expected(Success(okContains), 37850, CostDetails.ZeroCost, 1810)) get.checkVerify(input, Expected(value = Success(valueOpt), cost = 38372, CostDetails.ZeroCost, 1810 + additionalCost)) @@ -3313,7 +3375,7 @@ class SigmaDslSpecification extends SigmaDslTesting { val input = (tree, (keys, proof)) - getMany.checkExpected(input, success(expRes)) + getMany.checkExpected(input, Expected(Success(expRes), 38991, costDetails(105 + additionalDetails), 1811 + additionalCost)) getMany.checkVerify(input, Expected(value = Success(expRes), cost = 38991, CostDetails.ZeroCost, 1811 + additionalCost)) } diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala index d74afc63d1..ba88ccc291 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala @@ -578,6 +578,7 @@ class SigmaDslTesting extends PropSpec } } } + checkVerify(input, expected) } override def testCase(input: A, From f9e05a80bd76baf99dfff58ab328c3a890afe6bb Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Wed, 20 Apr 2022 13:24:21 +0200 Subject: [PATCH 35/44] v5.0-jitc-for-changed-feature-testing: Add jitc to 'AvlTree.update equivalence' test --- .../special/sigma/SigmaDslSpecification.scala | 142 +++++++++++------- 1 file changed, 88 insertions(+), 54 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 1b5deaa1c0..316b4c4963 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -3633,67 +3633,101 @@ class SigmaDslSpecification extends SigmaDslTesting )) val cost = 40952 - def success[T](v: T) = Expected(Success(v), 0) + val testTraceBase = Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(MethodCall), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(SAvlTree.isUpdateAllowedMethod, FixedCost(JitCost(15))) + ) + val costDetails1 = TracedCost(testTraceBase) + val costDetails2 = TracedCost( + testTraceBase ++ Array( + SeqCostItem(NamedDesc("CreateAvlVerifier"), PerItemCost(JitCost(110), JitCost(20), 64), 111), + SeqCostItem(NamedDesc("UpdateAvlTree"), PerItemCost(JitCost(120), JitCost(20), 1), 1), + FixedCostItem(SAvlTree.updateDigestMethod, FixedCost(JitCost(40))) + ) + ) + val costDetails3 = TracedCost( + testTraceBase ++ Array( + SeqCostItem(NamedDesc("CreateAvlVerifier"), PerItemCost(JitCost(110), JitCost(20), 64), 111), + SeqCostItem(NamedDesc("UpdateAvlTree"), PerItemCost(JitCost(120), JitCost(20), 1), 1) + ) + ) - forAll(keyCollGen, bytesCollGen) { (key, value) => - val (_, avlProver) = createAvlTreeAndProver(key -> value) - val preUpdateDigest = avlProver.digest.toColl - val newValue = bytesCollGen.sample.get - val updateProof = performUpdate(avlProver, key, newValue) - val kvs = Colls.fromItems((key -> newValue)) - val endDigest = avlProver.digest.toColl + val key = Colls.fromArray(Array[Byte](1,0,1,1,73,-67,-128,1,1,0,93,0,127,87,95,51,1,127,1,-3,74,-66,-128,1,89,-18,1,-1,-62,0,-33,51)) + val value = Colls.fromArray(Array[Byte](1,-50,1,-128,120,1)) + val (_, avlProver) = createAvlTreeAndProver(key -> value) + val preUpdateDigest = avlProver.digest.toColl + // val newValue = bytesCollGen.sample.get + val newValue = Colls.fromArray(Array[Byte](2,-1,127,91,0,-1,-128,-1,38,-128,-105,-68,-128,-128,127,127,127,-74,88,127,127,127,-81,-30,-89,121,127,-1,-1,-34,127,1,-12,-128,108,75,127,-14,-63,-128,-103,127,1,-57,0,1,-128,127,-85,23,0,-128,70,-110,127,-85,-30,15,-1,-71,0,127,1,42,127,-118,-1,0,-53,126,42,0,127,127,0,-10,-1,127,19,-4,-1,-88,-128,-96,61,-116,127,-111,6,-128,-1,-86,-39,114,0,127,-92,40)) + val updateProof = performUpdate(avlProver, key, newValue) + val kvs = Colls.fromItems((key -> newValue)) + val endDigest = avlProver.digest.toColl - { // positive: update to newValue - val preUpdateTree = createTree(preUpdateDigest, updateAllowed = true) - val endTree = preUpdateTree.updateDigest(endDigest) - val input = (preUpdateTree, (kvs, updateProof)) - val res = Some(endTree) - update.checkExpected(input, success(res)) - update.checkVerify(input, Expected(Success(res), cost)) - } + { // positive: update to newValue + val preUpdateTree = createTree(preUpdateDigest, updateAllowed = true) + val endTree = preUpdateTree.updateDigest(endDigest) + val input = (preUpdateTree, (kvs, updateProof)) + val res = Some(endTree) + // TODO mainnet v5: Possible duplication of tests. + update.checkExpected(input, Expected(Success(res), cost, costDetails2, 1825)) + update.checkVerify(input, Expected(Success(res), cost)) + } - { // positive: update to the same value (identity operation) - val tree = createTree(preUpdateDigest, updateAllowed = true) - val keys = Colls.fromItems((key -> value)) - val input = (tree, (keys, updateProof)) - val res = Some(tree) - update.checkExpected(input, success(res)) - update.checkVerify(input, Expected(value = Success(res), cost = cost)) - } + { // positive: update to the same value (identity operation) + val tree = createTree(preUpdateDigest, updateAllowed = true) + val keys = Colls.fromItems((key -> value)) + val input = (tree, (keys, updateProof)) + val res = Some(tree) + update.checkExpected(input, Expected(Success(res), cost, costDetails2, 1825)) + update.checkVerify(input, Expected(Success(res), cost)) + } - { // negative: readonly tree - val readonlyTree = createTree(preUpdateDigest) - val input = (readonlyTree, (kvs, updateProof)) - update.checkExpected(input, success(None)) - update.checkVerify(input, Expected(value = Success(None), cost = cost)) - } + { // negative: readonly tree + val readonlyTree = createTree(preUpdateDigest) + val input = (readonlyTree, (kvs, updateProof)) + update.checkExpected(input, Expected(Success(None), cost, costDetails1, 1792)) + update.checkVerify(input, Expected(Success(None), cost)) + } - { // negative: invalid key - val tree = createTree(preUpdateDigest, updateAllowed = true) - val invalidKey = key.map(x => (-x).toByte) // any other different from key - val invalidKvs = Colls.fromItems((invalidKey -> newValue)) - val input = (tree, (invalidKvs, updateProof)) - update.checkExpected(input, success(None)) - update.checkVerify(input, Expected(value = Success(None), cost = cost)) - } + { // negative: invalid key + val tree = createTree(preUpdateDigest, updateAllowed = true) + val invalidKey = key.map(x => (-x).toByte) // any other different from key + val invalidKvs = Colls.fromItems((invalidKey -> newValue)) + val input = (tree, (invalidKvs, updateProof)) + update.checkExpected(input, Expected(Success(None), cost, costDetails3, 1821)) + update.checkVerify(input, Expected(Success(None), cost)) + } - { // negative: invalid value (different from the value in the proof) - val tree = createTree(preUpdateDigest, updateAllowed = true) - val invalidValue = newValue.map(x => (-x).toByte) - val invalidKvs = Colls.fromItems((key -> invalidValue)) - val input = (tree, (invalidKvs, updateProof)) - val (res, _) = update.checkEquality(input).getOrThrow - res.isDefined shouldBe true // TODO v6.0: should it really be true? (looks like a bug) - update.checkVerify(input, Expected(value = Success(res), cost = cost)) - } + { // negative: invalid value (different from the value in the proof) + val tree = createTree(preUpdateDigest, updateAllowed = true) + val invalidValue = newValue.map(x => (-x).toByte) + val invalidKvs = Colls.fromItems((key -> invalidValue)) + val input = (tree, (invalidKvs, updateProof)) + val (res, _) = update.checkEquality(input).getOrThrow + res.isDefined shouldBe true // TODO v6.0: should it really be true? (looks like a bug) + update.checkVerify(input, Expected(Success(res), cost)) + } - { // negative: invalid proof - val tree = createTree(preUpdateDigest, updateAllowed = true) - val invalidProof = updateProof.map(x => (-x).toByte) // any other different from proof - val input = (tree, (kvs, invalidProof)) - update.checkExpected(input, success(None)) - update.checkVerify(input, Expected(value = Success(None), cost = cost)) - } + { // negative: invalid proof + val tree = createTree(preUpdateDigest, updateAllowed = true) + val invalidProof = updateProof.map(x => (-x).toByte) // any other different from proof + val input = (tree, (kvs, invalidProof)) + update.checkExpected(input, Expected(Success(None), cost, costDetails3, 1821)) + update.checkVerify(input, Expected(Success(None), cost)) } } From 19607a7fe2303c318fd3baad81630a84b6a0f4be Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Wed, 20 Apr 2022 13:25:43 +0200 Subject: [PATCH 36/44] v5.0-jitc-for-changed-feature-testing: Add jitc to 'AvlTree.remove equivalence' test --- .../special/sigma/SigmaDslSpecification.scala | 77 ++++++++++++++++--- 1 file changed, 67 insertions(+), 10 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 316b4c4963..e50e24b8b3 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -3768,11 +3768,66 @@ class SigmaDslSpecification extends SigmaDslTesting ) )) - def success[T](v: T) = Expected(Success(v), 0) + val testTraceBase = Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(MethodCall), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(SAvlTree.isRemoveAllowedMethod, FixedCost(JitCost(15))) + ) + val costDetails1 = TracedCost( + testTraceBase ++ Array( + SeqCostItem(NamedDesc("CreateAvlVerifier"), PerItemCost(JitCost(110), JitCost(20), 64), 436), + SeqCostItem(NamedDesc("RemoveAvlTree"), PerItemCost(JitCost(100), JitCost(15), 1), 3), + SeqCostItem(NamedDesc("RemoveAvlTree"), PerItemCost(JitCost(100), JitCost(15), 1), 3), + FixedCostItem(SAvlTree.digestMethod, FixedCost(JitCost(15))), + FixedCostItem(SAvlTree.updateDigestMethod, FixedCost(JitCost(40))) + ) + ) + val costDetails2 = TracedCost( + testTraceBase ++ Array( + SeqCostItem(NamedDesc("CreateAvlVerifier"), PerItemCost(JitCost(110), JitCost(20), 64), 140), + SeqCostItem(NamedDesc("RemoveAvlTree"), PerItemCost(JitCost(100), JitCost(15), 1), 1), + FixedCostItem(SAvlTree.digestMethod, FixedCost(JitCost(15))), + FixedCostItem(SAvlTree.updateDigestMethod, FixedCost(JitCost(40))) + ) + ) + val costDetails3 = TracedCost(testTraceBase) + val costDetails4 = TracedCost( + testTraceBase ++ Array( + SeqCostItem(NamedDesc("CreateAvlVerifier"), PerItemCost(JitCost(110), JitCost(20), 64), 140), + SeqCostItem(NamedDesc("RemoveAvlTree"), PerItemCost(JitCost(100), JitCost(15), 1), 1), + FixedCostItem(SAvlTree.digestMethod, FixedCost(JitCost(15))) + ) + ) - { // positive test with many keys in the tree and to remove - val keys = arrayOfN(100, keyCollGen).sample.get - val values = arrayOfN(100, bytesCollGen).sample.get + { + val keys = Array( + Colls.fromArray(Array[Byte](6,71,1,-123,-1,17,-1,-1,-128,-1,-99,127,2,-37,-1,-17,-1,-90,-33,-50,-122,127,1,127,-81,1,-57,118,-38,-36,-2,1)), + Colls.fromArray(Array[Byte](-84,-128,1,0,30,0,73,127,71,1,0,39,127,-1,-109,66,0,1,-128,-43,-1,-95,-55,-97,12,1,-1,0,-128,-1,5,0)), + Colls.fromArray(Array[Byte](0,-84,1,-1,-128,93,-21,0,-128,0,-128,-128,85,-128,-123,-1,-70,0,38,-1,123,-1,1,21,0,-11,-59,122,-108,-64,1,124)), + Colls.fromArray(Array[Byte](1,-29,-1,0,-1,0,0,0,1,44,0,2,0,17,1,-36,50,58,118,-125,108,-93,3,65,-128,77,127,109,-121,-61,-128,-128)), + Colls.fromArray(Array[Byte](127,0,0,70,127,1,-109,60,-128,-106,-77,-1,-1,0,127,-108,-128,0,0,-27,-9,-128,89,127,107,68,-76,3,-102,127,4,0)) + ) + val values = Array( + Colls.fromArray(Array[Byte](-1,91,0,31,-86,-1,39,127,76,78,-1,0,0,-112,36,-16,55,-9,-21,45,0,-128,23,-1,1,-128,63,-33,-60,117,116,-53,-19,-1,0,1,-128,0,127,127,16,127,-84,-128,0,1,-5,1,-128,-103,114,-128,-105,-128,79,62,-1,46,0,-128,-40,-1,89,40,103,1,44,-128,97,-107,111,-1,0,-8,1,42,-38,88,127,127,118,127,127,127,-6,-1,20,32,-128,-1,69,1,127,1,127,22,-128,127)), + Colls.fromArray(Array[Byte](-75,-38,-1,0,-127,-104,-128,-128,-47,113,98,-128,-120,101,1,-128,1,-128,19,1,0,10,88,90,-1,-49,-13,127,26,82,-1,-1,-1,1,-1,-62,-128,-128)), + Colls.fromArray(Array[Byte](-95,-76,-128,-128,127,16,0,-1,-18,1,-93,1,127,-128,1,-92,111,-128,59,-1,-128,-1,96,-87,127,101,14,73,-9,-128,-1,1,-128,-1,127,-72,6,127,1,67,-1,-128,3,111,1,1,127,-118,127,43,-99,1,-128,0,127,-128,1,-128,-128,100,1,73,0,127,1,-121,1,104,-50,0,0,-1,119,127,80,-69,-128,23,-128,1,-1,127,18,-128,124,-128)), + Colls.fromArray(Array[Byte](-65,-128,127,0,-7,35,-128,-127,-120,-128,-8,64,-128,16)), + Colls.fromArray(Array[Byte](127,100,8,-128,1,56,113,-35,-50,53,-128,-61,-1)) + ) val (_, avlProver) = createAvlTreeAndProver(keys.zip(values):_*) val preRemoveDigest = avlProver.digest.toColl val keysToRemove = keys.zipWithIndex.collect { case (k, i) if i % 2 != 0 => k } @@ -3783,10 +3838,12 @@ class SigmaDslSpecification extends SigmaDslTesting val endTree = preRemoveTree.updateDigest(endDigest) val input = (preRemoveTree, (Colls.fromArray(keysToRemove), removeProof)) val res = Some(endTree) - remove.checkExpected(input, success(res)) + remove.checkExpected(input, Expected(Success(res), 38270, costDetails1, 1852)) } - forAll(keyCollGen, bytesCollGen) { (key, value) => + { + val key = Colls.fromArray(Array[Byte](-60,42,60,-1,-128,-122,107,-1,-1,-128,47,24,-1,-13,-40,-58,-1,127,-41,-12,100,0,15,-108,-41,127,-7,-1,126,-1,-1,115)) + val value = Colls.fromArray(Array[Byte](0,-40,1,1,-60,-119,-68,0,-128,-128,127,-3,5,54,-1,49,47,33,126,-82,-115,1,0,-123,1,15,-1,-49,-107,73,-1)) val (_, avlProver) = createAvlTreeAndProver(key -> value) val preRemoveDigest = avlProver.digest.toColl val removeProof = performRemove(avlProver, Array(key)) @@ -3799,14 +3856,14 @@ class SigmaDslSpecification extends SigmaDslTesting val endTree = preRemoveTree.updateDigest(endDigest) val input = (preRemoveTree, (keys, removeProof)) val res = Some(endTree) - remove.checkExpected(input, success(res)) + remove.checkExpected(input, Expected(Success(res), cost, costDetails2, 1826)) remove.checkVerify(input, Expected(value = Success(res), cost = cost)) } { // negative: readonly tree val readonlyTree = createTree(preRemoveDigest) val input = (readonlyTree, (keys, removeProof)) - remove.checkExpected(input, success(None)) + remove.checkExpected(input, Expected(Success(None), cost, costDetails3, 1792)) remove.checkVerify(input, Expected(value = Success(None), cost = cost)) } @@ -3815,7 +3872,7 @@ class SigmaDslSpecification extends SigmaDslTesting val invalidKey = key.map(x => (-x).toByte) // any other different from `key` val invalidKeys = Colls.fromItems(invalidKey) val input = (tree, (invalidKeys, removeProof)) - remove.checkExpected(input, success(None)) + remove.checkExpected(input, Expected(Success(None), cost, costDetails4, 1822)) remove.checkVerify(input, Expected(value = Success(None), cost = cost)) } @@ -3823,7 +3880,7 @@ class SigmaDslSpecification extends SigmaDslTesting val tree = createTree(preRemoveDigest, removeAllowed = true) val invalidProof = removeProof.map(x => (-x).toByte) // any other different from `removeProof` val input = (tree, (keys, invalidProof)) - remove.checkExpected(input, success(None)) + remove.checkExpected(input, Expected(Success(None), cost, costDetails4, 1822)) remove.checkVerify(input, Expected(value = Success(None), cost = cost)) } } From 5bef13cfbcf0041f8c4ac1e2d1f9b808877267ee Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Thu, 21 Apr 2022 14:38:29 +0200 Subject: [PATCH 37/44] v5.0-jitc-for-changed-feature-testing: Add jitc to 'AvlTree.{contains, get, getMany, updateDigest, updateOperations} equivalence' test --- .../special/sigma/SigmaDslSpecification.scala | 42 +++++++++++++++---- .../scala/special/sigma/SigmaDslTesting.scala | 2 + 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index e50e24b8b3..63d7ecc305 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -3360,14 +3360,43 @@ class SigmaDslSpecification extends SigmaDslTesting ) ) + val updateDigestCostDetails = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(MethodCall), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(SAvlTree.updateDigestMethod, FixedCost(JitCost(40))) + ) + ) + + val updateOperationsCostDetails = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(MethodCall), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(SAvlTree.updateOperationsMethod, FixedCost(JitCost(45))) + ) + ) + // positive test { val input = (tree, (key, proof)) contains.checkExpected(input, Expected(Success(okContains), 37850, costDetails(105 + additionalDetails), 1810)) get.checkExpected(input, Expected(Success(valueOpt), 38372, costDetails(105 + additionalDetails), 1810 + additionalCost)) - - contains.checkVerify(input, Expected(Success(okContains), 37850, CostDetails.ZeroCost, 1810)) - get.checkVerify(input, Expected(value = Success(valueOpt), cost = 38372, CostDetails.ZeroCost, 1810 + additionalCost)) } val keys = Colls.fromItems(key) @@ -3376,14 +3405,13 @@ class SigmaDslSpecification extends SigmaDslTesting { val input = (tree, (keys, proof)) getMany.checkExpected(input, Expected(Success(expRes), 38991, costDetails(105 + additionalDetails), 1811 + additionalCost)) - getMany.checkVerify(input, Expected(value = Success(expRes), cost = 38991, CostDetails.ZeroCost, 1811 + additionalCost)) } { val input = (tree, digest) val (res, _) = updateDigest.checkEquality(input).getOrThrow res.digest shouldBe digest - updateDigest.checkVerify(input, Expected(value = Success(res), cost = 36341, CostDetails.ZeroCost, 1791)) + updateDigest.checkExpected(input, Expected(Success(res), 36341, updateDigestCostDetails, 1791)) } val newOps = 1.toByte @@ -3392,7 +3420,7 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (tree, newOps) val (res,_) = updateOperations.checkEquality(input).getOrThrow res.enabledOperations shouldBe newOps - updateOperations.checkVerify(input, Expected(value = Success(res), cost = 36341, CostDetails.ZeroCost, 1791)) + updateOperations.checkExpected(input, Expected(Success(res), 36341, updateOperationsCostDetails, 1791)) } // negative tests: invalid proof @@ -3402,7 +3430,7 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (tree, (key, invalidProof)) val (res, _) = contains.checkEquality(input).getOrThrow res shouldBe false - contains.checkVerify(input, Expected(value = Success(res), cost = 37850, CostDetails.ZeroCost, 1810)) + contains.checkExpected(input, Expected(Success(res), 37850, costDetails(105 + additionalDetails), 1810)) } { diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala index ba88ccc291..054aef3aaf 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala @@ -751,6 +751,7 @@ class SigmaDslTesting extends PropSpec } } } + checkVerify(input, expected) } override def testCase(input: A, @@ -835,6 +836,7 @@ class SigmaDslTesting extends PropSpec val newExpectedRes = expected.newResults(ergoTreeVersionInTests) newRes shouldBe newExpectedRes._1.value.get } + checkVerify(input, expected) } override def testCase(input: A, From 46b0f44d8e0620c6fa27475af2341ed1a8b1306b Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Thu, 21 Apr 2022 14:39:20 +0200 Subject: [PATCH 38/44] v5.0-jitc-for-changed-feature-testing: Add jitc to 'AvlTree.insert equivalence' test --- .../special/sigma/SigmaDslSpecification.scala | 34 +++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 63d7ecc305..fcc0bce12d 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -3544,9 +3544,31 @@ class SigmaDslSpecification extends SigmaDslTesting ) )) - val newCostDetails = TracedCost( - traceBase ++ Array( - FixedCostItem(PropertyCall) + val testTraceBase = Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(MethodCall), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(SAvlTree.isInsertAllowedMethod, FixedCost(JitCost(15))) + ) + val costDetails1 = TracedCost(testTraceBase) + val costDetails2 = TracedCost( + testTraceBase ++ Array( + SeqCostItem(NamedDesc("CreateAvlVerifier"), PerItemCost(JitCost(110), JitCost(20), 64), 70), + SeqCostItem(NamedDesc("InsertIntoAvlTree"), PerItemCost(JitCost(40), JitCost(10), 1), 1), + FixedCostItem(SAvlTree.updateDigestMethod, FixedCost(JitCost(40))) ) ) @@ -3561,7 +3583,7 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (preInsertTree, (kvs, insertProof)) val (res, _) = insert.checkEquality(input).getOrThrow res.isDefined shouldBe true - insert.checkVerify(input, Expected(Success(res), 38501, newCostDetails, 1816)) + insert.checkExpected(input, Expected(Success(res), 38501, costDetails2, 1816)) } { // negative: readonly tree @@ -3569,7 +3591,7 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (readonlyTree, (kvs, insertProof)) val (res, _) = insert.checkEquality(input).getOrThrow res.isDefined shouldBe false - insert.checkVerify(input, Expected(value = Success(res), cost = 38501)) + insert.checkExpected(input, Expected(Success(res), 38501, costDetails1, 1792)) } { // negative: invalid key @@ -3579,7 +3601,7 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (tree, (invalidKvs, insertProof)) val (res, _) = insert.checkEquality(input).getOrThrow res.isDefined shouldBe true // TODO v6.0: should it really be true? (looks like a bug) - insert.checkVerify(input, Expected(value = Success(res), cost = 38501)) + insert.checkExpected(input, Expected(Success(res), 38501, costDetails2, 1816)) } { // negative: invalid proof From c1067767fad2bee8f2344047ea857e905e812826 Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Thu, 21 Apr 2022 14:40:00 +0200 Subject: [PATCH 39/44] v5.0-jitc-for-changed-feature-testing: Add jitc to 'AvlTree.update equivalence' test --- .../test/scala/special/sigma/SigmaDslSpecification.scala | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index fcc0bce12d..a4a7cb004f 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -3732,9 +3732,7 @@ class SigmaDslSpecification extends SigmaDslTesting val endTree = preUpdateTree.updateDigest(endDigest) val input = (preUpdateTree, (kvs, updateProof)) val res = Some(endTree) - // TODO mainnet v5: Possible duplication of tests. update.checkExpected(input, Expected(Success(res), cost, costDetails2, 1825)) - update.checkVerify(input, Expected(Success(res), cost)) } { // positive: update to the same value (identity operation) @@ -3743,14 +3741,12 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (tree, (keys, updateProof)) val res = Some(tree) update.checkExpected(input, Expected(Success(res), cost, costDetails2, 1825)) - update.checkVerify(input, Expected(Success(res), cost)) } { // negative: readonly tree val readonlyTree = createTree(preUpdateDigest) val input = (readonlyTree, (kvs, updateProof)) update.checkExpected(input, Expected(Success(None), cost, costDetails1, 1792)) - update.checkVerify(input, Expected(Success(None), cost)) } { // negative: invalid key @@ -3759,7 +3755,6 @@ class SigmaDslSpecification extends SigmaDslTesting val invalidKvs = Colls.fromItems((invalidKey -> newValue)) val input = (tree, (invalidKvs, updateProof)) update.checkExpected(input, Expected(Success(None), cost, costDetails3, 1821)) - update.checkVerify(input, Expected(Success(None), cost)) } { // negative: invalid value (different from the value in the proof) @@ -3769,7 +3764,7 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (tree, (invalidKvs, updateProof)) val (res, _) = update.checkEquality(input).getOrThrow res.isDefined shouldBe true // TODO v6.0: should it really be true? (looks like a bug) - update.checkVerify(input, Expected(Success(res), cost)) + update.checkExpected(input, Expected(Success(res), cost, costDetails2, 1825)) } { // negative: invalid proof @@ -3777,7 +3772,6 @@ class SigmaDslSpecification extends SigmaDslTesting val invalidProof = updateProof.map(x => (-x).toByte) // any other different from proof val input = (tree, (kvs, invalidProof)) update.checkExpected(input, Expected(Success(None), cost, costDetails3, 1821)) - update.checkVerify(input, Expected(Success(None), cost)) } } From 99d260d753833b3c87bf8efdebc041458ab8c62b Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Thu, 21 Apr 2022 14:40:42 +0200 Subject: [PATCH 40/44] v5.0-jitc-for-changed-feature-testing: Add jitc to 'AvlTree.remove equivalence' test --- .../src/test/scala/special/sigma/SigmaDslSpecification.scala | 4 ---- 1 file changed, 4 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index a4a7cb004f..68483ba680 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -3901,14 +3901,12 @@ class SigmaDslSpecification extends SigmaDslTesting val input = (preRemoveTree, (keys, removeProof)) val res = Some(endTree) remove.checkExpected(input, Expected(Success(res), cost, costDetails2, 1826)) - remove.checkVerify(input, Expected(value = Success(res), cost = cost)) } { // negative: readonly tree val readonlyTree = createTree(preRemoveDigest) val input = (readonlyTree, (keys, removeProof)) remove.checkExpected(input, Expected(Success(None), cost, costDetails3, 1792)) - remove.checkVerify(input, Expected(value = Success(None), cost = cost)) } { // negative: invalid key @@ -3917,7 +3915,6 @@ class SigmaDslSpecification extends SigmaDslTesting val invalidKeys = Colls.fromItems(invalidKey) val input = (tree, (invalidKeys, removeProof)) remove.checkExpected(input, Expected(Success(None), cost, costDetails4, 1822)) - remove.checkVerify(input, Expected(value = Success(None), cost = cost)) } { // negative: invalid proof @@ -3925,7 +3922,6 @@ class SigmaDslSpecification extends SigmaDslTesting val invalidProof = removeProof.map(x => (-x).toByte) // any other different from `removeProof` val input = (tree, (keys, invalidProof)) remove.checkExpected(input, Expected(Success(None), cost, costDetails4, 1822)) - remove.checkVerify(input, Expected(value = Success(None), cost = cost)) } } } From d4dadd1da142131ef5835eacbdaddad4240e745b Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Fri, 22 Apr 2022 13:05:19 +0200 Subject: [PATCH 41/44] v5.0-add-jit-costing-for-tests: 'Coll slice method equivalence' test --- .../special/sigma/SigmaDslSpecification.scala | 48 +++++++++++++------ 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 6008ed2da7..2454e3ecca 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -8331,25 +8331,45 @@ class SigmaDslSpecification extends SigmaDslTesting ))) } - // TODO mainnet v5.0: NoSuchMethodException thrown inside the test. property("Coll slice method equivalence") { + def costDetails(i: Int) = TracedCost( + Array( + FixedCostItem(Apply), + FixedCostItem(FuncValue), + FixedCostItem(GetVar), + FixedCostItem(OptionGet), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + SeqCostItem(CompanionDesc(BlockValue), PerItemCost(JitCost(1), JitCost(1), 10), 1), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(FuncValue.AddToEnvironmentDesc, FixedCost(JitCost(5))), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + FixedCostItem(ValUse), + FixedCostItem(SelectField), + SeqCostItem(CompanionDesc(Slice), PerItemCost(JitCost(10), JitCost(2), 100), i) + ) + ) val samples = genSamples(collWithRangeGen, DefaultMinSuccessful) if (lowerMethodCallsInTests) { verifyCases( - // (coll, (from, until)) { - def success[T](v: T) = Expected(Success(v), 36964) + val cost = 36964 + val newCost = 1792 Seq( - ((Coll[Int](), (-1, 0)), success(Coll[Int]())), - ((Coll[Int](), (0, 0)), success(Coll[Int]())), - ((Coll[Int](1), (0, 0)), success(Coll[Int]())), - ((Coll[Int](1), (0, -1)), success(Coll[Int]())), - ((Coll[Int](1), (1, 1)), success(Coll[Int]())), - ((Coll[Int](1), (-1, 1)), success(Coll[Int](1))), - ((Coll[Int](1, 2), (1, 1)), success(Coll[Int]())), - ((Coll[Int](1, 2), (1, 0)), success(Coll[Int]())), - ((Coll[Int](1, 2), (1, 2)), success(Coll[Int](2))), - ((Coll[Int](1, 2, 3, 4), (1, 3)), success(Coll[Int](2, 3))) + // (coll, (from, until)) + ((Coll[Int](), (-1, 0)), Expected(Success(Coll[Int]()), cost, costDetails(1), newCost)), + ((Coll[Int](), (0, 0)), Expected(Success(Coll[Int]()), cost, costDetails(0), newCost)), + ((Coll[Int](1), (0, 0)), Expected(Success(Coll[Int]()), cost, costDetails(0), newCost)), + ((Coll[Int](1), (0, -1)), Expected(Success(Coll[Int]()), cost, costDetails(0), newCost)), + ((Coll[Int](1), (1, 1)), Expected(Success(Coll[Int]()), cost, costDetails(0), newCost)), + ((Coll[Int](1), (-1, 1)), Expected(Success(Coll[Int](1)), cost, costDetails(2), newCost)), + ((Coll[Int](1, 2), (1, 1)), Expected(Success(Coll[Int]()), cost, costDetails(0), newCost)), + ((Coll[Int](1, 2), (1, 0)), Expected(Success(Coll[Int]()), cost, costDetails(0), newCost)), + ((Coll[Int](1, 2), (1, 2)), Expected(Success(Coll[Int](2)), cost, costDetails(1), newCost)), + ((Coll[Int](1, 2, 3, 4), (1, 3)), Expected(Success(Coll[Int](2, 3)), cost, costDetails(2), newCost)) ) }, existingFeature((x: (Coll[Int], (Int, Int))) => x._1.slice(x._2._1, x._2._2), @@ -8381,7 +8401,7 @@ class SigmaDslSpecification extends SigmaDslTesting } else { assertExceptionThrown( verifyCases( - Seq( (Coll[Int](), (-1, 0)) -> Expected(Success(Coll[Int]()), 37765) ), + Seq( (Coll[Int](), (-1, 0)) -> Expected(Success(Coll[Int]()), 0, CostDetails.ZeroCost, 0) ), existingFeature((x: (Coll[Int], (Int, Int))) => x._1.slice(x._2._1, x._2._2), "{ (x: (Coll[Int], (Int, Int))) => x._1.slice(x._2._1, x._2._2) }" )), From 4e269e91abfff8e6f129943ccba8ec0422a5b8a7 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Fri, 22 Apr 2022 22:47:17 +0200 Subject: [PATCH 42/44] v5.0-jitc-for-changed-feature-testing: resolve remaining TODO mainnet --- .../special/sigma/SigmaDslSpecification.scala | 7 +- .../scala/special/sigma/SigmaDslTesting.scala | 85 ++++++++++--------- 2 files changed, 49 insertions(+), 43 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 68483ba680..87c8d9c666 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -8635,7 +8635,6 @@ class SigmaDslSpecification extends SigmaDslTesting ))) } - // TODO mainnet v5.0: NoSuchMethodException thrown inside the test. property("Coll slice method equivalence") { val samples = genSamples(collWithRangeGen, DefaultMinSuccessful) if (lowerMethodCallsInTests) { @@ -9679,10 +9678,12 @@ class SigmaDslSpecification extends SigmaDslTesting ) if (lowerMethodCallsInTests) { + val ir = IR + val error = new ir.StagingException("Don't know how to evaluate(s2034 -> Placeholder(BaseElemLiftable))", Nil) verifyCases( Seq( ctx -> Expected( - Failure(new NoSuchElementException("None.get")), + Failure(error), cost = 37694, expectedDetails = CostDetails.ZeroCost, newCost = 1796, @@ -9691,7 +9692,7 @@ class SigmaDslSpecification extends SigmaDslTesting ), changedFeature( { (x: Context) => - Option.empty[Int].get + throw error true }, { (x: Context) => diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala index 054aef3aaf..5315b968c4 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala @@ -729,26 +729,51 @@ class SigmaDslTesting extends PropSpec newRes } - /** compares the old and new implementations against - * semantic function (scalaFunc) on the given input, also checking the given expected result. + /** compares the old and new implementations against semantic functions (scalaFunc and + * scalaFuncNew) on the given input, also checking the given expected result. */ override def checkExpected(input: A, expected: Expected[B]): Unit = { - // check the old implementation with Scala semantic - val (oldRes, _) = checkEq(scalaFunc)(oldF)(input).get - oldRes shouldBe expected.value.get + // check the new implementation with Scala semantic + val newRes = VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) { + checkEq(scalaFuncNew)(newF)(input) + } - if (!(newImpl eq oldImpl)) { - // check the new implementation with Scala semantic - val (newRes, newDetails) = VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) { - checkEq(scalaFuncNew)(newF)(input).get + if (!VersionContext.current.isJitActivated) { + // check the old implementation with Scala semantic + val expectedOldRes = expected.value + val oldRes = checkEq(scalaFunc)(oldF)(input) + checkResult(oldRes.map(_._1), expectedOldRes, failOnTestVectors = true, + "Comparing oldRes with expected: !isJitActivated: ") + + val newValue = newRes.map(_._1) + (expectedOldRes, newValue) match { + case (_: Failure[_], _: Success[_]) if allowNewToSucceed => + // NOTE, we are in ChangedFeature (compare with ExistingFeature) + // this is the case when old v4.x version fails with exception (e.g. due to AOT costing), + // but the new v5.0 produces result. (See property("Option fold workaround method")) + // Thus, we allow some scripts which fail in v4.x to pass in v5.0. + // see ScalaDoc for allowNewToSucceed + case (_: Failure[_], _: Failure[_]) if allowDifferentErrors => + // this is the case when old v4.x version fails with one exception (e.g. due to AOT costing), + // but the new v5.0 fails with another exception. + case _ => + checkResult(newValue, expectedOldRes, failOnTestVectors = true, + "Comparing newRes with expected old result: !isJitActivated: ") } - val newExpectedRes = expected.newResults(ergoTreeVersionInTests) - newRes shouldBe newExpectedRes._1.value.get - newExpectedRes._2.foreach { expDetails => - if (newDetails.trace != expDetails.trace) { - printCostDetails(script, newDetails) - newDetails.trace shouldBe expDetails.trace - } + } else { + val (newExpectedRes, newExpectedDetailsOpt) = expected.newResults(ergoTreeVersionInTests) + newRes match { + case Success((newValue, newDetails)) => + newValue shouldBe newExpectedRes.value.get + newExpectedDetailsOpt.foreach { expDetails => + if (newDetails.trace != expDetails.trace) { + printCostDetails(script, newDetails) + newDetails.trace shouldBe expDetails.trace + } + } + case _ => + checkResult(newRes.map(_._1), newExpectedRes.value, failOnTestVectors = true, + "Comparing newRes with new expected: isJitActivated: ") } } checkVerify(input, expected) @@ -766,29 +791,7 @@ class SigmaDslTesting extends PropSpec expected: Expected[B], printTestCases: Boolean, failOnTestVectors: Boolean): Unit = { - val funcRes = checkEquality(input, printTestCases) - // TODO mainnet v5: How to handle different results (for features that changed behaviour) - // uncomment to see failing tests - // checkResult(funcRes.map(_._1), expected.value, failOnTestVectors) - val newRes = expected.newResults(ergoTreeVersionInTests) - val expectedTrace = newRes._2.fold(Seq.empty[CostItem])(_.trace) - if (expectedTrace.isEmpty) { - // new cost expectation is missing, print out actual cost results - if (evalSettings.printTestVectors) { - funcRes.foreach { case (_, newCost) => - printCostDetails(script, newCost) - } - } - } - else { - // new cost expectation is specified, compare it with the actual result - funcRes.foreach { case (_, newCost) => - if (newCost.trace != expectedTrace) { - printCostDetails(script, newCost) - newCost.trace shouldBe expectedTrace - } - } - } + checkExpected(input, expected) checkVerify(input, expected) } } @@ -1033,7 +1036,9 @@ class SigmaDslTesting extends PropSpec protected def checkResult[B](res: Try[B], expectedRes: Try[B], failOnTestVectors: Boolean, hint: String = ""): Unit = { (res, expectedRes) match { case (Failure(exception), Failure(expectedException)) => - rootCause(exception).getClass shouldBe rootCause(expectedException).getClass + withClue(hint) { + rootCause(exception).getClass shouldBe rootCause(expectedException).getClass + } case _ => if (failOnTestVectors) { val actual = rootCause(res) From 41e1f2a5f925763ff24d7e2bb28c7b7fca50da64 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Sat, 23 Apr 2022 11:53:08 +0200 Subject: [PATCH 43/44] v5.0-jitc-for-changed-feature-testing: code cleanup + ScalaDocs --- .../special/sigma/SigmaDslSpecification.scala | 12 ++---- .../scala/special/sigma/SigmaDslTesting.scala | 41 +++++++++++-------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala index 87c8d9c666..ac22a4fff1 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslSpecification.scala @@ -116,7 +116,8 @@ class SigmaDslSpecification extends SigmaDslTesting val res = f.checkEquality(x) val resValue = res.map(_._1) val (expected, expDetailsOpt) = expectedRes.newResults(ergoTreeVersionInTests) - checkResult(resValue, expected.value, failOnTestVectors = true) + checkResult(resValue, expected.value, failOnTestVectors = true, + "SigmaDslSpecifiction#testCases: compare expected new result with res = f.checkEquality(x)") res match { case Success((value, details)) => details.cost shouldBe JitCost(expected.verificationCost.get) @@ -9641,14 +9642,7 @@ class SigmaDslSpecification extends SigmaDslTesting ))) } - /* Original issue: https://github.com/ScorexFoundation/sigmastate-interpreter/issues/604 - * - * Test fails for v4.x with - * `new StagingException("Don't know how to evaluate(s1252 -> Placeholder(BaseElemLiftable))")` - * but that cannot be explicitly stated in cases. Therefore we use different exception (None.get) - * and also `allowDifferentErrors` flag. - * Test passes for v5.x so we use flag `allowNewToSucceed`. - */ + // Original issue: https://github.com/ScorexFoundation/sigmastate-interpreter/issues/604 property("Random headers access and comparison (originaly from spam tests)") { val (_, _, _, ctx, _, _) = contextData() val costDetails = TracedCost( diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala index 5315b968c4..0ce903ae2c 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala @@ -544,7 +544,8 @@ class SigmaDslTesting extends PropSpec } } case _ => - checkResult(rootCause(newRes), rootCause(oldRes), failOnTestVectors = true) + checkResult(rootCause(newRes), rootCause(oldRes), failOnTestVectors = true, + "ExistingFeature.checkEquality: Comparing newRes with oldRes when failure") } if (logInputOutput) { @@ -565,19 +566,18 @@ class SigmaDslTesting extends PropSpec val (oldRes, _) = checkEq(scalaFunc)(oldF)(input).get oldRes shouldBe expected.value.get - if (!(newImpl eq oldImpl)) { - // check the new implementation with Scala semantic - val (newRes, newDetails) = VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) { - checkEq(scalaFunc)(newF)(input).get - } - newRes shouldBe expected.value.get - expected.newResults(ergoTreeVersionInTests)._2.foreach { expDetails => - if (newDetails.trace != expDetails.trace) { - printCostDetails(script, newDetails) - newDetails.trace shouldBe expDetails.trace - } + // check the new implementation with Scala semantic + val (newRes, newDetails) = VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) { + checkEq(scalaFunc)(newF)(input).get + } + newRes shouldBe expected.value.get + expected.newResults(ergoTreeVersionInTests)._2.foreach { expDetails => + if (newDetails.trace != expDetails.trace) { + printCostDetails(script, newDetails) + newDetails.trace shouldBe expDetails.trace } } + checkVerify(input, expected) } @@ -586,7 +586,7 @@ class SigmaDslTesting extends PropSpec printTestCases: Boolean, failOnTestVectors: Boolean): Unit = { val res = checkEquality(input, printTestCases).map(_._1) - checkResult(res, expectedResult, failOnTestVectors) + checkResult(res, expectedResult, failOnTestVectors, "ExistingFeature#testCase: ") } override def verifyCase(input: A, @@ -595,7 +595,8 @@ class SigmaDslTesting extends PropSpec failOnTestVectors: Boolean): Unit = { val funcRes = checkEquality(input, printTestCases) // NOTE: funcRes comes from newImpl - checkResult(funcRes.map(_._1), expected.value, failOnTestVectors) + checkResult(funcRes.map(_._1), expected.value, failOnTestVectors, + "ExistingFeature#verifyCase: ") val newRes = expected.newResults(ergoTreeVersionInTests) val expectedTrace = newRes._2.fold(Seq.empty[CostItem])(_.trace) @@ -641,6 +642,7 @@ class SigmaDslTesting extends PropSpec * accepting without check. * This approach allows to fix bugs in the implementation of * some of v4.x operations. + * @param allowDifferentErrors if true, allow v4.x and v5.0 to fail with different error */ case class ChangedFeature[A, B]( script: String, @@ -656,6 +658,7 @@ class SigmaDslTesting extends PropSpec implicit val cs = compilerSettingsInTests + /** Apply given function to the context variable 1 */ private def getApplyExpr(funcValue: SValue) = { val sType = Evaluation.rtypeToSType(RType[A]) Apply(funcValue, IndexedSeq(OptionGet(GetVar[SType](1.toByte, sType)))) @@ -733,7 +736,7 @@ class SigmaDslTesting extends PropSpec * scalaFuncNew) on the given input, also checking the given expected result. */ override def checkExpected(input: A, expected: Expected[B]): Unit = { - // check the new implementation with Scala semantic + // check the new implementation with Scala semantic function val newRes = VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) { checkEq(scalaFuncNew)(newF)(input) } @@ -784,7 +787,8 @@ class SigmaDslTesting extends PropSpec printTestCases: Boolean, failOnTestVectors: Boolean): Unit = { val res = checkEquality(input, printTestCases).map(_._1) - checkResult(res, expectedResult, failOnTestVectors) + checkResult(res, expectedResult, failOnTestVectors, + "ChangedFeature#testCase: ") } override def verifyCase(input: A, @@ -1030,10 +1034,11 @@ class SigmaDslTesting extends PropSpec /** NOTE, this should be `def` to allow overriding of generatorDrivenConfig in derived Spec classes. */ def DefaultMinSuccessful: MinSuccessful = MinSuccessful(generatorDrivenConfig.minSuccessful) - val PrintTestCasesDefault: Boolean = false // true + val PrintTestCasesDefault: Boolean = false val FailOnTestVectorsDefault: Boolean = true - protected def checkResult[B](res: Try[B], expectedRes: Try[B], failOnTestVectors: Boolean, hint: String = ""): Unit = { + /** Because this method is called from many places it should always be called with `hint`. */ + protected def checkResult[B](res: Try[B], expectedRes: Try[B], failOnTestVectors: Boolean, hint: String): Unit = { (res, expectedRes) match { case (Failure(exception), Failure(expectedException)) => withClue(hint) { From 0d8e8735baa20f4a9134907b0c1e92ae66f83409 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Sat, 23 Apr 2022 12:15:15 +0200 Subject: [PATCH 44/44] v5.0-jitc-for-changed-feature-testing: renamings --- .../test/scala/special/sigma/SigmaDslTesting.scala | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala index 0ce903ae2c..8c7cd58dfb 100644 --- a/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala +++ b/sigmastate/src/test/scala/special/sigma/SigmaDslTesting.scala @@ -603,17 +603,17 @@ class SigmaDslTesting extends PropSpec if (expectedTrace.isEmpty) { // new cost expectation is missing, print out actual cost results if (evalSettings.printTestVectors) { - funcRes.foreach { case (_, newCost) => - printCostDetails(script, newCost) + funcRes.foreach { case (_, newDetails) => + printCostDetails(script, newDetails) } } } else { // new cost expectation is specified, compare it with the actual result - funcRes.foreach { case (_, newCost) => - if (newCost.trace != expectedTrace) { - printCostDetails(script, newCost) - newCost.trace shouldBe expectedTrace + funcRes.foreach { case (_, newDetails) => + if (newDetails.trace != expectedTrace) { + printCostDetails(script, newDetails) + newDetails.trace shouldBe expectedTrace } } }