<blockquote>
  <p>Does that mean that if mempool contains a transaction saying "A" gave "B" 0.1 BTC then miner is supposed to actually make sure that "A" has that much unspent BTC before he can select that transaction into a block to be mined? </p>
</blockquote>

<p>This is a misunderstanding of how bitcoin transactions work, which assumes that coins are "spent from a wallet," which is not the case.</p>

<p>All transactions spend one or more previous transaction outputs (TXOs). The outputs are always spent in full, and become the inputs for the new transaction. Each transaction output is locked with a public key hash, such that only the person who knows the private key related to the public key can unlock the output to spend it. This is done by digitally signing the spending transaction with the private key.</p>

<p>A wallet, such as the one belonging to <code>A</code>, is simply a collection of private/public key pairs - one pair for each TXO which belongs to <code>A</code>. (Although the same key can be used for multiple TXOs, this is discouraged). The balance of wallet <code>A</code> is the sum of balances for all of the unspent transaction outputs (UTXO) for which the wallet has the private key to unlock and spend.</p>

<p>So when <code>A</code> wants to pay 0.1BTC to <code>B</code>, he will pick one or more of the UTXOs he is able to spend such that the collective balance of them is at least 0.1BTC. These will become the input of his new transaction. The transaction will contain an output with a public key belonging to <code>B</code> (their bitcoin address) for the amount of 0.1BTC. The transaction may also contain a change output, which spends the difference between the the sum of the TXOs which were spent by <code>A</code> and the 0.1BTC payment to <code>B</code>, minus transaction fees - this change output spends to a new address owned by <code>A</code>. These two outputs become new UTXOs, and the previous ones become spent.</p>

<p>Miners do not have any knowledge of the wallet balances of <code>A</code> or <code>B</code> - they can only see the amount of each UTXO, and they determine whether they are allowed to be spent based on whether the signature in the transaction is the correct one for the public key which locks the UTXO. They determine that the sum of output amounts is less than the sum of input amounts, and the difference between the two becomes the transaction fee which the miner collects. If all the checks pass, the transaction is valid.</p>

<p>Miners therefore, only need to keep a reference about all of the UTXOs on the blockchain to determine that they've not yet been spent. All previous historical transactions which were spent are archived and do not need to be accessed.</p>

<p>In Bitcoin Core, all of the block data is archived, and the software also maintains an index called the UTXO set, which says where each UTXO can be found in the archive. The UTXO set is held in a memory-mapped database, for performance.</p>

<p>The UTXOs are identified by transaction id - which is the double-SHA256 of the full transaction data found in a block somewhere, and the index of the output in that transaction. (Collectively referred to as a transaction out-point).</p>

<p>When new transactions or blocks come in from the network, the miner must validate them all. Each transaction's input contains a transaction out-point which must be looked up in the UTXO set - and the signatures must all be verified to correspond to the public keys which can spend the output (done as part of a script, which must always evaluate successfully). If any checks fail, the transaction or block is invalid. If they all succeed, then the UTXOs which are spent by the transactions are removed from the UTXO index and the new outputs are added to it.</p>

