Search in sources :

Example 21 with TreeOptions

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

the class MerkleSortTreeTest method testAddDuplicatedData.

@Test
public void testAddDuplicatedData() {
    Random random = new Random();
    byte[] data = new byte[32];
    random.nextBytes(data);
    MemoryKVStorage storage = new MemoryKVStorage();
    // 配置选项设置为”不报告重复数据项“;
    // 以不同的 id 重复设置两个相同的数据,预期不会报告异常;
    MerkleProofException ex = null;
    try {
        TreeOptions options = TreeOptions.build().setDefaultHashAlgorithm(HASH_ALGORITHM.code()).setReportKeyStorageConfliction(false);
        MerkleSortTree<byte[]> mst = MerkleSortTree.createBytesTree(options, DEFAULT_MKL_KEY_PREFIX, storage);
        mst.set(1, data);
        mst.set(2, data);
        mst.commit();
    } catch (MerkleProofException e) {
        ex = e;
    }
    assertNull(ex);
    // 配置选项设置为”报告重复数据项“;
    // 以不同的 id 重复设置两个相同的数据,预期将报告异常;
    ex = null;
    try {
        TreeOptions options = TreeOptions.build().setDefaultHashAlgorithm(HASH_ALGORITHM.code()).setReportKeyStorageConfliction(true);
        MerkleSortTree<byte[]> mst = MerkleSortTree.createBytesTree(options, DEFAULT_MKL_KEY_PREFIX, storage);
        mst.set(1, data);
        mst.set(2, data);
        mst.commit();
    } catch (MerkleProofException e) {
        ex = e;
    }
    assertNotNull(ex);
}
Also used : MerkleProofException(com.jd.blockchain.ledger.core.MerkleProofException) Random(java.util.Random) SecureRandom(java.security.SecureRandom) TreeOptions(com.jd.blockchain.ledger.merkletree.TreeOptions) 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