Search in sources :

Example 26 with CryptoSetting

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

the class TransactionSetTest method testTransactionSequence.

@Test
public void testTransactionSequence() {
    CryptoSetting cryptoSetting = LedgerTestUtils.createDefaultCryptoSetting();
    MemoryKVStorage testStorage = new MemoryKVStorage();
    // Create a new TransactionSet;
    TransactionSetEditor txset = new TransactionSetEditor(cryptoSetting, keyPrefix, testStorage, testStorage, LedgerDataStructure.MERKLE_TREE);
    HashDigest ledgerHash = LedgerTestUtils.generateRandomHash();
    long blockHeight = 8922L;
    // 生成指定数量的测试数据:交易请求和交易执行结果;
    int txCount0 = 10;
    TransactionRequest[] txRequests_0 = new TransactionRequest[txCount0];
    TransactionResult[] txResults_0 = new TransactionResult[txCount0];
    buildRequestAndResult(ledgerHash, blockHeight, cryptoSetting, txCount0, txRequests_0, txResults_0);
    // add tx to trasaction set;
    for (int i = 0; i < txCount0; i++) {
        txset.addTransaction(txRequests_0[i], txResults_0[i]);
    }
    txset.commit();
    // 验证交易集合中记录的交易顺序;
    assertEquals(txCount0, txset.getTotalCount());
    TransactionResult[] actualTxResults = txset.getTransactionResults(0, txCount0);
    assertEquals(txCount0, actualTxResults.length);
    for (int i = 0; i < txCount0; i++) {
        assertTransactionEquals(txResults_0[i], actualTxResults[i]);
    }
    // 重新加载交易集合;
    HashDigest txsetRootHash = txset.getRootHash();
    TransactionSetEditor reloadTxset = new TransactionSetEditor(-1, txsetRootHash, cryptoSetting, keyPrefix, testStorage, testStorage, LedgerDataStructure.MERKLE_TREE, true);
    // 验证重新加载之后的交易集合中记录的交易顺序;
    assertEquals(txCount0, reloadTxset.getTotalCount());
    TransactionResult[] actualTxResults_reload = reloadTxset.getTransactionResults(0, txCount0);
    assertEquals(txCount0, actualTxResults_reload.length);
    for (int i = 0; i < txCount0; i++) {
        assertTransactionEquals(txResults_0[i], actualTxResults_reload[i]);
    }
    // 生成指定数量的测试数据:交易请求和交易执行结果;
    int txCount1 = new Random().nextInt(200) + 1;
    TransactionRequest[] txRequests_1 = new TransactionRequest[txCount1];
    TransactionResult[] txResults_1 = new TransactionResult[txCount1];
    buildRequestAndResult(ledgerHash, blockHeight, cryptoSetting, txCount1, txRequests_1, txResults_1);
    // add tx to trasaction set;
    TransactionSetEditor newTxset = new TransactionSetEditor(-1, txsetRootHash, cryptoSetting, keyPrefix, testStorage, testStorage, LedgerDataStructure.MERKLE_TREE, false);
    for (int i = 0; i < txCount1; i++) {
        newTxset.addTransaction(txRequests_1[i], txResults_1[i]);
    }
    newTxset.commit();
    // 验证交易集合中记录的交易顺序;
    int totalCount = txCount0 + txCount1;
    assertEquals(totalCount, newTxset.getTotalCount());
    TransactionResult[] actualTxResults_reload_2 = newTxset.getTransactionResults(0, totalCount);
    assertEquals(totalCount, actualTxResults_reload_2.length);
    TransactionRequest[] txRequests = ArrayUtils.concat(txRequests_0, txRequests_1, TransactionRequest.class);
    TransactionResult[] txResults = ArrayUtils.concat(txResults_0, txResults_1, TransactionResult.class);
    for (int i = 0; i < totalCount; i++) {
        assertTransactionEquals(txResults[i], actualTxResults_reload_2[i]);
    }
}
Also used : TransactionResult(com.jd.blockchain.ledger.TransactionResult) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) HashDigest(com.jd.blockchain.crypto.HashDigest) Random(java.util.Random) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) TransactionSetEditor(com.jd.blockchain.ledger.core.TransactionSetEditor) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest) Test(org.junit.Test)

