Example #1
0
 public MerkleTree(Vector<Indexable> v) {
   nodeMap = new HashMap<Integer, MerkleTreeNode>();
   this.store = v;
   int exp = 0;
   while ((int) Math.pow(2, exp) < store.size()) {
     exp++;
   }
   for (int i = store.size(); i < (int) Math.pow(2, exp); i++) {
     store.add(i, null);
   }
   Vector<MerkleTreeNode> hashes = new Vector<MerkleTreeNode>();
   for (int i = 0; i < store.size(); i++) {
     if (store.get(i) != null) {
       MerkleTreeNode temp = new MerkleTreeNode(store.get(i).getBytes(), i);
       nodeMap.put(i, temp);
       hashes.add(temp);
     } else {
       MerkleTreeNode temp = new MerkleTreeNode(i);
       nodeMap.put(i, temp);
       hashes.add(temp);
     }
   }
   while (hashes.size() > 1) {
     Vector<MerkleTreeNode> temp = new Vector<MerkleTreeNode>();
     for (int i = 0; i < hashes.size(); i += 2) {
       temp.add(MerkleTreeNode.combineDigestFactory(hashes.get(i), hashes.get(i + 1)));
     }
     hashes = temp;
   }
   root = hashes.get(0);
 }
Example #2
0
 public Indexable getLeafData(int index) {
   return store.get(index);
 }