Skip to content

feat(node): self-defense disputeChannel on stale channel close (#1586) - #1593

Merged
alpergundogdu merged 1 commit into
mainfrom
claude/priceless-dirac-9979d4
Aug 4, 2026
Merged

alpergundogdu merged 1 commit into
mainfrom
claude/priceless-dirac-9979d4

Conversation

@alpergundogdu

@alpergundogdu alpergundogdu commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #1586. Today the daemon only observes ChannelCloseInitiated — it reconciles the pending-settle obligation but never disputes. If a counterparty closes one of this node's channels at a stale watermark (an older voucher, or none at all via closeChannelWithoutVoucher), the node's higher-nonce voucher is stranded and the un-redeemed residual settles to the counterparty's benefit.

This adds a lightweight in-process self-defense reaction — not the withdrawn third-party cdn/watchtower/v1 protocol (#324, closed/out of scope). In the existing ChannelCloseInitiated arm, after reconcile, the watcher reads the on-chain close watermark via getChannel and, when the node's persisted voucher nonce is strictly higher AND the recoverable residual (myAmount − onchainClaimedAmount) clears an estimated gas-cost floor, submits disputeChannel inside the dispute window. It is best-effort and benign-revert tolerant — it never fails the watcher tick.

Changes

  • decdn-incentive: add the disputeChannel binding + ChannelDisputed event (previously intentionally omitted); update module docs.
  • Decision logic: pure should_dispute — strictly-higher nonce + residual >= floor, with an explicit zero-residual guard so a 0 floor still can't trigger a zero-recovery dispute.
  • Handler wiring: maybe_dispute_stale_close in the ChannelCloseInitiated arm, reusing the send_and_await_receipt path; honors the dispute window (unix_now() skip + contract backstop).
  • Config: governance-tunable settlement_dispute_min_residual_micro_usdc (default 0.1 USDC; 0 disables the floor) threaded through types → resolved → runtime bootstrap, surfaced in decdn config summary + init template.
  • Metrics: settlement_dispute_ok / settlement_dispute_failures counters.
  • ClientHandler::channel_state_snapshot: reads the latest persisted voucher for the dispute path.

Test coverage

  • Unit (should_dispute): dispute fires on stale close, no-op on honest close (equal and higher nonce), no-op when residual < floor, plus a zero-residual guard case.
  • Metrics: start-at-zero-and-increment for the two new counters.
  • e2e (anvil settlement): channel 2 now closes stale (closeChannelWithoutVoucher) and asserts the watcher reacts with disputeChannel, ratcheting claimedNonce/claimedAmount up to the node's persisted voucher before settle.

Verification

  • cargo build && cargo clippy (lib + --all-targets) — clean
  • cargo nextest run -p decdn-node -p decdn-incentive -p decdn-common -p decdn-cli — 2022 passed
  • cargo test --no-run -p decdn-e2e -p decdn-node --features anvil-e2e — compiles
  • pre-commit (fmt, clippy, doc, deny) — passed

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added self-defense dispute mechanism enabling automatic responses to stale channel closes for residual recovery.
    • Settlement service now actively reconciles pending obligations and reacts to stale closes.
  • Configuration

    • New configuration option to set minimum residual threshold before submitting disputes (default: 0.1 USDC).
  • Monitoring

    • Added metrics tracking settlement dispute attempts and outcomes.

Copilot AI lite review requested due to automatic review settings August 4, 2026 12:54
@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown

Coverage report

Total: 80.26% (was 80.30%, ▼ 0.05%) — 56738/70697 lines

File Coverage Lines Δ
crates/cli/src/commands/config.rs 84.86% 454/535 ▲ +0.04%
crates/common/src/config/mod.rs 97.13% 6946/7151 ● 0.00%
crates/common/src/config/resolved.rs 100.00% 33/33 ● 0.00%
crates/node/src/handlers/client/mod.rs 89.56% 738/824 ▼ 0.53%
crates/node/src/metrics.rs 95.67% 1105/1155 ▲ +0.07%
crates/node/src/payment_settlement.rs 57.47% 1077/1874 ▼ 0.89%
crates/node/src/runtime/mod.rs 46.61% 1092/2343 ● 0.00%
crates/node/src/runtime/reload.rs 96.40% 1658/1720 ● 0.00%
History
Commit Total Δ vs base
bd17f2f (HEAD) 80.26% ▼ 0.05%

@coderabbitai

coderabbitai Bot commented Aug 4, 2026 •

Copy link
Copy Markdown

Review Change Stack

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Free

Run ID: e27c5df0-12fe-433d-8364-157fbec7526c

📥 Commits

Reviewing files that changed from the base of the PR and between 38c0d47 and 37944e9.

📒 Files selected for processing (14)
  • crates/cli/src/commands/config.rs
  • crates/cli/tests/config_validate.rs
  • crates/common/src/config/mod.rs
  • crates/common/src/config/resolved.rs
  • crates/common/src/config/types.rs
  • crates/incentive/src/payment_channel.rs
  • crates/node/src/handlers/client/mod.rs
  • crates/node/src/metrics.rs
  • crates/node/src/payment_settlement.rs
  • crates/node/src/runtime/mod.rs
  • crates/node/src/runtime/reload.rs
  • crates/node/tests/anvil_bringup_shutdown_e2e.rs
  • crates/node/tests/anvil_settlement_e2e.rs
  • crates/node/tests/sighup_signal.rs

Walkthrough

This PR adds a self-defense dispute mechanism for stale payment channel closes. It introduces a disputeChannel contract function and ChannelDisputed event, a configurable minimum-residual threshold, settlement-watcher decision and submission logic, new metrics counters, and supporting configuration and test wiring.

Changes

Self-defense dispute mechanism

Layer / File(s) Summary
Contract interface: disputeChannel and ChannelDisputed
crates/incentive/src/payment_channel.rs
Adds the disputeChannel function and ChannelDisputed event to the PaymentChannel contract interface. Updates documentation on signature formats and settlement watcher behavior to reflect active dispute reconciliation.
Configuration schema and CLI validation
crates/common/src/config/types.rs, crates/common/src/config/resolved.rs, crates/cli/src/commands/config.rs, crates/cli/tests/config_validate.rs
Adds settlement_dispute_min_residual_micro_usdc to BlockchainConfig and ResolvedBlockchain. Surfaces the field in the CLI config validate summary, the default template, the template coverage test, and a CLI test fixture.
Settlement watcher dispute decision and submission
crates/node/src/payment_settlement.rs, crates/node/src/handlers/client/mod.rs
Adds a pure should_dispute decision function and an async maybe_dispute_stale_close submission routine bounded by a receipt timeout. Wires the call into the ChannelCloseInitiated handler after reconciliation, adds a dispute_min_residual field to SettlementSink, adds unit tests for the decision logic, and adds a ClientHandler::channel_state_snapshot accessor.
Dispute metrics counters
crates/node/src/metrics.rs
Adds settlement_dispute_ok and settlement_dispute_failures counters with recorder methods and a test verifying zero-start and independent increments.
Bootstrap and test-fixture wiring for dispute residual
crates/node/src/runtime/mod.rs, crates/node/src/runtime/reload.rs, crates/node/tests/anvil_bringup_shutdown_e2e.rs, crates/node/tests/sighup_signal.rs
Threads the new residual configuration value into PaymentChannelService::bootstrap and updates ResolvedBlockchain test fixtures across the reload, bringup, and sighup tests.
Anvil settlement e2e dispute scenario
crates/node/tests/anvil_settlement_e2e.rs
Configures a dispute residual floor, creates a stale zero-claim watermark for a test channel, polls for watcher-driven dispute and claim ratcheting, and passes the residual floor into multiple settlement service bootstraps in the test.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Watcher as SettlementSink
  participant Decision as should_dispute
  participant Submitter as maybe_dispute_stale_close
  participant Contract as PaymentChannel

  Watcher->>Watcher: receive ChannelCloseInitiated event
  Watcher->>Watcher: reconcile_closing_channel
  Watcher->>Submitter: maybe_dispute_stale_close
  Submitter->>Decision: should_dispute(voucher, onchain watermark, min_residual)
  Decision-->>Submitter: Some(residual) or None
  alt residual meets threshold and nonce is higher
    Submitter->>Contract: disputeChannel(channelId, amount, nonce, bytesDelivered, signature)
    Contract-->>Submitter: ChannelDisputed event or revert or timeout
  end
Loading

Note

🎁 Summarized by CodeRabbit Free

Your organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login.

Comment @coderabbitai help to get the list of available commands.

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 an in-process “self-defense” reaction in the node’s seller-side settlement watcher to dispute a channel close when the counterparty initiates closing at a stale watermark (older/no voucher), preventing residual value from being stranded and benefiting the counterparty.

Changes:

  • Implements a pure dispute decision (should_dispute) and wires a best-effort disputeChannel submission into the ChannelCloseInitiated handler (maybe_dispute_stale_close).
  • Extends decdn-incentive PaymentChannel bindings/docs to include disputeChannel and the ChannelDisputed event.
  • Threads a governance-tunable config floor (settlement_dispute_min_residual_micro_usdc) through config types/resolution/runtime and adds metrics + unit/e2e coverage.

Reviewed changes

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

Show a summary per file
File Description
crates/node/src/payment_settlement.rs Adds dispute decision logic and watcher-side disputeChannel submission on stale closes.
crates/incentive/src/payment_channel.rs Extends contract bindings and module docs with disputeChannel + ChannelDisputed.
crates/node/src/handlers/client/mod.rs Adds channel_state_snapshot to read the latest persisted voucher state for disputes.
crates/common/src/config/types.rs Introduces the new dispute residual floor config field (typed).
crates/common/src/config/mod.rs Defaults and resolves settlement_dispute_min_residual_micro_usdc (always-on reaction).
crates/common/src/config/resolved.rs Adds resolved value for dispute residual floor to the runtime config.
crates/node/src/runtime/mod.rs Wires resolved dispute residual floor into settlement service bootstrap.
crates/node/src/runtime/reload.rs Updates test config seeds to include the new resolved field.
crates/node/src/metrics.rs Adds settlement_dispute_ok / settlement_dispute_failures counters + tests.
crates/cli/src/commands/config.rs Surfaces the resolved setting in config summary and template coverage tests.
crates/cli/tests/config_validate.rs Updates resolved-config seed to include the new field.
crates/node/tests/anvil_settlement_e2e.rs Expands e2e to close a channel stale and assert watcher disputes and ratchets claim.
crates/node/tests/anvil_bringup_shutdown_e2e.rs Updates test config seed to include the new resolved field.
crates/node/tests/sighup_signal.rs Updates test config seed to include the new resolved field.

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

Comment thread crates/node/src/payment_settlement.rs Resolved
The settlement watcher previously only observed `ChannelCloseInitiated` and
reconciled the pending-settle obligation. If a counterparty closed one of this
node's channels at a stale watermark (an older voucher, or none at all via
`closeChannelWithoutVoucher`), the node's higher-nonce voucher was stranded —
the un-redeemed residual settled to the counterparty's benefit.

Add a lightweight in-process reaction (not the withdrawn third-party
`cdn/watchtower/v1`, #324): in the existing `ChannelCloseInitiated` arm, after
reconcile, read the on-chain close watermark via `getChannel` and, when the
node's persisted voucher nonce is strictly higher AND the recoverable residual
(`myAmount - onchainClaimedAmount`) clears an estimated gas-cost floor, submit
`disputeChannel` inside the dispute window. Best-effort and benign-revert
tolerant — never fails the watcher tick.

- add the `disputeChannel` binding + `ChannelDisputed` event to
  `decdn-incentive` (previously intentionally omitted)
- pure `should_dispute` decision (strictly-higher nonce + residual >= floor,
  zero-residual guarded) with unit coverage for all three cases
- governance-tunable `settlement_dispute_min_residual_micro_usdc` (default
  0.1 USDC; 0 disables the floor) threaded through config -> runtime bootstrap
- `settlement_dispute_ok` / `settlement_dispute_failures` metrics
- `ClientHandler::channel_state_snapshot` to read the latest persisted voucher
- e2e: channel 2 now closes stale and asserts the watcher ratchets the claim
  up to the node's voucher (anvil settlement e2e)

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@alpergundogdu
alpergundogdu force-pushed the claude/priceless-dirac-9979d4 branch from bd17f2f to 37944e9 Compare August 4, 2026 14:16
@alpergundogdu
alpergundogdu merged commit 8fb359f into main Aug 4, 2026
23 of 24 checks passed
@alpergundogdu
alpergundogdu deleted the claude/priceless-dirac-9979d4 branch August 4, 2026 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In-process conditional dispute monitor (provider self-defense)

2 participants