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 ?

Does a node validates a transaction that comes with a block if it's already in its mempool?

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.


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.

  • 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 Apr 9 at 7:54
  • Thanks @Andrew Your answer at link is also very very helpful. Somehow I could not find it when I was searching earlier. Thanks. – dNyaanopaasak Apr 9 at 8:28

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