超级账本1.2版本--官网翻译(Using Private Data in Fabric)-part1

Use the chaincode API GetPrivateData() to query private data in the
database. GetPrivateData() takes two arguments, the collection name
and the data key. Recall the collection collectionMarbles allows members of
Org1 and Org2 to have the private data in a side database, and the collection
collectionMarblePrivateDetails allows only members of Org1 to have the
private data in a side database. For implementation details refer to the
following two marbles private data functions <https://github.com/hyperledger/fabric-samples/blob/master/chaincode/marbles02_private/go/marbles_chaincode_private.go>__:

使用链码API GetPrivateData() 在数据库中查询私有数据。 GetPrivateData()需要两个参数,集合名称和数据的键。

  • readMarble for querying the values of the name, color, size and owner attributes
  • readMarble 用于查询名称,颜色,大小和所有者属性的值。
  • readMarblePrivateDetails for querying the values of the price attribute
  • readMarblePrivateDetails 用于查询价格的值。
    When we issue the database queries using the peer commands later in this tutorial,
    we will call these two functions.


Writing private data-写入私有数据

Use the chaincode API PutPrivateData() to store the private data
into the private database. The API also requires the name of the collection.
Since the marbles private data sample includes two different collections, it is called
twice in the chaincode:


  1. Write the private data name, color, size and owner using the
    collection named collectionMarbles.

    使用集合名称collectionMarbles写入私有数据 name, color, size and owner.

  2. Write the private data price using the collection named

使用集合名称collectionMarblePrivateDetails写入私有数据 price.

For example, in the following snippet of the initMarble function,
PutPrivateData() is called twice, once for each set of private data.


… code-block:: GO

