Home  >  Article  >  Backend Development  >  Why use Go language to write blockchain

Why use Go language to write blockchain

青灯夜游
青灯夜游Original
2021-03-04 15:42:537222browse

Reason: 1. Go language has the advantages of simple deployment, excellent performance, good parallel execution performance, good language design, a large number of built-in libraries, and an awesome team. 2. Both Ethereum and Hyperledger choose to use Go as the development language; these two super blockchains have great influence. They not only occupy a large niche in the ecology, but also implicitly formulate the blockchain standards.

Why use Go language to write blockchain

The operating environment of this tutorial: windows10 system, GO 1.18, thinkpad t480 computer.

In the public blockchain development circle, we have found some popular programming languages, including C, Golang, Python and the recently launched Rust, etc.

Let’s make some statistics on the programming languages ​​used by the more famous projects, as shown below:

Why use Go language to write blockchain

The older generation of public chains, such as Bitcoin and Litcoin, are generally C/C was used more (let’s look at that time, Go had not yet emerged). The new generation of public chains such as Ethereum and the leader of the alliance chain, Hyperledger, began to use the Go language more. Of course, we saw the development of Rust. The momentum is also very strong. In the past two years, many public chains such as Polkadot and Grin have begun to use the Rust language for development.

Advantages of Go language

Easy deployment

Go compile What is generated is a static executable file with no other external dependencies except glibc. This makes deployment extremely convenient: only a basic system and necessary management and monitoring tools are needed on the target machine, and there is no need to worry about the dependencies of various packages and libraries required by the application, greatly reducing the burden of maintenance. It can be directly compiled into machine code and does not rely on other libraries. The version of glibc has certain requirements. Deployment is completed by throwing a file up.

Excellent performance

Although it is not as good as C and Java, it is usually an order of magnitude higher than native Python applications, and is suitable for writing some bottleneck businesses. The memory usage is also very economical.

Concurrency & Channel

Goroutine and channel make it very easy to write high-concurrency server software, and in many cases it is completely unnecessary Consider the locking mechanism and the various problems it brings. A single Go application can also effectively utilize multiple CPU cores and achieve good parallel execution performance.

Good language design

Go is very simple and easy to learn. From an academic perspective, the Go language is actually very mediocre and does not support many advanced language features; but from an engineering perspective, Go's design is very good: the specifications are simple and flexible enough. Because of Go's simplicity, any Python, Elixir, C, Scala or Java developer can form an efficient Go team within a month.

Standard Library & Tools

Go currently has a large number of built-in libraries, especially the network library which is very powerful. More importantly, Go comes with a complete tool chain, which greatly improves the consistency of team collaboration. For example, gofmt automatically formats Go code, which largely eliminates the problem of inconsistent formatting styles of codes written by different people. Configure the editor to automatically run gofmt when editing the archive, so that you can place it anywhere when writing code, and it will automatically become correctly formatted code when archiving. In addition, there are very useful tools such as gofix and govet.

The team is awesome

The supporter behind Go language is Google. The language is enough to be tested in various scenarios. At the same time, the founder is still The father of the C language, we can look forward to future development and innovation.

Go successful projects

Go language has been widely used in the cloud era, especially killer products like Docker and K8s The emergence of the Go language has given it a place in the engineering world. In addition to the Go language, there are many successfully running software:

nsq: bitly's open source message queue system has very high performance. Currently, they process it every day Billions of messages

packer: used to generate image files for different platforms, such as VM, vbox, AWS, etc. The author is the author of vagrant

skynet: distributed scheduling framework Doozer: Distributed synchronization tool, similar to ZooKeeper

Heka: mazila open source log processing system

cbfs: couchbase open source distributed file system

tsuru: open source PAAS platform, and The functions implemented by SAE are exactly the same

groupcache: a caching system written by the author of memcahe for the Google download system

god: a caching system similar to redis, but supports distribution and scalability

gor: Network traffic packet capture and replay tool

Ecological card slots and implicit standards

