From 317b98b6b09ce213c8d0de3a8a210a8d495bfb39 Mon Sep 17 00:00:00 2001
From: Benoit Lavenier <benoit.lavenier@e-is.pro>
Date: Mon, 21 Aug 2023 10:21:59 +0200
Subject: [PATCH] fix: Merkle tree: do not try to read rows if no one exists

---
 .../core/util/merkle/MerkleTreeTest.java      | 100 ++++++++++--------
 1 file changed, 56 insertions(+), 44 deletions(-)

diff --git a/duniter4j-core-shared/src/test/java/org/duniter/core/util/merkle/MerkleTreeTest.java b/duniter4j-core-shared/src/test/java/org/duniter/core/util/merkle/MerkleTreeTest.java
index 7b8094d7..0ca732d6 100644
--- a/duniter4j-core-shared/src/test/java/org/duniter/core/util/merkle/MerkleTreeTest.java
+++ b/duniter4j-core-shared/src/test/java/org/duniter/core/util/merkle/MerkleTreeTest.java
@@ -25,25 +25,21 @@ package org.duniter.core.util.merkle;
 import com.google.common.collect.ImmutableList;
 import org.junit.Assert;
 import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import java.util.List;
 
 public class MerkleTreeTest {
 
-    private static final Logger log = LoggerFactory.getLogger(MerkleTreeTest.class);
-
     @Test
     public void createMerkleTree() {
 
         MerkleTree tree;
         List<String> abcde = ImmutableList.of(
-                "a",
-                "b",
-                "c",
-                "d",
-                "e"
+            "a",
+            "b",
+            "c",
+            "d",
+            "e"
         );
 
         // SHA1
@@ -93,41 +89,41 @@ public class MerkleTreeTest {
 
         MerkleTree tree;
         List<String> peersHash = ImmutableList.of(
-                "0DC0D9D029FF6164866DC7DF256A15D6572C649B4B634629D4C6E8A2066005C8",
-                "8EEA5E37DDC4074FE7E51A2BC347A34AC1A83ED1722411A65B61D8B848344E54",
-                "706424BC360BA6F3F9ED1EC2AADA886D9082C2597874773402376B46EC77A627",
-                "96FE4D8F1EB72260B9914A0134051E8E711EE1EC40ACAF08D9EF1A9A4CBED45D",
-                "9F6F5B3A9AD734F657478D5D6966AE7E672FACD0520EAB929BB4E2A286B1EA9C",
-                "292F985A37CC7DDAF1489919B3688A97D9C26AA3813D72029C395DED960B45D1",
-                "8BC5139CF21E3369BD5F167EC0975415289ACD4E685A41661D7FC87EB099FD9A",
-                "26274CF9EE574DC9C488968957219700430388DF9FA42D9C59EB9B423DA2B19E",
-                "D404CFEB173ADBD249093CA720442A3BC45BF30A6F0E474A12E8F3DC38F7EB3C",
-                "81466D5012223534DD73333E40E09F439821597A329C37E243FA4C1825569037",
-                "916DED5460AD10C9703D00C94CBA98ADA57C70E99909F399E07E58044974D54A",
-                "E9E7E2EFAB5D17627BAF6EC5BC01D125FF0499B9527080A00CA3920CF9051482",
-                "34F8F4DA28515E65C422014124D9AA1B130BC6ECA1DA0EED612BE1D693F34FFF",
-                "FC7ED841C6BB4A7B52F28AE4F39E5696F17779998CF5894DBF456BD243C31760",
-                "DEF950E508A316DD71A1EF808B86C2F65354A5F81B82B66C22FF586966E18DD8",
-                "D02E24D9947591D3074674C5DE69B86BD48B5C344F125CEF9392794822EAE62C",
-                "2D914E851C90739609988FF3EC37A2360AFB8E8EA58FD1B1D747E4B391227964",
-                "2F1984E0ABF7702346211402653E1C9DEE8C1FF6AEFE449D88D608985AA08740",
-                "8870EC1E8763F77804B1A30616E68A0FEA8B4A1185EDCF89AA4B14545BB59AA7",
-                "788CD0E2510C1F37299A10756FDF52B91465014069EA87A8CDC44A52EA52047D",
-                "607B251FFEB53714AC36527CFB71D60DD9BD073134DF833C69BAF5D24BF0A340",
-                "0CFC52A8811D04A692E49008E5019E37055DCA80115A62A6FFEAEFBE9E837044",
-                "1C58D24C807B332986876CE92413618A9827E8A9E5D9D134D59E8BC123C4165A",
-                "F9816088514831D59D9242E8D00F352692C52FCBCABD524A576FD68CB5DE4CA4",
-                "2DDD2A4F9A44324FEC9C4D33F01C947A003EEBA5AE9C982EA21EFC087ECBC5E5",
-                "521499BF20AB21D5C1998FBBE28182746593191BF6E8B9A81FB921E8F1F88E82",
-                "7B4F84385064002780D578296C2DA570B4E8A1C82CF3657A6EE4A58148D72B6A",
-                "FB589D44B4E6EF3513CAE2314840A4B9C736470E374784575343A4F66C039B37",
-                "E8F486AD89E69ED2EE5D5DAFB7F985CD5EB011AE7425BB19324E08A53AE3CE14",
-                "DB3357D2E09CFD994D076B4F5D4E720320DA8C701440C282DFA479DC7E26E116",
-                "15AD4713526DCCC1D46F38A2E0B01D48EE6048818F4604D08CB38EC14A8F6D70",
-                "C2E41814704D8B4FD95C77EF2129DBB9774C244F7D946B89C0191FD0F690F012",
-                "820C470B1D68CF6AF9EAA478F43A7DF4AE9FC43E198DC0F159B2A8D80845E9CD",
-                "EADF749753B422E7A198168005FE1BF846B164C5BEC2CA0446B4781883B720E0",
-                "D7C99BD78A76CF8F0D0BEF87C9120C6717EAF12BD6E55611BCA3EF070637758C"
+            "0DC0D9D029FF6164866DC7DF256A15D6572C649B4B634629D4C6E8A2066005C8",
+            "8EEA5E37DDC4074FE7E51A2BC347A34AC1A83ED1722411A65B61D8B848344E54",
+            "706424BC360BA6F3F9ED1EC2AADA886D9082C2597874773402376B46EC77A627",
+            "96FE4D8F1EB72260B9914A0134051E8E711EE1EC40ACAF08D9EF1A9A4CBED45D",
+            "9F6F5B3A9AD734F657478D5D6966AE7E672FACD0520EAB929BB4E2A286B1EA9C",
+            "292F985A37CC7DDAF1489919B3688A97D9C26AA3813D72029C395DED960B45D1",
+            "8BC5139CF21E3369BD5F167EC0975415289ACD4E685A41661D7FC87EB099FD9A",
+            "26274CF9EE574DC9C488968957219700430388DF9FA42D9C59EB9B423DA2B19E",
+            "D404CFEB173ADBD249093CA720442A3BC45BF30A6F0E474A12E8F3DC38F7EB3C",
+            "81466D5012223534DD73333E40E09F439821597A329C37E243FA4C1825569037",
+            "916DED5460AD10C9703D00C94CBA98ADA57C70E99909F399E07E58044974D54A",
+            "E9E7E2EFAB5D17627BAF6EC5BC01D125FF0499B9527080A00CA3920CF9051482",
+            "34F8F4DA28515E65C422014124D9AA1B130BC6ECA1DA0EED612BE1D693F34FFF",
+            "FC7ED841C6BB4A7B52F28AE4F39E5696F17779998CF5894DBF456BD243C31760",
+            "DEF950E508A316DD71A1EF808B86C2F65354A5F81B82B66C22FF586966E18DD8",
+            "D02E24D9947591D3074674C5DE69B86BD48B5C344F125CEF9392794822EAE62C",
+            "2D914E851C90739609988FF3EC37A2360AFB8E8EA58FD1B1D747E4B391227964",
+            "2F1984E0ABF7702346211402653E1C9DEE8C1FF6AEFE449D88D608985AA08740",
+            "8870EC1E8763F77804B1A30616E68A0FEA8B4A1185EDCF89AA4B14545BB59AA7",
+            "788CD0E2510C1F37299A10756FDF52B91465014069EA87A8CDC44A52EA52047D",
+            "607B251FFEB53714AC36527CFB71D60DD9BD073134DF833C69BAF5D24BF0A340",
+            "0CFC52A8811D04A692E49008E5019E37055DCA80115A62A6FFEAEFBE9E837044",
+            "1C58D24C807B332986876CE92413618A9827E8A9E5D9D134D59E8BC123C4165A",
+            "F9816088514831D59D9242E8D00F352692C52FCBCABD524A576FD68CB5DE4CA4",
+            "2DDD2A4F9A44324FEC9C4D33F01C947A003EEBA5AE9C982EA21EFC087ECBC5E5",
+            "521499BF20AB21D5C1998FBBE28182746593191BF6E8B9A81FB921E8F1F88E82",
+            "7B4F84385064002780D578296C2DA570B4E8A1C82CF3657A6EE4A58148D72B6A",
+            "FB589D44B4E6EF3513CAE2314840A4B9C736470E374784575343A4F66C039B37",
+            "E8F486AD89E69ED2EE5D5DAFB7F985CD5EB011AE7425BB19324E08A53AE3CE14",
+            "DB3357D2E09CFD994D076B4F5D4E720320DA8C701440C282DFA479DC7E26E116",
+            "15AD4713526DCCC1D46F38A2E0B01D48EE6048818F4604D08CB38EC14A8F6D70",
+            "C2E41814704D8B4FD95C77EF2129DBB9774C244F7D946B89C0191FD0F690F012",
+            "820C470B1D68CF6AF9EAA478F43A7DF4AE9FC43E198DC0F159B2A8D80845E9CD",
+            "EADF749753B422E7A198168005FE1BF846B164C5BEC2CA0446B4781883B720E0",
+            "D7C99BD78A76CF8F0D0BEF87C9120C6717EAF12BD6E55611BCA3EF070637758C"
         );
 
         tree = new MerkleTree("sha256", peersHash, true);
@@ -136,4 +132,20 @@ public class MerkleTreeTest {
         Assert.assertEquals(6, tree.depth());
         Assert.assertEquals(38, tree.nodes());
     }
+
+
+    @Test
+    public void createEmptyMerkleTree() {
+
+        MerkleTree tree;
+        List<String> emptyList = ImmutableList.of();
+
+        // SHA1
+        {
+            tree = new MerkleTree("sha1", emptyList, true);
+            Assert.assertNotNull(tree);
+            Assert.assertNull(tree.root());
+
+        }
+    }
 }
-- 
GitLab