1.环境准备
进入test-network目录下,使用which peer 检查一下环境是否有问题
如果有问题就下面指令配置一下(后面的路径根据自己的fabric-sample路径确定)
export PATH=$PATH:/home/ubuntu/go/src/github.com/hyperledger/fabric-samples/bin
使用 echo $FABRIC_CFG_PATH 查看是否输入相关路径,否则输入指令
export PATH=${PWD}/../bin:$PATH
export FABRIC_CFG_PATH=$PWD/../config/
可以启动两个回话模拟两个组织 org1 和 org2 ,在两个会话中分别输入下面的指令以配置环境
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
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
然后使用peer channel list指令查看目前通道情况
报错情况:
ubuntu@VM-16-12-ubuntu:~/go/src/github.com/hyperledger/fabric-samples/test-network$ peer channel list
2022-12-06 21:07:20.412 CST 0001 ERRO [main] InitCmd -> Cannot run peer because cannot init crypto, specified path "/home/ubuntu/go/src/github.com/hyperledger/fabric-samples/config/msp" does not exist or cannot be accessed: stat /home/ubuntu/go/src/github.com/hyperledger/fabric-samples/config/msp: no such file or directory
原因是因为没有配置环境变量来使用org1或者org2来进行交互
2. erc20部署
2.1 打包链码
在打包链码之前先检查一下erc20链码所在的目录中是否包含vendor,如果没有的话依旧是手动拉取
ls ../token-erc-20/chaincode-go/
我这里没有,所以需要执行下面命令手动拉取
cd ../token-erc-20/chaincode-go/
go mod vendor
然后再回到test-network目录下打包链码(org1)
peer lifecycle chaincode package erc20.tar.gz --path ../token-erc-20/chaincode-go/ --lang golang --label erc20_1.0
2.2 安装链码(1&2)
peer lifecycle chaincode install erc20.tar.gz
查看链码安装情况
peer lifecycle chaincode queryinstalled
设置环境变量(org1+org2)
export ERC20_ID=erc20_1.0:7c99f9616d100b2736ad1d03850e24b844e0d5d1af433b8ab673ba934acf8e8c
approve(org1+org2)
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name erc20 --version 1.0 --package-id $ERC20_ID --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
可以检查一下是否可以commit
peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name erc20 --version 1.0 --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
commit(org1/org2)
peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name erc20 --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
2.3 测试erc20
mint测试(只有org1可以执⾏) mint相当于初始化最初的资源数量 类似于发行货币
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 erc20 --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":"Mint","Args":["900"]}'