基于test-network网络,clone fabric-sample ,进入test-work文件夹进行测试。
test-network使用
通道创建和加入脚本解析
新的链码生命周期脚本解析
隐私数据
test-network使用
Fabric的流程:生成证书、创世块,启动网络,创建创建通道,安装链码。
生成证书
1 2
| cryptogen generate --config=./organizations/cryptogen/crypto-config-org1.yaml --output="organizations"
|
生成区块
1 2
| configtxgen -profile TwoOrgsOrdererGenesis -channelID system-channel -outputBlock ./system-genesis-block/genesis.block
|
网络启动
不用脚本就通过docker-compose启动各个容器。
创建channel
1 2
| ./network.sh createChannel
|
使用创建通道脚本
安装链码
使用链码部署脚本进行安装
通道创建和加入
创建channel
创建channel脚本 scripts/createChannel.sh
创建通道tx文件
1 2 3 4 5 6
| configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/${CHANNEL_NAME}.tx -channelID $CHANNEL_NAME
|
1 2
| configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/${orgmsp}anchors.tx -channelID $CHANNEL_NAME -asOrg ${orgmsp}
|
创建通道
1 2 3 4 5 6 7 8 9 10 11 12
| peer channel create -o localhost:7050 -c $CHANNEL_NAME --ordererTLSHostnameOverride orderer.example.com -f ./channel-artifacts/${CHANNEL_NAME}.tx --outputBlock ./channel-artifacts/${CHANNEL_NAME}.block --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA
peer channel create -o orderer.example.com:7050 -c succhannel --ordererTLSHostnameOverride orderer.example.com -f ./channel-artifacts/succhannel.tx --outputBlock ./channel-artifacts/succhannel.block --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem >&createChannel.log
peer channel create -o localhost:7050 -c mychannel -f ./channel-artifacts/mychannel.tx --outputBlock ./channel-artifacts/mychannel.block
|
加入通道
1 2
| peer channel join -b ./channel-artifacts/succhannel.block >&log.txt
|
更新锚节点
1 2 3 4 5
| peer channel update -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx >&log.txt
peer channel update -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA
|
新的链码管理
提出lifecycle指令对链码的部署进行管理
多个组织必须审批链码通过后,才能进行链码提交到帐本中
链码提交到账本的过程:打包、安装、审批、提交。(升级过程和安装启动过程一样)
链码部署
一键打包脚本 scripts/deployCC.sh
打包
1 2 3 4
| peer lifecycle chaincode package fabcar.tar.gz --path ../../fabric-samples/chaincode/fabcar/go/ --lang golang --label fabcar_1 >&package.log
|
1 2 3
| # 出failed to normalize chaincode path: 'go list' 错误的解决办法 go env -w GO111MODULE=on go env -w GOPROXY=https://goproxy.cn,direct
|
安装
1 2
| peer lifecycle chaincode install fabcar.tar.gz >&log.txt
|
审批
审批—配置链码定义
定义包括名字,版本,包背书策略,签名策略,隐私数据配置等。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| peer lifecycle chaincode approveformyorg -o orderer.example.com:7050 --channelID $CHANNEL_NAME --name fabcar --version ${VERSION} --init-required --package-id ${PACKAGE_ID} --sequence ${VERSION} --waitForEvent >&log.txt
--signature-policy "OR('Org1MSP.member','Org2MSP.member')" --channel-config-policy "OR('Org1MSP.member','Org2MSP.member')"
/* export ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
export CORE_PEER_LOCALMSPID="Org1MSP" export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp export CORE_PEER_ADDRESS=peer0.org1.example.com:7051
peer lifecycle chaincode approveformyorg -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --peerAddresses peer0.org1.example.com:7051 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/tlscacerts/tls-localhost-7054-ca-org1.pem --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/fabric-ca/org1/tls-cert.pem --channelID succhannel --name fabcartest1 --version 1 --init-required --package-id fabcar_1:aca5cf806d53b8860e3bc4234af8fe9a017e7082d98108e07ffc7ea36dbc2e07 --sequence 1 --signature-policy "OR('Org1MSP.member','Org2MSP.member')"
peer lifecycle chaincode approveformyorg -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/fabric-ca/org1/tls-cert.pem --channelID succhannel --name fabcartest1 --version 1 --init-required --package-id fabcar_1:aca5cf806d53b8860e3bc4234af8fe9a017e7082d98108e07ffc7ea36dbc2e07 --sequence 1 --signature-policy "OR('Org1MSP.member','Org2MSP.member')"
*/
export CORE_PEER_LOCALMSPID="Org2MSP" export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp export CORE_PEER_ADDRESS=peer0.org2.example.com:9051
|
审批—检查能否提交
1 2
| peer lifecycle chaincode checkcommitreadiness --channelID succhannel --name fabcar --version 1 --sequence 1 --output json --init-required
|
提交
1 2 3 4 5 6 7
| peer lifecycle chaincode commit -o localhost:7050 --channelID mychannel1 --name fabcar $PEER_CONN_PARMS --version 1.0 --sequence 1 --init-required
peer lifecycle chaincode commit -o orderer.example.com:7050 --channelID mychannel1 --name fabcar --version 1.0 --sequence 1 --init-required --tls --cafile $ORDERER_CA --peerAddresses peer0.org1.example.com:7051
peer lifecycle chaincode commit -o orderer.example.com:7050 --ordererTLSHostnameOverride orderer.example.com --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --channelID mychannel1 --name fabcar1 --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt --version 1.0 --sequence 1 --init-required
|
隐私数据
隐私数据集合定义:
一个集合定义包含了一个或者多个集合,每个集合具有一个策略列出了在集合中的所有组织,还包括用来控制在背书阶段私有数据的传播所使用的属性,另外还有一个可选项来决定数据是否会被删除。
在2.0版本,隐私数据集合将由channel成员进行定义审批条件,当链码定义被提交到通道时部署隐私数据集合。
配置文件内容示意:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| [ { "name": "collectionMarblePrivateDetails", "policy": "OR('Org1MSP.member')", "requiredPeerCount": 0, "maxPeerCount": 3, "blockToLive":3, "memberOnlyRead": true, "memberOnlyWrite":true, "endorsementPolicy": { "signaturePolicy": "OR('Org1MSP.member')" } } ]
|
API中隐私数据的存取
1 2
| PutPrivateData(collection,key,value) GetPrivateData(collection,key)
|