backport: fix(wallet): calculate asset lock tx fee dynamically based on input count#636
Conversation
…ount Replace hardcoded 3000 duff fee with dynamic fee calculation that accounts for actual number of inputs. Estimates tx size using standard component sizes (P2PKH input ~148B, output ~34B, header ~10B, payload ~60B) and uses max(3000, estimated_size) to always meet the min relay fee. Properly handles fee shortfall when allow_take_fee_from_amount is set, and returns clear error messages for insufficient funds. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThis pull request introduces dynamic asset lock fee calculation logic that replaces hard-coded fees with a size-based computation model, alongside comprehensive manual test documentation. The implementation includes fee estimation, change output handling, and dust threshold logic within the asset_lock_transaction module. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 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 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 |
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR backports a fix to make asset-lock transaction fees dynamic (instead of a hardcoded 3000 duffs) by estimating transaction size from the selected input/output count, then recalculating the amount/change based on the resulting fee.
Changes:
- Select UTXOs using an initial fee estimate, then compute a dynamic fee from estimated tx size and recompute
actual_amount+ change. - Add graceful handling for fee shortfall when
allow_take_fee_from_amountis enabled. - Add a new manual test plan document covering common and edge-case scenarios.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
src/model/wallet/asset_lock_transaction.rs |
Replaces hardcoded fee with size-based fee estimation and recalculates amount/change accordingly. |
docs/ai-design/2026-02-24-asset-lock-fee-fix/manual-test-scenarios.md |
Adds manual test scenarios for validating dynamic fee behavior and related edge cases. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Extract fee/amount/change computation from the inline logic in asset_lock_transaction_from_private_key() into a standalone calculate_asset_lock_fee() function. Uses an iterative approach that fixes a bug where the fee was computed assuming a change output existed (based on the initial 3000-duff estimate). When the real fee eliminated the change, the code overestimated by 34 bytes and could trigger a false insufficient-funds error on edge cases with many inputs. Also removes stale edge case E3 from manual test scenarios (referenced a database refactor not in this PR). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Covers basic fee scenarios (minimum fee, scaling with inputs, exact change, fee-from-amount, insufficient funds) and two regression tests that prove the bug fixed in the previous commit — false insufficient funds when change disappears under the real fee with many inputs. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Use checked_add for requested_amount + fee to prevent u64 overflow - Add DUST_THRESHOLD (546 duffs) — change below this is absorbed into the fee instead of creating a non-standard dust output - Replace hardcoded 3_000u64 with MIN_ASSET_LOCK_FEE constant in caller Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
docs/ai-design/2026-02-24-asset-lock-fee-fix/manual-test-scenarios.md (1)
256-272: Edge-case numbering skips E3.Consider adding E3 or renumbering so the checklist is unambiguous during manual runs.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@docs/ai-design/2026-02-24-asset-lock-fee-fix/manual-test-scenarios.md` around lines 256 - 272, The edge-case checklist skips E3 (currently has E1, E2, E4); update the headings so numbering is sequential by either inserting a new E3 test case (e.g., a short description for the missing scenario) or renumbering E4→E3 and adjust any related headings/descriptions; ensure any references or links to these headings (e.g., "E4: Concurrent Asset Lock Creation") are updated to the new symbol names so the manual test checklist is unambiguous.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/model/wallet/asset_lock_transaction.rs`:
- Around line 247-276: The selection uses MIN_ASSET_LOCK_FEE to pick UTXOs via
take_unspent_utxos_for but then recomputes the fee in calculate_asset_lock_fee
(which uses estimate_tx_size), so when inputs grow the actual fee may exceed the
initial estimate and cause failure; modify the logic to loop: after
take_unspent_utxos_for returns utxos, compute total_input_value, num_inputs and
call calculate_asset_lock_fee, and if it returns an insufficient-funds error
then restore utxos and retry selection with an increased fee estimate (e.g., set
the new estimate to the returned required fee or add a small buffer) by calling
take_unspent_utxos_for again until success or no more UTXOs; ensure the
rollback/restore uses self.utxos.extend(utxos.into_iter()) as currently done and
preserve existing error propagation when retries are exhausted.
---
Nitpick comments:
In `@docs/ai-design/2026-02-24-asset-lock-fee-fix/manual-test-scenarios.md`:
- Around line 256-272: The edge-case checklist skips E3 (currently has E1, E2,
E4); update the headings so numbering is sequential by either inserting a new E3
test case (e.g., a short description for the missing scenario) or renumbering
E4→E3 and adjust any related headings/descriptions; ensure any references or
links to these headings (e.g., "E4: Concurrent Asset Lock Creation") are updated
to the new symbol names so the manual test checklist is unambiguous.
ℹ️ Review info
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
docs/ai-design/2026-02-24-asset-lock-fee-fix/manual-test-scenarios.mdsrc/model/wallet/asset_lock_transaction.rs
…rios.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…ror path The rollback code used `extend()` which expected `(Address, HashMap<OutPoint, TxOut>)` but received `(OutPoint, (TxOut, Address))` from `take_unspent_utxos_for()`. Re-group UTXOs by address using entry API to match `self.utxos` structure. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace the old asset-lock-fee-fix test scenarios (from PR #636) with comprehensive scenarios covering the full PR #651 scope: Core wallet payment fees, asset lock fees, Platform fee consolidation, UTXO removal consistency, and edge cases (SPV mode, dust threshold, large input count). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary
allow_take_fee_from_amountis setTest plan
Manual test scenarios:
docs/ai-design/2026-02-24-asset-lock-fee-fix/manual-test-scenarios.md🤖 Co-authored by Claudius the Magnificent AI Agent
Summary by CodeRabbit
Bug Fixes
Documentation