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
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.
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.