Search in sources :

Example 6 with MerkleProofException

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

the class MerkleSortTree method loadBytes.

/**
 * 加载指定节点的内容,如果不存在,则抛出异常;
 *
 * @param nodeHash
 * @return
 */
@SuppressWarnings("unused")
private byte[] loadBytes(byte[] key) {
    Bytes storageKey = encodeStorageKey(key);
    byte[] nodeBytes = KV_STORAGE.get(storageKey);
    if (nodeBytes == null) {
        throw new MerkleProofException("Merkle node does not exist! -- key=" + storageKey.toBase58());
    }
    return nodeBytes;
}
Also used : Bytes(utils.Bytes) MerkleProofException(com.jd.blockchain.ledger.core.MerkleProofException)

Example 7 with MerkleProofException

use of com.jd.blockchain.ledger.core.MerkleProofException 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

MerkleProofException (com.jd.blockchain.ledger.core.MerkleProofException)7 Bytes (utils.Bytes)6 MerkleDataNode (com.jd.blockchain.ledger.MerkleDataNode)1 TreeOptions (com.jd.blockchain.ledger.merkletree.TreeOptions)1 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)1 SecureRandom (java.security.SecureRandom)1 Random (java.util.Random)1 Test (org.junit.Test)1