Skip to content

Commit

Permalink
ergotree-version: update DefaultHeader to latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
aslesarenko committed Sep 24, 2023
1 parent 11c98a3 commit c2f27b2
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import sigmastate.crypto.DLogProtocol.{ProveDlog, ProveDlogProp}
import sigmastate.exceptions.SigmaException
import sigmastate.serialization._
import sigmastate.utxo.{DeserializeContext, Slice}
import sigma.Coll
import sigma.{Coll, VersionContext}
import sigmastate.Values.ErgoTree.{ZeroHeader, setVersionBits}

import scala.util.Try

Expand Down Expand Up @@ -163,7 +164,11 @@ class Pay2SHAddress(val scriptHash: Array[Byte])(implicit val encoder: ErgoAddre
ByteArrayConstant(scriptHash)
)
val scriptIsCorrect = DeserializeContext(scriptId, SSigmaProp)
ErgoTree.withoutSegregation(SigmaAnd(hashEquals.toSigmaProp, scriptIsCorrect))
// Get script version either from the default context or from a context provided by an application
// This is never part of the consensus and can be controlled by applications
val treeVersion = VersionContext.current.ergoTreeVersion
val header = setVersionBits(ZeroHeader, treeVersion)
ErgoTree.withoutSegregation(header, SigmaAnd(hashEquals.toSigmaProp, scriptIsCorrect))
}

