Fabric source Reading and compiling

Source: Internet
Author: User
Tags using git git clone hyperledger fabric
Preface

Hyperledger fabric, what is hypeledger and what is fabric? Hypeledger literally means the Super ledger, which was launched by the Linux Foundation in December 2015 as an open-source project called "Super Ledger", designed to drive collaboration to create a blockchain-based, enterprise-class distributed ledger underlying technology for building industry applications and platforms that support the business To support a wide variety of commercial scenarios. Fabric is a project in the super Ledger that provides the underlying support for the business district block chain application. Fabric is a combination of code from three companies such as IBM, digital assets, and Blockstream. preparatory work

Environment Preparation (Ubuntu)

A. Installing the Go development environment
B. Install Git tools and environment configuration
C. Download the fabric source using Git.
git clone https://github.com/hyperledger/fabric.git (preferably downloaded under the SRC folder of the Go configuration path)

Skill Preparation

The 2.1 fabric project is written primarily using the Go language, all requiring the basics of go language.

A. Basic knowledge of Go grammar.
B.go knowledge of concurrency.
C. The idea of a certain project architecture design pattern

2.2 Data interactions between peer network nodes deployed through fabric projects are communication between different service (processes), using GRPC (Google's Remote Procedure call protocol). So it is necessary to master the basic usage and knowledge of GRPC. The Interactive data format uses Protocol buffers. reading style-how

Step 1 understands the role of each module package under the Fabric project, the fabric source project is made up of different module packages, and understanding the role of each module package can help you quickly find the source class you need to read. module and package division refer to http://qukuaiwang.com.cn/ News/722.html. The fabric project has three main modules peer and order and FABRIC-CA, wherein the FABRIC-CA module requires additional source code. This article discusses the source of the fabric, Only peer and order two modules are included. The different packages in the other fabric sources are for these two modules.

Step 2 Select the module section you want to read and understand.

The contents of peer and O-service belong to the modules in the fabric source code.

Source reading for step 3 module (example)
Note: The image required for the Docker deployment Fabric Network is a part of the module that compiles the source code of the fabric, for example, the peer image in the fabric network is through the make peer module's source code.
eg if you choose to read the source code of the order module, you need to control the order container that is started by compiling the order Docker image generated from the source code of the Order module.

3.1 The following figure is a Docker container rollout that is initiated by a fabric-deployed network node.-The last container to start is the information that starts the order container, the command that runs at startup is "Orderer", and the container service open port is Port 7050. For GRPC service communication.

3.2 Enter the folder of the fabric source directory and locate the Main.go portal file.

Code understanding of 3.3 Main.go Portal file

Description of the third-party package in the Main.go file
Kingpin-A Go (golang) command line and the flag parser support subcommands.

Initialization of the command variable, app var (app = Kingpin. New ("Orderer", "Hyperledger Fabric orderer node") Start = App. Command ("Start", "Start the Orderer node"). Default () Version = App. Command ("version", "Show version Information"))//Ingress function main Func main () {//Start parsing commands kingpin switch kingpin. Mustparse (app. Parse (OS. Args[1:]) {//"Start" command case start. Fullcommand (): Logger. Infof ("Starting%s", metadata. GetVersionInfo ()) conf: = config. Load () initializelogginglevel (CONF) initializeprofilingservice (conf) Grpcserver: = Initializegrpcs Erver (CONF)//Initialize GPRC service configuration initializelocalmsp (conf) Signer: = Localmsp.
        Newsigner () Manager: = Initializemultichainmanager (conf, signer) Server: = NewServer (manager, signer) Ab. Registeratomicbroadcastserver (Grpcserver.server (), Server) logger. Info ("Beginning to serve Requests") Grpcserver.start ()//Start service//"version" command case version.
Fullcommand ():        Fmt. Println (metadata. GetVersionInfo ())}}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

