Search in sources :

Example 1 with KVEntry

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

the class MerkleHashTrie method printDatas.

public void printDatas() {
    SkippingIterator<KVEntry> iterator = iterator();
    System.out.println("\r\n\rn-------- HASH-SORTING-MERKLE-TREE -------");
    System.out.printf("total-size=%s\r\n", iterator.getTotalCount());
    int i = 0;
    while (iterator.hasNext()) {
        KVEntry data = iterator.next();
        System.out.printf("[%s] - KEY=%s; VERSION=%s;\r\n", i, data.getKey().toBase58(), data.getVersion());
    }
    System.out.printf("\r\n------------------\r\n", iterator.getTotalCount());
}
Also used : KVEntry(com.jd.blockchain.ledger.merkletree.KVEntry)

Example 2 with KVEntry

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

the class MerkleHashTrieTest method testMerkleTreeDiffPathsType.

@Test
public void testMerkleTreeDiffPathsType() {
    CryptoSetting cryptoSetting = createCryptoSetting();
    MemoryKVStorage storage = new MemoryKVStorage();
    List<String> newdataListString = new ArrayList<String>();
    List<String> dataListString = new ArrayList<String>();
    List<VersioningKVData<String, byte[]>> dataList = new ArrayList<VersioningKVData<String, byte[]>>();
    List<VersioningKVData<String, byte[]>> newdataList = new ArrayList<VersioningKVData<String, byte[]>>();
    int count = 2;
    int newAddCount = 5;
    VersioningKVData<String, byte[]> orginData0 = new VersioningKVData<String, byte[]>("KEY-19745261600024463", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
    dataList.add(orginData0);
    dataListString.add(orginData0.getKey());
    VersioningKVData<String, byte[]> orginData1 = new VersioningKVData<String, byte[]>("KEY-155749221633494971", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
    dataList.add(orginData1);
    dataListString.add(orginData1.getKey());
    VersioningKVData<String, byte[]>[] datas = toArray(dataList);
    MerkleHashTrie merkleTree = newMerkleTree_with_committed(datas, cryptoSetting, storage);
    HashDigest rootHash0 = merkleTree.getRootHash();
    assertNotNull(rootHash0);
    assertEquals(count, merkleTree.getTotalKeys());
    assertEquals(count, merkleTree.getTotalRecords());
    // reload and add random key data item;
    MerkleHashTrie merkleTree_reload = new MerkleHashTrie(rootHash0, cryptoSetting, KEY_PREFIX, storage, false);
    assertEquals(count, merkleTree_reload.getTotalKeys());
    assertEquals(count, merkleTree_reload.getTotalRecords());
    assertEquals(rootHash0, merkleTree_reload.getRootHash());
    VersioningKVData<String, byte[]> data0 = new VersioningKVData<String, byte[]>("KEY-1741789838495252", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
    newdataList.add(data0);
    VersioningKVData<String, byte[]> data1 = new VersioningKVData<String, byte[]>("KEY-2741789838505562", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
    newdataList.add(data1);
    VersioningKVData<String, byte[]> data2 = new VersioningKVData<String, byte[]>("KEY-0745023937104559", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
    newdataList.add(data2);
    VersioningKVData<String, byte[]> data3 = new VersioningKVData<String, byte[]>("KEY-7745261599950097", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
    newdataList.add(data3);
    VersioningKVData<String, byte[]> data4 = new VersioningKVData<String, byte[]>("KEY-9745261599963367", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
    newdataList.add(data4);
    for (int i = 0; i < newdataList.size(); i++) {
        merkleTree_reload.setData(newdataList.get(i).getKey(), newdataList.get(i).getVersion(), newdataList.get(i).getValue());
        newdataListString.add(newdataList.get(i).getKey());
    }
    merkleTree_reload.commit();
    HashDigest rootHash1 = merkleTree_reload.getRootHash();
    assertNotNull(rootHash1);
    assertNotEquals(rootHash0, rootHash1);
    assertEquals(count + newAddCount, merkleTree_reload.getTotalKeys());
    assertEquals(count + newAddCount, merkleTree_reload.getTotalRecords());
    SkippingIterator<KVEntry> diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
    // max boundary skip test
    assertEquals(newAddCount, diffIterator.getTotalCount());
    assertEquals(-1, diffIterator.getCursor());
    assertTrue(diffIterator.hasNext());
    long skipped = diffIterator.skip(newAddCount);
    assertEquals(newAddCount, skipped);
    assertFalse(diffIterator.hasNext());
    // re-interator and random skip test
    int skipNum = 4;
    diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
    assertEquals(newAddCount, diffIterator.getTotalCount());
    assertEquals(-1, diffIterator.getCursor());
    assertTrue(diffIterator.hasNext());
    long skipped1 = diffIterator.skip(skipNum);
    assertEquals(skipNum, skipped1);
    int diffNum = 0;
    // TODO: 无效的验证逻辑; by huanghaiquan at 2020-07-15;
    // while (diffIterator.hasNext()) {
    // MerkleData data = diffIterator.next();
    // assertNotNull(data);
    // assertFalse(dataList.contains(new String(data.getKey())));
    // assertTrue(newdataListString.contains(new String(data.getKey())));
    // diffNum++;
    // }
    // assertEquals(diffNum, diffIterator.getCount() - skipNum);
    // re-interator and next test
    diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
    int diffNum1 = 0;
    assertEquals(newAddCount, diffIterator.getTotalCount());
    while (diffIterator.hasNext()) {
        KVEntry data = diffIterator.next();
        assertNotNull(data);
        diffNum1++;
    }
    assertFalse(diffIterator.hasNext());
    assertEquals(newAddCount - 1, diffIterator.getCursor());
    assertEquals(newAddCount, diffIterator.getTotalCount());
    assertEquals(diffNum1, diffIterator.getTotalCount());
    // re-interator and test next key consistency
    diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
// TODO: 无效的验证逻辑; by huanghaiquan at 2020-07-15;
// while (diffIterator.hasNext()) {
// MerkleData data = diffIterator.next();
// assertNotNull(data);
// assertFalse(dataList.contains(new String(data.getKey())));
// assertTrue(newdataListString.contains(new String(data.getKey())));
// }
}
Also used : ArrayList(java.util.ArrayList) VersioningKVData(com.jd.blockchain.storage.service.utils.VersioningKVData) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) KVEntry(com.jd.blockchain.ledger.merkletree.KVEntry) HashDigest(com.jd.blockchain.crypto.HashDigest) MerkleHashTrie(com.jd.blockchain.ledger.proof.MerkleHashTrie) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) Test(org.junit.Test)

Example 3 with KVEntry

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

the class MerkleHashTrieTest method testMerkleTreeDiffMerkleDataIteratorType.

@Test
public void testMerkleTreeDiffMerkleDataIteratorType() {
    CryptoSetting cryptoSetting = createCryptoSetting();
    MemoryKVStorage storage = new MemoryKVStorage();
    List<String> newdataListString = new ArrayList<String>();
    List<String> dataListString = new ArrayList<String>();
    List<VersioningKVData<String, byte[]>> dataList = new ArrayList<VersioningKVData<String, byte[]>>();
    List<VersioningKVData<String, byte[]>> newdataList = new ArrayList<VersioningKVData<String, byte[]>>();
    int count = 1;
    int newAddCount = 5;
    VersioningKVData<String, byte[]> orginData0 = new VersioningKVData<String, byte[]>("KEY-0", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
    dataList.add(orginData0);
    dataListString.add(orginData0.getKey());
    VersioningKVData<String, byte[]>[] datas = toArray(dataList);
    MerkleHashTrie merkleTree = newMerkleTree_with_committed(datas, cryptoSetting, storage);
    HashDigest rootHash0 = merkleTree.getRootHash();
    assertNotNull(rootHash0);
    assertEquals(count, merkleTree.getTotalKeys());
    assertEquals(count, merkleTree.getTotalRecords());
    // reload and add random key data item;
    MerkleHashTrie merkleTree_reload = new MerkleHashTrie(rootHash0, cryptoSetting, KEY_PREFIX, storage, false);
    assertEquals(count, merkleTree_reload.getTotalKeys());
    assertEquals(count, merkleTree_reload.getTotalRecords());
    assertEquals(rootHash0, merkleTree_reload.getRootHash());
    VersioningKVData<String, byte[]> data0 = new VersioningKVData<String, byte[]>("KEY-1741789838495252", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
    newdataList.add(data0);
    VersioningKVData<String, byte[]> data1 = new VersioningKVData<String, byte[]>("KEY-2741789838505562", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
    newdataList.add(data1);
    VersioningKVData<String, byte[]> data2 = new VersioningKVData<String, byte[]>("KEY-0745023937104559", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
    newdataList.add(data2);
    VersioningKVData<String, byte[]> data3 = new VersioningKVData<String, byte[]>("KEY-7745261599950097", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
    newdataList.add(data3);
    VersioningKVData<String, byte[]> data4 = new VersioningKVData<String, byte[]>("KEY-9745261599963367", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
    newdataList.add(data4);
    for (int i = 0; i < newdataList.size(); i++) {
        merkleTree_reload.setData(newdataList.get(i).getKey(), newdataList.get(i).getVersion(), newdataList.get(i).getValue());
        newdataListString.add(newdataList.get(i).getKey());
    }
    merkleTree_reload.commit();
    HashDigest rootHash1 = merkleTree_reload.getRootHash();
    assertNotNull(rootHash1);
    assertNotEquals(rootHash0, rootHash1);
    assertEquals(count + newAddCount, merkleTree_reload.getTotalKeys());
    assertEquals(count + newAddCount, merkleTree_reload.getTotalRecords());
    SkippingIterator<KVEntry> diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
    // max boundary skip test
    assertEquals(newAddCount, diffIterator.getTotalCount());
    assertEquals(-1, diffIterator.getCursor());
    assertTrue(diffIterator.hasNext());
    long skipped = diffIterator.skip(newAddCount);
    assertEquals(newAddCount, skipped);
    assertFalse(diffIterator.hasNext());
    // re-interator and random skip test
    int skipNum = 4;
    diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
    assertEquals(newAddCount, diffIterator.getTotalCount());
    assertEquals(-1, diffIterator.getCursor());
    assertTrue(diffIterator.hasNext());
    long skipped1 = diffIterator.skip(skipNum);
    assertEquals(skipNum, skipped1);
    int diffNum = 0;
    // TODO: 无效的验证逻辑; by huanghaiquan at 2020-07-15;
    // while (diffIterator.hasNext()) {
    // MerkleData data = diffIterator.next();
    // assertNotNull(data);
    // assertFalse(dataList.contains(new String(data.getKey())));
    // assertTrue(newdataListString.contains(new String(data.getKey())));
    // diffNum++;
    // }
    // assertEquals(diffNum, diffIterator.getCount() - skipNum);
    // re-interator and next test
    diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
    int diffNum1 = 0;
    assertEquals(newAddCount, diffIterator.getTotalCount());
    while (diffIterator.hasNext()) {
        KVEntry data = diffIterator.next();
        assertNotNull(data);
        diffNum1++;
    }
    assertFalse(diffIterator.hasNext());
    assertEquals(newAddCount - 1, diffIterator.getCursor());
    assertEquals(newAddCount, diffIterator.getTotalCount());
    assertEquals(diffNum1, diffIterator.getTotalCount());
    // re-interator and test next key consistency
    diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
// TODO: 无效的验证逻辑; by huanghaiquan at 2020-07-15;
// while (diffIterator.hasNext()) {
// MerkleData data = diffIterator.next();
// assertNotNull(data);
// assertFalse(dataList.contains(new String(data.getKey())));
// assertTrue(newdataListString.contains(new String(data.getKey())));
// }
}
Also used : ArrayList(java.util.ArrayList) VersioningKVData(com.jd.blockchain.storage.service.utils.VersioningKVData) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) KVEntry(com.jd.blockchain.ledger.merkletree.KVEntry) HashDigest(com.jd.blockchain.crypto.HashDigest) MerkleHashTrie(com.jd.blockchain.ledger.proof.MerkleHashTrie) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) Test(org.junit.Test)

Example 4 with KVEntry

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

the class MerkleHashTrieTest method testMerkleTreeKeysDiffMultiCommit.

/**
 * 构造KEY固定的两棵树,多次执行commit, 验证是否能得到预期的差异数据
 */
@Test
public void testMerkleTreeKeysDiffMultiCommit() {
    List<VersioningKVData<String, byte[]>> newdataList = new ArrayList<VersioningKVData<String, byte[]>>();
    List<String> newdataListString = new ArrayList<String>();
    List<String> newdataListString1 = new ArrayList<String>();
    CryptoSetting cryptoSetting = createCryptoSetting();
    MemoryKVStorage storage = new MemoryKVStorage();
    int count = 10240;
    int newAddCount = 5700;
    List<VersioningKVData<String, byte[]>> dataList = generateDatas(count);
    VersioningKVData<String, byte[]>[] datas = toArray(dataList);
    MerkleHashTrie merkleTree = newMerkleTree_with_committed(datas, cryptoSetting, storage);
    HashDigest rootHash0 = merkleTree.getRootHash();
    assertNotNull(rootHash0);
    assertEquals(count, merkleTree.getTotalKeys());
    assertEquals(count, merkleTree.getTotalRecords());
    // reload and add random key data item;
    MerkleHashTrie merkleTree_reload = new MerkleHashTrie(rootHash0, cryptoSetting, KEY_PREFIX, storage, false);
    assertEquals(count, merkleTree_reload.getTotalKeys());
    assertEquals(count, merkleTree_reload.getTotalRecords());
    assertEquals(rootHash0, merkleTree_reload.getRootHash());
    newdataListString = generateNewDatasString(count, newAddCount);
    for (int i = 0; i < newAddCount; i++) {
        VersioningKVData<String, byte[]> newdataKV = new VersioningKVData<String, byte[]>(newdataListString.get(i), 0, BytesUtils.toBytes("NEW-VALUE"));
        merkleTree_reload.setData(newdataKV.getKey(), newdataKV.getVersion(), newdataKV.getValue());
    }
    // first commit
    merkleTree_reload.commit();
    HashDigest rootHash1 = merkleTree_reload.getRootHash();
    assertNotNull(rootHash1);
    assertNotEquals(rootHash0, rootHash1);
    assertEquals(count + newAddCount, merkleTree_reload.getTotalKeys());
    assertEquals(count + newAddCount, merkleTree_reload.getTotalRecords());
    int newAddCount1 = 200;
    newdataListString1 = generateNewDatasString(count + newAddCount, newAddCount1);
    for (int i = 0; i < newAddCount1; i++) {
        VersioningKVData<String, byte[]> newdataKV = new VersioningKVData<String, byte[]>(newdataListString1.get(i), 0, BytesUtils.toBytes("NEW-VALUE"));
        merkleTree_reload.setData(newdataKV.getKey(), newdataKV.getVersion(), newdataKV.getValue());
    }
    // second commit
    merkleTree_reload.commit();
    HashDigest rootHash2 = merkleTree_reload.getRootHash();
    assertNotNull(rootHash2);
    assertNotEquals(rootHash1, rootHash2);
    assertEquals(count + newAddCount + newAddCount1, merkleTree_reload.getTotalKeys());
    assertEquals(count + newAddCount + newAddCount1, merkleTree_reload.getTotalRecords());
    SkippingIterator<KVEntry> diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
    // max boundary skip test
    assertEquals(newAddCount + newAddCount1, diffIterator.getTotalCount());
    assertEquals(-1, diffIterator.getCursor());
    assertTrue(diffIterator.hasNext());
    long skipped = diffIterator.skip(newAddCount + newAddCount1);
    assertEquals(newAddCount + newAddCount1, skipped);
    assertFalse(diffIterator.hasNext());
    // re-interator and random skip test
    int skipNum = 200;
    diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
    assertEquals(newAddCount + newAddCount1, diffIterator.getTotalCount());
    assertEquals(-1, diffIterator.getCursor());
    assertTrue(diffIterator.hasNext());
    long skipped1 = diffIterator.skip(skipNum);
    assertEquals(skipNum, skipped1);
    int diffNum = 0;
    // TODO: 无效的验证逻辑; by huanghaiquan at 2020-07-15;
    // while (diffIterator.hasNext()) {
    // MerkleData data = diffIterator.next();
    // assertNotNull(data);
    // assertFalse(dataList.contains(new String(data.getKey())));
    // assertTrue(newdataListString.contains(new String(data.getKey()))
    // || newdataListString1.contains(new String(data.getKey())));
    // diffNum++;
    // }
    // assertEquals(diffNum, diffIterator.getCount() - skipNum);
    // re-interator and next test
    diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
    int diffNum1 = 0;
    while (diffIterator.hasNext()) {
        KVEntry data = diffIterator.next();
        assertNotNull(data);
        diffNum1++;
    }
    assertFalse(diffIterator.hasNext());
    assertEquals(newAddCount + newAddCount1 - 1, diffIterator.getCursor());
    assertEquals(newAddCount + newAddCount1, diffIterator.getTotalCount());
    assertEquals(diffNum1, diffIterator.getTotalCount());
    // re-interator and test next key consistency
    diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
// TODO: 无效的验证逻辑; by huanghaiquan at 2020-07-15;
// while (diffIterator.hasNext()) {
// MerkleData data = diffIterator.next();
// assertNotNull(data);
// assertFalse(dataList.contains(new String(data.getKey())));
// assertTrue(newdataListString.contains(new String(data.getKey()))
// || newdataListString1.contains(new String(data.getKey())));
// }
}
Also used : ArrayList(java.util.ArrayList) VersioningKVData(com.jd.blockchain.storage.service.utils.VersioningKVData) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) KVEntry(com.jd.blockchain.ledger.merkletree.KVEntry) HashDigest(com.jd.blockchain.crypto.HashDigest) MerkleHashTrie(com.jd.blockchain.ledger.proof.MerkleHashTrie) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) Test(org.junit.Test)

Example 5 with KVEntry

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

the class MerkleHashTrieTest method assertEqualsTrie.

private void assertEqualsTrie(MerkleHashTrie merkleTree, MerkleHashTrie merkleTree1) {
    HashDigest rootHash_N1 = merkleTree.getRootHash();
    HashDigest rootHash_N2 = merkleTree1.getRootHash();
    assertNotNull(rootHash_N1);
    assertNotNull(rootHash_N2);
    SkippingIterator<KVEntry> iterator = merkleTree.iterator();
    SkippingIterator<KVEntry> iterator1 = merkleTree1.iterator();
    KVEntry dt;
    KVEntry dt1;
    assertEquals(iterator.getTotalCount(), iterator1.getTotalCount());
    for (int i = 0; i < iterator.getTotalCount(); i++) {
        assertTrue(iterator.hasNext());
        assertTrue(iterator1.hasNext());
        dt = iterator.next();
        dt1 = iterator1.next();
        assertTrue(dt.getKey().equals(dt1.getKey()));
        assertEquals(dt.getValue(), dt1.getValue());
        assertEquals(dt.getVersion(), dt1.getVersion());
    // TODO: 暂时忽略默克尔证明的测试;
    // MerkleProof proof = merkleTree.getProof(dt.getKey());
    // MerkleProof proof1 = merkleTree1.getProof(dt.getKey());
    // assertMerkleProofEquals(dt, proof, proof1);
    }
}
Also used : KVEntry(com.jd.blockchain.ledger.merkletree.KVEntry) HashDigest(com.jd.blockchain.crypto.HashDigest)

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