Skip to content

Commit 789bc95

Browse files
authored
Merge pull request #93 from ApexTheory/84-batch-merkle-proof
Batch Merkle Proof - override equals for matcher array equality
2 parents 2237785 + 5649aaa commit 789bc95

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/main/scala/scorex/crypto/authds/merkle/BatchMerkleProof.scala

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import scorex.crypto.authds.merkle.MerkleTree.InternalNodePrefix
55
import scorex.crypto.hash.{CryptographicHash, Digest}
66
import scorex.util.ScorexEncoding
77

8+
import java.util
89
import scala.language.postfixOps
910

1011
/**
@@ -90,6 +91,31 @@ case class BatchMerkleProof[D <: Digest](indices: Seq[(Int, Digest)], proofs: Se
9091
}
9192

9293
val e = indices sortBy(_._1)
93-
loop(indices.map(_._1), e, proofs).head.sameElements(expectedRootHash)
94+
loop(indices.map(_._1), e, proofs) match {
95+
case root: Seq[Digest] if root.size == 1 => root.head.sameElements(expectedRootHash)
96+
case _ => false
97+
}
9498
}
99+
100+
override def equals(obj: Any): Boolean = obj match {
101+
case that: BatchMerkleProof[D] =>
102+
if (this.indices.size != that.indices.size ||
103+
this.proofs.size != that.proofs.size) {
104+
return false
105+
}
106+
for (i <- this.indices.indices) {
107+
if (this.indices.apply(i)._1 != that.indices.apply(i)._1 ||
108+
!util.Arrays.equals(this.indices.apply(i)._2, that.indices.apply(i)._2)) {
109+
return false
110+
}
111+
}
112+
for (i <- this.proofs.indices) {
113+
if (this.proofs.apply(i)._2 != that.proofs.apply(i)._2 ||
114+
!util.Arrays.equals(this.proofs.apply(i)._1, that.proofs.apply(i)._1)) {
115+
return false
116+
}
117+
}
118+
true
119+
case _ => false
120+
}
95121
}

src/main/scala/scorex/crypto/authds/merkle/MerkleTree.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ case class MerkleTree[D <: Digest](topNode: Node[D],
9595
m
9696
}
9797

98-
if (indices.forall(index => index >= 0 && index < length)) {
98+
if (indices.nonEmpty && indices.forall(index => index >= 0 && index < length)) {
9999
val hashes: Seq[Digest] = elementsHashIndex.toSeq.sortBy(_._2).map(_._1.toArray.asInstanceOf[Digest])
100100
val normalized_indices = indices.distinct.sorted
101101
val multiproof = loop(normalized_indices, hashes)

src/test/scala/scorex/crypto/authds/merkle/MerkleTreeSpecification.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ class MerkleTreeSpecification extends AnyPropSpec with ScalaCheckDrivenPropertyC
8282
tree.proofByIndices(Seq(2,10)) shouldBe None
8383
}
8484

85+
property("Empty Batch proof generation should be None") {
86+
val d = (0 until 10).map(_ => LeafData @@ scorex.utils.Random.randomBytes(LeafSize))
87+
val tree = MerkleTree(d)
88+
tree.proofByIndices(Seq.empty[Int]) shouldBe None
89+
}
90+
8591
property("Tree creation from 0 elements") {
8692
val tree = MerkleTree(Seq.empty)(hf)
8793
tree.rootHash shouldEqual Array.fill(hf.DigestSize)(0: Byte)

0 commit comments

Comments
 (0)