Fabric source Compilation and examples

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

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 was created by compiling a code-shaped Docker image of the peer/order/fabric-ca/module in fabric. A solid compiled image allows the system we are developing to be locked down, and each application system to be developed has its own characteristics, and we need to build the image that the application needs by compiling the rewritten module code of the fabric. For example, Ali is a self-compiled software development package based on OPENJDK custom TAOBAOVM.
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 and modify the bug. Source code compilation is based on the fabric of the source of the reading, when the system is 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/gopathThere are two folders under this folder:srcUsed to store fabric source code.binUsed to store the go-compiled executable file.
2.1Install the Go Package management toolgopm.
export GOPATH=/home/gopath
go get -u github.com/gpmgo/gopm
/home/gopath/bin目录下将有gopm,go get是下载和编译的意思
2.2PassgopmInstallationGoimports. (Go Code formatting tool, automatically fix import of the package)
export GOPATH=/home/gopath
gopm get -g -d golang.org/x/tools/cmd/goimports
再使用 go install 安装 goimports
go install golang.org/x/tools/cmd/goimports
/home/gopath/bin目录下将有goimports
2.3InstallationGocov(Go's unit test coverage Check 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目录下将有gocov
2.4InstallationGocov-xml
GOPATH=/home/gopath
gopm get -g -d github.com/AlekSi/gocov-xml
go install github.com/AlekSi/gocov-xml
/home/gopath/bin目录下将有 gocov-xml
2.5 Installing additional components
yum install -y gcc libtool libltdl-dev libtool-ltdl-devel openssl

3.MakefileThe grammatical understanding of the file.
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.1The general form of the makefile file.
Target ...: Prerequisites ...
Command
...
...
which...Represents more than one.
Targetis a target file, which can be either an object or an executable file. It can also be a label (LabelMake will not automatically find the dependencies of the lable file, and will not automatically execute the commands defined later. Need to be explicitly usedMake Label。 Before the command is executed.
Prerequisitesis the file or target required to generate the target.
CommandThat is, make the command to execute. (Arbitrary shell Command)
Description, the general form above is a dependency of a file. 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.2Dependency 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.3The pseudo-target use method of the terminal compile command is make lable, to avoid the current folder also has a file called lable.
伪目标eg: .PHONY lable.PHONY避免同名 lable: rm *.o
The terminal uses make Labe, and the action is to delete all the. o files.
一般形式的eg : main.o : main.c defs.h cc -c main.c #make最后生成 main.o文件依赖于main.c defs.h两个文件,cc -c main.c执行可执行文件main.c进行编译.
3.4The first target in the 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出现protoc-gen-go执行文件
3. According to the instructions in the makefile file in the Fabric source directory. Open the terminal under the source directory, select the target you want to compile according to the targets pseudo target of the 1 Chinese box, execute the commandmake 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

Description: The command that runs when the peer network node is started is peer node start , in fact, 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 testimport (    "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 that interacts with the network.`,    Run: func(cmd *cobra.Command, args []string) {        logger.Info("Build test by JinaWenJun----------------------")    },}

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))    //添加的另外一个test命令,并在前面导入包"github.com/hyperledger/fabric/peer/test"    mainCmd.AddCommand(test.Cmd())

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 the terminal and executepeer test


Solution to problems encountered

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



Method: You need to add environment variables under terminal to perform 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,


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 error occurs.


Method: Based on the error prompt core/chaincode/ccproviderimpl.go:20:2:Cannot find the context. Go to the corresponding file Ccproviderimpl.go modify.


If you are prompted where you cannot find the "context" below the specified file, modify it as described above.
编译成功:

The corresponding build/bin/folder has a peer executable file.
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.