Refer to the video tutorial:
**Learn Hyperledger Fabric actual combat alliance chain **
Getting started with Hyperledger Fabric 1: get started quickly
jasonruan 2018.11.17
- preface
This article is the first article to learn about the super ledger Fabric. It mainly experiences the basic function of building a network environment quickly, and uses the self-contained sample to experience the deployment and call of chain code, so as to pave the way for the subsequent gradual deepening of its internal principle and chain code development.
- Noun interpretation
I have just learned the super ledger Fabric, and many terms are really obscured. Some terms are excerpted below.
term | meaning |
---|---|
Anchor Peer | A peer node in a channel that can be detected by all peer nodes and can communicate with them. Each member in the channel has one or more anchor nodes, allowing nodes belonging to different identities to discover other nodes in the channel. |
Block | Blocks are a set of orderly transactions, which are connected with the preceding blocks after hash encryption in the channel. |
Chaincode | The application code on the blockchain is extended from the concept of "smart contract". |
Channel | Private isolation on Fabric networks. The chain codes and transactions in the channel are visible only to the nodes joining the channel. |
Consensus | Consensus is a comprehensive verification of the correctness of a group of transactions containing blocks throughout the whole transaction process. |
Endorser (Endorsement node) | Be responsible for checking whether a transaction is legal and willing to guarantee and sign for it. |
Fabric Ca (certificate node) | The default certificate management component, which issues PKI based certificates to network members and their users. CA issues a root certificate for each member, a registration certificate for unauthorized users, and a large number of transaction certificates for each registration certificate. |
Genesis Block | It is the first block on the blockchain and the configuration block for initializing the blockchain network or channel |
Ledger (ledger) | The ledger is a database maintained by each peer in the channel. |
MSP (Member Service Provider) | Abstract components that implement member services (authentication, certificate management, etc.) to achieve pluggable support for different types of member services. |
Orderer Peer | It is responsible for sorting the transactions seen and providing the order of global confirmation. |
Ordering Service | The collection of nodes that put transaction sorting into the block. The sorting service is independent of the peer process and sorts transactions for all channels on the network in a first come first served manner. |
State Database | The current status data is stored in the status database for effective reading and query from chaincode. These databases include levelDB and couchDB. |
Transaction | Execute a function call on the ledger or deploy chaincode. The specific functions called are implemented in chaincode. |
Validating Peer | Maintain the core node of the ledger, participate in consistency maintenance, transaction verification and execution. |
World State | The latest global ledger status. Fabric uses it to store the latest status generated after historical transactions, which can be realized by key value or document database. |
- Environment construction
2.1 pre dependency
Omit the specific installation steps
-
ubuntu 18.04
-
go 1.11.2
-
docker-ce 18.09
-
docker-compose 1.23.1
2.2 fabric Download
Download address: https://github.com/hyperledger/fabric
$ git clone https://github.com/hyperledger/fabric.git Cloning into 'fabric'... remote: Enumerating objects: 183, done. remote: Counting objects: 100% (183/183), done. remote: Compressing objects: 100% (164/164), done. remote: Total 81151 (delta 64), reused 135 (delta 18), pack-reused 80968 Receiving objects: 100% (81151/81151), 77.50 MiB | 440.00 KiB/s, done. Resolving deltas: 100% (51831/51831), done.
2.3 fabric installation
Steps:
-
Enter the clone good source directory fabric/scripts
-
Execute the bootstrap.sh script, which will perform the following operations:
>First, install fabric samples
(1) Download hyperledger / fabric samples
Installing hyperledger/fabric-samples repo ===> Cloning hyperledger/fabric-samples repo and checkout v1.3.0
(2) Download the fabric binary package of the current platform
===> Downloading version 1.3.0 platform specific fabric binaries ===> Downloading: https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/linux-amd64-1.3.0/hyperledger-fabric-linux-amd64-1.3.0.tar.gz
(3) Download the CA client of the current platform
===> Downloading version 1.3.0 platform specific fabric-ca-client binary ===> Downloading: https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric-ca/hyperledger-fabric-ca/linux-amd64-1.3.0/hyperledger-fabric-ca-linux-amd64-1.3.0.tar.gz
>Next, pull the fabric image
(4) peer node mirroring
==> FABRIC IMAGE: peer 1.3.0: Pulling from hyperledger/fabric-peer Digest: sha256:6756c7c48234ae6b0a8822a378681017cf6dbfeadfbf1f8a528ee6c4db343621 Status: Image is up to date for hyperledger/fabric-peer:1.3.0
(5) Sort node mirror
==> FABRIC IMAGE: orderer 1.3.0: Pulling from hyperledger/fabric-orderer Digest: sha256:6ee1abcfd84031765d67544e5d6b4f3af08c3f064312c65715587d392fe7c3eb Status: Image is up to date for hyperledger/fabric-orderer:1.3.0
(6) CC environment image
==> FABRIC IMAGE: ccenv 1.3.0: Pulling from hyperledger/fabric-ccenv Digest: sha256:05fce5513fcae3110ac041469ed9e0e4c9661f44782f52ef5d8930eb416c2197 Status: Image is up to date for hyperledger/fabric-ccenv:1.3.0
(7) Tool mirroring
==> FABRIC IMAGE: tools 1.3.0: Pulling from hyperledger/fabric-tools Digest: sha256:058cff3b378c1f3ebe35d56deb7bf33171bf19b327d91b452991509b8e9c7870 Status: Image is up to date for hyperledger/fabric-tools:1.3.0
>Next, pull the fabric CA image
(8) CA image
==> FABRIC CA IMAGE 1.3.0: Pulling from hyperledger/fabric-ca Digest: sha256:83abc367c5273a12d59ef9777637eb6c1abf04a5d00d66a0bffc55c40075850e Status: Image is up to date for hyperledger/fabric-ca:1.3.0
>Finally, pull the third-party docker image
(9) couchdb mirror
==> THIRDPARTY DOCKER IMAGE: couchdb 0.4.13: Pulling from hyperledger/fabric-couchdb Digest: sha256:b26dfcf9abb466ffb60d0734d0c5c3bf1d4816c96e944ba69584d94681028cfd Status: Image is up to date for hyperledger/fabric-couchdb:0.4.13
(10) kafka mirror
==> THIRDPARTY DOCKER IMAGE: kafka 0.4.13: Pulling from hyperledger/fabric-kafka Digest: sha256:7caa31210504bc99ae45d19d528671f238d25418d9433c42f8c582ba415703b4 Status: Image is up to date for hyperledger/fabric-kafka:0.4.13
(11) zookeeper mirror
==> THIRDPARTY DOCKER IMAGE: zookeeper 0.4.13: Pulling from hyperledger/fabric-zookeeper Digest: sha256:9fdff6aa2c086816ad693339c25f1e49ea4a0b6b7102c7342aff4137991a15a1 Status: Image is up to date for hyperledger/fabric-zookeeper:0.4.13
After the image is pulled, the image list is listed
===> List out hyperledger docker images hyperledger/fabric-ca 1.3.0 5c6b20ba944f 4 weeks ago 244MB hyperledger/fabric-ca latest 5c6b20ba944f 4 weeks ago 244MB hyperledger/fabric-tools 1.3.0 c056cd9890e7 4 weeks ago 1.5GB hyperledger/fabric-tools latest c056cd9890e7 4 weeks ago 1.5GB hyperledger/fabric-ccenv 1.3.0 953124d80237 4 weeks ago 1.38GB hyperledger/fabric-ccenv latest 953124d80237 4 weeks ago 1.38GB hyperledger/fabric-orderer 1.3.0 f430f581b46b 4 weeks ago 145MB hyperledger/fabric-orderer latest f430f581b46b 4 weeks ago 145MB hyperledger/fabric-peer 1.3.0 f3ea63abddaa 4 weeks ago 151MB hyperledger/fabric-peer latest f3ea63abddaa 4 weeks ago 151MB hyperledger/fabric-zookeeper 0.4.13 e62e0af39193 4 weeks ago 1.39GB hyperledger/fabric-zookeeper latest e62e0af39193 4 weeks ago 1.39GB hyperledger/fabric-kafka 0.4.13 4121ea662c47 4 weeks ago 1.4GB hyperledger/fabric-kafka latest 4121ea662c47 4 weeks ago 1.4GB hyperledger/fabric-couchdb 0.4.13 1d3266e01e64 4 weeks ago 1.45GB hyperledger/fabric-couchdb latest 1d3266e01e64 4 weeks ago 1.45GB hyperledger/fabric-baseos amd64-0.4.13 f0fe49196c40 4 weeks ago 124MB
- Build network
The following is to quickly build our first super ledger fabric network based on BYFN(build your first network) provided by fabric samples to get familiar with the whole operation process.
3.1 generation configuration
3.1.1 execute command
Enter: fabric / scripts / fabric samples / first network /, and execute the command:
$ sudo ./byfn.sh -m generate -c jschannel
3.1.2 command description
-
This command can also be executed without execution. When using the up command to start the network, if it is found that the required configuration is not generated, it will be executed automatically.
-
This command will generate initialization configuration according to the configuration file (crypto-config.yaml), including MSP certificates of Peer node and sorting service node
# crypto-config.yaml OrdererOrgs: - Name: Orderer Domain: example.com Specs: - Hostname: orderer PeerOrgs: - Name: Org1 Domain: org1.example.com EnableNodeOUs: true Template: Count: 2 Users: Count: 1 - Name: Org2 Domain: org2.example.com EnableNodeOUs: true Template: Count: 2 Users: Count: 1
- Generate Genesis blocks according to the configuration file configtx.yaml
# configtx.yaml Organizations: - &OrdererOrg Name: OrdererOrg ID: OrdererMSP MSPDir: crypto-config/ordererOrganizations/example.com/msp Policies: ...... - &Org1 Name: Org1MSP ID: Org1MSP MSPDir: crypto-config/peerOrganizations/org1.example.com/msp Policies: Readers: ...... AnchorPeers: - Host: peer0.org1.example.com Port: 7051 - &Org2 ...... Capabilities: ...... Application: &ApplicationDefaults Organizations: Policies: ...... Orderer: &OrdererDefaults OrdererType: solo Addresses: - orderer.example.com:7050 BatchTimeout: 2s BatchSize: ...... Kafka: Brokers: - 127.0.0.1:9092 Organizations: Policies: ...... Channel: &ChannelDefaults ...... Capabilities: <<: *ChannelCapabilities Profiles: ......
3.1.3 step description
(1) Generate certificate
Use the cryptogen tool to generate MSP certificates for org1 and org2. MSP certificates are the identity of super ledger network entities. Entities use certificates for signature and verification during communication and transactions. After successful execution, the certificate file will be placed in the crypto config directory.
########################################################## ##### Generate certificates using cryptogen tool ######### ########################################################## + cryptogen generate --config=./crypto-config.yaml org1.example.com org2.example.com
(2) Create creation block
The creation block is generated for the sorting service. After successful execution, the creation block file: genesis.block will be generated in the channel artifacts directory
########################################################## ######### Generating Orderer Genesis block ############## ########################################################## + configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
(3) Create channel
The channel configuration file channel.tx is generated. After successful execution, the channel configuration channel.tx will be generated in the channel artifacts directory
################################################################# ### Generating channel configuration transaction 'channel.tx' ### ################################################################# + configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID jschannel
(4) Generate anchor node configuration
Generate the anchor node configuration of Org1MSP and Org2MSP. After successful execution, the generated anchor point configuration files Org1MSPanchors.tx and Org2MSPanchors.tx are also located in the directory channel artifacts.
################################################################# ####### Generating anchor peer update for Org1MSP ########## ################################################################# + configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID jschannel -asOrg Org1MSP ################################################################# ####### Generating anchor peer update for Org2MSP ########## ################################################################# + configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID jschannel -asOrg Org2MSP
3.1.4 configuration output
To sum up, after the command is executed, two folders will be generated in the current directory, with the contents as follows:
- Channel artifacts
$ tree channel-artifacts/ channel-artifacts/ ├── channel.tx # Channel configuration Chuangshi block ├── genesis.block # Sorting service Genesis block ├── Org1MSPanchors.tx # Org1 anchor node configuration └── Org2MSPanchors.tx # Org2 anchor node configuration
- Certificate directory (crypto config)
>This directory stores MSP certificate files for generating sorting service nodes and Peer nodes
$ tree crypto-config crypto-config ├── ordererOrganizations │ └── example.com │ ├── ca │ │ ├── 2b749470c3d6cae6d63caa7b69ee9ebbc310ca68526521e444012f186e97bd92_sk │ │ └── ca.example.com-cert.pem │ ├── msp │ │ ├── admincerts │ │ │ └── Admin@example.com-cert.pem │ │ ├── cacerts │ │ │ └── ca.example.com-cert.pem │ │ └── tlscacerts │ │ └── tlsca.example.com-cert.pem │ ├── orderers │ │ └── orderer.example.com │ │ ├── msp │ │ └── tls │ │ ├── ca.crt │ │ ├── server.crt │ │ └── server.key │ ├── tlsca │ │ ├── c79b35c1154c5377923a77dfa6036707652715671e2b1450e6313b64a65f238e_sk │ │ └── tlsca.example.com-cert.pem │ └── users │ └── Admin@example.com │ ├── msp └── peerOrganizations ├── org1.example.com │ ├── ca │ ├── msp │ ├── peers │ ├── tlsca │ └── users └── org2.example.com
3.2 start the network
3.2.1 execute command
$ sudo ./byfn.sh -m up -c jschannel
3.2.2 command description
-
After executing this command, first start the super ledger fabric network according to the docker-compose-cli.yaml configuration file
-
It contains 1 sorting service node, 4 Peer nodes, and 1 command line container cli
# docker-compose-cli.yaml services: orderer.example.com: extends: file: base/docker-compose-base.yaml service: orderer.example.com container_name: orderer.example.com networks: - byfn peer0.org1.example.com: container_name: peer0.org1.example.com extends: file: base/docker-compose-base.yaml service: peer0.org1.example.com networks: - byfn peer1.org1.example.com: ...... peer0.org2.example.com: container_name: peer0.org2.example.com extends: file: base/docker-compose-base.yaml service: peer0.org2.example.com networks: - byfn peer1.org2.example.com: ...... cli: container_name: cli image: hyperledger/fabric-tools:$IMAGE_TAG tty: true stdin_open: true environment: ...... working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer command: /bin/bash volumes: - ./../chaincode/:/opt/gopath/src/github.com/chaincode - ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/ ...... depends_on: - orderer.example.com - peer0.org1.example.com - peer1.org1.example.com - peer0.org2.example.com - peer1.org2.example.com networks: - byfn
3.2.3 command output
The following information indicates that the super ledger Fabric network has been started.
Creating network "net_byfn" with the default driver Creating volume "net_orderer.example.com" with default driver Creating volume "net_peer0.org1.example.com" with default driver Creating volume "net_peer1.org1.example.com" with default driver Creating volume "net_peer0.org2.example.com" with default driver Creating volume "net_peer1.org2.example.com" with default driver Creating peer1.org2.example.com ... done Creating orderer.example.com ... done Creating peer1.org1.example.com ... done Creating peer0.org2.example.com ... done Creating peer0.org1.example.com ... done Creating cli ... done
3.2.4 viewing containers
Use the docker ps command to view the container startup
$ sudo docker ps CONTAINER ID IMAGE COMMAND PORTS NAMES c32c37c5742f hyperledger/fabric-tools:latest "/bin/bash" cli dadda8aff9ae hyperledger/fabric-peer:latest "peer node start" 0.0.0.0:8051->7051/tcp, 0.0.0.0:8053->7053/tcp peer1.org1.example.com f35e303b931d hyperledger/fabric-peer:latest "peer node start" 0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp peer0.org1.example.com 5bf8fdf0f804 hyperledger/fabric-peer:latest "peer node start" 0.0.0.0:9051->7051/tcp, 0.0.0.0:9053->7053/tcp peer0.org2.example.com 48c9b04f48db hyperledger/fabric-peer:latest "peer node start" 0.0.0.0:10051->7051/tcp, 0.0.0.0:10053->7053/tcp peer1.org2.example.com 023b2fed2737 hyperledger/fabric-orderer:latest "orderer" 0.0.0.0:7050->7050/tcp orderer.example.com
3.3 chain code call
3.3.1 execute command
In the $sudo. / byfn.sh - m up - C jschannel used to start the network, the started cli container will call scripts/script.sh script, which will create channels, add channels to Peer nodes, install and instantiate chain codes, and call and query chain codes. Let's further explain the command below.
3.3.2 command description
The specific commands executed in scripts/script.sh are as follows. Each command corresponds to a shell script function
## Create channel createChannel ## Join all the peers to the channel joinChannel ## Set the anchor peers for each org in the channel updateAnchorPeers 0 1 updateAnchorPeers 0 2 ## Install chaincode on peer0.org1 and peer0.org2 installChaincode 0 1 installChaincode 0 2 # Instantiate chaincode on peer0.org2 instantiateChaincode 0 2 # Query chaincode on peer0.org1 chaincodeQuery 0 1 100 # Invoke chaincode on peer0.org1 and peer0.org2 chaincodeInvoke 0 1 0 2 ## Install chaincode on peer1.org2 installChaincode 1 2 # Query on chaincode on peer1.org2, check if the result is 90 chaincodeQuery 1 2 90
3.3.3 step description
(1) Create channel
Enter the cli container and execute the create channel command. After successful execution, the channel configuration jschannel.block will be generated under the current path of the cli container, which needs to be used when adding channel operations.
Channel name : jschannel Creating channel... + peer channel create -o orderer.example.com:7050 -c jschannel -f ./channel-artifacts/channel.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem ===================== Channel 'mychannel' created =====================
(2) Four peer nodes join the channel in turn
By setting different environment variables in the cli container and setting the connected Peer nodes and MSP, four Peer nodes are added to the channel in turn
+ peer channel join -b jschannel.block Having all peers join the channel... ===================== peer0.org1 joined channel 'jschannel' ===================== + peer channel join -b jschannel.block ===================== peer1.org1 joined channel 'jschannel' ===================== + peer channel join -b jschannel.block ===================== peer0.org2 joined channel 'jschannel' ===================== + peer channel join -b jschannel.block ===================== peer1.org2 joined channel 'jschannel' =====================
(3) Update the organization's anchor node
Updating anchor peers for org1... + peer channel update -o orderer.example.com:7050 -c jschannel -f ./channel-artifacts/Org1MSPanchors.tx --tls true --cafile ========= Anchor peers updated for org 'Org1MSP' on channel 'jschannel' ========= Updating anchor peers for org2... + peer channel update -o orderer.example.com:7050 -c jschannel -f ./channel-artifacts/Org2MSPanchors.tx --tls true --cafile ========== Anchor peers updated for org 'Org2MSP' on channel 'jschannel' ==========
(4) Install chain codes on Peer nodes (peer0.org1 and peer0.org2)
The application executes the function of smart contract through the chain code. You need to install the chain code on the Peer node first, and then instantiate the chain code on the channel
Installing chaincode on peer0.org1... + peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/ ===================== Chaincode is installed on peer0.org1 ===================== Install chaincode on peer0.org2... + peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/ ===================== Chaincode is installed on peer0.org2 =====================
(5) Instantiate the chain code on the Peer node (peer0.org2)
Instantiating chain code only needs to be executed once. This command is complex, - c: specify initialization parameters- P: The endorsement policy is specified. Each transaction of chain code needs to be endorsed and signed by Org1MSP and Org2MSP before it can continue to enter the subsequent transaction process through the endorsement policy. After successful execution, the chain code container mycc will be started.
Instantiating chaincode on peer0.org2... + peer chaincode instantiate -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C jschannel -n mycc -l golang -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P 'AND ('\''Org1MSP.peer'\'','\''Org2MSP.peer'\'')' ========= Chaincode is instantiated on peer0.org2 on channel 'jschannel' =========
(6) Execute the chain code query a on the Peer node (peer0.org1)
Querying chaincode on peer0.org1... ========== Querying on peer0.org1 on channel 'jschannel'... ========== + peer chaincode query -C jschannel -n mycc -c '{"Args":["query","a"]}' 100 ========== Query successful on peer0.org1 on channel 'jschannel' ==========
(7) Call the chain code at the Peer node (peer0.org1) and transfer from a to 10 to b
Sending invoke transaction on peer0.org1 peer0.org2... + peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C jschannel -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}' ==== Invoke transaction successful on peer0.org1 peer0.org2 on channel 'jschannel' ====
(8) Install the chain code on the Peer node (peer1.org2)
Installing chaincode on peer1.org2... + peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/ ===================== Chaincode is installed on peer1.org2 =====================
(9) Execute the chain code query on the Peer node (peer1.org2) to query the value of a
Querying chaincode on peer1.org2... ========== Querying on peer1.org2 on channel 'jschannel'... ========== + peer chaincode query -C jschannel -n mycc -c '{"Args":["query","a"]}' 90 ========= Query successful on peer1.org2 on channel 'jschannel' ========= ========= All GOOD, BYFN execution completed ===========
3.4 shut down the network
3.4.1 execute command
After the test is completed, use the following command to turn off the network
$ sudo ./byfn.sh -m down
- summary
Through decomposition, this paper gradually introduces the basic process of building super ledger Fabric network and the method of chain code deployment and call. After understanding and mastering the most basic steps, it also lays a foundation for further learning the principle of super ledger and developing more complex chain code applications.