Suppose there is a miner who consistently cheats. That is, she will include "Alice sent me 100 bitcoins" in every block she creates. If she became the first miner to find a passable hash, then she would get 100 coins. This scenario is not impossible. How does the mining algorithm prevent this?
4 Answers
This isn't possible because every node in the network enforces the consensus rules.
A miner is only allowed to include 1 transaction that moves coins with no previous inputs, which is the coinbase transaction. The output value of the coinbase transaction must not exceed the block reward plus any transaction fees from transactions in that block. If the value exceeds this amount, the block will be rejected by the network, and the miner loses the work they did to create it.
If the miner includes the "Alice sent me 100 bitcoin" tx by using previous outputs from Alice as inputs, they must be part of a valid transaction. That is to say, Alice must have signed those inputs to prove that she wants the BTC to be spent. If the miner includes them as unsigned inputs, the transaction is invalid, and any block they create including it is also invalid.
The mining algorithm does nothing to prevent this. However, the mining algorithm is just a part of the larger consensus algorithm. This is the algorithm that is not just implemented by a single participant, but by the network as a whole.
If a block includes a transaction that says "Alice sent me 100 bitcoins", it would need to contain a valid signature from Alice. If it doesn't, then the transaction is invalid as well as the block that contains it. Every full node on the network checks this transaction when the block is propagated, and will ignore the block as spam if it doesn't validate. If a miner were to put in the computing power to generate this block, knowing full well that it will be ignored, then they are wasting a considerable amount of their own money. This is how Bitcoin incentivizes good participants.
A miner isn't able to add anything they want to the block. Each transaction they include still has to be valid. For all but the most esoteric transactions, part of the requirements of being valid is that the transaction contains a signature proving that the creator of the transaction knows the private key of the transaction inputs.
In other words, nothing about mining gives you any power over creating valid transactions. To create a valid transaction you still need to be the actual owner of the bitcoins going into that transaction. Mining gives you control over what transactions are included in the block you are mining but gives you no special abilities or control over transaction validity.
In the same way that a miner can't just create bitcoins out of nowhere (aside from their mining reward), they also can't arbitrarily create transactions out of nowhere.
The transaction "Alice sends me 100 BTC" is not valid unless existing unspent transaction outputs are referenced in the transaction inputs and the transaction is signed correctly.
If either the referenced transaction inputs are no longer available for spending, or the signature is invalid, the transaction is invalid. If an invalid transaction is included in a block, the block is invalid. Invalid blocks are rejected by all full nodes since every full node enforces all consensus rules independently.
Hence, the mining algorithm doesn't prevent creation of invalid blocks, but the remaining network will reject the block and the mining work invested by the invalid block's author is wasted.