summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <me@fwei.tk>2019-07-11 12:04:53 -0400
committerFranklin Wei <me@fwei.tk>2019-07-11 12:04:53 -0400
commitfa1301ff9b73339c3d3e4a5fe4213eee26928282 (patch)
tree5e6f7b8f4f8731338b5f7085a49ca0d06d5b5f76
parent9ed3f5dae9a60adbce6090a46e9556924b33c3fc (diff)
downloaddcr-fa1301ff9b73339c3d3e4a5fe4213eee26928282.zip
dcr-fa1301ff9b73339c3d3e4a5fe4213eee26928282.tar.gz
dcr-fa1301ff9b73339c3d3e4a5fe4213eee26928282.tar.bz2
dcr-fa1301ff9b73339c3d3e4a5fe4213eee26928282.tar.xz
Finish up IOMT class
-rw-r--r--java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/DCRChaincode.java5
-rw-r--r--java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/FabricIOMT.java20
-rw-r--r--java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/IOMT.java108
-rwxr-xr-xjava-chaincode/start.sh2
4 files changed, 104 insertions, 31 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 cfe48ed..51b151c 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
@@ -157,7 +157,7 @@ public class DCRChaincode extends ChaincodeBase {
String val;
- val = iomt.getNodeValue(id);
+ val = iomt.getNode(id);
return newSuccessResponse(val);
} catch (IndexOutOfBoundsException e) {
@@ -187,7 +187,7 @@ public class DCRChaincode extends ChaincodeBase {
String val = paramList.get(2);
- iomt.setNodeValue(id, val);
+ iomt.setNode(id, val);
return newSuccessResponse(val);
} catch (Exception e) {
@@ -248,7 +248,6 @@ public class DCRChaincode extends ChaincodeBase {
private boolean listHasDifferentSizeThen(List<String> list, int expectedElementsNumber) {
return list.size() != expectedElementsNumber;
-
}
public static void main(String [] args){
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 9915028..3a3d3c8 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
@@ -147,7 +147,7 @@ public class FabricIOMT extends IOMT {
* @return hash value associated with node
* @throws IndexOutOfBoundsException
*/
- public String getNodeValue(int node_id) throws IndexOutOfBoundsException {
+ public String getNode(int node_id) throws IndexOutOfBoundsException {
if (invalidNodeId(node_id)) {
throw new IndexOutOfBoundsException("out of bounds" + getMaxNodeId() + " " + height + (1 << height));
}
@@ -182,7 +182,7 @@ public class FabricIOMT extends IOMT {
* @param nodeId index
* @param val hash
*/
- public void setNodeValue(int nodeId, String val) {
+ public void setNode(int nodeId, String val) {
if (!isValidHash(val))
throw new RuntimeException("invalid hash");
if (invalidNodeId(nodeId))
@@ -198,7 +198,7 @@ public class FabricIOMT extends IOMT {
chaincodeStub.putStringState(buildNodeKey(nodeId).toString(), node.toString());
}
- public IOMTLeaf getLeafValue(int leafId) {
+ public IOMTLeaf getLeafPhysical(int leafId) {
if (!isValidLeafId(leafId)) {
throw new IndexOutOfBoundsException("out of bounds");
}
@@ -229,7 +229,7 @@ public class FabricIOMT extends IOMT {
return val;
}
- public void setLeafValue(int leafId, IOMTLeaf val) {
+ public void setLeafPhysical(int leafId, IOMTLeaf val) {
JSONObject leaf = new JSONObject();
try {
leaf.put("idx", val.getIdx());
@@ -243,11 +243,21 @@ public class FabricIOMT extends IOMT {
leaf.toString());
}
- public IOMTLeaf findLeafByVirtualIndex(int virtIdx, Integer outPhysIdx) {
+ public IOMTLeaf getLeafVirtual(int virtIdx, Integer outPhysIdx) {
// TODO: couchdb query
//chaincodeStub.getQueryResult();
return new IOMTLeaf();
}
+
+ public IOMTLeaf getEncloser(int virtIdx, Integer outPhysLeafIdx) {
+ return new IOMTLeaf();
+ }
+
+ public IOMTLeaf getLeafOrEncloser(int virtIdx,
+ Integer outPhysLeafIdx) {
+ return new IOMTLeaf();
+ }
+
} \ No newline at end of file
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 3974971..93a11b1 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,5 @@
package tk.fwei.dcr;
-import com.google.protobuf.Message;
import org.apache.commons.lang3.StringUtils;
import java.security.MessageDigest;
@@ -149,19 +148,28 @@ public abstract class IOMT {
return !(leafId < 0 || leafId > getMaxLeafId());
}
- abstract public String getNodeValue(int node_id);
+ /* Implementations must implement the following */
+ abstract public String getNode(int node_id);
- abstract public void setNodeValue(int nodeId, String val);
+ abstract public void setNode(int nodeId, String val);
/**
* Look up a leaf's value (a 3-tuple of index, nextIndex, and value) by its PHYSICAL
- * index--not its virtual index. Use findLeafByVirtualIndex if you want that.
+ * index--not its virtual index. Use getLeafVirtual if you want that.
* @param leaf_id
* @return
*/
- abstract public IOMTLeaf getLeafValue(int physIdx);
+ abstract public IOMTLeaf getLeafPhysical(int physIdx);
- abstract public void setLeafValue(int physIdx, IOMTLeaf val);
+ abstract public void setLeafPhysical(int physIdx, IOMTLeaf val);
+
+ // to be implemented on CouchDB
+ abstract public IOMTLeaf getLeafVirtual(int virtIdx, Integer outPhysLeafIdx);
+
+ abstract public IOMTLeaf getEncloser(int virtIdx, Integer outPhysLeafIdx);
+
+ abstract public IOMTLeaf getLeafOrEncloser(int virtIdx,
+ Integer outPhysLeafIdx);
/**
* Change the value of a level-0 NODE in the tree--NOT a leaf. Update will propagate
@@ -174,17 +182,17 @@ public abstract class IOMT {
assert(isValidHash(newLeafHash));
- setNodeValue(idx, newLeafHash);
+ setNode(idx, newLeafHash);
for(int i = 0; i < height; ++i)
{
/* find the merkle parent of the two children first */
- String parent = merkleParent(getNodeValue(idx),
- getNodeValue(bintreeSibling(idx)),
+ String parent = merkleParent(getNode(idx),
+ getNode(bintreeSibling(idx)),
(idx + 1) & 1);
idx = bintreeParent(idx);
- setNodeValue(idx, parent);
+ setNode(idx, parent);
}
}
@@ -194,12 +202,37 @@ public abstract class IOMT {
* @param physIdx physical position of leaf
* @param leaf new leaf value (a 3-tuple)
*/
- public void updatePhysLeaf(int physIdx, IOMTLeaf leaf) {
- setLeafValue(physIdx, leaf);
+ public void updateLeafPhysicalFull(int physIdx, IOMTLeaf leaf) {
+ setLeafPhysical(physIdx, leaf);
updateLeafNode(physIdx, leaf.hash());
}
+ /* Convenience functions. */
+ public void updateLeafPhysicalIdx(int physIdx, int idx) {
+ IOMTLeaf old = getLeafPhysical(physIdx);
+
+ old.setIdx(idx);
+
+ updateLeafPhysicalFull(physIdx, old);
+ }
+
+ public void updateLeafPhysicalNextIdx(int physIdx, int nextIdx) {
+ IOMTLeaf old = getLeafPhysical(physIdx);
+
+ old.setNextIdx(nextIdx);
+
+ updateLeafPhysicalFull(physIdx, old);
+ }
+
+ public void updateLeafPhysicalVal(int physIdx, String hash) {
+ IOMTLeaf old = getLeafPhysical(physIdx);
+
+ old.setVal(hash);
+
+ updateLeafPhysicalFull(physIdx, old);
+ }
+
static private int bintreeParent(int idx)
{
return (idx - 1) / 2;
@@ -208,22 +241,53 @@ public abstract class IOMT {
/**
/* Convenience functions. */
- public void updateLeafFull(int physIdx, IOMTLeaf newLeaf) {
+ public void updateLeafVirtualFull(int virtIdx, IOMTLeaf newLeaf) {
+ Integer physIdx = -1;
- }
+ getLeafVirtual(virtIdx, physIdx);
+
+ if(physIdx < 0)
+ throw new RuntimeException("leaf not found");
- public void updateLeafIdx(int physIdx, int newIdx) {
-
+ updateLeafPhysicalFull(physIdx,newLeaf);
}
-
- public void updateLeafNextIdx(int virtIdx, int newNextIdx) {
-
+
+ public void updateLeafVirtualIdx(int virtIdx, int newIdx) {
+ Integer physIdx = -1;
+
+ IOMTLeaf old = getLeafVirtual(virtIdx, physIdx);
+
+ if(physIdx < 0)
+ throw new RuntimeException("leaf not found");
+
+ old.setIdx(newIdx);
+
+ updateLeafPhysicalFull(physIdx, old);
}
- public void updateLeafVal(int virtIdx, String hash) {
+ public void updateLeafVirtualNextIdx(int virtIdx, int newNextIdx) {
+ Integer physIdx = -1;
+
+ IOMTLeaf old = getLeafVirtual(virtIdx, physIdx);
+ if(physIdx < 0)
+ throw new RuntimeException("leaf not found");
+
+ old.setNextIdx(newNextIdx);
+
+ updateLeafPhysicalFull(physIdx, old);
}
- // to be implemented on CouchDB
- abstract public IOMTLeaf findLeafByVirtualIndex(int virtIdx, Integer outPhysLeafIdx);
+ public void updateLeafVirtualVal(int virtIdx, String hash) {
+ Integer physIdx = -1;
+
+ IOMTLeaf old = getLeafVirtual(virtIdx, physIdx);
+
+ if(physIdx < 0)
+ throw new RuntimeException("leaf not found");
+
+ old.setVal(hash);
+
+ updateLeafPhysicalFull(physIdx, old);
+ }
}
diff --git a/java-chaincode/start.sh b/java-chaincode/start.sh
index fdfb085..c071e52 100755
--- a/java-chaincode/start.sh
+++ b/java-chaincode/start.sh
@@ -1,6 +1,6 @@
export COMPOSE_PROJECT_NAME=java-chaincode
docker-compose -f docker-compose.yaml down
docker-compose -f docker-compose.yaml up -d
-sleep 10
+sleep 15
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.mydomain.com/msp" peer0.org1.mydomain.com peer channel create -o orderer.mydomain.com:7050 -c mychannel -f /etc/hyperledger/configtx/channel.tx
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.mydomain.com/msp" peer0.org1.mydomain.com peer channel join -b mychannel.block