0
点赞
收藏
分享

微信扫一扫

(二)熟悉fabric并查看测试test-network

骑在牛背上看书 2022-03-11 阅读 33
fabric运维

首先在~/go/src/github.com/hyperledger/fabric下进行orderer和peer的编译

编译Orderer

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

$ make orderer

命令执行后终端输出如下:

编译peer

$ make peer

命令执行后终端输出如下:

编译生成相关工具

Hyperledger Fabric 除了 Orderer 和 Peer 之外,还为我们提供了在搭建网络环境时所需要的一系列辅助工具:

  • configtxgen:生成初始区块及通道交易配置文件的工具
  • cryptogen:生成组织结构及相应的的身份文件的工具
  • configtxlator:将指定的文件在二进制格式与JSON格式之间进行转换

编译生成这些工具同样使用 make 即可:

$ make configtxgen 
$ make cryptogen 
$ make configtxlator 

 

查看 .build/bin 目录

$ ll build/bin/

测试fabric-samples/test-network这个测试例子

首先我们启动这个测试例子

cd fabric-samples/test-network

然后先移除先前运行中的所有容器

sudo ./network.sh down

然后重新打开容器

sudo ./network.sh up

可以通过命令来查看是否已经打开:

sudo docker ps -a

我们可以仔细看一下上面命令后得到的结果:

创建channel

Channel是一个用于特定网络成员之间通信的私有层,只有被邀请加入Channel的成员才能够使用它,对网络中其他成员不可见,每个Channel都有一个单独的区块链账本,被邀请加入Channel的peer可以存储Channel的账本然后验证Channel的交易。

运行如下命令可以创建一个名为mychannel的Channel连接Org1和Org2。

sudo ./network.sh createChannel

 为用户添加一个权限

sudo chmod -R 777 ~/go

同时由于还需要给用户添加opt的权限

sudo chmod -R 777 /opt

我们还需要设置go的环境

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direc

这样的话就可以进行链码的部署,注意这里不能用sudo 

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

若出现这样的情况,说明链码部署成功 