// ==== Create marble object and marshal to JSON ====
objectType := “marble”
marble := &marble{objectType, marbleName, color, size, owner}
marbleJSONasBytes, err := json.Marshal(marble)
if err != nil {
return shim.Error(err.Error())
//Alternatively, build the marble json string manually if you don’t want to use struct marshalling
//marbleJSONasString := {"docType":"Marble", "name": " + marbleName + ", "color": " + color + ", "size": + strconv.Itoa(size) + , "owner": " + owner + "}
//marbleJSONasBytes := []byte(str)

// === Save marble to state ===
err = stub.PutPrivateData("collectionMarbles", marbleName, marbleJSONasBytes)
if err != nil {
	return shim.Error(err.Error())

// ==== Save marble private details ====
objectType = "marblePrivateDetails"
marblePrivateDetails := &marblePrivateDetails{objectType, marbleName, price}
marblePrivateDetailsBytes, err := json.Marshal(marblePrivateDetails)
if err != nil {
	return shim.Error(err.Error())
err = stub.PutPrivateData("collectionMarblePrivateDetails", marbleName, marblePrivateDetailsBytes)
if err != nil {
	return shim.Error(err.Error())


To summarize, the policy definition above for our collection.json
allows all peers in Org1 and Org2 can store and transact (endorse, commit,
query) with the marbles private data name, color, size, owner in their
private database. But only peers in Org1 can can store and transact with
the price private data in an additional private database.

总而言之,上面在collection.json中定义的策略允许Org1和Org2中的所有peer都可以在其私有数据库中存储和交易(认可,提交,查询)marbles私有数据名称,颜色,大小,所有者。 但只有Org1中的peer可以在另外的私有数据库中存储和交易价格私有数据。

As an additional data privacy benefit, since a collection is being used,
only the private data hashes go through orderer, not the private data itself,
keeping private data confidential from orderer.


Start the network-启动网络

Now we are ready to step through some commands which demonstrate using private


:guilabel:Try it yourself

Before installing and instantiating the marbles private data chaincode below,
we need to start the BYFN network. For the sake of this tutorial, we want to
operate from a known initial state. The following command will kill any active
or stale docker containers and remove previously generated artifacts.
Therefore let’s run the following command to clean up any previous


… code:: bash

cd fabric-samples/first-network
./byfn.sh -m down

Start up the BYFN network with CouchDB by running the following command:


… code:: bash

./byfn.sh up -c mychannel -s couchdb

This will create a simple Fabric network consisting of a single channel named
mychannel with two organizations (each maintaining two peer nodes) and an
ordering service while using CouchDB as the state database. Either LevelDB
or CouchDB may be used with collections. CouchDB was chosen to demonstrate
how to use indexes with private data.

这将创建一个简单的Fabric网络,该网络由一个名为mychannel的通道组成,其中包含两个组织(每个组织维护两个peer节点)和一个orderer服务,同时使用CouchDB作为状态数据库。LevelDB或CouchDB可以与集合一起使用。 选择CouchDB来演示如何将索引与私有数据一起使用。

… note:: For collections to work, it is important to have cross organizational
gossip configured correctly. Refer to our documentation on :doc:gossip,
paying particular attention to the section on “anchor peers”. Our tutorial
does not focus on gossip given it is already configured in the BYFN sample,
but when configuring a channel, the gossip anchors peers are critical to
configure for collections to work properly.

注意:要使集合起作用,必须正确配置跨组织的gossip。 请参阅我们的文档:doc:gossip。特别注意“anchor peers”的章节部分。我们的教程并没有关注gossip,因为它已经在BYFN例子中配置了。但是当配置一个通道的时候, gossip anchors peers对于配置数据集以使其正常工作至关重要。

… _pd-install-instantiate_cc:

Install and instantiate chaincode with a collection-使用集合安装和初始化链码

Client applications interact with the blockchain ledger through chaincode. As
such we need to install and instantiate the chaincode on every peer that will
execute and endorse our transactions. Chaincode is installed onto a peer and
then instantiated onto the channel using :doc:peer-commands.


Install chaincode on all peers - 安装链码到所有的peers

As discussed above, the BYFN network includes two organizations, Org1 and Org2,
with two peers each. Therefore the chaincode has to be installed on four peers:


Use the peer chaincode install <http://hyperledger-fabric.readthedocs.io/en/master/commands/peerchaincode.html?%20chaincode%20instantiate#peer-chaincode-install>__ command to install the Marbles chaincode on each peer.

使用 peer chaincode install <http://hyperledger-fabric.readthedocs.io/en/master/commands/peerchaincode.html?%20chaincode%20instantiate#peer-chaincode-install> 命令在每个peer上安装Marbles链码。

:guilabel:Try it yourself

Assuming you have started the BYFN network, enter the CLI container.


… code:: bash

docker exec -it cli bash

Your command prompt will change to something similar to:


[email protected]:/opt/gopath/src/github.com/hyperledger/fabric/peer#

  1. Use the following command to install the Marbles chaincode from the git
    repository onto the peer peer0.org1.example.com in your BYFN network.
    (By default, after starting the BYFN network, the active peer is set to:

    使用下面的命令,安装Marbles链码从git仓库到BYFN网络的peer peer0.org1.example.com节点,

    … code:: bash

    peer chaincode install -n marblesp -v 1.0 -p github.com/chaincode/marbles02_private/go/

    When it is complete you should see something similar to:


    … code:: bash

    install -> INFO 003 Installed remotely response:<status:200 payload:“OK” >

  2. Use the CLI to switch the active peer to the second peer in Org1 and
    install the chaincode. Copy and paste the following entire block of
    commands into the CLI container and run them.


    … code:: bash

    export CORE_PEER_ADDRESS=peer1.org1.example.com:7051
    peer chaincode install -n marblesp -v 1.0 -p github.com/chaincode/marbles02_private/go/

  3. Use the CLI to switch to Org2. Copy and paste the following block of
    commands as a group into the peer container and run them all at once.


    … code:: bash

    export PEER0_ORG2_CA=/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/[email protected]/msp

  4. Switch the active peer to the first peer in Org2 and install the chaincode:


.. code:: bash

   export CORE_PEER_ADDRESS=peer0.org2.example.com:7051
   peer chaincode install -n marblesp -v 1.0 -p github.com/chaincode/marbles02_private/go/
  1. Switch the active peer to the second peer in org2 and install the chaincode:


.. code:: bash

   export CORE_PEER_ADDRESS=peer1.org2.example.com:7051
   peer chaincode install -n marblesp -v 1.0 -p github.com/chaincode/marbles02_private/go/