目录
一、参考文档
-
hyperledger超级账本官方文档地址:https://hyperledger-fabric.readthedocs.io/en/release-2.2/
-
hyperledger中文文档:https://hyperledgercn.github.io/hyperledgerDocs/
二、环境依赖
- ubuntu 20.04.1
- go version go1.16.3 linux/amd64
- git version 2.25.1
- docker 20.10.17
- docker-compose 1.29.2
Fabric_15">三、Fabric源码安装
3.1、创建链目录
mkdir -p $GOPATH/src/github.com/hyperledger
3.2、下载源码
cd $GOPATH/src/github.com/hyperledger
git clone https://gitee.com/hyperledger/fabric.git -b v2.2.0
3.3、修改安装脚本
vim $GOPATH/src/github.com/hyperledger/fabric/scripts/bootstrap.sh
- 把
https://github.com/hyperledger/fabric-samples.git
修改为https://gitee.com/hyperledger/fabric-samples.git
- 注释
pullBinaries
3.4、开始安装
3.4.1、执行安装脚本
cd $GOPATH/src/github.com/hyperledger/fabric/scripts/
./bootstrap.sh
若报错:
Pull Hyperledger Fabric docker images
FABRIC_IMAGES: peer orderer ccenv tools nodeenv baseos javaenv
===> Pulling fabric Images
====> hyperledger/fabric-peer:2.2.0
Error response from daemon: error parsing HTTP 408 response body: invalid character '<' looking for beginning of value: "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n"
Error response from daemon: No such image: hyperledger/fabric-peer:2.2.0
Error response from daemon: No such image: hyperledger/fabric-peer:2.2.0
====> hyperledger/fabric-orderer:2.2.0
2.2.0: Pulling from hyperledger/fabric-orderer
error parsing HTTP 408 response body: invalid character '<' looking for beginning of value: "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n"
Error response from daemon: No such image: hyperledger/fabric-orderer:2.2.0
Error response from daemon: No such image: hyperledger/fabric-orderer:2.2.0
====> hyperledger/fabric-ccenv:2.2.0
则尝试调整/etc/docker/daemon.json
:
{
"registry-mirrors": ["http://hub-mirror.c.163.com","https://registry.docker-cn.com","https://docker.mirrors.ustc.edu.cn"]
}
sudo systemctl daemon-reload
sudo systemctl restart docker
3.4.2、手动下载ca和二进制配置包
下载bootstrap.sh
中指定的对应版本ca和二进制配置的包:
# VERSION=2.2.0
wget https://github.com/hyperledger/fabric/releases/download/v2.2.0/hyperledger-fabric-linux-amd64-2.2.0.tar.gz
# CA_VERSION=1.4.7
wget https://github.com/hyperledger/fabric-ca/releases/download/v1.4.7/hyperledger-fabric-ca-linux-amd64-1.4.7.tar.gz
复制到fabric-samples目录:
tar -zxvf hyperledger-fabric-linux-amd64-2.2.0.tar.gz
tar -zxvf hyperledger-fabric-ca-linux-amd64-1.4.7.tar.gz
cp -r bin fabric-samples/
cp -r config fabric-samples/
四、启动测试网络
cd $GOPATH/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network
# cryptogen启动方式
./network.sh up
# ca-server启动方式
# ./network.sh up -ca
日志:
Starting nodes with CLI timeout of '5' tries and CLI delay of '3' seconds and using database 'leveldb' with crypto from 'cryptogen'
LOCAL_VERSION=2.2.0
DOCKER_IMAGE_VERSION=2.2.0
/usr/local/software/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/../bin/cryptogen
Generating certificates using cryptogen tool
Creating Org1 Identities
+ cryptogen generate --config=./organizations/cryptogen/crypto-config-org1.yaml --output=organizations
org1.example.com
+ res=0
Creating Org2 Identities
+ cryptogen generate --config=./organizations/cryptogen/crypto-config-org2.yaml --output=organizations
org2.example.com
+ res=0
Creating Orderer Org Identities
+ cryptogen generate --config=./organizations/cryptogen/crypto-config-orderer.yaml --output=organizations
+ res=0
Generating CCP files for Org1 and Org2
/usr/local/software/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/../bin/configtxgen
Generating Orderer Genesis block
+ configtxgen -profile TwoOrgsOrdererGenesis -channelID system-channel -outputBlock ./system-genesis-block/genesis.block
2023-11-01 16:23:18.229 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2023-11-01 16:23:18.244 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 002 orderer type: etcdraft
2023-11-01 16:23:18.244 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 Orderer.EtcdRaft.Options unset, setting to tick_interval:"500ms" election_tick:10 heartbeat_tick:1 max_inflight_blocks:5 snapshot_interval_size:16777216
2023-11-01 16:23:18.244 CST [common.tools.configtxgen.localconfig] Load -> INFO 004 Loaded configuration: /usr/local/software/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/configtx/configtx.yaml
2023-11-01 16:23:18.246 CST [common.tools.configtxgen] doOutputBlock -> INFO 005 Generating genesis block
2023-11-01 16:23:18.247 CST [common.tools.configtxgen] doOutputBlock -> INFO 006 Writing genesis block
+ res=0
Creating network "fabric_test" with the default driver
Creating volume "docker_orderer.example.com" with default driver
Creating volume "docker_peer0.org1.example.com" with default driver
Creating volume "docker_peer0.org2.example.com" with default driver
Creating peer0.org1.example.com ... done
Creating orderer.example.com ... done
Creating peer0.org2.example.com ... done
Creating cli ... done
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
677082ab5dc4 hyperledger/fabric-tools:latest "/bin/bash" 1 second ago Up Less than a second cli
9e567df423f2 hyperledger/fabric-peer:latest "peer node start" 3 seconds ago Up 1 second 0.0.0.0:9051->9051/tcp, :::9051->9051/tcp, 7051/tcp, 0.0.0.0:19051->19051/tcp, :::19051->19051/tcp peer0.org2.example.com
30248ff4a8c7 hyperledger/fabric-orderer:latest "orderer" 3 seconds ago Up Less than a second 0.0.0.0:7050->7050/tcp, :::7050->7050/tcp, 0.0.0.0:17050->17050/tcp, :::17050->17050/tcp orderer.example.com
884783874fcc hyperledger/fabric-peer:latest "peer node start" 3 seconds ago Up 1 second 0.0.0.0:7051->7051/tcp, :::7051->7051/tcp, 0.0.0.0:17051->17051/tcp, :::17051->17051/tcp peer0.org1.example.com
看到docker的2个peer节点容器、1个orderer节点容器和客户端tools容器都正常运行,测试网络启动成功。
五、使用测试网络
5.1、创建应用通道
使用network.sh脚本在org1和org2之间创建通道并加入他们的对等节点,执行以下命令创建一个通道:
./network.sh createChannel -c mychannel
5.2、部署链码
./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-java -ccl java
5.3、发送交易
配置环境变量:
cd $GOPATH/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network
export PATH=${PWD}/../bin:$PATH
# 该目录下包含peer节点的core.yaml
export FABRIC_CFG_PATH=$PWD/../config/
# 客户端连接peer0
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051
发交易,初始化资产(调用链码的初始化方法InitLedger):
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"InitLedger","Args":[]}'
查询初始化后的资产(调用链码的GetAllAssets方法):
peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
六、关闭测试网络
./network.sh down