Repository navigation
feat(cli): add decdn node bond — idempotent stake-to-tier (#932) - #934
Conversation
There was a problem hiding this comment.
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>: readsbondRequired, computes shortfall, conditionallyapproves,bonds, anddeclareMbps(with--dry-run/--jsonoutput). - Refactor shared blockchain/config/keystore/provider/address helpers into
commands::chain_ctxand flatten a sharedChainArgsgroup into on-chain node subcommands. - Extend
CapacityBondalloy 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.
| 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") | ||
| } |
|
Warning Gemini encountered an error creating the review. You can try again by commenting |
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]>
054593b to
c532d9d
Compare
|
Rebased onto the updated #931 and addressed the review:
|
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]>
c532d9d to
09d33d8
Compare
Coverage reportTotal: 85.25% (was 85.52%, ▼ 0.27%) — 37540/44037 lines
History
|
Closes #932. Stacked on #931 (
decdn node register) — review/merge that first; the base will retarget tomainonce 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-computek × Mbps^αand submitapprove/bond/declareMbpsby hand.decdn node bond --mbps <N>The operator passes only
--mbps; the TOKEN amount is read from the on-chainbondRequiredcurve. Idempotent stake-to-tier:bondRequired(mbps),activeBond,minBond,declaredMbps, the[minCapacityMbps, maxCapacityMbps]band,token(), and the ERC-20 allowance.--mbpsup front — before any bond is posted — so it can't strand behind adeclareMbpsrevert.target = max(minBond, bondRequired(mbps));shortfall = target − activeBond(saturating). Check TOKEN balance, thenapprove(only if allowance < shortfall) andbond(shortfall).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-runprints the plan (all chain reads, no sends);--jsonfor machine output. Each step prints its tx hash orskipped.Refactor
Extracts the shared chain plumbing — config-coordinate resolution, keystore-signer loading, provider build, address/hex helpers — out of
register.rsintocommands::chain_ctx, and flattens a sharedChainArgsclap flag group into bothRegisterArgsandBondArgs.registeris refactored onto it (behaviour-preserving; the precedence + output-shape tests moved with it).Bindings
Extends the
CapacityBondsol!bindings withtoken/activeBond/minBond/declaredMbps/bondRequired/min+maxCapacityMbpsviews andbond/declareMbpswrites. Reusesdecdn_incentive::Erc20forallowance/approve/balanceOf.Tests
cargo nextest run -p decdn-cli -p decdn-incentive— 374 green, incl. bond top-up/shortfall math (target − prior,minBondfloor, already-bonded no-op) and the movedchain_ctxprecedence tests. clippy / fmt clean; full pre-commit suite passed.Operator flow is now
decdn node bond --mbps 1000thendecdn node register --region DE --multiaddr …. Thedecdn setupwizard that chains these is tracked in #933.🤖 Generated with Claude Code