Skip to content

Commit

Permalink
versioned methods check in ErgoTreeSpecification
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Aug 31, 2024
1 parent 107d666 commit bc66fdd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
13 changes: 7 additions & 6 deletions data/shared/src/main/scala/sigma/ast/methods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ case object SBigIntMethods extends SNumericTypeMethods {

protected override def getMethods(): Seq[SMethod] = {
if (VersionContext.current.isV6SoftForkActivated) {
super.getMethods() ++ Seq(ToNBits)
super.getMethods()
// ModQMethod,
// PlusModQMethod,
// MinusModQMethod,
Expand Down Expand Up @@ -1073,7 +1073,7 @@ case object SBoxMethods extends MonoTypeMethods {
ArgInfo("regId", "zero-based identifier of the register."))

lazy val getRegMethodV6 = SMethod(this, "getReg",
SFunc(Array(SBox, SInt), SOption(tT), Array(paramT)), 7, FixedCost(JitCost(50)), Seq(tT))
SFunc(Array(SBox, SInt), SOption(tT), Array(paramT)), 7, ExtractRegisterAs.costKind, Seq(tT))
.withIRInfo(MethodCallIrBuilder,
javaMethodOf[Box, Int, RType[_]]("getReg"),
{ mtype => Array(mtype.tRange.asOption[SType].elemType) })
Expand All @@ -1088,7 +1088,7 @@ case object SBoxMethods extends MonoTypeMethods {
.withIRInfo(MethodCallIrBuilder)
.withInfo(PropertyCall, "Secondary tokens")

lazy val commonBoxMethods = Array(
lazy val commonBoxMethods = super.getMethods() ++ Array(
ValueMethod, // see ExtractAmount
PropositionBytesMethod, // see ExtractScriptBytes
BytesMethod, // see ExtractBytes
Expand All @@ -1108,12 +1108,13 @@ case object SBoxMethods extends MonoTypeMethods {

// should be lazy to solve recursive initialization
protected override def getMethods() = {
if(VersionContext.current.isV6SoftForkActivated) {
super.getMethods() ++ v6Methods
if (VersionContext.current.isV6SoftForkActivated) {
v6Methods
} else {
super.getMethods() ++ v5Methods
v5Methods
}
}

}

/** Type descriptor of `AvlTree` type of ErgoTree. */
Expand Down
13 changes: 10 additions & 3 deletions sc/shared/src/main/scala/sigma/compiler/ir/MethodCalls.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ trait MethodCalls extends Base { self: IRContext =>
* given `method`.
*/
case class MethodCall private[MethodCalls](receiver: Sym, method: RMethod, args: Seq[AnyRef], neverInvoke: Boolean)
(val resultType: Elem[Any], val isAdapterCall: Boolean = false, val typeSubst: Map[STypeVar, SType]) extends Def[Any] {
(val resultType: Elem[Any],
val isAdapterCall: Boolean = false,
val typeSubst: Map[STypeVar, SType]) extends Def[Any] {

override def mirror(t: Transformer): Ref[Any] = {
val len = args.length
Expand Down Expand Up @@ -100,8 +102,13 @@ trait MethodCalls extends Base { self: IRContext =>
}

/** Creates new MethodCall node and returns its node ref. */
def mkMethodCall(receiver: Sym, method: RMethod, args: Seq[AnyRef],
neverInvoke: Boolean, isAdapterCall: Boolean, resultElem: Elem[_], typeSubst: Map[STypeVar, SType] = Map.empty): Sym = {
def mkMethodCall(receiver: Sym,
method: RMethod,
args: Seq[AnyRef],
neverInvoke: Boolean,
isAdapterCall: Boolean,
resultElem: Elem[_],
typeSubst: Map[STypeVar, SType] = Map.empty): Sym = {
reifyObject(MethodCall(receiver, method, args, neverInvoke)(asElem[Any](resultElem), isAdapterCall, typeSubst))
}

Expand Down
13 changes: 8 additions & 5 deletions sc/shared/src/test/scala/sigmastate/ErgoTreeSpecification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import sigma.compiler.CompilerSettings
import sigma.eval.EvalSettings
import sigma.exceptions.{CostLimitException, InterpreterException}
import sigma.serialization.ErgoTreeSerializer.DefaultSerializer
import sigmastate.Plus
import sigmastate.{CrossVersionProps, Plus}
import sigmastate.utils.Helpers.TryOps


/** Regression tests with ErgoTree related test vectors.
* This test vectors verify various constants which are consensus critical and should not change.
*/
class ErgoTreeSpecification extends SigmaDslTesting with ContractsTestkit {
class ErgoTreeSpecification extends SigmaDslTesting with ContractsTestkit with CrossVersionProps {

property("Value.sourceContext") {
val srcCtx = SourceContext.fromParserIndex(0, "")
Expand Down Expand Up @@ -316,7 +316,7 @@ class ErgoTreeSpecification extends SigmaDslTesting with ContractsTestkit {
// NOTE, the type code constants are checked above
// The methodId codes as checked here, they MUST be PRESERVED.
// The following table should be made dependent on HF activation
val methods = Table(
def methods = Table(
("typeId", "methods", "CanHaveMethods"),
(SBoolean.typeId, Seq.empty[MInfo], true),
(SByte.typeId, Seq.empty[MInfo], false),
Expand Down Expand Up @@ -367,9 +367,12 @@ class ErgoTreeSpecification extends SigmaDslTesting with ContractsTestkit {
MInfo(4, BytesWithoutRefMethod),
MInfo(5, IdMethod),
MInfo(6, creationInfoMethod),
MInfo(7, getRegMethodV5),
MInfo(8, tokensMethod)
) ++ registers(idOfs = 8)
) ++ (if (VersionContext.current.isV6SoftForkActivated) {
Seq(MInfo(7, getRegMethodV6))
} else {
Seq(MInfo(7, getRegMethodV5))
}) ++ registers(idOfs = 8)
.zipWithIndex
.map { case (m,i) => MInfo((8 + i + 1).toByte, m) }, true)
},
Expand Down

0 comments on commit bc66fdd

Please sign in to comment.