# Bitcoin mining process-the leading zeros before the hash-how is the mining puzzle solved exactly?

In Bitcoin proof of work, mining is done to create and validate new blocks. Who becomes the sole block producer is subject to winning a "mining puzzle".

As I understand, the difficulty is that the solution of this puzzle must have a specific number of leading zeros in the resulting hash.

My questions are:

1) must the hashing result have X number of leading zeros, or is the constraint more like "less or equal X number of leading zeros"?

2) If the mining puzzle's solution has a hash as a result, what is then the input parameters for doing the hashing? What are the miners exactly hashing, what are all the input factors? As i understand, input factors are e.g. the Nonce, previous Block's hash, etc...

3) I understand a hash function is only one way function. You have an Input which produces a unique hash. This means that if multiple different parties are hashing the same input then their hash results must be identical, right?

Given this definition, I really do not understand how the situation of orphan blocks in Bitcoin can happen? Because orphans happen for example when 2 miners at the same time solve the mining puzzle. But both are solving the exact same mining puzzle right? This means, they both have dealt with the same difficulty level of the puzzle and arrived at the same number of leading zeros for their hash, right?

How is the blockchain then distinguishing between those 2 miners, who has "put more work into the mining" if they have solved the same thing? On which parameters is the network looking to determine which of the two miners should get the reward?

Thanks

I understand a hash function is only one way function. You have an Input which produces a unique hash. This means that if multiple different parties are hashing the same input then their hash results must be identical, right?

Multiple parties will never be hashing the same information. Either the parties are coordinating or they're not. If they're coordinating, they'll carefully avoid doing the same work because to do otherwise is foolish. If they're not coordinating, they can't possibly be doing the same work. I'm trying to find a hash that pays me money. If you're not coordinating with me, why would you also be doing that?

Given this definition, I really do not understand how the situation of orphan blocks in Bitcoin can happen? Because orphans happen for example when 2 miners at the same time solve the mining puzzle. But both are solving the exact same mining puzzle right? This means, they both have dealt with the same difficulty level of the puzzle and arrived at the same number of leading zeros for their hash, right?

Probably. But they will produce different blocks since they're either trying to mine to different destination addresses or carefully coordinating to avoid duplication fo work.

How is the blockchain then distinguishing between those 2 miners, who has "put more work into the mining" if they have solved the same thing? On which parameters is the network looking to determine which of the two miners should get the reward?

When the next block is found, whichever block the miner who found that block built on top of will be in a longer chain than the other, causing everyone to switch over to the longer chain. To get to keep your mining reward, the block you found must stay in the longest chain. To have the highest odds of this, you always try to mine to extend the chain that is already the longest. So any ties are quickly broken -- as soon as a block on top of one of the longest chains currently existing is found and no other block is found at around that same time, nearly everyone will be building on top of that block.

1. At least X number of leading zeroes.

More leading zeroes are harder. Fewer is not hard enough! Note that any arbitrary list of digits would be equally hard. A list of all zeroes is just a convenient choice used by Bitcoin. Miners are finding a hash whose first n digits matches some arbitrary number. They do it by generating huge numbers of hashes (varying the inputs trivially) and discarding hash results whose first n digits don't match the whole required sequence. The bigger n is, the harder it is to find such a hash. It would be trivial to find a hash with < n digits (any old hash would do, no significant work would be required).

A luicky miner's first attempt might produce a hash with too many zeroes. That would be fine. Nobody cares whether the remainder of the hash has any special properties (other than being part of a valid hash)

2. See What exactly is Mining? Its a bunch of stuff, including what you listed but mainly including transaction details of course (usually, you can have blocks without transactions but that is rare).

3. Yes, same inputs = same hash result. Hash functions must be deterministic but non-reversible (effectively).

He who publishes a block first wins, mostly. First meaning first seen by adjacent nodes.

"Orphan" blocks are better called something like abandoned blocks as they still have valid parents.

These are blocks that didn't get enough work built on top of them and were replaced by a more productive end-chain AFAIK.