This is a creation in Article, where the information may have evolved or changed.
Fourth issue introduction to the code implementation of the difficulty of mining in Go language (4)
Less card cool
wechat:13260325501
The previous is all over, because the code too many lead to the article longer. So added a piece. At the end of this, we will finally show the mining process in front of everyone. It is recommended that you also run the code in Goland, which will make you feel more comfortable.
Code implementation of mining difficulty
- Speak not much, directly to the code implementation.
/* Code logic: First Step: Guide Library The second step: declare and initialize the values, a total of 5 data: Blockid (Block ID), Prehash,timestamp (chunk birth time), data, Nounce (Do not understand why the need for these data can be found in the fourth issue (2) to see) The third step: declare and create a hash object, shaobj Fourth step: Iterate Call function isvalidhashdifficulty to determine whether the hash value is valid or not. The iteration changes the Nounce value by 1, and then each time it mates with other information to generate the current hash. */package mainimport ("FMT"//1th Step "crypto/sha256") func main () {//2nd step var (blockid = "100") PreHash = "0000fedaa499741317a18f1ad626f933776ad24822cb422634978bfe8005c94b" TimeStamp = "Thu, APR 2018 03 : 30:54 GMT "data =" A-B "nonce = 0//Initialize nounce to 0 (note here: Nounce is of type int, but the number of operands on both sides of the arithmetic operator at hash time According to the type must be consistent, so to convert the type when string concatenation) hashstring = ""//The current hash is initialized to an empty string)//3rd step shaobj: = sha256. New ()//4th step for!isvalidhashdifficulty (hashstring, 4) {nonce++ Input: = blockid + PreHash + timeStamp + Data + string (nonce) Shaobj.write ([]byte (input)) hashstring = Fmt. Sprintf ("%x", Shaobj.sum (nil)); Fmt. PRINTLN (hashstring)//Here print each time used to match the hash value just to show the mining process image, and noAdditional meaning, write not write can}//when generating a valid hash, mining success, iteration stop. In reality, mining without stopping, continue to dig a node can be. }func isvalidhashdifficulty (h string, difficulty int) bool {b: = Len (h)//+ var i int for i = 0; i < b; i++ {if h[i]! = ' 0 ' {break}} return I >= difficulty}
- There is no better way to run the results. Here we only intercept part of it. We recommend that you take the code down and run.
- If you have read the previous chapter, believe that the code of the principle you already know the chest. Here are just a few things to note.
- The 5 data we give is a simple example and does not fully enumerate all the data involved in generating a chunk hash.
- For the sake of simplicity, the Blockid is set to string type, and does not mean that the chunk ID must be a string type.
- The hash difficulty here is what we think set, not the actual hash difficulty of the generation method. That's true. The difficulty of hashing is achieved by a very complicated mathematical model, which is automatically adjusted according to the current full net calculation force.
- Ok. The truth of the mining is already in the white. Do not know that everyone has the feeling of the enlightened? Next we'll meet together to explore more blockchain content!