Search in sources :

Example 1 with PathNode

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

the class MerkleHashTrieTest method testDataSerialize.

/**
 * 测试数据序列化;
 */
@Test
public void testDataSerialize() {
    byte[] key = BytesUtils.toBytes("KEY-1");
    HashDigest hashDigest = SHA256_HASH_FUNC.hash(key);
    MerkleTrieData merkleData = new MerkleTrieDataEntry(key, 0, hashDigest.toBytes());
    testMerkleDataSerialize(merkleData);
    merkleData = new MerkleTrieDataEntry(key, 1024, hashDigest.toBytes());
    testMerkleDataSerialize(merkleData);
    merkleData = new MerkleTrieDataEntry(key, NumberMask.LONG.MAX_BOUNDARY_SIZE - 1, hashDigest.toBytes());
    testMerkleDataSerialize(merkleData);
    // 数据大小;
    System.out.println("------ Merkle Data Serialize ------");
    byte[] dataBytes = BinaryProtocol.encode(merkleData, MerkleTrieData.class);
    System.out.printf("DataBytes= %s B\r\n", dataBytes.length);
    System.out.println("------ Merkle Path Serialize ------");
    int degree = 8;
    PathNode pathNode = new PathNode(degree);
    dataBytes = BinaryProtocol.encode(pathNode, MerklePath.class);
    System.out.printf("childs=%s; bytes=%s B\r\n", 0, dataBytes.length);
    for (int i = 0; i < degree; i++) {
        HashDigest childHash = SHA256_HASH_FUNC.hash(BytesUtils.toBytes(1024 + i));
        pathNode.setChildNode((byte) i, childHash, null);
        dataBytes = BinaryProtocol.encode(pathNode, MerklePath.class);
        System.out.printf("childs=%s; bytes=%s B\r\n", i + 1, dataBytes.length);
    }
}
Also used : MerkleTrieDataEntry(com.jd.blockchain.ledger.proof.MerkleTrieDataEntry) HashDigest(com.jd.blockchain.crypto.HashDigest) MerkleTrieData(com.jd.blockchain.ledger.proof.MerkleTrieData) MerklePath(com.jd.blockchain.ledger.proof.MerklePath) PathNode(com.jd.blockchain.ledger.proof.PathNode) Test(org.junit.Test)

Aggregations

HashDigest (com.jd.blockchain.crypto.HashDigest)1 MerklePath (com.jd.blockchain.ledger.proof.MerklePath)1 MerkleTrieData (com.jd.blockchain.ledger.proof.MerkleTrieData)1 MerkleTrieDataEntry (com.jd.blockchain.ledger.proof.MerkleTrieDataEntry)1 PathNode (com.jd.blockchain.ledger.proof.PathNode)1 Test (org.junit.Test)1