二、hyperledger fabric fabric-samples環境測試
基礎環境搭建參考
一、下載fabric-samples、二進位制檔案、映象檔案
1、進入gopath目錄下,建立目錄src/github.com/hyperledger,進到該目錄
2、下載官網上面的bootstrap.sh,修改可執行許可權,chmod +x bootstrap.sh
3、執行bootstrap.sh下載fabric-samples、二進位制檔案、docker映象,網路不好的同學耐心等待哈。
二、執行fabric-samples下面的first-network,網路測試
1、cd first-network
2、先關閉網路,確保服務是關閉狀態
./byfn.sh down
3、建立網路
./byfn.sh generate
建立成功後生成兩個組織、四個對等節點
genesis.block為創世區塊的配置檔案
4、啟動網路測試
./byfn.sh up
檢視docker程序
5、關閉網路
./byfn.sh down
三、測試智慧合約呼叫
1、生成初始區塊
../bin/cryptogen generate --config=./crypto-config.yaml
設定FABRIC配置檔案路徑
export FABRIC_CFG_PATH=$PWD
生成創世區塊
../bin/configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block
2、設定區塊鏈名稱,生成應用通道相關資訊
export CHANNEL_NAME=mychannel
../bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME
3、生成錨節點,更新配置檔案
../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP
../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP
4、啟動網路
docker-compose -f docker-compose-cli.yaml up -d
5、進入docker 命令列
docker exec -it cli bash
6、生成一個通道
export CHANNEL_NAME=mychannel
建立通道
peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -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
7、加入通道
peer channel join -b mychannel.block
8、安裝鏈碼
peer chaincode install -n mycc -v 1.0 -p github.com/chaincode/chaincode_example02/go/
9、例項化鏈碼,時間較長,耐心等待
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 $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a", "100", "b","200"]}' -P "AND ('Org1MSP.peer','Org2MSP.peer')"
10、查詢
peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'
11、轉賬呼叫(invoke)
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 mychannel -n mycc -c '{"Args":["invoke","a","b","20"]}'