Search in sources :

Example 16 with MerkleProof

use of com.jd.blockchain.ledger.MerkleProof in project jdchain-core by blockchain-jd-com.

the class MerkleHashSortTreeTest method testMerkleProofCorrectness.

/**
 * 测试 Merkle 证明的正确性;
 */
// TODO: 暂时注释掉默克尔证明相关的内容;
// @Test
public void testMerkleProofCorrectness() {
    // 长度为 0 的情况;
    int count = 0;
    // System.out.printf("\r\n\r\n================= %s 个节点 =================\r\n\r\n", count);
    List<VersioningKVData<String, byte[]>> dataList = generateDatas(count);
    VersioningKVData<String, byte[]>[] datas = toArray(dataList);
    MerkleHashSortTree merkleTree = newMerkleTree_with_committed(datas);
    HashDigest rootHash0 = merkleTree.getRootHash();
    assertNotNull(rootHash0);
    // 预期空的默克尔树中查询任何数据的证明都获得 null 返回;
    MerkleProof proof = merkleTree.getProof("KEY-0");
    assertNull(proof);
    // 长度为 1 的情况;
    count = 1;
    // System.out.printf("\r\n\r\n================= %s 个节点 =================\r\n\r\n", count);
    dataList = generateDatas(count);
    datas = toArray(dataList);
    merkleTree = newMerkleTree_with_committed(datas);
    HashDigest rootHash1 = merkleTree.getRootHash();
    assertNotNull(rootHash1);
    // 预期在只有 1 条数据的情况下可以正常得到该数据的默克尔证明;
    MerkleProof proof1_0 = merkleTree.getProof("KEY-0");
    assertNotNull(proof1_0);
    // 依照设计,预期任何默克尔证明都至少有 4 条路径;
    assertMerkleProofPath(proof1_0, merkleTree.getRootHash(), merkleTree.getData("KEY-0"));
    // 长度为 2 的情况;
    count = 2;
    // System.out.printf("\r\n\r\n================= %s 个节点 =================\r\n\r\n", count);
    dataList = generateDatas(count);
    datas = toArray(dataList);
    merkleTree = newMerkleTree_with_committed(datas);
    HashDigest rootHash2 = merkleTree.getRootHash();
    assertNotNull(rootHash2);
    MerkleProof proof2_0 = merkleTree.getProof("KEY-0");
    assertNotNull(proof2_0);
    // 依照设计,预期任何默克尔证明都至少有 4 条路径;
    assertMerkleProofPath(proof2_0, merkleTree.getRootHash(), merkleTree.getData("KEY-0"));
    // 长度为 16 的情况;
    count = 16;
    // System.out.printf("\r\n\r\n================= %s 个节点 =================\r\n\r\n", count);
    dataList = generateDatas(count);
    datas = toArray(dataList);
    merkleTree = newMerkleTree_with_committed(datas);
    HashDigest rootHash16 = merkleTree.getRootHash();
    assertNotNull(rootHash16);
    MerkleProof proof16_0 = merkleTree.getProof("KEY-0");
    assertNotNull(proof16_0);
    // 依照设计,预期任何默克尔证明都至少有 4 条路径;
    assertMerkleProofPath(proof16_0, merkleTree.getRootHash(), merkleTree.getData("KEY-0"));
    // 长度为 32 的情况;
    count = 32;
    // System.out.printf("\r\n\r\n================= %s 个节点 =================\r\n\r\n", count);
    dataList = generateDatas(count);
    datas = toArray(dataList);
    merkleTree = newMerkleTree_with_committed(datas);
    HashDigest rootHash32 = merkleTree.getRootHash();
    assertNotNull(rootHash32);
    MerkleProof proof32_0 = merkleTree.getProof("KEY-0");
    assertNotNull(proof32_0);
    // 依照设计,预期任何默克尔证明都至少有 4 条路径;
    assertMerkleProofPath(proof32_0, merkleTree.getRootHash(), merkleTree.getData("KEY-0"));
    // 长度为 1025 的情况;
    count = 1025;
    // System.out.printf("\r\n\r\n================= %s 个节点 =================\r\n\r\n", count);
    dataList = generateDatas(count);
    datas = toArray(dataList);
    merkleTree = newMerkleTree_with_committed(datas);
    HashDigest rootHash1025 = merkleTree.getRootHash();
    assertNotNull(rootHash1025);
    MerkleProof proof1025 = merkleTree.getProof("KEY-0");
    assertNotNull(proof1025);
    // 依照设计,预期任何默克尔证明都至少有 4 条路径;
    assertMerkleProofPath(proof1025, merkleTree.getRootHash(), merkleTree.getData("KEY-0"));
}
Also used : HashDigest(com.jd.blockchain.crypto.HashDigest) MerkleHashSortTree(com.jd.blockchain.ledger.merkletree.MerkleHashSortTree) MerkleProof(com.jd.blockchain.ledger.MerkleProof) VersioningKVData(com.jd.blockchain.storage.service.utils.VersioningKVData)

Aggregations

MerkleProof (com.jd.blockchain.ledger.MerkleProof)16 HashDigest (com.jd.blockchain.crypto.HashDigest)8 Bytes (utils.Bytes)6 Test (org.junit.Test)4 VersioningKVData (com.jd.blockchain.storage.service.utils.VersioningKVData)3 Random (java.util.Random)3 CryptoSetting (com.jd.blockchain.ledger.CryptoSetting)2 MerkleHashDataset (com.jd.blockchain.ledger.core.MerkleHashDataset)2 MerkleHashTrie (com.jd.blockchain.ledger.proof.MerkleHashTrie)2 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 DataEntry (utils.DataEntry)2 Dataset (utils.Dataset)2 CryptoProvider (com.jd.blockchain.crypto.CryptoProvider)1 MerkleDataNode (com.jd.blockchain.ledger.MerkleDataNode)1 MerkleNode (com.jd.blockchain.ledger.MerkleNode)1 CryptoConfig (com.jd.blockchain.ledger.core.CryptoConfig)1 MerkleHashSortTree (com.jd.blockchain.ledger.merkletree.MerkleHashSortTree)1