Fabric source Compilation and examples

Source: Internet
Author: User

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 directory will have Gopm,go get is downloaded and compiled meaning
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
Then use go install Goimports
Go Install Golang.org/x/tools/cmd/goimports
/home/gopath/bin Directory will have 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 Directory will have 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 Directory will have Gocov-xml
2.5 Installing additional components
Yum install-y gcc libtool libltdl-dev libtool-ltdl-devel OpenSSL

3. Syntax understanding of theMakefile 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.
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.

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 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----------------------")
    },
}

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 ())

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

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.