Skip to content

Commit

Permalink
ledger-api-client: Generate a submission id if it's empty in the `Com…
Browse files Browse the repository at this point in the history
…mandClient` [KVL-1104] (#10926)

* Throw an exception if the submission id is empty in `CommandTracker`

CHANGELOG_BEGIN
CHANGELOG_END

* Generate a submission ID in the `CommandClient` if it's empty

CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
hubert-da authored Sep 20, 2021
1 parent 04d8f75 commit fc2c87d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ private[commands] class CommandTracker[Context](
val submissionId = commands.submissionId
val commandId = commands.commandId
logger.trace(s"Begin tracking of command $commandId for submission $submissionId.")
if (commands.submissionId.isEmpty) {
throw new IllegalArgumentException(
s"The submission id for the command $commandId is empty. This should not happen."
)
}
if (pendingCommands.contains(TrackedCommandKey(submissionId, commandId))) {
// TODO return an error identical to the server side duplicate command error once that's defined.
throw new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import akka.stream.Materializer
import akka.stream.scaladsl.{Flow, Keep, Sink, Source}
import com.codahale.metrics.Counter
import com.daml.grpc.adapter.ExecutionSequencerFactory
import com.daml.ledger.api.SubmissionIdGenerator
import com.daml.ledger.api.domain.LedgerId
import com.daml.ledger.api.v1.command_completion_service.CommandCompletionServiceGrpc.CommandCompletionServiceStub
import com.daml.ledger.api.v1.command_completion_service.{
Expand Down Expand Up @@ -64,6 +65,8 @@ private[daml] final class CommandClient(
],
]

private val submissionIdGenerator: SubmissionIdGenerator = SubmissionIdGenerator.Random

/** Submit a single command. Successful result does not guarantee that the resulting transaction has been written to
* the ledger. In order to get that semantic, use [[trackCommands]] or [[trackCommandsUnbounded]].
*/
Expand Down Expand Up @@ -198,10 +201,15 @@ private[daml] final class CommandClient(
throw new IllegalArgumentException(
s"Failing fast on submission request of command ${commands.commandId} with invalid ledger ID ${commands.ledgerId} (client expected $ledgerIdToUse)"
)
else if (commands.applicationId != applicationId)
if (commands.applicationId != applicationId)
throw new IllegalArgumentException(
s"Failing fast on submission request of command ${commands.commandId} with invalid application ID ${commands.applicationId} (client expected $applicationId)"
)
val nonEmptySubmissionId = if (commands.submissionId.isEmpty) {
submissionIdGenerator.generate()
} else {
commands.submissionId
}
val updatedDeduplicationPeriod = commands.deduplicationPeriod match {
case DeduplicationPeriod.Empty =>
DeduplicationPeriod.DeduplicationTime(
Expand All @@ -213,7 +221,12 @@ private[daml] final class CommandClient(
)
case existing => existing
}
submission.copy(commands = commands.copy(deduplicationPeriod = updatedDeduplicationPeriod))
submission.copy(commands =
commands.copy(
submissionId = nonEmptySubmissionId,
deduplicationPeriod = updatedDeduplicationPeriod,
)
)
})

def submissionFlow[Context](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ class CommandTrackerFlowTest
}
}

"a command is submitted without a submission id" should {

"throw an exception" in {
val Handle(submissions, results, _, _) =
runCommandTrackingFlow(allSubmissionsSuccessful)

submissions.sendNext(newSubmission("", commandId))

val actualException = results.expectError()
actualException shouldBe an[IllegalArgumentException]
actualException.getMessage shouldBe s"The submission id for the command $commandId is empty. This should not happen."
}
}

"the stream fails" should {

"expose internal state as materialized value" in {
Expand Down Expand Up @@ -328,6 +342,7 @@ class CommandTrackerFlowTest
CommandSubmission(
Commands(
commandId = commandId,
submissionId = submissionId,
deduplicationPeriod =
Commands.DeduplicationPeriod.DeduplicationTime(deduplicationTime),
)
Expand Down

0 comments on commit fc2c87d

Please sign in to comment.