Author

Topic: A TECHNICAL question about blockchain, btc mining (Read 389 times)

sr. member
Activity: 279
Merit: 435
Hi,

Quote
I watched on the subject have made an impression that only in some cases it is necessary to change something in the block and start hashing all over again
sha256 is a deterministic function, if you don't change anything in the input, you get the same output. Thus you must change the input in order to change the output (the hash), and that's why there is the nonce (which is not large enough with the current difficulty, though).

Quote
Do I understand it correctly this time?
It seems so ^^

Quote
Really stupid question: can the hash change with a different order of transactions in the block or with a different repeat at the end (the end is repeated if there’s a an odd number of branches right?)
Yes. I think the merkle tree changes if the transaction order changes, which implies the merkle root (stored in the header) to be different, which implies the hash of the header to be different.
copper member
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
I always thought the leading zeros idea was too stupid for this system.

The difficulty would be too discrete as it would be doubled in difficulty with every increase in the number of zeros... I think that original explaination might actually have made a lot of people think as I did and think the network was too unstable. Although I knew it worked so it must have been more complex.

Really stupid question: can the hash change with a different order of transactions in the block or with a different repeat at the end (the end is repeated if there’s a an odd number of branches right?)
legendary
Activity: 3374
Merit: 2198
I stand with Ukraine.
I used to think that I understood how Bitcoin mining works, but after reading this thread I've changed my mind. Firstly I'm going to show what I thought it was to mine a block.

1. A miner validates the the very last block previously mined by someone else, in order to build a new block on top of it.

2. Miner constructs an empty block which contains a block header with the hash of the previous block, Version and a Time stamp.

3. Then the miner starts adding transactions to the block. The first one is the coinbase transaction which assigns the mining reward to the miner. Then miner picks up a few hundred transactions from the mempool, preferring those with the highest satoshis/byte ratio and adds them to the block too.

4. Miner makes a Merkle root by hashing the hashes of all the transactions in the block, and adds it(the Merkle root) to the block header.

5. Then the miner starts hashing the block, changing the nonce each time until it ends up with a hash which starts with a certain amount of zeros(18 zeros currently), or rather which is less than or equal to the target value, as @achow101 pointed out.

6.Then it is shared with other mining nodes and if it is validated, Voila!, the block reward is yours (along with all the fees associated with the txs in the block, and that's why transactions with the highest fees are preferred).

Now, what have I understood after reading this thread(sorry, it took so long to get to the point). Various articles and videos I watched on the subject have made an impression that only in some cases it is necessary to change something in the block and start hashing all over again, while in reality this is always the case. In reality, a miner never finds the right hash with the very first block assembled, and always has to try various combinations numerous times, because

... an ASIC machine can hash 12 trillion times PER SECOND. and our nonce is 2,097,181,953. so that means winning miner spent only fraction of a second to calculate 2 billions hashes.
~

Do I understand it correctly this time?
newbie
Activity: 8
Merit: 6
isnt the difficulty 7,019,199,231,177.173 right now? not 6,727,225,469,722  

You are right. The last difficulty was 6,727,225,469,722. Adjustment happened ~400 blocks ago.
Don't know where this old data came from  Grin



And I thought max value of a 32 bit can be 2,147,483,647 where did the 4,294,967,295 come from?

32 bit can represent 232 (= 4.294.967.296) different numbers. So from 0 to 4.294.967.295.

2,147,483,647 is the max number if you want to store positive and negative values inside a 32 bit variable.
But the overall amount of values a 32 bit integer can represent is 4.294.967.296


alright yeah things are clearer now haha Cheesy thanks.
legendary
Activity: 1624
Merit: 2481
isnt the difficulty 7,019,199,231,177.173 right now? not 6,727,225,469,722  

You are right. The last difficulty was 6,727,225,469,722. Adjustment happened ~400 blocks ago.
Don't know where this old data came from  Grin



And I thought max value of a 32 bit can be 2,147,483,647 where did the 4,294,967,295 come from?

32 bit can represent 232 (= 4.294.967.296) different numbers. So from 0 to 4.294.967.295.

2,147,483,647 is the max number if you want to store positive and negative values inside a 32 bit variable.
But the overall amount of values a 32 bit integer can represent is 4.294.967.296
newbie
Activity: 8
Merit: 6
Just one more thing, so the nonce has nothing to do with the difficulty number right?  

Correct.



so nonce can be greater than difficulty value? they are not related at all? if they are not related what is the maximum number a nonce can be?

Currently, the difficulty is 6,727,225,469,722.
And the nonce is a 4 byte (32 bit) number. So max value = 4.294.967.295

The nonce CAN be greater than the difficulty IF the difficulty is extremely low.
But nonce and difficulty are not related at all..



when the miner decides to change something in the block?

When he has tried out all nonces without a single one leading to a hash below the current target.





isnt the difficulty 7,019,199,231,177.173 right now? not 6,727,225,469,722 


And I thought max value of a 32 bit can be 2,147,483,647 where did the 4,294,967,295 come from?

legendary
Activity: 1624
Merit: 2481
Just one more thing, so the nonce has nothing to do with the difficulty number right? 

Correct.



so nonce can be greater than difficulty value? they are not related at all? if they are not related what is the maximum number a nonce can be?

Currently, the difficulty is 6,727,225,469,722.
And the nonce is a 4 byte (32 bit) number. So max value = 4.294.967.295

The nonce CAN be greater than the difficulty IF the difficulty is extremely low.
But nonce and difficulty are not related at all..



when the miner decides to change something in the block?

When he has tried out all nonces without a single one leading to a hash below the current target.

newbie
Activity: 8
Merit: 6
achow101 thanks a lot, it all makes sense now. Great explanation. And what you said led me more digging into details.

this is what I understand, in summary;

example for block 540494


Code:

difficulty = 7019199231177.173

maxTarget (Hexadecimal) = 0x00000000FFFF0000000000000000000000000000000000000000000000000000

maxTarget (Decimal) 26959535291011309493156476344723991336010898738574164086137773096960

target = maxTarget / difficulty

target (decimal) = 3840827764407250020633179921320621786723403550981928027



so calculated hash must be less than or equal to the target value...


Code:

target (hexadecimal) = 2819A0FFFFFFFF791A56A92A5F71C70648653408B0B45B

which is represented as follows;

0000000000000000002819A0FFFFFFFF791A56A92A5F71C70648653408B0B45B

and using the nonce '2097181953' the valid hash for this block;

00000000000000000024ADA2B097CB7B76BB751751DADC55A47DB2238863EE96


so it is clear to see that the hash is less than the target...

I understand why there are zeros. it is just the representation. it all makes sense now..

Just one more thing, so the nonce has nothing to do with the difficulty number right?  so nonce can be greater than difficulty value? they are not related at all? if they are not related what is the maximum number a nonce can be? when the miner decides to change something in the block?

EDIT: I believe it can be 2147483647 at most. which is the largest 32bit value. so when it hits this number, miner decides to change something else within the block





staff
Activity: 3458
Merit: 6793
Just writing some code
1-) I'm looking at block 540494, its nonce is 2097181953 its hash is 00000000000000000024ada2........

