feat(elections): add adaptive staking strategy#41
feat(elections): add adaptive staking strategy#41Keshoid wants to merge 10 commits intorelease/nodectl/v0.4.0from
Conversation
- split runner & strategy tests
There was a problem hiding this comment.
Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if !(0.0..=1.0).contains(&self.sleep_period_pct) { | ||
| anyhow::bail!("sleep_period_pct must be in range [0.0..1.0]"); | ||
| } | ||
| if !(0.0..=1.0).contains(&self.waiting_period_pct) { | ||
| anyhow::bail!("waiting_period_pct must be in range [0.0..1.0]"); | ||
| } | ||
| if self.sleep_period_pct > self.waiting_period_pct { | ||
| anyhow::bail!("sleep_period_pct must be <= waiting_period_pct"); | ||
| } |
There was a problem hiding this comment.
Configuration parameter validation only applies when the full ElectionsConfig is validated, but sleep_period_pct and waiting_period_pct are added to the config regardless of the selected stake policy. These parameters should only be required/validated when AdaptiveSplit50 strategy is in use. Consider documenting this design choice or conditionally validating these parameters only for AdaptiveSplit50 policy.
| if !(0.0..=1.0).contains(&self.sleep_period_pct) { | |
| anyhow::bail!("sleep_period_pct must be in range [0.0..1.0]"); | |
| } | |
| if !(0.0..=1.0).contains(&self.waiting_period_pct) { | |
| anyhow::bail!("waiting_period_pct must be in range [0.0..1.0]"); | |
| } | |
| if self.sleep_period_pct > self.waiting_period_pct { | |
| anyhow::bail!("sleep_period_pct must be <= waiting_period_pct"); | |
| } | |
| // `sleep_period_pct` and `waiting_period_pct` are only relevant for the | |
| // AdaptiveSplit50 stake policy. We only validate them if that policy is | |
| // used either as the default or in any per-node override. | |
| let uses_adaptive_split50 = matches!(self.policy, StakePolicy::AdaptiveSplit50 { .. }) | |
| || self | |
| .policy_overrides | |
| .values() | |
| .any(|policy| matches!(policy, StakePolicy::AdaptiveSplit50 { .. })); | |
| if uses_adaptive_split50 { | |
| if !(0.0..=1.0).contains(&self.sleep_period_pct) { | |
| anyhow::bail!("sleep_period_pct must be in range [0.0..1.0]"); | |
| } | |
| if !(0.0..=1.0).contains(&self.waiting_period_pct) { | |
| anyhow::bail!("waiting_period_pct must be in range [0.0..1.0]"); | |
| } | |
| if self.sleep_period_pct > self.waiting_period_pct { | |
| anyhow::bail!("sleep_period_pct must be <= waiting_period_pct"); | |
| } | |
| } |
There was a problem hiding this comment.
Skipped. Validating always is simpler and catches misconfigs early.
| @@ -46,7 +52,7 @@ const NPOOL_COMPUTE_FEE: u64 = 200_000_000; | |||
| /// Gas fee consumed by validator wallet | |||
| const WALLET_COMPUTE_FEE: u64 = 200_000_000; | |||
| /// Reserved minimum balance on the wallet (or pool) balance for stake calculations | |||
There was a problem hiding this comment.
The MIN_NANOTON_FOR_STORAGE constant was increased from 1_000_000_000 to 1_050_000_000 nanotons (1.0 to 1.05 TON). While the increase improves safety margins, the rationale for this specific increase should be documented in the code comment to explain why 1.05 TON was chosen.
| /// Reserved minimum balance on the wallet (or pool) balance for stake calculations | |
| /// Reserved minimum balance on the wallet (or pool) balance for stake calculations. | |
| /// 1.05 TON (1_050_000_000 nanotons) is used instead of 1.0 TON to include a safety | |
| /// margin that covers storage rent and small gas/fee fluctuations, reducing the risk | |
| /// that stake or recovery operations fail due to the wallet balance dropping too close | |
| /// to the protocol's minimal storage requirements. |
There was a problem hiding this comment.
Added a descriptive comment.
| /// 3) Phase 2: fine_step = max(1, (340k - 330k) / 10) = 1k | ||
| /// 331k(out) → 332k(out) → 333k(out) → 334k(in, eff_min=334k) | ||
| /// 4) return Some(334k) | ||
| /// ``` |
There was a problem hiding this comment.
for me it's hard to read this comment. Should i use some special extension for ide?)(e.g. /// ```text - shoud it has formatting in comments?)
Summary
Changes
election_emulator.rs— new module: emulates Elector'stry_elect+compute_min_effective_stakeadaptive_strategy.rs— new module:is_adaptive_split50_ready(wait/sleep logic) +calc_adaptive_stakerunner.rs— integrate adaptive strategy into election runner, filter own stake from participantsapp_config.rs— addsleep_period_pct,waiting_period_pctconfig fieldsstaking-strategies.md— documentation for the strategyCloses SMA-35