区块链:安装 Hyperledger Fabric v2
悠扬的幻想天空 - 博客
May 23, 2020 技术 • 作者:悠扬
参考:https://hyperledger-fabric.readthedocs.io/en/release-2.0/
环境
CentOS Linux release 7.6.1810 (Core)
Docker version 19.03.8
Fabric v2.1.0
安装 Fabric-Samples
项目仓:<https://github.com/hyperledger/fabric-samples
在计算机上确定要放置 Fabric-Samples 库的位置,并执行以下命令:
curl -sSL https://bit.ly/2ysbOFE | bash -s
运行 Test-Network
参考:https://hyperledger-fabric.readthedocs.io/en/release-2.0/test_network.html
您可以在 Fabric-Samples 存储库的 test-network 目录中找到启动网络的脚本。
进入测试网络的目录:
cd fabric-samples/test-network
启动带 CA节点 的区块链网络
会创建出一个 order 节点,两个 peer 节点,和三个 ca 节点:
./network.sh up -ca
创建 channel
会创建一个默认名字为 mychannel 的 channel:
./network.sh createChannel
安装智能合约
使用 go 编写的智能合约:
./network.sh deployCC
也可以选择 java 语言的智能合约:
./network.sh deployCC -l java
你可以直接修改 /fabric-samples/chaincode/fabcar
目录下的文件夹,java 文件夹对应的就是 java 语言的智能合约,导入到 IDE 里面开发就行了。注意更新项目文件之后,调整好文件的读写权限,不然到时候编译不通过。
测试 Fabric
确保是在 test-network
目录进行操作,设置环境变量:
export PATH=${PWD}/../bin:${PWD}:$PATH
export FABRIC_CFG_PATH=$PWD/../config/
配置 org1 环境变量:
# Environment variables for Org1
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
以 org1 身份查询区块链数据:
peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}'
再修改区块链数据,使得 CAR9 的拥有者为 Dave:
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls true --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n fabcar --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":"changeCarOwner","Args":["CAR9","Dave"]}'
配置 org2 身份:
# Environment variables for Org2
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
export CORE_PEER_ADDRESS=localhost:9051
查询 org2 上的账本数据:
peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryCar","CAR9"]}'
发现 "CAR9
的拥有者是 Dave:
{"make":"Holden","model":"Barina","colour":"brown","owner":"Dave"}