# trafficの残高を管理する

> gRPCのTrafficServiceの4つのRPCで、アカウントの残高を読み、増減させ、古いイベントを刈り込む呼び方を、パスと要求と応答の欄と例で追います。

- URL: https://cardene.dev/diagrams/canton-traffic-accounts
- 公開日: 2026-11-23
- 更新日: 2026-11-23
- トピック: Blockchain
- タグ: Canton
- 親の図解: [Ledger APIの保守のサービス](https://cardene.dev/diagrams/canton-ledger-api-maintenance.md): Ledger APIが持つ保守のサービスを、コマンドの状態の調べ方、pruning、版と機能、時刻、trafficの順に、gRPCでどう呼ぶかを4頁で追います。

頁「ノードを保守するRPCを使い分ける」では、保守のRPCを、サービスごとのパスと要求と応答の欄で並べました。
この頁は、そのうちgRPCでtrafficのアカウントを扱う`TrafficService`の4つのRPCを、アカウントの残高を読み、増減させ、古いイベントを刈り込む順に追います。
RPCは、ネットワーク越しに、別のプログラムの処理を呼び出す操作です。
頁「trafficのアカウントを読み足す」は、trafficのアカウントの意味と、`TrafficService`をLedger APIが公開する条件を追っています。

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

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

## TrafficServiceは4つのRPCを持つ

`TrafficService`は、Ledger APIの参照の「Other APIs」に入る、`com.digitalasset.canton.tea.v1`のパッケージのサービスです。
パッケージは、関連する定義をまとめた単位です。
パッケージは、1つのソースのファイルに、1つのサービスと4つのエンドポイントと8つのメッセージを持ち、列挙型を持ちません。

ソースのファイルは、`community/traffic-enforcement/api/protobuf/com/digitalasset/canton/tea/v1/traffic_service.proto`です。
パスと型の一覧は、公式の参照が比較した版のうち、パッケージの定義を最後に確認できる版の物です。

サービスは、`GetAccount`と`Prune`と`PruneEvents`と`UpdateAccount`の4つのRPCを持ち、どれもクライアントもサーバーも、続けて何度もデータを送るストリーミングを使いません。
各RPCの頁は、そのRPCを含む最後の版の定義を載せます。
次の表は、4つのRPCのパスと、足された版と、要求と応答の欄です。

| RPC | パス | 足された版 | 要求の欄 | 応答の欄 |
|---|---|---|---|---|
| `GetAccount` | `/com.digitalasset.canton.tea.v1.TrafficService/GetAccount` | 3.5.15 | 文字列の`account_id` | 文字列の`account_id`と、int64の`balance` |
| `UpdateAccount` | `/com.digitalasset.canton.tea.v1.TrafficService/UpdateAccount` | 3.5.15 | 文字列の`account_id`と、int64の`balance_delta`と、文字列の`deduplication_id` | `GetAccountResponse`を持つ`response` |
| `PruneEvents` | `/com.digitalasset.canton.tea.v1.TrafficService/PruneEvents` | 3.6.1 | Timestampの`before_or_at` | int32の`pruned_event_count`だけ |
| `Prune` | `/com.digitalasset.canton.tea.v1.TrafficService/Prune` | 3.7.0 | Timestampの`before_or_at` | int32の`pruned_event_count`と`pruned_correlation_count` |

Timestampは時刻の型で、int32とint64は符号付きの整数の型です。
`PruneEvents`は、Canton 3.7.0で非推奨になり、後継は`Prune`です。
`PruneEvents`は、3.8で既定で無効になり、3.9で外されます。

`PruneEvents`の応答は、`Prune`と違って、`pruned_correlation_count`を持ちません。
`UpdateAccount`の応答は、`GetAccountResponse`を`response`の欄に入れて返すので、`GetAccount`の応答と同じ2つの欄が、1段深く入ります。

## 4つのRPCの欄を見る

図Aは、中央に`TrafficService`を置き、4つのRPCと、それぞれの要求と応答の欄を足します。
段1が4つのRPCを置き、段2が`GetAccount`の欄を、段3が`UpdateAccount`の欄を、段4が`PruneEvents`の欄を、段5が`Prune`の欄を足します。
欄の字は、要求と応答のどちらの欄かを、先頭の語で示します。

```dragon
mind TrafficServiceの4つのRPCの欄

step 4つのRPC
  say TrafficServiceは、4つのRPCを持ちます。
  idea root as "TrafficService" [color: slate]
  idea ga as "GetAccount" of root [color: blue]
  idea ua as "UpdateAccount" of root [color: blue]
  idea pe as "PruneEvents" of root [color: blue]
  idea pr as "Prune" of root [color: blue]
  source CantonのTrafficService [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-digitalasset-canton-tea-v1]

step GetAccountの欄
  say GetAccountの要求は、アカウントのIDを1つ持ち、応答は、アカウントのIDと残高を返します。
  idea ga1 as "要求のaccount_id" of ga [color: blue]
  idea ga2 as "応答のaccount_id" of ga [color: blue]
  idea ga3 as "応答のbalance" of ga [color: blue]
  source CantonのGetAccount [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-digitalasset-canton-tea-v1/trafficservice/getaccount]

step UpdateAccountの欄
  say UpdateAccountの要求は、3つの欄を持ち、応答は、GetAccountの応答を、responseの欄に入れて返します。
  idea ua1 as "要求のaccount_id" of ua [color: blue]
  idea ua2 as "要求のbalance_delta" of ua [color: blue]
  idea ua3 as "要求のdeduplication_id" of ua [color: blue]
  idea ua4 as "応答のresponse" of ua [color: blue]
  source CantonのUpdateAccount [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-digitalasset-canton-tea-v1/trafficservice/updateaccount]

step PruneEventsの欄
  say PruneEventsの要求は時刻を1つ持ち、応答は数を1つ返します。PruneEventsは非推奨で、後継がPruneです。
  idea pe1 as "要求のbefore_or_at" of pe [color: blue]
  idea pe2 as "応答のpruned_event_count" of pe [color: blue]
  source CantonのPruneEvents [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-digitalasset-canton-tea-v1/trafficservice/pruneevents]

step Pruneの欄
  say Pruneの要求は時刻を1つ持ち、応答は2つの数を返します。
  idea pr1 as "要求のbefore_or_at" of pr [color: blue]
  idea pr2 as "応答のpruned_event_count" of pr [color: blue]
  idea pr3 as "応答のpruned_correlation_count" of pr [color: blue]
  source CantonのPrune [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-digitalasset-canton-tea-v1/trafficservice/prune]
```

## 残高の取得と更新

`GetAccount`は、アカウントのIDを送り、アカウントのIDと残高を受け取ります。
`UpdateAccount`は、アカウントのIDと、残高の増減を示す`balance_delta`と、`deduplication_id`を送ります。
`balance_delta`の意味と、`deduplication_id`の使い方は、頁「trafficのアカウントを読み足す」が追っています。
`UpdateAccount`の応答は、更新の後のアカウントのIDと残高を、`response`の中に返します。

`grpcurl`は、gRPCのサーバーのRPCを、JSONで呼び出すための道具です。
`grpcurl`の例は、接続先のホスト名とポートを、コロンでつないだ値にして、シェルの変数`HOST_PORT`に設定してから実行します。
変数は引用符で囲んで渡し、TLSを使わないサーバーを呼ぶ時は、`-plaintext`を足します。

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

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

`GetAccount`の例は、接続先と、サービス名とRPC名と、JSONの要求を、1つの呼び出しに組み合わせた形です。
JSONの要求は、`-d @`と、heredocの`<<'EOF'`で、コマンドの標準入力から渡します。

```bash
# Add -plaintext if the server is not using TLS.
grpcurl \
  -d @ \
  "$HOST_PORT" \
  com.digitalasset.canton.tea.v1.TrafficService/GetAccount <<'EOF'
{
  "accountId": "string"
}
EOF
```

`UpdateAccount`の例は、`accountId`と`balanceDelta`と`deduplicationId`を送ります。
`balanceDelta`は、int64の値を文字列の`"0"`で表します。
OKの応答の例は、`response`の中に`accountId`と`balance`を返し、`balance`も`"0"`の文字列です。
`GetAccount`の応答の例も、`balance`を文字列の`"0"`で返します。

| RPC | 例の要求 | 例の応答 |
|---|---|---|
| `GetAccount` | `{"accountId": "string"}` | `{"accountId": "string", "balance": "0"}` |
| `UpdateAccount` | `{"accountId": "string", "balanceDelta": "0", "deduplicationId": "string"}` | `{"response": {"accountId": "string", "balance": "0"}}` |

この例の`"string"`と`"0"`は、欄の型を示す仮の値で、実際のアカウントのIDと残高ではありません。
頁「trafficのアカウントを読み足す」のHTTPのJSON APIの例は、`balanceDelta`と`balance`を数で書きます。
この頁の`grpcurl`の例は、int64をJSONの文字列`"0"`で表示するので、2つの例の見え方は違います。

## 古いイベントを刈り込む

`Prune`は、イベントと、送信の要求の対応を刈り込みます。
`PruneEvents`は、Canton 3.7.0で非推奨になった操作で、後継は`Prune`です。
どちらもTimestampの`before_or_at`を送ります。

`Prune`に渡す時刻は、呼び出した時点から、少なくともParticipant Nodeの最大の重複除けの期間だけ過去に置きます。
条件を満たさない時刻は、`TRAFFIC_UNSAFE_TO_PRUNE`で失敗します。
刈り込むのは、完了した送信の要求の対応だけにします。

進行中の送信の要求の対応を刈り込むと、その送信の要求のtrafficの数えに影響します。
消す前に、Participant Nodeのバックアップを取っておきます。
頁「trafficのアカウントを読み足す」が、これらの条件を追っています。

次の例の要求は、`beforeOrAt`に時刻の文字列を入れる形です。
表の`"string"`は、Timestampの欄を示す仮の値です。
呼ぶ時は、上の条件を満たす実際の時刻に置き換えます。

| RPC | 例の要求 | 例の応答 |
|---|---|---|
| `Prune` | `{"beforeOrAt": "string"}` | `{"prunedEventCount": 0, "prunedCorrelationCount": 0}` |
| `PruneEvents` | `{"beforeOrAt": "string"}` | `{"prunedEventCount": 0}` |

`Prune`の応答の例は、`prunedEventCount`と`prunedCorrelationCount`を数で返し、`PruneEvents`の応答の例は、`prunedEventCount`だけを数で返します。
応答の2つの数は、int32で、例では数のまま書かれ、文字列にしません。

## 呼び出しを順に追う

図Bは、呼び出す側とLedger APIのサーバーの2つの箱で、4つのRPCの要求と応答を、1つのRPCにつき1つの段で追います。
呼び出す側は、例の`grpcurl`です。
段は、残高を読む所から、イベントを刈り込む所までの並びに置きますが、この図はRPCを呼ぶ順の決まりを示しません。

図で読み書きの対象になるのは残高で、刈り込みの対象は、イベントと送信の要求の対応です。
どの段も、別の呼び出しの始まりです。

```dragon
sequence 残高を読み書きしイベントを刈り込む

box G as 呼び出す側 [note: gRPCの呼び出しを送る, color: slate, icon: exe]
box S as Ledger APIのサーバー [note: ホスト名とポートで指定する, color: rose, icon: server]

step 残高を読む
  say 残高を読むRPCは、アカウントのIDを送り、IDと残高を受け取ります。
  G -> S: アカウントのID
  S -> G: IDと残高
  source CantonのGetAccount [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-digitalasset-canton-tea-v1/trafficservice/getaccount]

step 残高を増減させる
  say 増減させるRPCは、アカウントのIDと、残高の差分と、重複を防ぐIDを送り、更新の後のIDと残高を、responseの中に受け取ります。
  G -> S: IDと差分と重複除けのID
  S -> G: IDと更新後の残高
  source CantonのUpdateAccount [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-digitalasset-canton-tea-v1/trafficservice/updateaccount]

step イベントだけを刈り込む
  say 非推奨になった3.6.1のRPCは、時刻を送り、刈り込んだイベントの数だけを受け取ります。
  G -> S: 時刻
  S -> G: 刈り込んだイベントの数
  source CantonのPruneEvents [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-digitalasset-canton-tea-v1/trafficservice/pruneevents]

step イベントと対応を刈り込む
  say 3.7.0で足された後継のRPCは、時刻を送り、刈り込んだイベントの数と、対応の数を受け取ります。
  G -> S: 時刻
  S -> G: 刈り込んだ2つの数
  source CantonのPrune [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-digitalasset-canton-tea-v1/trafficservice/prune]
```

## 次に読む頁

次の頁「試験用の台帳の時刻を動かす」は、`TimeService`の2つのRPCで、試験用の台帳の今の時刻を読み、時刻を設定する呼び方を追います。

https://cardene.dev/diagrams/canton-lapi-testing-time

## 出典

https://docs.canton.network/reference/grpc-ledger-api-reference/com-digitalasset-canton-tea-v1

https://docs.canton.network/reference/grpc-ledger-api-reference/com-digitalasset-canton-tea-v1/trafficservice/getaccount

https://docs.canton.network/reference/grpc-ledger-api-reference/com-digitalasset-canton-tea-v1/trafficservice/prune

https://docs.canton.network/reference/grpc-ledger-api-reference/com-digitalasset-canton-tea-v1/trafficservice/pruneevents

https://docs.canton.network/reference/grpc-ledger-api-reference/com-digitalasset-canton-tea-v1/trafficservice/updateaccount

https://docs.canton.network/global-synchronizer/release-notes/canton-releases/3-7-0

https://docs.canton.network/global-synchronizer/production-operations/local-traffic-management
