summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <me@fwei.tk>2019-07-16 12:24:00 -0400
committerFranklin Wei <me@fwei.tk>2019-07-16 12:24:00 -0400
commite5561e6b5a4ef082ab90741652d4d247cfc063b1 (patch)
treeb688b91f1501f558d3b13e6c6ec97d0195283c71
parentca2f7d7fdb9ea3321c122bbb6e05d03c35a0b063 (diff)
downloaddcr-e5561e6b5a4ef082ab90741652d4d247cfc063b1.zip
dcr-e5561e6b5a4ef082ab90741652d4d247cfc063b1.tar.gz
dcr-e5561e6b5a4ef082ab90741652d4d247cfc063b1.tar.bz2
dcr-e5561e6b5a4ef082ab90741652d4d247cfc063b1.tar.xz
Some work on encloser query. Bugfixes. Add update leaf chaincode.
-rw-r--r--java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/DCRChaincode.java49
-rw-r--r--java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/FabricIOMT.java2
-rw-r--r--java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/IOMT.java16
-rw-r--r--java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/IOMTLeaf.java49
-rw-r--r--java-chaincode/encloser-query.json50
-rwxr-xr-xjava-chaincode/peerlog.sh3
6 files changed, 157 insertions, 12 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 6590da5..ef3f0cc 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
@@ -21,7 +21,7 @@ import java.util.stream.IntStream;
public class DCRChaincode extends ChaincodeBase {
- private static Log LOG = LogFactory.getLog(DCRChaincode.class);
+ public static Log LOG = LogFactory.getLog(DCRChaincode.class);
public static final String INVOKE_FUNCTION = "invoke";
public static final String QUERY_FUNCTION = "query";
@@ -34,6 +34,10 @@ 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 UPDATE_VIRTLEAF_VAL_FUNCTION =
+ "updateLeafVirtualVal"; // will propagate
+
public static final String GET_HEADER_FUNCTION = "getHeader"; // get header only
@Override
@@ -87,6 +91,10 @@ public class DCRChaincode extends ChaincodeBase {
{
return performGetHeaderOperation(chaincodeStub, paramList);
}
+ else if (UPDATE_VIRTLEAF_VAL_FUNCTION.equalsIgnoreCase(functionName))
+ {
+ return performUpdateLeafOperation(chaincodeStub, paramList);
+ }
else
return newErrorResponse(functionName + " function is currently supported");
}
@@ -270,7 +278,7 @@ public class DCRChaincode extends ChaincodeBase {
return newErrorResponse("invalid arguments");
}
- int physIdx = Integer.valueOf(idx_str);
+ int physIdx = Integer.valueOf(physIdx_str);
int idx = Integer.valueOf(idx_str);
int nextIdx = Integer.valueOf(nextIdx_str);
@@ -278,7 +286,6 @@ public class DCRChaincode extends ChaincodeBase {
!iomt.isValidLeafVirtIdx(nextIdx)) {
return newErrorResponse("invalid index(es)");
}
-
IOMTLeaf newLeaf = new IOMTLeaf(idx, nextIdx, val);
iomt.setLeafPhysical(physIdx, newLeaf);
@@ -289,12 +296,48 @@ public class DCRChaincode extends ChaincodeBase {
}
}
+ private Response performUpdateLeafOperation(ChaincodeStub chaincodeStub,
+ List<String> paramList) {
+ if (listHasDifferentSizeThen(paramList, 3)) {
+ return newErrorResponse("incorrect number of arguments");
+ }
+
+ String iomt_name = paramList.get(0);
+
+ try {
+ FabricIOMT iomt = new FabricIOMT(chaincodeStub, iomt_name);
+
+ String idx_str = paramList.get(1), val = paramList.get(2);
+
+ if(!StringUtils.isNumeric(idx_str) || !IOMT.isValidHash(val)) {
+ return newErrorResponse("invalid arguments");
+ }
+
+ int virtIdx = Integer.valueOf(idx_str);
+
+ if(!iomt.isValidLeafVirtIdx(virtIdx)) {
+ return newErrorResponse("invalid index(es)");
+ }
+
+ iomt.updateLeafVirtualVal(virtIdx,val);
+
+ return newSuccessResponse(val);
+ } catch (Exception e) {
+ return newErrorResponse(e.getMessage());
+ }
+ }
+
private Response performInitOperation(ChaincodeStub chaincodeStub, List<String> paramList) {
if(listHasDifferentSizeThen(paramList, 2)) {
return newErrorResponse("incorrect number of arguments");
}
String name = paramList.get(0);
+
+ if(FabricIOMT.treeExists(chaincodeStub,name)) {
+ return newErrorResponse("already exists");
+ }
+
String height_s = paramList.get(1);
if(!StringUtils.isNumeric(height_s)) {
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 9e31068..e38d256 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
@@ -133,7 +133,7 @@ public class FabricIOMT extends IOMT {
*/
public JSONObject toJSON() throws JSONException {
JSONObject obj = new JSONObject();
-4
+
try {
obj.put("height", Integer.toString(height));
obj.put("name", name);
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 cb55e48..cd003ad 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
@@ -10,8 +10,9 @@ import java.security.MessageDigest;
*
* IOMT representation is as follows:
*
- * Each IOMT has a mostly-fixed height (though some implementations may opt to allow resizing.
- * Each IOMT has two lists: 1) INTERNAL NODES and 2) LEAVES. Implementations must implement
+ * Each IOMT has a mostly-fixed height, specified by the `height' variable
+ * (though some implementations may opt to allow resizing. Each IOMT has two
+ * lists: 1) INTERNAL NODES and 2) LEAVES. Implementations must implement
* get/set node functions and get/set leaf functions.
*
* There is an important distinction between a VIRTUAL and PHYSICAL leaf index. A physical
@@ -24,7 +25,7 @@ public abstract class IOMT {
public static final int DEFAULT_HEIGHT = 10; // arbitrary
/* Change if not SHA-256 */
- public static final int HASH_LEN = 64;
+ public static final int HASH_LEN = 64; // in hex nibbles
public static final String ZERO_HASH = StringUtils.repeat('0', HASH_LEN);
protected int height;
@@ -60,7 +61,7 @@ public abstract class IOMT {
* @param array
* @return
*/
- private static String hexFromBytes(byte [] array) {
+ public static String hexFromBytes(byte [] array) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < array.length; i++)
{
@@ -202,17 +203,20 @@ public abstract class IOMT {
assert(isValidHash(newLeafHash));
+ // NOTE: this update will not be reflected in subsequent getNode()
+ // calls due to the read-write set semantics of Fabric.
setNode(idx, newLeafHash);
for(int i = 0; i < height; ++i)
{
/* find the merkle parent of the two children first */
- String parent = merkleParent(getNode(idx),
+ String parent = merkleParent(newLeafHash, // kludge
getNode(bintreeSibling(idx)),
(idx + 1) & 1);
idx = bintreeParent(idx);
setNode(idx, parent);
+ newLeafHash = parent;
}
}
@@ -225,6 +229,8 @@ public abstract class IOMT {
public void updateLeafPhysicalFull(int physIdx, IOMTLeaf leaf) {
setLeafPhysical(physIdx, leaf);
+ System.out.printf("leaf hash: %s", leaf.hash());
+
updateLeafNode(physIdx, leaf.hash());
}
diff --git a/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/IOMTLeaf.java b/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/IOMTLeaf.java
index 4822027..67ac36d 100644
--- a/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/IOMTLeaf.java
+++ b/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/IOMTLeaf.java
@@ -2,6 +2,10 @@ package tk.fwei.dcr;
import org.apache.commons.lang3.StringUtils;
+import java.nio.ByteBuffer;
+import java.security.MessageDigest;
+import java.util.Arrays;
+
public class IOMTLeaf {
private String val;
@@ -31,10 +35,49 @@ public class IOMTLeaf {
this.idx = idx;
}
+
public String hash() {
- // TODO
- return StringUtils.repeat('1', IOMT.HASH_LEN);
- //return IOMT.ZERO_HASH;
+ /* append and hash */
+ try {
+ if(true) {
+ MessageDigest md = MessageDigest.getInstance("SHA-256");
+
+ ByteBuffer buf1, buf2;
+ buf1 = ByteBuffer.allocate(4).putInt(getIdx());
+ buf2 = ByteBuffer.allocate(4).putInt(getNextIdx());
+
+ byte[] buf3 = getVal().getBytes();
+
+ DCRChaincode.LOG.info(IOMT.hexFromBytes(buf1.array()) + " " +
+ IOMT.hexFromBytes(buf2.array()) + " " + IOMT.hexFromBytes(buf3));
+
+ md.update(buf1);
+ md.update(buf2);
+ md.update(buf3);
+
+ return IOMT.hexFromBytes(md.digest());
+ }
+ else {
+ MessageDigest md = MessageDigest.getInstance("SHA-256");
+ byte buf1[] = {0, 0, 0, 1};
+ byte buf2[] = {0, 0, 0, 1};
+ byte buf3[] = new byte[64];
+
+ Arrays.fill(buf3, (byte) 0x30);
+ buf3[63] = (byte) 0x31;
+
+ DCRChaincode.LOG.info(IOMT.hexFromBytes(buf1) + " " +
+ IOMT.hexFromBytes(buf2) + " " + IOMT.hexFromBytes(buf3));
+
+ md.update(buf1);
+ md.update(buf1);
+ md.update(buf3);
+ return IOMT.hexFromBytes(md.digest());
+ }
+
+ } catch (Exception e) {
+ throw new RuntimeException("hash");
+ }
}
public String toString() {
diff --git a/java-chaincode/encloser-query.json b/java-chaincode/encloser-query.json
new file mode 100644
index 0000000..9beeb4c
--- /dev/null
+++ b/java-chaincode/encloser-query.json
@@ -0,0 +1,50 @@
+{
+ "selector": {
+ "docType": "iomtleaf",
+ "parentIOMT": "a",
+ "$or": [
+ {
+ "$and": [
+ {
+ "nextIdx": {
+ "$lte": "idx"
+ }
+ },
+ {
+ "idx": {
+ "$lt": 2
+ }
+ }
+ ]
+ },
+ {
+ "$and": [
+ {
+ "nextIdx": {
+ "$gt": 2
+ }
+ },
+ {
+ "idx": {
+ "$lt": 2
+ }
+ }
+ ]
+ },
+ {
+ "$and": [
+ {
+ "nextIdx": {
+ "$gt": 2
+ }
+ },
+ {
+ "idx": {
+ "$gte": "nextIdx"
+ }
+ }
+ ]
+ }
+ ]
+ }
+}
diff --git a/java-chaincode/peerlog.sh b/java-chaincode/peerlog.sh
new file mode 100755
index 0000000..cfaa3f8
--- /dev/null
+++ b/java-chaincode/peerlog.sh
@@ -0,0 +1,3 @@
+#!/usr/bin/env bash
+
+export ID=$(cat .ccid) \ No newline at end of file