Skip to content

Commit

Permalink
Merge pull request #1334 from wavesplatform/node-966-asset-id-in-sc-api
Browse files Browse the repository at this point in the history
NODE-966: AssetId field in transferTransaction
  • Loading branch information
ismagin authored Jul 24, 2018
2 parents b061dce + aa1f4c2 commit 9b9c23c
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 45 deletions.
19 changes: 13 additions & 6 deletions lang/jvm/src/test/scala/com/wavesplatform/lang/Common.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,23 @@ object Common {
val pointTypeA = CaseType("PointA", List("X" -> LONG, "YA" -> LONG))
val pointTypeB = CaseType("PointB", List("X" -> LONG, "YB" -> LONG))
val pointTypeC = CaseType("PointC", List("YB" -> LONG))
val pointTypeD = CaseType("PointD", List("YB" -> UNION(LONG, UNIT)))

val AorB = UNION(pointTypeA.typeRef, pointTypeB.typeRef)
val AorBorC = UNION(pointTypeA.typeRef, pointTypeB.typeRef, pointTypeC.typeRef)
val BorC = UNION(pointTypeB.typeRef, pointTypeC.typeRef)

val pointAInstance = CaseObj(pointTypeA.typeRef, Map("X" -> 3L, "YA" -> 40L))
val pointBInstance = CaseObj(pointTypeB.typeRef, Map("X" -> 3L, "YB" -> 41L))
val pointCInstance = CaseObj(pointTypeC.typeRef, Map("YB" -> 42L))

val sampleTypes = Seq(pointTypeA, pointTypeB, pointTypeC) ++ Seq(UnionType("PointAB", AorB.l), UnionType("PointBC", BorC.l))
val CorD = UNION(pointTypeC.typeRef, pointTypeD.typeRef)

val pointAInstance = CaseObj(pointTypeA.typeRef, Map("X" -> 3L, "YA" -> 40L))
val pointBInstance = CaseObj(pointTypeB.typeRef, Map("X" -> 3L, "YB" -> 41L))
val pointCInstance = CaseObj(pointTypeC.typeRef, Map("YB" -> 42L))
val pointDInstance1 = CaseObj(pointTypeD.typeRef, Map("YB" -> 43L))
private val unit: Unit = ()
val pointDInstance2 = CaseObj(pointTypeD.typeRef, Map("YB" -> unit))

val sampleTypes = Seq(pointTypeA, pointTypeB, pointTypeC, pointTypeD) ++ Seq(UnionType("PointAB", AorB.l),
UnionType("PointBC", BorC.l),
UnionType("PointCD", CorD.l))

def sampleUnionContext(instance: CaseObj) =
EvaluationContext.build(Map.empty, Map("p" -> LazyVal(EitherT.pure(instance))), Seq.empty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ class IntegrationTest extends PropSpec with PropertyChecks with ScriptGen with M
eval[Long](sampleScript, Some(pointBInstance)) shouldBe Right(1)
}

private def eval[T](code: String, pointInstance: Option[CaseObj] = None): Either[String, T] = {
private def eval[T](code: String, pointInstance: Option[CaseObj] = None, pointType: FINAL = AorBorC): Either[String, T] = {
val untyped = Parser(code).get.value
require(untyped.size == 1)
val lazyVal = LazyVal(EitherT.pure(pointInstance.orNull))
val stringToTuple: Map[String, (FINAL, LazyVal)] = Map(("p", (AorBorC, lazyVal)))
val stringToTuple: Map[String, (FINAL, LazyVal)] = Map(("p", (pointType, lazyVal)))
val ctx: CTX =
Monoid.combine(PureContext.ctx, CTX(sampleTypes, stringToTuple, Seq.empty))
val typed = CompilerV1(ctx.compilerContext, untyped.head)
Expand Down Expand Up @@ -230,4 +230,21 @@ class IntegrationTest extends PropSpec with PropertyChecks with ScriptGen with M
eval[Long](sampleScript, Some(pointBInstance)) shouldBe Right(3)
eval[Long](sampleScript, Some(pointCInstance)) shouldBe Right(42)
}

property("different types, same name of field") {
val sampleScript =
"""match (p.YB) {
| case l: Int => l
| case u: Unit => 1
| }
""".stripMargin
eval[Long](sampleScript, Some(pointCInstance), CorD) shouldBe Right(42)
eval[Long](sampleScript, Some(pointDInstance1), CorD) shouldBe Right(43)
eval[Long](sampleScript, Some(pointDInstance2), CorD) shouldBe Right(1)

eval[Long]("p.YB", Some(pointCInstance), CorD) shouldBe Right(42)
eval[Long]("p.YB", Some(pointDInstance1), CorD) shouldBe Right(43)
eval[Unit]("p.YB", Some(pointDInstance2), CorD) shouldBe Right(())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ object Bindings {
CaseObj(genesisTransactionType.typeRef, Map("amount" -> amount) ++ headerPart(h) + mapRecipient(recipient))
case Tx.Payment(p, amount, recipient) =>
CaseObj(paymentTransactionType.typeRef, Map("amount" -> amount) ++ provenTxPart(p) + mapRecipient(recipient))
case Tx.Transfer(p, feeAssetId, transferAssetId, amount, recipient, attachment) =>
case Tx.Transfer(p, feeAssetId, assetId, amount, recipient, attachment) =>
CaseObj(
transferTransactionType.typeRef,
Map(
"amount" -> amount,
"feeAssetId" -> fromOption(feeAssetId),
"transferAssetId" -> fromOption(transferAssetId),
"attachment" -> attachment
"amount" -> amount,
"feeAssetId" -> fromOption(feeAssetId),
"assetId" -> fromOption(assetId),
"attachment" -> attachment
) ++ provenTxPart(p) + mapRecipient(recipient)
)
case Issue(p, quantity, name, description, reissuable, decimals, scriptOpt) =>
Expand Down Expand Up @@ -139,10 +139,10 @@ object Bindings {
Map(
"transfers" -> transfers
.map(bv => CaseObj(transfer.typeRef, Map(mapRecipient(bv.recipient), "amount" -> bv.amount))),
"transferAssetId" -> fromOption(assetId),
"transferCount" -> transferCount,
"totalAmount" -> totalAmount,
"attachment" -> attachment
"assetId" -> fromOption(assetId),
"transferCount" -> transferCount,
"totalAmount" -> totalAmount,
"attachment" -> attachment
) ++ provenTxPart(p)
)
case SetScript(p, scriptOpt) =>
Expand All @@ -155,9 +155,7 @@ object Bindings {
case Data(p, data) =>
CaseObj(
dataTransactionType.typeRef,
Map("data" -> data.map(e =>
CaseObj(dataEntryType.typeRef, Map("key" -> e.key, "value" -> e.value)))
) ++ provenTxPart(p)
Map("data" -> data.map(e => CaseObj(dataEntryType.typeRef, Map("key" -> e.key, "value" -> e.value)))) ++ provenTxPart(p)
)
case Exchange(p, price, amount, buyMatcherFee, sellMatcherFee, buyOrder, sellOrder) =>
CaseObj(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ object Types {
val transferTransactionType = CaseType(
"TransferTransaction",
List(
"feeAssetId" -> optionByteVector,
"amount" -> LONG,
"transferAssetId" -> optionByteVector,
"recipient" -> addressOrAliasType,
"attachment" -> BYTEVECTOR
"feeAssetId" -> optionByteVector,
"amount" -> LONG,
"assetId" -> optionByteVector,
"recipient" -> addressOrAliasType,
"attachment" -> BYTEVECTOR
) ++ header ++ proven
)

Expand Down Expand Up @@ -157,12 +157,12 @@ object Types {
val massTransferTransactionType = CaseType(
"MassTransferTransaction",
List(
"feeAssetId" -> optionByteVector,
"transferAssetId" -> optionByteVector,
"totalAmount" -> LONG,
"transfers" -> listTransfers,
"transferCount" -> LONG,
"attachment" -> BYTEVECTOR
"feeAssetId" -> optionByteVector,
"assetId" -> optionByteVector,
"totalAmount" -> LONG,
"transfers" -> listTransfers,
"transferCount" -> LONG,
"attachment" -> BYTEVECTOR
) ++ header ++ proven
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ trait DataItem[T] {
}

object DataItem {
case class Lng(k: String, v: Long) extends DataItem[Long] { val key = k; val value = v }
case class Bool(k: String, v: Boolean) extends DataItem[Boolean] { val key = k; val value = v }
case class Lng(k: String, v: Long) extends DataItem[Long] { val key = k; val value = v }
case class Bool(k: String, v: Boolean) extends DataItem[Boolean] { val key = k; val value = v }
case class Bin(k: String, v: ByteVector) extends DataItem[ByteVector] { val key = k; val value = v }
case class Str(k: String, v: String) extends DataItem[String] { val key = k; val value = v }
case class Str(k: String, v: String) extends DataItem[String] { val key = k; val value = v }
}

case class Ord(id: ByteVector,
Expand All @@ -52,7 +52,7 @@ object Tx {
case class Payment(p: Proven, amount: Long, recipient: Recipient) extends Tx
case class Transfer(p: Proven,
feeAssetId: Option[ByteVector],
transferAssetId: Option[ByteVector],
assetId: Option[ByteVector],
amount: Long,
recipient: Recipient,
attachment: ByteVector)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ object RealTransactionWrapper {
Tx.Transfer(
proven(t),
feeAssetId = t.feeAssetId.map(a => ByteVector(a.arr)),
transferAssetId = t.assetId.map(a => ByteVector(a.arr)),
assetId = t.assetId.map(a => ByteVector(a.arr)),
amount = t.amount,
recipient = t.recipient,
attachment = ByteVector(t.attachment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CommonFunctionsTest extends PropSpec with PropertyChecks with Matchers wit
val result = runScript[ByteVector](
"""
|match tx {
| case ttx : TransferTransaction => extract(ttx.transferAssetId)
| case ttx : TransferTransaction => extract(ttx.assetId)
| case other => throw
| }
|""".stripMargin,
Expand All @@ -37,7 +37,7 @@ class CommonFunctionsTest extends PropSpec with PropertyChecks with Matchers wit
val result = runScript[Boolean](
"""
|match tx {
| case ttx : TransferTransaction => isDefined(ttx.transferAssetId)
| case ttx : TransferTransaction => isDefined(ttx.assetId)
| case other => throw
| }
|""".stripMargin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ class TransactionBindingsTest extends PropSpec with PropertyChecks with Matchers
| let feeAssetId = if (${t.feeAssetId.isDefined})
| then extract(t.feeAssetId) == base58'${t.feeAssetId.getOrElse(ByteStr.empty).base58}'
| else isDefined(t.feeAssetId) == false
| let transferAssetId = if (${t.assetId.isDefined})
| then extract(t.transferAssetId) == base58'${t.assetId.getOrElse(ByteStr.empty).base58}'
| else isDefined(t.transferAssetId) == false
| let assetId = if (${t.assetId.isDefined})
| then extract(t.assetId) == base58'${t.assetId.getOrElse(ByteStr.empty).base58}'
| else isDefined(t.assetId) == false
| let recipient = match (t.recipient) {
| case a: Address => a.bytes == base58'${t.recipient.cast[Address].map(_.bytes.base58).getOrElse("")}'
| case a: Alias => a.alias == ${Json.toJson(t.recipient.cast[Alias].map(_.name).getOrElse(""))}
| }
| let attachment = t.attachment == base58'${ByteStr(t.attachment).base58}'
| $assertProvenPart && amount && feeAssetId && transferAssetId && recipient && attachment
| $assertProvenPart && amount && feeAssetId && assetId && recipient && attachment
| case other => throw
| }
|""".stripMargin,
Expand Down Expand Up @@ -294,10 +294,10 @@ class TransactionBindingsTest extends PropSpec with PropertyChecks with Matchers
val script = s"""
|match tx {
| case t : MassTransferTransaction =>
| let assetId = if (${t.assetId.isDefined}) then extract(t.transferAssetId) == base58'${t.assetId
| let assetId = if (${t.assetId.isDefined}) then extract(t.assetId) == base58'${t.assetId
.getOrElse(ByteStr.empty)
.base58}'
| else isDefined(t.transferAssetId) == false
| else isDefined(t.assetId) == false
| let transferCount = t.transferCount == ${t.transfers.length}
| let totalAmount = t.totalAmount == ${t.transfers.map(_.amount).sum}
| let attachment = t.attachment == base58'${ByteStr(t.attachment).base58}'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TransactionFieldAccessTest extends PropSpec with PropertyChecks with Match
|
| match tx {
| case ttx: TransferTransaction =>
| isDefined(ttx.transferAssetId)==false
| isDefined(ttx.assetId)==false
| case other =>
| false
| }
Expand Down

0 comments on commit 9b9c23c

Please sign in to comment.