Skip to content

Commit

Permalink
fixing most of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Oct 3, 2024
1 parent 40fe947 commit 19c752d
Show file tree
Hide file tree
Showing 19 changed files with 82 additions and 80 deletions.
1 change: 1 addition & 0 deletions data/js/src/main/scala/sigma/Platform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ object Platform {
case ge: GroupElement => Nullable(mkConstant[SGroupElement.type](ge, SGroupElement))
case b: Boolean => Nullable(if (b) TrueLeaf else FalseLeaf)
case v: String => Nullable(mkConstant[SString.type](v, SString))
case h: Header if VersionContext.current.isV6SoftForkActivated => Nullable(mkConstant[SHeader.type](h, SHeader))

// The Box lifting was broken in v4.x. `SigmaDsl.Box(b)` was missing which means the
// isCorrectType requirement would fail in ConstantNode constructor.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.ergoplatform

import org.ergoplatform.ErgoAddressEncoder.NetworkPrefix
import sigma.Colls
import sigma.ast.SType
import sigma.ast.syntax.SigmaPropValue
import sigma.ast.Value
Expand Down Expand Up @@ -32,7 +33,7 @@ object ErgoScriptPredef {
networkPrefix: NetworkPrefix)
(implicit IR: IRContext): SigmaPropValue = {
val env = emptyEnv +
("tokenId" -> tokenId, "thresholdAmount" -> thresholdAmount)
("tokenId" -> Colls.fromArray(tokenId), "thresholdAmount" -> thresholdAmount)
val res = compileWithCosting(env,
"""{
| val sumValues = { (xs: Coll[Long]) => xs.fold(0L, { (acc: Long, amt: Long) => acc + amt }) }
Expand Down
24 changes: 12 additions & 12 deletions sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class ErgoTreePredefSpec extends CompilerTestingCommons with CompilerCrossVersio
boxesToSpend = inputBoxes,
spendingTransaction,
self = inputBox, activatedVersionInTests)
val pr = prover.prove(emptyEnv + (ScriptNameProp -> "boxCreationHeight_prove"), propTree, ctx, fakeMessage).get
verifier.verify(emptyEnv + (ScriptNameProp -> "boxCreationHeight_verify"), propTree, ctx, pr, fakeMessage).get._1 shouldBe true
val pr = prover.prove(emptyEnv, propTree, ctx, fakeMessage).get
verifier.verify(emptyEnv, propTree, ctx, pr, fakeMessage).get._1 shouldBe true
}

property("collect coins from the founders' box") {
Expand Down Expand Up @@ -118,8 +118,8 @@ class ErgoTreePredefSpec extends CompilerTestingCommons with CompilerCrossVersio
boxesToSpend = inputBoxes,
spendingTransaction,
self = inputBoxes.head, activatedVersionInTests)
val pr = prover.prove(emptyEnv + (ScriptNameProp -> "checkSpending_prove"), prop, ctx, fakeMessage).get
verifier.verify(emptyEnv + (ScriptNameProp -> "checkSpending_verify"), prop, ctx, pr, fakeMessage).get._1 shouldBe true
val pr = prover.prove(emptyEnv, prop, ctx, fakeMessage).get
verifier.verify(emptyEnv, prop, ctx, pr, fakeMessage).get._1 shouldBe true
}
}

Expand Down Expand Up @@ -148,13 +148,13 @@ class ErgoTreePredefSpec extends CompilerTestingCommons with CompilerCrossVersio
self = inputBoxes.head, activatedVersionInTests)

// should not be able to collect before minerRewardDelay
val prove = prover.prove(emptyEnv + (ScriptNameProp -> "rewardOutputScript_prove"), prop, ctx, fakeMessage).get
verifier.verify(emptyEnv + (ScriptNameProp -> "rewardOutputScript_verify"), prop, prevBlockCtx, prove, fakeMessage)
val prove = prover.prove(emptyEnv, prop, ctx, fakeMessage).get
verifier.verify(emptyEnv, prop, prevBlockCtx, prove, fakeMessage)
.getOrThrow should matchPattern { case (false,_) => }

// should be able to collect after minerRewardDelay
val pr = prover.prove(emptyEnv + (ScriptNameProp -> "prove"), prop, ctx, fakeMessage).getOrThrow
verifier.verify(emptyEnv + (ScriptNameProp -> "verify"), prop, ctx, pr, fakeMessage).getOrThrow._1 shouldBe true
val pr = prover.prove(emptyEnv, prop, ctx, fakeMessage).getOrThrow
verifier.verify(emptyEnv, prop, ctx, pr, fakeMessage).getOrThrow._1 shouldBe true
}

property("create transaction collecting the emission box") {
Expand Down Expand Up @@ -232,8 +232,8 @@ class ErgoTreePredefSpec extends CompilerTestingCommons with CompilerCrossVersio
self = inputBoxes.head,
activatedVersionInTests).withCostLimit(scriptCostLimitInTests * 10)

val pr = prover.prove(emptyEnv + (ScriptNameProp -> "tokenThresholdScript_prove"), prop, ctx, fakeMessage).getOrThrow
verifier.verify(emptyEnv + (ScriptNameProp -> "tokenThresholdScript_verify"), prop, ctx, pr, fakeMessage).getOrThrow._1 shouldBe true
val pr = prover.prove(emptyEnv, prop, ctx, fakeMessage).getOrThrow
verifier.verify(emptyEnv, prop, ctx, pr, fakeMessage).getOrThrow._1 shouldBe true
}


Expand Down Expand Up @@ -308,8 +308,8 @@ class ErgoTreePredefSpec extends CompilerTestingCommons with CompilerCrossVersio
boxesToSpend = inputBoxes,
spendingTransaction,
self = inputBoxes.head, activatedVersionInTests)
val pr = prover.prove(emptyEnv + (ScriptNameProp -> "checkRewardTx_prove"), prop, ctx, fakeMessage).getOrThrow
verifier.verify(emptyEnv + (ScriptNameProp -> "checkRewardTx_verify"), prop, ctx, pr, fakeMessage).getOrThrow._1 shouldBe true
val pr = prover.prove(emptyEnv, prop, ctx, fakeMessage).getOrThrow
verifier.verify(emptyEnv, prop, ctx, pr, fakeMessage).getOrThrow._1 shouldBe true
spendingTransaction
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import scorex.util.encode.{Base16, Base58}
import sigma.Colls
import sigma.VersionContext.V6SoftForkVersion
import sigma.VersionContext
import sigma.data.{CAND, CAvlTree, CHeader, ProveDlog, SigmaBoolean, TrivialProp}
import sigma.data.{CAND, CAvlTree, CBox, CHeader, ProveDlog, SigmaBoolean, TrivialProp}
import sigma.interpreter.ContextExtension
import sigma.util.Extensions.IntOps
import sigmastate.helpers.{CompilerTestingCommons, ErgoLikeContextTesting, ErgoLikeTestInterpreter, ErgoLikeTestProvingInterpreter}
Expand Down Expand Up @@ -143,12 +143,18 @@ class TestingInterpreterSpecification extends CompilerTestingCommons
val env = Map(
"dk1" -> dk1,
"dk2" -> dk2,
"bytes1" -> Array[Byte](1, 2, 3),
"bytes2" -> Array[Byte](4, 5, 6),
"box1" -> testBox(10, TrueTree, 0, Seq(), Map(
"bytes1" -> Colls.fromArray(Array[Byte](1, 2, 3)),
"bytes2" -> Colls.fromArray(Array[Byte](4, 5, 6)),
"box1" -> (if(VersionContext.current.isJitActivated) {
CBox(testBox(10, TrueTree, 0, Seq(), Map(
reg1 -> IntArrayConstant(Array[Int](1, 2, 3)),
reg2 -> BoolArrayConstant(Array[Boolean](true, false, true))
)))} else {
testBox(10, TrueTree, 0, Seq(), Map(
reg1 -> IntArrayConstant(Array[Int](1, 2, 3)),
reg2 -> BoolArrayConstant(Array[Boolean](true, false, true))
))
))
})
)
val prop = mkTestErgoTree(compile(env, code)(IR).asBoolValue.toSigmaProp)
val challenge = Array.fill(32)(Random.nextInt(100).toByte)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingIn
import sigmastate.helpers.TestingHelpers._
import sigmastate.interpreter.Interpreter.ScriptNameProp
import sigma.ast.syntax._
import sigma.Coll
import sigma.{AvlTree, Coll, Colls, Context}
import sigma.ast.SAvlTree
import sigma.ast.syntax.{GetVarByteArray, OptionValueOps}
import sigma.compiler.ir.IRContext
import sigma.data.{AvlTreeData, AvlTreeFlags, CSigmaProp, TrivialProp}
import sigma.eval.SigmaDsl
import sigma.interpreter.ProverResult
import sigma.{AvlTree, Context}
import sigmastate.eval.Extensions.AvlTreeOps


Expand All @@ -39,9 +38,6 @@ class AVLTreeScriptsSpecification extends CompilerTestingCommons
def genKey(str: String): ADKey = ADKey @@@ Blake2b256("key: " + str)
def genValue(str: String): ADValue = ADValue @@@ Blake2b256("val: " + str)

val inKey = genKey("init key")
val inValue = genValue("init value")

property("avl tree - removals") {
case class AvlTreeContract[Spec <: ContractSpec]
(ops: Coll[Coll[Byte]], proof: Coll[Byte], prover: Spec#ProvingParty)
Expand Down Expand Up @@ -204,7 +200,7 @@ class AVLTreeScriptsSpecification extends CompilerTestingCommons

val treeData = new AvlTreeData(digest.toColl, AvlTreeFlags.ReadOnly, 32, None)

val env = Map("key" -> key, "proof" -> proof)
val env = Map("key" -> Colls.fromArray(key), "proof" -> Colls.fromArray(proof))
val prop = compile(env, """SELF.R4[AvlTree].get.contains(key, proof)""").asBoolValue.toSigmaProp

val propExp = IR.builder.mkMethodCall(
Expand Down Expand Up @@ -374,7 +370,7 @@ class AVLTreeScriptsSpecification extends CompilerTestingCommons
val treeData = SigmaDsl.avlTree(new AvlTreeData(digest.toColl, AvlTreeFlags.ReadOnly, 32, None))

val env = Map("proofId" -> proofId.toLong,
"keys" -> ConcreteCollection.fromItems(genKey("3"), genKey("4"), genKey("5")))
"keys" -> Colls.fromItems(Colls.fromArray(genKey("3")), Colls.fromArray(genKey("4")), Colls.fromArray(genKey("5"))))
val prop = compile(env,
"""{
| val tree = SELF.R4[AvlTree].get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import scorex.crypto.hash.Blake2b256
import sigma.ast._
import sigma.ast.syntax._
import sigmastate._
import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter, CompilerTestingCommons}
import sigma.Coll
import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter}
import sigma.{Coll, Colls}


class ContextEnrichingSpecification extends CompilerTestingCommons
Expand All @@ -19,7 +19,7 @@ class ContextEnrichingSpecification extends CompilerTestingCommons
val preimage = prover.contextExtenders(1).value.asInstanceOf[Coll[Byte]]
val pubkey = prover.dlogSecrets.head.publicImage

val env = Map("blake" -> Blake2b256(preimage.toArray), "pubkey" -> pubkey)
val env = Map("blake" -> Colls.fromArray(Blake2b256(preimage.toArray)), "pubkey" -> pubkey)
val prop = compile(env,
"""{
| pubkey && blake2b256(getVar[Coll[Byte]](1).get) == blake
Expand Down Expand Up @@ -49,7 +49,7 @@ class ContextEnrichingSpecification extends CompilerTestingCommons
val preimage2 = prover.contextExtenders(2).value.asInstanceOf[Coll[Byte]]
val pubkey = prover.dlogSecrets.head.publicImage

val env = Map("blake" -> Blake2b256(preimage1.append(preimage2).toArray), "pubkey" -> pubkey)
val env = Map("blake" -> Colls.fromArray(Blake2b256(preimage1.append(preimage2).toArray)), "pubkey" -> pubkey)
val prop = compile(env,
"""{
| pubkey && blake2b256(getVar[Coll[Byte]](1).get ++ getVar[Coll[Byte]](2).get) == blake
Expand Down Expand Up @@ -89,7 +89,7 @@ class ContextEnrichingSpecification extends CompilerTestingCommons
.withContextExtender(k1, ByteArrayConstant(v1))
.withContextExtender(k2, ByteArrayConstant(v2))

val env = Map("k1" -> k1.toInt, "k2" -> k2.toInt, "r" -> r)
val env = Map("k1" -> k1.toInt, "k2" -> k2.toInt, "r" -> Colls.fromArray(r))
val prop = compile(env,
"{ xor(getVar[Coll[Byte]](k1).get, getVar[Coll[Byte]](k2).get) == r }").asBoolValue.toSigmaProp
val propTree = mkTestErgoTree(prop)
Expand Down Expand Up @@ -119,7 +119,7 @@ class ContextEnrichingSpecification extends CompilerTestingCommons
val prover = new ContextEnrichingTestProvingInterpreter
val preimage = prover.contextExtenders(1).value.asInstanceOf[Coll[Byte]]

val env = Map("blake" -> Blake2b256(preimage.toArray))
val env = Map("blake" -> Colls.fromArray(Blake2b256(preimage.toArray)))
val prop = compile(env,
"""{
| blake2b256(getVar[Coll[Byte]](1).get) == blake
Expand Down Expand Up @@ -149,7 +149,7 @@ class ContextEnrichingSpecification extends CompilerTestingCommons
val preimage1 = prover.contextExtenders(1).value.asInstanceOf[Coll[Byte]]
val preimage2 = prover.contextExtenders(2).value.asInstanceOf[Coll[Byte]]

val env = Map("blake" -> Blake2b256(preimage2.append(preimage1).toArray))
val env = Map("blake" -> Colls.fromArray(Blake2b256(preimage2.append(preimage1).toArray)))
val prop = compile(env,
"""{
| blake2b256(getVar[Coll[Byte]](2).get ++ getVar[Coll[Byte]](1).get) == blake
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import org.ergoplatform.ErgoBox.R4
import org.ergoplatform._
import org.scalatest.TryValues._
import scorex.crypto.hash.Blake2b256
import sigma.{Colls, VersionContext}
import sigma.ast.SCollection.SByteArray
import sigma.ast._
import sigmastate._
import sigma.ast.syntax._
import sigma.data.{AvlTreeData, ProveDHTuple, ProveDlog, TrivialProp}
import sigma.data.{AvlTreeData, CBox, ProveDHTuple, ProveDlog, TrivialProp}
import sigma.util.Extensions.EcpOps
import sigma.validation.ValidationException
import sigmastate.eval._
Expand Down Expand Up @@ -51,8 +52,8 @@ class ErgoLikeInterpreterSpecification extends CompilerTestingCommons
.withErgoTreeVersion(ergoTreeVersionInTests)

val e = compile(Map(
"h1" -> ErgoTree.withSegregation(ergoTreeHeaderInTests, h1).bytes,
"h2" -> ErgoTree.withSegregation(ergoTreeHeaderInTests, h2).bytes),
"h1" -> Colls.fromArray(ErgoTree.withSegregation(ergoTreeHeaderInTests, h1).bytes),
"h2" -> Colls.fromArray(ErgoTree.withSegregation(ergoTreeHeaderInTests, h2).bytes)),
"h1 == h1")
val exp = TrueLeaf
e shouldBe exp
Expand Down Expand Up @@ -192,7 +193,7 @@ class ErgoLikeInterpreterSpecification extends CompilerTestingCommons
val spendingTransaction = createTransaction(newBoxes)

def mixingRequestProp(sender: ProveDlog, timeout: Int): ErgoTree = {
val env = Map("sender" -> sender, "timeout" -> timeout, "properHash" -> properHash)
val env = Map("sender" -> sender, "timeout" -> timeout, "properHash" -> Colls.fromArray(properHash))
val compiledProp = compile(env,
"""{
| val notTimePassed = HEIGHT <= timeout
Expand Down Expand Up @@ -459,9 +460,14 @@ class ErgoLikeInterpreterSpecification extends CompilerTestingCommons

val pubkey1 = prover.dlogSecrets.head.publicImage
val pubkey2 = prover.dlogSecrets(1).publicImage
val tb = testBox(value = 10, ergoTree = mkTestErgoTree(pubkey1), creationHeight = 0)
val brother = if(VersionContext.current.isJitActivated) {
CBox(tb)
} else {
tb
}

val brother = testBox(value = 10, ergoTree = mkTestErgoTree(pubkey1), creationHeight = 0)
val brotherWithWrongId = testBox(value = 10,
val tbWithWrongId = testBox(value = 10,
ergoTree = mkTestErgoTree(pubkey1),
creationHeight = 0,
boxIndex = 120: Short)
Expand All @@ -482,7 +488,7 @@ class ErgoLikeInterpreterSpecification extends CompilerTestingCommons

val propExpected = BinAnd(
EQ(SizeOf(Inputs), IntConstant(2)),
EQ(ExtractId(ByIndex(Inputs, 0)), ExtractId(BoxConstant(brother)))).toSigmaProp
EQ(ExtractId(ByIndex(Inputs, 0)), ExtractId(BoxConstant(tb)))).toSigmaProp
prop shouldBe propExpected

// try a version of the script that matches the white paper
Expand All @@ -496,18 +502,18 @@ class ErgoLikeInterpreterSpecification extends CompilerTestingCommons
currentHeight = 50,
lastBlockUtxoRoot = AvlTreeData.dummy,
minerPubkey = ErgoLikeContextTesting.dummyPubkey,
boxesToSpend = IndexedSeq(brother, s),
boxesToSpend = IndexedSeq(tb, s),
spendingTransaction,
self = s, activatedVersionInTests)

val pr = prover.prove(emptyEnv + (ScriptNameProp -> "prove_prop"), propTree, ctx, fakeMessage).getOrThrow
verifier.verify(emptyEnv + (ScriptNameProp -> "verify_prop"), propTree, ctx, pr, fakeMessage).getOrThrow._1 shouldBe true
val pr = prover.prove(emptyEnv, propTree, ctx, fakeMessage).getOrThrow
verifier.verify(emptyEnv, propTree, ctx, pr, fakeMessage).getOrThrow._1 shouldBe true

val wrongCtx = ErgoLikeContextTesting(
currentHeight = 50,
lastBlockUtxoRoot = AvlTreeData.dummy,
minerPubkey = ErgoLikeContextTesting.dummyPubkey,
boxesToSpend = IndexedSeq(brotherWithWrongId, s),
boxesToSpend = IndexedSeq(tbWithWrongId, s),
spendingTransaction,
self = s, activatedVersionInTests)

Expand All @@ -522,9 +528,9 @@ class ErgoLikeInterpreterSpecification extends CompilerTestingCommons
}""".stripMargin).asBoolValue.toSigmaProp
val prop2Tree = mkTestErgoTree(prop2)

prover.prove(emptyEnv + (ScriptNameProp -> "prove_prop2"), prop2Tree, ctx, fakeMessage).isFailure shouldBe true
prover.prove(emptyEnv, prop2Tree, ctx, fakeMessage).isFailure shouldBe true
verifier
.verify(emptyEnv + (ScriptNameProp -> "verify_prop2"), prop2Tree, ctx, pr, fakeMessage)
.verify(emptyEnv, prop2Tree, ctx, pr, fakeMessage)
.getOrThrow._1 shouldBe false
}

Expand All @@ -547,7 +553,12 @@ class ErgoLikeInterpreterSpecification extends CompilerTestingCommons
val newBoxes = IndexedSeq(newBox)
val spendingTransaction = createTransaction(newBoxes)

val env = Map("friend" -> friend)
val friendVar = if(VersionContext.current.isJitActivated){
CBox(friend)
} else {
friend
}
val env = Map("friend" -> friendVar)
val prop = compile(env,
"""{
|
Expand Down Expand Up @@ -623,7 +634,7 @@ class ErgoLikeInterpreterSpecification extends CompilerTestingCommons

val helloHash = Blake2b256.hash(preimageHello)

val env = Map("helloHash" -> helloHash)
val env = Map("helloHash" -> Colls.fromArray(helloHash))
val prop = compile(env,
"""{
| val cond = INPUTS(0).value > 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class AtomicSwapExampleSpecification extends CompilerTestingCommons with Compile
val deadlineB = 500

val env = Map(
ScriptNameProp -> "atomic",
"height1" -> height1, "height2" -> height2,
"deadlineBob" -> deadlineB, "deadlineAlice" -> deadlineA,
"pubkeyA" -> pubkeyA, "pubkeyB" -> pubkeyB, "hx" -> hx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingIn
import sigmastate.helpers.TestingHelpers._
import sigmastate.interpreter.Interpreter.ScriptNameProp
import sigma.ast.syntax._
import sigmastate.interpreter.Interpreter


class ColdWalletAdvContractExampleSpecification extends CompilerTestingCommons
Expand All @@ -35,7 +36,6 @@ class ColdWalletAdvContractExampleSpecification extends CompilerTestingCommons
val minSpend = 100

val env = Map(
ScriptNameProp -> "env",
"user1" -> alicePubKey,
"user2" -> bobPubKey,
"user3" -> carolPubKey,
Expand Down Expand Up @@ -109,12 +109,9 @@ class ColdWalletAdvContractExampleSpecification extends CompilerTestingCommons
)
)

val dave = new ErgoLikeTestProvingInterpreter // paying to dave, some arbitrary user
val davePubKey = dave.dlogSecrets.head.publicImage

val firstWithdrawHeight = depositHeight + 1 //

val spendEnv = Map(ScriptNameProp -> "spendEnv")
val spendEnv = Interpreter.emptyEnv

// One of Alice, Bob or Carol withdraws
val firstWithdrawAmount1Key = depositAmount * percent1Key / 100 // less than or equal to percent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class ColdWalletContractExampleSpecification extends CompilerTestingCommons
val minSpend = 100

val env = Map(
ScriptNameProp -> "env",
"alice" -> alicePubKey,
"bob" -> bobPubKey,
"blocksIn24h" -> IntConstant(blocksIn24h),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class DHTupleExampleSpecification extends CompilerTestingCommons
val g_x = alicePubKey.value // g_x is Alice's public key (g_x = g^x)

val env = Map(
ScriptNameProp -> "env",
"g" -> g.toGroupElement,
"g_x" -> g_x.toGroupElement
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class DemurrageExampleSpecification extends CompilerTestingCommons
val regScript = userProver.dlogSecrets.head.publicImage

val env = Map(
ScriptNameProp -> "Demurrage",
"demurragePeriod" -> demurragePeriod,
"demurrageCoeff" -> demurrageCoeff,
"regScript" -> regScript
Expand Down
Loading

0 comments on commit 19c752d

Please sign in to comment.