当前位置: 代码迷 >> 综合 >> Hyperledger fabric 2.0(文档7.1 Deploying a smart contract to a channel)
  详细解决方案

Hyperledger fabric 2.0(文档7.1 Deploying a smart contract to a channel)

热度:88   发布时间:2024-02-12 21:51:34.0

写在前面:因为之前一直在使用1.0.0和1.4版本,文档500多页看到 key concepts就已经感觉这个文档是没有办法看完的,但是在网上照着很多博主的文章去做,还是会有很多错误,最后还是继续做死 做人,照着文档去做
本文是根据hyperledger fabric 2.0文档进行
前面五个步骤安装go、docker、docker compose都是按照之前的博文安装的
https://blog.csdn.net/zzzbeauty/article/details/107772698
从第六步安装fabric 版本开始就不一样了

六、下载fabric源码(2.0)

1、

mkdir -p $GOPATH/src/github.com/hyperledger
cd $GOPATH/src/github.com/hyperledger
git clone https://github.com/hyperledger/fabric.git

2、下载好之后

cd $GOPATH/src/github.com/hyperledger/fabric

可以使用?查看现在发布的release版本:

git branch -a
git checkout release-2.0
cd scripts
./bootstrap.sh

?执行bootstrap.sh过程很漫长(可能一个小时,而且还可能会出错,尝试前两次因为网络问题,总是卡在这里:)但是挂上梯子vpn之后又尝试了一次 ,时间缩短了超级多而且也成功了。在这里插入图片描述
成功如下图:下载好的docker images
在这里插入图片描述
3、因为后面需要用到node和npm,所以提前安装

sudo apt install nodejs
sudo apt install npm

查看版本:

node -v
npm -v

七、测试test-network

可以先在图形化的文件夹中熟悉一下这些文件

cd fabric-samples
cd test-network
./network.sh up

启动完毕:在这里插入图片描述

八、Deploying a smart contract to a channel(进入正文)

以下都是在 test-network 目录下进行的
1、确保network没有在运行

./network.sh down

创建channel(这里默认是mychannel,并启动)

./network.sh up createChannel

启动成功:
在这里插入图片描述
2、Setup LogsOUT(输出日志)
这一步不是必须做的,可以运行一下看一下整个过程的打印日志

cd fabric-samples/test-networkcp ../commercial-paper/organization/digibank/configuration/cli/monitordocker.sh./monitordocker.sh net_test

在这里插入图片描述一直开着这个界面,就能看到所有情况。
3、Package the smart contract
本文采用的是java scripts版(还有其余两种 go 、java版 可以从文档里看)
开始一个新的终端Terminal界面

cd fabric-samples/chaincode/fabcar/javascript

?看到路径~!
可以在这个文件中看到很多东西package.json,还有\lib\fabcar.js的内容,这里不展开了。。。。(理论知识有机会再补充吧,主要是实现完整的步骤)

npm install
cd ../../../test-network

The peer binaries are located in the bin folder of the fabric-samples repository. Use the following command to add those binaries to your CLI Path:

export PATH=${PWD}/../bin:$PATHexport FABRIC_CFG_PATH=$PWD/../config/

再查看:

peer verison

在这里插入图片描述将fabcar文件夹复制到本目录下 并命名为fabcar_1

peer lifecycle chaincode package fabcar.tar.gz --path ../chaincode/fabcar/javascript/ --lang node --label fabcar_1

4、 Install the chaincode package
有两个组织organization,为org1,org2,分别又有自己的peer:
? peer0.org1.example.com
? peer0.org2.example.com
所以通过更改CORE_PEER_ADDRESS 的方式,将链码分别安装到两个organizations
先peer0.org1.example. com.

export CORE_PEER_TLS_ENABLED=true 
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
peer lifecycle chaincode install fabcar.tar.gz

安装成功:
在这里插入图片描述
peer0.org2.example.com.


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
peer lifecycle chaincode install fabcar.tar.gz

成功:
在这里插入图片描述
5、 Approve a chaincode de?nition
这里采用的背书策略是2/2,org1,org2都是表示同意,该链码才是有效被承认的。
上面操作结束之后我们现在环境还是处于org2中,所以我们先对org2进行操作。
查询package ID(每个人的不一样):

peer lifecycle chaincode queryinstalled

在这里插入图片描述

export CC_PACKAGE_ID=fabcar_1:

?冒号后面填上上面查询到的package ID
比如 我自己的就是:export CC_PACKAGE_ID=fabcar_1:54f0fb232129783ee008727d8adc748a0ffbd3cb88812b0d2ce3796ccb70806a

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 true --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
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 true --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

最后查看一下支持结果:

peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name fabcar --version 1.0 --sequence 1 --tls true --cafile ${PWD}/organizations/ ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca. example.com-cert.pem --output json

在这里插入图片描述
两个org已经通过之后,还要经过order

peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --version 1.0 --sequence 1 --tls true --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

在这里插入图片描述
6、Invoking the chaincode

peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls true --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"]}'

在这里插入图片描述
CAR0—CAR9
7、Upgrading a smart contract

cd ../chaincode/fabcar/javascript 
npm install 
cd ../../../test-network

重新复制到当前目录下 一个fabcar文件 命名为fabcar_2

export PATH=${PWD}/../bin:$PATH export FABRIC_CFG_PATH=$PWD/../config/ export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msppeer lifecycle chaincode package fabcar_2.tar.gz --path
../chaincode/fabcar/javascript/ --lang node --label fabcar_2

确保环境在org1

export CORE_PEER_TLS_ENABLED=trueexport CORE_PEER_LOCALMSPID="Org1MSP"export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/mspexportCORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crtexport CORE_PEER_ADDRESS=localhost:7051
peer lifecycle chaincode install fabcar_2.tar.gz

在这里插入图片描述
查看现在拥有的package ID:

peer lifecycle chaincode queryinstalled

在这里插入图片描述

export NEW_CC_PACKAGE_ID=fabcar_2:

?冒号后面 是package ID

peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --version 2.0 --package-id $CC_PACKAGE_ID --sequence 2 --tls true --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

切换到org2:

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
peer lifecycle chaincode install fabcar_2.tar.gz
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --version 2.0 --package-id $CC_PACKAGE_ID --sequence 2 --tls true --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

查看安装情况:

peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name fabcar --version 2.0 --sequence 2 --tls true --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 2.0 --sequence 2 --tls true --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 chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls true --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":"createCar","Args":["CAR11","Honda","Accord","Black","Tom"]}'

查询:

peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}'

在这里插入图片描述
8、clean up

docker stop logspout 
docker rm logspout
./network.sh down

?!!!7.1完结~?
有错误请指正哦~

  相关解决方案