# Mining

## The Nonce

The cryptographic puzzle in Bitcoin mining is the process of finding a Nonce (number used only once) that generates a valid hash for the current block. The Nonce is an integer number that is used as input for the SHA256 cryptographic function, along with the Block Number, Data, and Previous Block's Hash.

The goal of the cryptographic puzzle is to make it difficult for miners to find a valid hash, but not impossible. This is done by setting a target hash that is constantly adjusted based on the network's hash rate (aggregate computational power of all Bitcoin miners). The more miners join the network, the lower the target hash will be, and therefore the harder it will be to find a valid hash.

The Bitcoin mining process is a complex computational process that involves verifying and adding new transactions to the Bitcoin blockchain. It is also the process by which new bitcoins are created.

**Here is a step-by-step explanation of the Bitcoin mining process:**

1. **Miners collect pending transactions from the mempool.** The mempool is a temporary storage area for unconfirmed transactions. Miners select transactions from the mempool and bundle them together into a block.
2. **Miners add a block header to the block.** The block header contains information such as the previous block hash, the timestamp, and the nonce. The nonce is a random number that is used to generate a hash value for the block.
3. **Miners try to find a nonce that generates a hash value that is less than or equal to the target hash.** The target hash is adjusted periodically to keep the mining difficulty at a desired level.\
   \
   The target hash in Bitcoin mining is a 256-bit number that miners must find a nonce that generates a hash value that is less than or equal to. The nonce is a random number that is used to generate a hash value for the block.
4. **Once a miner finds a valid nonce, they broadcast the block to the network.**
5. **Other nodes on the network verify the block and add it to the blockchain.**
6. **The miner who found the block is rewarded with bitcoins.**

**Bitcoin mining is a competitive process, and miners use specialized hardware called ASICs (application-specific integrated circuits) to increase their chances of finding a block.** ASICs are very expensive, and mining Bitcoin can be a costly undertaking.

**How miners reach the target value in Bitcoin mining:**

1. Miners collect pending transactions from the mempool and put them together in a block.
2. Miners add a block header to the block, which includes the block number, timestamp, previous block hash, nonce, and merkle root.
3. Miners try different values for the nonce until they find one that generates a hash value that is less than or equal to the target hash.
4. Once a miner finds a valid nonce, they broadcast the block to the network.
5. Other nodes on the network verify the block and add it to the blockchain.
6. The miner who found the block is rewarded with bitcoins.

**Where the target value is set:**

The target value is set by the Bitcoin protocol and adjusted every 2,016 blocks (approximately every two weeks) based on the network hashrate. The network hashrate is the total computational power of all Bitcoin miners.

The target value is a number that miners must beat in order to solve a block. The target value is adjusted every 2,016 blocks (approximately every two weeks) based on the network hashrate. The network hashrate is the total computational power of all Bitcoin miners.

**How miners know the target value:**

Miners can learn the current target value by connecting to a Bitcoin node. Bitcoin nodes are computers that maintain a copy of the Bitcoin blockchain and broadcast transactions and blocks to other nodes on the network.

#### &#x20;Bitcoin mining: Reaching the target value

Bitcoin mining is a process by which new bitcoins are created and transactions are verified. Miners use powerful computers to solve complex mathematical problems. The first miner to solve a problem is rewarded with bitcoins.

**The target value**

The target value is a number that miners must beat in order to solve a block. The target value is adjusted every 2,016 blocks (approximately every two weeks) based on the network hashrate. The network hashrate is the total computational power of all Bitcoin miners.

**How miners reach the target value**

Miners try to reach the target value by guessing nonces. A nonce is a random number that is used as input for the SHA-256 cryptographic function. Miners keep guessing nonces until they find one that generates a hash value that is less than or equal to the target value.

**Finding a valid nonce is difficult because the SHA-256 function is a one-way function.** This means that it is easy to compute the hash of a given input, but it is very difficult to reverse the function and find the input that generated the hash.

**The difficulty of mining is adjusted so that it takes about 10 minutes to find a block.** If it takes too long to find a block, then the blockchain will grow too slowly. If it takes too little time to find a block, then the blockchain will grow too quickly.

**Once a miner finds a valid nonce, they broadcast the block to the network.** Other nodes on the network verify the block and add it to the blockchain. The miner who found the block is rewarded with bitcoins.

**How do miners know the target value?**

\\

{% embed url="<https://blockchaindemo.io/>" %}