so it starts with 18 leading zeros..

that means a miner starts from 0 calculates its hash with the nonce=0 IF no valid result, THEN increments nonce by 1. now nonce=1, and keep doing this until a hash is found with 18 leading zeros. Because current difficulty requires them to find a hash with 18 leading zeros, right?
Yes, and no. Yes, a miner started from 0 and incremented the nonce until they found a hash that met the proof of work requirements. No, the difficulty does not require them to find a hash with 18 leading zeroes.

The leading zeroes thing is just a simplification of how the proof of work check actually works. What actually happens is that there is some target value that is a 256 bit integer. Block hashes are interpreted as a 256 bit integer. So all that happens is that the block hash (interpreted as a 256 bit integer) is compared to the target value. The block hash must then be less than or equal to the target value. Because the target value is "small" (small in terms of the 256 bit number space), it will have many leading zeroes. But the zeroes themselves do not actually matter, all that matters is the value.

Sub-question: I know difficulty changes every 2 weeks, it is now 7,019,199,231,177 but who decides how many leading zeros should be in the valid hash? is it something to do with "target difficulty" rather than the "difficulty number" itself? Are these 2 different difficulty parameters that define the difficulty?
There is only one parameter that matters: the target value. The difficulty is just the inverse of the target divided by the maximum target value.

The difficulty readjustment is really a target readjustment. The target changes every 2016 blocks, which is approximately two weeks. It does not actually change every two weeks since it may take shorter or longer than 2 weeks to find 2016 blocks. The target is just scaled proportionally to the amount of time it took to mine 2016 blocks and the amount of time it should take to mine 2016 blocks (2 weeks). Of course there must have been a starting point for the target, and that is the maximum target value (so difficulty is the lowest) which is defined in the consensus rules. The maximum target value was used in the genesis block so that is the starting point for target adjustment.

So for this particular example (block 540494) the nonce is 2097181953

that means a miner tried every single nonce from 0 to 2,097,181,953, and calculated hash 2,097,181,953 (lets say 2 billions) times in total and found the valid result which is the hash starting with 18 zeros. (00000000000000000024ada2........)

