Skip to content

Commit 2fb971c

Browse files
committed
put tree version into Ergo tree in /script/p2s
1 parent 21ca898 commit 2fb971c

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

src/main/scala/org/ergoplatform/http/api/ScriptApiRoute.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ case class ScriptApiRoute(readersHolder: ActorRef, ergoSettings: ErgoSettings)
5353
keys.zipWithIndex.map { case (pk, i) => s"myPubKey_$i" -> pk }.toMap
5454
}
5555

56-
private def compileSource(source: String, env: Map[String, Any], treeVersion: Byte = 0): Try[ErgoTree] = {
56+
private def compileSource(source: String, env: Map[String, Any], treeVersion: Byte): Try[ErgoTree] = {
5757
val compiler = new SigmaCompiler(ergoSettings.chainSettings.addressPrefix)
58-
val ergoTreeHeader = ErgoTree.defaultHeaderWithVersion(treeVersion.toByte)
58+
val ergoTreeHeader = ErgoTree.defaultHeaderWithVersion(treeVersion)
5959
Try(compiler.compile(env, source)(new CompiletimeIRContext)).flatMap {
6060
case CompilerResult(_, _, _, script: Value[SSigmaProp.type@unchecked]) if script.tpe == SSigmaProp =>
6161
Success(ErgoTree.fromProposition(ergoTreeHeader, script))
@@ -77,7 +77,7 @@ case class ScriptApiRoute(readersHolder: ActorRef, ergoSettings: ErgoSettings)
7777
val scriptVersion = Header.scriptFromBlockVersion(bv)
7878
val treeVersion = compileRequest.treeVersion
7979
VersionContext.withVersions(scriptVersion, treeVersion) {
80-
compileSource(compileRequest.source, keysToEnv(addrs.map(_.pubkey))).map(Pay2SAddress.apply).fold(
80+
compileSource(compileRequest.source, keysToEnv(addrs.map(_.pubkey)), treeVersion).map(Pay2SAddress.apply).fold(
8181
e => BadRequest(e.getMessage),
8282
address => ApiResponse(addressResponse(address))
8383
)
@@ -93,7 +93,7 @@ case class ScriptApiRoute(readersHolder: ActorRef, ergoSettings: ErgoSettings)
9393
val scriptVersion = Header.scriptFromBlockVersion(bv)
9494
val treeVersion = compileRequest.treeVersion
9595
VersionContext.withVersions(scriptVersion, treeVersion) {
96-
compileSource(compileRequest.source, keysToEnv(addrs.map(_.pubkey))).map(Pay2SHAddress.apply).fold(
96+
compileSource(compileRequest.source, keysToEnv(addrs.map(_.pubkey)), treeVersion).map(Pay2SHAddress.apply).fold(
9797
e => BadRequest(e.getMessage),
9898
address => ApiResponse(addressResponse(address))
9999
)

src/test/scala/org/ergoplatform/http/routes/ScriptApiRouteSpec.scala

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,64 @@ class ScriptApiRouteSpec extends AnyFlatSpec
148148
Get(s"$prefix/$suffix/$p2s") ~> route ~> check(assertion(responseAs[Json], p2s))
149149
}
150150

151+
it should "generate addresses with different tree versions" in {
152+
val p2sSuffix = "/p2sAddress"
153+
val p2shSuffix = "/p2shAddress"
154+
155+
var p2sAddressV0: String = ""
156+
var p2shAddressV0: String = ""
157+
var p2sAddressV1: String = ""
158+
var p2shAddressV1: String = ""
159+
160+
// Test with tree version 0
161+
Post(prefix + p2sSuffix, Json.obj("source" -> scriptSource.asJson, "treeVersion" -> 0.asJson)) ~> route ~> check {
162+
status shouldBe StatusCodes.OK
163+
val addressStr = responseAs[Json].hcursor.downField("address").as[String].right.get
164+
addressEncoder.fromString(addressStr).get.addressTypePrefix shouldEqual Pay2SAddress.addressTypePrefix
165+
p2sAddressV0 = addressStr
166+
}
167+
168+
Post(prefix + p2shSuffix, Json.obj("source" -> scriptSource.asJson, "treeVersion" -> 0.asJson)) ~> route ~> check {
169+
status shouldBe StatusCodes.OK
170+
val addressStr = responseAs[Json].hcursor.downField("address").as[String].right.get
171+
addressEncoder.fromString(addressStr).get.addressTypePrefix shouldEqual Pay2SHAddress.addressTypePrefix
172+
p2shAddressV0 = addressStr
173+
}
174+
175+
// Test with tree version 1
176+
Post(prefix + p2sSuffix, Json.obj("source" -> scriptSource.asJson, "treeVersion" -> 1.asJson)) ~> route ~> check {
177+
status shouldBe StatusCodes.OK
178+
val addressStr = responseAs[Json].hcursor.downField("address").as[String].right.get
179+
addressEncoder.fromString(addressStr).get.addressTypePrefix shouldEqual Pay2SAddress.addressTypePrefix
180+
p2sAddressV1 = addressStr
181+
}
182+
183+
Post(prefix + p2shSuffix, Json.obj("source" -> scriptSource.asJson, "treeVersion" -> 1.asJson)) ~> route ~> check {
184+
status shouldBe StatusCodes.OK
185+
val addressStr = responseAs[Json].hcursor.downField("address").as[String].right.get
186+
addressEncoder.fromString(addressStr).get.addressTypePrefix shouldEqual Pay2SHAddress.addressTypePrefix
187+
p2shAddressV1 = addressStr
188+
}
189+
190+
// Get the actual Ergo trees and verify they have different version bytes
191+
val p2sTreeV0 = addressEncoder.fromString(p2sAddressV0).get.script
192+
val p2sTreeV1 = addressEncoder.fromString(p2sAddressV1).get.script
193+
val p2shTreeV0 = addressEncoder.fromString(p2shAddressV0).get.script
194+
val p2shTreeV1 = addressEncoder.fromString(p2shAddressV1).get.script
195+
196+
// Check that the trees have different version bytes
197+
p2sTreeV0.bytes should not equal p2sTreeV1.bytes
198+
p2shTreeV0.bytes shouldBe p2shTreeV1.bytes
199+
200+
// Specifically check the version byte (first byte of ErgoTree)
201+
p2sTreeV0.bytes.head should not equal p2sTreeV1.bytes.head
202+
p2shTreeV0.bytes.head shouldBe p2shTreeV1.bytes.head
203+
204+
// Verify the actual version bytes match what we requested
205+
p2sTreeV0.bytes.head shouldEqual 16
206+
p2sTreeV1.bytes.head shouldEqual 25
207+
p2shTreeV0.bytes.head shouldEqual 0
208+
p2shTreeV1.bytes.head shouldEqual 0
209+
}
210+
151211
}

0 commit comments

Comments
 (0)