Example 27 with CryptoSetting

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

the class TransactionSetTest method testSpecialCase_1.

/**
 * 根据实际运行中一个随机出现的错误中提取到的数据来建立的测试用例,可以更简化地验证正确性;
 *
 * <p>
 *
 * 注:重构了 {@link LedgerTransaction} 和 {@link TransactionContent}
 * 等交易结构相关的类型之后,此用例已经失效; by huanghaiquan on 2020-09-16;
 */
// @Test
public void testSpecialCase_1() {
    CryptoSetting defCryptoSetting = LedgerTestUtils.createDefaultCryptoSetting();
    MemoryKVStorage testStorage = new MemoryKVStorage();
    BufferedKVStorage bufferStorage = new BufferedKVStorage(null, testStorage, testStorage, false);
    // Create a new TransactionSet, it's empty;
    TransactionSetEditor txset = new TransactionSetEditor(defCryptoSetting, keyPrefix, bufferStorage, bufferStorage, LedgerDataStructure.MERKLE_TREE);
    assertTrue(txset.isUpdated());
    assertFalse(txset.isReadonly());
    assertNull(txset.getRootHash());
    HashDigest ledgerHash = Crypto.resolveAsHashDigest(Base58Utils.decode("j5iF5xJ7KN4kjRrhD3EUKVSPmHz2bExxp3h9avqxcnnzch"));
    assertEquals("j5iF5xJ7KN4kjRrhD3EUKVSPmHz2bExxp3h9avqxcnnzch", ledgerHash.toBase58());
    BlockchainKeypair parti0 = LedgerTestUtils.createKeyPair("7VeRLBwqTAz8oRazEazeaEfqei46sk2FzvBgyHMUBJvrUEGT", "7VeRUm27GbrsX9HbQSZguChLp24HZYub6s5FJ7FjBht8BmbA");
    BlockchainKeypair userKeypair1 = LedgerTestUtils.createKeyPair("7VeRKf3GFLFcBfzvtzmtyMXEoX2HYGEJ4j7CmHcnRV99W5Dp", "7VeRYQjeAaQY5Po8MMtmGNHA2SniqLXmJaZwBS5K8zTtMAU1");
    TransactionRequest transactionRequest1 = LedgerTestUtils.createTxRequest_UserReg_SHA256(userKeypair1, ledgerHash, 1580315317127L, parti0, parti0);
    // TransactionRequest transactionRequest1 = LedgerTestUtils.createTxRequest_UserReg(userKeypair1, ledgerHash, 202001202020L,
    // parti0, parti0);
    System.out.printf("\r\n ===||=== transactionRequest1.getTransactionHash()=[%s]\r\n", transactionRequest1.getTransactionHash().toBase58());
    // assertEquals("j5sXmpcomtM2QMUNWeQWsF8bNFFnyeXoCjVAekEeLSscgY", transactionRequest1.getTransactionHash().toBase58());
    assertEquals("j5wPGKT5CUzwi8j6VfCWaP2p9YZ6WVWtMANp9HbHWzvhgG", transactionRequest1.getTransactionHash().toBase58());
    TransactionStagedSnapshot txSnapshot = new TransactionStagedSnapshot();
    txSnapshot.setAdminAccountHash(Crypto.resolveAsHashDigest(Base58Utils.decode("j5taeK6cpmJGcn8QbEYCqadna6s7NDSheDTK6NJdU4mFhh")));
    txSnapshot.setUserAccountSetHash(Crypto.resolveAsHashDigest(Base58Utils.decode("j5oQDSob92mCoGSHtrXa9soqgAtMyjwfRMt2kj7igXXJrP")));
    TransactionResult tx = new TransactionResultData(transactionRequest1.getTransactionHash(), 1, TransactionState.SUCCESS, txSnapshot);
    txset.addTransaction(transactionRequest1, tx);
    LedgerTransaction tx_query = txset.getTransaction(transactionRequest1.getTransactionHash());
    assertNotNull(tx_query);
    txset.commit();
    bufferStorage.commit();
    tx_query = txset.getTransaction(transactionRequest1.getTransactionHash());
    TransactionState tx_state = txset.getState(transactionRequest1.getTransactionHash());
    assertNotNull(tx_query);
    assertEquals(0, tx_state.CODE);
    HashDigest txsetRootHash = txset.getRootHash();
    txset = new TransactionSetEditor(-1, txsetRootHash, defCryptoSetting, keyPrefix, testStorage, testStorage, LedgerDataStructure.MERKLE_TREE, false);
    tx_query = txset.getTransaction(transactionRequest1.getTransactionHash());
    tx_state = txset.getState(transactionRequest1.getTransactionHash());
    assertNotNull(tx_query);
    assertEquals(0, tx_state.CODE);
}
Also used : TransactionState(com.jd.blockchain.ledger.TransactionState) TransactionStagedSnapshot(com.jd.blockchain.ledger.core.TransactionStagedSnapshot) TransactionResult(com.jd.blockchain.ledger.TransactionResult) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) BufferedKVStorage(com.jd.blockchain.storage.service.utils.BufferedKVStorage) HashDigest(com.jd.blockchain.crypto.HashDigest) LedgerTransaction(com.jd.blockchain.ledger.LedgerTransaction) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) TransactionSetEditor(com.jd.blockchain.ledger.core.TransactionSetEditor) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest) TransactionResultData(com.jd.blockchain.ledger.core.TransactionResultData)