Now what I dont understand is that an ASIC machine can hash 12 trillion times PER SECOND. and our nonce is 2,097,181,953. so that means winning miner spent only fraction of a second to calculate 2 billions hashes. But it cant be true because block time is 10 minutes, right? so what is wrong here?? What am I doing wrong with this calculation?

My understanding is like the following;


* Miner have a list of transactions and all the properties (headers I believe) to calculate the hash of the block.

* Miner calculates hashes for all the nonces between 0 and the current difficulty (it is now 7,019,199,231,177)

* IF miner finds a hash with at least 18 leading zeros (can be more than 18 zeros, right? since even if it has 20 leading zeros, it is still proof of the work.) that miner gets the reward.

* IF miner cant find a nonce between 0 - 7,019,199,231,177 then miner picks different set of transactions (since calculations with the current list of transactions wont be resulting valid hash.. So miner adds couple more transactions into the block and starts the whole process all over again with the new list of transactions.. hoping to find a valid hash with the nonce between 0 and 7,019,199,231,177 and with the new set of transactions.
That is close. Miners don't usually re-select transactions after every set of nonces is exhausted. Usually they will change something within the block they already have. Specifically, they will set some value in their coinbase transaction that is known as the extra nonce. This extra nonce is then incremented every time the nonce runs out so that the merkle root is different and thus the nonces can be tried again. Furthermore, miners may change things like the block timestamp and block version number in order to get different candidate blocks.

* Miner have a list of transactions and all the properties (headers I believe) to calculate the hash of the block.
Transactions are just transactions. There are no transaction headers. The block contains the transactions themselves and then the hash of all of the transactions is included in the merkle root which is in the block header.

* Miner calculates hashes for all the nonces between 0 and the current difficulty (it is now 7,019,199,231,177)
No, the nonce is a 32 bit integer. It can only have the 32 bit integer values. It does not change with the difficulty.

* IF miner finds a hash with at least 18 leading zeros (can be more than 18 zeros, right? since even if it has 20 leading zeros, it is still proof of the work.) that miner gets the reward.
A block hash is valid so long as it is less than or equal to the current target. So one with more leading zeroes is inherently less than, thus it will still be accepted.
newbie
Activity: 8
Merit: 6
Hello everyone, this is my first topic here. I feel excited little bit lol. And sorry for my English, it is not my first language.

Ok so I've been interested in cryptoCurrencies since the August 2017, but lately I decided to learn more about the technology itself; blockchain. I know most of the parts about how it works. but some little details that I dont understand bothering me.

I have researched everywhere. I love researching and learning. But no success for this particular topic.


My questions is about mining (technical side). I understand how it works basically, I'm a home miner myself but couple things really annoying me.


1-) I'm looking at block 540494, its nonce is 2097181953 its hash is 00000000000000000024ada2........

so it starts with 18 leading zeros..

that means a miner starts from 0 calculates its hash with the nonce=0 IF no valid result, THEN increments nonce by 1. now nonce=1, and keep doing this until a hash is found with 18 leading zeros. Because current difficulty requires them to find a hash with 18 leading zeros, right?

Sub-question: I know difficulty changes every 2 weeks, it is now 7,019,199,231,177 but who decides how many leading zeros should be in the valid hash? is it something to do with "target difficulty" rather than the "difficulty number" itself? Are these 2 different difficulty parameters that define the difficulty?


So for this particular example (block 540494) the nonce is 2097181953

that means a miner tried every single nonce from 0 to 2,097,181,953, and calculated hash 2,097,181,953 (lets say 2 billions) times in total and found the valid result which is the hash starting with 18 zeros. (00000000000000000024ada2........)

Now what I dont understand is that an ASIC machine can hash 12 trillion times PER SECOND. and our nonce is 2,097,181,953. so that means winning miner spent only fraction of a second to calculate 2 billions hashes. But it cant be true because block time is 10 minutes, right? so what is wrong here?? What am I doing wrong with this calculation?

My understanding is like the following;


* Miner have a list of transactions and all the properties (headers I believe) to calculate the hash of the block.

* Miner calculates hashes for all the nonces between 0 and the current difficulty (it is now 7,019,199,231,177)

* IF miner finds a hash with at least 18 leading zeros (can be more than 18 zeros, right? since even if it has 20 leading zeros, it is still proof of the work.) that miner gets the reward.

* IF miner cant find a nonce between 0 - 7,019,199,231,177 then miner picks different set of transactions (since calculations with the current list of transactions wont be resulting valid hash.. So miner adds couple more transactions into the block and starts the whole process all over again with the new list of transactions.. hoping to find a valid hash with the nonce between 0 and 7,019,199,231,177 and with the new set of transactions.

So did I get it all wrong? and confused myself for nothing? 😄 or this is actually what is happening? I know there are couple of gurus here and can confirm if I'm right or wrong..


Thanks
Jump to: