-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[6.0] Revise liftToConstant method #1021
Changes from 17 commits
fb3e52e
ec215df
d257b5a
1644380
5f6a0d0
19a3e7f
3901ba2
3c06856
40fe947
19c752d
f77be78
6c1ac1e
469eab7
b2ef5b5
326b315
6e34213
780b28b
38c2f63
b85ba9c
3952ab4
2ad87f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,29 +16,32 @@ object Platform { | |
private[sigma] def liftToConstant(obj: Any, builder: SigmaBuilder): Nullable[Constant[SType]] = { | ||
import builder._ | ||
obj match { | ||
case arr: Array[Boolean] => Nullable(mkCollectionConstant[SBoolean.type](arr, SBoolean)) | ||
case arr: Array[Byte] => Nullable(mkCollectionConstant[SByte.type](arr, SByte)) | ||
case arr: Array[Short] => Nullable(mkCollectionConstant[SShort.type](arr, SShort)) | ||
case arr: Array[Int] => Nullable(mkCollectionConstant[SInt.type](arr, SInt)) | ||
case arr: Array[Long] => Nullable(mkCollectionConstant[SLong.type](arr, SLong)) | ||
case arr: Array[BigInteger] => Nullable(mkCollectionConstant[SBigInt.type](arr.map(SigmaDsl.BigInt(_)), SBigInt)) | ||
case arr: Array[String] => Nullable(mkCollectionConstant[SString.type](arr, SString)) | ||
case arr: Array[Boolean] if !VersionContext.current.isV6SoftForkActivated => Nullable(mkCollectionConstant[SBoolean.type](arr, SBoolean)) | ||
case arr: Array[Byte] if !VersionContext.current.isV6SoftForkActivated => Nullable(mkCollectionConstant[SByte.type](arr, SByte)) | ||
case arr: Array[Short] if !VersionContext.current.isV6SoftForkActivated => Nullable(mkCollectionConstant[SShort.type](arr, SShort)) | ||
case arr: Array[Int] if !VersionContext.current.isV6SoftForkActivated => Nullable(mkCollectionConstant[SInt.type](arr, SInt)) | ||
case arr: Array[Long] if !VersionContext.current.isV6SoftForkActivated => Nullable(mkCollectionConstant[SLong.type](arr, SLong)) | ||
case arr: Array[BigInteger] if !VersionContext.current.isV6SoftForkActivated => Nullable(mkCollectionConstant[SBigInt.type](arr.map(SigmaDsl.BigInt(_)), SBigInt)) | ||
case arr: Array[String] if !VersionContext.current.isV6SoftForkActivated => Nullable(mkCollectionConstant[SString.type](arr, SString)) | ||
case v: Byte => Nullable(mkConstant[SByte.type](v, SByte)) | ||
case v: Short => Nullable(mkConstant[SShort.type](v, SShort)) | ||
case v: Int => Nullable(mkConstant[SInt.type](v, SInt)) | ||
case v: Long => Nullable(mkConstant[SLong.type](v, SLong)) | ||
case v: BigInteger => Nullable(mkConstant[SBigInt.type](SigmaDsl.BigInt(v), SBigInt)) | ||
case v: BigInteger if !VersionContext.current.isV6SoftForkActivated => Nullable(mkConstant[SBigInt.type](SigmaDsl.BigInt(v), SBigInt)) | ||
case n: sigma.BigInt => Nullable(mkConstant[SBigInt.type](n, SBigInt)) | ||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PreHeader is missing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
case n: sigma.UnsignedBigInt if VersionContext.current.isV6SoftForkActivated => Nullable(mkConstant[SUnsignedBigInt.type](n, SUnsignedBigInt)) | ||
|
||
case v: String if !VersionContext.current.isV6SoftForkActivated => Nullable(mkConstant[SString.type](v, SString)) | ||
|
||
// The Box lifting was broken in v4.x. `SigmaDsl.Box(b)` was missing which means the | ||
// isCorrectType requirement would fail in ConstantNode constructor. | ||
// This method is used as part of consensus in SubstConstants operation, however | ||
// ErgoBox cannot be passed as argument as it is never valid value during evaluation. | ||
// Thus we can use activation-based versioning and fix this code when v5.0 is activated. | ||
case b: ErgoBox => | ||
case b: ErgoBox if !VersionContext.current.isV6SoftForkActivated => | ||
Nullable(mkConstant[SBox.type](SigmaDsl.Box(b), SBox)) // fixed in v5.0 | ||
|
||
// this case is added in v5.0 and it can be useful when the box value comes from a | ||
|
@@ -48,7 +51,7 @@ object Platform { | |
Nullable(mkConstant[SBox.type](b, SBox)) | ||
else | ||
Nullable.None // return the same result as in v4.x when there was no this case | ||
case avl: AvlTreeData => Nullable(mkConstant[SAvlTree.type](SigmaDsl.avlTree(avl), SAvlTree)) | ||
case avl: AvlTreeData if !VersionContext.current.isV6SoftForkActivated => Nullable(mkConstant[SAvlTree.type](SigmaDsl.avlTree(avl), SAvlTree)) | ||
case avl: AvlTree => Nullable(mkConstant[SAvlTree.type](avl, SAvlTree)) | ||
case sb: SigmaBoolean => Nullable(mkConstant[SSigmaProp.type](SigmaDsl.SigmaProp(sb), SSigmaProp)) | ||
case p: SigmaProp => Nullable(mkConstant[SSigmaProp.type](p, SSigmaProp)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,6 +122,13 @@ class SigmaBuilderTest extends AnyPropSpec with ScalaCheckPropertyChecks with Ma | |
testSuccess(arr, TransformingSigmaBuilder.mkCollectionConstant(arr, c.tpe)) | ||
} | ||
|
||
def testArrayFailure[T <: SType] | ||
(v: T#WrappedType, c: Constant[T])(implicit t: RType[T#WrappedType]) = { | ||
// for any Byte and Short value `v`, lifting of array should succeed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't is generic enough to work for any type? Comment somewhat misleading. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
val arr = Array.fill[T#WrappedType](10)(v)(t.classTag) | ||
testFailure(arr) | ||
} | ||
|
||
def testColl[T <: SType] | ||
(v: T#WrappedType, c: Constant[T])(implicit t: RType[T#WrappedType]) = { | ||
// for any Byte and Short value `v`, lifting of Coll should succeed | ||
|
@@ -134,7 +141,11 @@ class SigmaBuilderTest extends AnyPropSpec with ScalaCheckPropertyChecks with Ma | |
val v = true | ||
val c = BooleanConstant(v) | ||
test[SBoolean.type](v, c) | ||
testArray[SBoolean.type](v, c) // TODO v6.0: arrays should not be liftable directly (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/905) | ||
if (!VersionContext.current.isV6SoftForkActivated) { | ||
testArray[SBoolean.type](v, c) | ||
} else { | ||
testArrayFailure[SBoolean.type](v, c) | ||
} | ||
testColl[SBoolean.type](v, c) | ||
} | ||
|
||
|
@@ -143,7 +154,11 @@ class SigmaBuilderTest extends AnyPropSpec with ScalaCheckPropertyChecks with Ma | |
val c = ByteConstant(v) | ||
testNumeric[SByte.type](v, c) | ||
testLiftingOfCAnyValue[SByte.type](v, c) | ||
testArray[SByte.type](v, c) // TODO v6.0: arrays should not be liftable directly (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/905) | ||
if (!VersionContext.current.isV6SoftForkActivated) { | ||
testArray[SByte.type](v, c) | ||
} else { | ||
testArrayFailure[SByte.type](v, c) | ||
} | ||
testColl[SByte.type](v, c) | ||
} | ||
|
||
|
@@ -152,40 +167,65 @@ class SigmaBuilderTest extends AnyPropSpec with ScalaCheckPropertyChecks with Ma | |
val c = ShortConstant(v) | ||
testNumeric[SShort.type](v, c) | ||
testLiftingOfCAnyValue[SShort.type](v, c) | ||
testArray[SShort.type](v, c) // TODO v6.0: arrays should not be liftable directly (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/905) | ||
if (!VersionContext.current.isV6SoftForkActivated) { | ||
testArray[SShort.type](v, c) | ||
} else { | ||
testArrayFailure[SShort.type](v, c) | ||
} | ||
testColl[SShort.type](v, c) | ||
} | ||
|
||
property("liftToConstant Int") { | ||
val v = 1 | ||
val c = IntConstant(v) | ||
test[SInt.type](v, c) | ||
testArray[SInt.type](v, c) // TODO v6.0: arrays should not be liftable directly (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/905) | ||
if (!VersionContext.current.isV6SoftForkActivated) { | ||
testArray[SInt.type](v, c) | ||
} else { | ||
testArrayFailure[SInt.type](v, c) | ||
} | ||
testColl[SInt.type](v, c) | ||
} | ||
|
||
property("liftToConstant Long") { | ||
val v = 1L | ||
val c = LongConstant(v) | ||
test[SLong.type](v, c) | ||
testArray[SLong.type](v, c) // TODO v6.0: arrays should not be liftable directly (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/905) | ||
if (!VersionContext.current.isV6SoftForkActivated) { | ||
testArray[SLong.type](v, c) | ||
} else { | ||
testArrayFailure[SLong.type](v, c) | ||
} | ||
testColl[SLong.type](v, c) | ||
} | ||
|
||
property("liftToConstant String") { | ||
val v = "abc" | ||
val c = StringConstant(v) | ||
test[SString.type](v, c) | ||
testArray[SString.type](v, c) // TODO v6.0: String should be liftable at all (not supported in ErgoTree) (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/905) | ||
testColl[SString.type](v, c) | ||
if (!VersionContext.current.isV6SoftForkActivated) { | ||
// v6.0: String should be liftable at all (not supported in ErgoTree) (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/905) | ||
kushti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
test[SString.type](v, c) | ||
testArray[SString.type](v, c) | ||
testColl[SString.type](v, c) | ||
} else { | ||
testFailure(v) | ||
} | ||
} | ||
|
||
property("liftToConstant BigInteger") { | ||
val v = BigInteger.valueOf(1L) | ||
val c = BigIntConstant(v) | ||
testSuccess(v, c) // TODO v6.0: both BigInteger and arrays should not be liftable directly (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/905) | ||
if (!VersionContext.current.isV6SoftForkActivated) { | ||
testSuccess(v, c) | ||
} else { | ||
testFailure(v) | ||
} | ||
val arr = Array.fill(10)(v) | ||
testSuccess(arr, TransformingSigmaBuilder.mkCollectionConstant[SBigInt.type](arr.map(SigmaDsl.BigInt), c.tpe)) | ||
if (!VersionContext.current.isV6SoftForkActivated) { | ||
testSuccess(arr, TransformingSigmaBuilder.mkCollectionConstant[SBigInt.type](arr.map(SigmaDsl.BigInt), c.tpe)) | ||
} else { | ||
testFailure(arr) | ||
} | ||
} | ||
|
||
property("liftToConstant BigInt") { | ||
|
@@ -204,10 +244,26 @@ class SigmaBuilderTest extends AnyPropSpec with ScalaCheckPropertyChecks with Ma | |
testColl[SGroupElement.type](v, c) | ||
} | ||
|
||
property("liftToConstant Header") { | ||
val h = TestData.h1 | ||
val c = HeaderConstant(h) | ||
if (VersionContext.current.isV6SoftForkActivated) { | ||
testSuccess(h, c) | ||
} else { | ||
testFailure(h) | ||
} | ||
testFailure(Array.fill(10)(h)) | ||
testColl[SHeader.type](h, c) | ||
} | ||
|
||
property("liftToConstant ErgoBox") { | ||
val v = TestData.b2.asInstanceOf[CBox].wrappedValue | ||
val c = BoxConstant(TestData.b2) | ||
testSuccess(v, c) // TODO v6.0: ErgoBox should not be liftable directly (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/905) | ||
if (!VersionContext.current.isV6SoftForkActivated) { | ||
testSuccess(v, c) | ||
} else { | ||
testFailure(v) | ||
} | ||
testFailure(Array.fill(10)(v)) | ||
} | ||
|
||
|
@@ -234,7 +290,11 @@ class SigmaBuilderTest extends AnyPropSpec with ScalaCheckPropertyChecks with Ma | |
property("liftToConstant AvlTreeData") { | ||
val v = TestData.t1.asInstanceOf[CAvlTree].wrappedValue | ||
val c = AvlTreeConstant(SigmaDsl.avlTree(v)) | ||
testSuccess(v, c) // TODO v6.0: AvlTreeData should not be liftable directly (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/905) | ||
if (!VersionContext.current.isV6SoftForkActivated) { | ||
testSuccess(v, c) | ||
} else { | ||
testFailure(v) | ||
} | ||
testFailure(Array.fill(10)(v)) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PreHeader is also supported type, they usually come together everywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed