summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2019-07-19 15:30:08 -0400
committerFranklin Wei <git@fwei.tk>2019-07-19 15:30:08 -0400
commite857918f4872f0a15276c1f6a31a1048b98b98bc (patch)
tree25f303bb7c0dfe232990624d0b2438753501248d
parentde2cd3c7afe244b1439944289e6adb1fbc22b269 (diff)
downloaddcr-e857918f4872f0a15276c1f6a31a1048b98b98bc.zip
dcr-e857918f4872f0a15276c1f6a31a1048b98b98bc.tar.gz
dcr-e857918f4872f0a15276c1f6a31a1048b98b98bc.tar.bz2
dcr-e857918f4872f0a15276c1f6a31a1048b98b98bc.tar.xz
iomt: finish encloser query for remaining cases
Also adds chaincode command for testing.
-rw-r--r--java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/DCRChaincode.java41
-rw-r--r--java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/FabricIOMT.java9
2 files changed, 49 insertions, 1 deletions
diff --git a/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/DCRChaincode.java b/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/DCRChaincode.java
index de0ebad..261c083 100644
--- a/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/DCRChaincode.java
+++ b/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/DCRChaincode.java
@@ -2,6 +2,7 @@ package tk.fwei.dcr;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hyperledger.fabric.shim.ChaincodeBase;
@@ -34,6 +35,7 @@ public class DCRChaincode extends ChaincodeBase {
public static final String SET_NODE_FUNCTION = "setNode";
public static final String GET_VIRTLEAF_FUNCTION = "getLeafVirtual";
public static final String SET_PHYSLEAF_FUNCTION = "setLeafPhysical";
+ public static final String GET_ENCLOSER_FUNCTION = "getEncloser";
public static final String UPDATE_VIRTLEAF_VAL_FUNCTION =
"updateLeafVirtualVal"; // will propagate
@@ -95,6 +97,10 @@ public class DCRChaincode extends ChaincodeBase {
{
return performUpdateLeafOperation(chaincodeStub, paramList);
}
+ else if (GET_ENCLOSER_FUNCTION.equalsIgnoreCase(functionName))
+ {
+ return performEncloserLookupOperation(chaincodeStub, paramList);
+ }
else
return newErrorResponse(functionName + " function is currently supported");
}
@@ -163,6 +169,41 @@ public class DCRChaincode extends ChaincodeBase {
return DigestUtils.sha256Hex(data);
}
+
+ private Response performEncloserLookupOperation(ChaincodeStub chaincodeStub, List<String> paramList) {
+ if (listHasDifferentSizeThen(paramList, 2)) {
+ return newErrorResponse("incorrect number of arguments");
+ }
+
+ String iomtName = paramList.get(0);
+
+ if(!FabricIOMT.treeExists(chaincodeStub, iomtName)) {
+ return newErrorResponse("IOMT does not exist");
+ }
+
+ try {
+ FabricIOMT iomt = new FabricIOMT(chaincodeStub, iomtName);
+
+ String virtIdx_s = paramList.get(1);
+ if(!StringUtils.isNumeric(virtIdx_s)) {
+ return newErrorResponse("virtual index must be numeric");
+ }
+
+ int virtIdx = Integer.valueOf(virtIdx_s);
+
+ String val;
+
+ ImmutablePair<IOMTLeaf, Integer> leafPair = iomt.getEncloser(virtIdx);
+
+ return newSuccessResponse("found encloser with physIdx " + leafPair.right.toString() +
+ " with value " + leafPair.left.toString());
+ } catch (IndexOutOfBoundsException e) {
+ return newErrorResponse(e.getMessage());
+ } catch (JSONException e) {
+ return newErrorResponse("corrupt JSON");
+ }
+ }
+
private Response performQueryNodeFunction(ChaincodeStub chaincodeStub, List<String> paramList) {
if (listHasDifferentSizeThen(paramList, 2)) {
return newErrorResponse("incorrect number of arguments");
diff --git a/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/FabricIOMT.java b/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/FabricIOMT.java
index f58504e..4cb1cb0 100644
--- a/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/FabricIOMT.java
+++ b/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/FabricIOMT.java
@@ -350,7 +350,14 @@ public class FabricIOMT extends IOMT {
public ImmutablePair<IOMTLeaf, Integer> getEncloser(int virtIdx)
{
// first the low-hanging fruit: a < i_min or a > i_max
- // TODO
+ if(virtIdx < this.minVirtIdx || virtIdx > this.maxVirtIdx) {
+ // This correctly returns the maximum leaf. The leaf with the maximum virtual index
+ // wraps around to the minimum index. Thus, it is the leaf with the *greatest*
+ // index which proves that all indexes < minIdx do not exist. Of course, the same is
+ // true of non-existent indexes > maxIdx, so it is appropriate to always return the
+ // greatest leaf in these cases.
+ return getLeafAndPhysIdxVirtual(this.maxVirtIdx);
+ }
// see encloser-query.json for where this comes from
JSONObject jsonQuery = new JSONObject();