1. 程式人生 > >HyperLedger(1)啟動Fabric及建立channel、部署chaincode

HyperLedger(1)啟動Fabric及建立channel、部署chaincode

HyperLedger

前提

git clone https://github.com/hyperledger/fabric.git

#編譯configtxgen工具
cd $GOPATH/src/github.com/hyperledger/fabric
make configtxgen # 如果出錯:'ltdl.h' file not found sudo apt install libtool libltdl-dev

#進入examples/e2e_cli目錄,首先從Docker Hub拉取映象:
# 使指令碼可執行
chmod +x download-dockerimages.sh
# 執行指令碼
./download-dockerimages.sh

啟動fabric

sudo docker-compose -f docker-compose-cli.yaml up -d

本文所進入的cli,其環境依賴於peer0.org1;如果想要在該cli中對其它peer操作需要設定相應的引數:

CORE_PEER_ADDRESS

CORE_PEER_MSPCONFIGPATH

CORE_PEER_TLS_ROOTCERT_FILE

CORE_PEER_LOCALMSPID

Create channel

#進入cli容器
sudo docker exec -it cli
bash #針對orderer的channel create peer channel create -o orderer.example.com:7050 -c mychannel -f channel-artifacts/channel.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

Join channel

peer channel join
-b mychannel.block

Update anchor peer

#update anchor peer info of org1
peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

#update anchor peer info of org2
peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

Chaincode

#install chaincode
peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02

#instantiate chaincode
peer chaincode instantiate -o orderer.example.com:7050 --tls --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 mychannel -n mycc -v 1.0 -c '{"Args":["init","a", "100", "b","200"]}' -P "OR ('Org1MSP.member','Org2MSP.member')"

#invoke chaincode
peer chaincode invoke -o orderer.example.com:7050 --tls --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 mychannel -n mycc -c '{"Args":["invoke","a","b","10"]}'

#query chaincode
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'