anders94 / blockchain-demo

A web-based demonstration of blockchain concepts.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] How does one calculate the max nonces limit?

AveragePythonEnjoyer29 opened this issue · comments

commented

I am trying to make an Proof-of-work script myself, and i want the nonce limit to change dynamically with the difficulty.
The example table shows that the difficulty 4 has a max of 500,000. How would i calculate the max, for a different difficulty?

Thanks in advance

When you say "I want the nonce limit to change" I assume you mean "I want the difficulty limit to change". The nonce is not connected to the difficulty, its only there as something you can arbitrarily change to make the hash digest differ and (hopefully) have a low enough difficulty to be considered a solution to the block.

However, you do note the number 500,000. I put that in there so the app wouldn't run in an endless loop forever. I wanted to stop it at some point on the off chance there was something wrong with the demo. (like you set the number of zeros to 8 or more making the puzzle essentially unsolvable by a web browser)

commented

yes, that's what i was going for. The limit of the nonces to be generated to prevent an infinite loop. I want this to scale with the difficulty. Any idea on how to do that?

That's going to be an issue with the way the code works right now. Difficulty should scale as more and more mining peers enter the network but all peers are simulated in a single web browser. What would be needed is a coordinating website that could relay messages between an arbitrarily large group of web browsers that connect, each running their own version of the chain. The code doesn't work like that and making it do so would be a fairly heavy lift.

I'm always open to PRs though if someone wants to take a crack at it.

commented

yeah, but how would you calculate the limit of nonces? I have scaling implemented in my test network, but what i need to figure out is how i would change the limit of nonces. For example the max limit for the difficulty 4 is 500,00. How would i calculate the limit for 9, or 12? That's the issue i am currently having

commented

i know how to change it, i just don't know how to calculate it.

For each additional zero digit, multiply the max (500,000) by 16.

const difficulty = 6;

let maximumNonce = 500000;
for (let x=4; x<difficulty; x++)
    maximumNonce = maximumNonce * 16;
commented

thank you, that was all i needed to know. Thanks for taking the time to respond!