# 待たずに送るか結果まで待つか選ぶ

> gRPCのLedger APIで、要求にCommandsを取る3つのRPCが、何を受け取り、何を返すかを、待たずに送る1つと結果まで待つ2つに分けて、図と表で追います。

- URL: https://cardene.dev/diagrams/canton-lapi-submit
- 公開日: 2026-11-23
- 更新日: 2026-11-23
- トピック: Blockchain
- タグ: Canton
- 親の図解: [コマンドの送信と完了](https://cardene.dev/diagrams/canton-command-submission-flow.md): Ledger APIへコマンドを送り、非同期に返る完了と台帳への効果を受け取り、送り直しの重複を除くまでを、章の中の図解で追います。

前の頁「コマンドを送って結果を知る」では、コマンドを送るサービスと、完了を受け取るサービスを見ました。
この頁では、gRPCのLedger APIの参照が載せるRPCのうち、要求に`Commands`を取る3つを追います。
待たずに送るRPCが1つと、結果まで待つRPCが2つで、受け取る欄と返す欄がどう違うかを見ます。

:::details gRPC
gRPCは、Googleが開発した、ネットワーク越しに別のプログラムの処理を呼び出すための通信の仕組みです。
:::

RPCは、ネットワーク越しに、別のプログラムの処理を呼び出す操作です。

https://cardene.dev/diagrams/canton-ledger-commands

## コマンドを送るRPCは3つある

`Commands`を要求に取るRPCは、`CommandSubmissionService`の`Submit`と、`CommandService`の`SubmitAndWait`と`SubmitAndWaitForTransaction`です。
`CommandSubmissionService`は`command_submission_service.proto`にあり、2つの操作を持ちます。
`CommandService`は`command_service.proto`にあり、3つの操作を持ちます。
次の表は、このうち`Commands`を取る3つのRPCです。

| RPC | サービス | 要求 | 応答 |
| --- | --- | --- | --- |
| `Submit` | `CommandSubmissionService` | `SubmitRequest` | `SubmitResponse` |
| `SubmitAndWait` | `CommandService` | `SubmitAndWaitRequest` | `SubmitAndWaitResponse` |
| `SubmitAndWaitForTransaction` | `CommandService` | `SubmitAndWaitForTransactionRequest` | `SubmitAndWaitForTransactionResponse` |

3つのRPCは、どれも要求を1件送って、応答を1件受け取る形です。
複数のメッセージを順に受け渡すstreamは、クライアント側にもサーバー側にも使いません。
パスは、先頭の`/`に、サービスの名前の`com.daml.ledger.api.v2.CommandSubmissionService`か`com.daml.ledger.api.v2.CommandService`、`/`、RPCの名前を続けた形です。
たとえば、`Submit`のパスは`/com.daml.ledger.api.v2.CommandSubmissionService/Submit`です。

reassignmentのコマンドを送る`SubmitReassignment`と`SubmitAndWaitForReassignment`は、別の頁が追います。
reassignmentは、頁「複数のSynchronizerを使い分ける」で見ました。
`Commands`を取る3つのRPCと、reassignmentの2つのRPCを合わせて、送るRPCは5つです。

https://cardene.dev/diagrams/canton-grpc-reassignment-submit

## 待たずに送るSubmit

`SubmitRequest`は、`commands`の1つの欄だけを持ちます。
欄の型は、前の章の頁「送るコマンドの中身を組む」で見た`Commands`です。

https://cardene.dev/diagrams/canton-lapi-commands

`SubmitResponse`は、欄を持ちません。
`grpcurl`の例が示す応答も、空のJSONの`{}`で、トランザクションを返しません。

:::details JSON
JSONは、データを文字で表すための形式で、Webのプログラムどうしのデータのやり取りに広く使われています。
:::

前の頁で見たとおり、Command Submission Serviceの呼び出しは、サーバーがコマンドを読んで、受け付けるか拒むかを決めた時点で返ります。
`Submit`の空の応答は、受け付けに成功した時の応答で、コマンドが実行されたことは意味しません。
完了の成否は、Command Completion Serviceが知らせます。
台帳に起きた変化は、Update Serviceが知らせます。

## 結果まで待つSubmitAndWait

`SubmitAndWaitRequest`も、`commands`の1つの欄だけを持ちます。
`SubmitAndWaitResponse`は、`update_id`のstringと、`completion_offset`のint64の2つの欄を返します。
int64は、64ビットの整数の型です。

`update_id`は、updateのIDを入れる欄です。
`completion_offset`は、完了のoffsetを入れる欄です。
offsetは、「partyのlocal ledgerと時刻」の頁で見た、streamの中の位置を表す値です。
重複を除く手順は、この完了のoffsetを、後の送信の判定に使えます。

結果まで待つ`SubmitAndWait`の応答の欄は、この2つだけです。
この応答は、完了を待ってコマンドが成功した時に返ります。
待ち時間を超えると、結果がまだ分からない場合があります。
詳しくは、頁「コマンドを1度だけ反映させる」が追います。

## 結果のトランザクションまで返すSubmitAndWaitForTransaction

`SubmitAndWaitForTransactionRequest`は、送るコマンドの`commands`と、返すトランザクションの内容を指定する`transaction_format`の2つの欄を持ちます。
`transaction_format`の型は`TransactionFormat`です。
`TransactionFormat`は、`event_format`と`transaction_shape`の2つの欄を持ちます。
`event_format`の型は`EventFormat`で、返すイベントを、partyなどで絞り込む指定を持ちます。

応答の`SubmitAndWaitForTransactionResponse`は、コマンドの結果のトランザクションを、`transaction`の1つの欄で返します。
この応答も、完了を待ってコマンドが成功した時に返ります。

図では、アプリとLedger APIのサーバーを並べ、待たずに送る呼び出しと、結果まで待つ2つの呼び出しで、アプリが応答として受け取る物を3つの段で追います。

```dragon
sequence 3つのRPCでアプリが応答として受け取る物

box APP as アプリ [note: コマンドを送る, color: blue, icon: app]
box LAPI as Ledger APIのサーバー [note: コマンドを受ける, color: rose, icon: server]

step 待たずに送ると受け付けた時に空の応答が返る
  say 待たずに送るRPCは、送るコマンドだけを渡します。サーバーが受け付けた時の応答は、欄を持たず、例の応答は空のJSONで、トランザクションを返しません。
  APP -> LAPI: 送るコマンド
  LAPI -> APP: 空の応答
  source CantonのSubmit [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2/commandsubmissionservice/submit]

step 完了を待つと更新のIDと完了の位置が返る
  say 結果まで待つRPCは、送るコマンドだけを渡します。コマンドが完了して成功した時の応答は、updateのIDと、完了のoffsetの2つの値です。
  APP -> LAPI: 送るコマンド
  LAPI -> APP: 更新のIDと完了の位置
  source CantonのSubmitAndWait [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2/commandservice/submitandwait]

step 完了を待ってトランザクションを返す
  say もう1つの結果まで待つRPCは、送るコマンドに加えて、返すトランザクションの内容の指定を渡します。コマンドが完了して成功した時の応答は、コマンドの結果のトランザクションです。
  APP -> LAPI: 送るコマンドと返す形式
  LAPI -> APP: 結果のトランザクション
  source CantonのSubmitAndWaitForTransaction [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2/commandservice/submitandwaitfortransaction]
```

## grpcurlの例の読み方

3つのRPCの頁は、`grpcurl`の呼び出しの例を載せます。
`grpcurl`は、gRPCのサーバーのRPCを、JSONで呼び出すための道具です。
個別頁が示す通信のプロトコルは、gRPCです。

`grpcurl`の例と、応答のJSONは、gRPCの要求と応答を、JSONの形で表示した物です。
書き方は、頁「Ledger APIのgRPC参照を読む」が追っています。

https://cardene.dev/diagrams/canton-lapi-overview

例は、`grpcurl -d @ <HOST:PORT>`に、サービスの名前とRPCの名前を続けた形で、標準入力からJSONの要求を渡します。
`grpcurl`に渡すのは、先頭の`/`を除いた`com.daml.ledger.api.v2.CommandService/SubmitAndWait`のような形です。
サーバーがTLSを使わない時は、`-plaintext`を足します。

:::details TLS
TLSは、ネットワークの通信を暗号化し、通信の相手が本物かを証明書で確かめるための規格です。
WebサイトのHTTPSの通信も、TLSを使っています。
:::

例のJSONは、欄の名前を`workflowId`や`transactionFormat`のようなcamelCaseで書きます。
camelCaseは、単語をつなぎ、2つ目からの単語の頭を大文字にする書き方です。
例のbytesの欄は、base64で符号化した文字列で、int64の欄は`"0"`のような文字列で表されます。
`"string"`や`"0"`は、型を示す例の値で、そのまま実行に使える値ではありません。

`SubmitAndWait`の例は、`commands`の中に、`workflowId`、`userId`、`commandId`、`create`のコマンド、`deduplicationDuration`、`minLedgerTimeAbs`、`minLedgerTimeRel`を置きます。
`create`のコマンドは、`templateId`と`createArguments`を持ちます。
例では、DurationとTimestampの欄も、`"string"`の文字列で表されます。
`grpcurl`が表示する成功した応答の例は、`updateId`の文字列と、`"0"`の`completionOffset`だけです。

`SubmitAndWaitForTransaction`の例は、`transactionFormat`の`transactionShape`に`TRANSACTION_SHAPE_UNSPECIFIED`を、`eventFormat`の`verbose`に`true`を置きます。
どちらも、例に載る値です。

`SubmitAndWait`の頁の関連するスキーマは、全部で22件です。
先頭の1件が`SubmitAndWaitRequest`で、最後の1件が応答の`SubmitAndWaitResponse`です。
間の20件は、要求から辿る`Commands`と4種のコマンド、`Value`の値の型の全て、`DisclosedContract`、`PrefetchContractKey`です。
プロトコルと入力と出力、関連する型、`grpcurl`の例は、`SubmitAndWait`の個別頁で確かめられます。

図では、3つのRPCの要求と応答の欄を、RPCごとに並べて比べます。

```dragon
mind 3つのRPCの要求と応答の欄の比較

step コマンドを送る3つのRPCを比べる
  say Commandsを取る3つのRPCは、待たずに送る1つと、結果まで待つ2つです。
  idea root as "Commandsを取る3つのRPC" [color: slate]
  source CantonのLedger APIのv2の参照 [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2]

step 待たずに送る物は応答の欄を持たない
  say 待たずに送る物は、要求がコマンドの1つの欄で、応答が欄を持ちません。
  idea sub as "待たずに送る" of root [color: blue]
  idea sub1 as "送るコマンド" of sub [color: blue]
  idea sub2 as "応答の欄なし" of sub [color: blue]
  source CantonのSubmit [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2/commandsubmissionservice/submit]

step 更新のIDと完了の位置を返す物
  say 結果まで待つ物の1つは、要求がコマンドの1つの欄で、応答が更新のIDと完了の位置の2つの欄です。
  idea wait as "待って更新のIDと完了の位置を返す" of root [color: blue]
  idea wait1 as "送るコマンド" of wait [color: blue]
  idea wait2 as "返る更新のID" of wait [color: blue]
  idea wait3 as "返る完了の位置" of wait [color: blue]
  source CantonのSubmitAndWait [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2/commandservice/submitandwait]

step 結果のトランザクションを返す物
  say 結果まで待つもう1つの物は、要求が送るコマンドと返すトランザクションの内容の指定の2つの欄で、応答がトランザクションの1つの欄です。
  idea tx as "待ってトランザクションを返す" of root [color: blue]
  idea tx1 as "送るコマンド" of tx [color: blue]
  idea tx2 as "返す内容の指定" of tx [color: blue]
  idea tx3 as "返るトランザクション" of tx [color: blue]
  source CantonのSubmitAndWaitForTransaction [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2/commandservice/submitandwaitfortransaction]
```

## 次に読む頁

次の頁「送ったコマンドの完了を受け取る」は、送ったコマンドが受け入れられたか拒まれたかを、完了のstreamとtransaction hashでの照会で受け取る3つのRPCを追います。

https://cardene.dev/diagrams/canton-lapi-completions

## 出典

https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2

https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2/commandservice/submitandwait

https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2/commandservice/submitandwaitfortransaction

https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2/commandsubmissionservice/submit

https://docs.canton.network/reference/protobuf/operations/com-daml-ledger-api-v2/commandservice/submitandwait

https://docs.canton.network/reference/protobuf/operations/com-daml-ledger-api-v2/commandservice/submitandwaitfortransaction

https://docs.canton.network/reference/protobuf/operations/com-daml-ledger-api-v2/commandsubmissionservice/submit

https://docs.canton.network/sdks-tools/api-reference/ledger-api-services
