Search in sources :

Example 6 with MerkleHashSortTree

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

the class MerkleHashSortTreeTest method testCancel.

/**
 * 验证 HashSortingMerkleTree 在未提交之前,新增的数据记录可读和可回滚特性;
 */
@Test
public void testCancel() {
    // 数据集合长度为 1024 时也能正常生成;
    int count = 1024;
    List<VersioningKVData<String, byte[]>> dataList = generateDatas(count);
    VersioningKVData<String, byte[]>[] datas = toArray(dataList);
    MerkleHashSortTree merkleTree = newMerkleTree(datas);
    assertTrue(merkleTree.isUpdated());
    // 未提交之前查不到信息;
    assertNull(merkleTree.getRootHash());
    assertEquals(0, merkleTree.getTotalKeys());
    KVEntry dt = merkleTree.getData("KEY-69");
    assertNotNull(dt);
    assertEquals(0, dt.getVersion());
    dt = merkleTree.getData("KEY-69", 0);
    assertNotNull(dt);
    assertEquals(0, dt.getVersion());
    dt = merkleTree.getData("KEY-69", 1);
    assertNull(dt);
    VersioningKVData<String, byte[]> data69 = new VersioningKVData<String, byte[]>("KEY-69", 1, BytesUtils.toBytes("NEW-VALUE-VERSION-1"));
    merkleTree.setData(data69.getKey(), data69.getVersion(), data69.getValue());
    dt = merkleTree.getData("KEY-69", 1);
    assertNotNull(dt);
    assertEquals(1, dt.getVersion());
    assertTrue(merkleTree.isUpdated());
    // 回滚全部数据;
    merkleTree.cancel();
    HashDigest rootHash = merkleTree.getRootHash();
    assertNull(rootHash);
    assertEquals(0, merkleTree.getTotalKeys());
    dt = merkleTree.getData("KEY-69");
    assertNull(dt);
    dt = merkleTree.getData("KEY-69", 0);
    assertNull(dt);
    dt = merkleTree.getData("KEY-69", 1);
    assertNull(dt);
    // 对于新创建的空的默克尔树的 updated 属性为 true,回滚之后仍然是空的,此时预期 updated 属性为 true;
    assertTrue(merkleTree.isUpdated());
    // 加入数据,提交后继续加入数据,验证能够正常回滚到上一次提交;
    setDatas(merkleTree, datas);
    merkleTree.commit();
    assertDataExist(merkleTree, datas);
    int count2 = 200;
    List<VersioningKVData<String, byte[]>> dataList2 = generateDatasFrom(count, count2);
    VersioningKVData<String, byte[]>[] datas2 = toArray(dataList2);
    setDatas(merkleTree, datas2);
    // 在回滚之前,验证全部的数据都存在;
    assertDataExist(merkleTree, datas);
    assertDataExist(merkleTree, datas2);
    // 回滚未提交的第二批数据;
    merkleTree.cancel();
    // 验证仅剩第一批数据;
    assertDataExist(merkleTree, datas);
    assertDataNotExist(merkleTree, datas2);
}
Also used : KVEntry(com.jd.blockchain.ledger.merkletree.KVEntry) HashDigest(com.jd.blockchain.crypto.HashDigest) MerkleHashSortTree(com.jd.blockchain.ledger.merkletree.MerkleHashSortTree) VersioningKVData(com.jd.blockchain.storage.service.utils.VersioningKVData) Test(org.junit.Test)

Example 7 with MerkleHashSortTree

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

the class MerkleHashSortTreeTest method testSpecialUseCase_1.

@Test
public void testSpecialUseCase_1() {
    TreeOptions treeOptions = createTreeOptions();
    MemoryKVStorage storage = new MemoryKVStorage();
    MerkleHashSortTree merkleTree = new MerkleHashSortTree(treeOptions, KEY_PREFIX, storage);
    byte[] key = Base58Utils.decode("j5sXmpcomtM2QMUNWeQWsF8bNFFnyeXoCjVAekEeLSscgY");
    byte[] value = BytesUtils.toBytes("Special Use-Case VALUE");
    long version = 0;
    merkleTree.setData(key, version, value);
    KVEntry mkdata = merkleTree.getData(key);
    assertNotNull(mkdata);
    merkleTree.commit();
    mkdata = merkleTree.getData(key);
    assertNotNull(mkdata);
    MerkleTree merkleTreeReload = new MerkleHashSortTree(merkleTree.getRootHash(), treeOptions, KEY_PREFIX, storage);
    mkdata = merkleTreeReload.getData(key);
    assertNotNull(mkdata);
}
Also used : MerkleTree(com.jd.blockchain.ledger.merkletree.MerkleTree) KVEntry(com.jd.blockchain.ledger.merkletree.KVEntry) TreeOptions(com.jd.blockchain.ledger.merkletree.TreeOptions) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) MerkleHashSortTree(com.jd.blockchain.ledger.merkletree.MerkleHashSortTree) Test(org.junit.Test)

