組織間可以使用私有資料進行交易操作之前,所有的組織需要創建集合定義JSON檔案,從而定義與合約匹配的私有資料集合,存盤在私有資料集合中的資料僅會分發給確切組織的節點,而不是通道的所有成員,集合定義檔案描述了組織可以讀寫的所有的私有資料集合,
每一個私有資料集合都包含以下幾種屬性:
- name:集合名稱
- policy:定義了允許持久化資料的組織節點
- requiredPeerCount:分發私有資料要求的對等節點數量,也作為合約背書的條件
- maxPeerCount:對于資料冗余的目的,當前要向其分發資料的其他節點的數量,當私有資料拉取請求發過來,如果背書節點掛掉,其他的節點在提交的時候還可用,
- blockToLive:對于價格或者個人資訊等敏感資訊,這個引數代表了這些資料應該在私有資料集合中存在時間,資料將會存在于私有資料庫指定的區塊數量中,在這之后資料就會被清洗,從網路中淘汰,為了保持私有資料一直存在的方法是永不清洗私有資料,設定 屬性blockToLive的值為0,
- memberOnlyRead:值為true代表了節點自動強制只有屬于集合成員的組織的客戶端被允許訪問讀取私有資料,
- memberOnlyWrite:true代表節點自定強制只有屬于集合成員的組織客戶端允許寫私有資料,
- endorsementPolicy:定義了需要滿足的背書策略來寫私有資料集,集合級的背書策略會覆寫合約級的背書策略,
這份相同的定義檔案需要部署到所有使用合約的組織,就算組織不屬于任何集合,集合會在集合檔案中明確定義出來,每個組織可以訪問他們節點上確定的集合,集合只能被指定的組織訪問,
私有資料資產轉移的案例包含了 collections_config.json檔案,定義了3個私有資料集合定義:assetCollection、Org1MSPPrivateCollection,和Org2MSPPrivateCollection,
[
{
"name": "assetCollection",
"policy": "OR('Org1MSP.member', 'Org2MSP.member')",
"requiredPeerCount": 1,
"maxPeerCount": 1,
"blockToLive":1000000,
"memberOnlyRead": true,
"memberOnlyWrite": true
},
{
"name": "Org1MSPPrivateCollection",
"policy": "OR('Org1MSP.member')",
"requiredPeerCount": 0,
"maxPeerCount": 1,
"blockToLive":3,
"memberOnlyRead": true,
"memberOnlyWrite": false,
"endorsementPolicy": {
"signaturePolicy": "OR('Org1MSP.member')"
}
},
{
"name": "Org2MSPPrivateCollection",
"policy": "OR('Org2MSP.member')",
"requiredPeerCount": 0,
"maxPeerCount": 1,
"blockToLive":3,
"memberOnlyRead": true,
"memberOnlyWrite": false,
"endorsementPolicy": {
"signaturePolicy": "OR('Org2MSP.member')"
}
}
]
私有資料的使用案例:
1、base64格式化編碼私有資料引數
export private_create=$(echo -n "{\"assetID\":\"10004\" , \"objectType\":\"1\" , \"color\" : \"red\" , \"appraisedValue\" : 1000 , \"size\":100 }" | base64 | tr -d \\n)
2、安裝智能合約
peer lifecycle chaincode install /opt/gopath/src/github.com/chaincode/private.tar.gz
3、配置合約引數ID
export CC_PACKAGE_ID=private:939ab0bae5f72a707fffac97f38600db7d4fb116d3d7c45bc0be2082c58f33c0
4、匯入私有引數的配置路徑
export CC_COLL_CONFIG=/opt/gopath/src/github.com/chaincode/collections_config.json
5、節點授權安裝合約
peer lifecycle chaincode approveformyorg --signature-policy "OR('Org1MSP.member','Org2MSP.member','Org3MSP.member')" -o orderer.xxx.com:7050 --channelID mychannel --name private --version 1.0 --package-id $CC_PACKAGE_ID --sequence 10 --collections-config $CC_COLL_CONFIG --tls --cafile $ORDERER_TLSCA
6、提交合約
peer lifecycle chaincode commit --signature-policy "OR('Org1MSP.member','Org2MSP.member','Org3MSP.member')" -o orderer.xxx.com:7050 --channelID mychannel --name private --version 1.0 --sequence 11 --tls --cafile $ORDERER_TLSCA --collections-config $CC_COLL_CONFIG --peerAddresses peer0.org1.xxx.com:7051 --tlsRootCertFiles ${PWD}/config/crypto-config/peerOrganizations/org1.xxx.com/peers/peer0.org1.xxx.com/tls/ca.crt --peerAddresses peer0.org2.xxx.com:7051 --tlsRootCertFiles ${PWD}/config/crypto-config/peerOrganizations/org2.xxx.com/peers/peer0.org2.xxx.com/tls/ca.crt --peerAddresses peer0.org3.xxx.com:7051 --tlsRootCertFiles ${PWD}/config/crypto-config/peerOrganizations/org3.xxx.com/peers/peer0.org3.xxx.com/tls/ca.crt
7、通過以上引數的設定好后,呼叫合約的命令就會非常簡潔
peer chaincode invoke -o orderer.xxx.com:7050 --tls --cafile $ORDERER_TLSCA -C mychannel -n private -c '{"Args":["CreateAsset"]}' --transient "{\"asset_properties\":\"$private_create\"}"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/241895.html
標籤:區塊鏈
上一篇:Wannacry(永恒之藍)病毒
下一篇:零基礎 學 零知識證明
