@@ -554,7 +554,40 @@ class MerklePatriciaTrieSuite extends AnyFunSuite with ScalaCheckPropertyChecks
554
554
assert(proof.isEmpty)
555
555
}
556
556
557
- test(" getProof returns proof result for non-existing address" ) {
557
+ test(" PatriciaTrie can get proof(at least the root node) for all inserted key-value pairs" ) {
558
+ forAll(keyValueListGen()) { keyValueList : Seq [(Int , Int )] =>
559
+ val trie = addEveryKeyValuePair(keyValueList)
560
+ assertCanGetProofForEveryKeyValue(trie, keyValueList)
561
+ }
562
+ }
563
+
564
+ test(" PatriciaTrie return root as proof when no common nibbles are found between MPT root hash and search key" ) {
565
+ forAll(keyValueListGen(1 , 10 )) { keyValueList : Seq [(Int , Int )] =>
566
+ val trie = addEveryKeyValuePair(keyValueList)
567
+ val wrongKey = 22
568
+ val proof = trie.getProof(wrongKey)
569
+ assert(proof.getOrElse(Vector .empty).toList match {
570
+ case _ @ HashNode (_) :: Nil => true
571
+ case _ => false
572
+ })
573
+ }
574
+ }
575
+
576
+ test(" PatriciaTrie return proof when having all nibbles in common except the last one between MPT root hash and search key" ) {
577
+
578
+ val key = 1111
579
+ val wrongKey = 1112
580
+ val emptyTrie = MerklePatriciaTrie [Int , Int ](emptyEphemNodeStorage)
581
+ .put(key, 1 )
582
+ .put(wrongKey, 2 )
583
+ val proof = emptyTrie.getProof(key = wrongKey)
584
+ assert(proof.getOrElse(Vector .empty).toList match {
585
+ case _ @ HashNode (_) :: tail => tail.nonEmpty
586
+ case _ => false
587
+ })
588
+ }
589
+
590
+ test(" getProof returns proof result for non-existing key" ) {
558
591
// given
559
592
val EmptyTrie = MerklePatriciaTrie [Array [Byte ], Array [Byte ]](emptyEphemNodeStorage)
560
593
val key1 : Array [Byte ] = Hex .decode(" 10000001" )
@@ -611,6 +644,12 @@ class MerklePatriciaTrieSuite extends AnyFunSuite with ScalaCheckPropertyChecks
611
644
assert(obtained.get == value)
612
645
}
613
646
647
+ private def assertCanGetProofForEveryKeyValue [K , V ](trie : MerklePatriciaTrie [K , V ], kvs : Seq [(K , V )]): Unit =
648
+ kvs.foreach { case (key, _) =>
649
+ val obtained = trie.getProof(key)
650
+ assert(obtained.getOrElse(Vector .empty).nonEmpty)
651
+ }
652
+
614
653
private def assertCanGetEveryKeyValues [K , V ](trie : MerklePatriciaTrie [K , Array [V ]], kvs : List [(K , Array [V ])]): Unit =
615
654
kvs.foreach { case (key, value) =>
616
655
val obtained = trie.get(key)
0 commit comments