Skip to content

feat(cli): add decdn node bond — idempotent stake-to-tier (#932) - #934

Merged
alpergundogdu merged 1 commit into
mainfrom
claude/node-bond-932
Jun 17, 2026
Merged

alpergundogdu merged 1 commit into
mainfrom
claude/node-bond-932

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Closes #932. Stacked on #931 (decdn node register) — review/merge that first; the base will retarget to main once it lands.

Implements ADR 019 § Step 2.1–2.2: the operator command that stakes to a capacity tier, the precondition for decdn node register. Until now operators had to hand-compute k × Mbps^α and submit approve/bond/declareMbps by hand.

decdn node bond --mbps <N>

The operator passes only --mbps; the TOKEN amount is read from the on-chain bondRequired curve. Idempotent stake-to-tier:

  1. Read bondRequired(mbps), activeBond, minBond, declaredMbps, the [minCapacityMbps, maxCapacityMbps] band, token(), and the ERC-20 allowance.
  2. Reject an out-of-band --mbps up front — before any bond is posted — so it can't strand behind a declareMbps revert.
  3. target = max(minBond, bondRequired(mbps)); shortfall = target − activeBond (saturating). Check TOKEN balance, then approve (only if allowance < shortfall) and bond(shortfall).
  4. declareMbps(mbps) unless already at the tier.

Bonding the shortfall rather than a flat amount is what makes a re-run after a partial failure converge instead of double-bonding. --dry-run prints the plan (all chain reads, no sends); --json for machine output. Each step prints its tx hash or skipped.

Refactor

Extracts the shared chain plumbing — config-coordinate resolution, keystore-signer loading, provider build, address/hex helpers — out of register.rs into commands::chain_ctx, and flattens a shared ChainArgs clap flag group into both RegisterArgs and BondArgs. register is refactored onto it (behaviour-preserving; the precedence + output-shape tests moved with it).

Bindings

Extends the CapacityBond sol! bindings with token/activeBond/minBond/declaredMbps/bondRequired/min+maxCapacityMbps views and bond/declareMbps writes. Reuses decdn_incentive::Erc20 for allowance/approve/balanceOf.

Tests

cargo nextest run -p decdn-cli -p decdn-incentive — 374 green, incl. bond top-up/shortfall math (target − prior, minBond floor, already-bonded no-op) and the moved chain_ctx precedence tests. clippy / fmt clean; full pre-commit suite passed.

Operator flow is now decdn node bond --mbps 1000 then decdn node register --region DE --multiaddr …. The decdn setup wizard that chains these is tracked in #933.

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings June 17, 2026 14:26

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 operator-facing onboarding primitive, decdn node bond, to stake TOKEN up to the on-chain capacity tier requirements (ADR 019 §2.1–2.2) in an idempotent, retry-safe way. This complements (and is a precondition for) decdn node register by automating the approve/bond/declareMbps sequence and by refactoring shared chain plumbing for reuse.

Changes:

  • Add decdn node bond --mbps <N>: reads bondRequired, computes shortfall, conditionally approves, bonds, and declareMbps (with --dry-run / --json output).
  • Refactor shared blockchain/config/keystore/provider/address helpers into commands::chain_ctx and flatten a shared ChainArgs group into on-chain node subcommands.
  • Extend CapacityBond alloy bindings with bonding-related views/writes needed by the new command.

Reviewed changes

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

Show a summary per file
File Description
crates/incentive/src/capacity_bond.rs Extends CapacityBond ABI surface with bonding views/writes (token, activeBond, minBond, bondRequired, bond, declareMbps, etc.).
crates/common/src/cli/node.rs Adds NodeCommand::Bond, introduces shared ChainArgs, and defines BondArgs / refactors RegisterArgs to use flattened chain flags.
crates/common/src/cli/mod.rs Re-exports new CLI arg types (BondArgs, ChainArgs).
crates/cli/src/commands/register.rs Refactors register to use shared chain_ctx plumbing (behavior-preserving).
crates/cli/src/commands/node.rs Wires NodeCommand::Bond dispatch to the new command implementation.
crates/cli/src/commands/mod.rs Registers new bond and chain_ctx modules.
crates/cli/src/commands/chain_ctx.rs New shared chain context: config resolution, keystore signer loading, provider construction, address parsing, hex helper, plus tests.
crates/cli/src/commands/bond.rs New decdn node bond implementation: plan computation, execution, and output formatting/tests.

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

Comment thread crates/cli/src/commands/bond.rs Outdated
Comment on lines +210 to +246
fn write_plan(w: &mut impl io::Write, p: &Plan, json: bool, o: &Outcome) -> io::Result<()> {
let submitted = o.approve.is_some() || o.bond.is_some() || o.declare.is_some();
let tx_hex = |h: Option<&B256>| h.map(|v| format!("{v:#x}"));
if json {
let value = serde_json::json!({
"submitted": submitted,
"mbps": p.mbps,
"capacity_bond": format!("{:#x}", p.capacity_bond),
"token": format!("{:#x}", p.token),
"bond_required_base": p.required.to_string(),
"target_bond_base": p.target.to_string(),
"prior_bond_base": p.prior.to_string(),
"bonded_base": p.shortfall.to_string(),
"needs_declare": p.needs_declare,
"approve_tx": tx_hex(o.approve.as_ref()),
"bond_tx": tx_hex(o.bond.as_ref()),
"declare_tx": tx_hex(o.declare.as_ref()),
});
return writeln!(w, "{value}");
}
// Base units: 1 TOKEN = 1e18 base units.
writeln!(w, "mbps={}", p.mbps)?;
writeln!(w, "capacity_bond={:#x}", p.capacity_bond)?;
writeln!(w, "token={:#x}", p.token)?;
writeln!(w, "bond_required_base={}", p.required)?;
writeln!(w, "target_bond_base={}", p.target)?;
writeln!(w, "prior_bond_base={}", p.prior)?;
writeln!(w, "bonded_base={}", p.shortfall)?;
writeln!(w, "needs_declare={}", p.needs_declare)?;
write_tx_line(w, "approve_tx", o.approve.as_ref())?;
write_tx_line(w, "bond_tx", o.bond.as_ref())?;
write_tx_line(w, "declare_tx", o.declare.as_ref())?;
if submitted {
writeln!(w, "submitted=true")
} else {
writeln!(w, "submitted=false dry_run=true")
}
@gemini-code-assist

Copy link
Copy Markdown

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

alpergundogdu added a commit that referenced this pull request Jun 17, 2026
Implements ADR 019 § Step 2.1–2.2: the operator command that stakes to a
capacity tier before registration. The operator passes only `--mbps`; the
TOKEN amount is read from the on-chain `bondRequired` curve.

Idempotent by construction: it reads `bondRequired(mbps)`, `activeBond`,
`minBond`, `declaredMbps`, the band, and the ERC-20 allowance, computes
`target = max(minBond, bondRequired(mbps))`, and approves + bonds only the
shortfall (`target − activeBond`), then `declareMbps` unless already at the
tier. Bonding the shortfall rather than a flat amount means a re-run after a
partial failure converges instead of double-bonding. The tier is validated
against the on-chain `[minCapacityMbps, maxCapacityMbps]` band up front so an
out-of-band `--mbps` is rejected before any bond is stranded behind a
`declareMbps` revert. `--dry-run` prints the plan; `--json` for machine output.

- incentive: extend the CapacityBond `sol!` bindings with token/activeBond/
  minBond/declaredMbps/bondRequired/min+maxCapacityMbps views and bond/
  declareMbps writes. Reuse `decdn_incentive::Erc20` for allowance/approve/
  balanceOf.
- cli: extract the shared chain plumbing (config-coordinate resolution,
  keystore-signer loading, provider build, address/hex helpers) into
  `commands::chain_ctx`, flatten a shared `ChainArgs` flag group into both
  `RegisterArgs` and `BondArgs`, and refactor `register` onto it.

Review fixes (#934): a real no-op run now reports `submitted=false
dry_run=false` rather than masquerading as a dry-run preview; the shared
`chain_ctx` carries the register-branch fixes (scrypt keystore load offloaded
to `spawn_blocking`; config-sourced `data_dir`/`eth_keystore` paths
tilde-expanded).

Stacked on #929 (PR #931); operator flow is `decdn node bond --mbps N` then
`decdn node register …`. The `decdn setup` wizard is tracked in #933.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@alpergundogdu
alpergundogdu force-pushed the claude/node-bond-932 branch from 054593b to c532d9d Compare June 17, 2026 14:45
@alpergundogdu

Copy link
Copy Markdown
Contributor Author

Rebased onto the updated #931 and addressed the review:

  • Copilot: a real run that's already at the target tier now reports submitted=false dry_run=false (a genuine no-op) instead of masquerading as dry_run=true; added a regression test.
  • The shared chain_ctx (extracted here) carries the feat(cli): add decdn node register for on-chain node registration (#929) #931 fixes: spawn_blocking keystore load and tilde-expanded config paths apply to bond too.

Base automatically changed from claude/node-register-929 to main June 17, 2026 15:16
Implements ADR 019 § Step 2.1–2.2: the operator command that stakes to a
capacity tier before registration. The operator passes only `--mbps`; the
TOKEN amount is read from the on-chain `bondRequired` curve.

Idempotent by construction: it reads `bondRequired(mbps)`, `activeBond`,
`minBond`, `declaredMbps`, the band, and the ERC-20 allowance, computes
`target = max(minBond, bondRequired(mbps))`, and approves + bonds only the
shortfall (`target − activeBond`), then `declareMbps` unless already at the
tier. Bonding the shortfall rather than a flat amount means a re-run after a
partial failure converges instead of double-bonding. The tier is validated
against the on-chain `[minCapacityMbps, maxCapacityMbps]` band up front so an
out-of-band `--mbps` is rejected before any bond is stranded behind a
`declareMbps` revert. `--dry-run` prints the plan; `--json` for machine output.

- incentive: extend the CapacityBond `sol!` bindings with token/activeBond/
  minBond/declaredMbps/bondRequired/min+maxCapacityMbps views and bond/
  declareMbps writes. Reuse `decdn_incentive::Erc20` for allowance/approve/
  balanceOf.
- cli: extract the shared chain plumbing (config-coordinate resolution,
  keystore-signer loading, provider build, address/hex helpers) into
  `commands::chain_ctx`, flatten a shared `ChainArgs` flag group into both
  `RegisterArgs` and `BondArgs`, and refactor `register` onto it.

Review fixes (#934): a real no-op run now reports `submitted=false
dry_run=false` rather than masquerading as a dry-run preview; the shared
`chain_ctx` carries the register-branch fixes (scrypt keystore load offloaded
to `spawn_blocking`; config-sourced `data_dir`/`eth_keystore` paths
tilde-expanded).

Stacked on #929 (PR #931); operator flow is `decdn node bond --mbps N` then
`decdn node register …`. The `decdn setup` wizard is tracked in #933.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@alpergundogdu
alpergundogdu force-pushed the claude/node-bond-932 branch from c532d9d to 09d33d8 Compare June 17, 2026 15:18
@github-actions

Copy link
Copy Markdown

Coverage report

Total: 85.25% (was 85.52%, ▼ 0.27%) — 37540/44037 lines

File Coverage Lines Δ
crates/cli/src/commands/bond.rs 32.06% 67/209 new
crates/cli/src/commands/chain_ctx.rs 65.95% 122/185 new
crates/cli/src/commands/node.rs 82.67% 1078/1304 ▼ 0.06%
crates/cli/src/commands/register.rs 28.67% 41/143 ▼ 22.39%
History
Commit Total Δ vs base
09d33d8 (HEAD) 85.25% ▼ 0.27%

@alpergundogdu
alpergundogdu merged commit 5842086 into main Jun 17, 2026
20 checks passed
@alpergundogdu
alpergundogdu deleted the claude/node-bond-932 branch June 17, 2026 15:34
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.

Implement decdn node bond — idempotent stake-to-capacity-tier (ADR 019 §2.1–2.2)

2 participants