Environmental inventory
Basic environment
- Operating system: Ubuntu 21.04 amd64
- Golang : Version 1.16.5
- Docker : Version 20.10.7
- Docker Compose : Version 1.29.2
- Hyperledger Fabric : Github - hyperledger/fabric @v2.4.0
For basic environment installation, refer to the previous article: Hyperledger learning (I): basic environment installation and one click deployment script
New content
- Hyperledger Fabric-Samples : Github - hyperledger/fabric @v2.3.0
Sample download
1. Download Image
The official image download script is available in the directory GitHub COM / hyperledger / fabric / scripts /, execute the following commands in the script directory
- Download Image
cd $GOPATH/src/github.com/hyperledger/fabric/scripts ./bootstrap.sh
- View mirror list
The download process takes some time. After that, the image list should be as follows:fabric@ubuntu:~/code/go/src/github.com/hyperledger/fabric/scripts$ docker images ===> List out hyperledger docker images hyperledger/fabric-tools 2.3 a206a1593b4c 2 months ago 448MB hyperledger/fabric-tools 2.3.2 a206a1593b4c 2 months ago 448MB hyperledger/fabric-tools latest a206a1593b4c 2 months ago 448MB hyperledger/fabric-peer 2.3 85c825d4769f 2 months ago 54.2MB hyperledger/fabric-peer 2.3.2 85c825d4769f 2 months ago 54.2MB hyperledger/fabric-peer latest 85c825d4769f 2 months ago 54.2MB hyperledger/fabric-orderer 2.3 7cad713cbfea 2 months ago 37.8MB hyperledger/fabric-orderer 2.3.2 7cad713cbfea 2 months ago 37.8MB hyperledger/fabric-orderer latest 7cad713cbfea 2 months ago 37.8MB hyperledger/fabric-ccenv 2.3 627c556b15ca 2 months ago 514MB hyperledger/fabric-ccenv 2.3.2 627c556b15ca 2 months ago 514MB hyperledger/fabric-ccenv latest 627c556b15ca 2 months ago 514MB hyperledger/fabric-baseos 2.3 e50ea411d694 2 months ago 6.86MB hyperledger/fabric-baseos 2.3.2 e50ea411d694 2 months ago 6.86MB hyperledger/fabric-baseos latest e50ea411d694 2 months ago 6.86MB hyperledger/fabric-ca 1.5 24a7c19a9fd8 3 months ago 70.8MB hyperledger/fabric-ca 1.5.0 24a7c19a9fd8 3 months ago 70.8MB hyperledger/fabric-ca latest 24a7c19a9fd8 3 months ago 70.8MB fabric@ubuntu:~/code/go/src/github.com/hyperledger/fabric/scripts$
2. Download the official sample fabric samples
It is officially recommended to install to $gopath / SRC / GitHub com/<your_ github_ Userid > directory,
But I have both github and gitee warehouses. My download path here is $gopath / SRC / w0x21 / hyperledger study
You can use git clone or go get to copy to the specified directory
Method 1: git clone
mkdir -p $GOPATH/src/w0x21/HyperledgerStudy cd $GOPATH/src/w0x21/HyperledgerStudy git clone https://github.com/hyperledger/fabric-samples
Method 2: go get
mkdir -p $GOPATH/src/w0x21/HyperledgerStudy go get github.com/hyperledger/fabric-samples cp -rf $GOPATH/src/github.com/hyperledger/fabric-samples $GOPATH/src/w0x21/HyperledgerStudy/
Running example
This article will not explain the detailed working principle of Fabric, but before we start, we still need to understand some concepts involved in the following examples to better understand what happened and learn from simple to deep.
stay Introduction to official documents - privacy and confidentiality , this paragraph states:
Hyperledger Fabric, being a permissioned platform, enables confidentiality through its channel architecture and private data feature. In channels, participants on a Fabric network establish a sub-network where every member has visibility to a particular set of transactions. Thus, only those nodes that participate in a channel have access to the smart contract (chaincode) and data transacted, preserving the privacy and confidentiality of both. Private data allows collections between members on a channel, allowing much of the same protection as channels without the maintenance overhead of creating and maintaining a separate channel.
Hyperledger Fabric is a licensed platform that achieves confidentiality through its channel architecture and private data capabilities. In the channel, participants in the Fabric network establish a sub network, in which each member can see a specific set of transactions. Therefore, only the nodes participating in the channel can access the data of the smart contract and transaction, so as to protect their privacy and confidentiality. Private data allows collection between members on a channel, providing the same protection as a channel without the maintenance overhead of creating and maintaining a separate channel.
There are two concepts:
- Smart contract (Chaincode):
Chaincode is software, running on a ledger, to encode assets and the transaction instructions (business logic) for modifying the assets.
Smart contract (chain code - Chaincode) is a software running on the ledger. It can encode assets, and the transaction instructions (or business logic) can also be used to modify assets.
- Channel:
A channel is a private blockchain overlay on a Fabric network, allowing for data isolation and confidentiality. A channel-specific ledger is shared across the peers in the channel, and transacting parties must be properly authenticated to a channel in order to interact with it. Channels are defined by a Configuration-Block.
Channel is a private blockchain built on Fabric network, which realizes data isolation and confidentiality. The channel specific ledger is shared with all peer nodes in the channel, and the transaction party must pass the correct verification of the channel to interact with the ledger. Channels are defined by a "configuration block".
Because the Fabric project involves a large number of conceptual terms, if you haven't checked the official project documents, I will mark the terms in bold brackets where relevant terms are quoted as much as possible. If necessary, I will introduce the meaning of words in the text. Of course, there must be some negligence. It is recommended to refer to the vocabulary interpretation in the official documents
- Hyperledger fabric Chinese online document (Glossary)
- Hyperledger fabric official online documentation (Glossary)
1. Example introduction
-
1.1. Docker nodes running in the example:
name Docker name Role NOUN explain Sorting service orderer.example.com Ordering Service (sorting service or consensus service) A collection of nodes that sort transactions into blocks. The ordering service is independent of the peer process and sorts transactions for all channels on the network on a first come first served basis. The ordering service supports pluggable implementation. At present, SOLO and Kafka are implemented by default. The ordering service is a public binding of the whole network, which contains encryption materials related to each member. Peer node for organization 1 peer0.org1.example.com Peer (node) The network entity of organization 1 maintains the ledger and runs the chain code container to perform read-write operations on the ledger. Nodes (peers) are owned and maintained by members. Peer node for organization 2 peer0.org2.example.com Peer (node) The network entity of organization 2 maintains the ledger and runs the chain code container to perform read-write operations on the ledger. Nodes (peers) are owned and maintained by members. -
1.2 introduction to example chain code:
The selected run example is asset transfer basic
This example is a basic example smart contract (smart contract, i.e. chain code - chain code), which allows you to create and transfer assets by putting data into the ledger and retrieving it.The above bold font is the official introduction
But! But! But!
The official document does not explain what the asset example of the chain code represents! (or maybe I didn't find it!)
For ease of understanding, I assume that classified assets represent colored buckets, and size represents bucket capacityThe list of assets included after the initialization of chain code operation is as follows:
name colour size Owner value asset1 blue 5 Tomoko 300 asset2 red 5 Brad 400 asset3 green 10 Jin Soo 500 asset4 yellow 10 Max 600 asset5 black 15 Adriana 700 asset6 white 15 Michel 800
2. Run test network (Docker operation)
The official example warehouse fabric samples has very perfect operation scripts. We can directly enter the corresponding directory to perform operations
- 1. Delete all containers and components from previous runs
cd $GOPATH/src/w0x21/HyperledgerStudy/fabric-samples/test-network ./network.sh down
- 2. Create Fabric network
./network.sh up
- 3. View running nodes
fabric@ubuntu:~/code/go/src/w0x21/HyperledgerStudy/fabric-samples/test-network$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6e8f97041f76 hyperledger/fabric-tools:latest "/bin/bash" 8 minutes ago Up 8 minutes cli 48b5469b1de8 hyperledger/fabric-peer:latest "peer node start" 8 minutes ago Up 8 minutes 0.0.0.0:7051->7051/tcp, :::7051->7051/tcp, 0.0.0.0:17051->17051/tcp, :::17051->17051/tcp peer0.org1.example.com 17a4597187b3 hyperledger/fabric-orderer:latest "orderer" 8 minutes ago Up 8 minutes 0.0.0.0:7050->7050/tcp, :::7050->7050/tcp, 0.0.0.0:7053->7053/tcp, :::7053->7053/tcp, 0.0.0.0:17050->17050/tcp, :::17050->17050/tcp orderer.example.com f1671d011063 hyperledger/fabric-peer:latest "peer node start" 8 minutes ago Up 8 minutes 0.0.0.0:9051->9051/tcp, :::9051->9051/tcp, 7051/tcp, 0.0.0.0:19051->19051/tcp, :::19051->19051/tcp peer0.org2.example.com fabric@ubuntu:~/code/go/src/w0x21/HyperledgerStudy/fabric-samples/test-network$
If the running node is the same as mine, it indicates that the Fabric network is running successfully, and then you can run the smart contract (chain code - Chaincode)
3. Run smart contract (chain code - Chaincode)
- 1. Initialize environment variables
cd ~/code/go/src/w0x21/HyperledgerStudy/fabric-samples/test-network export PATH=${PWD}/../bin:$PATH export FABRIC_CFG_PATH=$PWD/../config/
- 2. Create channel
./network.sh createChannel -c mychannel
- 3. Start chain code (deploy chain code)
./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
4. Call smart contract
All the following operations are run in the directory ~ / code / go / SRC / w0x21 / hyperledger study / fabric samples / test network
- 1. Initialize member identity to organization 1
export CORE_PEER_TLS_ENABLED=true export CORE_PEER_LOCALMSPID="Org1MSP" export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp export CORE_PEER_ADDRESS=localhost:7051
- 2. Initialize ledger
peer chaincode invoke -o localhost:7050 \ --ordererTLSHostnameOverride orderer.example.com \ --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" \ -C mychannel \ -n basic \ --peerAddresses localhost:7051 \ --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" \ --peerAddresses localhost:9051 \ --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" \ -c '{"function":"InitLedger","Args":[]}'
- 3. Query the list of assets added to the channel ledger
If successful, the results should be as follows:peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
[ {"ID": "asset1", "color": "blue", "size": 5, "owner": "Tomoko", "appraisedValue": 300}, {"ID": "asset2", "color": "red", "size": 5, "owner": "Brad", "appraisedValue": 400}, {"ID": "asset3", "color": "green", "size": 10, "owner": "Jin Soo", "appraisedValue": 500}, {"ID": "asset4", "color": "yellow", "size": 10, "owner": "Max", "appraisedValue": 600}, {"ID": "asset5", "color": "black", "size": 15, "owner": "Adriana", "appraisedValue": 700}, {"ID": "asset6", "color": "white", "size": 15, "owner": "Michel", "appraisedValue": 800} ]
- 4. Add asset (increase)
peer chaincode invoke -o localhost:7050 \ --ordererTLSHostnameOverride orderer.example.com \ --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" \ -C mychannel \ -n basic \ --peerAddresses localhost:7051 \ --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" \ --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" \ -c '{"function":"CreateAsset","Args":["asset7","Blue","6","w0x0021","1021"]}'
- 5. Delete asset (delete)
peer chaincode invoke -o localhost:7050 \ --ordererTLSHostnameOverride orderer.example.com \ --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" \ -C mychannel \ -n basic \ --peerAddresses localhost:7051 \ --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" \ --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" \ -c '{"function":"DeleteAsset","Args":["asset6"]}'
- 6. Change assets (change)
peer chaincode invoke -o localhost:7050 \ --ordererTLSHostnameOverride orderer.example.com \ --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" \ -C mychannel \ -n basic \ --peerAddresses localhost:7051 \ --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" \ --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" \ -c '{"function":"UpdateAsset","Args":["asset7","Blue","6","w0x0021","1033"]}'
- 7. Transferred assets (modified)
peer chaincode invoke -o localhost:7050 \ --ordererTLSHostnameOverride orderer.example.com \ --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" \ -C mychannel \ -n basic \ --peerAddresses localhost:7051 \ --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" \ --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" \ -c '{"function":"TransferAsset","Args":["asset6","w0x0021"]}'
- 8. Query assets (query)
peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'
5. Switch identity
The operation demonstration in the previous section is to use the identity of organization 1 (Org1) to verify the addition, deletion, modification and query function of the chain code, and switch the identity to the authentication function of organization 2 (Org2). You only need to change the environment variables in 4.1. In bash, execute the following code:
export PATH=${PWD}/../bin:$PATH export CORE_PEER_TLS_ENABLED=true export CORE_PEER_LOCALMSPID="Org2MSP" export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp export CORE_PEER_ADDRESS=localhost:9051
The specific operations are not repeated here. You can try to open two bash windows and initialize them to different identities. One window performs asset change and the other window performs asset query to test the synchronization effect of blockchain.
summary
This article simply expands the official examples and does not deeply study the operation of chain code. However, if the whole process is operated, it should not be difficult to find that the Fabric framework is actually a way of RPC remote call in the process of calling chain code, and authentication is included in the framework. However:
- How is the specific block storage stored?
- How to generate and verify certificates in authentication?
- What does the certificate contain?
- What preparations should be made to create the initial block?
- ......?
Can be expanded through this simple example, which has brought me too many problems. So far, Fabric really contains too many knowledge points. The improvement brought by in-depth study of this framework will be very great. Next, continue to study chain code call from shallow to deep.