# gRPCでreassignmentを送る

> コントラクトを別のSynchronizerへ移すunassignとassignのコマンドを、gRPCのLedger APIで組み、待たずに送る形と結果まで待つ形で、受け付けと完了と返る物がどう違うかを追います。

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

頁「待たずに送るか結果まで待つか選ぶ」では、コントラクトを作るコマンドと、choiceを行使するコマンドを送るRPCを見ました。
この頁では、コントラクトを別のSynchronizerへ移すreassignmentのコマンドを、gRPCのLedger APIで送る2つのRPCを追います。
unassignとassignのコマンドの組み方と、待たずに送る形と結果まで待つ形で、受け付けと完了と返る物がどう違うかを見ます。

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

RPCは、アプリがLedger APIに対して呼び出す処理です。

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

reassignmentは、移す元のSynchronizerでunassignし、移す先のSynchronizerでassignする2段の手順です。
Synchronizerをまたぐ2段は、頁「Synchronizerが受け持つ仕事を知る」と、頁「コントラクトのSynchronizerを移す」で見ました。

https://cardene.dev/diagrams/canton-reassignment-protocol

## reassignmentを送るRPCは2つある

reassignmentのコマンドを送るRPCは、`CommandSubmissionService`の`SubmitReassignment`と、`CommandService`の`SubmitAndWaitForReassignment`の2つです。
次の表は、2つのRPCの要求と応答と、パスです。

| RPC | 要求 | 応答 | パス |
| --- | --- | --- | --- |
| `SubmitReassignment` | `SubmitReassignmentRequest` | `SubmitReassignmentResponse` | `/com.daml.ledger.api.v2.CommandSubmissionService/SubmitReassignment` |
| `SubmitAndWaitForReassignment` | `SubmitAndWaitForReassignmentRequest` | `SubmitAndWaitForReassignmentResponse` | `/com.daml.ledger.api.v2.CommandService/SubmitAndWaitForReassignment` |

2つのRPCは、どちらも要求を1件送って応答を1件受け取る形で、複数のメッセージを順に受け渡すstreamは使いません。
`grpcurl`に渡すサービスとRPCの名前は、表のパスから先頭の`/`を除いた形です。

`SubmitReassignmentRequest`は、`reassignment_commands`の`ReassignmentCommands`の1つの欄だけを持ち、`event_format`を持ちません。
`SubmitReassignmentResponse`は、欄を持たず、例の応答は`{}`です。
`SubmitAndWaitForReassignmentRequest`は、`reassignment_commands`と、`event_format`の`EventFormat`の2つの欄を持ちます。
`SubmitAndWaitForReassignmentResponse`は、`reassignment`の`Reassignment`の1つの欄を返します。

## ReassignmentCommandsの組み方

`ReassignmentCommands`は、6つの欄を持ちます。
`workflow_id`、`user_id`、`command_id`、`submitter`、`submission_id`は、どれも文字列の欄で、`commands`は、`ReassignmentCommand`の列の欄です。

`Commands`は、送るpartyを`act_as`の列で表しますが、`ReassignmentCommands`は、送信するpartyを示す1つの`submitter`の文字列で表します。
図の「提出者」は、この`submitter`の欄です。

`Commands`の欄は、前の章の頁「送るコマンドの中身を組む」が追っています。

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

`ReassignmentCommand`は、`unassign_command`の`UnassignCommand`か、`assign_command`の`AssignCommand`の、2種の欄のどちらかを持ちます。
`commands`の列の1つ1つの要素が、unassignかassignのどちらか1つを持ちます。
次の表は、2つのコマンドが持つ欄です。

| コマンド | 欄 |
| --- | --- |
| `UnassignCommand` | `contract_id`、移す元の`source`、移す先の`target`、`override_source_validation_package_id`、`override_target_validation_package_id`の5つの文字列 |
| `AssignCommand` | `reassignment_id`、`source`、`target`の3つの文字列 |

