Blockchain concept that you must Know the fourth issue (3)

Source: Internet
Author: User
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 (3)

Less card cool

wechat:13260325501

After all the groundwork, this issue we will make a simple demonstration of the difficulty code implementation. (Below is the case where the recent improvements in block storage information are not considered.) For the adjustment of block information, there are methods such as "Quarantine verification". It is not relevant for the time being to understand. We will introduce them to you as we learn more in depth. )

In this issue, we will give an example of some of the information in Figure II of the previous issue, and then the code simulates the difficulty of implementation.

How is the hash generated in the chunk?

    • As mentioned in the fourth issue (1), each chunk contains information such as Index,current hash,previous hash,timestamp,data,nounce. The current hash is the result of stitching the rest of the information into a string and then hashing the string. Just as the current hash of the previous chunk is also generated by this information hash of the chunks before it.
    • Among them, nounce as a computer exhaustive random number, constantly changing, forming a string change, resulting in a different hash value. This hash continues to match the current hash difficulty. The hash collision succeeds when the Nounce value is taken to a certain number and the hash value that it forms matches the current valid hash difficulty.
    • The following is code implementation

Hash sha256

    • Give the code First
/*代码逻辑:    第一步:导入crypto/sha256库    第二步:声明字符串    第三步:sha256.New()创建一个对象    第四步:将字符串转换为字节数组    第五步:使用对象调用write方法    第六步:first.Sum(nil) 返回hash值*/package mainimport (    //第1步    "crypto/sha256"    "fmt"    "bytes")func main() {    //第2步    const input1 = "199778A - > B 100"    //第3步    first := sha256.New()    //第4步    //第5步    first.Write([]byte(input1))    //第6步    fmt.Printf("%x\n", first.Sum(nil))    const input2 = "Hello"    second := sha256.New()    second.Write([]byte(input2))    fmt.Printf("%x\n", second.Sum(nil))    //输出两个哈希是否相等,bytes.Equal()    fmt.Println(bytes.Equal(first.Sum(nil), second.Sum(nil)))}
    • ==sha256== Output Results:
0000e1343c8d9ec8a8996f0c1c8d1f9f1d954a750c3db5525205f401c516222d185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969falseProcess finished with exit code 0
    • Some of these functions are very eager to understand, but the encapsulation of these functions is too difficult, we do not delve into this. The main thing is to remember four key steps 3,4,5,6, and skilled use. These include the following points:
    1. ==sha256. New () = = has a return value and needs to declare the variable and receive it. And its return value is a hash object, and it is a pointer object. It can call some methods, which have the Write method. --3rd Step
    2. The hash object calls ==obj. The Write () = = method is required to pass parameters, and the parameter type is a byte array. So it involves converting the hash string first to ==[]byte== type--4th step 5th step
    3. The hash object calls ==first. The Sum (nil) = = method returns a hash value with parameters and a fixed parameter of ==nil==, which can declare the variable to be received or output directly. --6th Step

Judgment on the difficulty of hashing

    • To write the complete code of mining difficulty, one of the links must be solved. That is how to determine whether the hash difficulty is effective? Let's first give the code
/*代码逻辑:(我们将代码)    第一步:声明一个随机取的哈希值,来在之后测验isValidDifficulty是否有效    第二步:设置当前哈希难度,difficulty    第三步:写入一个循环来迭代匹配哈希值对应位置的值,查看是否这个位置是否是哈希难度要求的值    第四步:打印对这个哈希值的判断*/package mainimport "fmt"func main() {    h := "0000e1343c8d9ec8a8996f0c1c8d1f9f1d954a750c3db5525205f401c516222d"    //h1 := "0033e1343c8d9ec8a8996f0c1c8d1f9f1d954a750c3db5525205f401c516222d"    //h2 := "0003e1343c8d9ec8a8996f0c1c8d1f9f1d954a750c3db5525205f401c516222d"    difficulty := 4    var i int    for i = 0; i < len(h); i++ {        if h[i] != '0' {            break        }    }    fmt.Print( i >= difficulty)}
    • H Run Result
trueProcess finished with exit code 0
    • H1 running Results
falseProcess finished with exit code 0
    • H2 running Results
falseProcess finished with exit code 0
    • So we encapsulate the code to make it look clearer and more convenient to call.
package mainimport "fmt"func main() {    h := "0000e1343c8d9ec8a8996f0c1c8d1f9f1d954a750c3db5525205f401c516222d"    fmt.Println(isValidHashDifficulty(h, 4))}func isValidHashDifficulty(h string, difficulty int) bool {    b := len(h) // 64    var i int    for i = 0; i < b; i++ {        if h[i] != '0' {            break        }    }    return i >= difficulty}
    • The next article gives the complete code
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.