summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2019-07-11 18:58:58 -0400
committerFranklin Wei <git@fwei.tk>2019-07-11 18:58:58 -0400
commit4ad6dfeb3063593e717f4491f6fdb1a0dd58e291 (patch)
treebadb7d2d5ddee53a9a9035ef3b19f934787c80d0
parentfa1301ff9b73339c3d3e4a5fe4213eee26928282 (diff)
downloaddcr-4ad6dfeb3063593e717f4491f6fdb1a0dd58e291.zip
dcr-4ad6dfeb3063593e717f4491f6fdb1a0dd58e291.tar.gz
dcr-4ad6dfeb3063593e717f4491f6fdb1a0dd58e291.tar.bz2
dcr-4ad6dfeb3063593e717f4491f6fdb1a0dd58e291.tar.xz
Some partial work on CouchDB queries for virtual index lookup
Not working yet.
-rw-r--r--java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/FabricIOMT.java94
-rw-r--r--java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/IOMT.java17
2 files changed, 78 insertions, 33 deletions
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 3a3d3c8..3bdc63f 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
@@ -2,9 +2,13 @@ package tk.fwei.dcr;
import org.hyperledger.fabric.shim.ChaincodeStub;
import org.hyperledger.fabric.shim.ledger.CompositeKey;
+import org.hyperledger.fabric.shim.ledger.KeyValue;
+import org.hyperledger.fabric.shim.ledger.QueryResultsIterator;
import org.json.JSONException;
import org.json.JSONObject;
+import java.util.Iterator;
+
/**
* Representation of an index-ordered Merkle tree, backed by the Hyperledger
* Fabric state database. Intended for use in chaincode.
@@ -82,6 +86,18 @@ public class FabricIOMT extends IOMT {
return chaincodeStub.createCompositeKey("iomt", name);
}
+ public CompositeKey buildNodeKey(int node_id) {
+ return buildNodeKey(chaincodeStub, name, node_id);
+ }
+
+ public CompositeKey buildLeafKey(int leaf_id) {
+ return buildLeafKey(chaincodeStub, name, leaf_id);
+ }
+
+ public CompositeKey buildTreeKey() {
+ return buildTreeKey(chaincodeStub, name);
+ }
+
/**
* Look up the JSON header of a tree.
*
@@ -107,18 +123,6 @@ public class FabricIOMT extends IOMT {
return !queryTreeJSON(stub, iomt_name).isEmpty();
}
- public CompositeKey buildNodeKey(int node_id) {
- return buildNodeKey(chaincodeStub, name, node_id);
- }
-
- public CompositeKey buildLeafKey(int leaf_id) {
- return buildLeafKey(chaincodeStub, name, leaf_id);
- }
-
- public CompositeKey buildTreeKey() {
- return buildTreeKey(chaincodeStub, name);
- }
-
/**
* Return a JSON descriptor of the tree. NOTE: this descriptor does
* NOT include the tree's contents; those are stored as separate key-value
@@ -190,6 +194,7 @@ public class FabricIOMT extends IOMT {
JSONObject node = new JSONObject();
try {
+ node.put("docType", "iomtnode");
node.put("val", val);
} catch (JSONException e) {
throw new RuntimeException("err");
@@ -198,18 +203,10 @@ public class FabricIOMT extends IOMT {
chaincodeStub.putStringState(buildNodeKey(nodeId).toString(), node.toString());
}
- public IOMTLeaf getLeafPhysical(int leafId) {
- if (!isValidLeafId(leafId)) {
- throw new IndexOutOfBoundsException("out of bounds");
- }
-
- CompositeKey key = buildLeafKey(leafId);
-
- String leaf_s = chaincodeStub.getStringState(key.toString());
-
+ private IOMTLeaf parseLeaf(String json) {
JSONObject node;
try {
- node = new JSONObject(leaf_s);
+ node = new JSONObject(json);
} catch (JSONException e) {
return new IOMTLeaf(); // null leaf (will hash to null)
}
@@ -227,11 +224,25 @@ public class FabricIOMT extends IOMT {
assert (isValidHash(val.getVal()));
return val;
+
+ }
+
+ public IOMTLeaf getLeafPhysical(int leafId) {
+ if (!isValidLeafId(leafId)) {
+ throw new IndexOutOfBoundsException("out of bounds");
+ }
+
+ CompositeKey key = buildLeafKey(leafId);
+
+ String leaf_s = chaincodeStub.getStringState(key.toString());
+
+ return parseLeaf(leaf_s);
}
public void setLeafPhysical(int leafId, IOMTLeaf val) {
JSONObject leaf = new JSONObject();
try {
+ leaf.put("docType", "iomtleaf");
leaf.put("idx", val.getIdx());
leaf.put("nextIdx", val.getNextIdx());
leaf.put("val", val.getVal());
@@ -243,20 +254,47 @@ public class FabricIOMT extends IOMT {
leaf.toString());
}
- public IOMTLeaf getLeafVirtual(int virtIdx, Integer outPhysIdx) {
+ public Pair<IOMTLeaf, Integer> getLeafVirtual(int virtIdx) {
// TODO: couchdb query
- //chaincodeStub.getQueryResult();
+ // build query string (see CouchDB docs)
- return new IOMTLeaf();
+ JSONObject jsonQuery = new JSONObject();
+ JSONObject selector = new JSONObject();
+ QueryResultsIterator results;
+ try {
+ selector.put("docType", "iomtleaf");
+ selector.put("idx", virtIdx);
+
+ jsonQuery.put("selector", selector);
+ results = chaincodeStub.getQueryResult(jsonQuery.toString());
+ } catch(JSONException e) {
+ throw new RuntimeException("json error");
+ }
+
+ Iterator<KeyValue> iterator = results.iterator();
+
+ if(!iterator.hasNext())
+ throw new IndexOutOfBoundsException("no leaf found with given index");
+
+ KeyValue pair = iterator.next();
+ String keyString = pair.getKey();
+ CompositeKey key = chaincodeStub.splitCompositeKey(keyString);
+
+ // get physical index
+ int physIdx = Integer.parseInt(key.getAttributes().get(0));
+
+ // get leaf fields
+ IOMTLeaf leaf = parseLeaf(pair.getStringValue());
+
+ return new Pair<IOMTLeaf, Integer>(leaf, physIdx);
}
- public IOMTLeaf getEncloser(int virtIdx, Integer outPhysLeafIdx) {
+ public Pair<IOMTLeaf, Integer> getEncloser(int virtIdx) {
return new IOMTLeaf();
}
- public IOMTLeaf getLeafOrEncloser(int virtIdx,
- Integer outPhysLeafIdx) {
+ public Pair<IOMTLeaf, Integer> getLeafOrEncloser(int virtIdx) {
return new IOMTLeaf();
}
diff --git a/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/IOMT.java b/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/IOMT.java
index 93a11b1..7322b0d 100644
--- a/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/IOMT.java
+++ b/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/IOMT.java
@@ -1,6 +1,7 @@
package tk.fwei.dcr;
import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.tuple.Pair;
import java.security.MessageDigest;
@@ -163,13 +164,19 @@ public abstract class IOMT {
abstract public void setLeafPhysical(int physIdx, IOMTLeaf val);
- // to be implemented on CouchDB
- abstract public IOMTLeaf getLeafVirtual(int virtIdx, Integer outPhysLeafIdx);
+ // to be implemented on CouchDB:
- abstract public IOMTLeaf getEncloser(int virtIdx, Integer outPhysLeafIdx);
+ /**
+ * Look up an IOMT leaf by its virtual index.
+ *
+ * @param virtIdx virtual index of leaf
+ * @return a pair containing the leaf's contents and its physical index in the tree
+ */
+ abstract public Pair<IOMTLeaf, Integer> getLeafVirtual(int virtIdx);
+
+ abstract public Pair<IOMTLeaf, Integer> getEncloser(int virtIdx);
- abstract public IOMTLeaf getLeafOrEncloser(int virtIdx,
- Integer outPhysLeafIdx);
+ abstract public Pair<IOMTLeaf, Integer> getLeafOrEncloser(int virtIdx);
/**
* Change the value of a level-0 NODE in the tree--NOT a leaf. Update will propagate