`AssignCommand`は、`UnassignCommand`と違って、`contract_id`を持ちません。
`AssignCommand`の`reassignment_id`は、移す元でunassignした時に返った結果の`UnassignedEvent`の、同じ名前の`reassignment_id`の値です。
コントラクトのIDでunassignを送り、その結果の`reassignment_id`を`AssignCommand`へ渡して、移す先へassignします。

Javaのバインディングでは、パッケージ`com.daml.ledger.javaapi.data`の`AssignCommand`が、`ReassignmentCommand`を継ぐ`final`のクラスで、`AssignCommand(String, String, String)`のコンストラクターと、`getReassignmentId()`、`getSource()`、`getTarget()`を持ちます。
図では、コマンドの組にどの欄があるかを、4つの段で表示します。

```dragon
mind reassignmentのコマンドの欄

step 組は5つの文字列の欄とコマンドの列を持つ
  say reassignmentのコマンドは、1つの組にまとめます。組は、5つの文字列の欄と、コマンドの列を持ち、送信するpartyは、1つの提出者の欄で表します。
  idea root as "コマンドの組" [color: slate]
  idea hd as "組の文字列の欄" of root [color: blue]
  idea cmds as "コマンドの列" of root [color: blue]
  source CantonのSubmitReassignment [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2/commandsubmissionservice/submitreassignment]

step 文字列の欄は5つある
  say 5つの文字列の欄は、workflow ID、user ID、command ID、提出者、submission IDです。
  idea h1 as "workflow ID" of hd [color: blue]
  idea h2 as "user ID" of hd [color: blue]
  idea h3 as "command ID" of hd [color: blue]
  idea h4 as "提出者" of hd [color: blue]
  idea h5 as "submission ID" of hd [color: blue]
  source CantonのSubmitReassignment [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2/commandsubmissionservice/submitreassignment]

step 列の要素がunassignの時は5つの欄を持つ
  say コマンドの列の1つ1つの要素は、unassignのコマンドか、assignのコマンドのどちらか1つを持ちます。要素がunassignの時は、contract ID、移す元、移す先と、package IDの欄を2つ持ちます。
  idea u1 as "unassignはcontract ID" of cmds [color: blue]
  idea u2 as "unassignは移す元" of cmds [color: blue]
  idea u3 as "unassignは移す先" of cmds [color: blue]
  idea u4 as "unassignはpackage IDの欄が2つ" of cmds [color: blue]
  source CantonのSubmitReassignment [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2/commandsubmissionservice/submitreassignment]

step 列の要素がassignの時は3つの欄を持つ
  say 要素がassignの時は、unassignの結果で得たreassignment ID、移す元、移す先を持ち、contract IDを持ちません。
  idea a1 as "assignはunassignの結果のID" of cmds [color: blue]
  idea a2 as "assignは移す元" of cmds [color: blue]
  idea a3 as "assignは移す先" of cmds [color: blue]
  source CantonのSubmitReassignment [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2/commandsubmissionservice/submitreassignment]
```

## 結果まで待つと返るReassignment

`Reassignment`は、9つの欄を持ちます。
`update_id`、`command_id`、`workflow_id`、`offset`、`ReassignmentEvent`の列の`events`、`trace_context`、`record_time`、`synchronizer_id`、`paid_traffic_cost`です。
`offset`と`paid_traffic_cost`は、int64の欄です。

`ReassignmentEvent`は、`unassigned`か`assigned`のどちらかを持ちます。
次の表は、2つのイベントが持つ欄です。

| イベント | 欄 |
| --- | --- |
| `UnassignedEvent` | `reassignment_id`、`contract_id`、`template_id`、`source`、`target`、`submitter`、uint64の`reassignment_counter`、Timestampの`assignment_exclusivity`、`witness_parties`、`package_name`、`offset`、`node_id` |
| `AssignedEvent` | `source`、`target`、`reassignment_id`、`submitter`、uint64の`reassignment_counter`、`CreatedEvent`の`created_event` |

