Search in sources :

Example 6 with MerkleTrieData

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

the class MerkleHashTrieTest method testReloadTreeAddRandomNewDataNode.

/**
 * 对已存在的树进行重载,增加新的数据节点,通过重载树验证新节点是否添加成功,total keys 与total records
 * 是否符合预期,新添加的数据节点Key随机产生
 */
@Test
public void testReloadTreeAddRandomNewDataNode() {
    Random random = new Random();
    byte[] bytes = new byte[200];
    random.nextBytes(bytes);
    String newDataKey = Arrays.toString(bytes);
    CryptoSetting cryptoSetting = createCryptoSetting();
    MemoryKVStorage storage = new MemoryKVStorage();
    int count = 1024;
    List<VersioningKVData<String, byte[]>> dataList = generateDatas(count);
    VersioningKVData<String, byte[]>[] datas = toArray(dataList);
    MerkleHashTrie merkleTree = newMerkleTree_with_committed(datas, cryptoSetting, storage);
    HashDigest rootHash0 = merkleTree.getRootHash();
    assertNotNull(rootHash0);
    assertEquals(count, merkleTree.getTotalKeys());
    assertEquals(count, merkleTree.getTotalRecords());
    // reload and add one data item;
    MerkleHashTrie merkleTree_reload = new MerkleHashTrie(rootHash0, cryptoSetting, KEY_PREFIX, storage, false);
    assertEquals(count, merkleTree_reload.getTotalKeys());
    assertEquals(count, merkleTree_reload.getTotalRecords());
    assertEquals(rootHash0, merkleTree_reload.getRootHash());
    VersioningKVData<String, byte[]> data1025 = new VersioningKVData<String, byte[]>(newDataKey, 0, BytesUtils.toBytes("NEW-VALUE-1025-VERSION-0"));
    merkleTree_reload.setData(data1025.getKey(), data1025.getVersion(), data1025.getValue());
    merkleTree_reload.commit();
    HashDigest rootHash1 = merkleTree_reload.getRootHash();
    assertNotNull(rootHash1);
    assertNotEquals(rootHash0, rootHash1);
    MerkleTrieData data1025_reload_0 = merkleTree_reload.getData(data1025.getKey(), 0);
    assertNotNull(data1025_reload_0);
    assertNull(data1025_reload_0.getPreviousEntryHash());
    MerkleTrieData data0_reload_0 = merkleTree_reload.getData("KEY-0", 0);
    assertNotNull(data0_reload_0);
    assertNull(data0_reload_0.getPreviousEntryHash());
    System.out.println("mkl reload total keys = " + merkleTree_reload.getTotalKeys());
    assertEquals(count + 1, merkleTree_reload.getTotalKeys());
    MerkleHashTrie merkleTree_reload_1 = new MerkleHashTrie(rootHash1, cryptoSetting, KEY_PREFIX, storage, false);
    assertEquals(count + 1, merkleTree_reload_1.getTotalKeys());
    assertEquals(count + 1, merkleTree_reload_1.getTotalRecords());
    assertEquals(rootHash1, merkleTree_reload_1.getRootHash());
    HashDigest rootHash2 = merkleTree_reload_1.getRootHash();
    assertNotNull(rootHash2);
    assertNotEquals(rootHash0, rootHash2);
    MerkleTrieData data1025_reload_1 = merkleTree_reload_1.getData(data1025.getKey(), 0);
    assertNotNull(data1025_reload_1);
    assertNull(data1025_reload_1.getPreviousEntryHash());
    MerkleTrieData data0_reload_1 = merkleTree_reload_1.getData("KEY-0", 0);
    assertNotNull(data0_reload_1);
    assertNull(data0_reload_1.getPreviousEntryHash());
    System.out.println("mkl reload total keys = " + merkleTree_reload_1.getTotalKeys());
    assertEquals(count + 1, merkleTree_reload_1.getTotalKeys());
}
Also used : VersioningKVData(com.jd.blockchain.storage.service.utils.VersioningKVData) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) Random(java.util.Random) SecureRandom(java.security.SecureRandom) 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 7 with MerkleTrieData

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

the class MerkleHashTrieTest method testReloading.

