Bitcoin Forum
May 23, 2026, 06:21:44 PM *
News: Latest Bitcoin Core release: 31.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Need some clarity on fundamental of proof of work from miners point of view  (Read 45 times)
hmbdofficial (OP)
Member
**
Offline

Activity: 195
Merit: 46


View Profile
Today at 06:38:06 AM
Merited by stwenhao (1)
 #1

Lately I’ve considered looking at the mining aspect which I’ve not been actually paying attention to much attention to before. Now I’m left with different thoughts after reading about mining which I think I could get some clarification here or better explanation;
- I couldn’t figure out how the SHA -256 hashing function is used in Bitcoin’s proof of work, and also why it is computationally easy to verify but hard to solve for a valid nonce?

- i also will like to know the actual role of merkle root in the block header, as how it allows efficient verification of transactions inclusions.

- is it also possible that the miner modifies their block header during the mining process?

- lastly I’ll appreciate a little explanation on how bitcoin difficulty adjustment algorithms work? And also how is the new difficulty calculated after every 2016 block, and also the targeted block time it aims to maintain?.
stwenhao
Hero Member
*****
Offline

Activity: 696
Merit: 1850


View Profile
Today at 10:46:31 AM
 #2

Quote
I couldn’t figure out how the SHA -256 hashing function is used in Bitcoin’s proof of work, and also why it is computationally easy to verify but hard to solve for a valid nonce?
The easiest way to figure it out, is to write any implementation of SHA-256 in any programming language. It would take at most few hundreds lines of code, probably less. And then, you will observe exactly, how a given message is turned into its hash.

Some example implementation, where some empty message is hashed: https://github.com/stwenhao/small_curves/blob/master/sha256.cpp

If you don't have any C++ compiler, you can run it online, for example here: https://www.onlinegdb.com/online_c++_compiler

Example online calculator with SHA-256: https://emn178.github.io/online-tools/sha256.html

Step-by-step calculator: https://sha256algorithm.com/

Quote
i also will like to know the actual role of merkle root in the block header, as how it allows efficient verification of transactions inclusions.
This is how the block header looks like: https://mempool.space/api/block/000000000000000000001de7b0cafc932a6e092f2160aeb3ba9a9602b0e3c394/header
Code:
version:             0060c522
previous block hash: c8a15d5133b05ed8db59ce711da1836c2baee4de429200000000000000000000
merkle root:         1a78052604f22987120ede93134ad73330a112f3bbf74110ea5bea99feea8ff2
timestamp:           2780116a
difficulty:          790f0217
nonce:               62324994
When you have merkle root, then it is just some 256-bit field. It is calculated once, and then, miners can change version, timestamp, and nonce, while leaving everything else as it is. And then, it doesn't matter, if your block takes 1 kB, or 1 MB: you still hash exactly the same 80-byte block header, every time.

However, if there would be no merkle root, and if you would need to hash the whole block, then, suddenly, mining 1 MB block would be slower, than mining 1 kB block. And then, miners would have an incentive, to make blocks as empty, as they could be. But fortunately, it is not the case.

Quote
is it also possible that the miner modifies their block header during the mining process?
Miners have to do that. You need to change something, to make a different hash. This is why nonce is needed in the first place. Early miners changed only their nonces, and when they checked all 2^32 possible values, and didn't find a valid block, then they changed the extra nonce in the coinbase transaction.

Later, it turned out, that changing the block version, or the timestamp, is faster than that. Which is why miners started doing it. Because changing the merkle root is expensive: you need to calculate SHA-256 once again, for a different data. While flipping a bit in the block header directly, is much faster.

Quote
how bitcoin difficulty adjustment algorithms work?
It is simply measured, how much time it took, to produce the last 2016 blocks. It should take two weeks. If it took longer, then the difficulty goes down, if it was done faster, then it goes up.

Quote
And also how is the new difficulty calculated after every 2016 block
https://github.com/bitcoin/bitcoin/blob/master/src/pow.cpp#L14

There is a function "GetNextWorkRequired", which just counts, how long it took to mine the last 2016 blocks, and adjusts it.

Proof of Work puzzle in mainnet, testnet4 and signet.
Zaguru12
Legendary
*
Offline

Activity: 1428
Merit: 1224



View Profile WWW
Today at 10:53:45 AM
 #3

- I couldn’t figure out how the SHA -25and also why it is computationally easy to verify but hard to solve for a valid nonce?
This is because nodes are taking the block data from the node that was able to compute the Nonce

Quote
- i also will like to know the actual role of merkle root in the block header, as how it allows efficient verification of transactions inclusions.

Let’s say you have all your transactions in a block, this transaction IDs (TXIDs) are hashed together in pairs  called merkle tree it until actually have a unique fingerprint for this transactions in the that block, this is now the Merkle root that is in the block header with other unique block contents.

So the verification works in such a way that if all the transactions are actually hashed together then if one of the transactions is later removed or  another one is added  that  means a different hash result (merkle root) will be gotten  from the first transactions and this clears differs from what is in the Block header. And this clear shows that there is something wrong.

Quote

- is it also possible that the miner modifies their block header during the mining process?

Yes miners modify block headers during mining process to get a different hash example is modifying the nonce in  coinbase transaction (transaction which has the block reward), removing or adding transactions which modifies the merkle tree and subsequently the merkle root in the block header, the block time can still be modified for accuracy and all this changes the block header, but once the transaction is confirmed it cannot be modified.

Quote

- lastly I’ll appreciate a little explanation on how bitcoin difficulty adjustment algorithms work? And also how is the new difficulty calculated after every 2016 block, and also the targeted block time it aims to maintain?.

Just like the word difficulty itself, it’s how difficult it is for miners to actually find a new block, the average time is 10 minutes but it is not stagnant because of it is as new miners join it will take lesser time to mine blocks with more miners join. So the difficulty adjust after the 2016 block.

It works like this, when 2016 block is mined the average time is for all this blocks let’s say 8 minutes is used to calculate the next difficulty

First multiple the difficulty by 10 minutes ( the average time) =20160
Multiple the actually average time with the total blocks= 2016 x 8 = 16128
Dived the average difficulty by the actually difficulty in this period = 20160/16128= 1.25

It’s this 1.25 that’s now multiple by the difficulty to get the new difficulty for the next 2016 blocks before this process is actually done again

BattleDog
Full Member
***
Online Online

Activity: 244
Merit: 226



View Profile WWW
Today at 04:59:01 PM
 #4

Quote
Is it also possible that the miner modifies their block header during the mining process?

Yes. They change the nonce first, but the nonce is only 32 bits, so modern miners exhaust that very quickly. After that they usually change the coinbase transaction through an extraNonce, which changes the Merkle root and gives them a fresh header search space. They can also adjust timestamp within consensus rules, choose a different transaction set, ordering, version bits, and so on. So the miner is not just staring at one frozen block forever like a confused goat.

Quote
Lastly I'll appreciate a little explanation on how bitcoin difficulty adjustment algorithms works?

Every 2016 blocks, Bitcoin compares how long those blocks actually took versus the expected two weeks. If they came too fast, the target is lowered, making valid hashes harder to find. If they came too slow, the target is raised, making valid hashes easier to find.

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!