private Hashable initHelper(Token left, Token right, byte depth, byte max) { if (depth == max) // we've reached the leaves return new Leaf(); Token midpoint = partitioner.midpoint(left, right); Hashable lchild = initHelper(left, midpoint, inc(depth), max); Hashable rchild = initHelper(midpoint, right, inc(depth), max); return new Inner(midpoint, lchild, rchild); }
private Hashable splitHelper(Hashable hashable, Token pleft, Token pright, byte depth, Token t) throws StopRecursion.TooDeep { if (depth >= hashdepth) throw new StopRecursion.TooDeep(); if (hashable instanceof Leaf) { // split size++; Token midpoint = partitioner.midpoint(pleft, pright); return new Inner(midpoint, new Leaf(), new Leaf()); } // else: node. // recurse on the matching child Inner node = (Inner) hashable; if (Range.contains(pleft, node.token, t)) // left child contains token node.lchild(splitHelper(node.lchild, pleft, node.token, inc(depth), t)); else // else: right child contains token node.rchild(splitHelper(node.rchild, node.token, pright, inc(depth), t)); return node; }
@Test public void testDifference() { int maxsize = 16; mts = new MerkleTrees(partitioner); mts.addMerkleTree(32, fullRange()); MerkleTrees mts2 = new MerkleTrees(partitioner); mts2.addMerkleTree(32, fullRange()); mts.init(); mts2.init(); // add dummy hashes to both trees for (TreeRange range : mts.invalids()) range.addAll(new HIterator(range.right)); for (TreeRange range : mts2.invalids()) range.addAll(new HIterator(range.right)); TreeRange leftmost = null; TreeRange middle = null; mts.maxsize(fullRange(), maxsize + 2); // give some room for splitting // split the leftmost Iterator<TreeRange> ranges = mts.invalids(); leftmost = ranges.next(); mts.split(leftmost.right); // set the hashes for the leaf of the created split middle = mts.get(leftmost.right); middle.hash("arbitrary!".getBytes()); mts.get(partitioner.midpoint(leftmost.left, leftmost.right)) .hash("even more arbitrary!".getBytes()); // trees should disagree for (leftmost.left, middle.right] List<Range<Token>> diffs = MerkleTrees.difference(mts, mts2); assertEquals(diffs + " contains wrong number of differences:", 1, diffs.size()); assertTrue(diffs.contains(new Range<>(leftmost.left, middle.right))); }