<p>The error indicates that the same input was spent twice in the same transaction.</p>
<p>Since you cannot reproduce it, it’ll be hard to fully track down what the issue was, but my guess would be that there was a concurrency issue where the way you were polling the data source was executing twice in parallel and added the same UTXO twice to your UTXO pool.</p>
<p>If there was any chance of asynchronicity or concurrency in your implementation, you might want to guard against this issue by deduplicating your UTXO pool before selecting inputs. You might also want to implement some sort of logging in case of errors.</p>

CC BY-SA 4.0
2024-09-20T12:57:10.183Z
2024-09-20T12:57:10.183Z
Murch
<p>The issue you're facing, marked by the error code &quot;-26, bad-txns-inputs-duplicate,&quot; suggests a problem with duplicate UTXO (Unspent Transaction Output) usage. This error occurs when the same UTXO is inadvertently used more than once in a single transaction, which Bitcoin's protocol doesn't allow.</p>
<p>Based on your process:</p>
<ol>
<li><p><strong>Polling the UTXO set</strong>: There might be a race condition. If you're polling too quickly or if the mempool hasn't fully updated, the same UTXO could be perceived as available, even though it was already committed to a transaction.</p>
</li>
<li><p><strong>Solution</strong>:</p>
<ul>
<li>Implement a delay between polling UTXOs and submitting the transaction to ensure the mempool is fully updated.</li>
<li>Alternatively, use a more robust mechanism for tracking spent UTXOs, such as checking the transaction's status in the blockchain rather than solely relying on mempool status.</li>
</ul>
</li>
<li><p><strong>First transaction issue</strong>: Although it's the first transaction for this wallet, a UTXO might be getting erroneously referenced multiple times due to how the inputs are selected. Double-check how your code iterates through and selects UTXOs. Ensure no UTXO is mistakenly added more than once to the input array.</p>
</li>
</ol>
<p>These tweaks should prevent the error from reoccurring.</p>

CC BY-SA 4.0
2024-09-19T13:06:26.930Z
2024-09-19T13:06:26.930Z
CoinChampion24
<p>Recently I had an error, which I could never reproduce again. I don't want this error to occur in production, but I don't know how to fix this either or what could even be the reason for it.</p>
<p>First, here is what I did, in short:</p>
<ol>
<li>My backend generated a new address.</li>
<li>I sent some funds to the address.</li>
<li>Backend waited, until the funds appear in the address (by polling mempool.space API).</li>
<li>Backend took the array of UTXOs of this address, iterated through this array only once, adding UTXOs to transaction inputs, until the necessary sending amount is covered.</li>
<li>Backend submitted TX Hex of this transaction to mempool.space.</li>
</ol>
<p>This resulted in the &quot;code&quot;: -26, &quot;bad-txns-inputs-duplicate&quot; error.</p>
<p>Google says it occurs, when a UTXO is spent multiple times in one transaction. I reviewed the code thoroughly, it is impossible that a UTXO was added into tx inputs multiple times.</p>
<p>Also, it was the first transaction of the wallet, so there was no previous transaction, that could spend a UTXO.</p>
<p>Any ideas regarding what could happen there?</p>

CC BY-SA 4.0
2024-09-19T14:33:26.493Z
Did you keep a copy of the raw transaction? What does it look like when you look at it in detail?
Murch
CC BY-SA 4.0
2024-09-19T18:12:30.030Z
@Murch yeah, this is a very good question, unfortunately no.
Maxgmer
<bitcoin-core><client><utxo><doublespend>
Error "bad-txns-inputs-duplicate". Very weird
Maxgmer
# Error "bad-txns-inputs-duplicate". Very weird

(Asked by: username_0 on 2024-09-19)

Recently I had an error, which I could never reproduce again. I don't want this error to occur in production, but I don't know how to fix this either or what could even be the reason for it.

First, here is what I did, in short:

1. My backend generated a new address.
2. I sent some funds to the address.
3. Backend waited, until the funds appear in the address (by polling mempool.space API).
4. Backend took the array of UTXOs of this address, iterated through this array only once, adding UTXOs to transaction inputs, until the necessary sending amount is covered.
5. Backend submitted TX Hex of this transaction to mempool.space.

This resulted in the "code": -26, "bad-txns-inputs-duplicate" error.

Google says it occurs, when a UTXO is spent multiple times in one transaction. I reviewed the code thoroughly, it is impossible that a UTXO was added into tx inputs multiple times.

Also, it was the first transaction of the wallet, so there was no previous transaction, that could spend a UTXO.

Any ideas regarding what could happen there?

**Tags:** bitcoin-core, client, utxo, doublespend

> **Comments:**
> **username_1**: Did you keep a copy of the raw transaction? What does it look like when you look at it in detail?  
> **username_0**: @username_1 yeah, this is a very good question, unfortunately no.  

---

## 2 answers

### Answer by username_1 on 2024-09-20. Score: 2 | Accepted answer

The error indicates that the same input was spent twice in the same transaction.

Since you cannot reproduce it, it’ll be hard to fully track down what the issue was, but my guess would be that there was a concurrency issue where the way you were polling the data source was executing twice in parallel and added the same UTXO twice to your UTXO pool.

If there was any chance of asynchronicity or concurrency in your implementation, you might want to guard against this issue by deduplicating your UTXO pool before selecting inputs. You might also want to implement some sort of logging in case of errors.


---

### Answer by username_2 on 2024-09-19. Score: -5

The issue you're facing, marked by the error code "-26, bad-txns-inputs-duplicate," suggests a problem with duplicate UTXO (Unspent Transaction Output) usage. This error occurs when the same UTXO is inadvertently used more than once in a single transaction, which Bitcoin's protocol doesn't allow.

Based on your process:

1. **Polling the UTXO set**: There might be a race condition. If you're polling too quickly or if the mempool hasn't fully updated, the same UTXO could be perceived as available, even though it was already committed to a transaction.
2. **Solution**:

   * Implement a delay between polling UTXOs and submitting the transaction to ensure the mempool is fully updated.
   * Alternatively, use a more robust mechanism for tracking spent UTXOs, such as checking the transaction's status in the blockchain rather than solely relying on mempool status.
3. **First transaction issue**: Although it's the first transaction for this wallet, a UTXO might be getting erroneously referenced multiple times due to how the inputs are selected. Double-check how your code iterates through and selects UTXOs. Ensure no UTXO is mistakenly added more than once to the input array.

These tweaks should prevent the error from reoccurring.


---

