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.