xzd@xzd-virtual-machine:~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network$ ./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
deploying chaincode on channel 'mychannel'
executing with the following
- CHANNEL_NAME: mychannel
- CC_NAME: basic
- CC_SRC_PATH: ../asset-transfer-basic/chaincode-go
- CC_SRC_LANGUAGE: go
- CC_VERSION: 1.0
- CC_SEQUENCE: 1
- CC_END_POLICY: NA
- CC_COLL_CONFIG: NA
- CC_INIT_FCN: NA
- DELAY: 3
- MAX_RETRY: 5
- VERBOSE: false
Vendoring Go dependencies at ../asset-transfer-basic/chaincode-go
~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/asset-transfer-basic/chaincode-go ~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network
go: downloading github.com/hyperledger/fabric-chaincode-go v0.0.0-20210718160520-38d29fabecb9
go: downloading github.com/hyperledger/fabric-protos-go v0.0.0-20201028172056-a3136dde2354
go: downloading github.com/golang/protobuf v1.3.2
go: downloading github.com/hyperledger/fabric-contract-api-go v1.1.1
go: downloading github.com/stretchr/testify v1.5.1
go: downloading google.golang.org/grpc v1.23.0
go: downloading google.golang.org/genproto v0.0.0-20180831171423-11092d34479b
go: downloading golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading github.com/pmezard/go-difflib v1.0.0
go: downloading gopkg.in/yaml.v2 v2.2.8
go: downloading github.com/xeipuuv/gojsonschema v1.2.0
go: downloading github.com/go-openapi/spec v0.19.4
go: downloading github.com/gobuffalo/packr v1.30.1
go: downloading golang.org/x/sys v0.0.0-20190710143415-6ec70d6a5542
go: downloading github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415
go: downloading github.com/gobuffalo/envy v1.7.0
go: downloading github.com/gobuffalo/packd v0.3.0
go: downloading github.com/go-openapi/jsonpointer v0.19.3
go: downloading github.com/go-openapi/jsonreference v0.19.2
go: downloading github.com/go-openapi/swag v0.19.5
go: downloading github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f
go: downloading github.com/joho/godotenv v1.3.0
go: downloading github.com/rogpeppe/go-internal v1.3.0
go: downloading github.com/PuerkitoBio/purell v1.1.1
go: downloading github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e
go: downloading golang.org/x/text v0.3.2
go: downloading github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578
~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network
Finished vendoring Go dependencies
+ peer lifecycle chaincode package basic.tar.gz --path ../asset-transfer-basic/chaincode-go --lang golang --label basic_1.0
+ res=0
Chaincode is packaged
Installing chaincode on peer0.org1...
Using organization 1
+ peer lifecycle chaincode install basic.tar.gz
+ res=0
2022-03-03 16:32:34.512 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\nJbasic_1.0:3cfcf67978d6b3f7c5e0375660c995b21db19c4330946079afc3925ad7306881\022\tbasic_1.0" > 
2022-03-03 16:32:34.513 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: basic_1.0:3cfcf67978d6b3f7c5e0375660c995b21db19c4330946079afc3925ad7306881
Chaincode is installed on peer0.org1
Install chaincode on peer0.org2...
Using organization 2
+ peer lifecycle chaincode install basic.tar.gz
+ res=0
2022-03-03 16:32:44.528 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\nJbasic_1.0:3cfcf67978d6b3f7c5e0375660c995b21db19c4330946079afc3925ad7306881\022\tbasic_1.0" > 
2022-03-03 16:32:44.528 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: basic_1.0:3cfcf67978d6b3f7c5e0375660c995b21db19c4330946079afc3925ad7306881
Chaincode is installed on peer0.org2
Using organization 1
+ peer lifecycle chaincode queryinstalled
+ res=0
Installed chaincodes on peer:
Package ID: basic_1.0:3cfcf67978d6b3f7c5e0375660c995b21db19c4330946079afc3925ad7306881, Label: basic_1.0
Query installed successful on peer0.org1 on channel
Using organization 1
+ peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile /home/xzd/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --channelID mychannel --name basic --version 1.0 --package-id basic_1.0:3cfcf67978d6b3f7c5e0375660c995b21db19c4330946079afc3925ad7306881 --sequence 1
+ res=0
2022-03-03 16:32:46.672 CST [chaincodeCmd] ClientWait -> INFO 001 txid [a4167851e17f523141c67776962bcee4d8b625dcc5deb5bbf0feafac0720edfc] committed with status (VALID) at localhost:7051
Chaincode definition approved on peer0.org1 on channel 'mychannel'
Using organization 1
Checking the commit readiness of the chaincode definition on peer0.org1 on channel 'mychannel'...
Attempting to check the commit readiness of the chaincode definition on peer0.org1, Retry after 3 seconds.
+ peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name basic --version 1.0 --sequence 1 --output json
+ res=0
{
	"approvals": {
		"Org1MSP": true,
		"Org2MSP": false
	}
}
Checking the commit readiness of the chaincode definition successful on peer0.org1 on channel 'mychannel'
Using organization 2
Checking the commit readiness of the chaincode definition on peer0.org2 on channel 'mychannel'...
Attempting to check the commit readiness of the chaincode definition on peer0.org2, Retry after 3 seconds.
+ peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name basic --version 1.0 --sequence 1 --output json
+ res=0
{
	"approvals": {
		"Org1MSP": true,
		"Org2MSP": false
	}
}
Checking the commit readiness of the chaincode definition successful on peer0.org2 on channel 'mychannel'
Using organization 2
+ peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile /home/xzd/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --channelID mychannel --name basic --version 1.0 --package-id basic_1.0:3cfcf67978d6b3f7c5e0375660c995b21db19c4330946079afc3925ad7306881 --sequence 1
+ res=0
2022-03-03 16:32:54.879 CST [chaincodeCmd] ClientWait -> INFO 001 txid [1dad07c2c2e1d4e7023af4e6519072dfee52888116fe18f302292e88e9b9d256] committed with status (VALID) at localhost:9051
Chaincode definition approved on peer0.org2 on channel 'mychannel'
Using organization 1
Checking the commit readiness of the chaincode definition on peer0.org1 on channel 'mychannel'...
Attempting to check the commit readiness of the chaincode definition on peer0.org1, Retry after 3 seconds.
+ peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name basic --version 1.0 --sequence 1 --output json
+ res=0
{
	"approvals": {
		"Org1MSP": true,
		"Org2MSP": true
	}
}
Checking the commit readiness of the chaincode definition successful on peer0.org1 on channel 'mychannel'
Using organization 2
Checking the commit readiness of the chaincode definition on peer0.org2 on channel 'mychannel'...
Attempting to check the commit readiness of the chaincode definition on peer0.org2, Retry after 3 seconds.
+ peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name basic --version 1.0 --sequence 1 --output json
+ res=0
{
	"approvals": {
		"Org1MSP": true,
		"Org2MSP": true
	}
}
Checking the commit readiness of the chaincode definition successful on peer0.org2 on channel 'mychannel'
Using organization 1
Using organization 2
+ peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile /home/xzd/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --channelID mychannel --name basic --peerAddresses localhost:7051 --tlsRootCertFiles /home/xzd/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles /home/xzd/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt --version 1.0 --sequence 1
+ res=0
2022-03-03 16:33:03.265 CST [chaincodeCmd] ClientWait -> INFO 001 txid [32a36a1aedd6e72465db7f8fcbc657e63049e21070e5a74eb508716882ac4fad] committed with status (VALID) at localhost:7051
2022-03-03 16:33:03.339 CST [chaincodeCmd] ClientWait -> INFO 002 txid [32a36a1aedd6e72465db7f8fcbc657e63049e21070e5a74eb508716882ac4fad] committed with status (VALID) at localhost:9051
Chaincode definition committed on channel 'mychannel'
Using organization 1
Querying chaincode definition on peer0.org1 on channel 'mychannel'...
Attempting to Query committed status on peer0.org1, Retry after 3 seconds.
+ peer lifecycle chaincode querycommitted --channelID mychannel --name basic
+ res=0
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]
Query chaincode definition successful on peer0.org1 on channel 'mychannel'
Using organization 2
Querying chaincode definition on peer0.org2 on channel 'mychannel'...
Attempting to Query committed status on peer0.org2, Retry after 3 seconds.
+ peer lifecycle chaincode querycommitted --channelID mychannel --name basic
+ res=0
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]
Query chaincode definition successful on peer0.org2 on channel 'mychannel'
Chaincode initialization is not required

