anders94 / blockchain-demo

A web-based demonstration of blockchain concepts.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Difficulty Target

Arne-Pfeilsticker opened this issue · comments

Hi Anders,
are the 4 zeros at the beginning of the hash the so-called difficultiy target, which must be found by the mining?

Thanks for the great work.
I wait eagerly for the continuation of your demo.

Best regards,
Arne

Yes, that's exactly right. The 4 zeros are my "difficulty target".

I picked 4 zeros so it would be reasonably difficult to find by manually updating the nonce but within a few seconds for a web browser. In a real blockchain (such as bitcoin) the difficulty target is far higher (something more like 14 or 15 zeros) and is dynamically updated every 2016 blocks to target 6 blocks per hour. (or one block every 10 minutes on average)

I also use "starting with 4 zeros" as a short-hand for what is more accurately "lower than a target number". As you know, the hash digest is actually a large number expressed in HEX format. We're really looking for a number under some threshold - not just "has a zero in the first 4 spots". Four zeros is just an easy way to spot it. Tiny point but I think it's worth mentioning.

Hi Anders.

How can i change the diffculty target to make it a 10 zero "diffculty target"

How do i upload the .js file in the website. Apologies if it is a basic question.

This presupposes you downloaded the code and are running it on your own computer. If not, because the project will work from just flat files, you could download the entire thing from my website, make the necessary changes and then just open the local files in your web browser.

Downloaded the website on my PC using HTTracks. Replaced the blockchain.js file to above mentioned file
https://github.com/anders94/blockchain-demo/blob/master/public/javascripts/blockchain.js#L1

Changed the diffculty to 10 instead of 4 and started to mine the block. (Used my name in the data). However after a couple of minutes the system stops and puts the Nonce at 500000 and the Hash does not have zeros.

How do i set it up to get 10 zeros

Adjust maximumNonce in the second line of that file as well.

Thanks. Following is the process as recommended by yourself. Highlighting it for the benefit of others.

Downloaded HTtracks. https://www.httrack.com/page/2/
Used HTtracks to download the website https://anders.com/blockchain/blockchain.html
Once downloaded, in the My Websites folder (Created by HTtracks) changed the file anders.com/blockchain/javascripts/blockchain.js by deleting all text and replacing it with
https://github.com/anders94/blockchain-demo/blob/master/public/javascripts/blockchain.js#L1.
Change the diffculty to desired level on line 1 and Nonce on line 2. (Anders has highlighted that for diffcutly greater than 6 needs more than 500,000,000 max nonces. Save the file.
Open file titled Index in the downloaded folder.
Change the data and mine the blockchain.

Using the above mentioned method i was able to mine upto 6 diffculty level.
6 diffculty level takes about 10-15 minutes on an I5 laptop.

@anders94 : Any idea how much time it would take to solve 7, 8,9 and 10 diffculty levels and what should be the recommended Nonce for each.

Hi Anders,
Next to Xanderking's comment above, could you give any idea how long it will take to solve 9 and 10 difficulty levels? I've followed the way introduced and tried both 9 and 10 with 500,000,000,000,000 max nonces but after 80 minutes still I can't mine. Could you offer me a good tip about this :) ?

For every step you need to 16x the max nonces so:

digits nonce time estimate
4 500,000 15 minutes
5 8,000,000 4 hours
6 128,000,000 3 days
7 2,048,000,000 a month
8 32,768,000,000 2 years
9 524,288,000,000 30 years
10 8,388,608,000,000 481 years
11 134,217,728,000,000 7,690 years
12 2,147,483,648,000,000 123,036 years
13 34,359,738,368,000,000 1,968,581 years
14 549,755,813,888,000,000 31,497,291 years
15 8,796,093,022,208,000,000 503,956,662 years

As I mention in the README:

In the production bitcoin blockchain, block 458091 has the hash digest
00000000000000000000011246f099d94f91628d71c9d75ad2f9a06e2beb7e92. That's 21 zeros in a row!

If we were to use my code (which is very inefficient) to mine out to 21 zeros, you would be looking at 8,454,989,768,407,765 years of computation!

Dear @anders94, do you know where to find a more efficient code to mine at higher difficulty levels?
Thanks for your great work!

The least efficient SHA256 code I know of is this interpreted JavaScript version. Faster than that would be natively compiled C such as OpenSSL's SHA256 library here: https://github.com/openssl/openssl/blob/master/crypto/sha/sha256.c Better than that would be a codebase run on a graphics card (GPU) such as: https://github.com/Horkyze/CudaSHA256 Better than that would be a codebase run on an FPGA (field programmable gate array) such as: https://github.com/secworks/sha256 Best would be an ASIC (application specific integrated circuit) which isn't "programmable" because they are purpose built to do one thing - calculate SHA256 hashes.

This roughly tracks the bitcoin mining history. Back in the day, mining was done only on CPUs. Eventually someone ported the code to GPUs and you had almost an order of magnitude increase in performance. Then they ported to FPGAs gaining about another 6x as people waited for ASICs to be built. That didn't take long and again we had another order of magnitude increase in performance. That's pretty much where the industry is today. Without ASICs, you can't profitably mine bitcoin.

Dear Anders, perfect! Thanks for the great overview!