【hyperledger-fabric】将智能合约部署到通道

简介

本文主要来自于B站视频教学视频,也主要参看了官方文档中下图这一章节。针对自己开发的代码做出相应的总结。

1.启动网络

shell 复制代码
# 跳转到指定的目录
cd /root/fabric/fabric-samples/test-network

# 启动docker容器并且创建通道
./network.sh up createChannel

2.打包智能合约

备注:这里参考的B站视频的go语言版本进行打包智能合约部分。

shell 复制代码
# 指定的链包路径下执行下述语句(这里参照视频是在/root/fabric/fabric-samples/chaincode/fabcar/go)
GO111MODULE=on go mod vendor

cd ../../test-network

# 添加环境变量
export PATH=${PWD}/../bin:$PATH

# 设置FABRIC_CFG_PATH指向存储库core.yaml中的文件fabric-samples
export FABRIC_CFG_PATH=$PWD/../config/

# 执行生命链周期代码。代码具体解析标识01
peer lifecycle chaincode package fabcar.tar.gz --path ../chaincode/fabcar/go/ --lang golang --label facar_1

至此,链代码包已经创建成功,可以在测试网络的对等点上安装连代码。

3.安装链码包

在我们打包资产转移(基本)智能合约后,我们可以在我们的节点上安装链码。链码需要安装在每个将背书交易的对等点上。因为我们将设置背书策略以要求 Org1 和 Org2 都背书,所以我们需要在两个组织运营的对等节点上安装链码:

shell 复制代码
# 代码具体解析标识02,简述以Org1管理员身份到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

# 安装链码
peer lifecycle chaincode install fabcar.tar.gz

# 代码具体解析与02类似,简述为以Org2管理员身份到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 lifecycle chaincode install fabcar.tar.gz

批准链码定义

安装链码包后,您需要批准组织的链码定义。该定义包括链码治理的重要参数,例如名称、版本和链码背书策略。

shell 复制代码
# 查询指定链包的包ID
peer lifecycle chaincode queryinstalled

# 将查询到到的id放到代码存放的位置
export CC_PACKAGE_ID=basic_1.0:69de748301770f6ef64b42aa6bb6cb291df20aa39542c3ef94008615704007f3

# 具体代码解析03,简述为由于安装链码时,设置的是Org2为管理员身份操作CLI,因此此时将链码的定义批准为Org2。
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --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 fabcar --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

将链码定义提交到通道

当足够数量的组织批准链码定义后,一个组织可以将链码定义提交到通道。如果大多数通道成员批准了该定义,则提交交易将成功,并且链码定义中商定的参数将在通道上实现。

shell 复制代码
# 检查通道成员是否批准了链码定义,该命令返回出来的结果显示频道成员是否批准
peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name fabcar --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

# 提交链码定义到通道
peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --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

# 查询链码定义是否提交到通道
peer lifecycle chaincode querycommitted --channelID mychannel --name fabcar --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

调用链码

将链码定义提交到通道后,链码将在加入安装了链码的通道的对等点上启动。资产转移(基本)链代码现在已准备好由客户端应用程序调用。使用以下命令在账本上创建一组初始资产。请注意,invoke 命令需要针对足够数量的对等点才能满足链码背书策略。(请注意,CLI 不会访问 Fabric Gateway 对等点,因此必须指定每个认可对等点。)

shell 复制代码
# 创建一组初始化资产
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 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":"initLedger","Args":[]}'

# 调用查询函数读取链码创建的汽车集
peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}'

代码具体解析标识

01:fabcar.tar.gz此命令将在当前目录中创建一个名为的包。该--lang标志用于指定链码语言,该--path标志提供智能合约代码的位置。该路径必须是完全限定路径或相对于当前工作目录的路径。该--label标志用于指定链码标签,该标签将在安装后识别您的链码。建议您的标签包含链代码名称和版本。

02:置以下环境变量以peer以 Org1 管理员用户身份操作 CLI。将CORE_PEER_ADDRESS设置为指向 Org1 对等点peer0.org1.example.com

  • CORE_PEER_TLS_ENABLED=true标识启用了TLS加密来保护与peer节点的通信。
  • CORE_PEER_LOCALMSPID="Org1MSP"表示本地MSP成员服务提供商的ID。被设置为Org1MSP,表示组织1的成员服务提供商ID。
  • CORE_PEER_TLS_ROOTCERT_FILE环境变量,指定了peer节点TLS根证书的位置。${PWD}是一个环境变量,表示当前工作目录。因此,这条命令使用了当前工作目录来构建TLS根证书的文件路径。
  • CORE_PEER_MSPCONFIGPATH环境变量,指定了MSP配置文件的位置。这个配置文件包含了与peer节点通信所需的认证信息。
  • 最后一个命令设置了CORE_PEER_ADDRESS环境变量,指定了peer节点的地址和端口号。在这里,peer节点被设置为在本地主机(localhost)的7051端口上监听

03:这个命令的目的是要在指定的通道上为您的组织批准特定链码的定义,这样就可以将其部署到该通道上。

  • peer lifecycle chaincode approveformyorg - 这是一个peer命令,用于在指定通道上为您的组织批准链码定义。
  • -o localhost:7050 - 这个标志指定了要连接的Orderer节点的地址和端口号。
  • --ordererTLSHostnameOverride orderer.example.com - 这个标志指定了用于TLS连接的Orderer的主机名。
  • --channelID mychannel - 这个标志指定了要在其上批准链码定义的通道ID。
  • --name fabcar - 这个标志指定了您要批准的链码的名称。
  • --version 1.0 - 这个标志指定了您要批准的链码的版本号。
  • --package-id $CC_PACKAGE_ID - 这个标志指定了您要批准的链码的包ID。包ID是在安装链码时生成的唯一标识符。
  • --sequence 1 - 这个标志指定了链码的序列号。对于每个chaincode名称,版本和通道需要一个唯一的序列号。
  • --tls - 这个标志指示使用TLS来加密通信。
  • --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem - 这个标志指定了用于TLS连接的认证机构(CA)的根证书文件的路径。
相关推荐
BingoGo1 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack1 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端
木西2 天前
揭秘 Web3 隐私社交标杆:CocoCat 的核心架构与智能合约实现
web3·智能合约·solidity
BingoGo2 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php
JaguarJack2 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php·服务端
木西3 天前
深度拆解 Grass 模式:基于 EIP-712 与 DePIN 架构的奖励分发系统实现
web3·智能合约·solidity
JaguarJack3 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
后端·php·服务端
BingoGo3 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
php
JaguarJack4 天前
告别 Laravel 缓慢的 Blade!Livewire Blaze 来了,为你的 Laravel 性能提速
后端·php·laravel
郑州光合科技余经理5 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php