From 70169663988b261d632de259b63ed491a4331544 Mon Sep 17 00:00:00 2001 From: Dan Newton Date: Tue, 5 Dec 2023 16:55:29 +0000 Subject: [PATCH] NOTICK Storing and loading merkle proofs `MerkleTree.calculateLeveledHashes` creates the level + index of the hashes to store in the database. `LoadedMerkleProofTree` takes leveled hashes + indexed merkle leaves to construct and class that can create a new `MerkleProof`. --- .../corda/v5/crypto/merkle/MerkleProof.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/crypto/src/main/java/net/corda/v5/crypto/merkle/MerkleProof.java b/crypto/src/main/java/net/corda/v5/crypto/merkle/MerkleProof.java index 92dc41cd90..63cae08369 100644 --- a/crypto/src/main/java/net/corda/v5/crypto/merkle/MerkleProof.java +++ b/crypto/src/main/java/net/corda/v5/crypto/merkle/MerkleProof.java @@ -6,6 +6,7 @@ import org.jetbrains.annotations.NotNull; import java.util.List; +import java.util.Objects; /** * {@link MerkleProof}s can be used to verify if some specific data is part of a {@link MerkleTree}. @@ -58,4 +59,53 @@ public interface MerkleProof { * @throws MerkleProofRebuildFailureException if the calculation of the root hash failed. */ SecureHash calculateRoot(@NotNull MerkleTreeHashDigest digest); + + List calculateLeveledHashes(@NotNull MerkleTreeHashDigest digest); + + class LeveledHash { + + private final int level; + private final int index; + private final SecureHash hash; + + public LeveledHash(int level, int index, SecureHash hash) { + this.level = level; + this.index = index; + this.hash = hash; + } + + public int getLevel() { + return level; + } + + public int getIndex() { + return index; + } + + public SecureHash getHash() { + return hash; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + LeveledHash that = (LeveledHash) o; + return level == that.level && index == that.index && Objects.equals(hash, that.hash); + } + + @Override + public int hashCode() { + return Objects.hash(level, index, hash); + } + + @Override + public String toString() { + return "LeveledHash{" + + "level=" + level + + ", index=" + index + + ", hash=" + hash + + '}'; + } + } }