Use and development of Hyperledger Fabric

Hyperledger Fabric is a cross-industry blockchain technology initiated by the Linux Foundation. It is currently used in many large companies. I will not introduce HF itself here. If you are interested, you can pay attention to its official website.

1. Preparation:

Before starting, certain preparations are required, including installing various middleware:

If the following problems occur when installing docker compose, you can handle them like this

question:

command 'gcc' failed with exit status 1

yum install python-devel

question:

Cannot uninstall 'requests'.

pip install docker-compose --ignore-installed requests

If necessary, you can install two management tools, cello and explorer, both of which are open source products under Hyperledger.

2. Local network deployment (HF2.0+)

This mode is generally used for users to learn the Hyperledger Fabric network and try to run the test network and write code locally. It is not used in the production environment. The production environment will use k8s to deploy related networks and generally only requires contracts or apiserver and other content.

1. Source code copying is best done when the network is open.

2. Start the network (couchdb+CA node)

./byfn.sh up -a -s couchdb

Start couchDB alone

docker-compose -f docker-compose-cli.yaml -f docker-compose-couch.yaml -f docker-compose-etcdraft2.yaml up -d

3. If it involves changing the source code of Hyperledger Fabric, such as changing its encryption algorithm to national secret, it needs to be compiled and packaged. The command is as follows:

(1) Fabric compilation - secondary files and main demo images

make dist-clean all

(2) Fabric-node compilation

cd docker
docker build -t hyperledger/fabric-nodeenv:latest .

(3) Offline deployment, general situation

docker save -o fabric-tools.tar hyperledger/fabric-tools:latest
docker load --input fabric-tools21.rar

If save-load fails, such as couchdb node, use export-import instead.

docker export -o fabric-couchdb.tar couchdb1
docker import fabric-couchdb.tar hyperledger/fabric-couchdb:0.4.18

3. Smart contract installation (HF2.0+)

After writing (or updating) the smart contract code,

(1) Use go build for code compilation

(2) Smart contract installation (requires execution by each node)

peer lifecycle chaincode install record9.tar.gz
# 环境变量设置
export CC_PACKAGE_ID=record9:0305dad14704dbdd48c2650d35f45038452e0aa281541f463c37a0e293a266b4

(3) Query installed contracts

peer lifecycle chaincode queryinstalled

(4) Switch node organization

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org2.example.com:9051
CORE_PEER_LOCALMSPID="Org2MSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt

(5) Recognition of contract definition (requires implementation by each organization)

peer lifecycle chaincode approveformyorg --channelID $CHANNEL_NAME --name {
   
   {test7}} --version 1.0 --init-required --package-id $CC_PACKAGE_ID --sequence 1 --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

(6) Check the organizations that have recognized the contract

peer lifecycle chaincode checkcommitreadiness --channelID $CHANNEL_NAME --name record9 --version 1.0 --init-required --sequence 1 --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 --output json

(7) Submit the contract definition to Channel

peer lifecycle chaincode commit -o orderer.example.com:7050 --channelID $CHANNEL_NAME --name record9 --version 1.0 --sequence 1 --init-required --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 --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:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt

(8) Contract initialization

peer chaincode invoke -o orderer.example.com:7050 --isInit --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 $CHANNEL_NAME -n record9 --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:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["Init","a","100"]}' --waitForEvent

(9) Execute contract query (code provided)

peer chaincode query -C $CHANNEL_NAME -n record9 -c '{"Args":["queryRecord","-2"]}'

(10) Execute contract call (code provided)

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 $CHANNEL_NAME -n record9 --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:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["createRecord","-2","0","test file hash","10000", "2020-2-13 10:11:00"]}' --waitForEvent

3. ApiServer installation and deployment (HF2.0+)

The apiserver deployment is quite diverse and differs according to the languages ​​used, such as go, java, and nodejs. Here we mainly introduce the general steps without showing the actual scripts.

1. Modify the configuration parameters for generating the image file, or fill in the input variables (if a CI platform has been built)

2. Generate the image (for example./buildImages.sh apiserver 1.0)

3. Generate compilation tool (makeYaml)

4. Birthday configuration file, contract identification name/network name/organization identification name/exposed ip+port/state secret-non-state secret (depending on the network), use the compilation tool in the third step

5. Copy client_sdk.yaml/crypto-config to the deploy folder

6. Start the image (docker compose)

7. Reverse proxy settings

Guess you like

Origin blog.csdn.net/aa466564931/article/details/132318125