refactor(PERC-8463): extract charge_fee_to_insurance helper - #81
Conversation
…icated settle_maintenance_fee logic
📝 WalkthroughWalkthroughExtracted maintenance-fee settlement logic for handling negative fee_credits into a new shared helper method Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/percolator.rs (1)
2593-2620: Use the new helper as the single debt-payment path.Line 2601 introduces the shared helper, but
pay_fee_debt_from_capital(around Line 2634 onward) still duplicates the same mutation sequence. Routing that function through this helper would reduce drift risk.♻️ Suggested follow-up refactor
fn pay_fee_debt_from_capital(&mut self, idx: u16) { - if self.accounts[idx as usize].fee_credits.is_negative() - && !self.accounts[idx as usize].capital.is_zero() - { - let owed = neg_i128_to_u128(self.accounts[idx as usize].fee_credits.get()); - let current_cap = self.accounts[idx as usize].capital.get(); - let pay = core::cmp::min(owed, current_cap); - if pay > 0 { - // Use set_capital helper to maintain c_tot aggregate (spec §4.1) - self.set_capital(idx as usize, current_cap.saturating_sub(pay)); - self.insurance_fund.balance += pay; - self.insurance_fund.fee_revenue += pay; - self.accounts[idx as usize].fee_credits = self.accounts[idx as usize] - .fee_credits - .saturating_add(u128_to_i128_clamped(pay)); - } - } + let _ = self.charge_fee_to_insurance(idx as usize); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/percolator.rs` around lines 2593 - 2620, The new helper charge_fee_to_insurance encapsulates the debt-pay sequence; update pay_fee_debt_from_capital to call charge_fee_to_insurance(idx) instead of duplicating the capital/insurance_fund/account mutations so there’s a single debt-payment path. Replace the duplicated logic that computes owed/current_cap/pay, calls set_capital, updates insurance_fund.balance and insurance_fund.fee_revenue, and increments accounts[idx].fee_credits with a call to charge_fee_to_insurance(idx), returning or aggregating its paid value as the original function did, ensuring any return value or side-effects (c_tot via set_capital) remain unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/percolator.rs`:
- Around line 2593-2620: The new helper charge_fee_to_insurance encapsulates the
debt-pay sequence; update pay_fee_debt_from_capital to call
charge_fee_to_insurance(idx) instead of duplicating the
capital/insurance_fund/account mutations so there’s a single debt-payment path.
Replace the duplicated logic that computes owed/current_cap/pay, calls
set_capital, updates insurance_fund.balance and insurance_fund.fee_revenue, and
increments accounts[idx].fee_credits with a call to
charge_fee_to_insurance(idx), returning or aggregating its paid value as the
original function did, ensuring any return value or side-effects (c_tot via
set_capital) remain unchanged.
Summary (SYNC-06 / PERC-8463)
Pure refactor: extract shared
charge_fee_to_insurancehelper from identical duplicated logic insettle_maintenance_feeandsettle_maintenance_fee_best_effort_for_crank(both inpercolator-core/src/percolator.rs).Previously, both functions contained byte-for-byte identical blocks that:
fee_creditsis negativeowed = -fee_credits,pay = min(owed, current_cap)set_capitalhelper (preserves c_tot aggregate per spec §4.1)insurance_fund.balanceandinsurance_fund.fee_revenuefee_creditsby amount paidpaid_from_capitalfor returnThese are now consolidated into
fn charge_fee_to_insurance(&mut self, idx: usize) -> u128.Semantics
IDENTICAL to prior behavior:
set_capital/saturating_sub/saturating_addarithmeticVerification
cargo check --all-featurespasses cleanTest plan
cargo testand fee/settle suites greenSummary by CodeRabbit