# updateで移動とtrafficを追う

> updateのreassignmentとtopology transactionが持つ欄と、trafficの費用が入らない場合と、アカウントの残高を更新するエンドポイントを追います。

- URL: https://cardene.dev/diagrams/canton-json-reassign-traffic
- 公開日: 2026-11-23
- 更新日: 2026-11-23
- トピック: Blockchain
- タグ: Canton
- 親の図解: [JSON Ledger APIの読み出し](https://cardene.dev/diagrams/canton-json-ledger-reading.md): JSON Ledger APIで台帳を読む呼び出しを、読むイベントの絞り込み、createとexerciseのイベント、activeなコントラクト、updateのstream、WebSocketまで、章の中の図解で追います。

前の頁「WebSocketでstreamを読む」では、updateのストリームが4つの形のどれかを返すことを見ました。
この頁では、そのうち、`Reassignment`と`TopologyTransaction`の欄と、trafficの費用を読みます。
続けて、別の呼び出しとして、JSON Ledger APIで、trafficのアカウントの残高を更新する`POST /v2/traffic/accounts`を追います。
gRPCのLedger APIで同じことを書いた頁は、頁「更新に入るイベントの形を知る」と、頁「trafficの残高を管理する」です。

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

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

https://cardene.dev/diagrams/canton-ledger-event-shapes

https://cardene.dev/diagrams/canton-traffic-accounts

## reassignmentのupdateは9つの欄を持つ

JSONでは、updateの`Reassignment`の`value`が`JsReassignment`で、台帳の上のreassignmentの全体を表します。
`JsReassignment`の型は、`updateId`、`commandId`、`workflowId`、`offset`、`events`、`traceContext`、`recordTime`、`synchronizerId`、`paidTrafficCost`の9つの欄を定義します。
`commandId`と`workflowId`と`traceContext`と`paidTrafficCost`は任意で、値が入らないことがあります。

| 欄 | 中身 |
| --- | --- |
| `updateId` | サーバーが割り当てる値で、ログの突き合わせに役立つ |
| `commandId` | 送ったParticipant Nodeの上の、送ったpartyにだけ入り、ほかの全員には入らない |
| `workflowId` | reassignmentのコマンドの送信で、`workflow_id`を設定した時だけ入る |
| `offset` | このParticipant Nodeのoffset |
| `events` | 空でない`JsReassignmentEvent`の並び。各要素は、`JsAssignmentEvent`か、`value`に`UnassignedEvent`を持つ`JsUnassignedEvent` |
| `traceContext` | 元のコマンドの送信のtrace context |
| `recordTime` | 記録された時刻。unassignのイベントでは移す元のSynchronizer、assignのイベントでは移す先のSynchronizerの時刻を指す |
| `synchronizerId` | このreassignmentを同期したSynchronizer |
| `paidTrafficCost` | このParticipant Nodeが、対応するassignかunassignの要求に払ったtrafficの費用 |

offsetは、「partyのlocal ledgerと時刻」の頁で見た、streamの中の位置を表す値です。

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

trace contextは、Ledger APIで処理の流れを追うための情報で、欄は、W3Cのtrace contextの`traceparent`と`tracestate`です。
元の送信にtrace contextが無い時は、ledger-api-serverが作った一意の値が入ります。
定義は、頁「トランザクションとexerciseを読む」が追っています。

https://cardene.dev/diagrams/canton-json-transaction-events

`JsAssignmentEvent`は、`source`、`target`、`reassignmentId`、`submitter`、`reassignmentCounter`、`createdEvent`を持ちます。
2つのイベントの欄は、頁「reassignmentでコントラクトを移す」が追っています。

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

trafficは、Cantonのトランザクションの料金を指す語です。
`paidTrafficCost`は、次の4つの場合には入りません。

- 別のParticipant Nodeが始めたreassignmentの場合
- repair serviceでオフラインに行ったreassignmentの場合
- そのParticipant NodeがLedger APIでtrafficの費用の提供を始める前に処理したreassignmentの場合
- 対応するassignかunassignのコマンドを送ったpartyとは別のpartyを指定した問い合わせの結果の場合

repair serviceは、関わる全てのParticipant Nodeでコントラクトの割り当てを手で直す道具です。

## topology transactionはParticipant Nodeの権限の変化を記録する

JSONでは、updateの`TopologyTransaction`の`value`が`JsTopologyTransaction`で、欄は、`updateId`、`offset`、`synchronizerId`、`recordTime`、`events`、`traceContext`の6つです。
`synchronizerId`は、このtopology transactionを同期したSynchronizerです。
`recordTime`は、中の変更が有効になる時刻で、順序付けから有効になるまでに、少しの遅れがあります。
topology transactionは、Synchronizerごとに、順序付けの時刻ではなく、有効になる時刻の順に並びます。

権限の変化を含めるには、頁「JSONで読むイベントを絞り込む」で見た、`updateFormat`の`includeTopologyEvents`を指定します。

https://cardene.dev/diagrams/canton-json-event-filters

`events`は、空でない`TopologyEvent`の並びです。
各要素は`event`を持ち、`event`は、`Empty`と`ParticipantAuthorizationAdded`と`ParticipantAuthorizationChanged`と`ParticipantAuthorizationOnboarding`と`ParticipantAuthorizationRevoked`のどれかです。
4つの`ParticipantAuthorization`のイベントは、それぞれ`value`の中に欄を持ちます。
`partyId`と`participantId`は、権限の対象になるpartyと、そのpartyをhostするParticipant Nodeの組を示します。

| イベント | 持つ欄 |
| --- | --- |
| `ParticipantAuthorizationAdded` | `partyId`と`participantId`と`participantPermission` |
| `ParticipantAuthorizationChanged` | `partyId`と`participantId`と`participantPermission` |
| `ParticipantAuthorizationOnboarding` | `partyId`と`participantId`と`participantPermission` |
| `ParticipantAuthorizationRevoked` | `partyId`と`participantId`だけ |

`participantPermission`に取れる値は、`PARTICIPANT_PERMISSION_UNSPECIFIED`、`PARTICIPANT_PERMISSION_SUBMISSION`、`PARTICIPANT_PERMISSION_CONFIRMATION`、`PARTICIPANT_PERMISSION_OBSERVATION`の4つです。
partyをhostするParticipant Nodeには、Submission、Confirmation、Observationのどれかの権限が付きます。
権限の値と未指定の値の違いは、頁「userの権利とhostの権限を読む」が追っています。

https://cardene.dev/diagrams/canton-users-and-permissions

`ParticipantAuthorizationOnboarding`から`ParticipantAuthorizationAdded`へ移る流れは、頁「ACSの書き出しと取り込みが版で変わった所」が見ています。

https://cardene.dev/diagrams/canton-acs-import-export

図では、updateの`Reassignment`と`TopologyTransaction`の型に定義された欄を、3つの段で足します。
任意の欄は、毎回返るとは限りません。

```dragon
mind updateのreassignmentとtopology transactionが持つ欄

step updateのreassignmentとtopology transactionは別の欄を持つ
  say updateのストリームが返す形のうち、reassignmentとtopology transactionは、それぞれ別の欄を持ちます。
  idea root as "updateの2つの形" [color: slate]
  idea rs as "reassignment" of root [color: blue]
  idea tp as "topology transaction" of root [color: blue]
  source CantonのPOST /v2/updates [url: https://docs.canton.network/reference/json-api-reference/post-v2updates]

step reassignmentの型は9つの欄を定義する
  say reassignmentの型は、update ID、command ID、workflow ID、offset、イベントの並び、trace context、record time、Synchronizer ID、払ったtrafficの費用の9つの欄を定義します。
  idea r1 as "update ID" of rs [color: blue]
  idea r2 as "command ID" of rs [color: blue]
  idea r3 as "workflow ID" of rs [color: blue]
  idea r4 as "offset" of rs [color: blue]
  idea r5 as "イベントの並び" of rs [color: blue]
  idea r6 as "trace context" of rs [color: blue]
  idea r7 as "record time" of rs [color: blue]
  idea r8 as "Synchronizer ID" of rs [color: blue]
  idea r9 as "trafficの費用" of rs [color: blue]
  source CantonのPOST /v2/updates [url: https://docs.canton.network/reference/json-api-reference/post-v2updates]

step topology transactionの型は6つの欄を定義する
  say topology transactionの型は、update ID、offset、Synchronizer ID、record time、イベントの並び、trace contextの6つの欄を定義し、trafficの費用の欄は定義しません。
  idea t1 as "update ID" of tp [color: blue]
  idea t2 as "offset" of tp [color: blue]
  idea t3 as "Synchronizer ID" of tp [color: blue]
  idea t4 as "record time" of tp [color: blue]
  idea t5 as "イベントの並び" of tp [color: blue]
  idea t6 as "trace context" of tp [color: blue]
  source CantonのPOST /v2/updates [url: https://docs.canton.network/reference/json-api-reference/post-v2updates]
```

## trafficのアカウントの残高を更新する

`POST /v2/traffic/accounts`は、指定したアカウントのIDのアカウントの状態を更新するエンドポイントで、版3.5で足されました。
Ledger APIの管理者のuserにだけ許します。
このエンドポイントは、trafficを強制する機能を有効にした時だけ公開され、設定は`canton.participants.participant1.traffic-enforcement.enabled = true`です。

| 欄 | 中身 |
| --- | --- |
| `accountId` | 必須。更新するアカウントのID。今は、アカウントのIDがpartyのIDに結び付いて識別される |
| `balanceDelta` | int64。今の残高に足す差分で、負の値で減り、正の値で増える。省くと、残高を変えない |
| `deduplicationId` | 必須。1回の更新の識別子で、重複を除くのに使う |

`accountId`の制約は、今後の版で外される見込みで、特定のpartyに結び付かない、利用者が決めるアカウントのIDを許す予定です。

`balanceDelta`が負で、残高から差し引く量が今の残高を上回ると、残高が負になることがあります。
呼び出し側は、異なる更新ごとに別のIDを使い、やり直せるエラーで失敗した更新をやり直す時は、同じIDを使うと定められています。
処理済みの`deduplicationId`を送ると、無視されます。
`balanceDelta`を指定した呼び出しで新しいIDを送ると、差分を残高にもう一度加えます。

200の応答は、`response`を持ちます。
`response`は`GetAccountResponse`で、更新した後のアカウントの状態として、`accountId`と、int64の`balance`を返します。
残高を読む呼び出しと、例の値は、頁「trafficのアカウントを読み足す」が追っています。

https://cardene.dev/diagrams/canton-traffic-accounting

図では、アプリとJSON Ledger APIを並べ、`balanceDelta`を指定して残高を更新する呼び出しと、重複排除のIDの働きと、残高が負になる場合を、5つの段で追います。

```dragon
sequence アカウントの残高を更新する

box APP as アプリ [note: 残高を更新する, color: rose, icon: app]
box JAPI as JSON Ledger API [note: アカウントを更新する, color: blue, icon: server]

step Ledger APIの管理者のuserが残高を更新する
  say Ledger APIの管理者のuserは、アカウントのIDと、残高に加える差分と、重複排除のIDを渡し、サーバーは、更新した後のアカウントのIDと残高を返します。
  APP -> JAPI: アカウントのIDと差分と重複排除のID
  JAPI -> APP: アカウントのIDと更新した後の残高
  source CantonのPOST /v2/traffic/accounts [url: https://docs.canton.network/reference/json-api-reference/post-v2trafficaccounts]

step やり直しは元と同じ重複排除のIDで送る
  say 差分を指定した更新が、やり直せるエラーで失敗した時は、アプリは、元と同じ重複排除のIDで、同じ要求を送ります。
  APP -> JAPI: 元と同じ重複排除のID
  source CantonのPOST /v2/traffic/accounts [url: https://docs.canton.network/reference/json-api-reference/post-v2trafficaccounts]

step 処理済みの重複排除のIDの要求は無視される
  say 重複排除のIDが処理済みなら、その要求は無視されるので、差分は重ねて加わりません。
  act JAPI: 処理済みの要求を無視する
  source CantonのPOST /v2/traffic/accounts [url: https://docs.canton.network/reference/json-api-reference/post-v2trafficaccounts]

step 同じ差分を新しい重複排除のIDで送ると加わる
  say 別の更新として、同じ差分を新しい重複排除のIDで送ると、差分を残高にもう一度加えます。
  APP -> JAPI: 同じ差分と新しい重複排除のID
  source CantonのPOST /v2/traffic/accounts [url: https://docs.canton.network/reference/json-api-reference/post-v2trafficaccounts]

step 差し引く量が残高を上回ると残高が負になる
  say 差分が負で、残高から差し引く量が今の残高を上回ると、残高が負になることがあります。
  act JAPI: 残高が負になることがある
  source CantonのPOST /v2/traffic/accounts [url: https://docs.canton.network/reference/json-api-reference/post-v2trafficaccounts]
```

## 出典

https://docs.canton.network/reference/json-api-reference/post-v2updates

https://docs.canton.network/reference/json-api-reference/post-v2trafficaccounts
