Search in sources :

Example 1 with TreeOptions

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

the class MerkleSortTreeTest method testIterator.

@Test
public void testIterator() {
    TreeOptions options = createTreeOptions();
    MemoryKVStorage storage = new MemoryKVStorage();
    MerkleSortTree<byte[]> mst = MerkleSortTree.createBytesTree(options, DEFAULT_MKL_KEY_PREFIX, storage);
    // 验证空的迭代器;
    SkippingIterator<MerkleValue<byte[]>> iter = mst.bytesIterator();
    assertEquals(0, iter.getTotalCount());
    assertEquals(-1, iter.getCursor());
    assertFalse(iter.hasNext());
    assertNull(iter.next());
    // 加入数据,验证顺序数据插入的生成的迭代器;
    int count1 = 10;
    byte[][] datas1 = generateRandomData(count1);
    long[] ids1 = generateSeqenceIDs(0, count1);
    HashMap<Long, byte[]> dataMap = new HashMap<Long, byte[]>();
    mapIdValues(ids1, datas1, dataMap);
    addDatasAndCommit(ids1, datas1, mst);
    iter = mst.iterator();
    assertIteratorSortedAndEquals(iter, count1, ids1, dataMap);
    // 随机加入;验证迭代器返回有序的序列;
    Set<Long> excludingIDs = createIdSet(ids1);
    int count2 = (int) power(4, 8) + 1;
    byte[][] datas2 = generateRandomData(count2);
    long[] ids2 = generateRandomIDs(count2, excludingIDs, true);
    mapIdValues(ids2, datas2, dataMap);
    addDatasAndCommit(ids2, datas2, mst);
    long[] totalIds = ArrayUtils.concat(ids1, ids2);
    Arrays.sort(totalIds);
    long totalCount = count1 + count2;
    iter = mst.iterator();
    assertIteratorSortedAndEquals(iter, totalCount, totalIds, dataMap);
    // 验证有跳跃的情形;
    iter = mst.iterator();
    assertEquals(-1, iter.getCursor());
    int index = -1;
    long skipped = 1;
    iter.skip(skipped);
    index += skipped;
    assertEquals(index, iter.getCursor());
    MerkleValue<byte[]> merkleData = iter.next();
    index++;
    assertEquals(index, iter.getCursor());
    assertNotNull(merkleData);
    assertEquals(totalIds[index], merkleData.getId());
    skipped = 2;
    iter.skip(skipped);
    index += skipped;
    assertEquals(index, iter.getCursor());
    merkleData = iter.next();
    index++;
    assertEquals(index, iter.getCursor());
    assertNotNull(merkleData);
    assertEquals(totalIds[index], merkleData.getId());
    skipped = 3;
    iter.skip(skipped);
    index += skipped;
    assertEquals(index, iter.getCursor());
    merkleData = iter.next();
    index++;
    assertEquals(index, iter.getCursor());
    assertNotNull(merkleData);
    assertEquals(totalIds[index], merkleData.getId());
    SecureRandom random = new SecureRandom();
    for (int j = 0; j < 100; j++) {
        skipped = random.nextInt(100);
        iter.skip(skipped);
        index += skipped;
        assertEquals(index, iter.getCursor());
        merkleData = iter.next();
        index++;
        assertEquals(index, iter.getCursor());
        assertNotNull(merkleData);
        assertEquals(totalIds[index], merkleData.getId());
    }
    // 验证直接跳跃到倒数第 1 条的情形;
    long left = iter.getCount();
    iter.skip(left - 1);
    assertTrue(iter.hasNext());
    assertEquals(1, iter.getCount());
    merkleData = iter.next();
    assertEquals(totalCount - 1, iter.getCursor());
    assertNotNull(merkleData);
    assertEquals(totalIds[(int) totalCount - 1], merkleData.getId());
    assertFalse(iter.hasNext());
    merkleData = iter.next();
    assertNull(merkleData);
    // 验证直接跳跃到末尾的情形;
    iter = mst.iterator();
    assertTrue(iter.hasNext());
    long c = iter.skip(totalCount);
    assertEquals(totalCount, c);
    assertFalse(iter.hasNext());
    merkleData = iter.next();
    assertNull(merkleData);
}
Also used : HashMap(java.util.HashMap) SecureRandom(java.security.SecureRandom) TreeOptions(com.jd.blockchain.ledger.merkletree.TreeOptions) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) MerkleValue(com.jd.blockchain.ledger.merkletree.MerkleValue) Test(org.junit.Test)

