<p>To avoid a race condition with <code>getblockcount</code>, you could use <code>getblockheader</code> with the <code>bestblock</code> result from <code>gettxout</code> and get its height. Then do the height calculation with the number of confirmations to get the block hash to pass to <code>getrawtransaction</code>.</p>

CC BY-SA 4.0
2021-09-01T17:39:59.640Z
2021-09-01T17:39:59.640Z
Ava Chow
<p>I'm trying to get the raw transaction hex string of a transaction that I know the id from.</p>
<p>I would like to do that without using txindex=1.</p>
<p>As I assume the transaction is unspent (and confirmed) I'm doing the following steps:</p>
<p><code>bitcoin-cli gettxout 'txid'</code></p>
<p><code>bitcoin-cli getblockcount</code></p>
<p><code>bitcoin-cli getblockhash 'blockcount - txout.confirmations + 1'</code></p>
<p><code>bitcoin-cli getrawtransaction 'txid' 0 'blockhash'</code></p>
<p>I have a race condition when getting the blockcount.</p>
<p>Is there a better way to do that?</p>

<json-rpc><raw-transaction><bitcoin-cli><txindex>
Get raw transaction from tx id without txindex=1
user44941
# Get raw transaction from tx id without txindex=1

(Asked by: username_0 on 2021-09-01)

I'm trying to get the raw transaction hex string of a transaction that I know the id from.

I would like to do that without using txindex=1.

As I assume the transaction is unspent (and confirmed) I'm doing the following steps:

`bitcoin-cli gettxout 'txid'`

`bitcoin-cli getblockcount`

`bitcoin-cli getblockhash 'blockcount - txout.confirmations + 1'`

`bitcoin-cli getrawtransaction 'txid' 0 'blockhash'`

I have a race condition when getting the blockcount.

Is there a better way to do that?

**Tags:** json-rpc, raw-transaction, bitcoin-cli, txindex


---

## 1 answers

### Answer by username_1 on 2021-09-01. Score: 1

To avoid a race condition with `getblockcount`, you could use `getblockheader` with the `bestblock` result from `gettxout` and get its height. Then do the height calculation with the number of confirmations to get the block hash to pass to `getrawtransaction`.


---

