簡化模擬:java
0.假設挖礦難度爲整數d(d>0)
1.隨機生成一個整數x
2.對整數x進行摘要計算hash(x),獲得s,即s=hash(x)
3.對s前導字符串斷定:如爲d個'0',則礦已挖到,結束;不然跳到步驟1.算法
一句話:不斷隨機生成整數,直到對生成的整數被進行摘要計算獲得的字符串以某個數目的0開頭爲止。app
摘要算法選用sha256dom
代碼實現:工具
1 package cn.ubingo.block_chain; 2 3 import java.util.Collections; 4 import java.util.Date; 5 import java.util.Random; 6 7 public class Demo { 8 static Random r = new Random(); 9 10 public static void main(String[] args) { 11 12 int dificulty = 3; 13 String zeros = String.join("", Collections.nCopies(dificulty, "0")); 14 System.out.println(zeros); 15 16 Date start = new Date(); 17 long times = 0; 18 while (true) { 19 String sha = Sha.getSHA256StrJava(r.nextInt() + ""); 20 // System.out.println(sha); 21 times++; 22 if (sha.startsWith(zeros)) 23 break; 24 } 25 Date end = new Date(); 26 long use = end.getTime() - start.getTime(); 27 28 System.out.println(times + " in " + use + "ms with dificulty " + dificulty); 29 } 30 31 }
輸出結果:spa
000code
4973 in 106ms with dificulty 3blog
耗時計算:字符串
讓難度從1遞增,每一個難度挖10次礦,取耗時最久的那次做爲參考。get
package cn.ubingo.block_chain; import java.util.Collections; import java.util.Date; import java.util.Random; public class App { static Random r = new Random(); public static void main(String[] args) { int dificulty = 0; while (true) { dificulty++; String zeros = String.join("", Collections.nCopies(dificulty, "0")); System.out.println(zeros); long maxUse = 0; long maxTimes = 0; for (int i = 0; i < 10; i++) { Date start = new Date(); long times = 0; while (true) { String sha = Sha.getSHA256StrJava(r.nextInt() + ""); //System.out.println(sha); times++; if (sha.startsWith(zeros)) break; } Date end = new Date(); long use = end.getTime() - start.getTime(); if (use > maxUse) { maxUse = use; maxTimes = times; } } System.out.println(maxTimes + " in " + maxUse + "ms with dificulty " + dificulty); } } }
輸出結果(代碼還在跑):
0
19 in 32ms with dificulty 1
00
366 in 11ms with dificulty 2
000
11377 in 73ms with dificulty 3
0000
153888 in 377ms with dificulty 4
00000
3160837 in 4568ms with dificulty 5
000000
55559940 in 81454ms with dificulty 6
0000000
637485941 in 977748ms with dificulty 7
00000000
8540898666 in 12283075ms with dificulty 8
000000000
===========================================================
從上面結果能夠看出,當難度爲8時,一我的挖,耗時204分鐘(12283075ms)左右。
因爲挖礦算法具備的隨機性,如要求在10分鐘內挖到礦,須要204/10=20個同等算力的礦工同時挖。
若是難度爲72(比特幣當前的難度),可想而知須要的礦工數量得是什麼級別。
因爲挖礦算法具備的隨機性,誰的算力強,誰的礦工多,誰挖到礦的概率就大,誰就能發大財。(因此礦機和礦場登場了)
===========================================================
代碼中用到的工具類以下(網上找的):
package cn.ubingo.block_chain; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Sha { /** * 利用java原生的摘要實現SHA256摘要計算 * @param content 原文 * @return */ public static String getSHA256StrJava(String content){ MessageDigest messageDigest; String encodeStr = ""; try { messageDigest = MessageDigest.getInstance("SHA-256"); messageDigest.update(content.getBytes("UTF-8")); encodeStr = byte2Hex(messageDigest.digest()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return encodeStr; } /** * 將byte轉爲16進制 * @param bytes * @return */ private static String byte2Hex(byte[] bytes){ StringBuffer stringBuffer = new StringBuffer(); String temp = null; for (int i=0;i<bytes.length;i++){ temp = Integer.toHexString(bytes[i] & 0xFF); if (temp.length()==1){ //1獲得一位的進行補0操做 stringBuffer.append("0"); } stringBuffer.append(temp); } return stringBuffer.toString(); } }