超级账本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.

当我们在本教程后面使用peer命令发出数据库查询时,我们将调用这两个函数。

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:

通过链码APIPutPrivateData()来把私有数据些人私有数据库。这个API同事还要求提供数据集的名称。由于marbles私有数据例中包含了两个不同的数据集,所在它在链码中被调用两次:

  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.

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

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

举例来说,在initMarble函数的以下片段中,PutPrivateData()被调用两次,每组私有数据集一次。

… 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.

作为一个另外的私有数据的优势,既然一个数据集被使用,只有那个数据集的hash通过orderer,而不是数据集本身,从而使私有数据对orderer保密。

Start the network-启动网络

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

现在我们准备使用私有数据的命令逐步完成一些演示。

: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
environments:

在下面安装和实例化marbles私有数据链代码之前,我们需要启动BYFN网络。为了本教程的缘故,我们希望从已知的初始状态开始操作。以下命令将终止所有活动或过时的docker容器并删除以前生成的构件。因此,让我们运行以下命令来清理以前的所有环境:

… code:: bash

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

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

使用一下命令启动一个使用CouchDB的BYFN网络:

… 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.

客户端通过链码和区块链账本交互。因此我们需要在我们将处理和背书我们的交易的peer节点安装和初始化链码。链码安装在peer节点然后使用: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:

就像上面讨论的一样,BYFN网络包含了两个组织,Org1和Org2,每个组织包含两个peer节点。因此,链码必须安装在四个peer上:

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.

假设您已经启动了BYFN网络,进入CLI容器。

… 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:
    CORE_PEER_ADDRESS=peer0.org1.example.com:7051):

    使用下面的命令,安装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.

    使用CLI去切换到Org组织的第二个活跃的peer安装链码。将以下整个命令块复制并粘贴到CLI容器中并运行它们。

    … 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.

    使用CLI切换到Org2。将以下命令块作为一个组复制并粘贴到peer容器中,并立即运行它们。

    … code:: bash

    export CORE_PEER_LOCALMSPID=Org2MSP
    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_TLS_ROOTCERT_FILE=$PEER0_ORG2_CA
    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:

切换到Org2中的第一个活跃节点然后安装链码:

.. 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:

切换到Org2中的第二个活跃节点然后安装链码:

.. 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/