Skip to content

Commit

Permalink
Merge pull request #920 from ScorexFoundation/ergotree-version
Browse files Browse the repository at this point in the history
HeaderType for ErgoTree.header
  • Loading branch information
aslesarenko authored Oct 21, 2023
2 parents 8442400 + 17e5c59 commit 0b9279e
Show file tree
Hide file tree
Showing 45 changed files with 350 additions and 274 deletions.
4 changes: 2 additions & 2 deletions core/shared/src/main/scala/sigma/VersionContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ object VersionContext {
val JitActivationVersion: Byte = 2

private val _defaultContext = VersionContext(
activatedVersion = 1/* v4.x */,
ergoTreeVersion = 1
activatedVersion = 2/* v5.x */,
ergoTreeVersion = 2
)

/** Universally accessible version context which is used to version the code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ 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 sigma.ast.{SInt, SSigmaProp}
import sigmastate.Values.ErgoTree.{ZeroHeader, setVersionBits}

import scala.util.Try

Expand Down Expand Up @@ -164,7 +165,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 @@ -6,6 +6,7 @@ import sigma.ast.{SBox, SInt, SLong, SSigmaProp}
import sigmastate.crypto.DLogProtocol.ProveDlog
import sigmastate.crypto.CryptoConstants
import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer
import sigmastate.Values.ErgoTree.{HeaderType, ZeroHeader}
import sigmastate.Values.{ErgoTree, FalseSigmaProp, IntArrayConstant, IntConstant, LongConstant, SigmaPropConstant, SigmaPropValue, TrueSigmaProp, Value}
import sigmastate.utxo._
import sigmastate._
Expand All @@ -14,17 +15,17 @@ import sigmastate.lang.Terms.ValueOps
object ErgoTreePredef {
/** Create ErgoTree with `false` proposition, which is never true.
*
* @param headerFlags ErgoTree header flags to be combined with default header
* @param header ErgoTree header to be used in the tree
* @see ErgoTree.headerWithVersion()
*/
def FalseProp(headerFlags: Byte): ErgoTree = ErgoTree.withoutSegregation(headerFlags, FalseSigmaProp)
def FalseProp(header: HeaderType): ErgoTree = ErgoTree.withoutSegregation(header, FalseSigmaProp)

/** Create ErgoTree with `true` proposition, which is always true.
*
* @param headerFlags ErgoTree header flags to be combined with default header
* @param header ErgoTree header to be used in the tree
* @see ErgoTree.headerWithVersion()
*/
def TrueProp(headerFlags: Byte): ErgoTree = ErgoTree.withoutSegregation(headerFlags, TrueSigmaProp)
def TrueProp(header: HeaderType): ErgoTree = ErgoTree.withoutSegregation(header, TrueSigmaProp)

/**
* Byte array value of the serialized reward output script proposition with pk being substituted
Expand All @@ -50,10 +51,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 +63,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 +93,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 +156,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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.ergoplatform.validation

import sigma.ast.{SGlobal, SOption, TypeCodes}
import sigma.util.Extensions.toUByte
import sigmastate.Values.ErgoTree.HeaderType
import sigmastate.Values.{ErgoTree, SValue}
import sigmastate._
import sigmastate.exceptions._
Expand Down Expand Up @@ -241,7 +242,7 @@ object ValidationRules {

object CheckHeaderSizeBit extends ValidationRule(1012,
"For version greater then 0, size bit should be set.") with SoftForkWhenReplaced {
final def apply(header: Byte): Unit = {
final def apply(header: HeaderType): Unit = {
checkRule()
val version = ErgoTree.getVersion(header)
if (version != 0 && !ErgoTree.hasSize(header)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer
import sigmastate.serialization.SigmaSerializer
import sigmastate.utils.SigmaByteWriter
import debox.cfor
import sigmastate.Values.ErgoTree.ZeroHeader
import sigmastate.crypto.GF2_192_Poly

import scala.language.existentials

object ConjectureType extends Enumeration {
Expand Down Expand Up @@ -255,7 +257,7 @@ object FiatShamirTree {
case _: UncheckedDiffieHellmanTuple | _: UnprovenDiffieHellmanTuple => ToBytes_DHT
}
fixedCostOp(costInfo) {
val propTree = ErgoTree.withSegregation(SigmaPropConstant(l.proposition))
val propTree = ErgoTree.withSegregation(ZeroHeader, SigmaPropConstant(l.proposition))
val propBytes = DefaultSerializer.serializeErgoTree(propTree)
val commitmentBytes = l.commitmentOpt.get.bytes
w.put(leafPrefix)
Expand Down
Loading

0 comments on commit 0b9279e

Please sign in to comment.