Fabric source analysis of the threads

Source: Internet
Author: User
Tags requires
Fabric Source Analysis of the threads Getting Started

Simply mention the Getting Started section of the fabric documentation. Description document download address in http://hyperledger-fabric.readthedocs.io/en/latest/. Where there is en in the Web site, the description of the various language versions of the document may be in the development, I hope that the Chinese version as soon as possible, after all, in some concepts, the understanding of the individual in English is still a little difficult. Getting started main guide you to do the following things: according to the requirements of prerequisites deployment environment, installation of Docker,go and so on. To download a separate small example of the operation experience and the desired image, unzip is a release directory, the following steps are to manipulate this small example of files, scripts or programs. Use Configtxgen and Cryptogen to generate configuration files for the area chain, gensis block files, trading profiles. These two tools can also be generated manually. Run the Docker image with the shell script. Execute the Peer command, deploy the area chain, execute an actual transaction, transfer A's 10 dollars to B, and then inquire. Find the Threads .

Start a strange project, my level can only begin with the main function. Think about this for a long time, also consulted some people, starting from the main function is the fastest and easiest way. Fortunately, the go language inherited a lot of C features, and we also have grep such a tool to search. Execute the following command at the fabric root--recursively search the main function in all go files and import the results into the Func_main.txt file:

grep "Func main" *-r-n--include=*.go > Func_main.txt

Opening the Func_main.txt file will find about seventy or eighty records, but it's quite regular, as follows:

As you can see, the results are concentrated only in the Bddtests,common,core,examples,orderer,peer,vendor directories.

Adhering to the learning posture, the results can be divided into three categories: the results can be directly eliminated, can assist in the study of source code results, the source itself results.

