Skip to content

Commit

Permalink
ergo tree version removed from withVersions
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Dec 6, 2024
1 parent 2effbba commit 8993b3a
Show file tree
Hide file tree
Showing 20 changed files with 64 additions and 61 deletions.
3 changes: 1 addition & 2 deletions core/shared/src/main/scala/sigma/VersionContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,10 @@ object VersionContext {
* necessary versions of Ergo protocol and ErgoTree.
*
* @param activatedVersion Currently activated script version == Block.headerVersion - 1
* @param ergoTreeVersion ErgoTree version to be set on the current thread
* @param block block of code to execute
* @return result of block execution
*/
def withVersions[T](activatedVersion: Byte, ergoTreeVersion: Byte)(block: => T): T =
def withScriptVersion[T](activatedVersion: Byte)(block: => T): T =
_versionContext.withValue(VersionContext(activatedVersion))(block)

/** Checks the version context has the given versions*/
Expand Down
4 changes: 2 additions & 2 deletions core/shared/src/test/scala/sigma/CollsTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class CollsTests extends AnyPropSpec with ScalaCheckPropertyChecks with Matchers
equalLengthMapped(pairs, squared(inc)) // due to problem with append
}
}
VersionContext.withVersions(VersionContext.JitActivationVersion, VersionContext.JitActivationVersion) {
VersionContext.withScriptVersion(VersionContext.JitActivationVersion) {
// TODO v5.0: make it work
// equalLengthMapped(pairs, squared(inc)) // problem fixed in v5.0
}
Expand All @@ -75,7 +75,7 @@ class CollsTests extends AnyPropSpec with ScalaCheckPropertyChecks with Matchers
equalLengthMapped(pairs.append(pairs), squared(inc)) // due to problem with append
}
}
VersionContext.withVersions(VersionContext.JitActivationVersion, VersionContext.JitActivationVersion) {
VersionContext.withScriptVersion(VersionContext.JitActivationVersion) {
// TODO v5.0: make it work
// equalLengthMapped(pairs.append(pairs), squared(inc)) // problem fixed in v5.0
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ trait VersionTestingProperty extends AnyPropSpec with VersionTesting {
(implicit pos: Position): Unit = {
super.property(testName, testTags:_*) {
forEachScriptAndErgoTreeVersion(activatedVersions, ergoTreeVersions) {
VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) {
VersionContext.withScriptVersion(activatedVersionInTests) {
testFun_Run(testName, testFun)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class ErgoTreeSerializer(protocolVersion: Byte = VersionContext.V6SoftForkVersio
val r = SigmaSerializer.startReader(bytes)
deserializeErgoTree(r, SigmaSerializer.MaxPropositionSize)
}

def deserializeErgoTree(r: SigmaByteReader, maxTreeSizeBytes: Int): ErgoTree = {
val startPos = r.position
val previousPositionLimit = r.positionLimit
Expand Down Expand Up @@ -385,4 +385,8 @@ class ErgoTreeSerializer(protocolVersion: Byte = VersionContext.V6SoftForkVersio
(resBytes, nConstants)
}

}

object ErgoTreeSerializer {
val DefaultSerializer = new ErgoTreeSerializer
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ trait Interpreter {
ctx: CTX,
env: ScriptEnv): ReductionResult = {
val context = ctx.withErgoTreeVersion(ergoTree.version).asInstanceOf[CTX]
VersionContext.withVersions(context.activatedScriptVersion, ergoTree.version) {
VersionContext.withScriptVersion(context.activatedScriptVersion) {
val prop = propositionFromErgoTree(ergoTree, context)

val res = prop match {
Expand All @@ -217,7 +217,7 @@ trait Interpreter {
ReductionResult(sb, resCost)
case _ if !ergoTree.hasDeserialize =>
val ctx = context.asInstanceOf[ErgoLikeContext]
val res = VersionContext.withVersions(ctx.activatedScriptVersion, ergoTree.version) {
val res = VersionContext.withScriptVersion(ctx.activatedScriptVersion) {
CErgoTreeEvaluator.evalToCrypto(ctx, ergoTree, evalSettings)
}
res
Expand All @@ -242,7 +242,7 @@ trait Interpreter {
context: CTX,
env: ScriptEnv): ReductionResult = {
implicit val vs: SigmaValidationSettings = context.validationSettings
val res = VersionContext.withVersions(context.activatedScriptVersion, ergoTree.version) {
val res = VersionContext.withScriptVersion(context.activatedScriptVersion) {
val deserializeSubstitutionCost = java7.compat.Math.multiplyExact(ergoTree.bytes.length, CostPerTreeByte)
val currCost = addCostChecked(context.initCost, deserializeSubstitutionCost, context.costLimit)
val context1 = context.withInitCost(currCost).asInstanceOf[CTX]
Expand Down Expand Up @@ -359,7 +359,7 @@ trait Interpreter {
case Some(resWhenSoftFork) => return Success(resWhenSoftFork)
case None => // proceed normally
}
VersionContext.withVersions(context.activatedScriptVersion, ergoTree.version) {
VersionContext.withScriptVersion(context.activatedScriptVersion) {
// NOTE, ergoTree.complexity is not acrued to the cost in v5.0
val reduced = fullReduction(ergoTree, context, env)
reduced.value match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ trait ProverInterpreter extends Interpreter with ProverUtils {
case None => // proceed normally
}

VersionContext.withVersions(context.activatedScriptVersion, ergoTree.version) {
VersionContext.withScriptVersion(context.activatedScriptVersion) {
val (resValue, resCost) = {
val reduced = fullReduction(ergoTree, context, env)
val fullCost = addCryptoCost(reduced.value, reduced.cost, context.costLimit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class DataSerializerSpecification extends SerializationSpecification {

withVersion match {
case Some(ver) =>
VersionContext.withVersions(ver, 1) {
VersionContext.withScriptVersion(ver) {
test()
}
case None =>
Expand Down Expand Up @@ -122,14 +122,14 @@ class DataSerializerSpecification extends SerializationSpecification {
val tT = Evaluation.stypeToRType(tpe)

an[Exception] should be thrownBy (
VersionContext.withVersions((VersionContext.V6SoftForkVersion - 1).toByte, 1) {
VersionContext.withScriptVersion((VersionContext.V6SoftForkVersion - 1).toByte) {
forAll { in: T#WrappedType =>
roundtrip[SType](Some(in).asWrappedType, SOption(tpe))
roundtrip[SOption[SCollection[T]]](Some(Colls.fromItems(in)(tT)), SOption(SCollectionType(tpe)))
}
})

VersionContext.withVersions(VersionContext.V6SoftForkVersion, 1) {
VersionContext.withScriptVersion(VersionContext.V6SoftForkVersion) {
forAll { in: T#WrappedType =>
roundtrip[SType](Some(in).asWrappedType, SOption(tpe))
roundtrip[SOption[T]](None, SOption(tpe))
Expand Down Expand Up @@ -189,12 +189,12 @@ class DataSerializerSpecification extends SerializationSpecification {
}

property("nuanced versioned test for header roundtrip") {
VersionContext.withVersions(VersionContext.V6SoftForkVersion, 1) {
VersionContext.withScriptVersion(VersionContext.V6SoftForkVersion) {
forAll { x: Header => roundtrip[SHeader.type](x, SHeader) }
}

an[SerializerException] should be thrownBy (
VersionContext.withVersions((VersionContext.V6SoftForkVersion - 1).toByte, 1) {
VersionContext.withScriptVersion((VersionContext.V6SoftForkVersion - 1).toByte) {
val h = headerGen.sample.get
roundtrip[SHeader.type](h, SHeader)
})
Expand All @@ -219,7 +219,7 @@ class DataSerializerSpecification extends SerializationSpecification {
Colls.emptyColl
)

VersionContext.withVersions(VersionContext.V6SoftForkVersion, 1) {
VersionContext.withScriptVersion(VersionContext.V6SoftForkVersion) {
roundtrip[SHeader.type](header, SHeader)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ class MethodCallSerializerSpecification extends SerializationSpecification {
roundTripTest(expr)
}

VersionContext.withVersions(VersionContext.V6SoftForkVersion, 1) {
VersionContext.withScriptVersion(VersionContext.V6SoftForkVersion) {
code
}

a[SerializerException] should be thrownBy (
VersionContext.withVersions((VersionContext.V6SoftForkVersion - 1).toByte, 1) {
VersionContext.withScriptVersion((VersionContext.V6SoftForkVersion - 1).toByte) {
code
}
)
Expand All @@ -65,12 +65,12 @@ class MethodCallSerializerSpecification extends SerializationSpecification {
roundTripTest(expr)
}

VersionContext.withVersions(VersionContext.V6SoftForkVersion, 1) {
VersionContext.withScriptVersion(VersionContext.V6SoftForkVersion) {
code
}

an[ValidationException] should be thrownBy (
VersionContext.withVersions((VersionContext.V6SoftForkVersion - 1).toByte, 1) {
VersionContext.withScriptVersion((VersionContext.V6SoftForkVersion - 1).toByte) {
code
}
)
Expand All @@ -87,12 +87,12 @@ class MethodCallSerializerSpecification extends SerializationSpecification {
roundTripTest(expr)
}

VersionContext.withVersions(VersionContext.V6SoftForkVersion, 1) {
VersionContext.withScriptVersion(VersionContext.V6SoftForkVersion) {
code
}

an[ValidationException] should be thrownBy (
VersionContext.withVersions((VersionContext.V6SoftForkVersion - 1).toByte, 1) {
VersionContext.withScriptVersion((VersionContext.V6SoftForkVersion - 1).toByte) {
code
}
)
Expand All @@ -111,12 +111,12 @@ class MethodCallSerializerSpecification extends SerializationSpecification {

println(SGlobalMethods.deserializeToMethod.hasExplicitTypeArgs)

VersionContext.withVersions(VersionContext.V6SoftForkVersion, 1) {
VersionContext.withScriptVersion(VersionContext.V6SoftForkVersion) {
code
}

an[Exception] should be thrownBy (
VersionContext.withVersions((VersionContext.V6SoftForkVersion - 1).toByte, 1) {
VersionContext.withScriptVersion((VersionContext.V6SoftForkVersion - 1).toByte) {
code
})
}
Expand All @@ -133,12 +133,12 @@ class MethodCallSerializerSpecification extends SerializationSpecification {
}

// should be ok
VersionContext.withVersions(VersionContext.V6SoftForkVersion, 1) {
VersionContext.withScriptVersion(VersionContext.V6SoftForkVersion) {
code
}

an[ValidationException] should be thrownBy (
VersionContext.withVersions((VersionContext.V6SoftForkVersion - 1).toByte, 1) {
VersionContext.withScriptVersion((VersionContext.V6SoftForkVersion - 1).toByte) {
code
})
}
Expand All @@ -154,12 +154,12 @@ class MethodCallSerializerSpecification extends SerializationSpecification {
roundTripTest(expr)
}

VersionContext.withVersions(VersionContext.V6SoftForkVersion, 1) {
VersionContext.withScriptVersion(VersionContext.V6SoftForkVersion) {
code
}

an[ValidationException] should be thrownBy (
VersionContext.withVersions((VersionContext.V6SoftForkVersion - 1).toByte, 1) {
VersionContext.withScriptVersion((VersionContext.V6SoftForkVersion - 1).toByte) {
code
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ trait SerializationSpecification extends AnyPropSpec
}
withVersion match {
case Some(ver) =>
VersionContext.withVersions(ver, 0) {
VersionContext.withScriptVersion(ver) {
test()
}
case None =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ trait CrossVersionProps extends AnyPropSpecLike with TestsBase {
System.gc()
}
forEachScriptAndErgoTreeVersion(activatedVersions, ergoTreeVersions) {
VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) {
VersionContext.withScriptVersion(activatedVersionInTests) {
testFun_Run(testName, testFun)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class BasicOpsTests extends AnyFunSuite with ContractsTestkit with Matchers {
coster = accumulator, DefaultProfiler, es)

val msg = Colls.fromArray(Base16.decode("0a101b8c6a4f2e").get)
VersionContext.withVersions(VersionContext.V6SoftForkVersion, VersionContext.V6SoftForkVersion) {
VersionContext.withScriptVersion(VersionContext.V6SoftForkVersion) {
val res = MethodCall(Global, SGlobalMethods.xorMethod,
IndexedSeq(ByteArrayConstant(msg), ByteArrayConstant(msg)), Map.empty)
.evalTo[sigma.Coll[Byte]](Map.empty)(evaluator)
Expand Down Expand Up @@ -125,7 +125,7 @@ class BasicOpsTests extends AnyFunSuite with ContractsTestkit with Matchers {
constants = ErgoTree.EmptyConstants,
coster = accumulator, DefaultProfiler, es)

VersionContext.withVersions(VersionContext.V6SoftForkVersion, VersionContext.V6SoftForkVersion) {
VersionContext.withScriptVersion(VersionContext.V6SoftForkVersion) {
val res = MethodCall(Global, SGlobalMethods.powHitMethod,
IndexedSeq(IntConstant(k), ByteArrayConstant(msg), ByteArrayConstant(nonce),
ByteArrayConstant(hbs), IntConstant(N)), Map.empty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ trait LangTests extends Matchers with NegativeTesting {

/** Execute the given `block` having `version` as both activated and ErgoTree version. */
def runWithVersion[T](version: Byte)(block: => T): T = {
VersionContext.withVersions(version, version)(block)
VersionContext.withScriptVersion(version)(block)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class ErgoAddressSpecification extends SigmaDslTesting
property("Pay2SHAddress.script should create ErgoTree v0") {
val (prop, _) = createPropAndScriptBytes()

val address = VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) {
val address = VersionContext.withScriptVersion(activatedVersionInTests) {
Pay2SHAddress(prop)
}
address.script.version shouldBe 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ abstract class LanguageSpecificationBase extends SigmaDslTesting
* parameters.
*/
protected override def testFun_Run(testName: String, testFun: => Any): Unit = {
VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) {
VersionContext.withScriptVersion(activatedVersionInTests) {
super.testFun_Run(testName, testFun)
}
}
Expand Down
18 changes: 9 additions & 9 deletions sc/shared/src/test/scala/sigma/SigmaDslTesting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -527,11 +527,11 @@ class SigmaDslTesting extends AnyPropSpec
*/
def checkEquality(input: A, logInputOutput: Boolean = false): Try[(B, CostDetails)] = {
// check the old implementation against Scala semantic function
val oldRes = VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) {
val oldRes = VersionContext.withScriptVersion(activatedVersionInTests) {
checkEq(scalaFunc)(oldF)(input)
}

val newRes = VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) {
val newRes = VersionContext.withScriptVersion(activatedVersionInTests) {
checkEq(scalaFunc)({ x =>
var y: (B, CostDetails) = null
val N = nBenchmarkIters + 1
Expand Down Expand Up @@ -588,13 +588,13 @@ class SigmaDslTesting extends AnyPropSpec
*/
override def checkExpected(input: A, expected: Expected[B]): Unit = {
// check the old implementation with Scala semantic
val (oldRes, _) = VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) {
val (oldRes, _) = VersionContext.withScriptVersion(activatedVersionInTests) {
checkEq(scalaFunc)(oldF)(input).get
}
oldRes shouldBe expected.value.get

// check the new implementation with Scala semantic
val (newRes, newDetails) = VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) {
val (newRes, newDetails) = VersionContext.withScriptVersion(activatedVersionInTests) {
checkEq(scalaFunc)(newF)(input).get
}
newRes shouldBe expected.value.get
Expand Down Expand Up @@ -716,7 +716,7 @@ class SigmaDslTesting extends AnyPropSpec
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
oldRes = VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) {
oldRes = VersionContext.withScriptVersion(activatedVersionInTests) {
try checkEq(scalaFunc)(oldF)(input)
catch {
case e: TestFailedException =>
Expand All @@ -734,7 +734,7 @@ class SigmaDslTesting extends AnyPropSpec

val newRes = {
// check the new implementation against Scala semantic function
val newRes = VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) {
val newRes = VersionContext.withScriptVersion(activatedVersionInTests) {
checkEq(scalaFuncNew)(newF)(input)
}
if (ergoTreeVersionInTests < VersionContext.JitActivationVersion) {
Expand Down Expand Up @@ -770,7 +770,7 @@ class SigmaDslTesting extends AnyPropSpec
*/
override def checkExpected(input: A, expected: Expected[B]): Unit = {
// check the new implementation with Scala semantic function
val newRes = VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) {
val newRes = VersionContext.withScriptVersion(activatedVersionInTests) {
checkEq(scalaFuncNew)(newF)(input)
}

Expand Down Expand Up @@ -910,7 +910,7 @@ class SigmaDslTesting extends AnyPropSpec
override def checkExpected(input: A, expected: Expected[B]): Unit = {
Try(oldF(input)).isFailure shouldBe true
if (!(newImpl eq oldImpl)) {
val (newRes, _) = VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) {
val (newRes, _) = VersionContext.withScriptVersion(activatedVersionInTests) {
checkEq(scalaFuncNew)(newF)(input).get
}
val newExpectedRes = expected.newResults(ergoTreeVersionInTests)
Expand Down Expand Up @@ -1264,7 +1264,7 @@ class SigmaDslTesting extends AnyPropSpec
val funcNoTrace = funcJit[A, B](f.script)(tA, tB, IR, noTraceSettings, cs)
var iCase = 0
val (res, total) = BenchmarkUtil.measureTimeNano {
VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) {
VersionContext.withScriptVersion(activatedVersionInTests) {
cases.map { x =>
assert(func(x)._1 == f.newF(x)._1)
iCase += 1
Expand Down
Loading

0 comments on commit 8993b3a

Please sign in to comment.