注意问题
本文以 2.5 版本为例
环境配置
安装 GO 环境
1 2 3 4 curl -O https://dl.google.com/go/go1.22.2.linux-amd64.tar.gzrm -rf /usr/local/go && tar -C /usr/local -xzf go1.22.2.linux-amd64.tar.gzexport PATH=$PATH :/usr/local/go/bin
注意环境问题:
1 export GOSUMDB=sum.golang.org
官方文档: https://go.dev/doc/install
其他依赖文件:
安装
CURL 命令执行的步骤:
获取下载命令
1 curl -sSLO https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh && chmod +x install-fabric.sh
测试
下载 fabric-samples
项目,然后进入
test-network
文件夹。
1 2 3 ./network.sh -h ./network.sh down ./network.sh up
与 Fabric
网络交互的每个节点和用户都需要属于一个组织才能参与网络。测试网络包括两个对等组织:Org
1 和 Org
2。它还包括一个排序组织。网络中每个节点都需要属于一个组织。在测试网络中,组织
peer0.org1.example.com
和组织
peer0.org1.example.com
分别拥有一个对等节点 Peer。
排序节点在接收到认可的交易后,将他们排序,并组成块,然后分发给 Peer
节点。在测试网络中,只有一个排序节点,使用 Raft 共识算法,组织为
orderer.example.com
。
每个通道对应一个账本,只有受邀进入组织的人才能使用对应的通道。
创建通道:
1 ./network.sh createChannel -c "channel1"
通过通道部署链码
1 ./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
接下来就是交互,在交互之前,需要设置几个环境变量。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 export PATH=/root/Desktop/HLF/fabric-samples/bin:$PATH export FABRIC_CFG_PATH=/root/Desktop/HLF/fabric-samples/configexport 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.crtexport CORE_PEER_MSPCONFIGPATH=${PWD} /organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/mspexport CORE_PEER_ADDRESS=localhost:7051export 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.crtexport CORE_PEER_MSPCONFIGPATH=${PWD} /organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/mspexport CORE_PEER_ADDRESS=localhost:9051
然后是正式的交互
1 2 3 4 peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}' 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":"TransferAsset","Args":["asset6","Christopher"]}'
其他操作:
1 ./network.sh cc list -org 1
参考资料: -
https://www.bilibili.com/video/BV1g3411h71Z/?spm_id_from=333.337.search-card.all.click&vd_source=bf133d296119691c661008e881da330a
开发合约
编译合约
1 2 3 4 5 go mod init github.com/hyperledger/fabric-samples/chaincode/contract-tutorial go get -u github.com/hyperledger/fabric-contract-api-go go mod tidy go mod vendor go build
注意,开启新的终端时一定要设置如下环境变量:
1 export GOSUMDB=sum.golang.org
GO 版本问题:不能使用 1.22,必须使用 1.19
1 curl -O https://dl.google.com/go/go1.19.linux-amd64.tar.gz
部署合约
1 2 cd /root/Desktop/HLF/fabric-samples/test-network ./network.sh deployCC -ccn mygotest -ccp /root/Desktop/gotest -ccl go
合约交互
1 2 3 4 5 6 7 8 9 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 mygotest --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 '{"Args":["Create", "KEY_1", "VALUE_1"]}' 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 mygotest --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 '{"Args":["Read", "KEY_1"]}' 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 mygotest --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 '{"Args":["Update", "KEY_1", "QYQYQY"]}'
参考资料:
https://github.com/hyperledger/fabric-contract-api-go/blob/main/tutorials/getting-started.md