Example 28 with CryptoSetting

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

the class MerkleHashTrieTest method testMerkleTreeDiffNewPathType.

@Test
public void testMerkleTreeDiffNewPathType() {
    CryptoSetting cryptoSetting = createCryptoSetting();
    MemoryKVStorage storage = new MemoryKVStorage();
    List<String> newdataListString = new ArrayList<String>();
    List<String> dataListString = new ArrayList<String>();
    List<VersioningKVData<String, byte[]>> newdataList = new ArrayList<VersioningKVData<String, byte[]>>();
    int count = 1;
    int newAddCount = 5;
    List<VersioningKVData<String, byte[]>> dataList = generateSpecKeyDatas(dataListString, 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 random key 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[]> data0 = new VersioningKVData<String, byte[]>("KEY-1741789838495252", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
    newdataList.add(data0);
    VersioningKVData<String, byte[]> data1 = new VersioningKVData<String, byte[]>("KEY-2741789838505562", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
    newdataList.add(data1);
    VersioningKVData<String, byte[]> data2 = new VersioningKVData<String, byte[]>("KEY-0745023937104559", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
    newdataList.add(data2);
    VersioningKVData<String, byte[]> data3 = new VersioningKVData<String, byte[]>("KEY-7745261599950097", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
    newdataList.add(data3);
    VersioningKVData<String, byte[]> data4 = new VersioningKVData<String, byte[]>("KEY-9745261599963367", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
    newdataList.add(data4);
    for (int i = 0; i < newdataList.size(); i++) {
        merkleTree_reload.setData(newdataList.get(i).getKey(), newdataList.get(i).getVersion(), newdataList.get(i).getValue());
        newdataListString.add(newdataList.get(i).getKey());
    }
    merkleTree_reload.commit();
    HashDigest rootHash1 = merkleTree_reload.getRootHash();
    assertNotNull(rootHash1);
    assertNotEquals(rootHash0, rootHash1);
    assertEquals(count + newAddCount, merkleTree_reload.getTotalKeys());
    assertEquals(count + newAddCount, merkleTree_reload.getTotalRecords());
    SkippingIterator<KVEntry> diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
    // max boundary skip test
    assertEquals(newAddCount, diffIterator.getTotalCount());
    assertEquals(-1, diffIterator.getCursor());
    assertTrue(diffIterator.hasNext());
    long skipped = diffIterator.skip(newAddCount);
    assertEquals(newAddCount, skipped);
    assertFalse(diffIterator.hasNext());
    // re-interator and random skip test
    int skipNum = 4;
    diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
    assertEquals(newAddCount, diffIterator.getTotalCount());
    assertEquals(-1, diffIterator.getCursor());
    assertTrue(diffIterator.hasNext());
    long skipped1 = diffIterator.skip(skipNum);
    assertEquals(skipNum, skipped1);
    int diffNum = 0;
    // TODO: 无效的验证逻辑; by huanghaiquan at 2020-07-15;
    // while (diffIterator.hasNext()) {
    // MerkleData data = diffIterator.next();
    // assertNotNull(data);
    // assertFalse(dataList.contains(new String(data.getKey())));
    // assertTrue(newdataListString.contains(new String(data.getKey())));
    // diffNum++;
    // }
    // assertEquals(diffNum, diffIterator.getCount() - skipNum);
    // re-interator and next test
    diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
    int diffNum1 = 0;
    assertEquals(newAddCount, diffIterator.getTotalCount());
    while (diffIterator.hasNext()) {
        KVEntry data = diffIterator.next();
        assertNotNull(data);
        diffNum1++;
    }
    assertFalse(diffIterator.hasNext());
    assertEquals(newAddCount - 1, diffIterator.getCursor());
    assertEquals(newAddCount, diffIterator.getTotalCount());
    assertEquals(diffNum1, diffIterator.getTotalCount());
    // re-interator and test next key consistency
    diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
// TODO: 无效的验证逻辑; by huanghaiquan at 2020-07-15;
// while (diffIterator.hasNext()) {
// MerkleData data = diffIterator.next();
// assertNotNull(data);
// assertFalse(dataList.contains(new String(data.getKey())));
// assertTrue(newdataListString.contains(new String(data.getKey())));
// }
}
Also used : ArrayList(java.util.ArrayList) VersioningKVData(com.jd.blockchain.storage.service.utils.VersioningKVData) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) KVEntry(com.jd.blockchain.ledger.merkletree.KVEntry) HashDigest(com.jd.blockchain.crypto.HashDigest) MerkleHashTrie(com.jd.blockchain.ledger.proof.MerkleHashTrie) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) Test(org.junit.Test)

Example 29 with CryptoSetting

use of com.jd.blockchain.ledger.CryptoSetting 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 30 with CryptoSetting

use of com.jd.blockchain.ledger.CryptoSetting 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)

Aggregations

CryptoSetting (com.jd.blockchain.ledger.CryptoSetting)35 Test (org.junit.Test)26 HashDigest (com.jd.blockchain.crypto.HashDigest)23 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)23 MerkleHashTrie (com.jd.blockchain.ledger.proof.MerkleHashTrie)15 VersioningKVData (com.jd.blockchain.storage.service.utils.VersioningKVData)12 Random (java.util.Random)11 KVEntry (com.jd.blockchain.ledger.merkletree.KVEntry)10 ArrayList (java.util.ArrayList)8 MerkleSequenceTree (com.jd.blockchain.ledger.proof.MerkleSequenceTree)7 ExistancePolicyKVStorageMap (com.jd.blockchain.storage.service.utils.ExistancePolicyKVStorageMap)7 MerkleNode (com.jd.blockchain.ledger.MerkleNode)4 TransactionRequest (com.jd.blockchain.ledger.TransactionRequest)4 TransactionSetEditor (com.jd.blockchain.ledger.core.TransactionSetEditor)4 MerkleTrieData (com.jd.blockchain.ledger.proof.MerkleTrieData)4 TreeMap (java.util.TreeMap)4 Bytes (utils.Bytes)4 TransactionResult (com.jd.blockchain.ledger.TransactionResult)3 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)2 LedgerTransaction (com.jd.blockchain.ledger.LedgerTransaction)2