override def equals(obj: Any): Boolean = obj match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import sigmastate.crypto.DLogProtocol.ProveDlog
import sigmastate.crypto.CryptoConstants
import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer
import sigmastate.SCollection.SByteArray
import sigmastate.Values.ErgoTree.HeaderType
import sigmastate.Values.ErgoTree.{HeaderType, ZeroHeader}
import sigmastate.Values.{ErgoTree, FalseSigmaProp, IntArrayConstant, IntConstant, LongConstant, SigmaPropConstant, SigmaPropValue, TrueSigmaProp, Value}
import sigmastate.utxo._
import sigmastate._
Expand Down Expand Up @@ -50,10 +50,10 @@ object ErgoTreePredef {
* Required script of the box, that collects mining rewards
*/
def rewardOutputScript(delta: Int, minerPk: ProveDlog): ErgoTree = {
SigmaAnd(
ErgoTree.withSegregation(ZeroHeader, SigmaAnd(
GE(Height, Plus(boxCreationHeight(Self), IntConstant(delta))).toSigmaProp,
SigmaPropConstant(minerPk)
).treeWithSegregation
))
}

/**
Expand All @@ -62,11 +62,11 @@ object ErgoTreePredef {
*/
def feeProposition(delta: Int = 720): ErgoTree = {
val out = ByIndex(Outputs, IntConstant(0))
AND(
ErgoTree.withSegregation(ZeroHeader, AND(
EQ(Height, boxCreationHeight(out)),
EQ(ExtractScriptBytes(out), expectedMinerOutScriptBytesVal(delta, MinerPubkey)),
EQ(SizeOf(Outputs), 1)
).toSigmaProp.treeWithSegregation
).toSigmaProp)
}

/**
Expand All @@ -92,11 +92,13 @@ object ErgoTreePredef {
EQ(ExtractScriptBytes(minerOut), expectedMinerOutScriptBytesVal(s.minerRewardDelay, MinerPubkey)),
EQ(Height, boxCreationHeight(minerOut))
)
AND(
heightIncreased,
correctMinerOutput,
OR(AND(outputsNum, sameScriptRule, correctCoinsConsumed, heightCorrect), lastCoins)
).toSigmaProp.treeWithSegregation
ErgoTree.withSegregation(ZeroHeader,
AND(
heightIncreased,
correctMinerOutput,
OR(AND(outputsNum, sameScriptRule, correctCoinsConsumed, heightCorrect), lastCoins)
).toSigmaProp
)
}

/**
Expand Down Expand Up @@ -153,7 +155,7 @@ object ErgoTreePredef {
// check, that additional rules defined by foundation members are satisfied
val customProposition = DeserializeRegister(ErgoBox.R4, SSigmaProp)
// combine 3 conditions above with AND conjunction
SigmaAnd(amountCorrect.toSigmaProp, sameScriptRule.toSigmaProp, customProposition).treeWithSegregation
ErgoTree.withSegregation(ZeroHeader, SigmaAnd(amountCorrect.toSigmaProp, sameScriptRule.toSigmaProp, customProposition))
}

/**
Expand Down
2 changes: 1 addition & 1 deletion interpreter/shared/src/main/scala/sigmastate/Values.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,7 @@ object Values {
val ZeroHeader: HeaderType = HeaderType @@ 0.toByte

/** Default value of ErgoTree.header byte */
val DefaultHeader: HeaderType = HeaderType @@ (0/*VersionFlag | SizeFlag*/).toByte
val DefaultHeader: HeaderType = HeaderType @@ (VersionFlag | SizeFlag).toByte

/** Default header with constant segregation enabled. */
val ConstantSegregationHeader: HeaderType = HeaderType @@ (DefaultHeader | ConstantSegregationFlag).toByte
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ case class CSigmaProp(sigmaTree: SigmaBoolean) extends SigmaProp with WrapperOf[
// the same serialization method is used in both cases
// TODO v6.0: add `pk.propBytes(version)` (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/903)
val root = sigmaTree.toSigmaProp
val ergoTree = new ErgoTree(ErgoTree.DefaultHeader, EmptyConstants, Right(root), 0, null, None)
val ergoTree = new ErgoTree(ErgoTree.ZeroHeader, EmptyConstants, Right(root), 0, null, None)
val bytes = DefaultSerializer.serializeErgoTree(ergoTree)
Colls.fromArray(bytes)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package sigmastate

import debox.cfor
import org.scalactic.source.Position

import scala.util.DynamicVariable
import org.scalatest.Tag
import sigmastate.eval.Profiler
import org.scalatest.propspec.AnyPropSpecLike
import sigma.VersionContext

trait CrossVersionProps extends AnyPropSpecLike with TestsBase {
/** Number of times each test property is warmed up (i.e. executed before final execution). */
Expand All @@ -29,7 +31,9 @@ trait CrossVersionProps extends AnyPropSpecLike with TestsBase {
System.gc()
}
forEachScriptAndErgoTreeVersion(activatedVersions, ergoTreeVersions) {
testFun_Run(testName, testFun)
VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) {
testFun_Run(testName, testFun)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,17 @@ class ErgoAddressSpecification extends SigmaDslTesting
// when everything is ok
testPay2SHAddress(addr, script = scriptVarId -> ByteArrayConstant(scriptBytes))

val expectedCost = if (ergoTreeVersionInTests == 0) 88 else 90 // account for size serialized for version > 0

// when limit is low
{
val deliberatelySmallLimit = 24

assertExceptionThrown(
testPay2SHAddress(addr,
script = scriptVarId -> ByteArrayConstant(scriptBytes),
costLimit = deliberatelySmallLimit),
rootCauseLike[CostLimitException](
s"Estimated execution cost 88 exceeds the limit $deliberatelySmallLimit")
s"Estimated execution cost $expectedCost exceeds the limit $deliberatelySmallLimit")
)
}

Expand All @@ -311,7 +312,7 @@ class ErgoAddressSpecification extends SigmaDslTesting
costLimit = deliberatelySmallLimit)
},
rootCauseLike[CostLimitException](
s"Estimated execution cost 88 exceeds the limit $deliberatelySmallLimit")
s"Estimated execution cost $expectedCost exceeds the limit $deliberatelySmallLimit")
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ErgoLikeTransactionSpec extends SigmaDslTesting {
val token2 = "a3ff007f00057600808001ff8f8000019000ffdb806fff7cc0b6015eb37fa600"
val b1 = new ErgoBoxCandidate(
10L,
ErgoTree(ErgoTree.DefaultHeader, Vector(), TrueSigmaProp),
ErgoTree(ErgoTree.ZeroHeader, Vector(), TrueSigmaProp),
100,
Coll(
(Digest32Coll @@@ (ErgoAlgos.decodeUnsafe(token1).toColl)) -> 10000000L,
Expand All @@ -34,7 +34,7 @@ class ErgoLikeTransactionSpec extends SigmaDslTesting {
)
val b1_clone = new ErgoBoxCandidate(
10L,
ErgoTree(ErgoTree.DefaultHeader, Vector(), TrueSigmaProp),
ErgoTree(ErgoTree.ZeroHeader, Vector(), TrueSigmaProp),
100,
Coll(
Digest32Coll @@ (ErgoAlgos.decodeUnsafe(token1).toColl) -> 10000000L,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,23 @@ import org.ergoplatform.{ErgoBox, ErgoLikeContext}
import org.ergoplatform.dsl.ContractSyntax.{ErgoScript, Proposition, Token}
import sigmastate.{AvlTreeData, SType}
import sigmastate.Values.{ErgoTree, EvaluatedValue}
import sigmastate.eval.{CSigmaProp, Evaluation, IRContext, CAnyValue}
import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter, CompilerTestingCommons}
import sigmastate.eval.{CAnyValue, CSigmaProp, Evaluation, IRContext}
import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter}
import sigmastate.helpers.TestingHelpers._
import sigmastate.lang.Terms.ValueOps
import sigma.{AnyValue, SigmaProp}
import sigmastate.Values.ErgoTree.ZeroHeader

case class TestContractSpec(testSuite: CompilerTestingCommons)(implicit val IR: IRContext) extends ContractSpec {

case class TestPropositionSpec(name: String, dslSpec: Proposition, scriptSpec: ErgoScript) extends PropositionSpec {
lazy val ergoTree: ErgoTree = {
val value = testSuite.compile(scriptSpec.env, scriptSpec.code)
val headerFlags = scriptSpec.scriptVersion match {
case Some(version) => ErgoTree.defaultHeaderWithVersion(version)
val header = scriptSpec.scriptVersion match {
case Some(version) => ErgoTree.headerWithVersion(ZeroHeader, version)
case None => testSuite.ergoTreeHeaderInTests
}
val tree: ErgoTree = ErgoTree.fromProposition(headerFlags, value.asSigmaProp)
val tree: ErgoTree = ErgoTree.fromProposition(header, value.asSigmaProp)
tree
}
}
Expand Down
55 changes: 23 additions & 32 deletions sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala
Original file line number Diff line number Diff line change
@@ -1,50 +1,41 @@
package special.sigma

import java.math.BigInteger
import org.ergoplatform.ErgoBox.AdditionalRegisters
import org.ergoplatform._
import org.ergoplatform.settings.ErgoAlgos
import org.scalacheck.Arbitrary._
import org.scalacheck.{Arbitrary, Gen}
import sigma.data.{ExactIntegral, ExactNumeric, ExactOrdering, RType}
import org.scalatest.BeforeAndAfterAll
import scorex.crypto.authds.avltree.batch._
import scorex.crypto.authds.{ADKey, ADValue}
import scorex.crypto.hash.{Blake2b256, Digest32}
import scorex.util.ModifierId
import sigma.Extensions._
import sigma.{VersionContext, _}
import sigma.data.RType._
import sigma.data.{ExactIntegral, ExactNumeric, ExactOrdering, RType}
import sigma.util.Extensions._
import sigmastate.utils.Extensions._
import sigmastate.SCollection._
import sigmastate.Values.IntConstant
import sigmastate.Values.ErgoTree.{HeaderType, ZeroHeader}
import sigmastate.Values.{IntConstant, _}
import sigmastate._
import sigmastate.crypto.DLogProtocol._
import sigmastate.Values._
import sigmastate.lang.Terms.Apply
import sigmastate.crypto.ProveDHTuple
import sigmastate.eval.Extensions._
import sigmastate.eval.OrderingOps._
import sigmastate.eval._
import sigmastate.lang.Terms.{MethodCall, PropertyCall}
import sigmastate.utxo._
import sigma._
import sigma.Extensions._
import sigmastate.helpers.TestingHelpers._
import sigmastate.interpreter._
import sigmastate.lang.Terms.{Apply, MethodCall, PropertyCall}
import sigmastate.serialization.OpCodes.OpCode
import sigmastate.utils.Extensions._
import sigmastate.utils.Helpers
import sigmastate.utils.Helpers._
import sigmastate.helpers.TestingHelpers._

import scala.util.{Failure, Success, Try}
import OrderingOps._
import org.ergoplatform.ErgoBox.AdditionalRegisters
import org.scalacheck.Arbitrary._
import org.scalacheck.Gen.frequency
import org.scalatest.{BeforeAndAfterAll, Tag}
import sigma.data.RType._
import scorex.util.ModifierId
import sigmastate.crypto.ProveDHTuple
import sigmastate.interpreter._
import org.scalactic.source.Position
import sigma.VersionContext
import sigmastate.Values.ErgoTree.HeaderType
import sigmastate.helpers.SigmaPPrint
import sigmastate.exceptions.GraphBuildingException
import supertagged.classic.@@
import sigmastate.utxo._

import java.math.BigInteger
import scala.collection.compat.immutable.ArraySeq
import scala.util.{Failure, Success}

/** This suite tests every method of every SigmaDsl type to be equivalent to
* the evaluation of the corresponding ErgoScript operation.
Expand Down Expand Up @@ -9656,14 +9647,14 @@ class SigmaDslSpecification extends SigmaDslTesting

property("substConstants equivalence") {
// tree without constant segregation
val t1 = ErgoTree(ErgoTree.DefaultHeader, Vector(), TrueSigmaProp)
val t1 = ErgoTree(ErgoTree.ZeroHeader, Vector(), TrueSigmaProp)
// tree with constant segregation, but without constants
val t2 = ErgoTree(ErgoTree.ConstantSegregationHeader, Vector(), TrueSigmaProp)
val t2 = ErgoTree(ErgoTree.setConstantSegregation(ZeroHeader), Vector(), TrueSigmaProp)
// tree with one segregated constant
val t3 = ErgoTree(ErgoTree.ConstantSegregationHeader, Vector(TrueSigmaProp), ConstantPlaceholder(0, SSigmaProp))
val t3 = ErgoTree(ErgoTree.setConstantSegregation(ZeroHeader), Vector(TrueSigmaProp), ConstantPlaceholder(0, SSigmaProp))
// tree with one segregated constant of different type
val t4 = ErgoTree(
ErgoTree.ConstantSegregationHeader,
ErgoTree.setConstantSegregation(ZeroHeader),
Vector(IntConstant(10)),
BoolToSigmaProp(EQ(ConstantPlaceholder(0, SInt), IntConstant(20))))
def costDetails(i: Int) = TracedCost(
Expand Down
3 changes: 2 additions & 1 deletion sc/shared/src/test/scala/sigma/SigmaDslTesting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import sigma.util.CollectionUtil._
import sigma.util.Extensions._
import sigma.util.StringUtil.StringUtilExtensions
import sigmastate.SType.AnyOps
import sigmastate.Values.ErgoTree.ZeroHeader
import sigmastate.Values.{ByteArrayConstant, Constant, ConstantNode, ErgoTree, IntConstant, SValue}
import sigmastate.crypto.DLogProtocol.{DLogProverInput, ProveDlog}
import sigmastate.crypto.SigmaProtocolPrivateInput
Expand Down Expand Up @@ -325,7 +326,7 @@ class SigmaDslTesting extends AnyPropSpec
pkAlice,
DeserializeRegister(ErgoBox.R5, SSigmaProp), // deserialize pkBob
DeserializeContext(2, SSigmaProp))) // deserialize pkCarol
val header = ErgoTree.defaultHeaderWithVersion(ergoTreeVersionInTests)
val header = ErgoTree.headerWithVersion(ZeroHeader, ergoTreeVersionInTests)
ErgoTree.withSegregation(header, sigmastate.SigmaOr(prop, multisig))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class SoftForkabilitySpecification extends SigmaTestingData
// cast Boolean typed prop to SigmaProp (which is invalid) // ErgoTree v0
lazy val invalidPropV1: ErgoTree =
ErgoTree.fromProposition(
ErgoTree.defaultHeaderWithVersion(0),
ErgoTree.headerWithVersion(ZeroHeader, 0),
booleanPropV1.asSigmaProp)

lazy val invalidTxV1 = createTransaction(createBox(boxAmt, invalidPropV1, 1))
Expand All @@ -48,7 +48,7 @@ class SoftForkabilitySpecification extends SigmaTestingData
lazy val propV1 = booleanPropV1.toSigmaProp
lazy val txV1 = createTransaction(
createBox(boxAmt,
ErgoTree.fromProposition(ErgoTree.defaultHeaderWithVersion(0), propV1), // ErgoTree v0
ErgoTree.fromProposition(ErgoTree.headerWithVersion(ZeroHeader, 0), propV1), // ErgoTree v0
1))
lazy val txV1bytes = txV1.messageToSign

Expand Down Expand Up @@ -163,7 +163,7 @@ class SoftForkabilitySpecification extends SigmaTestingData
// prepare bytes using default serialization and then replacing version in the header
val v2tree_withoutSize_bytes = runOnV2Node {
val tree = ErgoTree.fromProposition(
ErgoTree.defaultHeaderWithVersion(0), propV2) // ErgoTree v0
ErgoTree.headerWithVersion(ZeroHeader, 0), propV2) // ErgoTree v0
val bytes = tree.bytes
// set version to v2 while not setting the size bit,
// we cannot do this using ErgoTree constructor (due to require() check)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package sigmastate.utxo

import sigmastate.Values.ErgoTree
import sigmastate.Values.ErgoTree.ZeroHeader
import sigmastate._
import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeTestProvingInterpreter, CompilerTestingCommons}
import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeTestProvingInterpreter}
import sigmastate.interpreter._
import sigmastate.lang.Terms._

Expand Down Expand Up @@ -518,7 +520,10 @@ class DistributedSigSpecification extends CompilerTestingCommons

val sigAlice = proverA.signMessage(sigmaTree, msg, bagA).get

val bagB = proverB.bagForMultisig(ctx, sigmaTree, sigAlice, Seq(pubkeyAlice))
val bagB = proverB.bagForMultisig(ctx,
ErgoTree.fromSigmaBoolean(ergoTreeHeaderInTests, sigmaTree),
proof = sigAlice,
realSecretsToExtract = Seq(pubkeyAlice))
.addHint(hintsFromBob.ownCommitments.head)

val sigBob = proverB.signMessage(sigmaTree, msg, bagB).get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import scala.collection.mutable
import scala.util.{Random, Try}
import scorex.util._
import sigma.Colls
import sigmastate.Values.ErgoTree.ZeroHeader
import sigmastate.interpreter.ContextExtension
import sigmastate.interpreter.Interpreter.{ScriptNameProp, emptyEnv}
import sigmastate.utxo.blockchain.BlockchainSimulationTestingCommons.{FullBlock, ValidationState}
Expand Down Expand Up @@ -155,7 +156,7 @@ object BlockchainSimulationTestingCommons extends CompilerTestingCommons {
val boxes = (1 to 50).map(_ =>
testBox(10,
ErgoTree.fromProposition(
ErgoTree.defaultHeaderWithVersion(scriptVersion),
ErgoTree.headerWithVersion(ZeroHeader, scriptVersion),
Values.TrueLeaf.toSigmaProp),
i, Seq(), Map(), txId))
createTransaction(boxes)
Expand Down
Loading

0 comments on commit c2f27b2

Please sign in to comment.