|
| 1 | +package org.tron.core.zksnark; |
| 2 | + |
| 3 | +import com.alibaba.fastjson.JSONArray; |
| 4 | +import com.google.common.base.Charsets; |
| 5 | +import com.google.common.io.Files; |
| 6 | +import com.google.protobuf.ByteString; |
| 7 | +import java.io.File; |
| 8 | +import java.util.List; |
| 9 | +import org.junit.AfterClass; |
| 10 | +import org.junit.Assert; |
| 11 | +import org.junit.BeforeClass; |
| 12 | +import org.junit.Test; |
| 13 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 14 | +import org.testng.collections.Lists; |
| 15 | +import org.tron.common.application.TronApplicationContext; |
| 16 | +import org.tron.common.crypto.zksnark.ZksnarkUtils; |
| 17 | +import org.tron.common.utils.ByteArray; |
| 18 | +import org.tron.common.utils.FileUtil; |
| 19 | +import org.tron.core.Wallet; |
| 20 | +import org.tron.core.capsule.IncrementalMerkleTreeCapsule; |
| 21 | +import org.tron.core.capsule.IncrementalMerkleVoucherCapsule; |
| 22 | +import org.tron.core.capsule.PedersenHashCapsule; |
| 23 | +import org.tron.core.config.DefaultConfig; |
| 24 | +import org.tron.core.config.args.Args; |
| 25 | +import org.tron.core.db.Manager; |
| 26 | +import org.tron.core.zen.merkle.IncrementalMerkleTreeContainer; |
| 27 | +import org.tron.core.zen.merkle.MerklePath; |
| 28 | +import org.tron.protos.Contract.PedersenHash; |
| 29 | + |
| 30 | +public class MerkleTreeTest { |
| 31 | + |
| 32 | + public static final long totalBalance = 1000_0000_000_000L; |
| 33 | + private static String dbPath = "output_ShieldedTransaction_test"; |
| 34 | + private static String dbDirectory = "db_ShieldedTransaction_test"; |
| 35 | + private static String indexDirectory = "index_ShieldedTransaction_test"; |
| 36 | + private static AnnotationConfigApplicationContext context; |
| 37 | + private static Manager dbManager; |
| 38 | + private static Wallet wallet; |
| 39 | + |
| 40 | + |
| 41 | + static { |
| 42 | + Args.setParam( |
| 43 | + new String[]{ |
| 44 | + "--output-directory", dbPath, |
| 45 | + "--storage-db-directory", dbDirectory, |
| 46 | + "--storage-index-directory", indexDirectory, |
| 47 | + "-w", |
| 48 | + "--debug" |
| 49 | + }, |
| 50 | + "config-test-mainnet.conf" |
| 51 | + ); |
| 52 | + context = new TronApplicationContext(DefaultConfig.class); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Init data. |
| 57 | + */ |
| 58 | + @BeforeClass |
| 59 | + public static void init() { |
| 60 | + dbManager = context.getBean(Manager.class); |
| 61 | + wallet = context.getBean(Wallet.class); |
| 62 | + //init energy |
| 63 | + dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(1526647838000L); |
| 64 | + dbManager.getDynamicPropertiesStore().saveTotalEnergyWeight(100_000L); |
| 65 | + |
| 66 | + dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(0); |
| 67 | + } |
| 68 | + |
| 69 | + @AfterClass |
| 70 | + public static void removeDb() { |
| 71 | + Args.clearParam(); |
| 72 | + context.destroy(); |
| 73 | + FileUtil.deleteDir(new File(dbPath)); |
| 74 | + } |
| 75 | + |
| 76 | + private JSONArray readFile(String fileName) throws Exception { |
| 77 | + String file1 = SendCoinShieldTest.class.getClassLoader() |
| 78 | + .getResource("json" + File.separator + fileName).getFile(); |
| 79 | + List<String> readLines = Files.readLines(new File(file1), |
| 80 | + Charsets.UTF_8); |
| 81 | + |
| 82 | + JSONArray array = JSONArray |
| 83 | + .parseArray(readLines.stream().reduce((s, s2) -> s + s2).get()); |
| 84 | + |
| 85 | + return array; |
| 86 | + } |
| 87 | + |
| 88 | + private String PedersenHash2String(PedersenHash hash) { |
| 89 | + return ByteArray.toHexString(hash.getContent().toByteArray()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testComplexTreePath() throws Exception { |
| 94 | + IncrementalMerkleTreeContainer.DEPTH = 4; |
| 95 | + |
| 96 | + JSONArray root_tests = readFile("merkle_roots_sapling.json"); |
| 97 | + JSONArray path_tests = readFile("merkle_path_sapling.json"); |
| 98 | + JSONArray commitment_tests = readFile("merkle_commitments_sapling.json"); |
| 99 | + |
| 100 | + int path_i = 0; |
| 101 | + |
| 102 | +// MerkleContainer merkleContainer = new MerkleContainer(); |
| 103 | +// merkleContainer.getCurrentMerkle(); |
| 104 | + |
| 105 | + IncrementalMerkleTreeContainer tree = new IncrementalMerkleTreeCapsule() |
| 106 | + .toMerkleTreeContainer(); |
| 107 | + |
| 108 | + // The root of the tree at this point is expected to be the root of the |
| 109 | + // empty tree. |
| 110 | + Assert.assertEquals(PedersenHash2String(tree.root()), |
| 111 | + PedersenHash2String(IncrementalMerkleTreeContainer.emptyRoot())); |
| 112 | + |
| 113 | + try { |
| 114 | + tree.last(); |
| 115 | + Assert.fail("The tree doesn't have a 'last' element added since it's blank."); |
| 116 | + } catch (Exception ex) { |
| 117 | + |
| 118 | + } |
| 119 | + // The tree is empty. |
| 120 | + Assert.assertEquals(0, tree.size()); |
| 121 | + |
| 122 | + // We need to witness at every single point in the tree, so |
| 123 | + // that the consistency of the tree and the merkle paths can |
| 124 | + // be checked. |
| 125 | + List<IncrementalMerkleVoucherCapsule> witnesses = Lists.newArrayList(); |
| 126 | + |
| 127 | + for (int i = 0; i < 16; i++) { |
| 128 | + // Witness here |
| 129 | + witnesses.add(tree.toVoucher().getVoucherCapsule()); |
| 130 | + |
| 131 | + PedersenHashCapsule test_commitment = new PedersenHashCapsule(); |
| 132 | + byte[] bytes = ByteArray.fromHexString(commitment_tests.getString(i)); |
| 133 | + ZksnarkUtils.sort(bytes); |
| 134 | + test_commitment.setContent(ByteString.copyFrom(bytes)); |
| 135 | + // Now append a commitment to the tree |
| 136 | + tree.append(test_commitment.getInstance()); |
| 137 | + |
| 138 | + // Size incremented by one. |
| 139 | + Assert.assertEquals(i + 1, tree.size()); |
| 140 | + |
| 141 | + // Last element added to the tree was `test_commitment` |
| 142 | + Assert.assertEquals(PedersenHash2String(test_commitment.getInstance()), |
| 143 | + PedersenHash2String(tree.last())); |
| 144 | + |
| 145 | + //todo: |
| 146 | + // Check tree root consistency |
| 147 | + Assert.assertEquals(root_tests.getString(i), |
| 148 | + PedersenHash2String(tree.root())); |
| 149 | + |
| 150 | + // Check serialization of tree |
| 151 | +// expect_ser_test_vector(ser_tests[i], tree, tree); |
| 152 | + |
| 153 | + boolean first = true; // The first witness can never form a path |
| 154 | + for (IncrementalMerkleVoucherCapsule wit : witnesses) { |
| 155 | + // Append the same commitment to all the witnesses |
| 156 | + wit.toMerkleVoucherContainer().append(test_commitment.getInstance()); |
| 157 | + |
| 158 | + if (first) { |
| 159 | + try { |
| 160 | + wit.toMerkleVoucherContainer().path(); |
| 161 | + Assert.fail("The first witness can never form a path"); |
| 162 | + } catch (Exception ex) { |
| 163 | + |
| 164 | + } |
| 165 | + |
| 166 | + try { |
| 167 | + wit.toMerkleVoucherContainer().element(); |
| 168 | + Assert.fail("The first witness can never form a path"); |
| 169 | + } catch (Exception ex) { |
| 170 | + |
| 171 | + } |
| 172 | + } else { |
| 173 | + MerklePath path = wit.toMerkleVoucherContainer().path(); |
| 174 | + Assert.assertEquals(path_tests.getString(path_i++), ByteArray.toHexString(path.encode())); |
| 175 | + } |
| 176 | + |
| 177 | + Assert.assertEquals( |
| 178 | + PedersenHash2String(wit.toMerkleVoucherContainer().root()), |
| 179 | + PedersenHash2String(tree.root())); |
| 180 | + |
| 181 | + first = false; |
| 182 | + } |
| 183 | + } |
| 184 | + try { |
| 185 | + tree.append(new PedersenHashCapsule().getInstance()); |
| 186 | + Assert.fail("Tree should be full now"); |
| 187 | + } catch (Exception ex) { |
| 188 | + |
| 189 | + } |
| 190 | + |
| 191 | + for (IncrementalMerkleVoucherCapsule wit : witnesses) { |
| 192 | + try { |
| 193 | + wit.toMerkleVoucherContainer().append(new PedersenHashCapsule().getInstance()); |
| 194 | + Assert.fail("Tree should be full now"); |
| 195 | + } catch (Exception ex) { |
| 196 | + |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | +} |
0 commit comments