Skip to content

Commit

Permalink
tree-template: implemented fromHex, templateHex
Browse files Browse the repository at this point in the history
  • Loading branch information
aslesarenko committed Jul 26, 2023
1 parent 7550d6b commit 4d96fdc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
27 changes: 25 additions & 2 deletions interpreter/shared/src/main/scala/sigmastate/Values.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ import sigmastate.serialization.OpCodes._
import sigmastate.TrivialProp.{FalseProp, TrueProp}
import sigmastate.Values.ErgoTree.substConstants
import sigmastate.basics.DLogProtocol.ProveDlog
import sigmastate.basics.{ProveDHTuple, CryptoConstants}
import sigmastate.basics.{CryptoConstants, ProveDHTuple}
import sigmastate.lang.Terms._
import sigmastate.utxo._
import sigmastate.eval._
import sigmastate.eval.Extensions._
import scalan.util.Extensions.ByteOps
import sigmastate.interpreter.ErgoTreeEvaluator._
import debox.cfor
import scorex.util.encode.Base16
import sigmastate.exceptions.InterpreterException

import scala.language.implicitConversions
import scala.reflect.ClassTag
import sigmastate.lang.CheckingSigmaBuilder._
Expand Down Expand Up @@ -1314,13 +1316,16 @@ object Values {
}

/** Serialized proposition expression of SigmaProp type with
* ConstantPlaceholder nodes instead of Constant nodes
* ConstantPlaceholder nodes not replaced by Constant nodes.
*/
lazy val template: Array[Byte] = {
val r = SigmaSerializer.startReader(bytes)
DefaultSerializer.deserializeHeaderWithTreeBytes(r)._4
}

/** Base16 encoding of `template` bytes. */
def templateHex: String = Base16.encode(template)

/** Get proposition expression from this contract.
* When root.isRight then
* if replaceConstants == false this is the same as `root.right.get`.
Expand Down Expand Up @@ -1496,6 +1501,24 @@ object Values {
*/
def withSegregation(prop: SigmaPropValue): ErgoTree =
withSegregation(DefaultHeader, prop)

/** Deserializes an ErgoTree instance from a hexadecimal string.
*
* @param hex a hexadecimal string representing the serialized ErgoTree
*/
def fromHex(hex: String): ErgoTree = {
val bytes = Base16.decode(hex).get
fromBytes(bytes)
}

/** Deserializes an ErgoTree instance from an array of bytes.
*
* @param bytes an array of bytes representing the serialized ErgoTree
*/
def fromBytes(bytes: Array[Byte]): ErgoTree = {
ErgoTreeSerializer.DefaultSerializer.deserializeErgoTree(bytes)
}

}

}
19 changes: 13 additions & 6 deletions sc/shared/src/test/scala/sigmastate/ErgoTreeSpecification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,19 @@ class ErgoTreeSpecification extends SigmaDslTesting with ContractsTestkit {
}

property("ErgoTree.template") {
val t = new ErgoTree(
16.toByte,
Array(IntConstant(1)),
Right(BoolToSigmaProp(EQ(ConstantPlaceholder(0, SInt), IntConstant(1))))
)
t.template shouldBe ErgoAlgos.decodeUnsafe("d19373000402")
{
val t = new ErgoTree(
16.toByte,
Array(IntConstant(1)),
Right(BoolToSigmaProp(EQ(ConstantPlaceholder(0, SInt), IntConstant(1))))
)
t.template shouldBe ErgoAlgos.decodeUnsafe("d19373000402")
}

{
val t = ErgoTree.fromHex("100604000e000400040005000500d803d601e30004d602e4c6a70408d603e4c6a7050595e67201d804d604b2a5e4720100d605b2db63087204730000d606db6308a7d60799c1a7c17204d1968302019683050193c27204c2a7938c720501730193e4c672040408720293e4c672040505720393e4c67204060ec5a796830201929c998c7205029591b1720673028cb272067303000273047203720792720773057202")
t.templateHex shouldBe "d803d601e30004d602e4c6a70408d603e4c6a7050595e67201d804d604b2a5e4720100d605b2db63087204730000d606db6308a7d60799c1a7c17204d1968302019683050193c27204c2a7938c720501730193e4c672040408720293e4c672040505720393e4c67204060ec5a796830201929c998c7205029591b1720673028cb272067303000273047203720792720773057202"
}
}

property("ErgoTree.bytes") {
Expand Down
17 changes: 11 additions & 6 deletions sdk/js/src/main/scala/org/ergoplatform/sdk/js/ErgoTree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ class ErgoTree(tree: Values.ErgoTree) extends js.Object {
/** Serializes the ErgoTree instance to a hexadecimal string. */
def toHex(): String = tree.bytesHex

/** Serialized proposition expression of SigmaProp type with
* ConstantPlaceholder nodes not replaced by Constant nodes.
*/
def template(): Array[Byte] = tree.template

/** Base16 encoding of `template` bytes. */
def templateHex(): String = tree.templateHex

/** Returns segregated constants of this tree as [[js.Array]]. */
def constants(): js.Array[Value] = {
val constants = tree.constants
Expand All @@ -42,17 +50,14 @@ object ErgoTree extends js.Object {
*
* @param hex a hexadecimal string representing the serialized ErgoTree
*/
def fromHex(hex: String): ErgoTree = {
val bytes = Base16.decode(hex).get
fromBytes(bytes)
}
def fromHex(hex: String): ErgoTree =
new ErgoTree(Values.ErgoTree.fromHex(hex))

/** Deserializes an ErgoTree instance from an array of bytes.
*
* @param bytes an array of bytes representing the serialized ErgoTree
*/
def fromBytes(bytes: Array[Byte]): ErgoTree = {
val tree = ErgoTreeSerializer.DefaultSerializer.deserializeErgoTree(bytes)
new ErgoTree(tree)
new ErgoTree(Values.ErgoTree.fromBytes(bytes))
}
}
6 changes: 6 additions & 0 deletions sigma-js/tests/js/ErgoTree.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,11 @@ describe("Smoke tests for ErgoTree", () => {
let constants = tree.constants().map(c => c.toHex())
expect(constants).toEqual(["0400", "0e00", "0400", "0400", "0500", "0500"])
});

it("Has template", () => {
let tree = ErgoTreeObj.fromHex(hex);
let templateHex = tree.templateHex();
expect(templateHex).toEqual("d803d601e30004d602e4c6a70408d603e4c6a7050595e67201d804d604b2a5e4720100d605b2db63087204730000d606db6308a7d60799c1a7c17204d1968302019683050193c27204c2a7938c720501730193e4c672040408720293e4c672040505720393e4c67204060ec5a796830201929c998c7205029591b1720673028cb272067303000273047203720792720773057202")
});
});

0 comments on commit 4d96fdc

Please sign in to comment.