-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core-serializers2: moved CAnyValue to core
- Loading branch information
1 parent
094beeb
commit 3f85433
Showing
13 changed files
with
127 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package sigma.data | ||
|
||
import sigma.AnyValue | ||
import sigma.data.OverloadHack.Overloaded1 | ||
|
||
import scala.annotation.unused | ||
|
||
/** Default implementation of AnyValue interface. */ | ||
case class CAnyValue[A](value: A, tVal: RType[Any]) extends AnyValue { | ||
def tA: RType[A] = tVal.asInstanceOf[RType[A]] | ||
|
||
override def toString = s"TestValue($value)" | ||
} | ||
|
||
object CAnyValue { | ||
def apply[A](value: A)(implicit t: RType[A], @unused o: Overloaded1): CAnyValue[A] = | ||
new CAnyValue(value, t.asInstanceOf[RType[Any]]) | ||
} |
95 changes: 95 additions & 0 deletions
95
interpreter/shared/src/main/scala/sigmastate/eval/CBox.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package sigmastate.eval | ||
|
||
import org.ergoplatform.ErgoBox | ||
import scorex.utils.Ints | ||
import sigma.Evaluation.stypeToRType | ||
import sigma.ast.SCollection.SByteArray | ||
import sigma.ast.{SInt, STuple, SType} | ||
import sigma.data.{CAnyValue, RType, WrapperOf} | ||
import sigma.{AnyValue, Box, Coll, Colls} | ||
import sigmastate.Values.{ConstantNode, EvaluatedValue, SValue} | ||
import sigmastate.eval.CBox.regs | ||
import sigmastate.eval.Extensions.toAnyValue | ||
|
||
import java.util.Arrays | ||
|
||
/** A default implementation of [[Box]] interface. | ||
* | ||
* @see [[Box]] for detailed descriptions | ||
*/ | ||
case class CBox(ebox: ErgoBox) extends Box with WrapperOf[ErgoBox] { | ||
val builder = CSigmaDslBuilder | ||
|
||
val value = ebox.value | ||
lazy val id : Coll[Byte] = Colls.fromArray(ebox.id) | ||
lazy val bytes : Coll[Byte] = Colls.fromArray(ebox.bytes) | ||
lazy val bytesWithoutRef : Coll[Byte] = Colls.fromArray(ebox.bytesWithNoRef) | ||
lazy val propositionBytes: Coll[Byte] = Colls.fromArray(ebox.propositionBytes) | ||
lazy val registers : Coll[AnyValue] = regs(ebox) | ||
|
||
override def wrappedValue: ErgoBox = ebox | ||
|
||
override def getReg[T](i: Int)(implicit tT: RType[T]): Option[T] = { | ||
if (i < 0 || i >= registers.length) return None | ||
val value = registers(i) | ||
if (value != null) { | ||
// once the value is not null it should be of the right type | ||
value match { | ||
case value: CAnyValue[_] if value.value != null && value.tA == tT => | ||
Some(value.value.asInstanceOf[T]) | ||
case _ => | ||
throw new InvalidType(s"Cannot getReg[${tT.name}]($i): invalid type of value $value at id=$i") | ||
} | ||
} else None | ||
} | ||
|
||
override def creationInfo: (Int, Coll[Byte]) = { | ||
this.getReg[(Int, Coll[Byte])](3).get.asInstanceOf[Any] match { | ||
case info: Tuple2[Int, Coll[Byte]]@unchecked => info | ||
case ConstantNode(arr: Array[Any], STuple(IndexedSeq(SInt, SByteArray))) if arr.length == 2 => | ||
(arr(0).asInstanceOf[Int], builder.Colls.fromArray(arr(1).asInstanceOf[Array[Byte]])) | ||
case v => | ||
sys.error(s"Invalid value $v of creationInfo register R3") | ||
} | ||
} | ||
|
||
override def tokens: Coll[(Coll[Byte], Long)] = { | ||
this.getReg[Coll[(Coll[Byte], Long)]](ErgoBox.R2.asIndex).get | ||
} | ||
|
||
override def executeFromRegister[T](regId: Byte) | ||
(implicit cT: RType[T]): T = ??? // TODO implement | ||
|
||
override def hashCode(): Int = Ints.fromByteArray(id.toArray) | ||
|
||
override def equals(obj: Any): Boolean = (this eq obj.asInstanceOf[AnyRef]) || (obj != null && { | ||
obj match { | ||
case obj: Box => Arrays.equals(id.toArray, obj.id.toArray) | ||
case _ => | ||
// this case was missing in v4.x, however has never been a problem | ||
// Thus, v5.0 interpreter will not fail (while v4.x would fail here) | ||
false | ||
} | ||
}) | ||
} | ||
|
||
object CBox { | ||
def regs(ebox: ErgoBox): Coll[AnyValue] = { | ||
val res = new Array[AnyValue](ErgoBox.maxRegisters) | ||
|
||
def checkNotYetDefined(id: Int, newValue: SValue) = | ||
require(res(id) == null, s"register $id is defined more then once: previous value ${res(id)}, new value $newValue") | ||
|
||
for ( (k, v: EvaluatedValue[t]) <- ebox.additionalRegisters ) { | ||
checkNotYetDefined(k.number, v) | ||
res(k.number) = toAnyValue(v.value)(stypeToRType(v.tpe)) | ||
} | ||
for ( r <- ErgoBox.mandatoryRegisters ) { | ||
val regId = r.number | ||
val v = ebox.get(r).get.asInstanceOf[EvaluatedValue[SType]] | ||
checkNotYetDefined(regId, v) | ||
res(regId) = toAnyValue(v.value)(stypeToRType(v.tpe)) | ||
} | ||
Colls.fromArray(res) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
sc/shared/src/test/scala/sigmastate/eval/ExampleContracts.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
sc/shared/src/test/scala/sigmastate/utxo/SigmaCompilerSpecification.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters