Search in sources :

Example 1 with MerkleProofException

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

the class MerkleSequenceTree method loadDataNode.

private DataNode loadDataNode(HashDigest hashBytes, boolean verify) {
    Bytes key = encodeNodeKey(hashBytes);
    byte[] bytes = kvStorage.get(key);
    if (bytes == null || bytes.length == 0) {
        return null;
    }
    DataNode dataNode = MerkleTreeEncoder.resolve(bytes);
    if (verify && !hashBytes.equals(dataNode.nodeHash)) {
        String keyStr = hashBytes.toBase58();
        String actualHashStr = dataNode.nodeHash.toBase58();
        throw new MerkleProofException(String.format("The actually hash of DataNode is not equal with it's key! -- [Key=%s][ActualHash=%s]", keyStr, actualHashStr));
    }
    return dataNode;
}
Also used : Bytes(utils.Bytes) MerkleProofException(com.jd.blockchain.ledger.core.MerkleProofException) MerkleDataNode(com.jd.blockchain.ledger.MerkleDataNode)

Example 2 with MerkleProofException

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

the class MerkleSequenceTree method loadPathNode.

/**
 * Load {@link AbstractMerkleNode} from storage service;
 *
 * <p>
 *
 * @param hashDigest
 * @param verify
 * @return return instance of {@link PathNode}, or null if not exist;
 */
private PathNode loadPathNode(HashDigest hashDigest, boolean verify) {
    Bytes key = encodeNodeKey(hashDigest);
    byte[] bytes = kvStorage.get(key);
    if (bytes == null || bytes.length == 0) {
        return null;
    }
    PathNode pathNode = PathNode.parse(bytes, verify);
    if (verify && !hashDigest.equals(pathNode.getNodeHash())) {
        String keyStr = hashDigest.toBase58();
        String actualHashStr = pathNode.getNodeHash().toBase58();
        throw new MerkleProofException(String.format("The actually hash of PathNode is not equal with it's key! -- [Key=%s][ActualHash=%s]", keyStr, actualHashStr));
    }
    return pathNode;
}
Also used : Bytes(utils.Bytes) MerkleProofException(com.jd.blockchain.ledger.core.MerkleProofException)

Example 3 with MerkleProofException

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

the class MerkleHashTrie method loadMerkleTrieEntry.

private MerkleTrieEntry loadMerkleTrieEntry(HashDigest entryHash) {
    Bytes key = encodeEntryKey(entryHash);
    byte[] bytes = storage.get(key);
    if (bytes == null) {
        throw new MerkleProofException("The merkle trie entry with hash [" + entryHash.toBase58() + "] does not exist!");
    }
    MerkleTrieEntry entry = BinaryProtocol.decode(bytes);
    return entry;
}
Also used : Bytes(utils.Bytes) MerkleProofException(com.jd.blockchain.ledger.core.MerkleProofException)

Example 4 with MerkleProofException

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

the class LeafNode method addKeyNode.

public void addKeyNode(MerkleTrieDataEntry dataEntry) {
    if (keys.length == 0) {
        if (dataEntry.getVersion() != 0) {
            throw new MerkleProofException("The version of the new key is not zero!");
        }
        insertDataEntry(0, dataEntry);
    } else {
        // 按升序插入新元素;
        Bytes newKey = dataEntry.getKey();
        int i = 0;
        int count = keys.length;
        int c = -1;
        for (; i < count; i++) {
            c = keys[i].getKey().compare(newKey);
            if (c >= 0) {
                break;
            }
        }
        if (c == 0) {
            // 有相同的 key 存在,更新该 key 的新版本;
            setDataEntry(i, dataEntry);
        } else {
            // 没有相同的 key 存在,插入新的 key;
            insertDataEntry(i, dataEntry);
        }
    }
    setModified();
}
Also used : MerkleProofException(com.jd.blockchain.ledger.core.MerkleProofException) Bytes(utils.Bytes)

Example 5 with MerkleProofException

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

the class MerkleSortTree method loadBytes.

private byte[] loadBytes(ByteSequence 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)

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