Example 2 with TreeOptions

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

the class MerkleSortTreeTest method testMultiDataCountIterator.

/**
 * 测试包含数据策略中计数大于 1 的数据迭代;
 */
@Test
public void testMultiDataCountIterator() {
    TreeOptions options = createTreeOptions();
    MemoryKVStorage storage = new MemoryKVStorage();
    DataPolicy<byte[]> bytesDataPolicy = new DefaultDataPolicy<byte[]>() {

        @Override
        public byte[] updateData(long id, byte[] origData, byte[] newData) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            if (origData == null) {
                BytesUtils.writeInt(1, out);
            } else {
                int count = BytesUtils.toInt(origData) + 1;
                BytesUtils.writeInt(count, out);
                out.write(origData, 4, origData.length - 4);
            }
            BytesEncoding.writeInNormal(newData, out);
            return out.toByteArray();
        }

        @Override
        public long count(long id, byte[] data) {
            return BytesUtils.toInt(data);
        }

        @Override
        public SkippingIterator<MerkleValue<byte[]>> iterator(long id, byte[] bytesData, long count, BytesConverter<byte[]> converter) {
            byte[][] values = new byte[(int) count][];
            ByteArrayInputStream in = new ByteArrayInputStream(bytesData, 4, bytesData.length - 4);
            for (int i = 0; i < values.length; i++) {
                values[i] = BytesEncoding.readInNormal(in);
            }
            return new BytesEntriesIterator(id, values);
        }
    };
    MerkleSortTree<byte[]> mst = MerkleSortTree.createBytesTree(options, DEFAULT_MKL_KEY_PREFIX, storage, bytesDataPolicy);
    int count = 16;
    byte[][] datas = generateRandomData(count);
    long[] ids = new long[count];
    int startIndex = 10;
    for (int i = 0; i < startIndex; i++) {
        ids[i] = i;
    }
    // 从 10 开始,连续3条不同的记录使用相同的 编码;
    int testId = startIndex + 2;
    ids[startIndex] = testId;
    ids[startIndex + 1] = testId;
    ids[startIndex + 2] = testId;
    for (int i = 0; i < ids.length - startIndex - 3; i++) {
        ids[startIndex + i + 3] = startIndex + i + 5;
    }
    addDatas(ids, datas, mst);
    mst.commit();
    // 验证所有的数据都能够正常检索;
    SkippingIterator<MerkleValue<byte[]>> iter = mst.iterator();
    assertEquals(count, iter.getTotalCount());
    assertIteratorEquals(count, datas, ids, 0, iter);
    // 验证略过中间数据也能够正常检索:跳跃到连续 id 的前一条;
    iter = mst.iterator();
    iter.skip(startIndex - 1);
    int i = startIndex - 1;
    assertIteratorEquals(count - (startIndex - 1), datas, ids, startIndex - 1, iter);
    // 验证略过中间数据也能够正常检索:跳跃到连续 id 的第1条;
    iter = mst.iterator();
    iter.skip(startIndex);
    i = startIndex;
    {
        MerkleValue<byte[]> v = iter.next();
        assertNotNull(v);
        assertEquals(testId, v.getId());
        assertArrayEquals(datas[i], v.getValue());
        v = iter.next();
        assertNotNull(v);
        assertEquals(testId, v.getId());
        assertArrayEquals(datas[i + 1], v.getValue());
        v = iter.next();
        assertNotNull(v);
        assertEquals(testId, v.getId());
        assertArrayEquals(datas[i + 2], v.getValue());
    }
    assertIteratorEquals(count - (i + 3), datas, ids, i + 3, iter);
    // 验证略过中间数据也能够正常检索:跳跃到连续 id 的第2条;
    iter = mst.iterator();
    iter.skip(startIndex + 1);
    i = startIndex;
    {
        MerkleValue<byte[]> v = iter.next();
        assertNotNull(v);
        assertEquals(testId, v.getId());
        assertArrayEquals(datas[i + 1], v.getValue());
        v = iter.next();
        assertNotNull(v);
        assertEquals(testId, v.getId());
        assertArrayEquals(datas[i + 2], v.getValue());
    }
    assertIteratorEquals(count - (i + 3), datas, ids, i + 3, iter);
    // 验证略过中间数据也能够正常检索:跳跃到连续 id 的第3条;
    iter = mst.iterator();
    iter.skip(startIndex + 2);
    i = startIndex;
    {
        MerkleValue<byte[]> v = iter.next();
        assertNotNull(v);
        assertEquals(testId, v.getId());
        assertArrayEquals(datas[i + 2], v.getValue());
    }
    assertIteratorEquals(count - (i + 3), datas, ids, i + 3, iter);
    // 验证略过中间数据也能够正常检索:跳跃到连续 id 第3条;
    iter = mst.iterator();
    iter.skip(startIndex + 3);
    assertIteratorEquals(count - (startIndex + 3), datas, ids, startIndex + 3, iter);
}
Also used : DefaultDataPolicy(com.jd.blockchain.ledger.merkletree.DefaultDataPolicy) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TreeOptions(com.jd.blockchain.ledger.merkletree.TreeOptions) ByteArrayInputStream(java.io.ByteArrayInputStream) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) BytesConverter(com.jd.blockchain.ledger.merkletree.BytesConverter) MerkleValue(com.jd.blockchain.ledger.merkletree.MerkleValue) Test(org.junit.Test)

