本身的碩士研究方向和區塊鏈有關,工程上一直以IBM的Hyperledger Fabric爲基礎進行開發,對該項目關注也有兩年了。目前迎來了Hyperledger Fabric v1.4,這也是Fabric的第一個長期支持版本,所以也比較有表明性,故在此和你們分享一下本身的環境搭建過程。node
附上v1.4的官方文檔:https://hyperledger-fabric.readthedocs.io/en/release-1.4/。linux
環境說明:git
本人測試環境爲騰訊雲學生機(1Core/RAM 2G/ROM 50G),CentOS 7.5 64位。github
下載安裝包golang
mkdir download cd download
# 從國內站點下載合適的安裝包
wget https://studygolang.com/dl/golang/go1.12.4.linux-amd64.tar.gz
解壓 docker
tar -C /usr/local/ -xzvf go1.12.4.linux-amd64.tar.gz #解壓到/usr/local/go目錄下
配置npm
vim /etc/profile # 配置系統環境變量 # 在最下方插入 # Golang 環境變量 export GOROOT=/usr/local/go export GOPATH=/root/go export GOBIN=$GOPATH/bin export PATH=$PATH:$GOROOT/bin export PATH=$PATH:$GOPATH/bin #使配置的環境變量生效 source /etc/profile #檢查是否配置正確
go version
說明:Go的環境並不是運行的必須條件,但後期調試智能合約可能會用到,並且官網安裝說明也提到了須要安裝Go。bootstrap
經過nvm安裝Node.js和npm,該步驟也不是運行的必須條件,但後期若是須要node-sdk來開發應用程序的話就須要node環境。(npm源切換可參考npm 淘寶源切換教程)vim
安裝nvm(官方文檔:https://github.com/nvm-sh/nvm)centos
# 安裝後重啓該會話或從新開一個會話便可生效 curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
經過nvm安裝Node.js和npm
# 查看當前支持的版本,fabricv1.4要求只支持8.x nvm ls-remote # 安裝當前8.x LTS的最新版本(會同時安裝npm) nvm install 8.16.0 # 檢查node.js安裝版本 node -v # 檢查npm的安裝版本 npm -v
Docker安裝社區版的就能夠,參考官方文檔:https://docs.docker.com/install/linux/docker-ce/centos/
參考官方文檔的第一種經過倉庫安裝的方式(傻瓜操做的話第三種直接腳本安裝更簡單)。
p.s. 因爲docker-hub鏡像倉庫在國外,可能會被牆或者很慢,建議你們參考daocloud加速器配置一下倉庫,或者用雲服務器的能夠參考各家的docker倉庫源(阿里Docker鏡像庫、騰訊Docker鏡像庫等)。
卸載舊版本
sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine
安裝docker-ce的必需條件
sudo yum install -y yum-utils \ device-mapper-persistent-data \ lvm2
設置穩定版的倉庫
sudo yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo
安裝docker-ce
yum install docker-ce docker-ce-cli containerd.io
啓動docker
sudo systemctl start docker
測試docker是否安裝成功
sudo docker run hello-world
Fabric中節點容器編排使用的使docker-compose,故須要安裝docker-compose。官方文檔:https://docs.docker.com/compose/install/
下載docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
爲docker-compose配置執行權限
sudo chmod +x /usr/local/bin/docker-compose
檢查是否安裝成功
docker-compose -v
經過go get 的方式獲取源碼,配合git切換分支
獲取fabric源碼
# 獲取fabric源碼 go get -u github.com/hyperledger/fabric # 進入目錄 切換分支 cd go/src/github.com/hyperledger/fabric git checkout v1.4.1
獲取fabric-sample源碼
go get github.com/hyperledger/fabric-samples # 進入目錄,切換分支 cd go/src/github.com/hyperledger/fabric-samples/ git checkout v1.4.1
咱們經過fabric-sample測試
經過腳本獲取docker鏡像和可執行文件(建議事先配置好docker倉庫)
cd go/src/github.com/hyperledger/fabric-samples/scripts/ #此處可能由於網絡問題執行時間比較久 bash bootstrap.sh
經過fabric-sample中first-network示例測試,執行了一個A和B轉帳,中間屢次查詢餘額的一個過程。
docker運行須要權限,最好用root身份運行。
找到first-network示例
cd go/src/github.com/hyperledger/fabric-samples/first-network
生成密鑰文件
[root@VM_0_39_centos first-network]# bash byfn.sh generate
Generating certs and genesis block for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] y
proceeding ...
/root/go/src/github.com/hyperledger/fabric-samples/bin/cryptogen
##########################################################
##### Generate certificates using cryptogen tool #########
##########################################################
+ cryptogen generate --config=./crypto-config.yaml
org1.example.com
org2.example.com
+ res=0
+ set +x
/root/go/src/github.com/hyperledger/fabric-samples/bin/configtxgen
##########################################################
######### Generating Orderer Genesis block ##############
##########################################################
CONSENSUS_TYPE=solo
+ '[' solo == solo ']'
+ configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block
2019-05-03 21:22:43.122 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-05-03 21:22:43.232 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 002 orderer type: solo
2019-05-03 21:22:43.232 CST [common.tools.configtxgen.localconfig] Load -> INFO 003 Loaded configuration: /root/go/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml
2019-05-03 21:22:43.338 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 004 orderer type: solo
2019-05-03 21:22:43.338 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 005 Loaded configuration: /root/go/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml
2019-05-03 21:22:43.347 CST [common.tools.configtxgen] doOutputBlock -> INFO 006 Generating genesis block
2019-05-03 21:22:43.347 CST [common.tools.configtxgen] doOutputBlock -> INFO 007 Writing genesis block
+ res=0
+ set +x
#################################################################
### Generating channel configuration transaction 'channel.tx' ###
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel
2019-05-03 21:22:43.395 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-05-03 21:22:43.515 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /root/go/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml
2019-05-03 21:22:43.622 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-05-03 21:22:43.622 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /root/go/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml
2019-05-03 21:22:43.622 CST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 005 Generating new channel configtx
2019-05-03 21:22:43.657 CST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 006 Writing new channel tx
+ res=0
+ set +x
#################################################################
####### Generating anchor peer update for Org1MSP ##########
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP
2019-05-03 21:22:43.714 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-05-03 21:22:43.795 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /root/go/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml
2019-05-03 21:22:43.876 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-05-03 21:22:43.876 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /root/go/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml
2019-05-03 21:22:43.876 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2019-05-03 21:22:43.877 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 006 Writing anchor peer update
+ res=0
+ set +x
#################################################################
####### Generating anchor peer update for Org2MSP ##########
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
2019-05-03 21:22:43.913 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-05-03 21:22:43.995 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /root/go/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml
2019-05-03 21:22:44.073 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-05-03 21:22:44.073 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /root/go/src/github.com/hyperledger/fabric-samples/first-network/configtx.yaml
2019-05-03 21:22:44.073 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2019-05-03 21:22:44.074 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 006 Writing anchor peer update
+ res=0
+ set +x
啓動示例
[root@VM_0_39_centos first-network]# bash byfn.sh up Starting for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds Continue? [Y/n] y proceeding ... LOCAL_VERSION=1.4.1 DOCKER_IMAGE_VERSION=1.4.1 Creating network "net_byfn" with the default driver Creating volume "net_orderer.example.com" with default driver Creating volume "net_peer0.org1.example.com" with default driver Creating volume "net_peer1.org1.example.com" with default driver Creating volume "net_peer0.org2.example.com" with default driver Creating volume "net_peer1.org2.example.com" with default driver Creating orderer.example.com ... done Creating peer1.org2.example.com ... done Creating peer1.org1.example.com ... done Creating peer0.org2.example.com ... done Creating peer0.org1.example.com ... done Creating cli ... done CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2cf554ced6b0 hyperledger/fabric-tools:latest "/bin/bash" 1 second ago Up Less than a second cli fec18ca70d8d hyperledger/fabric-peer:latest "peer node start" 4 seconds ago Up 1 second 0.0.0.0:7051->7051/tcp peer0.org1.example.com 95427a7d76ce hyperledger/fabric-peer:latest "peer node start" 4 seconds ago Up 1 second 0.0.0.0:10051->10051/tcp peer1.org2.example.com 8c7084b42ea7 hyperledger/fabric-orderer:latest "orderer" 4 seconds ago Up 1 second 0.0.0.0:7050->7050/tcp orderer.example.com 3456e8793e46 hyperledger/fabric-peer:latest "peer node start" 4 seconds ago Up 1 second 0.0.0.0:8051->8051/tcp peer1.org1.example.com aa167f7fcc27 hyperledger/fabric-peer:latest "peer node start" 4 seconds ago Up 1 second 0.0.0.0:9051->9051/tcp peer0.org2.example.com 95271becf126 hello-world "/hello" 3 days ago Exited (0) 3 days ago pensive_curie ____ _____ _ ____ _____ / ___| |_ _| / \ | _ \ |_ _| \___ \ | | / _ \ | |_) | | | ___) | | | / ___ \ | _ < | | |____/ |_| /_/ \_\ |_| \_\ |_| Build your first network (BYFN) end-to-end test Channel name : mychannel Creating channel... + peer channel create -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/channel.tx --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 # 此處省略日誌輸出 Installing chaincode on peer1.org2... + peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/ + res=0 + set +x 2019-05-03 13:25:47.540 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 2019-05-03 13:25:47.540 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc 2019-05-03 13:25:47.751 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" > ===================== Chaincode is installed on peer1.org2 ===================== Querying chaincode on peer1.org2... ===================== Querying on peer1.org2 on channel 'mychannel'... ===================== Attempting to Query peer1.org2 ...3 secs + peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}' + res=0 + set +x 90 ===================== Query successful on peer1.org2 on channel 'mychannel' ===================== ========= All GOOD, BYFN execution completed =========== _____ _ _ ____ | ____| | \ | | | _ \ | _| | \| | | | | | | |___ | |\ | | |_| | |_____| |_| \_| |____/
清除示例
執行刪除密鑰文件,中止並刪除容器等操做
[root@VM_0_39_centos first-network]# bash byfn.sh down Stopping for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds Continue? [Y/n] y proceeding ... Stopping cli ... done Stopping peer0.org1.example.com ... done Stopping peer1.org2.example.com ... done Stopping orderer.example.com ... done Stopping peer1.org1.example.com ... done Stopping peer0.org2.example.com ... done Removing cli ... done Removing peer0.org1.example.com ... done Removing peer1.org2.example.com ... done Removing orderer.example.com ... done Removing peer1.org1.example.com ... done Removing peer0.org2.example.com ... done Removing network net_byfn Removing volume net_orderer.example.com Removing volume net_peer0.org1.example.com Removing volume net_peer1.org1.example.com Removing volume net_peer0.org2.example.com Removing volume net_peer1.org2.example.com Removing volume net_orderer2.example.com WARNING: Volume net_orderer2.example.com not found. Removing volume net_orderer3.example.com WARNING: Volume net_orderer3.example.com not found. Removing volume net_orderer4.example.com WARNING: Volume net_orderer4.example.com not found. Removing volume net_orderer5.example.com WARNING: Volume net_orderer5.example.com not found. Removing volume net_peer0.org3.example.com WARNING: Volume net_peer0.org3.example.com not found. Removing volume net_peer1.org3.example.com WARNING: Volume net_peer1.org3.example.com not found. d58e2eba8e9d aa7c6c5dd7f7 15ef69f6b748 Untagged: dev-peer1.org2.example.com-mycc-1.0-26c2ef32838554aac4f7ad6f100aca865e87959c9a126e86d764c8d01f8346ab:latest Deleted: sha256:044ed90bbd20fbc015f1c75f578d17d327b53c599bfa603cf59f8f2b6a3a2749 Deleted: sha256:4c90c84d87522dde761b147e98d803d61e8dd113dce2d612bac7683b1e95a0f0 Deleted: sha256:5916889234775a9d49d5e5f25c5f3e226076e453c7188e3121271c98709c89bc Deleted: sha256:7729eea67cc67a9d310f5b53bcb40e83b8496cd954d08230efabaa229cc30c41 Untagged: dev-peer0.org1.example.com-mycc-1.0-384f11f484b9302df90b453200cfb25174305fce8f53f4e94d45ee3b6cab0ce9:latest Deleted: sha256:f214e2732061d2e21dec13fa499296da861eb7da6a29631ee76c139218083197 Deleted: sha256:a4f9f0680f5b42484c8ae4675aec4d3aaf1c591c27bd375813f71ccbb2afca92 Deleted: sha256:d786c76106b411dd66b2d57904a215ca1dfce30ae2adca1127dc95b588655f15 Deleted: sha256:ba3660f49899db646d6b2a2703c4b73cf7a647b1d3a170fb8909a66fccc48c1d Untagged: dev-peer0.org2.example.com-mycc-1.0-15b571b3ce849066b7ec74497da3b27e54e0df1345daff3951b94245ce09c42b:latest Deleted: sha256:870943518d7142d03509e589e4ea57837c4b092d60dd3097d9f8d113078498a0 Deleted: sha256:5dc11c2c52f40e2883a450528fdd008b4a372aaebbd9c2aec548eb3b8a0b610a Deleted: sha256:528ba6c50b298c20b9a408fda4284372d0d0c031bbc595b8a309d3513497c355 Deleted: sha256:e85509bd000d5685db47a5031d4e929b08cebdff050f5dd35c903dbd1eabc0a4