Search in sources :

Example 6 with MerkleProof

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

the class MerkleHashTrieTest method testCreation.

/**
 * 测试树的创建的正确性;
 */
@Test
public void testCreation() {
    // 数据集合长度为 0 时也能正常生成;
    List<VersioningKVData<String, byte[]>> dataList = generateDatas(0);
    VersioningKVData<String, byte[]>[] datas = toArray(dataList);
    MerkleHashTrie merkleTree = newMerkleTree_with_committed(datas);
    HashDigest rootHash = merkleTree.getRootHash();
    assertNotNull(rootHash);
    assertEquals(0, merkleTree.getTotalKeys());
    assertEquals(0, merkleTree.getTotalRecords());
    MerkleProof proof = merkleTree.getProof("KEY_NOT_EXIST");
    assertNull(proof);
    // 数据集合长度为 1 时也能正常生成;
    dataList = generateDatas(1);
    datas = toArray(dataList);
    merkleTree = newMerkleTree_with_committed(datas);
    rootHash = merkleTree.getRootHash();
    assertNotNull(rootHash);
    assertEquals(1, merkleTree.getTotalKeys());
    assertEquals(1, merkleTree.getTotalRecords());
    // TODO: 暂时忽略默克尔证明的测试;
    // 默克尔证明路径的长度至少为 4 ——包括:根节点/叶子节点/数据节点/值哈希;
    // assertMerkleProofAndProofLength(datas[0], merkleTree, 4);
    // 数据集合长度为 2 时也能正常生成;
    dataList = generateDatas(2);
    datas = toArray(dataList);
    merkleTree = newMerkleTree_with_committed(datas);
    rootHash = merkleTree.getRootHash();
    assertNotNull(rootHash);
    assertEquals(2, merkleTree.getTotalKeys());
    assertEquals(2, merkleTree.getTotalRecords());
    // TODO: 暂时忽略默克尔证明的测试;
    // assertMerkleProofAndProofLength(datas[0], merkleTree, 4);
    // assertMerkleProofAndProofLength(datas[1], merkleTree, 4);
    // 数据集合长度为 100 时也能正常生成;
    dataList = generateDatas(100);
    datas = toArray(dataList);
    merkleTree = newMerkleTree_with_committed(datas);
    rootHash = merkleTree.getRootHash();
    assertNotNull(rootHash);
    assertEquals(100, merkleTree.getTotalKeys());
    assertEquals(100, merkleTree.getTotalRecords());
    // 数据集合长度为 1024 时也能正常生成;
    int count = 1024;
    dataList = generateDatas(count);
    datas = toArray(dataList);
    merkleTree = newMerkleTree_with_committed(datas);
    rootHash = merkleTree.getRootHash();
    assertNotNull(rootHash);
    assertEquals(count, merkleTree.getTotalKeys());
    assertEquals(count, merkleTree.getTotalRecords());
    // merkleTree.print();
    // TODO: 暂时忽略默克尔证明的测试;
    // for (VersioningKVData<String, byte[]> data : datas) {
    // assertMerkleProof(data, merkleTree);
    // }
    // testMerkleProof1024(datas, merkleTree);
    // 数据集合长度为 20000 时也能正常生成;
    count = 20000;
    dataList = generateDatas(count);
    datas = toArray(dataList);
    merkleTree = newMerkleTree_with_committed(datas);
    rootHash = merkleTree.getRootHash();
    assertNotNull(rootHash);
    assertEquals(count, merkleTree.getTotalKeys());
    assertEquals(count, merkleTree.getTotalRecords());
// merkleTree.print();
// TODO: 暂时忽略默克尔证明的测试;
// for (VersioningKVData<String, byte[]> data : datas) {
// assertMerkleProof(data, merkleTree);
// }
}
Also used : HashDigest(com.jd.blockchain.crypto.HashDigest) MerkleHashTrie(com.jd.blockchain.ledger.proof.MerkleHashTrie) MerkleProof(com.jd.blockchain.ledger.MerkleProof) VersioningKVData(com.jd.blockchain.storage.service.utils.VersioningKVData) Test(org.junit.Test)

Example 7 with MerkleProof

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

the class MerkleHashTrieTest method assertMerkleProofAndProofLength.

private void assertMerkleProofAndProofLength(VersioningKVData<String, byte[]> data, MerkleTree merkleTree, int expectProofPathCount) {
    MerkleProof proof = assertMerkleProof(data, merkleTree);
    assertEquals(expectProofPathCount, proof.getProofLevels().length);
}
Also used : MerkleProof(com.jd.blockchain.ledger.MerkleProof)

Example 8 with MerkleProof

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

the class MerkleHashSortTreeTest method assertMerkleProofAndProofLength.

private void assertMerkleProofAndProofLength(VersioningKVData<String, byte[]> data, MerkleTree merkleTree, int expectProofPathCount) {
    MerkleProof proof = assertMerkleProof(data, merkleTree);
    assertEquals(expectProofPathCount, proof.getProofLevels().length);
}
Also used : MerkleProof(com.jd.blockchain.ledger.MerkleProof)

Example 9 with MerkleProof

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

the class MerkleSequenceDataset method getDataProof.

public MerkleDataProof getDataProof(Bytes key) {
    DataEntry<Bytes, byte[]> dataEntry = getDataEntry(key);
    if (dataEntry == null) {
        return null;
    }
    MerkleProof proof = getProof(key);
    return new MerkleDataEntryWrapper(dataEntry, proof);
}
Also used : Bytes(utils.Bytes) MerkleProof(com.jd.blockchain.ledger.MerkleProof)

Example 10 with MerkleProof

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

the class MerkleHashDataset method getDataProof.

public MerkleDataProof getDataProof(Bytes key, long version) {
    DataEntry<Bytes, byte[]> dataEntry = getDataEntry(key, version);
    if (dataEntry == null) {
        return null;
    }
    MerkleProof proof = getProof(key);
    return new MerkleDataEntryWrapper(dataEntry, proof);
}
Also used : Bytes(utils.Bytes) MerkleProof(com.jd.blockchain.ledger.MerkleProof)

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