• Fabric中的私有数据


    参考:官方文档 & 私有数据视频

    代码参考:智能合约

    为什么要使用私有数据?

    使用私有数据为的是使数据分享给我们想要共享的组织。比如在我们在网上购物,同一个商品在不同活动时期的成交价格大部分是不相同的。但是我们并不想让客户知道彼此之间的成交价格,所以便有了私有数据,比如用户a和商家在同一个通道,成交价格存储在private state,只有用户a和商家可以访问这个数据,其他用户也是同理。

    在Peer 节点上存储和检索私有数据

    如下图,通道1中有机构1和机构2两个机构,分别对应节点0和节点1,peer0和peer1上各有一个账本。

    其中,peer0节点是已经授权的节点即可以使用私用数据,而peer1是未授权节点,无法访问私有数据。peer0和peer1的账本中有都有通道数据,为的是保证账本数据的不可篡改性,并以key的hash和value的hash存储,这样就保证了通道数据的安全性。

    peer0中还存有私有数据,是明文存储,只可以由机构1访问。

     

    使用私有数据

    在使用私有数据之前需要定义一个私有数据集的JSON文件

    在通道中使用私有数据的第一步是定义集合以决定私有数据的访问权限

    该集合的定义描述了谁可以保存数据,数据要分发给多少个节点,需要多少个节点来进行数据分发,以及私有数据在私有数据库中的保存时间。

    集合定义

    这是官网提供的一个集合定义,一般配置在connection_config.json中

    1. [
    2. {
    3. "name": "collectionMarbles",
    4. "policy": "OR('Org1MSP.member', 'Org2MSP.member')",
    5. "requiredPeerCount": 0,
    6. "maxPeerCount": 3,
    7. "blockToLive":1000000,
    8. "memberOnlyRead": true
    9. },
    10. {
    11. "name": "collectionMarblePrivateDetails",
    12. "policy": "OR('Org1MSP.member')",
    13. "requiredPeerCount": 0,
    14. "maxPeerCount": 3,
    15. "blockToLive":3,
    16. "memberOnlyRead": true
    17. }
    18. ]

    集合定义由以下几个属性组成:

    • name: 集合的名称。
    • policy: 定义了哪些组织中的 Peer 节点能够存储集合数据。
    • requiredPeerCount: 私有数据要分发到的节点数,这是链码背书成功的条件之一。
    • maxPeerCount: 为了数据冗余,当前背书节点将尝试向其他节点分发数据的数量。如果当前背书节点发生故障,其他的冗余节点可以承担私有数据查询的任务。
    • blockToLive: 对于非常敏感的信息,比如价格或者个人信息,这个值代表书库可以在私有数据库中保存的时间。数据会在私有数据库中保存 blockToLive 个区块,之后就会被清除。如果要永久保留,将此值设置为 0 即可。
    • memberOnlyRead: 设置为 true 时,节点会自动强制集合中定义的成员组织内的客户端对私有数据仅拥有只读权限。

    升级合约以支持私有数据

    首先在项目中添加connection_config.json文件

    1. [
    2. {
    3. "name": "collectionOrg1Cats",
    4. "policy": "OR('Org1MSP.member')",
    5. "requiredPeerCount": 0,
    6. "maxPeerCount": 3,
    7. "blockToLive": 1000000,
    8. "memberOnlyRead": true
    9. },
    10. {
    11. "name": "collectionOrg2Cats",
    12. "policy": "OR('Org2MSP.member')",
    13. "requiredPeerCount": 0,
    14. "maxPeerCount": 3,
    15. "blockToLive": 1,
    16. "memberOnlyRead": true
    17. }
    18. ]

    在链码文件中编写操作私有数据的方法(查询,跟更新,删除等等)。

    1. @Transaction
    2. public byte[] queryPrivateCatHash(final Context ctx, final String collection ,final String key) {
    3. ChaincodeStub stub = ctx.getStub();
    4. byte[] hash = stub.getPrivateDataHash(collection, key);
    5. if (ArrayUtils.isEmpty(hash)) {
    6. String errorMessage = String.format("Private Cat %s does not exist", key);
    7. log.log(Level.WARNING , errorMessage);
    8. throw new ChaincodeException(errorMessage);
    9. }
    10. return hash;
    11. }
    12. @Transaction
    13. public PrivateCat queryPrivateCat(final Context ctx, final String collection , final String key) {
    14. ChaincodeStub stub = ctx.getStub();
    15. log.info(String.format("查询私有数据 , collection [%s] key [%s] , mspId [%s] " , collection , stub.getMspId() , key));
    16. String catState = stub.getPrivateDataUTF8(collection , key);
    17. if (StringUtils.isBlank(catState)) {
    18. String errorMessage = String.format("Private Cat %s does not exist", key);
    19. log.log(Level.WARNING , errorMessage);
    20. throw new ChaincodeException(errorMessage);
    21. }
    22. return JSON.parseObject(catState , PrivateCat.class);
    23. }
    24. @Transaction
    25. public PrivateCat createPrivateCat(final Context ctx, final String collection , final String key , String name , Integer age , String color , String breed) {
    26. ChaincodeStub stub = ctx.getStub();
    27. log.info(String.format("创建私有数据 , collection [%s] , mspId [%s] , key [%s] , name [%s] age [%s] color [%s] breed [%s] " , collection , stub.getMspId() , key , name , age , color , breed));
    28. String catState = stub.getPrivateDataUTF8(collection , key);
    29. if (StringUtils.isNotBlank(catState)) {
    30. String errorMessage = String.format("Private Cat %s already exists", key);
    31. log.log(Level.WARNING , errorMessage);
    32. throw new ChaincodeException(errorMessage);
    33. }
    34. PrivateCat cat = new PrivateCat()
    35. .setCat(new Cat().setName(name)
    36. .setAge(age)
    37. .setBreed(breed)
    38. .setColor(color))
    39. .setCollection(collection);
    40. String json = JSON.toJSONString(cat);
    41. log.info(String.format("要保存的数据 %s" , json));
    42. stub.putPrivateData(collection , key , json);
    43. return cat;
    44. }
    45. @Transaction
    46. public PrivateCat updatePrivateCat(final Context ctx, final String collection, final String key , String name , Integer age , String color , String breed) {
    47. ChaincodeStub stub = ctx.getStub();
    48. log.info(String.format("更新私有数据 , collection [%s] , mspId [%s] , key [%s] , name [%s] age [%s] color [%s] breed [%s] " , collection,stub.getMspId() , key , name , age , color , breed));
    49. String catState = stub.getPrivateDataUTF8(collection , key);
    50. if (StringUtils.isBlank(catState)) {
    51. String errorMessage = String.format("Private Cat %s does not exist", key);
    52. log.log(Level.WARNING , errorMessage);
    53. throw new ChaincodeException(errorMessage);
    54. }
    55. PrivateCat cat = new PrivateCat()
    56. .setCat(new Cat().setName(name)
    57. .setAge(age)
    58. .setBreed(breed)
    59. .setColor(color))
    60. .setCollection(collection);
    61. String json = JSON.toJSONString(cat);
    62. log.info(String.format("要保存的数据 %s" , json));
    63. stub.putPrivateData(collection , key , json);
    64. return cat;
    65. }
    66. @Transaction
    67. public PrivateCat deletePrivateCat(final Context ctx, final String collection ,final String key) {
    68. ChaincodeStub stub = ctx.getStub();
    69. log.info(String.format("删除私有数据 , collection [%s] , mspId [%s] , key [%s] " , collection , stub.getMspId() , key));
    70. String catState = stub.getPrivateDataUTF8(collection , key);
    71. if (StringUtils.isBlank(catState)) {
    72. String errorMessage = String.format("Private Cat %s does not exist", key);
    73. log.log(Level.WARNING , errorMessage);
    74. throw new ChaincodeException(errorMessage);
    75. }
    76. stub.delPrivateData(collection , key);
    77. return JSON.parseObject(catState , PrivateCat.class);
    78. }

    在Ubuntu终端更新智能合约

    首先在​​​​​​​升级智能合约中运行到3.1,紧接着运行下面的3.2.

    3.2 Org2 通过链码定义修改为

    peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name hyperledger-fabric-contract-java-demo --version 2.0 --collections-config ../chaincode/hyperledger-fabric-contract-java-demo/collections_config.json --signature-policy "OR('Org1MSP.member','Org2MSP.member')" --package-id $NEW_CC_PACKAGE_ID --sequence 2 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

    3.3 Org1 通过链码定义

    1. export CORE_PEER_LOCALMSPID="Org1MSP"
    2. export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
    3. export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
    4. export CORE_PEER_ADDRESS=localhost:7051

    用 peer lifecycle chaincode approveformyorg命令通过链码定义

    peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name hyperledger-fabric-contract-java-demo --version 2.0 --collections-config ../chaincode/hyperledger-fabric-contract-java-demo/collections_config.json --signature-policy "OR('Org1MSP.member','Org2MSP.member')" --package-id $NEW_CC_PACKAGE_ID --sequence 2 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

    3.4 将链码提交到通道

    使用peer lifecycle chaincode checkcommitreadiness命令来检查通道成员是否已批准相同的链码定义:

    peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name hyperledger-fabric-contract-java-demo --version 2.0 --collections-config ../chaincode/hyperledger-fabric-contract-java-demo/collections_config.json --signature-policy "OR('Org1MSP.member','Org2MSP.member')" --sequence 2 --tls --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 hyperledger-fabric-contract-java-demo --version 2.0 --collections-config ../chaincode/hyperledger-fabric-contract-java-demo/collections_config.json --signature-policy "OR('Org1MSP.member','Org2MSP.member')" --sequence 2 --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

    可以使用peer lifecycle chaincode querycommitted命令来确认链码定义已提交给通道。

    peer lifecycle chaincode querycommitted --channelID mychannel --name hyperledger-fabric-contract-java-demo --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
    

    查看到已经升级成功

    调用私有方法

    首先创建一个私有小猫:

    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 -C mychannel -n hyperledger-fabric-contract-java-demo --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":"createPrivateCat","Args":["collectionOrg1Cats" , "cat-0" , "tom" ,  "3" , "蓝色" , "大懒猫"]}'
    

    然后查询小猫:

    peer chaincode query -C mychannel -n hyperledger-fabric-contract-java-demo -c '{"Args":["queryPrivateCat" , "collectionOrg1Cats" , "cat-0"]}'
    

    查询结果如下:

  • 相关阅读:
    云原生之容器化:3、Kubeadm部署Kubernetes
    前端工作总结195-vue带参数跳转其他页面
    决策树——依据水果特征分类
    第六章 dubbo接口测试
    北斗+5G 织就精确定位的“天罗地网”
    “人生苦短,我用Python“——身份认证攻击
    Pico-I / O嵌入式模块提供48点数字I / O接口
    Mysql 学习(十 三)InnoDB的BufferPool
    十大排序算法详解-上篇:比较排序算法【python 动态图解】
    mina通讯替代socket通讯的使用
  • 原文地址:https://blog.csdn.net/wan_ide/article/details/125894374