<p>It depends on how you have configured your bitcoind and what commands you are using.</p>

<p><code>getrawtransaction</code> first searches the mempool for your txid. The transactions in the mempool are held in memory and can be looked up by txid. If the transaction is not in the mempool. then you need to either provide the hash of the block it can be found in, or you need to have the transaction index enabled (by starting with <code>-txindex</code> or adding <code>txindex=1</code> to your bitcoin.conf).</p>

<p>If you provide the hash of the block the tx can be found in, then the block's location on disk is looked up in the block index. The block is then deserialized and searched linearly for the tx.</p>

<p>If you have the transaction index enabled, then the txid is looked up in the transaction index. The transaction index maps txids to a location on disk (similar to how the block index maps block hashes to a location on disk). So this database allows for fast lookups too.</p>

<p>The <code>gettransaction</code> RPC is different from <code>getrawtransaction</code>. This is a wallet only RPC. The wallet stores entire transactions. So <code>gettransaction</code> just looks up the txid in the map that stores the transactions.</p>

CC BY-SA 4.0
2020-04-03T17:07:22.890Z
2020-04-03T17:07:22.890Z
Ava Chow
<p>I am wondering how the daemon gets the transaction data from just a hash. I'm NOT asking how <em>i</em> can get it, i know i can get it via the rpc, i am asking how the daemon does it behind the scenes. Does it have a db of every transaction that has taken place and the block it was in? or does it just recursively search through every block?
Im stumped how it does this efficiently. Thanks</p>

<transactions><hash><getrawtransaction>
How does the daemon get a transaction via a hash
Leah Cornelius
# How does the daemon get a transaction via a hash

(Asked by: username_0 on 2020-04-03)

I am wondering how the daemon gets the transaction data from just a hash. I'm NOT asking how *i* can get it, i know i can get it via the rpc, i am asking how the daemon does it behind the scenes. Does it have a db of every transaction that has taken place and the block it was in? or does it just recursively search through every block?
Im stumped how it does this efficiently. Thanks

**Tags:** transactions, hash, getrawtransaction


---

## 1 answers

### Answer by username_1 on 2020-04-03. Score: 2 | Accepted answer

It depends on how you have configured your bitcoind and what commands you are using.

`getrawtransaction` first searches the mempool for your txid. The transactions in the mempool are held in memory and can be looked up by txid. If the transaction is not in the mempool. then you need to either provide the hash of the block it can be found in, or you need to have the transaction index enabled (by starting with `-txindex` or adding `txindex=1` to your bitcoin.conf).

If you provide the hash of the block the tx can be found in, then the block's location on disk is looked up in the block index. The block is then deserialized and searched linearly for the tx.

If you have the transaction index enabled, then the txid is looked up in the transaction index. The transaction index maps txids to a location on disk (similar to how the block index maps block hashes to a location on disk). So this database allows for fast lookups too.

The `gettransaction` RPC is different from `getrawtransaction`. This is a wallet only RPC. The wallet stores entire transactions. So `gettransaction` just looks up the txid in the map that stores the transactions.


---

