Let's say 100 miners with similar hashrate all try at the same time to mine the same block and finish at the same time, how will Bitcoin choose to which miner the reward will go? Is there some prority protocol?
It's all about probability. In about 10 minutes, a miner among those 100 will finally achieve to solve the proof of work for the given block before others will. So, he then will write that block on the blockchain, using it's address in the receiving address field of the coinbase transaction, thus obtaining it's reward.
what happen if they all mine the block at the same time
In that case, the blockchain would be temporary forked on the block at height X, given X the number of the block been mined at the same time by more than one miner. This won't last forever. Remember the golden rule of bitcoin:
the longest chain win. Among those forks, one will definitely be resolved (again, it's all about probability at the end of the day), meaning more hashrate will be brought to it, so it will grow faster (speaking about blockheight) than the other. That one will become the current blockchain.
What happens to the other forks? Well, they're simply not valid blockchains. They contain outdated data. If forking miner continue to mine on those blockchain, they'll do at a lost, because they will get rewards on a blockchain which is not the main one
Is there some priority protocol? No, absolutely. Just probability. The more hashrate you have, the more probably you can mine the next block
how the precise time of completion is calculated (is it using the miner's computer clock as timestamp? From the bitcoin wiki
A timestamp is accepted as valid if it is greater than the median timestamp of previous 11 blocks, and less than the network-adjusted time + 2 hours. "Network-adjusted time" is the median of the timestamps returned by all nodes connected to you. As a result block timestamps are not exactly accurate, and they do not need to be. Block times are accurate only to within an hour or two.Whenever a node connects to another node, it gets a UTC timestamp from it, and stores its offset from node-local UTC. The network-adjusted time is then the node-local UTC plus the median offset from all connected nodes.
I edited my question to ask more precisely about wht happen if they all mine the block at the same time. Is there some priorioty protocol? I also wonder how the precise time of completion is calculated (is it using the miner's computer clock as timestamp?).– StrobJul 28, 2020 at 2:28
Time in bitcoin isn't precise nor reliable, every peer can have their own idea of time and every peer has a view of other peers time that lead a single peer to have knowledge about time drift (
Any miner can mine using past or even future timestamps but there are rules that enforce blocks to be generated not too much in the past (using the median time of previous blocks) and not too much in the future (using the
adjusted time from your known peers)
Having said that, mining is a race, if many mine at the same time the network will have temporary different views based on peers and their neighborhood (e.g. if you are connected directly to a miner, you'll likely get first the block from it while if you have not direct connection to a miner but one of your peers does, you'll likely get the block from that miner too, basically it's about latency between nodes)
At that point your node have a view that will put one of these mined blocks as your TIP, if you are a miner and you succeed to find the next block before others add a block on other forks, you'll likely have a chance that your view will become the valid one for the rest of the network (if other big miners don't decide to ignore your block and try to mine on their own vision, in case they found the previous block and have incentives to do so)
Well, I understand my explaination isn't probably easy to follow because there are lot of nuances behind, in any case the short ELI5 answer is: when multiple miners mine a block at the same time, the network will sort it out waiting for the longer chain to emerge from forks.
EDIT P.S. note that when all of them mine, all of them get the reward in their own view, because all of them think to be on the tip of the chain. But only one of these blocks will survive because the longest chain based on these forks will become the valid one and so other miners will have to rollback their own view up to the fork point (so losing the reward they thought they won) and moving to the now-valid chain