Hyperledger-Fabric

注意问题

本文以 2.5 版本为例

环境配置

安装 GO 环境

1
2
3
4
curl -O https://dl.google.com/go/go1.22.2.linux-amd64.tar.gz
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.22.2.linux-amd64.tar.gz
# 配置环境变量
export PATH=$PATH:/usr/local/go/bin

注意环境问题:

1
export GOSUMDB=sum.golang.org

官方文档: https://go.dev/doc/install

其他依赖文件:

1
apt install jq

安装

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" # -c 指定通道名

通过通道部署链码

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/config
# env 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
# env 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

然后是正式的交互

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 # 此步骤非常重要, 否则后面无法生成vendor文件夹
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


Hyperledger-Fabric
https://d4wnnn.github.io/2024/04/16/Blockchain/Hyperledger-Fabric/
作者
D4wn
发布于
2024年4月16日
许可协议