When the 1.Order Docker container is running, the init command is "Orderer" and the init command parameter flag defaults to "Start", start = App.command ("Start", "Start the Orderer node"). Default ()
2. The parse command is a switch that is the "start" command, which runs start. The code under the Fullcommand () branch, which performs some initialization operations, is followed by a detailed view of the source initialization details of the order module. Reference Links

Protocol Buffers's study notes

GRPC Study Notes

2 ..... ...................

Source code compilation-why?
The compilation of fabric source code is based on the basic knowledge of some of the modules or modules of the fabric. But why compile the source of the fabric, the gods have already written the fabric of the project framework and compiled it, not to use it?

1. The Docker container that deploys the fabric network is created by compiling a code-shaped Docker image of the peer/order/fabric-ca/module in fabric. The built-in image of the solid has been locked down by the system we're developing. Each application system to be developed has its own characteristics, and we need to build the image required by the application system by compiling the rewritten module code of the fabric. For example, Ali through the TAOBAOVM based on OPENJDK customization, but also self-compiled software development package.
2. Compile the source of the fabric can verify their own ideas, when reading the fabric source code, how it runs just through our theoretical conjecture, through the actual compiled code can verify our conjecture.
3. Locate the error, modify the bug. The source code compilation is based on the fabric of the source of the reading, when the system running in the log error, according to the error can be located in the source code error points, according to the source can modify the bug. preparatory work

1. Installation of the compilation environment, go installation, the source code of the fabric download, in the fabric of the source of the reading of a story.
2. Install the other necessary components.

