Mac OS 区块链hyperledger环境搭建、环境架构介绍、环境如何用、部署 Chaincode、智能合约的调用

   日期:2020-10-05     浏览:96    评论:0    
核心提示:1. 环境搭建Install Common Packages# install utilities$ brew install unzip$ brew install git$ brew install curl$ brew install wget$ brew install vim$ brew install tree$ brew install jq# install gradle$ cd /tmp && wget https://services.gra

1. 环境搭建

Install Common Packages


# install utilities
$ brew install unzip
$ brew install git
$ brew install curl
$ brew install wget
$ brew install vim
$ brew install tree
$ brew install jq

# install gradle
$ cd /tmp && wget https://services.gradle.org/distributions/gradle-6.4-bin.zip 
$ unzip gradle-6.4-bin.zip
$ sudo mv gradle-6.4 /usr/local/gradle
$ sudo cat >> ~/.zshrc <<EOF # setup gradle environments # ===================== export PATH=$PATH:/usr/local/gradle/bin # ===================== EOF
$ source ~/.zshrc

下载 Docker Images

# image of ca
$ docker pull hyperledger/fabric-ca:1.4.6
# image of peer
$ docker pull hyperledger/fabric-peer:2.1.0
# image of orderer
$ docker pull hyperledger/fabric-orderer:2.1.0
# image of tools & utilities
$ docker pull hyperledger/fabric-tools:2.1.0
# image of Chaincode deployment for Programming Languages (Go | Java | Node.JS) $ docker pull hyperledger/fabric-ccenv:2.1.0
$ docker pull hyperledger/fabric-javaenv:2.1.0
$ docker pull hyperledger/fabric-nodeenv:2.1.0
# image of Base-OS of Chaincode runtime
$ docker pull hyperledger/fabric-baseos:0.4.20
# image of coucddb (one NOSQL DB for ledger state)
$ docker pull hyperledger/fabric-couchdb:0.4.20
# check image list to validate downloading
$ docker images

安装完毕以后的镜像列表

Install docker-compose

前置操作,安装 wget

% brew install wget

如果出现python3的错误,请先remove掉python3

brew uninstall python3

在安装一遍

brew install python

根据命令的提示,建立连接

brew link --overwrite python@3.8

开始安装 docker-compose

# download
$ wget https://github.com/docker/compose/releases/download/1.25.3/docker-compose-`uname -s`-`uname -m`

# copy to ` /usr/local/bin/ ` and rename
$ sudo mv docker-compose-`uname -s`-`uname -m` /usr/local/bin/docker-compose

# make executable
$ sudo chmod +x /usr/local/bin/docker-compose

# validate installation
$ docker-compose -v
# docker-compose version 1.25.4, build unknown

Install Go language SDK

笔者用的是zsh所以,配置文件路径在 ~/.zshrc, 如果用的是bash,配置文件路径在~/.bashrc。 在下面的命令中修改配置文件路径即可.

golang下载路径: https://golang.org/dl/
如果Terminal下载golang失败,可以浏览器下载成功以后,copy到/tmp 下。

# install Go
$ cd /tmp && wget https://golang.org/dl/go1.13.14.darwin-amd64.tar.gz 
$ sudo tar -C /usr/local -xzf go1.13.14.darwin-amd64.tar.gz
$ sudo cat >> ~/.zshrc <<EOF # setup go environments # ===================== export GO_HOME="/usr/local/go" export PATH=$GO_HOME/bin:$PATH export GOPATH=$HOME/gopath export GO111MODULE=on export GOPROXY=https://goproxy.cn # ===================== EOF
$ source ~/.zshrc 
$ go version
# go version go1.13.14 darwin/amd64

install Java SDK

# install Java
$ sudo apt-get update
$ sudo apt-get install -y openjdk-8-jdk 
$ java -version
# 笔者之前已经安装好JDK,信息如下,上面的命令应该是正常的
# java version "1.8.0_172"
# Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
# Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)

Install Node.JS via NVM

用 homebrew 安装 nvm

$ brew install nvm

编辑配置文件

 $ vim ~/.zshrc     
 # 如果用的bash请用 vim ~/.bashrc
 $ export NVM_DIR=~/.nvm
# 保存 esc > :wq

重新加载配置文件

$ source $(brew --prefix nvm)/nvm.sh
$ nvm --version
# 0.36.0
# install Node.JS version 10
$ nvm install 10

# check the version of Node.JS and NPM
$ node -v 
# v10.22.1
$ npm -v
# 6.14.6

#Tips: NVM Vs. NPM

  • NVM: Node Version Manager
  • NPM: Node Package Manager

Download workspace

# download workspace from gitlab.com
$ git clone https://gitlab.com/qubing/blockchain_lab_v2.git ~/Workspace/blockchain

Download Fabric Binaries

$ vim ~/.zshrc
# set block chain environment variables begin
export BLOCK_CHAIN_BASE_PATH=https://github.com/hyperledger
export BLOCK_CHAIN_FABRIC_VERSION=2.1.0
export BLOCK_CHAIN_CA_VERSION=1.4.6
export BLOCK_CHAIN_ARCH=$(echo "$(uname -s|tr '[:upper:]' '[:lower:]'|sed 's/mingw64_nt.*/windows/')-$(uname -m | sed 's/x86_64/amd64/g')") 
export BLOCK_CHAIN_FILE_NAME=hyperledger-fabric-${BLOCK_CHAIN_ARCH}-${BLOCK_CHAIN_FABRIC_VERSION}.tar.gz
export BLOCK_CHAIN_CA_FILE_NAME=hyperledger-fabric-ca-${BLOCK_CHAIN_ARCH}-${BLOCK_CHAIN_CA_VERSION}.tar.gz 
# set block chain environment variables end

# esc > :wq
$ source ~/.zshrc

$ cd /tmp

# download fabric binaries and fabric-ca binaries
# 下载太慢,手动下载Tips: download link on pan.baidu.com
# 链接:https://pan.baidu.com/s/1PSFgdKt6ijcu4UFDyAa9UA 
# 提取码:8p2z
$ mkdir -p ~/Workspace/blockchain/fabric-bin/${BLOCK_CHAIN_FABRIC_VERSION} && cd ~/Workspace/blockchain/fabric-bin
$ wget $BLOCK_CHAIN_BASE_PATH/fabric/releases/download/v$BLOCK_CHAIN_FABRIC_VERSION/$BLOCK_CHAIN_FILE_NAME
$ wget $BLOCK_CHAIN_BASE_PATH/fabric-ca/releases/download/v$BLOCK_CHAIN_CA_VERSION/$BLOCK_CHAIN_CA_FILE_NAME

# copy binaries
$ tar zxf ./$BLOCK_CHAIN_FILE_NAME
$ tar zxf ./$BLOCK_CHAIN_CA_FILE_NAME
$ chmod +x bin/* && sudo cp bin/* /usr/local/bin/

2. 环境搭建起来后,环境架构介绍




Initialize & Startup Network

# STEPS for Startup Environment
# 1. startup CA servers
# 2. register accounts for each organizations including (admin, users, peers) # 3. generate genesis blocks and channel setup transaction files
# 4. startup Orderer and Peers with DB
# 5. setup channel
# 6. generate connection profile for each peer
$ cd ~/Workspace/blockchain
$ sudo -s . ./init.sh

查看正在运行的container

# You can run docker command `docker ps -a` to check containers generated
$  docker ps
CONTAINER ID        IMAGE                               COMMAND                  CREATED             STATUS              PORTS                                        NAMES
2e0550b37998        hyperledger/fabric-peer:2.1.0       "peer node start"        2 minutes ago       Up 2 minutes        7051/tcp, 0.0.0.0:9051->9051/tcp             peer0.org2.example.com
8ab4aaffa713        hyperledger/fabric-peer:2.1.0       "peer node start"        2 minutes ago       Up 2 minutes        0.0.0.0:7051->7051/tcp                       peer0.org1.example.com
5809a9613b85        hyperledger/fabric-orderer:2.1.0    "orderer"                2 minutes ago       Up 2 minutes        0.0.0.0:7050->7050/tcp                       orderer.example.com
edf256ff4aa1        hyperledger/fabric-couchdb:0.4.20   "tini -- /docker-ent…"   2 minutes ago       Up 2 minutes        4369/tcp, 9100/tcp, 0.0.0.0:7984->5984/tcp   couchdb1
1f20e47faf64        hyperledger/fabric-couchdb:0.4.20   "tini -- /docker-ent…"   2 minutes ago       Up 2 minutes        4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp   couchdb0
cf8169852864        hyperledger/fabric-ca:1.4.6         "sh -c 'fabric-ca-se…"   2 minutes ago       Up 2 minutes        7054/tcp, 0.0.0.0:8054->8054/tcp             ca_org2
78362ec18a73        hyperledger/fabric-ca:1.4.6         "sh -c 'fabric-ca-se…"   2 minutes ago       Up 2 minutes        0.0.0.0:7054->7054/tcp                       ca_org1
95304b72d6a0        hyperledger/fabric-ca:1.4.6         "sh -c 'fabric-ca-se…"   2 minutes ago       Up 2 minutes        7054/tcp, 0.0.0.0:9054->9054/tcp             ca_orderer

Stop Network

# STEPS for Stop Environment (keep generated files)
# 1. stop docker containers of CA servers
# 2. stop docker containers of Orderer and Peers with DB
$ sudo -s . ./shutdown.sh
% docker ps
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

Startup Network

# STEPS for Startup Environment
# 1. startup CA servers as docker container
# 2. startup Orderer and Peers with DB as docker container
$ sudo -s . ./startup.sh

Teardown Network

# STEPS for Teardown Environment
# 1. remove all the docker containers
# 2. remove all the docker images generated for chaincode runtime # 3. delete all the volumes
# 4. delete all the networks
# 5. delete all the files generated during network startup
$ sudo -s . ./teardown.sh

Deploy Chaincode

# STEPS for Deploy Chaincode
# 1. package chaincode
# 2. install chaincode locally and get package id # 3. approve chaincode installation from org1
# 4. get approvals from org2 on channel
# 5. check chaincode commit readiness
# 6. commit chaincode deployment transaction
# 7. check commit status

$ sudo -s . ./init.sh
$ sudo -s . scripts/deploy_chaincode.sh

Access Chaincode via CLI

# STEPS for Access Chaincode via CLI
# 1. initialize chaincode
# 2. query chaincode (ledger-readonly) 
# 3. invoke chaincode (ledger-write)

$ sudo -s . ./init.sh
$ sudo -s  . scripts/test_example01.sh

问题1

问题待解决:

$ . ./startup.sh
4.Startup CA Services, Peers and Orderer in Network
Creating network "net_ORG1" with the default driver
Creating network "net_ABC" with the default driver
Creating network "net_ORG1_PEER0" with the default driver
Creating network "net_ORG2" with the default driver
Creating network "net_ORDERER" with the default driver
Creating network "net_ORG2_PEER0" with the default driver
ERROR: no such image: hyperledger/fabric-ca:: invalid reference format

问题2

问题待解决:

% sudo -s  . scripts/test_example01.sh
######## - (COMMON) setup variables - ########
'CHAINCODE_NAME' set to 'mycc'
'CHAINCODE_LANG' set to ''
'CHAINCODE_PATH' set to ''
+scripts/test_example01.sh:18> [[ true == true ]]
+scripts/test_example01.sh:19> peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls true --cafile /Users/zgpeace/Workspace/blockchain/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc --isInit -c '{"Function":"Init","Args":[]}'
Error: error getting endorser client for invoke: endorser client failed to connect to localhost:7051: failed to create new connection: connection error: desc = "transport: error while dialing: dial tcp [::1]:7051: connect: connection refused"
+scripts/test_example01.sh:29> set +x
######## - (ORG1) query chaincode - ########
+scripts/test_example01.sh:35> peer chaincode query -C mychannel -n mycc -c '{"Function":"Hi", "Args":[]}'
Error: error getting endorser client for query: endorser client failed to connect to localhost:7051: failed to create new connection: connection error: desc = "transport: error while dialing: dial tcp [::1]:7051: connect: connection refused"
+scripts/test_example01.sh:36> set +x
######## - (ORG2) query chaincode - ########
+scripts/test_example01.sh:41> peer chaincode query -C mychannel -n mycc -c '{"Function":"Hi", "Args":[]}'
Error: error getting endorser client for query: endorser client failed to connect to localhost:9051: failed to create new connection: connection error: desc = "transport: error while dialing: dial tcp [::1]:9051: connect: connection refused"
+scripts/test_example01.sh:42> set +x
############# END ###############
 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服