atomicDEX-API
atomicDEX-API copied to clipboard
TakerFeeSendFailed: Missing Inputs Error Due to Potential UTXO Double-Spend in Concurrent Swaps
Issue Summary
A user reported a taker swap failure with a "Missing inputs" error when attempting to broadcast a TakerFeeSend transaction. The error suggests that KDF attempted to spend UTXOs that didn't exist or were already spent, potentially due to race conditions when multiple swaps are executed concurrently.
Error Details
18 15:19:54, mm2_main::lp_swap::taker_swap:512] ERROR [swap uuid=9aad244c-6db9-4d46-b8ab-379ebe2a5af6] TakerFeeSendFailed(SwapError { error: "taker_swap:1370] mm2src/coins/[utxo.rs:1813](http://utxo.rs:1813/)] utxo_common:5044] client:884] Rpc(ResponseParseError(JsonRpcError { client_info: "coin: FIRO", request: JsonRpcRequest { jsonrpc: "2.0", id: 3866, method: "blockchain.transaction.broadcast", params: [String("01000000016e2b277cd34b72f0b8a9e8f7d18f6097244b9633767346384a3e95fbeb1954fc010000006a4730440220162f0ef400f79920777478e2f65a3d9ecaf86121fe88294f4bd5ddc2084cd4bb022066a5f0e3aa013ed759b32eb8b7c9b7d1897a7225374ffad52078b8eadab210ca0121034604d5bce7e25b69d2e7d7dfe29904dbdf5c01de005e8aa37bf086b14d1fb369ffffffff0232310000000000001976a914ca1e04745e8ca0c60d8c5881531d51bec470743f88acb08b0600000000001976a914953658c36f7be4026dee323bc50b7178cd60dbea88ac1a667a68")] }, error: Response([electrumx.firo.org:50001](http://electrumx.firo.org:50001/), Object {"code": Number(1), "message": String("the transaction was rejected by network rules.\n\nMissing inputs\n[01000000016e2b277cd34b72f0b8a9e8f7d18f6097244b9633767346384a3e95fbeb1954fc010000006a4730440220162f0ef400f79920777478e2f65a3d9ecaf86121fe88294f4bd5ddc2084cd4bb022066a5f0e3aa013ed759b32eb8b7c9b7d1897a7225374ffad52078b8eadab210ca0121034604d5bce7e25b69d2e7d7dfe29904dbdf5c01de005e8aa37bf086b14d1fb369ffffffff0232310000000000001976a914ca1e04745e8ca0c60d8c5881531d51bec470743f88acb08b0600000000001976a914953658c36f7be4026dee323bc50b7178cd60dbea88ac1a667a68]")}) }))" })
User Context
The user operates a trading bot that:
- Maintains maker orders in the orderbook
- Converts maker orders to taker orders when market conditions change
- Can potentially match multiple orders in rapid succession
- May have multiple taker swap related transactions mined in the same block (user reported up to 5)
Root Cause Analysis
The issue appears to stem from concurrent swap attempts trying to spend the same UTXOs. This can occur when:
- Multiple orders exceed available balance: User posts multiple sell orders totaling more than their actual balance
- Concurrent swap initiation: Multiple takers match orders simultaneously, leading to attempts to spend the same UTXOs
- Race conditions: The system doesn't properly lock UTXOs when a swap is initiated, allowing multiple swaps to reference the same inputs
Possible Scenarios Floated
Scenario 1: Over-committed Orders
- Balance: 100 KMD
- Posted orders:
- Sell 70 KMD for FIRO
- Sell 50 KMD for FIRO
- Sell 30 KMD for another coin
- Total committed: 150 KMD (exceeds balance)
- If all three orders are matched simultaneously, UTXO conflicts might occur
Scenario 2: Same Balance, Multiple Orders
- Balance: 100 KMD
- Posted orders:
- Sell 100 KMD for FIRO
- Sell 100 KMD for another coin
- If both orders are matched by different takers, both swaps attempt to spend the same 100 KMD
Expected Behavior
- UTXO Locking: When a swap is initiated, the required UTXOs should be immediately locked/reserved
- Balance Validation: Orders should be validated against available (unlocked) balance before matching
- Graceful Handling: When insufficient balance is detected:
- Some swaps should succeed (e.g., 70 KMD + 30 KMD in scenario 1)
- Remaining orders should fail to match without starting swaps
- No "missing inputs" errors should occur
Questions to Investigate
- Does KDF implement automatic UTXO splitting for concurrent swaps?
- Is there existing code to prevent multiple swaps from using the same UTXOs?
- How does KDF handle order matching when total order volume exceeds available balance?
- Are UTXOs created/locked at swap initiation or only at transaction broadcast time?
Additional Considerations
- The issue may be compounded by electrum servers being at different/wrong heights or having different mempool states
- The problem might affect all transaction types in the swap process
- This could be particularly problematic for automated trading bots that manage multiple orders for the same coin across multiple pairs