1

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?

user44941
  • 11
  • 3

1 Answers1

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.

Andrew Chow
  • 67,209
  • 5
  • 76
  • 149