与网络进行交互:

当测试网络成功启动之后,可以利用命令行的peer命令与网络交互,它允许用户调用部署好的智能合约,更新channel或者安装和部署新的智能合约。

首先要设置一下,在bin目录下有peer等二进制文件,在config目录下有若干yaml文件,如果需要的话,可以使用如下命令将peer等添加到环境变量中:

export PATH=<path to download location>/bin:$PATH

我的地址是:

export PATH=~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/bin:$PATH

设置了环境变量之后,前往test-network文件夹中

先保证你要在test-network中:配置临时变量:

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

此外还需要配置一下config所在的目录。

export FABRIC_CFG_PATH=$PWD/../config/

设置如下的环境变量,可以使得你作为Org1来操作peer命令。

# Environment variables 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

也可以作为Org2来操作peer命令,任选一个即可!我这里选择org1,这一段后面会用到暂时不用添加!!

# Environment variables 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

 CORE_PEER_TLS_ROOTCERT_FILECORE_PEER_MSPCONFIGPATH两行指明了Org1的加密所用的信息。

可以使用如下的命令来查看channel的账本中的汽车列表,这个列表是刚才使用deployCC参数时添加的。

查看fabcar链码:

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

调用fabcar链码:

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 \
--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 mychannel \
-n fabcar \
-c '{"function":"InitLedger","Args":[]}'

 出现"Chaincode invoke successful. result: status:200 "的字样,更新成功!

之后我们用org2来执行peer命令进行查询:

# Environment variables 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

然后可以使用如下命令来查看CAR9在账本中的情况。

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

 可以看到车辆已经被转移给了Dave,即刚才的更新成功了。

其他操作(转载 未尝试)

创建新的通道
// 生成通道配置文件
$ configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/testchannel.tx -channelID testchannel  -configPath ./configtx/

// 生成org1的锚节点配置文件
$ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID testchannel -asOrg Org1MSP -configPath ./configtx/

// 生成org2的锚节点配置文件
$ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID testchannel -asOrg Org2MSP -configPath ./configtx/

// 创建通道
$ peer channel create -o localhost:7050 -c testchannel --ordererTLSHostnameOverride orderer.example.com \
-f ./channel-artifacts/testchannel.tx \
--outputBlock ./channel-artifacts/testchannel.block \
--tls --cafile /home/verayy/codes/go/src/github.com/hyperledger/fabric-samples/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

// 每个org加入通道,需要切换上述的配置文件
$ peer channel join -b ./channel-artifacts/testchannel.block

// 使用每个org的管理员身份更新锚节点配置
$ peer channel update -o localhost:7050 --ordererTLSHostnameOverride orderer.e

————————————————
版权声明:本文为CSDN博主「shuizhongmose」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/shuizhongmose/article/details/109056691

关闭网络

当使用完测试网络的时候,可以用如下命令来关闭测试网络。

./network.sh down

这条命令会停止并移走节点和chaincode的容器,删除加密所用的信息,并且移走channel和docker卷等之前运行产生的东西,不过如果需要的话,之后可以利用up参数再次让网络运行。

启动过程详解:(转载)

  • ./network.sh为两个peer节点和一个order节点创建了证书和密钥,默认情况下,脚本会利用在organizations/cryptogen文件夹下的加密工具。
  • 脚本利用configtxgen工具创建了系统的创世块,它使用configtx/configtx.yaml文件来创建创世块,并存储在system-genesis-block文件夹中。
  • 当上述两步完成之后,./network.sh会启动网络,脚本利用在docker文件夹下的docker-compose-test-net.yaml文件创建peer和orderer节点。
  • 如果使用了createChannel子命令,脚本还会运行script文件夹下的createChannel.sh脚本来创建所需要的channel,脚本会用peer命令来创建channel,加入两个组织。
  • 如果运行了deployCC命令,脚本会在所有peers上运行script下的deployCC.sh脚本来安装fabcar chaincode,在chaincode的定义被提交到channel之后,peer命令会调用init函数来初始化chaincode,并将所需的数据放入chaincode中。

         

举报

相关推荐

0 条评论