Suppose Gopath is/home/gopath there are two folders under this folder: SRC is used to store the fabric source code. The bin is used for the go-compiled executable file.
2.1 Install the Go Package management tool GOPM.
Export Gopath=/home/gopath
Go get-u github.com/gpmgo/gopm
/home/gopath/bin directory will have Gopm,go get is downloaded and compiled meaning
2.2 Install Goimports via GOPM. (Go Code formatting tool, automatically fix import of the package)
Export Gopath=/home/gopath
GOPM Get-g-D golang.org/x/tools/cmd/goimports
Then use go install Goimports
Go Install Golang.org/x/tools/cmd/goimports
/home/gopath/bin Directory will have goimports
2.3 Installing Gocov (Go's Unit Test coverage checker tool)
Gopath=/home/gopath
GOPM Get-g-D golang.org/x/tools/cover
GOPM Get-g-D Github.com/axw/gocov/gocov
Go Install Github.com/axw/gocov/gocov
/home/gopath/bin Directory will have Gocov
2.4 Installing Gocov-xml
Gopath=/home/gopath
GOPM Get-g-D github.com/aleksi/gocov-xml
Go Install Github.com/aleksi/gocov-xml
/home/gopath/bin Directory will have Gocov-xml
2.5 Installing additional components
Yum install-y gcc libtool libltdl-dev libtool-ltdl-devel OpenSSL

The syntax of the 3.Makefile file is understood.
The source image of fabric is compiled by make according to the compilation rules of the files in makefile.

The grammar rules of Makefile are briefly introduced.
3.1 The general form of the makefile file.
Target ...: Prerequisites ...
Command
...
...
where ... Represents more than one.
Target is a destination file, which can be either an object or an executable file. It can also be a label, and make will not automatically find the dependencies of the lable file, and it will not automatically execute the commands defined later. Make Label needs to be explicitly used. Before the command is executed.
Prerequisites is the file or target that is required to generate the target.
command is what the make needs to execute. (Arbitrary shell Command)
Note that the general form above is a file dependency. Target this one or more of the destination files depend on the files in the prerequisites, and their generation rules are defined in the command. In particular, if more than one file in the prerequisites is newer than the target file, command-defined commands are executed. This is the rule of makefile.
3.2 Dependency is essentially a description of which files are generated by the target file, in other words, which files are updated by the target file.
3.3 Pseudo-target use method of the terminal compile command is make lable, to avoid the current folder also has a file called lable.
Pseudo target eg:
. Phony lable. Phony avoid the same name
Lable:
RM *.O

The terminal uses make Labe, and the action is to delete all the. o files.
General form of eg:
MAIN.O:MAIN.C Defs.h
Cc-c MAIN.C
#make最后生成 the MAIN.O file relies on main.c defs.h two files, cc-c MAIN.C executes the executable file main.c to compile.

The first target in the 3.4 makefile is used as its default target. How

1. Enter the fabric source directory/home/gopath/src/hyperledger/fabric. and add Gopath environment variables, be sure to add.
Export Gopath=/home/gopath
sudo su

2. Compile Build Protoc-gen-go
CD $GOPATH
GOPM Get-g-D github.com/golang/protobuf/protoc-gen-go
Go Install github.com/golang/protobuf/protoc-gen-go
#/home/gopath/bin appears protoc-gen-go execution file
3. According to the instructions in the makefile file in the Fabric source directory. Open the terminal under the source directory, according to the targets pseudo-target of the Chinese box below, select the target you want to compile, execute command make peer/all/orderer ...

After you execute the make command, a build folder is generated under the current folder. A compiled image or a running tool is found in this folder. code examples for compiling peer modules

Note: The command that is run when the peer network node is started is peer node start, actually the peer of the Fabric network node The Docker container executes a peer executable that is compiled from the fabric source to manipulate commands under peer. In this example, the test command is added by modifying the code and compiling it under the peer node.
1. Add the test package under/home/gopath/src/hyperledger/fabric/peer. and create the Test.go file under the test package.

Package Test
Import (
    "github.com/hyperledger/fabric/common/flogging" "
    Github.com/spf13/cobra"
)
var logger = flogging. Mustgetlogger ("Testcommand")
func Cmd () *cobra. Command {
    return testcommond
}
var testcommond = &cobra. command{use
    :   "Test", Short
    : "Test the node.",
    Long:  ' Test a node, interacts with the Network. ',
    run:func (cmd *cobra. Command, args []string] {
        logger. Info ("Build test by Jinawenjun----------------------")
    },
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

2. In the Main.go file under/home/gopath/src/hyperledger/fabric/peer, add

    Maincmd.addcommand (version. CMD ())
    Maincmd.addcommand (node. CMD ())
    Maincmd.addcommand (Chaincode. CMD (nil))
    Maincmd.addcommand (clilogging. CMD (nil))
    Maincmd.addcommand (channel. CMD (nil))
    //Add another Test command and import the package "Github.com/hyperledger/fabric/peer/test"
    maincmd.addcommand (Test) in front. CMD ())
1 2 3 4 5 6 7

3. In the source directory open the terminal to compile make peer, under/home/gopath/src/hyperledger/fabric/build/bin will generate the executable peer. Copy the file to/home/gopath/ Tests the newly added test command under bin. (simulates the execution of a peer command under a peer Docker container).
4. Open Terminal, execute peer test
solution to problems encountered

1. When the terminal executes make command, there is no that file or directory as shown,

Method: You need to add environment variable under Terminal, execute export gopath=/home/gopath

2. When the network terminal is stuck, the curl portion of the makefile file needs to be removed. Manually download the appropriate file Chaintool directory/build/bin below.

3. When the file cannot be found or the command cannot be found, as shown in figure

Method: The terminal executes the following two commands to copy the command into
CP $GOPATH/bin/protoc-gen-go $GOPATH/src/github.com/hyperledger/fabric/build/docker/gotools/bin/

CP $GOPATH/bin/gocov $GOPATH/src/github.com/hyperledger/fabric/build/docker/gotools/bin/

4. When the terminal make peer appears in the following image error.

Method: Based on Error Core/chaincode/ccproviderimpl.go:20:2: Cannot find the context. Go to the appropriate file Ccproviderimpl.go modify it.

If you are prompted where you cannot find the "context" below the specified file, modify it as described above.
Compilation succeeded:
The peer executable file under the corresponding build/bin/folder

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.