Search in sources :

Example 1 with MerkleTrieData

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

the class LedgerEditorTest method testMerkleDataSet1.

@Test
public void testMerkleDataSet1() {
    CryptoSetting setting = LedgerTestUtils.createDefaultCryptoSetting();
    Bytes keyPrefix = Bytes.fromString(LedgerTestUtils.LEDGER_KEY_PREFIX).concat(MerkleHashDataset.MERKLE_TREE_PREFIX);
    MemoryKVStorage storage = new MemoryKVStorage();
    byte[] key = Base58Utils.decode("j5q7n8ShYqKVitaobZrERtBK7GowGGZ54RuaUeWjLsdPYY");
    HashDigest valueHash = Crypto.resolveAsHashDigest(Base58Utils.decode("j5o6mMnMQqE5fJKJ93FzXPnu4vFCfpBKp7u4r8tUUaFRK8"));
    long version = 0;
    MerkleHashTrie merkleTree = new MerkleHashTrie(setting, keyPrefix, storage);
    merkleTree.setData(key, version, valueHash.toBytes());
    merkleTree.commit();
    MerkleTrieData data = merkleTree.getData(key);
    assertNotNull(data);
    merkleTree = new MerkleHashTrie(merkleTree.getRootHash(), setting, keyPrefix, storage, false);
    data = merkleTree.getData(key);
    assertNotNull(data);
// MerkleDataSet1 mkds = new MerkleDataSet1(setting, keyPrefix, storage, storage);
// HashDigest ledgerHash = new HashDigest(Base58Utils.decode("j5mxiw6RiHP7fhrySjYji1ER5aRe6d2quYHArtwUfsyoHZ"));
// 
// BlockchainKeypair user1 = LedgerTestUtils.createKeyPair("7VeRKf3GFLFcBfzvtzmtyMXEoX2HYGEJ4j7CmHcnRV99W5Dp",
// "7VeRYQjeAaQY5Po8MMtmGNHA2SniqLXmJaZwBS5K8zTtMAU1");
// TransactionRequest req1 = LedgerTestUtils.createTxRequest_UserReg(user1, ledgerHash, 1580315317127L, parti0,
// parti0);
// // 引发错误的参数:ts=1580315317127;
// // txhash=j5wPGKT5CUzwi8j6VfCWaP2p9YZ6WVWtMANp9HbHWzvhgG
// System.out.printf("\r\n ===||=== transactionRequest1.getTransactionContent().getHash()=[%s]\r\n",
// req1.getTransactionContent().getHash().toBase58());
}
Also used : Bytes(utils.Bytes) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) HashDigest(com.jd.blockchain.crypto.HashDigest) MerkleTrieData(com.jd.blockchain.ledger.proof.MerkleTrieData) MerkleHashTrie(com.jd.blockchain.ledger.proof.MerkleHashTrie) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) Test(org.junit.Test)

Example 2 with MerkleTrieData

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

the class MerkleHashTrieTest method testMerkleDataSerialize.

/**
 * 验证 MerkleData 序列化和反序列化的一致性;
 *
 * @param data
 */
private void testMerkleDataSerialize(MerkleTrieData data) {
    byte[] dataBytes = BinaryProtocol.encode(data, MerkleTrieData.class);
    int offset = 0;
    int code = BytesUtils.toInt(dataBytes, offset);
    offset += 12;
    assertEquals(DataCodes.MERKLE_TRIE_DATA, code);
    byte[] valueHashBytes = data.getValue().toBytes();
    int expectedSize = 12 + NumberMask.NORMAL.getMaskLength(data.getKey().size()) + data.getKey().size() + NumberMask.LONG.getMaskLength(data.getVersion()) + NumberMask.NORMAL.getMaskLength(valueHashBytes.length) + valueHashBytes.length + NumberMask.NORMAL.getMaskLength(0);
    assertEquals(expectedSize, dataBytes.length);
    DataContractEncoder dataContractEncoder = DataContractRegistry.register(MerkleTrieData.class);
    DataSpecification dataSpec = dataContractEncoder.getSpecification();
    assertEquals(4, dataSpec.getFields().size());
    assertEquals(5, dataSpec.getSlices().size());
    System.out.println(dataSpec.toString());
    MerkleTrieData dataDes = BinaryProtocol.decode(dataBytes);
    assertTrue(data.getKey().equals(dataDes.getKey()));
    assertEquals(data.getVersion(), dataDes.getVersion());
    assertEquals(data.getValue(), dataDes.getValue());
    assertEquals(data.getPreviousEntryHash(), dataDes.getPreviousEntryHash());
}
Also used : MerkleTrieData(com.jd.blockchain.ledger.proof.MerkleTrieData) DataSpecification(com.jd.binaryproto.DataSpecification) DataContractEncoder(com.jd.binaryproto.DataContractEncoder)

Example 3 with MerkleTrieData

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

the class MerkleHashTrieTest method testCancel.

/**
 * 验证 HashSortingMerkleTree 在未提交之前,新增的数据记录可读和可回滚特性;
 */
@Test
public void testCancel() {
    // 数据集合长度为 1024 时也能正常生成;
    int count = 1024;
    List<VersioningKVData<String, byte[]>> dataList = generateDatas(count);
    VersioningKVData<String, byte[]>[] datas = toArray(dataList);
    MerkleHashTrie merkleTree = newMerkleTree(datas);
    assertTrue(merkleTree.isUpdated());
    // 未提交之前查不到信息;
    assertNull(merkleTree.getRootHash());
    assertEquals(0, merkleTree.getTotalKeys());
    assertEquals(0, merkleTree.getTotalRecords());
    MerkleTrieData dt = merkleTree.getData("KEY-69");
    assertNotNull(dt);
    assertEquals(0, dt.getVersion());
    dt = merkleTree.getData("KEY-69", 0);
    assertNotNull(dt);
    assertEquals(0, dt.getVersion());
    dt = merkleTree.getData("KEY-69", 1);
    assertNull(dt);
    VersioningKVData<String, byte[]> data69 = new VersioningKVData<String, byte[]>("KEY-69", 1, BytesUtils.toBytes("NEW-VALUE-VERSION-1"));
    merkleTree.setData(data69.getKey(), data69.getVersion(), data69.getValue());
    dt = merkleTree.getData("KEY-69", 1);
    assertNotNull(dt);
    assertEquals(1, dt.getVersion());
    assertTrue(merkleTree.isUpdated());
    // 回滚全部数据;
    merkleTree.cancel();
    HashDigest rootHash = merkleTree.getRootHash();
    assertNull(rootHash);
    assertEquals(0, merkleTree.getTotalKeys());
    // 由于 KEY-69 写入了 2 个版本的记录;
    assertEquals(0, merkleTree.getTotalRecords());
    dt = merkleTree.getData("KEY-69");
    assertNull(dt);
    dt = merkleTree.getData("KEY-69", 0);
    assertNull(dt);
    dt = merkleTree.getData("KEY-69", 1);
    assertNull(dt);
    assertFalse(merkleTree.isUpdated());
}
Also used : MerkleTrieData(com.jd.blockchain.ledger.proof.MerkleTrieData) HashDigest(com.jd.blockchain.crypto.HashDigest) MerkleHashTrie(com.jd.blockchain.ledger.proof.MerkleHashTrie) VersioningKVData(com.jd.blockchain.storage.service.utils.VersioningKVData) Test(org.junit.Test)

Example 4 with MerkleTrieData

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

the class MerkleHashTrieTest method testReadUncommitting.

/**
 * 验证 HashSortingMerkleTree 在未提交之前的总数和根哈希维持不变的特性,新增的数据记录可读,但是具有临时性,一旦回滚则被清除;
 */
@Test
public void testReadUncommitting() {
    // 数据集合长度为 1024 时也能正常生成;
    int count = 500;
    List<VersioningKVData<String, byte[]>> dataList = generateDatas(count);
    VersioningKVData<String, byte[]>[] datas = toArray(dataList);
    MerkleHashTrie merkleTree = newMerkleTree(datas);
    // 未提交之前查不到信息;
    assertNull(merkleTree.getRootHash());
    assertEquals(0, merkleTree.getTotalKeys());
    assertEquals(0, merkleTree.getTotalRecords());
    MerkleTrieData dt = merkleTree.getData("KEY-69");
    assertNotNull(dt);
    assertEquals(0, dt.getVersion());
    dt = merkleTree.getData("KEY-69", 0);
    assertNotNull(dt);
    assertEquals(0, dt.getVersion());
    dt = merkleTree.getData("KEY-69", 1);
    assertNull(dt);
    VersioningKVData<String, byte[]> data69 = new VersioningKVData<String, byte[]>("KEY-69", 1, BytesUtils.toBytes("NEW-VALUE-VERSION-1"));
    merkleTree.setData(data69.getKey(), data69.getVersion(), data69.getValue());
    dt = merkleTree.getData("KEY-69", 1);
    assertNotNull(dt);
    assertEquals(1, dt.getVersion());
    merkleTree.commit();
    HashDigest rootHash = merkleTree.getRootHash();
    assertNotNull(rootHash);
    assertEquals(count, merkleTree.getTotalKeys());
    // 由于 KEY-69 写入了 2 个版本的记录;
    assertEquals(count + 1, merkleTree.getTotalRecords());
    dt = merkleTree.getData("KEY-69");
    assertNotNull(dt);
    assertEquals(1, dt.getVersion());
    dt = merkleTree.getData("KEY-69", 0);
    assertNotNull(dt);
    assertEquals(0, dt.getVersion());
    dt = merkleTree.getData("KEY-69", 1);
    assertNotNull(dt);
    assertEquals(1, dt.getVersion());
// merkleTree.print();
// TODO: 暂时忽略默克尔证明的测试;
// for (VersioningKVData<String, byte[]> data : datas) {
// assertMerkleProof(data, merkleTree);
// }
}
Also used : MerkleTrieData(com.jd.blockchain.ledger.proof.MerkleTrieData) HashDigest(com.jd.blockchain.crypto.HashDigest) MerkleHashTrie(com.jd.blockchain.ledger.proof.MerkleHashTrie) VersioningKVData(com.jd.blockchain.storage.service.utils.VersioningKVData) Test(org.junit.Test)

Example 5 with MerkleTrieData

use of com.jd.blockchain.ledger.proof.MerkleTrieData 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

MerkleTrieData (com.jd.blockchain.ledger.proof.MerkleTrieData)8 HashDigest (com.jd.blockchain.crypto.HashDigest)7 Test (org.junit.Test)7 MerkleHashTrie (com.jd.blockchain.ledger.proof.MerkleHashTrie)6 VersioningKVData (com.jd.blockchain.storage.service.utils.VersioningKVData)5 CryptoSetting (com.jd.blockchain.ledger.CryptoSetting)4 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)4 DataContractEncoder (com.jd.binaryproto.DataContractEncoder)1 DataSpecification (com.jd.binaryproto.DataSpecification)1 MerklePath (com.jd.blockchain.ledger.proof.MerklePath)1 MerkleTrieDataEntry (com.jd.blockchain.ledger.proof.MerkleTrieDataEntry)1 PathNode (com.jd.blockchain.ledger.proof.PathNode)1 SecureRandom (java.security.SecureRandom)1 Random (java.util.Random)1 Bytes (utils.Bytes)1