I am working on an educational project for bitcoin mining. I searched as much as I could but still haven't understood the following questions:
- What are the "exact" degrees of freedom in finding a valid block? (What exactly is the search space?)
- What guarantees that within those degrees of freedom (search space) a valid block "will" be found?
More details about my questions:
I know that a miner asks for a block template via "getblocktemplate" RPC command. Based on the "bits" field it can calculate the target hash relative to which the valid block's hash needs to be smaller than. I know over the course of mining, the network may create new block templates with updated info (for example additional transactions are added). So the miner can use the "longpool" feature to have the most updated version at all times. Moreover, the miner can "choose" which transactions to include in the next block. So they can exclude some transactions (as long as they are not marked mandatory). Furthermore, the miner can choose an arbitrary message string for the coinbase transaction. They can also play with the timestamp to some extend. So for each of these choices they go into a for loop over the "nonce" field from 0 to 2^32-1. If they cover all nonces (for a fixed set of choices) and still the target hash condition is not satisfied they can alter their choices now: for example choose a different message string for coinbase transaction and/or include/exclude transactions and/or modify timestamp.
First of all is my understanding accurate? In other words are the only degrees of freedom the following?:
- choice of coinbase transaction message
- choice of list of transactions to be included in the next block
- nonce field (what is the concept of extranonce? is it just part of the arbitrary message string for coinbase transaction?)
Second, what guarantees that within this search space, a valid block will be found? (is it just a probabilistic guarantee?)
My apologies in advance if this is a newbie question. I searched a lot and could not get an exact answer.