Example 8 with MerkleHashSortTree

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

the class MerkleHashSortTreeTest method testReloadTreeAddNewDataNode.

/**
 * 对已存在的树进行重载,增加新的数据节点,通过重载树验证新节点是否添加成功,total keys 与total records
 * 是否符合预期,新添加的数据节点Key具有一定的规律
 */
@Test
public void testReloadTreeAddNewDataNode() {
    TreeOptions cryptoSetting = createTreeOptions();
    MemoryKVStorage storage = new MemoryKVStorage();
    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());
    // reload and add one data item;
    MerkleHashSortTree merkleTree_reload = new MerkleHashSortTree(rootHash0, cryptoSetting, KEY_PREFIX, storage);
    assertEquals(count, merkleTree_reload.getTotalKeys());
    assertEquals(rootHash0, merkleTree_reload.getRootHash());
    VersioningKVData<String, byte[]> data1025 = new VersioningKVData<String, byte[]>("KEY-1025", 0, BytesUtils.toBytes("NEW-VALUE-1025-VERSION-0"));
    merkleTree_reload.setData(data1025.getKey(), data1025.getVersion(), data1025.getValue());
    merkleTree_reload.commit();
    HashDigest rootHash1 = merkleTree_reload.getRootHash();
    assertNotNull(rootHash1);
    assertNotEquals(rootHash0, rootHash1);
    KVEntry data1025_reload_0 = merkleTree_reload.getData(data1025.getKey(), 0);
    assertNotNull(data1025_reload_0);
    KVEntry data0_reload_0 = merkleTree_reload.getData("KEY-0", 0);
    assertNotNull(data0_reload_0);
    System.out.println("mkl reload total keys = " + merkleTree_reload.getTotalKeys());
    assertEquals(count + 1, merkleTree_reload.getTotalKeys());
    MerkleHashSortTree merkleTree_reload_1 = new MerkleHashSortTree(rootHash1, cryptoSetting, KEY_PREFIX, storage);
    assertEquals(count + 1, merkleTree_reload_1.getTotalKeys());
    assertEquals(rootHash1, merkleTree_reload_1.getRootHash());
    HashDigest rootHash2 = merkleTree_reload_1.getRootHash();
    assertNotNull(rootHash2);
    assertNotEquals(rootHash0, rootHash2);
    KVEntry data1025_reload_1 = merkleTree_reload_1.getData(data1025.getKey(), 0);
    assertNotNull(data1025_reload_1);
    KVEntry data0_reload_1 = merkleTree_reload_1.getData("KEY-0", 0);
    assertNotNull(data0_reload_1);
    System.out.println("mkl reload total keys = " + merkleTree_reload_1.getTotalKeys());
    assertEquals(count + 1, merkleTree_reload_1.getTotalKeys());
}
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)

Example 9 with MerkleHashSortTree

use of com.jd.blockchain.ledger.merkletree.MerkleHashSortTree 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 10 with MerkleHashSortTree

use of com.jd.blockchain.ledger.merkletree.MerkleHashSortTree 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

MerkleHashSortTree (com.jd.blockchain.ledger.merkletree.MerkleHashSortTree)14 HashDigest (com.jd.blockchain.crypto.HashDigest)11 VersioningKVData (com.jd.blockchain.storage.service.utils.VersioningKVData)11 Test (org.junit.Test)11 KVEntry (com.jd.blockchain.ledger.merkletree.KVEntry)9 TreeOptions (com.jd.blockchain.ledger.merkletree.TreeOptions)8 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)8 Random (java.util.Random)2 Bytes (utils.Bytes)2 MerkleProof (com.jd.blockchain.ledger.MerkleProof)1 MerkleTree (com.jd.blockchain.ledger.merkletree.MerkleTree)1 SecureRandom (java.security.SecureRandom)1 HashMap (java.util.HashMap)1