# reassignmentでコントラクトを移す

> コントラクトを別のSynchronizerへ移すreassignmentのコマンドをJSON Ledger APIで送り結果を待つと、何が返りどのイベントが記録されるかを、2つの図で追います。

- URL: https://cardene.dev/diagrams/canton-json-reassignment
- 公開日: 2026-11-23
- 更新日: 2026-11-23
- トピック: Blockchain
- タグ: Canton
- 親の図解: [JSON Ledger APIのコマンドの送信](https://cardene.dev/diagrams/canton-json-command-flow.md): JSON Ledger APIでコマンドを待たずに送る形と結果まで待つ形を見分け、完了の受け取りと、reassignmentで移したコントラクトの結果を、章の中の図解で追います。

頁「JSON APIでコマンドを送る」では、reassignmentのコマンドを待たずに送るエンドポイントが、本文が空のJSONのオブジェクトの200を返すのを見ました。
この頁では、JSON Ledger APIで、reassignmentのコマンドを送って結果を待つ`POST /v2/commands/submit-and-wait-for-reassignment`を追います。
このエンドポイントは、1つの複合のreassignmentのコマンドを送り、結果を待ってから、台帳に記録されたreassignmentの情報を返します。
このエンドポイントは、版3.5で変更されました。

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

gRPCのLedger APIで同じことを書いた頁は、頁「gRPCでreassignmentを送る」です。

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

reassignmentは、移す元のSynchronizerでunassignし、移す先のSynchronizerでassignする2段の手順で、頁「コントラクトのSynchronizerを移す」で見ました。

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

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

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

## 本文はreassignmentのコマンドの組を書く

要求の本文は、必須の`reassignmentCommands`を持ちます。
`reassignmentCommands`は、`workflowId`、`userId`、`commandId`、`submitter`、`submissionId`、`commands`の欄を持ちます。
本文は、返すreassignmentに含めるイベントと、各イベントに含めるデータを指定する、任意の`eventFormat`も持てます。

次の表は、`reassignmentCommands`の欄の意味です。

| 欄 | 中身 |
| --- | --- |
| `commandId` | reassignmentのコマンドを表すID。`userId`と`submitter`と合わせた3つ組が、change IDになる |
| `submitter` | アプリが代わりにコマンドを送る1つのparty |
| `userId` | Participant Nodeでコマンドを出した利用者のID。利用者のトークンで認証する時は、トークンの利用者のIDが使われるので省ける |
| `commands` | reassignmentを成す要素の並び。空にできない |

partyは、Damlの台帳とやり取りする物で、userか組織を表します。
change IDは、意図した変更と、その全ての完了を突き合わせるのに使います。
Ledger APIの認可が有効な時は、要求の送り手が`submitter`のpartyの代わりに動くことを、認可のメタデータが認めている事が前提です。

`commands`の各要素は、`command`の欄を持ち、その欄に、`AssignCommand`か`UnassignCommand`を、`value`の中の欄と一緒に書きます。
スキーマの型には、空の選択の`Empty`も載りますが、送れるコマンドの種類は`AssignCommand`と`UnassignCommand`の2つです。
`UnassignCommand`は、コントラクトをSynchronizerから外し、`contractId`と、元のSynchronizerのIDの`source`と、先のSynchronizerのIDの`target`を受け取ります。
`AssignCommand`は、コントラクトを先のSynchronizerへ割り当て、unassignedイベントが持つreassignmentのIDの`reassignmentId`と、`source`と`target`を受け取って、そのunassignを完了させます。

次の本体は、`AssignCommand`を1つ送る形の例で、値は仮の値です。
例は`userId`を省いているので、利用者のトークンで認証して、トークンの利用者のIDを使う形です。

```json
{
  "reassignmentCommands": {
    "commandId": "example-assign-1234",
    "submitter": "<partyId>",
    "commands": [
      {
        "command": {
          "AssignCommand": {
            "value": {
              "reassignmentId": "<reassignmentId>",
              "source": "<sourceSynchronizerId>",
              "target": "<targetSynchronizerId>"
            }
          }
        }
      }
    ]
  }
}
```

HTTPは、ブラウザやプログラムがWebのサーバーと要求と応答をやり取りする決まりです。
要求の本文を読み取れない時は、HTTPの400が`text/plain`で返ります。
Ledger APIが送信を拒んだ時は、gRPCのエラーコードに対応するHTTPのステータスと、JSONのエラーが返ります。
エラーのJSONは、必須の4つの欄と、任意の6つの欄を持ちます。

## 結果を待つとreassignmentを含む応答が返る

待たずに送るエンドポイントは、受け付けた時に空のJSONのオブジェクトの200を返しました。
結果を待つこのエンドポイントは、unassignかassignが完了した後に、台帳に記録されたreassignmentの情報を返します。
unassignの結果のreassignmentが持つunassignedイベントのreassignmentのIDが、assignのコマンドの入力になります。
次の図は、対象のイベントを含む成功の応答を、1件ずつ示す例です。

図では、アプリとJSON Ledger APIを並べ、unassignからassignまでを、3つの段で追います。

```dragon
sequence reassignmentを結果まで待って2回送る

box APP as アプリ [note: reassignmentを送る, color: blue, icon: app]
box JAPI as JSON Ledger API [note: 結果まで待って返す, color: rose, icon: server]

step unassignのコマンドを送って結果を待つ
  say アプリは、移す元と移す先のSynchronizerのIDと、移すコントラクトのIDを持つunassignのコマンドを送り、結果が出るまで待ちます。成功すると、完了後のreassignmentが返り、その中のイベントの集まりに、unassignedイベントが入ります。
  APP -> JAPI: unassignのコマンド
  JAPI -> APP: 完了後のreassignment
  source CantonのPOST /v2/commands/submit-and-wait-for-reassignment [url: https://docs.canton.network/reference/json-api-reference/post-v2commandssubmit-and-wait-for-reassignment]

step unassignedイベントのreassignmentのIDを取り出す
  say アプリは、応答のreassignmentの、イベントの集まりにあるunassignedイベントから、reassignmentのIDを読み取ります。このIDは、assignのコマンドの入力に使います。
  act APP: unassignedイベントのreassignmentのIDを読む
  source CantonのPOST /v2/commands/submit-and-wait-for-reassignment [url: https://docs.canton.network/reference/json-api-reference/post-v2commandssubmit-and-wait-for-reassignment]
  source Cantonの複数のSynchronizerの仕組み [url: https://docs.canton.network/overview/learn/multi-synchronizer]

step assignのコマンドを送ってunassignを完了させる
  say アプリは、reassignmentのIDと、移す元と移す先のSynchronizerのIDを持つassignのコマンドを送ります。このコマンドが、そのunassignを完了させ、成功すると、完了後のreassignmentが返り、その中のイベントの集まりに、assignedイベントが入ります。
  APP -> JAPI: assignのコマンド
  JAPI -> APP: 完了後のreassignment
  source CantonのPOST /v2/commands/submit-and-wait-for-reassignment [url: https://docs.canton.network/reference/json-api-reference/post-v2commandssubmit-and-wait-for-reassignment]
```

## 応答のreassignmentの欄

200の応答は、`reassignment`を1つ返します。
`reassignment`は、台帳に記録されたreassignmentの情報で、スキーマは次の9つの欄を定めます。
必須でない欄は、値が入らない場合があります。

| 欄 | 中身 |
| --- | --- |
| `updateId` | サーバーが割り当てるID。ログの突き合わせに役立つ。必須 |
| `commandId` | このreassignmentを起こしたコマンドのID。送ったpartyが、送ったParticipant Nodeで見る時だけ入る任意の欄 |
| `workflowId` | コマンドに`workflowId`を付けた時だけ入る任意の欄 |
| `offset` | Participant Nodeのoffset。必須 |
| `events` | reassignmentのイベントの集まり。必須で、空にできない |
| `traceContext` | trace context。任意 |
| `recordTime` | 記録された時刻。unassignedイベントでは元の、assignedイベントでは先のSynchronizerの時刻を指す。必須 |
| `synchronizerId` | このreassignmentを同期したSynchronizerのID。必須 |
| `paidTrafficCost` | このParticipant Nodeが、unassignかassignの要求に払ったtrafficの費用。任意 |

offsetは、頁「partyのlocal ledgerと時刻」で見た、streamの中の位置を表す値です。
record timeは、同じ頁で見た、台帳がトランザクションを記録する時に付ける時刻です。
trace contextは、Ledger APIで処理の流れを追うための情報です。
trafficは、Cantonでトランザクションの手数料を表す語です。

https://cardene.dev/diagrams/canton-local-ledger-time

`paidTrafficCost`は、別のParticipant Nodeが始めた物、repair serviceでオフラインに行った物、そのParticipant NodeがLedger APIでtrafficの費用を返し始める前に処理した物、送っていないpartyで絞った問い合わせの結果では入りません。
repair serviceは、関わる全てのParticipant Nodeでコントラクトの割り当てを手で直す道具です。
頁「ACS commitmentで状態の一致を示す」も、同じ道具を挙げています。

https://cardene.dev/diagrams/canton-acs-commitment-proof

## イベントはassignedとunassignedの2つがある

`events`の1つの要素は、`JsAssignmentEvent`か`JsUnassignedEvent`です。
`JsAssignmentEvent`は6つの欄を直下に持ち、`JsUnassignedEvent`は12の欄を`value`の中に持ちます。
次の表は、2つのイベントが持つ欄です。

| イベント | 欄 |
| --- | --- |
| `JsAssignmentEvent` | `source`、`target`、`reassignmentId`、`submitter`、`reassignmentCounter`、`createdEvent` |
| `JsUnassignedEvent`の`value` | `reassignmentId`、`contractId`、`templateId`、`source`、`target`、`packageName`、`witnessParties`、`offset`、`nodeId`、`submitter`、`reassignmentCounter`、`assignmentExclusivity` |

`JsUnassignedEvent`は、コントラクトが外され、元のSynchronizerで使えなくなったことを記録します。
`value`の`reassignmentId`は、unassignのIDで、assignのコマンドの入力に使います。
`submitter`は、unassignがその代わりに実行されたpartyで、repair serviceでオフラインに外した時は空になります。
`witnessParties`は、このイベントの通知を受けるpartyで、`nodeId`は、元のreassignmentの中のイベントの位置です。

`templateId`は、移されたコントラクトのtemplateのIDで、templateは、頁「templateでコントラクトを作る」が追っています。
assignedイベントの`createdEvent`は、移されたコントラクトのデータを持ち、新しいコントラクトを作るものではありません。

https://cardene.dev/diagrams/canton-daml-template-choice

`reassignmentCounter`は、対になるassignedとunassignedのイベントで同じ値を持ち、同じコントラクトのunassignのたびに厳密に増えます。
コントラクトの作成は、0にあたります。
`assignmentExclusivity`は、先のSynchronizerで測った時刻で、この時刻より前にassignを始められるのは、unassignを送ったpartyだけです。
reassignmentに関わるParticipant Nodeに対して定まる任意の欄です。

reassignmentのoffsetは、各Participant Nodeが管理します。
そのため、別のParticipant Nodeで同じoffsetになるとはみなせません。
node IDも、Participant Nodeごとに見える部分が違うので、一致するとは限りません。

図では、応答のイベントの集まりが持つ2種類のイベントの欄を、4つの段で追います。
9つの欄のうち、イベントの集まり以外は、上の表のとおりです。

```dragon
mind 応答のイベントの集まりが持つ欄

step イベントの集まりは2種類のイベントを持つ
  say 返るreassignmentは、台帳に記録されたreassignmentの情報で、空にできないイベントの集まりを持ちます。イベントの集まりの各要素は、assignedイベントか、unassignedイベントのどちらかです。
  idea root as "返るreassignmentのイベントの集まり" [color: slate]
  idea asg as "assignedイベント" of root [color: blue]
  idea una as "unassignedイベント" of root [color: blue]
  source CantonのPOST /v2/commands/submit-and-wait-for-reassignment [url: https://docs.canton.network/reference/json-api-reference/post-v2commandssubmit-and-wait-for-reassignment]

step assignedイベントは移したコントラクトのデータを持つ
  say assignedイベントは、移す元と移す先、reassignmentのID、代わりに実行されたparty、reassignmentの回数に加えて、移したコントラクトのデータを持つ作成のイベントを持ちます。
  idea a1 as "移す元" of asg [color: blue]
  idea a2 as "移す先" of asg [color: blue]
  idea a3 as "reassignmentのID" of asg [color: blue]
  idea a4 as "代わりに実行されたparty" of asg [color: blue]
  idea a5 as "reassignmentの回数" of asg [color: blue]
  idea a6 as "移したコントラクトのデータ" of asg [color: blue]
  source CantonのPOST /v2/commands/submit-and-wait-for-reassignment [url: https://docs.canton.network/reference/json-api-reference/post-v2commandssubmit-and-wait-for-reassignment]

step unassignedイベントはコントラクトと移す先を持つ
  say unassignedイベントは、reassignmentのID、コントラクトのIDとtemplateのID、移す元と移す先、パッケージ名、通知を受けるparty、offset、node IDを持ちます。
  idea u1 as "reassignmentのID" of una [color: blue]
  idea u2 as "コントラクトのID" of una [color: blue]
  idea u3 as "templateのID" of una [color: blue]
  idea u4 as "移す元" of una [color: blue]
  idea u5 as "移す先" of una [color: blue]
  idea u6 as "パッケージ名" of una [color: blue]
  idea u7 as "通知を受けるparty" of una [color: blue]
  idea u8 as "offset" of una [color: blue]
  idea u9 as "node ID" of una [color: blue]
  source CantonのPOST /v2/commands/submit-and-wait-for-reassignment [url: https://docs.canton.network/reference/json-api-reference/post-v2commandssubmit-and-wait-for-reassignment]

step unassignedイベントは代わりに実行されたpartyと回数と時刻も持つ
  say unassignedイベントは、代わりに実行されたpartyと、reassignmentの回数と、assignを始められるpartyを限る時刻も持ちます。
  idea u10 as "代わりに実行されたparty" of una [color: blue]
  idea u11 as "reassignmentの回数" of una [color: blue]
  idea u12 as "assignを始められるpartyを限る時刻" of una [color: blue]
  source CantonのPOST /v2/commands/submit-and-wait-for-reassignment [url: https://docs.canton.network/reference/json-api-reference/post-v2commandssubmit-and-wait-for-reassignment]
```

## 出典

https://docs.canton.network/reference/json-api-reference/post-v2commandssubmit-and-wait-for-reassignment

https://docs.canton.network/overview/learn/multi-synchronizer