You can first remove all the results from vendor, because the vendor directory is the code for the third-party library used by the go language. Second, there are test,example,sample words in the catalogue, all belong to the code of test and example category, can refer to the auxiliary source of our research, because sometimes do not understand the source code, see how the example is used, you may know what this function is to do or how to use. Finally, the source code itself, but also we want to study the thread, after the removal of the above two steps, the remaining, that is, source code: 2 Common/configtx/tool/configtxgen/main.go:310:func mainc () {3 common/tools /cryptogen/main.go:203:func Main () {Orderer/main.go:50:func main () {Peer/main.go:77:func main () {

If you manipulate getting started in a fabric document, it is easy to speculate that 2, 3 lines of these two main are used to generate Configtxgen and Cryptogen two tools. The main function of the 31 line is used to generate the peer program, because the operation of the transaction in the command exercise using the peer .... Getting started although there is no obvious use of the operation on the Orderer, but since it is assigned to the main function, it is self-contained, as a service, the final generation will be a orderer program.

In this way, the study of the whole project source code to pull out the thread is not at a glance. Verify the Threads

From Getting Started start, because can be a project Getting Started, is certainly belong to the very perfectly formed.

We know that Getting started started some containers with a script and deployed these containers as distributed clients for regional chains, delivering messages and transactions. Starting the container operation is the ./network_setup.sh up command, which is the network_setup.sh script used to view the file, and you know that the command ultimately executes the Networkup function in the script. Understanding this step requires you to know the basics of scripting.

The Networkup function primarily runs the generatecfgtrx.sh script (two tools are generated to generate the necessary configuration files for the region chain using two tools) and docker-compose-f $COMPOSE _file up-d command, where Docker-compose is the command that launches the appropriate container according to the specified YAML configuration file. The profile is Docker-compose-no-tls.yaml (the value of $COMPOSE _file), which in turn shifts our gaze to the Docker-compose-no-tls.yaml configuration file. Understanding this step requires you to know the basic Docker container operations and Docker-compose commands.

As we can see from the Yaml configuration file, the Docker-compose command launches the orderer0,peer0,peer1,peer2,peer3,cli six container service. In other words, the services running in these six containers cover the main services that will be generated in the source code we are studying, and are the most important core services. Understanding this step, along with each of the following steps, requires you to understand the basic configuration items of the Yaml configuration file.

The command option for the Orderer0 container is orderer, which starts the orderer program by default when the Orderer container is started. It corresponds to the orderer program generated by the main function in the Orderer directory we are involved in. This also shows that when the container is started, the Orderer program already exists with the container.

Each peer container does not have a command option, but has the extends option, which loads the peer-base-no-tls.yaml configuration file under the Peer-base directory. When we open the file, we see that the Peer-base service that it loads is executed by default as peer node Start–peer-defaultchain=false , which is Peer command , which corresponds to the peer program generated by the main function of the peer we are involved in. This also means that when the container is started, the peer program already exists with the container.

The command option for the CLI container service is /bin/bash-c './scripts/script.sh ${channel_name}; ' , the script.sh script under the scripts directory is executed by default when the container is started. Opening the script.sh script, we found that the final command was the Peer channel ... With peer Chaincode, the Peer command is executed, which corresponds to the peer program generated by the main function of the peer we are involved in.

Depending on the depends_on option (dependency) of each container, we can even speculate on which commands and services must be run before some command services in the area chain. Orderer container First, each peer can actually have no sequencing but must be after with the Orderer container, and finally the CLI container. This means that the Orderer service must precede the peer service, and the Peer node Start command must precede the peer channel or peer Chaincode command.

>
Ultimately, these points lead us to the main function of Orderer and peer , which is to study the source of the entire project. The next source analysis will begin with the thread of the main function of peer. simple spelling in the source code

Projects that can be supported by the Linux Foundation are naturally poor. The code is good at a glance. Many places in the code are all English-spelling, and sometimes you don't need to look at readme.md or annotations to know what the directory or function is for. But some directories, filenames also have the initials, know that these spelling for the smooth reading of the code is also very useful. Here are some of the simple pieces I've collected in my reading code, so you can look at them and correct them if you can't translate them.

Msp:membership Service Provider Member Services provider
Bccsp:blockchain (first two letters BC) cryptographic service provider zone chain Cryptographic Services Provider
Ab:atomic Broadcast Atomic (operation) broadcast
Lscc:lifecycle (L) system (S) Chaincode (CC) life cycle system chain code
Spec:specification, Specification standard, detailed description
Kv:key-value Key-value
Cds:chaincodedeploymentspec
Cis:chaincodeinvocationspec
Mgmt:management
Sw:software-based
Ab:atomicbroadcast
Gb:genesis block, block of the century, the first block in the area chain
CC or Cc:chaincode
SCC or Scc:system Chaincode
Cscc:configer system Chaincode
Lscc:lifecycle system Chaincode
Escc:endorser system Chaincode
Vscc:validator system Chaincode
Qscc:querier system Chaincode
Alg:algorithm algorithm
Mcs:mspmessagecryptoservice
Mock: To pretend, to learn, to imitate, basically to serve xxx_test.go, that is, for testing.
GOSSIP: An algorithm to achieve the final consistent state of distribution nodes
Attr:attribute
Fsblockstore:file System block Store
vdb:versioned database is also the state databases
Rtenv:runtime Environment Operating Environment
PKCS11:PCKS#11, a public-key encryption standard with a set of interfaces called Cryptoki, is a platform-device-agnostic API
Mcs:mspmessagecryptoservice, message Encryption service
Sa:securityadvisor
Impl:implement, many places Xxx.go and xxximpl.go are corresponding, the former is used for interface or definition, the latter is to implement the interface or the definition of
Fsm:finite State Machines Limited Machine
Fs:filesystem File System
Blk:block
Cli:command Lines Interface Command line interface
In Cfg:fabric_cfg_path, it should be the meaning of config.
Mgr:manager
Cpinfo:checkpoint information, checkpoint information
Devmode:development mode, development model
Reg:register, registration, registration
Hdr:header
Impl:implement
Oid:objectidentifier, object identifier
OU or ou:organizational unit
Crl:certificate Revocation list, revoking certificate lists
Prop:proposal, application, Exchange-sent application
Acl:access control List, access controls lists
Rwset:read/write set, read-write set
Tx,tx:transaction, trading
Csp:cryptographic Service PROVIDER,BCCSP After three letters, cryptographic services provider
Opt:option, Options
Opts:options, multiple options
SKI: Current certificate identification, the so-called identity, is generally a hash of the public key
AKI: The signatory's ski, that is, the signatory's public key identification
Hsm:hardware Security Modules
Ks:keystore,key storage, this key refers to the public key that is used for signing the private key
Oid:object IDENTIFIER, Object identitythe practice in the source code

There are many conventions in the source code, and it is a good habit to have in a large project (which is irrelevant to the language). This is also worth learning. The current practice of personal discovery has the following points, which are also helpful for reading the source code: the common directory is the common one in the hierarchy at which it resides. A/common, the code in the common is common in the a scope, A/b/c/common, and the code in the common is common in the C directory. A mock directory is a simulation data/environment that is used to facilitate testing in the Go test file (i.e., numerous xxx_test.go). This type of directory can be ignored in the initial phase of the research source. Xxx.go and Xxximpl.go are the companion codes for definition and implementation. The same transaction exists under different topics, respectively. Peer in the Protos directory and peer in the core directory are peer-related code, but the related topic code is placed separately. The NO-TLS is marked with NO-TLS and the description related code is not using secure transport Protocol (TLS). The Util folder, which is usually the source code of this level or the topic, has the auxiliary, the tool. the basic structure of the source directory

In fact, the basic structure of the source directory should be in the process of exploring the source of their own natural understanding. In this only to do record use, do not recommend you look at this part, but suggest that you read the source in the process of their own constantly through the catalogue, and slowly understand. The first source of research, you need to focus on the following directories. BCSSP Cryptographic Service Code Catalog Common global Common Code directory core core functionality code directory docs takes the. rst file as the kernel and compiles the generated document. Directory of the Description document event code directory for production and consumption information Examples sample directory gossip is the meaning of gossip, is a kind of can achieve de-centrality, there is a certain fault tolerance and can achieve the ultimate consistent communication algorithm MSP Member Services code catalog Orderer To understand the Orderer directory is good, Orderer is also considered a special term in the regional chain, used for message subscription and distribution processing Protos prototype catalog, define a variety of prototypes and generated corresponding XXX.PB.GO source code vendor originally intended to be vendors, This is where all the various third-party packages used in Go are stored

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.