1

搭建Fabric2.x开发环境

 1 year ago
source link: https://blog.51cto.com/sleep666/5911517
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

搭建Fabric2.x开发环境

精选 原创
搭建Fabric2.x开发环境_web3

在我们开始之前,如果您还没有这样做,您可能希望检查以下所有先决条件是否已安装在您将开发区块链应用程序或运行 Hyperledger Fabric 的平台上。

安装 Git

如果还没安装,下载最新版本的 ​ ​git​​ , 或者你运行curl命令有问题。

安装 cURL

如果尚未安装 cURl 或在服务器上运行 curl 命令出错时请下载最新版本的 ​ ​cURL​​ 工具。

Docker 和 Docker Compose

您将需要在将要运行或基于 Hyperledger Fabric 开发(或开发 Hyperledger Fabric)的平台上安装以下内容:

  • MacOSX, *nix 或 Windows 10: 要求 ​ ​Docker​​ 版本 17.06.2-ce 及以上。
  • 较旧版本的 Windows:​ ​Docker Toolbox​​ - 要求 Docker 版本 17.06.2-ce 及以上。

您可以通过执行以下命令来检查已安装的 Docker 的版本:

docker --version

确保 docker daemon 是在运行着的。

sudo systemctl start docker

可选:如果你希望 docker daemon 在系统启动的时候会自动启动的话,使用下边的命令:

sudo systemctl enable docker

将你的用户添加到 docker 组。

sudo usermod -a -G docker <username>

安装 fabric

curl -sSL https://bit.ly/2ysbOFE | bash -s -- <fabric_version> <fabric-ca_version>
#例如
curl -sSL https://bit.ly/2ysbOFE | bash -s -- 2.2.0 1.4.7
curl -sSL https://bit.ly/2ysbOFE | bash -s

#或者拉代码
git clone https://github.com/hyperledger/fabric-samples.git

启动测试网络

#2.进入目录
cd fabric-samples/test-network
# 启动网络并创建通道
./network.sh up createChannel

将这些二进制文件添加到您的CLI路径:

export PATH=${PWD}/../bin:$PATH

../bin
├── configtxgen
├── configtxlator
├── cryptogen
├── discover
├── fabric-ca-client
├── fabric-ca-server
├── ledgerutil
├── orderer
├── osnadmin
└── peer

导入环境变量

设置​​org1,org2​​的环境变量

# Environment variables for Org1
export FABRIC_CFG_PATH=$PWD/../config/
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/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:7051

# Environment variables for Org2
export FABRIC_CFG_PATH=$PWD/../config/
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/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:9051

如果未来使用在离线环境内,可以运行下面代码,链码如果没有​​vendor​​需要运行以下命令:

GO111MODULE=on
go mod vendor

回到 ​​test-network​​目录下进行链码打包

peer lifecycle chaincode package basic.tar.gz --path ../asset-transfer-basic/chaincode-go/ --lang golang --label basic_1.0
peer lifecycle chaincode install basic.tar.gz

查看链码安装状态

peer lifecycle chaincode queryinstalled
## 出现下面信息代表安装成功
Installed chaincodes on peer:
Package ID: basic_1.0:56069c8c46fe01a7837a218a6e2ab49a2b6c4a715c95ab2ab321a863b642d021, Label: basic_1.0

approve

设置链码包环境变量 (org1,org2)

我们将在批准链代码时使用​​CC_PACKAGE_ID​​​,因此让我们继续并将其保存为环境变量。将 返回的​​CC_PACKAGE_ID​​​ 粘贴到下面的命令中。注意:所有用户的​​CC_PACKAGE_ID​​​都不相同,因此您需要使用上一步从命令窗口返回的​​CC_PACKAGE_ID​​完成此步骤。

export CC_PACKAGE_ID=basic_1.0:56069c8c46fe01a7837a218a6e2ab49a2b6c4a715c95ab2ab321a863b642d021

由于环境变量已设置为peer以 Org2 管理员身份运行 CLI,因此我们可以批准 ​​basic​​​ 的链码定义为 Org2。Chaincode 在组织级别获得批准,因此该命令只需要针对一个对等方。使用​​gossip​​​将批准分发给组织内的其他同行。使用​ ​peer lifecycle chaincode approveformyorg​​命令批准链代码定义:

peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --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

将链代码定义提交到通道 commit

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

您可以使用​ ​peer lifecycle chaincode checkcommitreadiness​​命令来检查通道成员是否已批准相同的链代码定义。用于该checkcommitreadiness命令的标志与用于为您的组织批准链代码的标志相同。

peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name basic --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

主要是检查所有组织的授权状态,显示如下返回值:

{
"approvals": {
"Org1MSP": true,
"Org2MSP": true
}
}

由于作为通道成员的两个组织都批准了相同的参数,因此链代码定义已准备好提交给通道。您可以使用​ ​peer lifecycle chaincode commit​​命令将链代码定义提交到通道。提交命令也需要由组织管理员提交。(org1或org2)

peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --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命令来确认链码定义已提交到通道。

peer lifecycle chaincode querycommitted --channelID mychannel --name basic --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
Committed chaincode definition for chaincode 'basic' on channel 'mychannel':
Version: 1.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]

​--output json​​ 可以转换成json格式

{
"sequence": 1,
"version": "1.0",
"endorsement_plugin": "escc",
"validation_plugin": "vscc",
"validation_parameter": "EiAvQ2hhbm5lbC9BcHBsaWNhdGlvbi9FbmRvcnNlbWVudA==",
"collections": {},
"approvals": {
"Org1MSP": true,
"Org2MSP": true
}
}

上面方法太麻烦

./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go

在将链代码定义提交到通道后,链代码将在加入安装链代码的通道的对等节点上启动。​​basic​​​ 链码现在已准备好供客户端应用程序调用。请注意,​​invoke​​ 命令需要以足够数量的对等点为目标,以满足链代码背书策略。

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":"initLedger","Args":[]}'

如果命令成功,您应该能够收到类似于以下内容的响应:

2022-11-29 11:33:39.636 CST 0001 INFO [chaincodeCmd] chaincodeInvokeOrQuery -> Chaincode invoke successful. result: status:200

查询所有资产

peer chaincode query -C mychannel -n basic -c '{"Args":["getAllAssets"]}'
[{"AppraisedValue":300,"Color":"blue","ID":"asset1","Owner":"Tomoko","Size":5},{"AppraisedValue":400,"Color":"red","ID":"asset2","Owner":"Brad","Size":5},{"AppraisedValue":500,"Color":"green","ID":"asset3","Owner":"Jin Soo","Size":10},{"AppraisedValue":600,"Color":"yellow","ID":"asset4","Owner":"Max","Size":10},{"AppraisedValue":700,"Color":"black","ID":"asset5","Owner":"Adriana","Size":15},{"AppraisedValue":800,"Color":"white","ID":"asset6","Owner":"Michel","Size":15}]
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":"CreateAsset","Args":["asset7", "pink", "80", "Michel", "800"]}'
2022-11-29 11:51:29.706 CST 0001 INFO [chaincodeCmd] chaincodeInvokeOrQuery -> Chaincode invoke successful. result: status:200

再重新查询一下内容

可以看到新添加的内容就有了

[{"AppraisedValue":300,"Color":"blue","ID":"asset1","Owner":"Tomoko","Size":5},{"AppraisedValue":400,"Color":"red","ID":"asset2","Owner":"Brad","Size":5},{"AppraisedValue":500,"Color":"green","ID":"asset3","Owner":"Jin Soo","Size":10},{"AppraisedValue":600,"Color":"yellow","ID":"asset4","Owner":"Max","Size":10},{"AppraisedValue":700,"Color":"black","ID":"asset5","Owner":"Adriana","Size":15},{"AppraisedValue":800,"Color":"white","ID":"asset6","Owner":"Michel","Size":15},{"AppraisedValue":800,"Color":"pink","ID":"asset7","Owner":"Michel","Size":80}]

重新打包链

peer lifecycle chaincode package basic.2.0.0.tar.gz --path ../asset-transfer-basic/chaincode-go/ --lang golang --label basic_2.0

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK