Skip to content

feat(cli): add decdn node channels payment-channel snapshot (#749) - #791

Merged
thiras merged 5 commits into
mainfrom
feat/749-node-channels-cli
Jun 3, 2026
Merged

thiras merged 5 commits into
mainfrom
feat/749-node-channels-cli

Conversation

@thiras

@thiras thiras commented Jun 2, 2026 •

Copy link
Copy Markdown
Contributor

Closes #749

Adds a decdn node channels subcommand and the backing admin_v1_channels RPC that print a live snapshot of every open payment channel. Per channel: last-accepted nonce, outstanding accrued claim (micro-USDC), escrowed deposit, time since the last voucher, and settlement eligibility. Mirrors the existing decdn node status / decdn node peers admin-RPC surface.

What changed

  • crates/common/src/admin.rs — ChannelSnapshot / ChannelsResponse wire DTOs (owned String/u64/bool, no internal handles), a channels() method on the AdminRpc trait, CHANNEL_STORE_ERROR_CODE, plus serde round-trip + legacy-default tests.
  • crates/node/src/admin.rs — ChannelStatusHandles (channel-state store + voucher-activity clock + redeem threshold) attached via a with_dht-style with_channels builder so the ~15 AdminState::new call sites are untouched. The handler loads the store off the blocking pool (the store trait is sync), builds snapshots via the pure, unit-tested build_channel_snapshots, and computes settlement_eligible as outstanding >= redeem_threshold.
  • crates/incentive/src/voucher_activity.rs (new) — in-memory VoucherActivity clock, non-durable by design, shared between the cdn/client/v1 handler (stamps on each accepted voucher) and the admin snapshot (reports seconds-since).
  • crates/node/src/handlers/client.rs + runtime/mod.rs — wire the activity clock (writer) and the channel handles (reader) into the existing store/handler graph.
  • crates/common/src/cli/node.rs + crates/cli/src/commands/node.rs — Channels(ChannelsArgs) subcommand + handler with a grep-friendly plain-text table and --json, reusing the admin-client/URL-resolution helpers.

Design notes

  • Settlement eligibility is an upper bound. The admin surface does not read the on-chain withdrawnAmount, so it compares the full accrued claim against the threshold (documented on the DTO field). >= matches the redeemer's < threshold short-circuit in payment_settlement.rs.
  • Time-since-voucher is in-memory. ChannelState persists no last-voucher wall-clock, so a channel hydrated at boot reports None until its next voucher (rendered "never"). This is the correct shape for a current-liveness diagnostic.
  • Empty/unwired channel subsystem returns an empty list (not an error); store-load failure surfaces as CHANNEL_STORE_ERROR_CODE with an error! log.

Review

Self-review applied the code-reviewer, silent-failure-hunter, pr-test-analyzer, type-design-analyzer, and comment-analyzer lenses to the diff.

  • Addressed (1): comment-analyzer — the counterparty DTO doc claimed "lowercase hex", but alloy::Address Display is EIP-55 mixed-case checksummed (verified in the crate source). Corrected the doc and contrasted it with PeerView::node_id (plain lowercase iroh key).
  • Declined / noted (1): settlement_eligible is recomputable client-side from threshold + outstanding, but pre-computing server-side directly answers the issue's ask and is documented as an upper bound.

Post-review follow-up (commit f417cc2)

A second multi-agent review found the diff CLEAN on correctness but surfaced one load-bearing type-design issue and one real test gap; both are now fixed:

  • Memory leak (load-bearing, fixed). VoucherActivity.touch inserted per ChannelId with no eviction, so a channel that closed on-chain (removed from the live map) left its Instant lingering for the whole process lifetime — a slow leak on a long-running, high-churn node. Added a panic-free, idempotent VoucherActivity::forget (poisoned lock = no-op, same safe degradation as touch/seconds_since) and call it from ClientHandler::forget_channel, the single site that drops a closed channel from the live map (both ChannelSettled callers funnel through it). Unit tests cover removal + the unknown-channel no-op.
  • Wiring tests (gap, fixed). The two one-liners wiring the clock (attach_voucher_activity + the touch in the accept path) had zero test callers, so a dropped/mis-placed stamp — or the writer and the channels() reader holding different Arcs — would silently report seconds_since_last_voucher: None ("never") forever. Added: a client_loopback test where a real signed-voucher accept through the live handler advances the shared Arc<VoucherActivity> (crit 7); and an admin test where a touch on the same Arc the channels() reader holds surfaces as Some(age) and sorts the active channel first (crit 6). This supersedes the earlier "no dedicated accept→clock test" note.
  • MicroUsdc newtype (optional, declined-for-scope). Wrapping the three DTO amount fields in a #[serde(transparent)] MicroUsdc(u64) would keep the wire bytes identical, but it is not low-churn: the *_micro_usdc unit suffix already disambiguates the amounts from last_nonce, and the newtype would force .0/MicroUsdc(..) conversions across ~99 references in 8 files (config types, resolved config, runtime, reload, CLI) where micro-USDC flows as u64. Declined.

Verification

  • cargo build, cargo clippy --workspace --all-targets — clean (no warnings).
  • cargo nextest run -p decdn-cli -p decdn-common -p decdn-node -p decdn-incentive — 1007 passed, 0 failed (4 new tests).
  • cargo fmt -- --check — clean. Pre-commit hooks (fmt, clippy, doc, deny) all passed.

🤖 Generated with Claude Code

Add a `decdn node channels` subcommand and the backing `admin_v1_channels`
RPC that print a live snapshot of every open payment channel: per channel
the last-accepted nonce, outstanding accrued claim (micro-USDC), escrowed
deposit, time since the last voucher, and settlement eligibility.

- common: `ChannelSnapshot` / `ChannelsResponse` wire DTOs + `channels()`
  RPC method on the `AdminRpc` trait, `CHANNEL_STORE_ERROR_CODE`, and serde
  round-trip / legacy-default tests.
- node admin: `ChannelStatusHandles` (channel store + voucher-activity clock
  + redeem threshold) attached via `with_dht`-style `with_channels`; the
  handler loads the store off the blocking pool, builds snapshots, and
  computes `settlement_eligible` as `outstanding >= redeem_threshold`
  (upper bound — the admin surface has no on-chain `withdrawnAmount`).
- incentive: new in-memory `VoucherActivity` clock (non-durable, resets on
  restart) shared between the `cdn/client/v1` handler (stamps on each
  accepted voucher) and the admin snapshot (reports seconds-since).
- cli: `Channels(ChannelsArgs)` subcommand + handler with a grep-friendly
  plain-text table and `--json`, mirroring `node status` / `node peers`.

Settlement eligibility uses `>=` to match the redeemer's `< threshold`
short-circuit. Time-since-voucher is `None` after a restart until the next
voucher, since `ChannelState` persists no last-voucher wall-clock.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings June 2, 2026 19:26

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds the decdn node channels CLI command and the admin_v1_channels RPC endpoint to allow operators to inspect open payment channels and track voucher activity. The review feedback suggests reusing the existing format_age helper to avoid code duplication and simplifying the channel snapshot sorting logic using sort_unstable_by_key.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread crates/cli/src/commands/node.rs Resolved
Comment thread crates/cli/src/commands/node.rs Resolved
Comment thread crates/cli/src/commands/node.rs Resolved
Comment thread crates/node/src/admin.rs Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds operator-facing payment-channel introspection by extending the admin JSON-RPC surface with admin_v1_channels and exposing it via a new decdn node channels CLI subcommand, including an in-memory “time since last voucher” signal.

Changes:

  • Introduces ChannelSnapshot / ChannelsResponse DTOs and an AdminRpc::channels() method (plus error code) on the common admin wire surface.
  • Wires a shared in-memory VoucherActivity clock into the client voucher-acceptance path (writer) and admin channels snapshot (reader).
  • Implements the node-side admin_v1_channels handler and the CLI renderer (--json or grep-friendly table), with unit + integration tests.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
crates/node/src/runtime/mod.rs Wires VoucherActivity and attaches channel-status handles into the admin state builder.
crates/node/src/handlers/client.rs Stamps VoucherActivity on successful voucher acceptance (best-effort).
crates/node/src/admin.rs Adds channel introspection handles, implements admin_v1_channels, and unit-tests snapshot building + RPC behavior.
crates/incentive/src/voucher_activity.rs New in-memory per-channel last-voucher clock with unit tests.
crates/incentive/src/lib.rs Exposes voucher_activity module and re-exports VoucherActivity.
crates/common/src/cli/node.rs Adds Channels subcommand + args to the shared CLI definition.
crates/common/src/cli/mod.rs Re-exports ChannelsArgs from the CLI module.
crates/common/src/admin.rs Defines wire DTOs, adds admin RPC method + error code, and serde/back-compat tests.
crates/cli/tests/admin_peers.rs Adds HTTP integration tests for admin_v1_channels plus CLI error-path test.
crates/cli/src/commands/node.rs Implements decdn node channels command, table formatting, and unit tests for formatting helpers.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/node/src/admin.rs Resolved
@github-actions

github-actions Bot commented Jun 2, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 86.68% (was 86.60%, ▲ +0.08%) — 27076/31236 lines

File Coverage Lines Δ
crates/cli/src/commands/node.rs 84.37% 939/1113 ▲ +2.03%
crates/common/src/admin.rs 95.48% 148/155 ▲ +2.63%
crates/incentive/src/voucher_activity.rs 100.00% 53/53 new
crates/node/src/admin.rs 94.97% 1076/1133 ▼ 1.55%
crates/node/src/handlers/client.rs 76.76% 459/598 ▼ 0.11%
crates/node/src/runtime/mod.rs 44.78% 515/1150 ▼ 0.27%
History
Commit Total Δ vs base
90c7ece 86.63% ▲ +0.05%
f417cc2 86.65% ▲ +0.08%
43c389b 86.64% ▲ +0.06%
98a2786 86.66% ▲ +0.08%
8fbf5e3 (HEAD) 86.68% ▲ +0.08%

thiras and others added 2 commits June 3, 2026 01:48
…ck wiring (#791 review)

Multi-agent review of #791 found the new VoucherActivity clock CLEAN on
correctness but surfaced one type-design leak and one test gap.

LOAD-BEARING: VoucherActivity.touch inserted per ChannelId with no eviction.
On-chain channel close removes the channel from the live map but left its
Instant lingering for the process lifetime -- a slow leak on a long-running,
high-churn node. Add panic-free, idempotent VoucherActivity::forget (poisoned
lock = no-op, same safe degradation as touch/seconds_since) and call it from
ClientHandler::forget_channel, the single site that drops a closed channel
from the live map (both ChannelSettled callers funnel through it).

TEST GAP: the two one-liners wiring the clock (attach_voucher_activity + the
touch in the accept path) had zero test callers, so a dropped/mis-placed stamp
or the writer and reader holding different Arcs would silently report
seconds_since_last_voucher: None forever. Add:
  - client_loopback: a real signed-voucher accept through the live handler
    advances the shared Arc<VoucherActivity> (crit 7);
  - admin: a touch on the SAME Arc the channels() reader holds surfaces as
    Some(age) and sorts the active channel first (crit 6).
Plus VoucherActivity::forget unit tests (removes a stamped channel; unknown
channel no-op).

MicroUsdc newtype (optional #3): declined-for-scope -- the *_micro_usdc unit
suffix already disambiguates the three amount fields from last_nonce, and a
newtype on the DTO fields alone would force .0/MicroUsdc(..) conversions across
~99 references in 8 files (config types, resolved config, runtime, reload,
CLI) where micro-USDC flows as u64. Not low-churn.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…mplify sort, accurate JoinError log

- crates/cli: drop `format_age_secs`; the channels path now scales the
  whole-second wire value and reuses `format_age` (microsecond input) via
  `saturating_mul(1_000_000)`. Boundary buckets are identical and covered by
  `format_age_units`; the now-redundant `format_age_secs_units` test is removed.
- crates/node: `build_channel_snapshots` uses `sort_unstable_by_key` with the
  same total-order key tuple (no closure, no allocation), preserving ordering.
- crates/node: the channel-store `JoinError` log distinguishes cancellation
  from panic (`is_cancelled`) so a shutdown-time cancel isn't logged as a panic.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

@alpergundogdu alpergundogdu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review — decdn node channels snapshot

Solid, mergeable. No correctness, anti-panic, concurrency, or wire-compat blockers found. The wire DTO is cleanly separated from internal ChannelState (owned String/u64/bool), the sync store read is correctly offloaded with spawn_blocking, poison handling uses lock().ok()?/into_inner, and the with_channels builder keeps the ~15 AdminState::new call sites untouched.

Verified-correct design points worth confirming explicitly (they look surprising but are intentional and documented):

  • settlement_eligible compares the full accrued last_amount against the threshold (outstanding >= threshold), not the redeemer's un-redeemed delta (last_amount - withdrawnAmount < threshold). The admin surface deliberately never reads on-chain withdrawnAmount, so this is an upper-bound liveness signal — documented on the DTO field. Good.
  • forget_channel drops the async channels lock before activity.forget() / spawn_blocking — no lock held across .await.
  • The (is_none(), age, Reverse(outstanding)) sort key correctly orders active channels first, then by recency, then by claim size.

Test coverage is unusually strong — the shared-Arc wiring guard and the real signed-voucher loopback test (proving the accept path actually calls touch through the same Arc the channels() reader holds) close the most likely silent-failure gap. The VoucherActivity::forget eviction fixing the per-ChannelId Instant leak on channel close is a genuine improvement.

Minor (non-blocking):

  • crates/cli/src/commands/node.rs format_usdc / column width: the {:>12} OUTSTANDING/DEPOSIT columns overflow for format_usdc(u64::MAX) (21 chars). This only occurs on the saturating arm for a value exceeding the on-chain deposit (documented as unreachable for a real channel), so it's purely cosmetic — widen the columns or leave as-is.
  • Optional test gaps: no assertion on the CLI --json output path, and the table-render test uses a lowercase counterparty rather than an EIP-55 mixed-case one (note the counterparty doc was correctly fixed to say checksummed). Both low-risk.

LGTM.


Generated by Claude Code

…ndering (#791, Alper review)

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@thiras

thiras commented Jun 3, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in 98a2786:

  • --json path (was untested): added channels_json_pretty_roundtrips_and_carries_fields, which exercises the exact serde_json::to_string_pretty(&ChannelsResponse) seam the --json branch uses — asserts the pretty (multi-line) encoding round-trips back through the DTO with no lossy field and carries every wire key/value (redeem_threshold_micro_usdc, per-channel counterparty/last_nonce/outstanding_micro_usdc/deposit_micro_usdc/seconds_since_last_voucher/settlement_eligible). Mirrors the peers --json test pattern; no shell-out.
  • EIP-55 counterparty in the table-render test: write_channels_table_renders_header_and_rows now uses a real checksummed address (derived via alloy::primitives::address!("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"), the canonical EIP-55 example) instead of the lowercase placeholder, matching the corrected counterparty doc ("EIP-55 mixed-case checksummed"). The test guards that the fixture is genuinely mixed-case and asserts the rendered row carries the checksummed short-form prefix verbatim, so a regression that lowercased the address is caught.

Not changed, by decision: the {:>12} OUTSTANDING/DEPOSIT column width / format_usdc(u64::MAX) overflow is left as-is. The 21-char value only occurs on the saturating arm where outstanding > deposit, which the DTO documents as unreachable for a real channel (bounded by the on-chain deposit); widening every row to 21 chars would degrade the common-case table layout.

Verified green: cargo build, cargo clippy --workspace --all-targets -- -D warnings, cargo nextest run -p decdn-cli -p decdn-node -p decdn-common, cargo fmt -- --check.

# Conflicts:
#	crates/node/src/handlers/client.rs
#	crates/node/tests/client_loopback.rs
@thiras
thiras merged commit ebc1100 into main Jun 3, 2026
19 checks passed
@thiras
thiras deleted the feat/749-node-channels-cli branch June 3, 2026 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants