Repository navigation
feat(node): self-defense disputeChannel on stale channel close (#1586) - #1593
Conversation
Coverage reportTotal: 80.26% (was 80.30%, ▼ 0.05%) — 56738/70697 lines
History
|
ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Free Run ID: 📒 Files selected for processing (14)
WalkthroughThis 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. ChangesSelf-defense dispute mechanism
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
Note 🎁 Summarized by CodeRabbit FreeYour 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 |
There was a problem hiding this comment.
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-effortdisputeChannelsubmission into theChannelCloseInitiatedhandler (maybe_dispute_stale_close). - Extends
decdn-incentivePaymentChannel bindings/docs to includedisputeChanneland theChannelDisputedevent. - 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.
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]>
bd17f2f to
37944e9
Compare
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 viacloseChannelWithoutVoucher), 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/v1protocol (#324, closed/out of scope). In the existingChannelCloseInitiatedarm, after reconcile, the watcher reads the on-chain close watermark viagetChanneland, when the node's persisted voucher nonce is strictly higher AND the recoverable residual (myAmount − onchainClaimedAmount) clears an estimated gas-cost floor, submitsdisputeChannelinside the dispute window. It is best-effort and benign-revert tolerant — it never fails the watcher tick.Changes
decdn-incentive: add thedisputeChannelbinding +ChannelDisputedevent (previously intentionally omitted); update module docs.should_dispute— strictly-higher nonce +residual >= floor, with an explicit zero-residual guard so a0floor still can't trigger a zero-recovery dispute.maybe_dispute_stale_closein theChannelCloseInitiatedarm, reusing thesend_and_await_receiptpath; honors the dispute window (unix_now()skip + contract backstop).settlement_dispute_min_residual_micro_usdc(default 0.1 USDC;0disables the floor) threaded throughtypes→resolved→ runtimebootstrap, surfaced indecdn configsummary + init template.settlement_dispute_ok/settlement_dispute_failurescounters.ClientHandler::channel_state_snapshot: reads the latest persisted voucher for the dispute path.Test coverage
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.closeChannelWithoutVoucher) and asserts the watcher reacts withdisputeChannel, ratchetingclaimedNonce/claimedAmountup to the node's persisted voucher before settle.Verification
cargo build && cargo clippy(lib +--all-targets) — cleancargo nextest run -p decdn-node -p decdn-incentive -p decdn-common -p decdn-cli— 2022 passedcargo test --no-run -p decdn-e2e -p decdn-node --features anvil-e2e— compiles🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Configuration
Monitoring