MerkleBranch extends ChildMessage { public Sha256Hash calculateMerkleRoot(final Sha256Hash leaf) { byte[] target = leaf.getReversedBytes(); long mask = index; MessageDigest digest = Sha256Hash.newDigest(); for (Sha256Hash hash: hashes) { digest.reset(); if ((mask & 1) == 0) { digest.update(target); digest.update(hash.getReversedBytes()); } else { digest.update(hash.getReversedBytes()); digest.update(target); } target = digest.digest(); digest.reset(); target = digest.digest(target); mask >>= 1; } return Sha256Hash.wrapReversed(target); } MerkleBranch(NetworkParameters params, @Nullable ChildMessage parent);  MerkleBranch(NetworkParameters params, @Nullable ChildMessage parent, byte[] payload, int offset);  MerkleBranch(NetworkParameters params, ChildMessage parent, byte[] payload, int offset,
                        MessageSerializer serializer);  MerkleBranch(NetworkParameters params, @Nullable ChildMessage parent,
        final List<Sha256Hash> hashes, final long branchSideMask); static int calcLength(byte[] buf, int offset); Sha256Hash calculateMerkleRoot(final Sha256Hash leaf); List<Sha256Hash> getHashes(); long getIndex(); void setHashes(List<Sha256Hash> hashes); void setIndex(final long newIndex); int size(); int getOptimalEncodingMessageSize(); @Override String toString(); @Override boolean equals(Object o); @Override int hashCode();  }
@Test public void calculateRootBranch() throws Exception { byte[] branchAsBytes = getBytes(getClass().getResourceAsStream("auxpow_merkle_branch2.bin")); MerkleBranch branch = new MerkleBranch(params, (ChildMessage) null, branchAsBytes, 0); Sha256Hash txId = Sha256Hash.wrap("0c836b86991631d34a8a68054e2f62db919b39d1ee43c27ab3344d6aa82fa609"); Sha256Hash expected = Sha256Hash.wrap("ce3040fdb7e37484f6a1ca4f8f5da81e6b7e404ec91102315a233e03a0c39c95"); assertEquals(expected, branch.calculateMerkleRoot(txId)); }