Search in sources :

Example 1 with MerkleDataNodeEncoder

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

the class MerkleDataNodeEncoderTest method doTestV1.

private void doTestV1(long sn, long version, Bytes key, byte[] data) {
    HashFunction hashFunc = Crypto.getHashFunction(ClassicAlgorithm.SHA256);
    HashDigest dataHash = hashFunc.hash(data);
    MerkleDataNodeEncoder encoderV1 = new MerkleDataNodeEncoder_V1();
    DataNode node = encoderV1.create(ClassicAlgorithm.SHA256.code(), sn, key, version, data);
    assertEquals(dataHash, node.getValueHash());
    assertEquals(sn, node.getSN());
    assertEquals(version, node.getVersion());
    assertEquals(key, node.getKey());
    byte[] nodeBytes = node.toBytes();
    DataNode node_reversed = encoderV1.resolve(nodeBytes);
    assertEquals(dataHash, node_reversed.getValueHash());
    assertEquals(node.getNodeHash(), node_reversed.getNodeHash());
    assertEquals(encoderV1.getFormatVersion(), nodeBytes[0]);
    assertEquals(sn, node_reversed.getSN());
    assertEquals(version, node_reversed.getVersion());
    assertEquals(key, node_reversed.getKey());
}
Also used : HashFunction(com.jd.blockchain.crypto.HashFunction) HashDigest(com.jd.blockchain.crypto.HashDigest) DataNode(com.jd.blockchain.ledger.proof.MerkleSequenceTree.DataNode) MerkleDataNodeEncoder_V1(com.jd.blockchain.ledger.proof.MerkleDataNodeEncoder_V1) MerkleDataNodeEncoder(com.jd.blockchain.ledger.core.MerkleDataNodeEncoder)

Example 2 with MerkleDataNodeEncoder

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

the class MerkleDataNodeEncoderTest method testCompatibility.

@Test
public void testCompatibility() {
    Random rand = new Random();
    byte[] data = new byte[512];
    byte[] keyBytes = new byte[256];
    rand.nextBytes(data);
    rand.nextBytes(keyBytes);
    Bytes key = new Bytes(keyBytes);
    long sn = 1024;
    long version = 1;
    PreviousDataNode pdataNode = PreviousDataNode.newDataNode(ClassicAlgorithm.SHA256.code(), sn, key, version, data);
    MerkleDataNodeEncoder encoderV0 = new MerkleDataNodeEncoder_V0();
    DataNode dataNode = encoderV0.create(ClassicAlgorithm.SHA256.code(), sn, key, version, data);
    assertEquals(pdataNode.getNodeHash(), dataNode.getNodeHash());
    assertEquals(pdataNode.getSN(), dataNode.getSN());
    assertEquals(pdataNode.getVersion(), dataNode.getVersion());
    assertEquals(pdataNode.getKey(), dataNode.getKey());
    DataNode dataNode_reversed = encoderV0.resolve(pdataNode.toBytes());
    assertNull(dataNode_reversed.getValueHash());
    assertEquals(pdataNode.getNodeHash(), dataNode_reversed.getNodeHash());
    assertEquals(pdataNode.getSN(), dataNode_reversed.getSN());
    assertEquals(pdataNode.getVersion(), dataNode_reversed.getVersion());
    assertEquals(pdataNode.getKey(), dataNode_reversed.getKey());
}
Also used : MerkleDataNodeEncoder_V0(com.jd.blockchain.ledger.proof.MerkleDataNodeEncoder_V0) Bytes(utils.Bytes) Random(java.util.Random) DataNode(com.jd.blockchain.ledger.proof.MerkleSequenceTree.DataNode) MerkleDataNodeEncoder(com.jd.blockchain.ledger.core.MerkleDataNodeEncoder) Test(org.junit.Test)

Example 3 with MerkleDataNodeEncoder

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

the class MerkleDataNodeEncoderTest method doTestV0.

private void doTestV0(long sn, long version, Bytes key, byte[] data) {
    MerkleDataNodeEncoder encoderV0 = new MerkleDataNodeEncoder_V0();
    DataNode nodeV0 = encoderV0.create(ClassicAlgorithm.SHA256.code(), sn, key, version, data);
    assertNull(nodeV0.getValueHash());
    assertEquals(sn, nodeV0.getSN());
    assertEquals(version, nodeV0.getVersion());
    assertEquals(key, nodeV0.getKey());
    byte[] nodeBytes = nodeV0.toBytes();
    DataNode nodeV0_reversed = encoderV0.resolve(nodeBytes);
    assertNull(nodeV0_reversed.getValueHash());
    assertEquals(nodeV0.getNodeHash(), nodeV0_reversed.getNodeHash());
    assertEquals(encoderV0.getFormatVersion(), nodeBytes[0]);
    assertEquals(sn, nodeV0_reversed.getSN());
    assertEquals(version, nodeV0_reversed.getVersion());
    assertEquals(key, nodeV0_reversed.getKey());
}
Also used : MerkleDataNodeEncoder_V0(com.jd.blockchain.ledger.proof.MerkleDataNodeEncoder_V0) DataNode(com.jd.blockchain.ledger.proof.MerkleSequenceTree.DataNode) MerkleDataNodeEncoder(com.jd.blockchain.ledger.core.MerkleDataNodeEncoder)

Aggregations

MerkleDataNodeEncoder (com.jd.blockchain.ledger.core.MerkleDataNodeEncoder)3 DataNode (com.jd.blockchain.ledger.proof.MerkleSequenceTree.DataNode)3 MerkleDataNodeEncoder_V0 (com.jd.blockchain.ledger.proof.MerkleDataNodeEncoder_V0)2 HashDigest (com.jd.blockchain.crypto.HashDigest)1 HashFunction (com.jd.blockchain.crypto.HashFunction)1 MerkleDataNodeEncoder_V1 (com.jd.blockchain.ledger.proof.MerkleDataNodeEncoder_V1)1 Random (java.util.Random)1 Test (org.junit.Test)1 Bytes (utils.Bytes)1