Skip to content

Commit

Permalink
Use single party submit/submitTree where appropriate (#8995)
Browse files Browse the repository at this point in the history
* Move [] into submit

* Use submit instead of submitMulti for single party

changelog_begin
changelog_end

* Test multi-party submission

* Factor out construction of submit command

Co-authored-by: Andreas Herrmann <andreas.herrmann@tweag.io>
  • Loading branch information
aherrmann-da and aherrmann committed Mar 3, 2021
1 parent 145ddaa commit 926949e
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 29 deletions.
17 changes: 12 additions & 5 deletions daml-script/dump/src/main/scala/com/daml/script/dump/Encode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ private[dump] object Encode {
val encodedCids = referencedCids.map(encodeCid(cidMap, _))
(tuple(encodedCids) :+ " <- ", "pure " +: tuple(encodedCids))
}
val submit = "submitMulti " +: encodeParties(partyMap, submitters)
val actions = Doc.stack(evs.map { ev =>
val cid = ContractId(ev.contractId)
val bind = if (returnStmt.nonEmpty) {
Expand All @@ -284,7 +283,7 @@ private[dump] object Encode {
bind + encodeCreatedEvent(partyMap, cidMap, ev)
})
val body = Doc.stack(Seq(actions, returnStmt).filter(d => d.nonEmpty))
((bind + submit :+ " [] do") / body).hang(2)
((bind + submit(partyMap, submitters, isTree = false) :+ " do") / body).hang(2)
}

private[dump] def encodeACS(
Expand Down Expand Up @@ -324,9 +323,8 @@ private[dump] object Encode {
val cids = treeCreatedCids(tree)
val referencedCids = cids.filter(c => cidRefs.contains(c.cid))
val treeBind =
(Doc.text("tree <- submitTreeMulti ") + encodeParties(partyMap, submitters) + Doc.text(
" [] do"
) / Doc.stack(rootEvs.map(ev => encodeEv(partyMap, cidMap, ev)))).hang(2)
(("tree <- " +: submit(partyMap, submitters, isTree = true) :+ " do") /
Doc.stack(rootEvs.map(ev => encodeEv(partyMap, cidMap, ev)))).hang(2)
val cidBinds = referencedCids.map(bindCid(cidMap, _))
Doc.stack(treeBind +: cidBinds)
}
Expand All @@ -335,6 +333,15 @@ private[dump] object Encode {

private def encodeSelector(selector: Selector): Doc = Doc.str(selector.i)

private def submit(partyMap: Map[Party, String], submitters: Set[Party], isTree: Boolean): Doc = {
val tree = if (isTree) { Doc.text("Tree") }
else { Doc.empty }
submitters.toList match {
case ::(submitter, Nil) => "submit" +: tree & encodeParty(partyMap, submitter)
case _ => "submit" +: tree :+ "Multi" & encodeParties(partyMap, submitters) :+ " []"
}
}

private def encodeImport(moduleName: String) =
Doc.text("import qualified ") + Doc.text(moduleName)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@ class EncodeAcsSpec extends AnyFreeSpec with Matchers {
.toCreatedEvents
"batch size 1" in {
encodeACS(parties, cidMap, cidRefs, events, 1).render(80) shouldBe
"""_ <- submitMulti [alice_0] [] do
"""_ <- submit alice_0 do
| createCmd Module.Template
|_ <- submitMulti [alice_0] [] do
|_ <- submit alice_0 do
| createCmd Module.Template
|_ <- submitMulti [alice_0] [] do
|_ <- submit alice_0 do
| createCmd Module.Template
|_ <- submitMulti [alice_0] [] do
|_ <- submit alice_0 do
| createCmd Module.Template""".stripMargin.replace(
"\r\n",
"\n",
)
}
"batch size 2 - divides evenly" in {
encodeACS(parties, cidMap, cidRefs, events, 2).render(80) shouldBe
"""submitMulti [alice_0] [] do
"""submit alice_0 do
| _ <- createCmd Module.Template
| _ <- createCmd Module.Template
| pure ()
|submitMulti [alice_0] [] do
|submit alice_0 do
| _ <- createCmd Module.Template
| _ <- createCmd Module.Template
| pure ()""".stripMargin.replace(
Expand All @@ -59,20 +59,20 @@ class EncodeAcsSpec extends AnyFreeSpec with Matchers {
}
"batch size 3 - does not divide evenly" in {
encodeACS(parties, cidMap, cidRefs, events, 3).render(80) shouldBe
"""submitMulti [alice_0] [] do
"""submit alice_0 do
| _ <- createCmd Module.Template
| _ <- createCmd Module.Template
| _ <- createCmd Module.Template
| pure ()
|_ <- submitMulti [alice_0] [] do
|_ <- submit alice_0 do
| createCmd Module.Template""".stripMargin.replace(
"\r\n",
"\n",
)
}
"batch size 4 - equals total size" in {
encodeACS(parties, cidMap, cidRefs, events, 4).render(80) shouldBe
"""submitMulti [alice_0] [] do
"""submit alice_0 do
| _ <- createCmd Module.Template
| _ <- createCmd Module.Template
| _ <- createCmd Module.Template
Expand All @@ -84,7 +84,7 @@ class EncodeAcsSpec extends AnyFreeSpec with Matchers {
}
"batch size 5 - greater than total size" in {
encodeACS(parties, cidMap, cidRefs, events, 5).render(80) shouldBe
"""submitMulti [alice_0] [] do
"""submit alice_0 do
| _ <- createCmd Module.Template
| _ <- createCmd Module.Template
| _ <- createCmd Module.Template
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,33 @@ import org.scalatest.matchers.should.Matchers
class EncodeCreatedSpec extends AnyFreeSpec with Matchers {
import Encode._
"encodeCreatedEvent" - {
"multi party submissions" in {
val parties = Map(
Party("Alice") -> "alice_0",
Party("Bob") -> "bob_0",
)
val cidMap = Map(
ContractId("cid1") -> "contract_0_0",
ContractId("cid2") -> "contract_0_1",
)
val cidRefs = Set.empty[ContractId]
val events = TestData
.ACS(
Seq(
TestData.Created(ContractId("cid1"), submitters = Seq(Party("Alice"))),
TestData.Created(ContractId("cid2"), submitters = Seq(Party("Bob"))),
)
)
.toCreatedEvents
encodeSubmitCreatedEvents(parties, cidMap, cidRefs, events).render(80) shouldBe
"""submitMulti [alice_0, bob_0] [] do
| _ <- createCmd Module.Template
| _ <- createCmd Module.Template
| pure ()""".stripMargin.replace(
"\r\n",
"\n",
)
}
"contract id bindings" - {
"unreferenced" in {
val parties = Map(Party("Alice") -> "alice_0")
Expand All @@ -23,7 +50,7 @@ class EncodeCreatedSpec extends AnyFreeSpec with Matchers {
)
.toCreatedEvents
encodeSubmitCreatedEvents(parties, cidMap, cidRefs, events).render(80) shouldBe
"""_ <- submitMulti [alice_0] [] do
"""_ <- submit alice_0 do
| createCmd Module.Template""".stripMargin.replace("\r\n", "\n")
}
"referenced" in {
Expand All @@ -38,7 +65,7 @@ class EncodeCreatedSpec extends AnyFreeSpec with Matchers {
)
.toCreatedEvents
encodeSubmitCreatedEvents(parties, cidMap, cidRefs, events).render(80) shouldBe
"""contract_0_0 <- submitMulti [alice_0] [] do
"""contract_0_0 <- submit alice_0 do
| createCmd Module.Template""".stripMargin.replace(
"\r\n",
"\n",
Expand All @@ -62,7 +89,7 @@ class EncodeCreatedSpec extends AnyFreeSpec with Matchers {
)
.toCreatedEvents
encodeSubmitCreatedEvents(parties, cidMap, cidRefs, events).render(80) shouldBe
"""submitMulti [alice_0] [] do
"""submit alice_0 do
| _ <- createCmd Module.Template
| _ <- createCmd Module.Template
| _ <- createCmd Module.Template
Expand All @@ -89,7 +116,7 @@ class EncodeCreatedSpec extends AnyFreeSpec with Matchers {
)
.toCreatedEvents
encodeSubmitCreatedEvents(parties, cidMap, cidRefs, events).render(80) shouldBe
"""(contract_0_0, contract_0_2) <- submitMulti [alice_0] [] do
"""(contract_0_0, contract_0_2) <- submit alice_0 do
| contract_0_0 <- createCmd Module.Template
| _ <- createCmd Module.Template
| contract_0_2 <- createCmd Module.Template
Expand All @@ -114,7 +141,7 @@ class EncodeCreatedSpec extends AnyFreeSpec with Matchers {
)
.toCreatedEvents
encodeSubmitCreatedEvents(parties, cidMap, cidRefs, events).render(80) shouldBe
"""contract_0_1 <- submitMulti [alice_0] [] do
"""contract_0_1 <- submit alice_0 do
| _ <- createCmd Module.Template
| contract_0_1 <- createCmd Module.Template
| pure contract_0_1""".stripMargin.replace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@ import org.scalatest.matchers.should.Matchers
class EncodeTreeSpec extends AnyFreeSpec with Matchers {
import Encode._
"encodeTree" - {
"multi-party submissions" in {
val parties = Map(
Party("Alice") -> "alice_0",
Party("Bob") -> "bob_0",
)
val cidMap = Map(
ContractId("cid1") -> "contract_0_0",
ContractId("cid2") -> "contract_0_1",
)
val cidRefs = Set.empty[ContractId]
val tree = TestData
.Tree(
Seq[TestData.Event](
TestData.Created(ContractId("cid1"), submitters = Seq(Party("Alice"))),
TestData.Exercised(ContractId("cid2"), Seq.empty, actingParties = Seq(Party("Bob"))),
)
)
.toTransactionTree
encodeTree(parties, cidMap, cidRefs, tree).render(80) shouldBe
"""tree <- submitTreeMulti [alice_0, bob_0] [] do
| createCmd Module.Template
| exerciseCmd contract_0_1 (Module.Choice ())""".stripMargin.replace("\r\n", "\n")
}
"contract id bindings" - {
"unreferenced create" in {
val parties = Map(Party("Alice") -> "alice_0")
Expand All @@ -23,7 +46,7 @@ class EncodeTreeSpec extends AnyFreeSpec with Matchers {
)
.toTransactionTree
encodeTree(parties, cidMap, cidRefs, tree).render(80) shouldBe
"""_ <- submitMulti [alice_0] [] do
"""_ <- submit alice_0 do
| createCmd Module.Template""".stripMargin.replace("\r\n", "\n")
}
"unreferenced creates" in {
Expand All @@ -42,7 +65,7 @@ class EncodeTreeSpec extends AnyFreeSpec with Matchers {
)
.toTransactionTree
encodeTree(parties, cidMap, cidRefs, tree).render(80) shouldBe
"""submitMulti [alice_0] [] do
"""submit alice_0 do
| _ <- createCmd Module.Template
| _ <- createCmd Module.Template
| pure ()""".stripMargin.replace("\r\n", "\n")
Expand All @@ -68,7 +91,7 @@ class EncodeTreeSpec extends AnyFreeSpec with Matchers {
)
.toTransactionTree
encodeTree(parties, cidMap, cidRefs, tree).render(80) shouldBe
"""tree <- submitTreeMulti [alice_0] [] do
"""tree <- submitTree alice_0 do
| exerciseCmd contract_0_0 (Module.Choice ())""".stripMargin.replace("\r\n", "\n")
}
"referenced create" in {
Expand All @@ -83,7 +106,7 @@ class EncodeTreeSpec extends AnyFreeSpec with Matchers {
)
.toTransactionTree
encodeTree(parties, cidMap, cidRefs, tree).render(80) shouldBe
"""contract_0_0 <- submitMulti [alice_0] [] do
"""contract_0_0 <- submit alice_0 do
| createCmd Module.Template""".stripMargin.replace(
"\r\n",
"\n",
Expand All @@ -105,7 +128,7 @@ class EncodeTreeSpec extends AnyFreeSpec with Matchers {
)
.toTransactionTree
encodeTree(parties, cidMap, cidRefs, tree).render(80) shouldBe
"""(contract_1_0, contract_1_1) <- submitMulti [alice_0] [] do
"""(contract_1_0, contract_1_1) <- submit alice_0 do
| contract_1_0 <- createCmd Module.Template
| contract_1_1 <- createCmd Module.Template
| pure (contract_1_0, contract_1_1)""".stripMargin.replace(
Expand Down Expand Up @@ -135,7 +158,7 @@ class EncodeTreeSpec extends AnyFreeSpec with Matchers {
)
.toTransactionTree
encodeTree(parties, cidMap, cidRefs, tree).render(80) shouldBe
"""tree <- submitTreeMulti [alice_0] [] do
"""tree <- submitTree alice_0 do
| exerciseCmd contract_0_0 (Module.Choice ())
|let contract_1_0 = createdCid @Module.Template [0, 0] tree
|let contract_1_1 = createdCid @Module.Template [0, 1] tree""".stripMargin.replace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ object TestData {
sealed case class Created(
contractId: ContractId,
createArguments: Seq[RecordField] = Seq.empty,
submitters: Seq[Party] = defaultParties,
) extends Event {
def toCreatedEvent(eventId: String): CreatedEvent = {
CreatedEvent(
eventId = eventId,
templateId = Some(defaultTemplateId),
contractId = ContractId.unwrap(contractId),
signatories = Party.unsubst(defaultParties),
signatories = Party.unsubst(submitters),
createArguments = Some(Record(recordId = Some(defaultTemplateId), fields = createArguments)),
)
}
Expand All @@ -39,6 +40,7 @@ object TestData {
contractId: ContractId,
childEvents: Seq[Event],
choiceArgument: Value = defaultChoiceArgument,
actingParties: Seq[Party] = defaultParties,
) extends Event

sealed case class ACS(contracts: Seq[Created]) {
Expand All @@ -63,7 +65,7 @@ object TestData {
case event: Created =>
val treeEvent = TreeEvent(TreeEvent.Kind.Created(event.toCreatedEvent(eventId)))
(rootEventIds :+ eventId, eventsById + (eventId -> treeEvent))
case Exercised(contractId, childEvents, choiceArgument) =>
case Exercised(contractId, childEvents, choiceArgument, actingParties) =>
val (childEventIds, childEventsById) =
childEvents.foldLeft((Seq.empty[String], Map.empty[String, TreeEvent]))(go)
val treeEvent = TreeEvent(
Expand All @@ -72,7 +74,7 @@ object TestData {
eventId = eventId,
templateId = Some(defaultTemplateId),
contractId = ContractId.unwrap(contractId),
actingParties = Party.unsubst(defaultParties),
actingParties = Party.unsubst(actingParties),
choice = "Choice",
choiceArgument = Some(choiceArgument),
childEventIds = childEventIds,
Expand Down

0 comments on commit 926949e

Please sign in to comment.