Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit 8044bac

Browse files
Amol Pednekarcr22rc
authored andcommitted
[FABJ-380] Chaincode & tests-Private data via transient
Updated Java SDK's private data integration tests and corresponding golang chaincode to send arguments via transient field Change-Id: I4216cbcfada42fdf64466abe29ae57120b34ba0f Signed-off-by: Amol Pednekar <amol_pednekar@persistent.com>
1 parent ac2d4fe commit 8044bac

File tree

2 files changed

+75
-30
lines changed

2 files changed

+75
-30
lines changed

src/test/fixture/sdkintegration/gocc/samplePrivateData/src/github.com/private_data_cc/private_data_cc.go

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,21 @@ func (t *SimpleChaincode) move(stub shim.ChaincodeStubInterface, args []string)
7272
var X int // Transaction value
7373
var err error
7474

75-
if len(args) != 3 {
76-
return shim.Error("Incorrect number of arguments. Expecting 3, function followed by 2 names and 1 value")
75+
if len(args) != 0 {
76+
return shim.Error("Incorrect number of arguments. All attributes must be included in the transient map.")
77+
}
78+
79+
transMap, err := stub.GetTransient()
80+
if err != nil {
81+
return shim.Error("Error getting transient: " + err.Error())
82+
}
83+
84+
if len(transMap) !=3 {
85+
return shim.Error("Incorrect number of arguments. Expecting 3, function followed by 2 names and 1 value, got " + strconv.Itoa(len(transMap)))
7786
}
7887

79-
A = args[0]
80-
B = args[1]
88+
A = string(transMap["A"])
89+
B = string(transMap["B"])
8190

8291
// Get the state from the ledger
8392
// TODO: will be nice to have a GetAllState call to ledger
@@ -100,7 +109,7 @@ func (t *SimpleChaincode) move(stub shim.ChaincodeStubInterface, args []string)
100109
Bval, _ = strconv.Atoi(string(Bvalbytes))
101110

102111
// Perform the execution
103-
X, err = strconv.Atoi(args[2])
112+
X, err = strconv.Atoi(string(transMap["moveAmount"]))
104113
if err != nil {
105114
return shim.Error("Invalid transaction amount, expecting a integer value")
106115
}
@@ -128,19 +137,27 @@ func (t *SimpleChaincode) set(stub shim.ChaincodeStubInterface, args []string) p
128137
var Aval, Bval int // Asset holdings
129138
var err error
130139

131-
if len(args) != 4 {
132-
return shim.Error("Incorrect number of arguments. Expecting 4, function followed by 2 names and 2 values")
140+
if len(args) != 0 {
141+
return shim.Error("Incorrect number of arguments. All attributes must be included in the transient map.")
142+
}
143+
144+
transMap, err := stub.GetTransient()
145+
if err != nil {
146+
return shim.Error("Error getting transient: " + err.Error())
147+
}
148+
149+
if len(transMap) !=4 {
150+
return shim.Error("Incorrect number of arguments. Expecting 4, function followed by 2 names and 2 values, got " + strconv.Itoa(len(transMap)))
133151
}
134152

135-
A = args[0]
136-
Aval, err = strconv.Atoi(args[1])
153+
A = string(transMap["A"])
154+
Aval, err = strconv.Atoi(string(transMap["AVal"]))
137155
if err != nil {
138156
return shim.Error("Invalid A value amount, expecting a integer value")
139157
}
140158

141-
B = args[2]
142-
143-
Bval, err = strconv.Atoi(args[3])
159+
B = string(transMap["B"])
160+
Bval, err = strconv.Atoi(string(transMap["BVal"]))
144161
if err != nil {
145162
return shim.Error("Invalid B value amount, expecting a integer value")
146163
}
@@ -168,30 +185,39 @@ func (t *SimpleChaincode) set(stub shim.ChaincodeStubInterface, args []string) p
168185
// Query callback representing the query of a chaincode ===>>> ONLY Query B VALUES
169186
func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response {
170187

171-
var A string // Entities
188+
var QueryKey string // Entities
172189
var err error
173190

174-
if len(args) != 1 {
175-
return shim.Error("Incorrect number of arguments. Expecting name of the person to query")
191+
if len(args) != 0 {
192+
return shim.Error("Incorrect number of arguments. All attributes must be included in the transient map.")
193+
}
194+
195+
transMap, err := stub.GetTransient()
196+
if err != nil {
197+
return shim.Error("Error getting transient: " + err.Error())
198+
}
199+
200+
if len(transMap) !=1 {
201+
return shim.Error("Incorrect number of arguments. Expecting 1, function followed by query key, got " + strconv.Itoa(len(transMap)))
176202
}
177203

178-
A = args[0]
204+
QueryKey = string(transMap["B"])
179205

180-
logger.Infof("query for %s\n", A)
206+
logger.Infof("query for %s\n", QueryKey)
181207

182208
// Get the state from the ledger
183-
Avalbytes, err := stub.GetPrivateData("COLLECTION_FOR_B", A)
209+
Avalbytes, err := stub.GetPrivateData("COLLECTION_FOR_B", QueryKey)
184210
if err != nil {
185-
jsonResp := "{\"Error\":\"Failed to get state for " + A + "\"}"
211+
jsonResp := "{\"Error\":\"Failed to get state for " + QueryKey + "\"}"
186212
return shim.Error(jsonResp)
187213
}
188214

189215
if Avalbytes == nil {
190-
jsonResp := "{\"Error\":\"Nil amount for " + A + "\"}"
216+
jsonResp := "{\"Error\":\"Nil amount for " + QueryKey + "\"}"
191217
return shim.Error(jsonResp)
192218
}
193219

194-
jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}"
220+
jsonResp := "{\"Name\":\"" + QueryKey + "\",\"Amount\":\"" + string(Avalbytes) + "\"}"
195221
logger.Infof("Query Response:%s\n", jsonResp)
196222
return shim.Success(Avalbytes)
197223
}

src/test/java/org/hyperledger/fabric/sdkintegration/PrivateDataIT.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
import java.nio.file.Paths;
2222
import java.util.Arrays;
2323
import java.util.Collection;
24+
import java.util.HashMap;
2425
import java.util.HashSet;
2526
import java.util.LinkedList;
27+
import java.util.Map;
2628
import java.util.Set;
2729
import java.util.concurrent.CompletableFuture;
2830
import java.util.concurrent.CompletionException;
@@ -44,6 +46,7 @@
4446
import org.hyperledger.fabric.sdk.TransactionProposalRequest;
4547
import org.hyperledger.fabric.sdk.TransactionRequest;
4648
import org.hyperledger.fabric.sdk.User;
49+
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
4750
import org.hyperledger.fabric.sdk.exception.ProposalException;
4851
import org.hyperledger.fabric.sdk.exception.TransactionEventException;
4952
import org.hyperledger.fabric.sdk.security.CryptoSuite;
@@ -356,8 +359,15 @@ CompletableFuture<BlockEvent.TransactionEvent> moveAmount(HFClient client, Chann
356359
TransactionProposalRequest transactionProposalRequest = client.newTransactionProposalRequest();
357360
transactionProposalRequest.setChaincodeID(chaincodeID);
358361
transactionProposalRequest.setFcn("move");
359-
transactionProposalRequest.setArgs(new byte[][] {//test using bytes .. end2end uses Strings.
360-
"a".getBytes(UTF_8), "b".getBytes(UTF_8), moveAmount.getBytes(UTF_8)});
362+
363+
// Private data needs to be sent via Transient field to prevent identifiable
364+
//information being sent to the orderer.
365+
Map<String, byte[]> transientMap = new HashMap<>();
366+
transientMap.put("A", "a".getBytes(UTF_8)); //test using bytes .. end2end uses Strings.
367+
transientMap.put("B", "b".getBytes(UTF_8));
368+
transientMap.put("moveAmount", moveAmount.getBytes(UTF_8));
369+
transactionProposalRequest.setTransientMap(transientMap);
370+
361371
transactionProposalRequest.setProposalWaitTime(testConfig.getProposalWaitTime());
362372
if (user != null) { // specific user use that
363373
transactionProposalRequest.setUserContext(user);
@@ -411,7 +421,14 @@ CompletableFuture<BlockEvent.TransactionEvent> setAmount(HFClient client, Channe
411421
TransactionProposalRequest transactionProposalRequest = client.newTransactionProposalRequest();
412422
transactionProposalRequest.setChaincodeID(chaincodeID);
413423
transactionProposalRequest.setFcn("set");
414-
transactionProposalRequest.setArgs("a", "500", "b", "" + (200 + delta));
424+
425+
Map<String, byte[]> transientMap = new HashMap<>();
426+
transientMap.put("A", "a".getBytes(UTF_8)); // test using bytes as args. End2end uses Strings.
427+
transientMap.put("AVal", "500".getBytes(UTF_8));
428+
transientMap.put("B", "b".getBytes(UTF_8));
429+
String arg3 = "" + (200 + delta);
430+
transientMap.put("BVal", arg3.getBytes(UTF_8));
431+
transactionProposalRequest.setTransientMap(transientMap);
415432

416433
transactionProposalRequest.setProposalWaitTime(testConfig.getProposalWaitTime());
417434
if (user != null) { // specific user use that
@@ -457,14 +474,16 @@ CompletableFuture<BlockEvent.TransactionEvent> setAmount(HFClient client, Channe
457474
private void queryChaincodeForExpectedValue(HFClient client, Channel channel, final String expect, ChaincodeID chaincodeID) {
458475

459476
out("Now query chaincode %s on channel %s for the value of b expecting to see: %s", chaincodeID, channel.getName(), expect);
460-
QueryByChaincodeRequest queryByChaincodeRequest = client.newQueryProposalRequest();
461-
queryByChaincodeRequest.setArgs("b".getBytes(UTF_8)); // test using bytes as args. End2end uses Strings.
462-
queryByChaincodeRequest.setFcn("query");
463-
queryByChaincodeRequest.setChaincodeID(chaincodeID);
464-
465477
Collection<ProposalResponse> queryProposals;
466-
467478
try {
479+
QueryByChaincodeRequest queryByChaincodeRequest = client.newQueryProposalRequest();
480+
queryByChaincodeRequest.setFcn("query");
481+
queryByChaincodeRequest.setChaincodeID(chaincodeID);
482+
483+
Map<String, byte[]> tmap = new HashMap<>();
484+
tmap.put("B", "b".getBytes(UTF_8)); // test using bytes as args. End2end uses Strings.
485+
queryByChaincodeRequest.setTransientMap(tmap);
486+
468487
queryProposals = channel.queryByChaincode(queryByChaincodeRequest);
469488
} catch (Exception e) {
470489
throw new CompletionException(e);

0 commit comments

Comments
 (0)