fabric里的marble私有数据访问案例
Posted sanqima
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了fabric里的marble私有数据访问案例相关的知识,希望对你有一定的参考价值。
fabric里的marble案例,是一个关于弹珠私有数据的存储与访问的案例,弹珠的定义如下:
{“color”:“blue”,“docType”:“marble”,“name”:“marble1”,“owner”:“tom”,“size”:35}
{“docType”:“marblePrivateDetails”,“name”:“marble1”,“price”:99}
// 边侧数据库A,Org1、Org2的普通节点都可以访问
type marble struct {
ObjectType string `json:"docType"`
Name string `json:"name"`
Color string `json:"color"`
Size int `json:"size"`
Owner string `json:"owner"`
}
// 边侧数据库B,只有Org1里的普通节点可以访问
type marblePrivateDetails struct {
ObjectType string `json:"docType"`
Name string `json:"name"`
Price int `json:"price"`
}
私有数据的特定权限,定义如下:
- name, color, size, and owner 通道中所有成员可见(Org1 and Org2)
- price 只有 Org1 的成员可见
- readMarble() 用于查询 name, color, size and owner 属性的值,即查询边侧数据库A的记录
- readMarblePrivateDetails() 用于查询 price 属性的值,即查询边侧数据库B的记录
边侧数据库,权限定义配置文件: collections_config.json
// collections_config.json
[
{
"name": "collectionMarbles", //边侧数据库A
"policy": "OR('Org1MSP.member', 'Org2MSP.member')", //Org1、Org2都可以访问
"requiredPeerCount": 0,
"maxPeerCount": 3,
"blockToLive":1000000, //私有数据自毁时间:100万个区块高度之后,只保留hash值
"memberOnlyRead": true
},
{
"name": "collectionMarblePrivateDetails", //边侧数据库B
"policy": "OR('Org1MSP.member')", //只有Org1才可以访问
"requiredPeerCount": 0,
"maxPeerCount": 3,
"blockToLive":3, //私有数据自毁时间:3个区块高度之后,只保留hash值
"memberOnlyRead": true
}
]
使用PutPrivateData()函数,将私有数据写入到边侧数据库A、B;使用GetPrivateData()函数,从边侧数据库A、B里读取私有数据。下面介绍,marble案例的使用方法。
1、清空原有fabric网络
## 关闭网络
cd fabric-samples/first-network
./byfn.sh down
## 清空docker缓存数据
docker rm -f $(docker ps -a | awk '($2 ~ /dev-peer.*.marblesp.*/) {print $1}')
docker rmi -f $(docker images | awk '($1 ~ /dev-peer.*.marblesp.*/) {print $3}')
2、启动有couchDB的BYFN网络
./byfn.sh up -c mychannel -s couchdb
3、准备4个.sh脚本
BYFN是1order+4Peer网络架构,这4个Peer在一台主机上,它们共有一个cli容器,但4个Peer的环境是不同的,所以每次切换Peer时,环境变量也需要进行切换。
4个Peer: peer01、peer11、peer02、peer12
节点 | 脚本 | 含义 |
---|---|---|
peer0.org1 | mar_peer01.sh | org1的peer0节点环境变量脚本 |
peer1.org1 | mar_peer11.sh | org1的peer1节点环境变量脚本 |
peer0.org2 | mar_peer02.sh | org2的peer0节点环境变量脚本 |
peer1.org2 | mar_peer12.sh | org2的peer1节点环境变量脚本 |
mar_peer01.sh、mar_peer11.sh、mar_peer02.sh、mar_peer12.sh保存到fabric-samples/first-network/channel-artifacts目录下,如图(1)所示。
3.1 peer0.org1脚本
//mar_peer01.sh
#!/bin/bash
##peer01.sh
export CORE_PEER_ADDRESS=peer0.org1.example.com:7051
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 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
3.2 peer1.org1脚本
//mar_peer11.sh
#!/bin/bash
##peer11.sh
export CORE_PEER_ADDRESS=peer1.org1.example.com:8051
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/peer1.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 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
3.3 peer0.org2脚本
//mar_peer0.org2
#!/bin/bash
##peer02.sh
export CORE_PEER_ADDRESS=peer0.org2.example.com:9051
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 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
3.4 peer1.org2脚本
//mar_peer12.sh
#!/bin/bash
##peer12.sh
export CORE_PEER_ADDRESS=peer1.org2.example.com:10051
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/peer1.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 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
4、进入cli容器,并安装链码
4.1 进入cli容器
docker exec -it cli bash
4.2 安装链码
## 切换到peer01
source channel-artifacts/mar_peer01.sh
peer chaincode install -n marblesp -v 1.0 -p github.com/chaincode/marbles02_private/go/
## 切换到peer11
source channel-artifacts/mar_peer11.sh
peer chaincode install -n marblesp -v 1.0 -p github.com/chaincode/marbles02_private/go/
## 切换到peer02
source channel-artifacts/mar_peer02.sh
peer chaincode install -n marblesp -v 1.0 -p github.com/chaincode/marbles02_private/go/
## 切换到peer12
source channel-artifacts/mar_peer12.sh
peer chaincode install -n marblesp -v 1.0 -p github.com/chaincode/marbles02_private/go/
5、初始化链码
在一个通道中,链码只需要初始化一次即可,这里选择在peer0.org1上初始化该链码
source channel-artifacts/mar_peer01.sh
export Mar_Conf=$GOPATH/src/github.com/chaincode/marbles02_private/collections_config.json
peer chaincode instantiate -o orderer.example.com:7050 --tls --cafile $ORDERER_CA -C mychannel -n marblesp -v 1.0 -c '{"Args":["init"]}' -P "OR('Org1MSP.member','Org2MSP.member')" --collections-config $Mar_Conf
6、保存私有化数据,并查看
6.1 保存数据
export MARBLE=$(echo -n "{\\"name\\":\\"marble1\\",\\"color\\":\\"blue\\",\\"size\\":35,\\"owner\\":\\"tom\\",\\"price\\":99}" | base64 | tr -d \\\\n)
peer chaincode invoke -o orderer.example.com:7050 --tls --cafile $ORDERER_CA -C mychannel -n marblesp -c '{"Args":["initMarble"]}' --transient "{\\"marble\\":\\"$MARBLE\\"}"
6.2 在peer0.org1里,查看数据
peer chaincode query -C mychannel -n marblesp -c '{"Args":["readMarble","marble1"]}'
peer chaincode query -C mychannel -n marblesp -c '{"Args":["readMarblePrivateDetails","marble1"]}'
可以看到,Org1的节点peer0,可以访问边侧数据库A的name、color、size等数据,也可以访问边侧数据库B的price数据,即它遵循了文章开头的私有数据权限定义。
6.2 在peer0.org2里,查看数据
source channel-artifacts/mar_peer02.sh
peer chaincode query -C mychannel -n marblesp -c '{"Args":["readMarble","marble1"]}'
peer chaincode query -C mychannel -n marblesp -c '{"Args":["readMarblePrivateDetails","marble1"]}'
可以看到,Org2的节点peer0,可以访问边侧数据库A的name、color、size等数据,但无法访问边侧数据库B的price数据,即它遵循了文章开头的私有数据权限定义。
7、更换marble的所属人
将marble1的所属人由tom改成Alice,即tom把资产marble1转交给Alice。
## 切换到peer01
source channel-artifacts/mar_peer01.sh
peer chaincode query -C mychannel -n marblesp -c '{"Args":["readMarble","marble1"]}'
## 更换marble1资产的所属人
export MARBLE_OWNER=$(echo -n "{\\"name\\":\\"marble1\\",\\"color\\":\\"red\\",\\"size\\":35,\\"owner\\":\\"Alice\\",\\"price\\":99}" | base64 | tr -d \\\\n)
peer chaincode invoke -o orderer.example.com:7050 --tls --cafile $ORDERER_CA -C mychannel -n marblesp -c '{"Args":["transferMarble"]}' --transient "{\\"marble_owner\\":\\"$MARBLE_OWNER\\"}"
以上是关于fabric里的marble私有数据访问案例的主要内容,如果未能解决你的问题,请参考以下文章