CC BY-SA 4.0
2019-04-09T07:54:23.797Z
Andrew thanks for the other link, **Thank you very much Mark**. Your answer helped to clarify my doubt. So basically what you are saying is that UTXO set which is held in a memory mapped database for quick access, is essentially a smaller subset ( probably less than 20 percent of the whole Blockchain data). So every miner before picking up any transaction from mempool, must walk through all the UTXOs for A and make sure that he has those many unspent BitCoins to begin with. Is that correct? So the miners do make sure that sender party in every transaction has that much balance.
dNyaanopaasak
CC BY-SA 4.0
2019-04-09T08:28:14.143Z
**Thanks @Andrew** Your answer at [link](https://bitcoin.stackexchange.com/questions/85648/how-is-a-bitcoin-transaction-validated) is also very very helpful. Somehow I could not find it when I was searching earlier. Thanks.
dNyaanopaasak
CC BY-SA 4.0
2019-04-09T04:21:40.327Z
2019-04-09T04:21:40.327Z
Mark H
<p>I have read and searched enough about the roles and responsibilities of miners here; 
For example:
<a href="https://bitcoin.stackexchange.com/questions/65105/does-the-miner-first-validate-transaction-before-doing-pow-or-he-does-not-valida">Does the miner first validate transaction before doing POW or he does not validate at all ?</a></p>

<p><a href="https://bitcoin.stackexchange.com/questions/72871/does-a-node-validates-a-transaction-that-comes-with-a-block-if-its-already-in-i">Does a node validates a transaction that comes with a block if it&#39;s already in its mempool?</a></p>

<p>But this part is still a bit unclear to me. If a miner confirms a block that has an invalid transaction then he will not only waste too many resources in doing the PoW but he may also get penalized for that, so he validates every transaction.
Does that mean that if mempool contains a transaction saying "A" gave "B" 0.1 BTC then miner is supposed to actually make sure that "A" has that much unspent BTC before he can select that transaction into a block to be mined? If a miner has to validate each and every transaction like this then it will mean too much work, because the only way you can get a confirmed balance for any account is to literally start from the genesis block all the way to the current block. I understand that there are WebServices that give you balance for any bitcoin address but in turn they must be traversing the whole blockchain. So the question is does the miner actually makes sure that "A" had enough unspent BTC before this transaction or does it perform some other simpler check, before selecting and transaction from the mempool.</p>

<transaction-verification><validation><utxo-set>
How miner makes sure that a transaction he picked up from mempool is actually valid?
dNyaanopaasak
# How miner makes sure that a transaction he picked up from mempool is actually valid?

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

I have read and searched enough about the roles and responsibilities of miners here;
For example:
[Does the miner first validate transaction before doing POW or he does not validate at all ?](https://bitcoin.stackexchange.com/questions/65105/does-the-miner-first-validate-transaction-before-doing-pow-or-he-does-not-valida)

[Does a node validates a transaction that comes with a block if it's already in its mempool?](https://bitcoin.stackexchange.com/questions/72871/does-a-node-validates-a-transaction-that-comes-with-a-block-if-its-already-in-i)

But this part is still a bit unclear to me. If a miner confirms a block that has an invalid transaction then he will not only waste too many resources in doing the PoW but he may also get penalized for that, so he validates every transaction.
Does that mean that if mempool contains a transaction saying "A" gave "B" 0.1 BTC then miner is supposed to actually make sure that "A" has that much unspent BTC before he can select that transaction into a block to be mined? If a miner has to validate each and every transaction like this then it will mean too much work, because the only way you can get a confirmed balance for any account is to literally start from the genesis block all the way to the current block. I understand that there are WebServices that give you balance for any bitcoin address but in turn they must be traversing the whole blockchain. So the question is does the miner actually makes sure that "A" had enough unspent BTC before this transaction or does it perform some other simpler check, before selecting and transaction from the mempool.

**Tags:** transaction-verification, validation, utxo-set


---

## 1 answers

### Answer by username_1 on 2019-04-09. Score: 1 | Accepted answer

> Does that mean that if mempool contains a transaction saying "A" gave "B" 0.1 BTC then miner is supposed to actually make sure that "A" has that much unspent BTC before he can select that transaction into a block to be mined?

This is a misunderstanding of how bitcoin transactions work, which assumes that coins are "spent from a wallet," which is not the case.

All transactions spend one or more previous transaction outputs (TXOs). The outputs are always spent in full, and become the inputs for the new transaction. Each transaction output is locked with a public key hash, such that only the person who knows the private key related to the public key can unlock the output to spend it. This is done by digitally signing the spending transaction with the private key.

A wallet, such as the one belonging to `A`, is simply a collection of private/public key pairs - one pair for each TXO which belongs to `A`. (Although the same key can be used for multiple TXOs, this is discouraged). The balance of wallet `A` is the sum of balances for all of the unspent transaction outputs (UTXO) for which the wallet has the private key to unlock and spend.

So when `A` wants to pay 0.1BTC to `B`, he will pick one or more of the UTXOs he is able to spend such that the collective balance of them is at least 0.1BTC. These will become the input of his new transaction. The transaction will contain an output with a public key belonging to `B` (their bitcoin address) for the amount of 0.1BTC. The transaction may also contain a change output, which spends the difference between the the sum of the TXOs which were spent by `A` and the 0.1BTC payment to `B`, minus transaction fees - this change output spends to a new address owned by `A`. These two outputs become new UTXOs, and the previous ones become spent.

Miners do not have any knowledge of the wallet balances of `A` or `B` - they can only see the amount of each UTXO, and they determine whether they are allowed to be spent based on whether the signature in the transaction is the correct one for the public key which locks the UTXO. They determine that the sum of output amounts is less than the sum of input amounts, and the difference between the two becomes the transaction fee which the miner collects. If all the checks pass, the transaction is valid.

Miners therefore, only need to keep a reference about all of the UTXOs on the blockchain to determine that they've not yet been spent. All previous historical transactions which were spent are archived and do not need to be accessed.

In Bitcoin Core, all of the block data is archived, and the software also maintains an index called the UTXO set, which says where each UTXO can be found in the archive. The UTXO set is held in a memory-mapped database, for performance.

The UTXOs are identified by transaction id - which is the double-SHA256 of the full transaction data found in a block somewhere, and the index of the output in that transaction. (Collectively referred to as a transaction out-point).

When new transactions or blocks come in from the network, the miner must validate them all. Each transaction's input contains a transaction out-point which must be looked up in the UTXO set - and the signatures must all be verified to correspond to the public keys which can spend the output (done as part of a script, which must always evaluate successfully). If any checks fail, the transaction or block is invalid. If they all succeed, then the UTXOs which are spent by the transactions are removed from the UTXO index and the new outputs are added to it.

> **Comments:**
> **username_0**: Andrew thanks for the other link, **Thank you very much Mark**. Your answer helped to clarify my doubt. So basically what you are saying is that UTXO set which is held in a memory mapped database for quick access, is essentially a smaller subset ( probably less than 20 percent of the whole Blockchain data). So every miner before picking up any transaction from mempool, must walk through all the UTXOs for A and make sure that he has those many unspent BitCoins to begin with. Is that correct? So the miners do make sure that sender party in every transaction has that much balance.  
> **username_0**: **Thanks @Andrew** Your answer at [link](https://bitcoin.stackexchange.com/questions/85648/how-is-a-bitcoin-transaction-validated) is also very very helpful. Somehow I could not find it when I was searching earlier. Thanks.  

---

