0

I understand that each block on the Bitcoin blockchain contains a list of transactions, but let's assume (for the sake of this question) that the block in question contains only a single transaction--and that this particular transaction has a history of previous transactions (i.e., it was not a coinbase transaction). How would I figure out which block holds this single transaction's previous transaction(s)? I know that each transaction contains the hash of the previous transaction--but how do you find that hash without looking into every block on the blockchain? In other words, how can I quickly discern a transaction hash chain for a given transaction ID? Any links to explanations or previous questions would be appreciated--I tried searching and haven't seen anything that answers my question to my satisfaction.

Thank you.

3 Answers 3

4

-but how do you find that hash without looking into every block on the blockchain?

You can't. Of course, if you are smart, when you receive a block, you extract the data that you need and put it into a database where you store this information and can quickly look it up. So, starting from the beginning of the blockchain, you read through every block and every transaction, compute their hashes, and store in a separate database the hash and a pointer to where you can find the block or transaction (or something of that sort). So when you need to look up the outputs that a transaction spends, you can just lookup the txid in your database and be able to quickly find the transaction output that you need.

This is similar to what Bitcoin Core does. After it fully validates a block, it adds the UTXOs that are created to a separate database. That database contains the details for all of the UTXOs, so it has each UTXOs outpoint (txid + output index), the output value, and the output script. When a transaction spends a UTXO, it can be quickly found using the outpoint specified in the transaction and then the UTXO is removed from the database. The outputs of that transaction are then added to the database as new UTXOs.

1
  • "Ah-hah!" is all I can say. :)
    – Jazimov
    May 22, 2018 at 2:04
1

When nodes start up and sync the blockchain, they create and maintain something known as the utxo database. This contains all transaction outputs that have not been spent.

As new blocks are broadcasted, the inputs being spent in the transactions in those blocks are removed from the utxo, and the new outputs that were created are added in. This will usually happen even when a tx is broadcast, before it is mined into a block. Trying to broadcast an additional tx with the same inputs will fail with a mempool-conflict.

This database is saved to disk, so shutting down and starting up a node is not as intensive as the first sync and scan.

The raw blockchain itself is simply a series of series of serialized blocks and their transactions - The UTXO set is not a separate part of it, and must be constructed by iterating over it block by block.

0

The ledger i.e. Block chain is stored on every computer (node) that partakes in the network. ... Block chain is stored on all the computers running Bitcoin node. There are thousands of computers running full node. Each of these node record every new transaction in the blockchain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.