Getting started reference document:
https://hyperledger-fabric.readthedocs.io/zh_CN/release-2.2/test_network.html
If you want to learn a technology, the best place to start is the official documentation.
Download hyperledger official sample
# 只下载项目
git clone https://github.com/hyperledger/fabric-samples.git
# 全面下载
curl -sSL https://bit.ly/2ysbOFE | bash -s -- 2.2.2 1.4.9
dockerconfirmation
If it is not installed on your computer, install docker first
sudo apt install docker.io docker-compose
#或
sudo snap install docker docker-compose
switch to root environment
Note here, explain that on Linux Ubuntu, all subsequent operations must be performed under root.
So su before starting:
sudo su
Start deploying the first sample
Enter fabric-samples/test-network
, execute:
Start fabric service on docker
./network.sh up
Create a channel
./network.sh createChannel
Start a set of smart contracts (chain codes). Smart contracts on hyperledger are called chain codes. It is very strange. I still like the name of smart contracts under eth.
Use the JS or TS version of the chaincode
#ts
./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-typescript -ccl typescript
#js
./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-javascript -ccl javascript
Test the contract of this sample
Because here we need to use some binary tools such as peer to interact with the chain code, so we add the bin system environment variable.
Do as follows:
# 在root环境下操作
vi ~/.bashrc
# 在root的bashrc中增加:
export PATH=~/fabric/bin:$PATH
export FABRIC_CFG_PATH=~/fabric/config/
In addition, some environment variables need to be exported, which are related to the sample. We don’t write it into bashrc, just test-network
execute it directly:
# Environment variables for Org1
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/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:7051
For convenience, you test-network
can create a new envrc1
file below, then copy the above ones export
into it, and then enter test-network
the directory and only need to execute it source ./envrc1
to import these environment variables.
So why is it 1? Because there are 2 more. In the above steps, you should have discovered that network.sh
two docker peer nodes are actually created, that is, org1 and org2, so we have two sets of environment variables for these two orgs, pointing to these two organizations.
Specifically, the two envrc files contain encrypted files related to the organization for authentication.
The following isenvrc2
# Environment variables for Org2
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/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:9051
Start the test of reading and writing contracts
First use org1 to test:
source ./envrc1
First, let's initialize the ledger and write some data :
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":[]}'
Then let's read it :
peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
We will see a bunch of json data, which is the state of the ledger just after the initialization operation, that is, the assets of org1.
Next we do some transfer operations :
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","Christopher"]}'
Ok, let's look at the district ledger after the transfer, we use org2 to query, sosource ./envrc2
implement:
peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'
result:
{
"ID":"asset6","Color":"white","Size":15,"Owner":"Christopher","AppraisedValue":800,"docType":"asset"}
OK, it’s perfect, the asset belongs to Christopher, please check the sample project for transfer detailsasset-transfer-basic/chaincode-typescript
close service
After the test is over, if you need to close the sample, execute:
./network.sh down