fix: mint UTXO auto-consolidation fails when margin exceeds balance#393
Conversation
The previous consolidation logic calculated a target of collateral + 10% margin + 0.1 DGB, then checked if total balance exceeded that target. When the margin pushed the target above the available balance (e.g., 250K collateral + 10% = 275K > 274K available), the consolidation rejected with "Insufficient funds" even though enough DGB existed. Fix: sweep the entire wallet balance to self using subtractfeefromamount, which always succeeds and consolidates all UTXOs into one output. The fee check now only verifies that collateral + 0.2 DGB fees < total balance (the actual minimum needed). Tested: wallet with 500 UTXOs of 500 DGB each (250K total) where the top 400 UTXOs sum to only 221K — below the 250K collateral requirement. Previous code threw "Insufficient funds"; fix correctly consolidates and retries. Supersedes PR DigiByte-Core#391 consolidation logic. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Thanks @JohnnyLawDGB — solid fix, well-documented PR. 👏 Full code audit completed. Traced every code path this touches: ✅ Built from source and ran full test suite on the PR branch:
The sweep approach is cleaner and more robust than the margin math. Good catch on the real-world failure with @AussieEpic's 499-UTXO wallet. Minor note: The error message lost the UTXO count that was in the old message — not a blocker, just slightly less diagnostic info. LGTM — merging. 🚀 |
ae8b5b0
into
DigiByte-Core:feature/digidollar-v1
Problem
PR #391 introduced UTXO auto-consolidation for
mintdigidollarto handle wallets with >400 UTXOs (theMAX_TX_INPUTSlimit intxbuilder.cpp). However, the consolidation itself fails in a common scenario, leaving the bug effectively unfixed in RC25.Reported by: @AussieEpic in DigiSwarm — confirmed the bug is still present in RC25 with >400 mining UTXOs.
Root Cause
The consolidation logic calculates a target amount:
CAmount consolidationTarget = result.collateralRequired + (result.collateralRequired / 10) + 10000000; // collateral + 10% + 0.1 DGBWhen the wallet's total balance barely exceeds the collateral requirement, the 10% margin pushes the target above the available balance:
Result:
"Insufficient funds for collateral. Need 250309.79 DGB, have 274686.74 DGB across 499 small UTXOs."— the wallet clearly has enough DGB, but the margin math rejects it.Reproduction
On testnet19 (RC25), oracle wallet with 499 UTXOs:
The wallet has 274K DGB across 499 UTXOs. The top 400 UTXOs (the
MAX_TX_INPUTSlimit) sum to only 225K DGB — below the 250K collateral requirement. SoSelectCoinscorrectly identifies this as a fragmentation issue and triggers the consolidation path. But the consolidation rejects because275K target > 274K available.Fix
Replace the fixed-margin target calculation with a sweep-to-self approach using
subtractfeefromamount:total balance > collateral + 0.2 DGB(the actual minimum needed)subtractfeefromamount=true— the wallet's standard coin selection handles fee calculation, and the sweep always succeeds regardless of marginBuildMintTransactionThis approach:
CreateTransactionfor fee calculationTested
Built from source and tested on testnet19:
Same wallet, same UTXOs that failed on RC25 — now auto-consolidates and mints successfully.
Test Methodology
utxo-frag-testwallet (creating 499 UTXOs, 274K total)Files Changed
src/rpc/digidollar.cpp— consolidation block inmintdigidollarhandlerTest plan
consolidation_txidandutxos_consolidatedappear in response🤖 Generated with Claude Code