Example 3 with TreeOptions

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

the class MerkleSortTreeTest method newMerkleSortedTree.

private static MerkleSortTree<byte[]> newMerkleSortedTree() {
    TreeOptions options = createTreeOptions();
    MemoryKVStorage storage = new MemoryKVStorage();
    MerkleSortTree<byte[]> mst = MerkleSortTree.createBytesTree(options, DEFAULT_MKL_KEY_PREFIX, storage);
    return mst;
}
Also used : TreeOptions(com.jd.blockchain.ledger.merkletree.TreeOptions) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage)

Example 4 with TreeOptions

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

the class MerkleSortTreeTest method testAddingAndAssertingEquals.

private static void testAddingAndAssertingEquals(long[] ids, byte[][] datas) {
    TreeOptions options = createTreeOptions();
    MemoryKVStorage storage = new MemoryKVStorage();
    Counter counter = new Counter();
    MerkleSortTree<byte[]> mst = MerkleSortTree.createBytesTree(options, DEFAULT_MKL_KEY_PREFIX, storage, counter);
    assertNull(mst.getRootHash());
    addDatasAndCommit(ids, datas, mst);
    assertEquals(ids.length, counter.count.get());
    HashDigest rootHash = mst.getRootHash();
    assertNotNull(rootHash);
    assertDataEquals(mst, ids, datas);
    // reload merkle tree from storage;
    MerkleSortTree<byte[]> mst1 = MerkleSortTree.createBytesTree(rootHash, options, DEFAULT_MKL_KEY_PREFIX, storage);
    assertEquals(rootHash, mst1.getRootHash());
    assertDataEquals(mst1, ids, datas);
}
Also used : TreeOptions(com.jd.blockchain.ledger.merkletree.TreeOptions) HashDigest(com.jd.blockchain.crypto.HashDigest) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage)

Example 5 with TreeOptions

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

the class MerkleSortTreeTest method testReadUncommitting.

/**
 * 测试读未提交数据;
 */
