Skip to content

Commit

Permalink
[FAB-5080] Chaincode API Support for PrivateData
Browse files Browse the repository at this point in the history
This CR adds the following 6 chaincode APIs which enable chaincode
to acess/modify the partitioned private data. A collection name denotes
a partition. These will only be included in experimental builds.
1. GetPrivateData(collection, key)
2. PutPrivateData(collection, key, value)
3. DelPrivateData(collection, key)
4. GetPrivateDataByRange(collection, startKey, endKey)
5. GetPrivateDataByPartialCompositeKey(collection, objectType, []keys)
6. GetPrivateDataQueryResult(collection, query)

Changes have been made in both shim and peer side. For APIs 4 to 6,
ledger support is not added yet.

Enabled only TestQueries/TestQueriesPrivateData functions in
exectransaction_test.go to perform end-to-end test (chaincode
to ledger).  Earlier, all test functions in exectransaction_test.go
was disabled as it was taking a lot of time to execute.
Currently, TestQueries/TestQueriesPrivateData take less than
15 seconds and hence can be enabled for e2e testing

Also needed to modify core/chaincode/platforms/golang/platform.go
in order to support building chaincode with the experimental tag.

Change-Id: I6b026f0628c5c808776f05852c7910f60f4a99c4
Signed-off-by: senthil <cendhu@gmail.com>
Signed-off-by: David Enyeart <enyeart@us.ibm.com>
Signed-off-by: Gari Singh <gari.r.singh@gmail.com>
  • Loading branch information
cendhu authored and d1vyank committed Oct 16, 2017
1 parent 9227a52 commit 16a92d5
Show file tree
Hide file tree
Showing 17 changed files with 1,760 additions and 385 deletions.
146 changes: 107 additions & 39 deletions core/chaincode/chaincode_support_test.go

Large diffs are not rendered by default.

57 changes: 36 additions & 21 deletions core/chaincode/exectransaction_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Copyright IBM Corp. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

package chaincode
Expand Down Expand Up @@ -86,6 +76,9 @@ func initPeer(chainIDs ...string) (net.Listener, error) {

peer.MockSetMSPIDGetter(mspGetter)

// For unit-test, tls is not required.
viper.Set("peer.tls.enabled", false)

var opts []grpc.ServerOption
if viper.GetBool("peer.tls.enabled") {
creds, err := credentials.NewServerTLSFromFile(config.GetPath("peer.tls.cert.file"), config.GetPath("peer.tls.key.file"))
Expand Down Expand Up @@ -281,9 +274,30 @@ func endTxSimulation(chainID string, ccid *pb.ChaincodeID, txsim ledger.TxSimula
//see comment on _commitLock_
_commitLock_.Lock()
defer _commitLock_.Unlock()
if err := lgr.CommitWithPvtData(&ledger.BlockAndPvtData{
Block: block,
}); err != nil {

blockAndPvtData := &ledger.BlockAndPvtData{
Block: block,
BlockPvtData: make(map[uint64]*ledger.TxPvtData),
}

// All tests are performed with just one transaction in a block.
// Hence, we can simiplify the procedure of constructing the
// block with private data. There is not enough need to
// add more than one transaction in a block for testing chaincode
// API.

// ASSUMPTION: Only one transaction in a block.
seqInBlock := uint64(0)

if txSimulationResults.PvtSimulationResults != nil {

blockAndPvtData.BlockPvtData[seqInBlock] = &ledger.TxPvtData{
SeqInBlock: seqInBlock,
WriteSet: txSimulationResults.PvtSimulationResults,
}
}

if err := lgr.CommitWithPvtData(blockAndPvtData); err != nil {
return err
}
}
Expand Down Expand Up @@ -364,6 +378,7 @@ func deploy2(ctx context.Context, cccid *ccprovider.CCContext, chaincodeDeployme
if _, _, err = ExecuteWithErrorFilter(ctx, lsccid, cis); err != nil {
return nil, fmt.Errorf("Error deploying chaincode (1): %s", err)
}

if b, _, err = ExecuteWithErrorFilter(ctx, cccid, chaincodeDeploymentSpec); err != nil {
return nil, fmt.Errorf("Error deploying chaincode(2): %s", err)
}
Expand Down Expand Up @@ -1072,7 +1087,8 @@ func TestChaincodeInvokeChaincodeErrorCase(t *testing.T) {

// Test the invocation of a transaction.
func TestQueries(t *testing.T) {
testForSkip(t)
// Allow queries test alone so that end to end test can be performed. It takes less than 5 seconds.
//testForSkip(t)

chainID := util.GetTestChainID()

Expand Down Expand Up @@ -1107,6 +1123,7 @@ func TestQueries(t *testing.T) {
return
}

var keys []interface{}
// Add 101 marbles for testing range queries and rich queries (for capable ledgers)
// The tests will test both range and rich queries and queries with query limits
for i := 1; i <= 101; i++ {
Expand Down Expand Up @@ -1137,6 +1154,7 @@ func TestQueries(t *testing.T) {
theChaincodeSupport.Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec})
return
}

}

//The following range query for "marble001" to "marble011" should return 10 marbles
Expand All @@ -1153,7 +1171,6 @@ func TestQueries(t *testing.T) {
return
}

var keys []interface{}
err = json.Unmarshal(retval, &keys)
if len(keys) != 10 {
t.Fail()
Expand Down Expand Up @@ -1270,7 +1287,6 @@ func TestQueries(t *testing.T) {
theChaincodeSupport.Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec})
return
}

//Reset the query limit to 5
viper.Set("ledger.state.queryLimit", 5)

Expand All @@ -1290,7 +1306,6 @@ func TestQueries(t *testing.T) {

//unmarshal the results
err = json.Unmarshal(retval, &keys)

//check to see if there are 5 values
if len(keys) != 5 {
t.Fail()
Expand Down Expand Up @@ -1401,7 +1416,7 @@ func TestQueries(t *testing.T) {
err = json.Unmarshal(retval, &history)
if len(history) != 3 {
t.Fail()
t.Logf("Error detected with the history query, should have returned 3 but returned %v", len(keys))
t.Logf("Error detected with the history query, should have returned 3 but returned %v", len(history))
theChaincodeSupport.Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec})
return
}
Expand Down Expand Up @@ -1770,7 +1785,7 @@ func TestChaincodeInitializeInitError(t *testing.T) {

var nextBlockNumber uint64

// the chaincode to install and instanciate
// the chaincode to install and instantiate
chaincodeName := generateChaincodeName(tc.chaincodeType)
chaincodePath := tc.chaincodePath
chaincodeVersion := "1.0.0.0"
Expand Down
Loading

0 comments on commit 16a92d5

Please sign in to comment.