Yes, each miner decides which transactions to select into their block templates. Blocks must have a coinbase transaction, all other transactions are optional, but blocks are limited to 4,000,000 weight units.
Let me elaborate: unconfirmed transactions are relayed on the network by the participating nodes via the p2p gossip protocol. Each node maintains their own list of unconfirmed transactions in its memory pool (mempool). Miners generally pick transactions from their mempool to assemble block templates.
Blocks must have a coinbase transaction as the first transaction in the block, and the remaining transactions must be valid and sorted topologically. A block may not exceed 4,000,000 weight units. Other than that, miners are completely free to pick and choose the composition of their block. Blocks that only contain a coinbase transaction are allowed and referred to as empty-blocks.
It is expected that miners wish to optimize their revenue by collecting the most transaction fees possible. By default, the getBlockTemplate
call uses a greedy algorithm that iteratively picks the set of transactions with the highest effective feerate until the block is full. Miners may use the prioritisetransaction
rpc call to reprioritize transactions for the block template consideration. Some miners may use custom software to build their blocks.
In effect, block templates usually overlap almost completely across miners as nodes should see the same transactions on the network (with some slight delays due to relay). However, since each miner's block template attempts to pay the block reward to themselves via the coinbase transaction and each miner has a different payout address, every block template produced by miners will be unique and no miners will ever repeat work done by another miner.
Given a block template, miners search that template's combination space by injecting random data via varying nonces, extranonces, timestamps, malleating the version field, etc. Each new combination yields a unique block header which is hashed using SHA-256d. If a resulting digest meets the target, the miner has found a new block. Otherwise, they create a new block template and start over.
Once a new block is discovered, the block author broadcasts it to their peers. Other nodes that learn about the block append it to their blockchain, update their UTXO sets, and remove the confirmed transactions from their mempools. Since the best chain can only have one block at every height, it does not make sense for an honest miner to continue mining at the old height, therefore, they switch to mining at the new height as quickly as possible.