文章目录
- 相关安装
- 启动测试网络
- 创建channel
- 打包链码
- 安装链码包
- 通过链码定义
- 链码定义提交给通道
- 调用链码
- 关闭网络
- 遇到的问题
- 1.docker保持启动状态
- 2.忘起测试网络了
- 3.Java版本过高,推荐1.8
相关安装
-
npm、node、git、docker、docker-compose。docker保证一直运行
service docker start service docker status
2.安装Java、maven环境
https://blog.csdn.net/qq_41829594/article/details/122408706
https://www.oracle.com/java/technologies/downloads/
https://maven.apache.org/download.cgi
tar -zxvf
在/etc/profile配置环境变量,之后source文件使其生效,检查是否安装成功
# set java development
export JAVA_HOME=/etc/opt/java/jdk1.8.0 #Java解压路径
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
3.安装jq
apt install jq
jq --version
4.在fabric里下载.sh文件
curl -sSLO https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh && chmod +x install-fabric.sh
5.运行.sh文件,下载docker印象、Fabric示例和二进制文件
./install-fabric.sh docker samples binary
or
./install-fabric.sh d s b
启动测试网络
cd fabric-samples/test-network
./network.sh up
docker ps -a
创建channel
./network.sh createChannel
打包链码
下载别人写的Java合约源码
cd ~fabric-samples/chaincode/
git clone https://gitee.com/kernelHP/hyperledger-fabric-contract-java-demo.git
cd ../../test-network
export PATH=${PWD}/../bin:$PATH //将bin目录中二进制文件添加到CLI路径
export FABRIC_CFG_PATH=$PWD/../config/ //设置FABRIC_CFG_PATH为指向fabric-samples中的core.yaml文件
package
创建链码包
peer lifecycle chaincode package hyperledger-fabric-contract-java-demo.tar.gz --path ../chaincode/hyperledger-fabric-contract-java-demo/ --lang java --label hyperledger-fabric-contract-java-demo_1
安装链码包
链码打包后,我们需要在认可交易的每个peer节点上安装链码。我们将设置背书策略以要求来自Org1和Org2的背书,我们需要在两个组织的节点上安装链码:peer0.org1.example.com和peer0.org2.example.com
设置环境变量,以Org1的身份操作peer CLI
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
install
节点安装链码
peer lifecycle chaincode install hyperledger-fabric-contract-java-demo.tar.gz
过程会非常慢,因为需要下载一些文件,可以另起一个终端查看进程
链码启动成功的截图,重复执行上条指令可发现链码成功安装
设置环境变量,以Org2的身份操作节点,重复步骤安装链码
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_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
peer lifecycle chaincode install hyperledger-fabric-contract-java-demo.tar.gz
通过链码定义
安装链码包后,需要通过组织的链码定义。该定义包括链码管理的重要参数,如名称、版本和链码认可策略。如果组织已在节点上安装了链码,则可通过包ID 将节点安装的链码与通过的链码定义相关联
queryinstalled
查询包ID,包ID用于将peer节点上安装的链码与通过的链码定义相关联,并允许组织使用链码来认可交易
peer lifecycle chaincode queryinstalled
将包ID设置为环境变量
export CC_PACKAGE_ID=hyperledger-fabric-contract-java-demo_1:54a21ac334812b0505cd548f1aed0bc0a7b603eef44c98a17ef05f004737b451
approveformyorg
Org2通过链码定义(当前的环境变量设置管理员为Org2)
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name hyperledger-fabric-contract-java-demo --version 1.0 --package-id $CC_PACKAGE_ID --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
重新设置环境变量,使Org1为管理员
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_ADDRESS=localhost:7051
Org1通过链码定义
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name hyperledger-fabric-contract-java-demo --version 1.0 --package-id $CC_PACKAGE_ID --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
链码定义提交给通道
checkcommitreadiness
检查通道成员是否已批准相同的链码定义,将生成一个JSON映射,显示通道成员是否批准
peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name hyperledger-fabric-contract-java-demo --version 1.0 --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --output json
commit
将链码定义提交到通道
peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name hyperledger-fabric-contract-java-demo --version 1.0 --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --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
querycommitted
确认链码定义已提交给通道,若成功则放回链码定义的顺序和版本
调用链码
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 hyperledger-fabric-contract-java-demo --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":"createCat","Args":["cat-0" , "tom" , "3" , "蓝色" , "大懒猫"]}'
peer chaincode query -C mychannel -n hyperledger-fabric-contract-java-demo -c '{"Args":["queryCat" , "cat-0"]}'
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 hyperledger-fabric-contract-java-demo --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":"updateCat","Args":["cat-0" , "tom" , "3" , "白色" , "超级大懒猫"]}'
peer chaincode query -C mychannel -n hyperledger-fabric-contract-java-demo -c '{"Args":["queryCat" , "cat-0"]}'
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 hyperledger-fabric-contract-java-demo --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":"deleteCat","Args":["cat-0"]}'
peer chaincode query -C mychannel -n hyperledger-fabric-contract-java-demo -c '{"Args":["queryCat" , "cat-0"]}'
无猫查询
创建一只猫
再进行查询
关闭网络
./network.sh down
遇到的问题
1.docker保持启动状态
https://blog.csdn.net/ACkingdom/article/details/125747583