In addition to the need for hard work, there are also opportunities and luck that made the blockchain choose the Go language. Let’s take a look at the most successful public and consortium chain representatives since blockchain 2.0, Ethereum and Hyperledger Fabric. Without exception, they all choose to use Go as the development language (although Ethereum actually has client versions in other languages, but entering After the Homestead stage, the Go client occupies a dominant position). The influence of these two super blockchains is not comparable to that of ordinary projects. Not only do they occupy a large niche in the ecology, but they are also implicitly formulated. Without the standards of the blockchain, whether it is smart contracts in the public chain or alliance chain technology, Ethereum and Fabric cannot be bypassed. So for a company that wants to choose blockchain technology, the fastest way What is the implementation of ?

Naturally, we directly copy the innovations of these two projects. Another shortcut is to directly modify the open source code. Then naturally Go language becomes the first choice for latecomers. It is not easy to re-implement it in another language, and If you choose some innovative but not very mature languages, you will also lack the support of some specific libraries, which will make the project unable to be carried out.

Many people have no doubt about the influence of Ethereum, but in fact Fabric’s influence on enterprise blockchain deployment cannot be underestimated:

Why use Go language to write blockchain

Chart source "2019 Global Enterprise Blockchain Benchmark Research Report"

Hyperledger Fabric is the most used protocol framework in deployed enterprise blockchain networks, and Hyperledger Hyperledger (of which Fabric is the flagship protocol) is The protocol framework most commonly supported by integrators and software development platforms reaches 53%. Among all the blockchain technology books, the fact that books on Hyperledger are the most popular also confirms the influence of Hyperledger.

Practice of Bytom in Go

In the process of selecting programming languages, we considered C, C, Java , but large C/C projects are difficult to maintain, and Java is a bit cumbersome. At this time, the Go language has already shined in blockchain projects, and has gradually formed a head-end effect on technology and talents, so follow the trend and carry out technology The selection will naturally reduce the resistance encountered by the initial Bytom project. Of course, during the gradual development process, we also felt the convenience and advantages brought by choosing the Go language.

A Case of Go on the blockchain

Technically speaking, blockchain nodes require multiple modules to work together asynchronously, so Go language concurrency And channels are very advantageous. Let's look at the following example of transaction verification:

func ValidateTxs(txs []*bc.Tx, block *bc.Block) []*ValidateTxResult {
    txSize := len(txs)
    //init the goroutine validate worker
    var wg sync.WaitGroup
    workCh := make(chan *validateTxWork, txSize)
    resultCh := make(chan *ValidateTxResult, txSize)
    closeCh := make(chan struct{})
    for i := 0; i <= validateWorkerNum && i < txSize; i++ {
        wg.Add(1)
        go validateTxWorker(workCh, resultCh, closeCh, &wg)
    }

    //sent the works
    for i, tx := range txs {
        workCh <- &validateTxWork{i: i, tx: tx, block: block}
    }

    //collect validate results
    results := make([]*ValidateTxResult, txSize)
    for i := 0; i < txSize; i++ {
        result := <-resultCh
        results[result.i] = result
    }

    close(closeCh)
    wg.Wait()
    close(workCh)
    close(resultCh)
    return results
}

We use Routine Ch WaitGroup to build a concurrent transaction verification function within 30 lines of code. In a high-configuration In the case of a server, it can run more than 100,000 TPS.

Easily become a Go language master

In terms of talents, some members of the Bytom core development team have never done Go language development before, but they are all able to You get started quickly, and you can basically participate in the development and maintenance of the core code within half a month (especially easy for developers with experience in C/C/Java). This is the benefit of simple language to team building.

Unified collaboration

In terms of collaboration, gofmt automatically typesets Go code, allowing core team members and even community developers to submit The differences in coding styles are minimized, improving the overall quality and maintainability of the project.

Summary

The characteristics and advantages of the Go language itself pave the way for it, and the two super blocks of Ethereum and Hyperledger The blessing of chain projects has also made Go language the first choice for many blockchain projects. Bytom chose Go language to fully realize its advantages in developing the underlying blockchain, but there is no need to fall into the trap of language disputes and pay attention to pragmatism. This is the proper meaning of engineering. Bytom's core project is completed in Go language, but many surrounding sub-projects are also implemented in Java, Python or JavaScript. After all, ecological diversity is the foundation for the longevity of a project.

Recommended learning: Golang tutorial

The above is the detailed content of Why use Go language to write blockchain. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn