Search in sources :

Example 16 with KVEntry

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

the class MerkleHashSortTreeTest method assertDataExist.

private void assertDataExist(MerkleHashSortTree merkleTree, VersioningKVData<String, byte[]>[] datas) {
    for (int i = 0; i < datas.length; i++) {
        KVEntry kv = merkleTree.getData(datas[i].getKey());
        assertNotNull(kv);
        assertEquals(datas[i].getKey(), kv.getKey().toString("UTF-8"));
        assertEquals(datas[i].getVersion(), kv.getVersion());
        assertArrayEquals(datas[i].getValue(), kv.getValue().toBytes());
    }
}
Also used : KVEntry(com.jd.blockchain.ledger.merkletree.KVEntry)

Example 17 with KVEntry

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

the class MerkleHashSortTreeTest method assertDataNotExist.

/**
 * 断言指定的默克尔树不存在指定键的数据,或者不存在指定键的指定版本的数据;
 *
 * @param merkleTree
 * @param datas
 */
private void assertDataNotExist(MerkleHashSortTree merkleTree, VersioningKVData<String, byte[]>[] datas) {
    for (int i = 0; i < datas.length; i++) {
        KVEntry kv = merkleTree.getData(datas[i].getKey());
        // 数据不存在,或者指定版本的数据不存在;
        assertTrue(kv == null || kv.getVersion() < datas[i].getVersion());
    }
}
Also used : KVEntry(com.jd.blockchain.ledger.merkletree.KVEntry)

Example 18 with KVEntry

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

the class MerkleHashSortTreeTest method testDataIterator.

/**
 * 测试浏览默克尔树的所有数据节点;
 */
@Test
public void testDataIterator() {
    int count = 1024;
    List<VersioningKVData<String, byte[]>> dataList = generateDatas(count);
    VersioningKVData<String, byte[]>[] datas = toArray(dataList);
    MerkleHashSortTree merkleTree = newMerkleTree_with_committed(datas);
    HashDigest rootHash = merkleTree.getRootHash();
    assertNotNull(rootHash);
    assertEquals(count, merkleTree.getTotalKeys());
    Map<String, VersioningKVData<String, byte[]>> dataMap = new HashMap<String, VersioningKVData<String, byte[]>>();
    for (VersioningKVData<String, byte[]> data : datas) {
        dataMap.put(data.getKey(), data);
    }
    Iterator<KVEntry> dataIterator = merkleTree.iterator();
    String[] dataKeys = new String[count];
    int index = 0;
    while (dataIterator.hasNext()) {
        KVEntry data = dataIterator.next();
        assertNotNull(data);
        String key = data.getKey().toUTF8String();
        assertTrue(dataMap.containsKey(key));
        dataMap.remove(key);
        dataKeys[index] = key;
        index++;
    }
    assertEquals(0, dataMap.size());
    assertEquals(count, index);
    SkippingIterator<KVEntry> skippingIterator = merkleTree.iterator();
    testDataIteratorSkipping(dataKeys, skippingIterator, 0);
    skippingIterator = merkleTree.iterator();
    testDataIteratorSkipping(dataKeys, skippingIterator, 1);
    skippingIterator = merkleTree.iterator();
    testDataIteratorSkipping(dataKeys, skippingIterator, 2);
    skippingIterator = merkleTree.iterator();
    testDataIteratorSkipping(dataKeys, skippingIterator, 16);
    skippingIterator = merkleTree.iterator();
    testDataIteratorSkipping(dataKeys, skippingIterator, 128);
    skippingIterator = merkleTree.iterator();
    testDataIteratorSkipping(dataKeys, skippingIterator, 1023);
    skippingIterator = merkleTree.iterator();
    testDataIteratorSkipping(dataKeys, skippingIterator, 1024);
}
Also used : HashMap(java.util.HashMap) VersioningKVData(com.jd.blockchain.storage.service.utils.VersioningKVData) KVEntry(com.jd.blockchain.ledger.merkletree.KVEntry) HashDigest(com.jd.blockchain.crypto.HashDigest) MerkleHashSortTree(com.jd.blockchain.ledger.merkletree.MerkleHashSortTree) Test(org.junit.Test)

Example 19 with KVEntry

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

the class MerkleHashSortTreeTest method testDataIteratorSkipping.

private void testDataIteratorSkipping(String[] expectedKeys, SkippingIterator<KVEntry> iterator, int skip) {
    int count = expectedKeys.length;
    int index = skip;
    iterator.skip(index);
    if (skip < count) {
        assertTrue(iterator.hasNext());
    } else {
        assertFalse(iterator.hasNext());
    }
    while (iterator.hasNext()) {
        KVEntry data = iterator.next();
        assertNotNull(data);
        String key = data.getKey().toUTF8String();
        assertEquals(expectedKeys[index], key);
        index++;
    }
    assertEquals(count, index);
}
Also used : KVEntry(com.jd.blockchain.ledger.merkletree.KVEntry)

Example 20 with KVEntry

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

the class MerkleHashSortTreeTest method testReloading.

/**
 * 测试树的加载读取;
 */
@Test
public void testReloading() {
    TreeOptions cryptoSetting = createTreeOptions();
    MemoryKVStorage storage = new MemoryKVStorage();
    // 数据集合长度为 100 时也能正常生成;
    int count = 1024;
    List<VersioningKVData<String, byte[]>> dataList = generateDatas(count);
    VersioningKVData<String, byte[]>[] datas = toArray(dataList);
    MerkleHashSortTree merkleTree = newMerkleTree_with_committed(datas, cryptoSetting, storage);
    HashDigest rootHash0 = merkleTree.getRootHash();
    assertNotNull(rootHash0);
    assertEquals(count, merkleTree.getTotalKeys());
    // TODO: 暂时注释掉默克尔证明相关的内容;
    // for (VersioningKVData<String, byte[]> data : datas) {
    // assertMerkleProof(data, merkleTree);
    // }
    // testMerkleProof1024(datas, merkleTree);
    MerkleHashSortTree merkleTree_reload = new MerkleHashSortTree(rootHash0, cryptoSetting, KEY_PREFIX, storage);
    assertEquals(count, merkleTree_reload.getTotalKeys());
    // TODO: 暂时注释掉默克尔证明相关的内容;
    // testMerkleProof1024(datas, merkleTree_reload);
    VersioningKVData<String, byte[]> data28 = new VersioningKVData<String, byte[]>("KEY-28", 1, BytesUtils.toBytes("NEW-VALUE-VERSION-1"));
    VersioningKVData<String, byte[]> data606 = new VersioningKVData<String, byte[]>("KEY-606", 1, BytesUtils.toBytes("NEW-VALUE-VERSION-1"));
    VersioningKVData<String, byte[]> data770 = new VersioningKVData<String, byte[]>("KEY-770", 1, BytesUtils.toBytes("NEW-VALUE-VERSION-1"));
    VersioningKVData<String, byte[]> data898 = new VersioningKVData<String, byte[]>("KEY-898", 1, BytesUtils.toBytes("NEW-VALUE-VERSION-1"));
    VersioningKVData<String, byte[]> data69 = new VersioningKVData<String, byte[]>("KEY-69", 1, BytesUtils.toBytes("NEW-VALUE-VERSION-1"));
    merkleTree_reload.setData(data28.getKey(), data28.getVersion(), data28.getValue());
    merkleTree_reload.setData(data606.getKey(), data606.getVersion(), data606.getValue());
    merkleTree_reload.setData(data770.getKey(), data770.getVersion(), data770.getValue());
    merkleTree_reload.setData(data898.getKey(), data898.getVersion(), data898.getValue());
    merkleTree_reload.setData(data69.getKey(), data69.getVersion(), data69.getValue());
    merkleTree_reload.commit();
    HashDigest rootHash1 = merkleTree_reload.getRootHash();
    assertNotNull(rootHash1);
    assertNotEquals(rootHash0, rootHash1);
    // TODO: 暂时注释掉默克尔证明相关的内容;
    // MerkleProof proof = merkleTree_reload.getProof(data28.getKey(), 1);
    // assertNotNull(proof);
    // MerkleProofLevel[] hashPaths = proof.getProofLevels();
    // assertEquals(5, hashPaths.length);
    // proof = merkleTree_reload.getProof(data28.getKey(), 0);
    // assertNotNull(proof);
    // hashPaths = proof.getProofLevels();
    // assertEquals(6, hashPaths.length);
    KVEntry data28_reload_0 = merkleTree_reload.getData(data28.getKey(), 0);
    assertNotNull(data28_reload_0);
    assertEquals(data28.getKey(), data28_reload_0.getKey().toUTF8String());
    assertEquals(datas[28].getVersion(), data28_reload_0.getVersion());
    assertArrayEquals(datas[28].getValue(), data28_reload_0.getValue().toBytes());
    KVEntry data28_reload_1 = merkleTree_reload.getData(data28.getKey(), 1);
    assertNotNull(data28_reload_1);
    assertEquals(data28.getKey(), data28_reload_1.getKey().toUTF8String());
    assertEquals(data28.getVersion(), data28_reload_1.getVersion());
    assertArrayEquals(data28.getValue(), data28_reload_1.getValue().toBytes());
    // merkleTree_reload.print();
    // 测试不同根哈希加载的默克尔树能够检索的最新版本;
    MerkleHashSortTree merkleTree_0 = new MerkleHashSortTree(rootHash0, cryptoSetting, KEY_PREFIX, storage);
    MerkleHashSortTree merkleTree_1 = new MerkleHashSortTree(rootHash1, cryptoSetting, KEY_PREFIX, storage);
    KVEntry data28_reload = merkleTree_0.getData(data28.getKey());
    assertEquals(0, data28_reload.getVersion());
    data28_reload = merkleTree_1.getData(data28.getKey());
    assertEquals(1, data28_reload.getVersion());
    // 测试在修改状态下检索默克尔证明的正确性;
    VersioningKVData<String, byte[]> data28_2 = new VersioningKVData<String, byte[]>("KEY-28", 2, BytesUtils.toBytes("NEW-VALUE-VERSION-2"));
    // TODO: 暂时注释掉默克尔证明相关的内容;
    // MerkleProof proof28_1 = merkleTree_1.getProof("KEY-28", 1);
    // MerkleProof proof606_1 = merkleTree_1.getProof("KEY-606", 1);
    // assertNotNull(proof28_1);
    // assertNotNull(proof606_1);
    // 针对编号为 28 的数据加入一条新版本记录;
    merkleTree_1.setData(data28_2.getKey(), data28_2.getVersion(), data28_2.getValue());
    // TODO: 暂时注释掉默克尔证明相关的内容;
    // 对于修改中的数据项,查询未提交的最新版本数据的默克尔证明为 null,但是其已提交版本的证明不受影响;
    // 此外,其它未修改的数据项的默克尔证明也不受影响;
    // MerkleProof proof28_1_1 = merkleTree_1.getProof("KEY-28", 1);
    // MerkleProof proof28_2 = merkleTree_1.getProof("KEY-28", 2);
    // MerkleProof proof606_1_1 = merkleTree_1.getProof("KEY-606", 1);
    // assertNotNull(proof28_1_1);
    // assertNotNull(proof606_1_1);
    // assertNull(proof28_2);
    // assertEquals(proof28_1, proof28_1_1);
    // assertEquals(proof606_1, proof606_1_1);
    // 当提交修改之后,可以获取到修改数据项的最新版本的证明,同时其旧版本的证明也刷新了中间路径(加入了新版本数据节点); 
    merkleTree_1.commit();
// TODO: 暂时注释掉默克尔证明相关的内容;
// MerkleProof proof28_1_2 = merkleTree_1.getProof("KEY-28", 1);
// MerkleProof proof28_2_1 = merkleTree_1.getProof("KEY-28", 2);
// MerkleProof proof606_1_2 = merkleTree_1.getProof("KEY-606", 1);
// assertNotNull(proof28_1_2);
// assertNotNull(proof28_2_1);
// assertNotNull(proof606_1_2);
// TODO: 暂时注释掉默克尔证明相关的内容;
// 由于默克尔树发生了修改,所有默克尔证明发生了改变;
// assertFalse(proof28_1.equals(proof28_1_2));
// assertFalse(proof606_1.equals(proof606_1_2));
// 同一个key的数据项的最新版本的默克尔证明路径节点中数据节点部分(倒数第2项),出现在其前一个版本的更新后的数据证明中倒数第3项;
// 验证默克尔证明的长度增长;
// MerkleProof proof28_5 = merkleTree_1.getProof("KEY-28", 0);
// assertNotNull(proof28_5);
// hashPaths = proof28_5.getProofLevels();
// assertEquals(7, hashPaths.length);
// TODO: 暂时注释掉默克尔证明相关的内容;
// 重新加载默克尔树,默克尔证明是一致的;
// MerkleHashSortTree merkleTree_1_1 = new MerkleHashSortTree(rootHash1, cryptoSetting, KEY_PREFIX, storage);
// MerkleProof proof28_4 = merkleTree_1_1.getProof("KEY-28", 1);
// assertNotNull(proof28_4);
// assertEquals(proof28_1, proof28_4);
// merkleTree_1.print();
}
Also used : KVEntry(com.jd.blockchain.ledger.merkletree.KVEntry) TreeOptions(com.jd.blockchain.ledger.merkletree.TreeOptions) HashDigest(com.jd.blockchain.crypto.HashDigest) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) MerkleHashSortTree(com.jd.blockchain.ledger.merkletree.MerkleHashSortTree) VersioningKVData(com.jd.blockchain.storage.service.utils.VersioningKVData) Test(org.junit.Test)

Aggregations

KVEntry (com.jd.blockchain.ledger.merkletree.KVEntry)31 HashDigest (com.jd.blockchain.crypto.HashDigest)20 VersioningKVData (com.jd.blockchain.storage.service.utils.VersioningKVData)20 Test (org.junit.Test)20 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)17 MerkleHashTrie (com.jd.blockchain.ledger.proof.MerkleHashTrie)11 CryptoSetting (com.jd.blockchain.ledger.CryptoSetting)10 MerkleHashSortTree (com.jd.blockchain.ledger.merkletree.MerkleHashSortTree)9 ArrayList (java.util.ArrayList)8 TreeOptions (com.jd.blockchain.ledger.merkletree.TreeOptions)7 Bytes (utils.Bytes)5 HashMap (java.util.HashMap)3 MerkleTree (com.jd.blockchain.ledger.merkletree.MerkleTree)2 SecureRandom (java.security.SecureRandom)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 DataEntry (utils.DataEntry)1