/**
 * 测试树的加载读取;
 */
@Test
public void testReloading() {
    CryptoSetting cryptoSetting = createCryptoSetting();
    MemoryKVStorage storage = new MemoryKVStorage();
    // 数据集合长度为 100 时也能正常生成;
    int count = 1024;
    List<VersioningKVData<String, byte[]>> dataList = generateDatas(count);
    VersioningKVData<String, byte[]>[] datas = toArray(dataList);
    MerkleHashTrie merkleTree = newMerkleTree_with_committed(datas, cryptoSetting, storage);
    HashDigest rootHash0 = merkleTree.getRootHash();
    assertNotNull(rootHash0);
    assertEquals(count, merkleTree.getTotalKeys());
    assertEquals(count, merkleTree.getTotalRecords());
    // TODO: 暂时忽略默克尔证明的测试;
    // for (VersioningKVData<String, byte[]> data : datas) {
    // assertMerkleProof(data, merkleTree);
    // }
    // testMerkleProof1024(datas, merkleTree);
    MerkleHashTrie merkleTree_reload = new MerkleHashTrie(rootHash0, cryptoSetting, KEY_PREFIX, storage, false);
    assertEquals(count, merkleTree_reload.getTotalKeys());
    assertEquals(count, merkleTree_reload.getTotalRecords());
    // TODO: 暂时忽略默克尔证明的测试;
    // testMerkleProof1024(datas, merkleTree_reload);
    VersioningKVData<String, byte[]> data28 = new VersioningKVData<String, byte[]>("KEY-28", 1, BytesUtils.toBytes("NEW-VALUE-VERSION-1"));
    VersioningKVData<String, byte[]> data606 = new VersioningKVData<String, byte[]>("KEY-606", 1, BytesUtils.toBytes("NEW-VALUE-VERSION-1"));
    VersioningKVData<String, byte[]> data770 = new VersioningKVData<String, byte[]>("KEY-770", 1, BytesUtils.toBytes("NEW-VALUE-VERSION-1"));
    VersioningKVData<String, byte[]> data898 = new VersioningKVData<String, byte[]>("KEY-898", 1, BytesUtils.toBytes("NEW-VALUE-VERSION-1"));
    VersioningKVData<String, byte[]> data69 = new VersioningKVData<String, byte[]>("KEY-69", 1, BytesUtils.toBytes("NEW-VALUE-VERSION-1"));
    merkleTree_reload.setData(data28.getKey(), data28.getVersion(), data28.getValue());
    merkleTree_reload.setData(data606.getKey(), data606.getVersion(), data606.getValue());
    merkleTree_reload.setData(data770.getKey(), data770.getVersion(), data770.getValue());
    merkleTree_reload.setData(data898.getKey(), data898.getVersion(), data898.getValue());
    merkleTree_reload.setData(data69.getKey(), data69.getVersion(), data69.getValue());
    merkleTree_reload.commit();
    HashDigest rootHash1 = merkleTree_reload.getRootHash();
    assertNotNull(rootHash1);
    assertNotEquals(rootHash0, rootHash1);
    // TODO: 暂时忽略默克尔证明的测试;
    // MerkleProof proof = merkleTree_reload.getProof(data28.getKey(), 1);
    // assertNotNull(proof);
    // MerkleProofLevel[] hashPaths = proof.getProofLevels();
    // assertEquals(5, hashPaths.length);
    // proof = merkleTree_reload.getProof(data28.getKey(), 0);
    // assertNotNull(proof);
    // hashPaths = proof.getProofLevels();
    // assertEquals(6, hashPaths.length);
    MerkleTrieData data28_reload_0 = merkleTree_reload.getData(data28.getKey(), 0);
    assertNotNull(data28_reload_0);
    assertNull(data28_reload_0.getPreviousEntryHash());
    assertEquals(data28.getKey(), data28_reload_0.getKey().toUTF8String());
    assertEquals(datas[28].getVersion(), data28_reload_0.getVersion());
    assertArrayEquals(datas[28].getValue(), data28_reload_0.getValue().toBytes());
    MerkleTrieData data28_reload_1 = merkleTree_reload.getData(data28.getKey(), 1);
    assertNotNull(data28_reload_1);
    assertNotNull(data28_reload_1.getPreviousEntryHash());
    assertEquals(data28.getKey(), data28_reload_1.getKey().toUTF8String());
    assertEquals(data28.getVersion(), data28_reload_1.getVersion());
    assertArrayEquals(data28.getValue(), data28_reload_1.getValue().toBytes());
    // merkleTree_reload.print();
    // 测试不同根哈希加载的默克尔树能够检索的最新版本;
    MerkleHashTrie merkleTree_0 = new MerkleHashTrie(rootHash0, cryptoSetting, KEY_PREFIX, storage, false);
    MerkleHashTrie merkleTree_1 = new MerkleHashTrie(rootHash1, cryptoSetting, KEY_PREFIX, storage, false);
    MerkleTrieData data28_reload = merkleTree_0.getData(data28.getKey());
    assertEquals(0, data28_reload.getVersion());
    data28_reload = merkleTree_1.getData(data28.getKey());
    assertEquals(1, data28_reload.getVersion());
    // 测试在修改状态下检索默克尔证明的正确性;
    VersioningKVData<String, byte[]> data28_2 = new VersioningKVData<String, byte[]>("KEY-28", 2, BytesUtils.toBytes("NEW-VALUE-VERSION-2"));
// TODO: 暂时忽略默克尔证明的测试;
// MerkleProof proof28_1 = merkleTree_1.getProof("KEY-28", 1);
// MerkleProof proof606_1 = merkleTree_1.getProof("KEY-606", 1);
// assertNotNull(proof28_1);
// assertNotNull(proof606_1);
// 针对编号为 28 的数据加入一条新版本记录;
// merkleTree_1.setData(data28_2.getKey(), data28_2.getVersion(), data28_2.getValue());
// 对于修改中的数据项,查询未提交的最新版本数据的默克尔证明为 null,但是其已提交版本的证明不受影响;
// 此外,其它未修改的数据项的默克尔证明也不受影响;
// MerkleProof proof28_1_1 = merkleTree_1.getProof("KEY-28", 1);
// MerkleProof proof28_2 = merkleTree_1.getProof("KEY-28", 2);
// MerkleProof proof606_1_1 = merkleTree_1.getProof("KEY-606", 1);
// assertNotNull(proof28_1_1);
// assertNotNull(proof606_1_1);
// assertNull(proof28_2);
// assertEquals(proof28_1, proof28_1_1);
// assertEquals(proof606_1, proof606_1_1);
// 当提交修改之后,可以获取到修改数据项的最新版本的证明,同时其旧版本的证明也刷新了中间路径(加入了新版本数据节点); 
// merkleTree_1.commit();
// TODO: 暂时忽略默克尔证明的测试;
// MerkleProof proof28_1_2 = merkleTree_1.getProof("KEY-28", 1);
// MerkleProof proof28_2_1 = merkleTree_1.getProof("KEY-28", 2);
// MerkleProof proof606_1_2 = merkleTree_1.getProof("KEY-606", 1);
// assertNotNull(proof28_1_2);
// assertNotNull(proof28_2_1);
// assertNotNull(proof606_1_2);
// // 由于默克尔树发生了修改,所有默克尔证明发生了改变;
// assertFalse(proof28_1.equals(proof28_1_2));
// assertFalse(proof606_1.equals(proof606_1_2));
// 同一个key的数据项的最新版本的默克尔证明路径节点中数据节点部分(倒数第2项),出现在其前一个版本的更新后的数据证明中倒数第3项;
// 验证默克尔证明的长度增长;
// MerkleProof proof28_5 = merkleTree_1.getProof("KEY-28", 0);
// assertNotNull(proof28_5);
// hashPaths = proof28_5.getProofLevels();
// assertEquals(7, hashPaths.length);
// TODO: 暂时忽略默克尔证明的测试;
// 重新加载默克尔树,默克尔证明是一致的;
// MerkleHashTrie merkleTree_1_1 = new MerkleHashTrie(rootHash1, cryptoSetting, KEY_PREFIX, storage, false);
// MerkleProof proof28_4 = merkleTree_1_1.getProof("KEY-28", 1);
// assertNotNull(proof28_4);
// assertEquals(proof28_1, proof28_4);
// merkleTree_1.print();
}
Also used : 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) VersioningKVData(com.jd.blockchain.storage.service.utils.VersioningKVData) Test(org.junit.Test)

Example 8 with MerkleTrieData

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

the class MerkleHashTrieTest method testReloadTreeAddNewDataNode.

/**
 * 对已存在的树进行重载,增加新的数据节点,通过重载树验证新节点是否添加成功,total keys 与total records
 * 是否符合预期,新添加的数据节点Key具有一定的规律
 */
@Test
public void testReloadTreeAddNewDataNode() {
    CryptoSetting cryptoSetting = createCryptoSetting();
    MemoryKVStorage storage = new MemoryKVStorage();
    int count = 1024;
    List<VersioningKVData<String, byte[]>> dataList = generateDatas(count);
    VersioningKVData<String, byte[]>[] datas = toArray(dataList);
    MerkleHashTrie merkleTree = newMerkleTree_with_committed(datas, cryptoSetting, storage);
    HashDigest rootHash0 = merkleTree.getRootHash();
    assertNotNull(rootHash0);
    assertEquals(count, merkleTree.getTotalKeys());
    assertEquals(count, merkleTree.getTotalRecords());
    // reload and add one data item;
    MerkleHashTrie merkleTree_reload = new MerkleHashTrie(rootHash0, cryptoSetting, KEY_PREFIX, storage, false);
    assertEquals(count, merkleTree_reload.getTotalKeys());
    assertEquals(count, merkleTree_reload.getTotalRecords());
    assertEquals(rootHash0, merkleTree_reload.getRootHash());
    VersioningKVData<String, byte[]> data1025 = new VersioningKVData<String, byte[]>("KEY-1025", 0, BytesUtils.toBytes("NEW-VALUE-1025-VERSION-0"));
    merkleTree_reload.setData(data1025.getKey(), data1025.getVersion(), data1025.getValue());
    merkleTree_reload.commit();
    HashDigest rootHash1 = merkleTree_reload.getRootHash();
    assertNotNull(rootHash1);
    assertNotEquals(rootHash0, rootHash1);
    MerkleTrieData data1025_reload_0 = merkleTree_reload.getData(data1025.getKey(), 0);
    assertNotNull(data1025_reload_0);
    assertNull(data1025_reload_0.getPreviousEntryHash());
    MerkleTrieData data0_reload_0 = merkleTree_reload.getData("KEY-0", 0);
    assertNotNull(data0_reload_0);
    assertNull(data0_reload_0.getPreviousEntryHash());
    System.out.println("mkl reload total keys = " + merkleTree_reload.getTotalKeys());
    assertEquals(count + 1, merkleTree_reload.getTotalKeys());
    MerkleHashTrie merkleTree_reload_1 = new MerkleHashTrie(rootHash1, cryptoSetting, KEY_PREFIX, storage, false);
    assertEquals(count + 1, merkleTree_reload_1.getTotalKeys());
    assertEquals(count + 1, merkleTree_reload_1.getTotalRecords());
    assertEquals(rootHash1, merkleTree_reload_1.getRootHash());
    HashDigest rootHash2 = merkleTree_reload_1.getRootHash();
    assertNotNull(rootHash2);
    assertNotEquals(rootHash0, rootHash2);
    MerkleTrieData data1025_reload_1 = merkleTree_reload_1.getData(data1025.getKey(), 0);
    assertNotNull(data1025_reload_1);
    assertNull(data1025_reload_1.getPreviousEntryHash());
    MerkleTrieData data0_reload_1 = merkleTree_reload_1.getData("KEY-0", 0);
    assertNotNull(data0_reload_1);
    assertNull(data0_reload_1.getPreviousEntryHash());
    System.out.println("mkl reload total keys = " + merkleTree_reload_1.getTotalKeys());
    assertEquals(count + 1, merkleTree_reload_1.getTotalKeys());
}
Also used : 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) VersioningKVData(com.jd.blockchain.storage.service.utils.VersioningKVData) 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