Ethereum source Analysis (1) Design thinking and modular organization of Go-ethereum

Source: Internet
Author: User

Brother even blockchain Dean Yin Cheng senior Blockchain technical expert: graduated from Tsinghua University, has served as Google algorithm engineer, Microsoft Blockchain field of the world's most valuable experts, Microsoft Tech.ed Conference gold medal lecturer. Proficient in C + +, Python, go language, Sicikit-learn and TensorFlow. has 15 of programming experience and 5 years of teaching experience, senior Software Architect, Intel Software technology experts, well-known technical experts, with many years of the world's top IT company Microsoft Google's work experience. With many years of experience in software programming and lecturer teaching experience, and in human-computer interaction, education, information security, advertising, blockchain system development of many products. With deep project management experience and research and development experience, with two artificial intelligence invention patents, and the development of electronic money deployment to Microsoft Windowsazure experience. Teaching to explain in layman's language, so that students can do.

Design thinking and modular organization of Go-ethereum

===================================

Ethereum's goal is to build a centralized platform for running smart contracts based on blockchain technology.

# # One. Blockchain technology

Blockchain belongs to a kind of centralized digital bookkeeping technology, and blockchain data is maintained by nodes that are not trusted by each other, and each node replicates a complete record.

# Two. Ethereum Core concept

* EVM: Ethereum virtual machine, lightweight virtual machine environment, is the operation Environment of Ethereum Intelligent contract.

* Account: There are two types of accounts, contract accounts and external accounts. The contract account mainly stores the executed contract code, and the external account stores the etheric currency, corresponding to the specific common key.

* Transaction: Transactions on the Ethereum network, from one account to another, including the etheric currency or this smart contract parameter.

* Gas: The fuel that the Ethereum network runs, consumes a certain amount of fuel for every smart contract executed.

* Mine: Mining, Ethereum network through the workload proof algorithm to ensure the safe operation of the network.

* Peer network: All the nodes in the Ethereum distributed network have equal status and no central server.

# three. Ethereum model

The Ethereum essence is a trading-based state machine (transaction-based), with millions of transactions in the ether State,

These transactions are packaged in a block and each chunk is linked to the previous block, forming a reverse linked list, so called a blockchain. On the basis of the blockchain, a smart contract was added to create the Ethereum.

In order for a transaction to be considered valid, it must undergo a verification process, that is, mining (Mine). Any miner on the Ethereum network can try to create and validate chunks,

If a chunk is considered valid and is the fastest to complete validation, it is added to the main chain, which is the longest chain on the Ethereum network. If more than one miner packs a chunk at the same time, and it takes a while for the chunks to spread over the network,

Inevitably, there will be multiple paths, the so-called bifurcation.

In order to prevent the generation of multiple chains, Ethereum uses the **ghost protocol * * (greedy heaviest observed subtree), that is, to select a complete calculation of the path, the larger the block number, the longer the path, indicating the more the calculation of mining consumption.

Because success confirms that a new block will be rewarded with a certain amount of ethereum, choosing the main chain is optimal from the perspective of economics and game theory.

# Four. Architecture of Ethereum

The architecture design of Ethereum can be easily divided into three levels, protocol layer, interface layer and application layer. And the protocol layer can be divided into network layer and storage layer.

From the technical point of view, the protocol layer mainly includes peer-network communication, distributed algorithm, cryptographic signature and data storage technology. At the bottom of the data store, Bitcoin and Ethereum use the Google Open source leveldb database.

The interface layer is completely decoupled from the protocol layer, and in addition to interacting with the protocol layer at the time of transaction, the development of various blockchain-based application layer services is not constrained, including distributed storage services, machine learning, and Internet of things.

Application layer is mainly from the characteristics of the blockchain itself, without reference to third-party institutions, to provide a centralized, non-tamper-proof, safe and reliable scene application.

Mainly including financial services, credit and ownership management, resource sharing, investment management and the Internet of things and supply chain.

# Five. Ethereum Core Data structure

# # # 1. Block is one of the core data structures of Ethereum, and the block contains both the header and body parts.

# # # 2. Blockchain and Headerchain, Blockchain manages all blocks to form a one-way list. Headerchain manages all the headers and forms a one-way list, Headerchain is part of the blockchain.

# # # 3. Transaction is an important data structure for the body, and a transaction is a sequence of instructions, serialized, and then submitted to the blockchain by an externally owned account-generated cryptographic signature.

# # # 4. Ethereum Database System-merkle-patricia Trie (MPT), which is a series of nodes consisting of two of the tree, at the bottom of the tree contains a large number of leaf nodes of the source data, the parent node is two child nodes of the hash value, until the root node.

# # Six. Go-ethereum Source directory Structure

"' Golang

|---accounts ethereum account management

| The realization of the binary Merkle-patricia trie of---BMT

|---Build is primarily compiled and built with some scripts and configurations

|---cmd command-line tool

| |---Abigen contract interface generation tool

| |---bootnode node to implement network discovery

| |---EVM ethereum virtual machine

| |---faucet

| |---geth ethereum command-line Client

| | |---P2PSIM provides a tool to emulate the HTTP API

| |---puppeth a wizard to create a new Ethereum network

| | |---RLPDUMP provides formatted output of RLP data

| |---the access point of the Swarm swarm network

| |---util public components

| |---Wnode this is a simple whisper node. It can be used as a standalone boot node. In addition, it can be used for different testing and diagnostic purposes.

|---Common provides a number of common tool classes

|---consensus algorithm for consensus ether, including Ethhash, clique

|---Core ethereum data structures and algorithms (virtual machine, state, blockchain, Bron filter)

|---Crypto encryption, digital signature and hash algorithm

|---ETH implements the Ethereum protocol.

|---RPC Client for ethclient Ethereum

|---Database of ETHDB eth, mainly leveldb and corresponding interfaces

|---event handling in real time

|---Light enables on-demand retrieval for Ethereum lightweight clients

|---metrics provides disk counters

| Mining and consensus algorithm for---miner Ether Square

|---Some warpper used by mobile

|---node Ethereum's various types of nodes

|----PEER Ethernet-to-peer network protocol

|---RLP ethereum serialization and deserialization processing

|---RPC remote method call

|---swarm swarm network processing

|---trie an important data structure for the implementation of MPT

|---the protocol of the Whisper whisper node

```

# Seven. Information

[Ethereum Official documents] (http://ethdocs.org/en/latest/index.html)

[Ethereum Design principle] (Https://github.com/ethereum/wiki/wiki/Design-Rationale)

[Ethereum White Paper] (Https://github.com/ethereum/wiki/wiki/White-Paper)

[Ethereum Yellow Book] (https://ethereum.github.io/yellowpaper/paper.pdf)

# Go-ethereum Source Parsing

Since go Ethereum is the most widely used Ethereum client, subsequent source analysis is analyzed from this code on GitHub. Then I'm using a Windows 10 64-bit environment.

# # # Build Go ethereum Debug environment

First download go install package to install, because go website is wall, so download from the address below.

Https://studygolang.com/dl/golang/go1.9.1.windows-amd64.msi

After installation, set the environment variables, add the C:\Go\bin directory to your PATH environment variable, and then increase a gopath environment variable, gopath the value set to your Go language download code path (I set the C:\GOPATH)

To install Git tools, refer to the tutorials on the web to install Git tools, the Go language automatically download code from github requires the support of GIT tools

Open the command line tool to download the Go-ethereum code


Go get Github.com/ethereum/go-ethereum

After the command executes successfully, the code is downloaded to the following directory,%gopath%\src\github.com\ethereum\go-ethereum

If the process occurs during execution

# GITHUB.COM/ETHEREUM/GO-ETHEREUM/CRYPTO/SECP256K1

EXEC: "gcc": Executable file not found in%PATH%

You need to install the GCC tool and we download and install it from the address below

Http://tdm-gcc.tdragon.net/download

Next, install the IDE tools. I am using the IDE is JetBrains Gogland. Can be downloaded at the following address

Https://download.jetbrains.com/go/gogland-173.2696.28.exe

When the installation is complete, open the IDE. Select File---open, select the Gopath\src\github.com\ethereum\go-ethereum directory.

Then open Go-ethereum/rlp/decode_test.go. Right-click on the edit box to run, if successful, on behalf of the environment to build the completion.

# # # Go Ethereum directory probably introduction

The organizational structure of the Go-ethereum project is basically a directory of functional modules, the following is a brief description of the structure of each directory, each directory in the Go language is also a package, I understand that the package in Java should be similar to the meaning.

Accounts achieves a high level of Ethereum account management

The implementation of the Merkel tree of BMT binary

Build is primarily compiled and built with some scripts and configurations

CMD command-line tool, and a lot of command-line tools, one of the following describes

/abigen Source code generator to convert Ethereum contract definitions into easy-to-use, Compile-time type-safe Go PAC Kages

/bootnode Start a node that only implements network discovery

/EVM Ethereum Virtual Machine development tool to provide a configurable, isolated code debugging environment

/faucet

/geth ethereum command-line client, one of the most important tools

/p2psim provides a tool to emulate the HTTP API

/puppeth A wizard to create a new Ethereum network

/rlpdump provides a formatted output of the RLP data

/swarm access point for swarm network

/util provides a number of common tools

/wnode This is a simple whisper node. It can be used as a standalone boot node. In addition, it can be used for different testing and diagnostic purposes.

Common provides a number of common tool classes

Compression package RLE implements the Run-length encoding used for Ethereum data.

Consensus provides some consensus algorithms for Ethereum, such as Ethhash, clique (proof-of-authority)

Console Console Class

Contracts

Core Ethereum kernel data structures and algorithms (virtual machine, state, blockchain, Bron filter)

Crypto encryption and hash algorithms,

ETH has implemented the Ethereum protocol.

Ethclient provides an ethereum RPC client

ETHDB ETH database (including the actual leveldb used and the memory database used for testing)

Ethstats provides a report of the status of the network

Event handling of real-time events

Les implements a lightweight protocol subset of Ethereum

Light implementation provides on-demand retrieval for Ethereum lightweight clients

Log provides human-computer friendly logging information

Metrics provides disk counters

Miner provides ethereum block creation and mining

Some of the warpper used by mobile

Node Ethereum's various types of nodes

Ethernet-to-peer network protocol

RLP ethereum Serialization Processing

RPC Remote method call

Swarm Swarm Network Processing

Tests test

Trie ether Important data structure package Trie implements Merkle Patricia Tries.

Whisper provides the protocol for the Whisper node.

You can see the number of Ethereum code is still very large, but at a glance, the code structure is very good. I would like to start with some of the more independent modules to analyze. Then in-depth analysis of the internal code. Focus may focus on the Yellow Book is not involved in the network and other modules.

Bitcoin not catching up? Did you buy the ether currency? You missed the dream of becoming a millionaire but don't miss the chance to be a creator!!! Starting from July 7, every 8 o'clock in the evening-9 and half brothers even the blockchain academy officially starts:http://www.ydma.cn/open/course/16  "Tsinghua Genius with the global blockchain competition champion team" with your real-world block CHAIN development!!!

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.