`reassignment_counter`は、コントラクトが作られた時は0で、unassignのたびに1ずつ増える値です。
unassignのイベントと、対応するassignのイベントは、同じ値を持ちます。

図では、アプリとLedger APIのサーバーを並べ、2つのRPCを別々の呼び方として、受け付けと完了で返る物を2つの段で追います。

```dragon
sequence reassignmentを送る2つの呼び方

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

step 待たずに送ると受け付けた時に空の応答が返る
  say 待たずに送るRPCは、reassignmentのコマンドだけを渡し、サーバーが送信を受け付けた時点で、欄を持たない応答を返します。例の応答も空です。空の応答は、unassignやassignが完了したことを意味しません。
  APP -> LAPI: reassignmentのコマンド
  LAPI -> APP: 受付の空の応答
  source CantonのSubmitReassignment [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2/commandsubmissionservice/submitreassignment]

step 別の呼び方として、完了まで待つと結果が返る
  say もう1つのRPCは、送ったunassignかassignが完了するまで待ちます。reassignmentのコマンドに加えて、返すイベントの絞り込みを渡し、成功した時に、reassignmentを返します。例の応答は、unassignのイベントを持ちます。
  APP -> LAPI: コマンドと絞り込み
  LAPI -> APP: 完了後のreassignment
  source CantonのSubmitAndWaitForReassignment [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2/commandservice/submitandwaitforreassignment]
```

## grpcurlの例

`grpcurl`は、gRPCのサーバーのRPCを、端末からJSONで呼び出すコマンドです。

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

書き方は、前の章の頁「Ledger APIのgRPC参照を読む」が追っています。

`SubmitAndWaitForReassignment`の頁は、`grpcurl`の呼び出しの例を載せ、`reassignmentCommands`に`workflowId`、`userId`、`commandId`、`submitter`、`submissionId`と、1つの`unassignCommand`を書きます。
`unassignCommand`は、`contractId`、`source`、`target`、`overrideSourceValidationPackageId`、`overrideTargetValidationPackageId`を持ちます。

例の`eventFormat`は、`filtersByParty`と`filtersForAnyParty`の両方に、`cumulative`の`wildcardFilter`を置き、`verbose`を`true`にします。
`filters_by_party`の型は、mapです。
例のJSONは、型から生成された表示で、`filtersByParty`を配列で表示します。
実際に送るJSONでは、mapはparty IDをキーにしたオブジェクトで書き、生成例は、そのまま実行に使える形ではありません。

OKの応答の例は、`reassignment`の中に、`updateId`、`commandId`、`workflowId`、`offset`、`unassigned`のイベント、`traceContext`、`recordTime`、`synchronizerId`を返します。
`unassigned`のイベントは、`reassignmentId`、`contractId`、`templateId`、`source`、`target`、`submitter`、`reassignmentCounter`、`assignmentExclusivity`を持ちます。
`reassignmentCounter`の`"0"`は、生成された見本の値です。
実際のカウンターは、unassignのたびに増えます。

`SubmitAndWaitForReassignment`の頁の関連するスキーマは、全部で31件です。
reassignmentのコマンド、絞り込みの`EventFormat`から`TemplateFilter`、返る`Reassignment`とそのunassignとassignのイベント、`CreatedEvent`と`Value`の値の型、`InterfaceView`、`TraceContext`の型を並べます。

## 次に読む頁

次の頁「Canton Consoleからコマンドを出す」は、Canton Consoleから、Ledger APIでコマンドとreassignmentと外部の署名付きのトランザクションを出し、結果をどう待つかを追います。

https://cardene.dev/diagrams/canton-ledger-api-submit

## 出典

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/submitandwaitforreassignment

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

https://docs.canton.network/reference/java/com-daml-ledger-javaapi-data/assigncommand

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

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

https://docs.canton.network/overview/reference/reassignment-protocol
