<blockquote>
  <p>To do this the miners have to traverse through huge number of blocks to the block of then, won't it take much time?</p>
</blockquote>

<p>When A wants to make a payment, he would reference the transaction <code>txid</code> and <code>n</code> in the input from where he earnt the bitcoins that he now wishes to spend. Full nodes generate an indexed database of all unspent transaction outputs (UTXOs), and refers to it when checking transactions for validity. This data is stored in <code>$DATADIR/chainstate</code> and aggressively cached in memory. Thus the nodes do not have to go through all the blocks prior to that, because they have all the unspent outputs handy. This functionality also allows full nodes to <code>prune</code> the blockchain to a desired size without losing the capability of verifying the transactions independently.</p>

CC BY-SA 4.0
2019-04-17T12:24:13.043Z
Does Merkle tree plays any role in this?
abhishek
CC BY-SA 4.0
2019-04-17T12:27:41.367Z
@abhisheksingla Merkle tree is basically constructed by grouping and hashing the `txids` of the transactions in the block until you get to the root. It can no way tell, if that transaction has been spent earlier. They can be used to inform, say a simplified node, that a particular transaction has been included in the block by providing them a merkle path.
Ugam Kamat
CC BY-SA 4.0
2019-04-17T13:05:40.013Z
can we say that merkle tree reduces the time to reach a particular transaction by providing the merkle path which should be the order of O(log(n)) being a binary tree? or is it a totally different thing and only "aggressively cached in memory" is responsible for verifying faster?
abhishek
CC BY-SA 4.0
2019-04-17T13:17:18.053Z
@abhisheksingla the latter. Caching UTXOs in the RAM is what allows faster transaction validation. If you do not have the UTXO set, you have to go through the entire blockchain to see that the transaction and vout referenced has not been spent earlier. In fact, when you initialize a full node, this UTXO set is built from scratch by checking all the way up from the genesis block.
Ugam Kamat
CC BY-SA 4.0
2019-04-17T11:58:16.170Z
2019-04-17T12:21:40.240Z
2019-04-17T12:21:40.240Z
Ugam Kamat
<p>Miners check the validity of a transaction by checking if its in the <code>UTXO set</code>. UTXO set are all the transaction that are unspent. Miners and every full node maintains the UTXO set in level db (I think!). So in order to see if a transaction is unspent or not, miner just need to check if the transaction is in UTXO set by using the transaction id.</p>

CC BY-SA 4.0
2019-04-17T12:23:56.440Z
Does Merkle tree plays any role in this?
abhishek
CC BY-SA 4.0
2019-04-17T12:35:30.840Z
I don't think merkle tree play any role in verifying an unspent transaction. Merkle tree is formed by the transactions that is included in a specific block.
spyder
CC BY-SA 4.0
2019-04-17T11:57:53.133Z
2019-04-17T11:57:53.133Z
spyder
<p>By verification I do not mean the puzzle solving/hash calculating part but the part involving searching all of the blocks to check if the transaction is valid or not? </p>

<p>For example if A transferred some coins to B ten years ago and after that A didn't make any transaction. Now A wants to make some transaction. To verify this transaction, the miners need to know A's balance which was last updated ten years ago. To do this the miners have to traverse through huge number of blocks to the block of then, won't it take much time?</p>

<blockchain><mining-theory>
Is the time taken for the verification of a transcation not much relevant?
abhishek
# Is the time taken for the verification of a transcation not much relevant?

(Asked by: username_0 on 2019-04-17)

By verification I do not mean the puzzle solving/hash calculating part but the part involving searching all of the blocks to check if the transaction is valid or not?

For example if A transferred some coins to B ten years ago and after that A didn't make any transaction. Now A wants to make some transaction. To verify this transaction, the miners need to know A's balance which was last updated ten years ago. To do this the miners have to traverse through huge number of blocks to the block of then, won't it take much time?

**Tags:** blockchain, mining-theory


---

## 2 answers

### Answer by username_1 on 2019-04-17. Score: 1

> To do this the miners have to traverse through huge number of blocks to the block of then, won't it take much time?

When A wants to make a payment, he would reference the transaction `txid` and `n` in the input from where he earnt the bitcoins that he now wishes to spend. Full nodes generate an indexed database of all unspent transaction outputs (UTXOs), and refers to it when checking transactions for validity. This data is stored in `$DATADIR/chainstate` and aggressively cached in memory. Thus the nodes do not have to go through all the blocks prior to that, because they have all the unspent outputs handy. This functionality also allows full nodes to `prune` the blockchain to a desired size without losing the capability of verifying the transactions independently.

> **Comments:**
> **username_0**: Does Merkle tree plays any role in this?  
> **username_1**: @abhisheksingla Merkle tree is basically constructed by grouping and hashing the `txids` of the transactions in the block until you get to the root. It can no way tell, if that transaction has been spent earlier. They can be used to inform, say a simplified node, that a particular transaction has been included in the block by providing them a merkle path.  
> **username_0**: can we say that merkle tree reduces the time to reach a particular transaction by providing the merkle path which should be the order of O(log(n)) being a binary tree? or is it a totally different thing and only "aggressively cached in memory" is responsible for verifying faster?  
> **username_1**: @abhisheksingla the latter. Caching UTXOs in the RAM is what allows faster transaction validation. If you do not have the UTXO set, you have to go through the entire blockchain to see that the transaction and vout referenced has not been spent earlier. In fact, when you initialize a full node, this UTXO set is built from scratch by checking all the way up from the genesis block.  

---

### Answer by username_2 on 2019-04-17. Score: 0

Miners check the validity of a transaction by checking if its in the `UTXO set`. UTXO set are all the transaction that are unspent. Miners and every full node maintains the UTXO set in level db (I think!). So in order to see if a transaction is unspent or not, miner just need to check if the transaction is in UTXO set by using the transaction id.

> **Comments:**
> **username_0**: Does Merkle tree plays any role in this?  
> **username_2**: I don't think merkle tree play any role in verifying an unspent transaction. Merkle tree is formed by the transactions that is included in a specific block.  

---