@Test
public void testReadUncommitting() {
    int count = 10;
    byte[][] datas = generateRandomData(count);
    long[] ids = generateSeqenceIDs(0, count);
    TreeOptions options = createTreeOptions();
    MemoryKVStorage storage = new MemoryKVStorage();
    MerkleSortTree<byte[]> mst = MerkleSortTree.createBytesTree(options, DEFAULT_MKL_KEY_PREFIX, storage);
    addDatas(ids, datas, mst);
    // 验证未提交之前能够读取到对应的数据;
    assertNull(mst.getRootHash());
    assertEquals(0, mst.getCount());
    assertEquals(ids[ids.length - 1], mst.getMaxId());
    assertDataExists(mst, ids, datas);
    mst.commit();
    assertNotNull(mst.getRootHash());
    assertDataEquals(mst, ids, datas);
    HashDigest rootHash = mst.getRootHash();
    mst = MerkleSortTree.createBytesTree(rootHash, options, DEFAULT_MKL_KEY_PREFIX, storage);
    assertDataEquals(mst, ids, datas);
    // 在已经有数据的默克尔树中以编码顺序递增的方式加入数据,验证在未提交之前能够读取到新加入的数据;
    int count1 = 200;
    byte[][] datas1 = generateRandomData(count1);
    long[] ids1 = generateSeqenceIDs(count + 10, count1);
    addDatas(ids1, datas1, mst);
    assertEquals(ids1[ids1.length - 1], mst.getMaxId());
    assertDataExists(mst, ids, datas);
    assertDataExists(mst, ids1, datas1);
    mst.commit();
    assertDataExists(mst, ids1, datas1);
    // 在已经有数据的默克尔树中以编码随机不重复产生的方式加入数据,验证在未提交之前能够读取到新加入的数据;
    Set<Long> excludingIDs = createIdSet(ids);
    joinIdSet(ids1, excludingIDs);
    int count2 = 300;
    byte[][] datas2 = generateRandomData(count2);
    long[] ids2 = generateRandomIDs(count2, excludingIDs, true);
    HashDigest rootHash1 = mst.getRootHash();
    mst = MerkleSortTree.createBytesTree(rootHash1, options, DEFAULT_MKL_KEY_PREFIX, storage);
    addDatas(ids2, datas2, mst);
    assertEquals(count + count1, mst.getCount());
    assertDataExists(mst, ids, datas);
    assertDataExists(mst, ids1, datas1);
    assertDataExists(mst, ids2, datas2);
    mst.commit();
    assertEquals(count + count1 + count2, mst.getCount());
    assertDataExists(mst, ids, datas);
    assertDataExists(mst, ids1, datas1);
    assertDataExists(mst, ids2, datas2);
}
Also used : TreeOptions(com.jd.blockchain.ledger.merkletree.TreeOptions) HashDigest(com.jd.blockchain.crypto.HashDigest) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) Test(org.junit.Test)

Aggregations

TreeOptions (com.jd.blockchain.ledger.merkletree.TreeOptions)21 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)21 Test (org.junit.Test)15 HashDigest (com.jd.blockchain.crypto.HashDigest)10 MerkleHashSortTree (com.jd.blockchain.ledger.merkletree.MerkleHashSortTree)8 KVEntry (com.jd.blockchain.ledger.merkletree.KVEntry)7 VersioningKVData (com.jd.blockchain.storage.service.utils.VersioningKVData)6 MerkleValue (com.jd.blockchain.ledger.merkletree.MerkleValue)5 Random (java.util.Random)4 Bytes (utils.Bytes)4 SecureRandom (java.security.SecureRandom)3 HashMap (java.util.HashMap)2 MerkleProofException (com.jd.blockchain.ledger.core.MerkleProofException)1 BytesConverter (com.jd.blockchain.ledger.merkletree.BytesConverter)1 BytesKeyValue (com.jd.blockchain.ledger.merkletree.BytesKeyValue)1 DefaultDataPolicy (com.jd.blockchain.ledger.merkletree.DefaultDataPolicy)1 HashBucketEntry (com.jd.blockchain.ledger.merkletree.HashBucketEntry)1 HashEntry (com.jd.blockchain.ledger.merkletree.HashEntry)1 MerkleHashBucket (com.jd.blockchain.ledger.merkletree.MerkleHashBucket)1 MerkleTree (com.jd.blockchain.ledger.merkletree.MerkleTree)1