Search in sources :

Example 16 with MemoryKVStorage

use of com.jd.blockchain.storage.service.utils.MemoryKVStorage in project jdchain-core by blockchain-jd-com.

the class MerkleSortTreeTest method newMerkleSortedTree.

private static MerkleSortTree<byte[]> newMerkleSortedTree() {
    TreeOptions options = createTreeOptions();
    MemoryKVStorage storage = new MemoryKVStorage();
    MerkleSortTree<byte[]> mst = MerkleSortTree.createBytesTree(options, DEFAULT_MKL_KEY_PREFIX, storage);
    return mst;
}
Also used : TreeOptions(com.jd.blockchain.ledger.merkletree.TreeOptions) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage)

Example 17 with MemoryKVStorage

use of com.jd.blockchain.storage.service.utils.MemoryKVStorage in project jdchain-core by blockchain-jd-com.

the class MerkleSortTreeTest method testAddingAndAssertingEquals.

private static void testAddingAndAssertingEquals(long[] ids, byte[][] datas) {
    TreeOptions options = createTreeOptions();
    MemoryKVStorage storage = new MemoryKVStorage();
    Counter counter = new Counter();
    MerkleSortTree<byte[]> mst = MerkleSortTree.createBytesTree(options, DEFAULT_MKL_KEY_PREFIX, storage, counter);
    assertNull(mst.getRootHash());
    addDatasAndCommit(ids, datas, mst);
    assertEquals(ids.length, counter.count.get());
    HashDigest rootHash = mst.getRootHash();
    assertNotNull(rootHash);
    assertDataEquals(mst, ids, datas);
    // reload merkle tree from storage;
    MerkleSortTree<byte[]> mst1 = MerkleSortTree.createBytesTree(rootHash, options, DEFAULT_MKL_KEY_PREFIX, storage);
    assertEquals(rootHash, mst1.getRootHash());
    assertDataEquals(mst1, ids, datas);
}
Also used : TreeOptions(com.jd.blockchain.ledger.merkletree.TreeOptions) HashDigest(com.jd.blockchain.crypto.HashDigest) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage)

Example 18 with MemoryKVStorage

use of com.jd.blockchain.storage.service.utils.MemoryKVStorage in project jdchain-core by blockchain-jd-com.

the class MerkleSortTreeTest method testReadUncommitting.

/**
 * 测试读未提交数据;
 */
@Test
public void testReadUncommitting() {
    int count = 10;
    byte[][] datas = generateRandomData(count);
    long[] ids = generateSeqenceIDs(0, count);
    TreeOptions options = createTreeOptions();
    MemoryKVStorage storage = new MemoryKVStorage();
    MerkleSortTree<byte[]> mst = MerkleSortTree.createBytesTree(options, DEFAULT_MKL_KEY_PREFIX, storage);
    addDatas(ids, datas, mst);
    // 验证未提交之前能够读取到对应的数据;
    assertNull(mst.getRootHash());
    assertEquals(0, mst.getCount());
    assertEquals(ids[ids.length - 1], mst.getMaxId());
    assertDataExists(mst, ids, datas);
    mst.commit();
    assertNotNull(mst.getRootHash());
    assertDataEquals(mst, ids, datas);
    HashDigest rootHash = mst.getRootHash();
    mst = MerkleSortTree.createBytesTree(rootHash, options, DEFAULT_MKL_KEY_PREFIX, storage);
    assertDataEquals(mst, ids, datas);
    // 在已经有数据的默克尔树中以编码顺序递增的方式加入数据,验证在未提交之前能够读取到新加入的数据;
    int count1 = 200;
    byte[][] datas1 = generateRandomData(count1);
    long[] ids1 = generateSeqenceIDs(count + 10, count1);
    addDatas(ids1, datas1, mst);
    assertEquals(ids1[ids1.length - 1], mst.getMaxId());
    assertDataExists(mst, ids, datas);
    assertDataExists(mst, ids1, datas1);
    mst.commit();
    assertDataExists(mst, ids1, datas1);
    // 在已经有数据的默克尔树中以编码随机不重复产生的方式加入数据,验证在未提交之前能够读取到新加入的数据;
    Set<Long> excludingIDs = createIdSet(ids);
    joinIdSet(ids1, excludingIDs);
    int count2 = 300;
    byte[][] datas2 = generateRandomData(count2);
    long[] ids2 = generateRandomIDs(count2, excludingIDs, true);
    HashDigest rootHash1 = mst.getRootHash();
    mst = MerkleSortTree.createBytesTree(rootHash1, options, DEFAULT_MKL_KEY_PREFIX, storage);
    addDatas(ids2, datas2, mst);
    assertEquals(count + count1, mst.getCount());
    assertDataExists(mst, ids, datas);
    assertDataExists(mst, ids1, datas1);
    assertDataExists(mst, ids2, datas2);
    mst.commit();
    assertEquals(count + count1 + count2, mst.getCount());
    assertDataExists(mst, ids, datas);
    assertDataExists(mst, ids1, datas1);
    assertDataExists(mst, ids2, datas2);
}
Also used : TreeOptions(com.jd.blockchain.ledger.merkletree.TreeOptions) HashDigest(com.jd.blockchain.crypto.HashDigest) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) Test(org.junit.Test)

Example 19 with MemoryKVStorage

use of com.jd.blockchain.storage.service.utils.MemoryKVStorage in project jdchain-core by blockchain-jd-com.

the class TransactionBatchProcessorTest method testMultiTxsProcess.

@Test
public void testMultiTxsProcess() {
    final MemoryKVStorage STORAGE = new MemoryKVStorage();
    // 初始化账本到指定的存储库;
    HashDigest ledgerHash = LedgerTestUtils.initLedger(STORAGE, parti0, parti1, parti2, parti3);
    // 加载账本;
    LedgerManager ledgerManager = new LedgerManager();
    LedgerRepository ledgerRepo = ledgerManager.register(ledgerHash, STORAGE, LedgerDataStructure.MERKLE_TREE);
    // 验证参与方账户的存在;
    LedgerDataSet previousBlockDataset = ledgerRepo.getLedgerDataSet(ledgerRepo.getLatestBlock());
    UserAccount user0 = previousBlockDataset.getUserAccountSet().getAccount(parti0.getAddress());
    assertNotNull(user0);
    boolean partiRegistered = previousBlockDataset.getUserAccountSet().contains(parti0.getAddress());
    assertTrue(partiRegistered);
    // 生成新区块;
    LedgerEditor newBlockEditor = ledgerRepo.createNextBlock();
    OperationHandleRegisteration opReg = new DefaultOperationHandleRegisteration();
    LedgerSecurityManager securityManager = getSecurityManager();
    TransactionBatchProcessor txbatchProcessor = new TransactionBatchProcessor(securityManager, newBlockEditor, ledgerRepo, opReg);
    // 注册新用户;
    BlockchainKeypair userKeypair1 = BlockchainKeyGenerator.getInstance().generate();
    TransactionRequest transactionRequest1 = LedgerTestUtils.createTxRequest_UserReg(userKeypair1, ledgerHash, parti0, parti0);
    TransactionResponse txResp1 = txbatchProcessor.schedule(transactionRequest1);
    BlockchainKeypair userKeypair2 = BlockchainKeyGenerator.getInstance().generate();
    TransactionRequest transactionRequest2 = LedgerTestUtils.createTxRequest_UserReg(userKeypair2, ledgerHash, parti0, parti0);
    TransactionResponse txResp2 = txbatchProcessor.schedule(transactionRequest2);
    LedgerBlock newBlock = newBlockEditor.prepare();
    newBlockEditor.commit();
    assertEquals(TransactionState.SUCCESS, txResp1.getExecutionState());
    assertEquals(TransactionState.SUCCESS, txResp2.getExecutionState());
    // 验证正确性;
    ledgerManager = new LedgerManager();
    ledgerRepo = ledgerManager.register(ledgerHash, STORAGE, LedgerDataStructure.MERKLE_TREE);
    LedgerBlock latestBlock = ledgerRepo.getLatestBlock();
    assertEquals(newBlock.getHash(), latestBlock.getHash());
    assertEquals(1, newBlock.getHeight());
    LedgerDataSet ledgerDS = ledgerRepo.getLedgerDataSet(latestBlock);
    boolean existUser1 = ledgerDS.getUserAccountSet().contains(userKeypair1.getAddress());
    boolean existUser2 = ledgerDS.getUserAccountSet().contains(userKeypair2.getAddress());
    assertTrue(existUser1);
    assertTrue(existUser2);
}
Also used : LedgerManager(com.jd.blockchain.ledger.core.LedgerManager) DefaultOperationHandleRegisteration(com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration) LedgerSecurityManager(com.jd.blockchain.ledger.core.LedgerSecurityManager) LedgerBlock(com.jd.blockchain.ledger.LedgerBlock) TransactionBatchProcessor(com.jd.blockchain.ledger.core.TransactionBatchProcessor) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) LedgerRepository(com.jd.blockchain.ledger.core.LedgerRepository) LedgerDataSet(com.jd.blockchain.ledger.core.LedgerDataSet) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest) TransactionResponse(com.jd.blockchain.ledger.TransactionResponse) HashDigest(com.jd.blockchain.crypto.HashDigest) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) UserAccount(com.jd.blockchain.ledger.core.UserAccount) OperationHandleRegisteration(com.jd.blockchain.ledger.core.OperationHandleRegisteration) DefaultOperationHandleRegisteration(com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration) Test(org.junit.Test)

Example 20 with MemoryKVStorage

use of com.jd.blockchain.storage.service.utils.MemoryKVStorage in project jdchain-core by blockchain-jd-com.

the class TransactionBatchProcessorTest method testTxRollback.

@Test
public void testTxRollback() {
    System.out.println("------------ keys -----------");
    System.out.printf("Parti0 -- PUB:[%s]; PRIV:[%s]\r\n", parti0.getPubKey().toBase58(), parti0.getPrivKey().toBase58());
    System.out.printf("Parti1 -- PUB:[%s]; PRIV:[%s]\r\n", parti1.getPubKey().toBase58(), parti1.getPrivKey().toBase58());
    System.out.printf("Parti2 -- PUB:[%s]; PRIV:[%s]\r\n", parti2.getPubKey().toBase58(), parti2.getPrivKey().toBase58());
    System.out.printf("Parti3 -- PUB:[%s]; PRIV:[%s]\r\n", parti3.getPubKey().toBase58(), parti3.getPrivKey().toBase58());
    System.out.println("------------ end-keys -----------");
    final MemoryKVStorage STORAGE = new MemoryKVStorage();
    // 初始化账本到指定的存储库;
    HashDigest ledgerHash = LedgerTestUtils.initLedger(STORAGE, parti0, parti1, parti2, parti3);
    System.out.printf("\r\n------------ LEDGER [%s] -----------\r\n", ledgerHash.toBase58());
    // 加载账本;
    LedgerManager ledgerManager = new LedgerManager();
    LedgerRepository ledgerRepo = ledgerManager.register(ledgerHash, STORAGE, LedgerDataStructure.MERKLE_TREE);
    CryptoSetting cryptoSetting = ledgerRepo.getAdminSettings().getSettings().getCryptoSetting();
    // 验证参与方账户的存在;
    LedgerDataSet previousBlockDataset = ledgerRepo.getLedgerDataSet(ledgerRepo.getLatestBlock());
    UserAccount user0 = previousBlockDataset.getUserAccountSet().getAccount(parti0.getAddress());
    assertNotNull(user0);
    boolean partiRegistered = previousBlockDataset.getUserAccountSet().contains(parti0.getAddress());
    assertTrue(partiRegistered);
    // 生成新区块;
    LedgerEditor newBlockEditor = ledgerRepo.createNextBlock();
    OperationHandleRegisteration opReg = new DefaultOperationHandleRegisteration();
    LedgerSecurityManager securityManager = getSecurityManager();
    TransactionBatchProcessor txbatchProcessor = new TransactionBatchProcessor(securityManager, newBlockEditor, ledgerRepo, opReg);
    // 注册新用户;
    // BlockchainKeypair userKeypair1 = BlockchainKeyGenerator.getInstance().generate();
    BlockchainKeypair userKeypair1 = LedgerTestUtils.createKeyPair("7VeRKf3GFLFcBfzvtzmtyMXEoX2HYGEJ4j7CmHcnRV99W5Dp", "7VeRYQjeAaQY5Po8MMtmGNHA2SniqLXmJaZwBS5K8zTtMAU1");
    TransactionRequest transactionRequest1 = LedgerTestUtils.createTxRequest_UserReg_SHA256(userKeypair1, ledgerHash, 1580315317127L, parti0, parti0);
    // 错误参数:ts=1580315317127; txhash=j5wPGKT5CUzwi8j6VfCWaP2p9YZ6WVWtMANp9HbHWzvhgG
    System.out.printf("\r\n ===||=== transactionRequest1.getTransactionHash()=[%s]\r\n", transactionRequest1.getTransactionHash().toBase58());
    TransactionResponse txResp1 = txbatchProcessor.schedule(transactionRequest1);
    // BlockchainKeypair userKeypair2 = BlockchainKeyGenerator.getInstance().generate();
    BlockchainKeypair userKeypair2 = LedgerTestUtils.createKeyPair("7VeRKSnDFveTfLLMsLZDmmhGmgf7i142XHgBFjnrKuS95tY3", "7VeRTiJ2TpQD9aBi29ajnqdntgoVBANmC3oCbHThKb5tzfTJ");
    TransactionRequest transactionRequest2 = LedgerTestUtils.createTxRequest_MultiOPs_WithNotExistedDataAccount_SHA256(userKeypair2, ledgerHash, 202001202020L, parti0, parti0);
    System.out.printf("\r\n ===||=== transactionRequest2.getTransactionHash()=[%s]\r\n", transactionRequest2.getTransactionHash().toBase58());
    TransactionResponse txResp2 = txbatchProcessor.schedule(transactionRequest2);
    // BlockchainKeypair userKeypair3 = BlockchainKeyGenerator.getInstance().generate();
    BlockchainKeypair userKeypair3 = LedgerTestUtils.createKeyPair("7VeRDoaSexqLWKkaZyrQwdwSuE9n5nszduMrYBfYRfEkREQV", "7VeRdFtTuLfrzCYJzQ6enQUkGTc83ATgjr8WbmfjBQuTFpHt");
    TransactionRequest transactionRequest3 = LedgerTestUtils.createTxRequest_UserReg_SHA256(userKeypair3, ledgerHash, 202001202020L, parti0, parti0);
    System.out.printf("\r\n ===||=== transactionRequest3.getTransactionHash()=[%s]\r\n", transactionRequest3.getTransactionHash().toBase58());
    TransactionResponse txResp3 = txbatchProcessor.schedule(transactionRequest3);
    LedgerBlock newBlock = newBlockEditor.prepare();
    newBlockEditor.commit();
    // 在重新加载之前验证一次; 
    long blockHeight = newBlock.getHeight();
    assertEquals(1, blockHeight);
    HashDigest blockHash = newBlock.getHash();
    assertNotNull(blockHash);
    assertEquals(TransactionState.SUCCESS, txResp1.getExecutionState());
    assertEquals(TransactionState.DATA_ACCOUNT_DOES_NOT_EXIST, txResp2.getExecutionState());
    assertEquals(TransactionState.SUCCESS, txResp3.getExecutionState());
    LedgerTransaction tx1 = ledgerRepo.getTransactionSet().getTransaction(transactionRequest1.getTransactionHash());
    LedgerTransaction tx2 = ledgerRepo.getTransactionSet().getTransaction(transactionRequest2.getTransactionHash());
    LedgerTransaction tx3 = ledgerRepo.getTransactionSet().getTransaction(transactionRequest3.getTransactionHash());
    assertNotNull(tx3);
    assertEquals(TransactionState.SUCCESS, tx3.getResult().getExecutionState());
    assertNotNull(tx2);
    assertEquals(TransactionState.DATA_ACCOUNT_DOES_NOT_EXIST, tx2.getResult().getExecutionState());
    assertNotNull(tx1);
    assertEquals(TransactionState.SUCCESS, tx1.getResult().getExecutionState());
    HashDigest txsetRootHash = ledgerRepo.getTransactionSet().getRootHash();
    // 单独加载交易集合;
    TransactionSetEditor txset = new TransactionSetEditor(-1, txsetRootHash, cryptoSetting, "LDG://3A3dP4", STORAGE, STORAGE, LedgerDataStructure.MERKLE_TREE, false);
    tx1 = txset.getTransaction(transactionRequest1.getTransactionHash());
    // tx2 = txset.get(transactionRequest2.getTransactionHash());
    tx3 = txset.getTransaction(transactionRequest3.getTransactionHash());
    assertNotNull(tx3);
    // assertNotNull(tx2);
    assertNotNull(tx1);
    // 重新加载之后验证正确性;
    ledgerManager = new LedgerManager();
    ledgerRepo = ledgerManager.register(ledgerHash, STORAGE, LedgerDataStructure.MERKLE_TREE);
    LedgerBlock latestBlock = ledgerRepo.getLatestBlock();
    assertEquals(blockHash, latestBlock.getHash());
    assertEquals(blockHeight, latestBlock.getHeight());
    assertEquals(txsetRootHash, ledgerRepo.getTransactionSet().getRootHash());
    tx1 = ledgerRepo.getTransactionSet().getTransaction(transactionRequest1.getTransactionHash());
    tx2 = ledgerRepo.getTransactionSet().getTransaction(transactionRequest2.getTransactionHash());
    tx3 = ledgerRepo.getTransactionSet().getTransaction(transactionRequest3.getTransactionHash());
    assertNotNull(tx1);
    assertEquals(TransactionState.SUCCESS, tx1.getResult().getExecutionState());
    assertNotNull(tx2);
    assertEquals(TransactionState.DATA_ACCOUNT_DOES_NOT_EXIST, tx2.getResult().getExecutionState());
    assertNotNull(tx3);
    assertEquals(TransactionState.SUCCESS, tx3.getResult().getExecutionState());
    LedgerDataSet ledgerDS = ledgerRepo.getLedgerDataSet(latestBlock);
    boolean existUser1 = ledgerDS.getUserAccountSet().contains(userKeypair1.getAddress());
    boolean existUser2 = ledgerDS.getUserAccountSet().contains(userKeypair2.getAddress());
    boolean existUser3 = ledgerDS.getUserAccountSet().contains(userKeypair3.getAddress());
    assertTrue(existUser1);
    assertFalse(existUser2);
    assertTrue(existUser3);
}
Also used : LedgerManager(com.jd.blockchain.ledger.core.LedgerManager) DefaultOperationHandleRegisteration(com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration) LedgerSecurityManager(com.jd.blockchain.ledger.core.LedgerSecurityManager) LedgerBlock(com.jd.blockchain.ledger.LedgerBlock) TransactionBatchProcessor(com.jd.blockchain.ledger.core.TransactionBatchProcessor) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) LedgerRepository(com.jd.blockchain.ledger.core.LedgerRepository) LedgerDataSet(com.jd.blockchain.ledger.core.LedgerDataSet) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) TransactionResponse(com.jd.blockchain.ledger.TransactionResponse) HashDigest(com.jd.blockchain.crypto.HashDigest) LedgerTransaction(com.jd.blockchain.ledger.LedgerTransaction) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) TransactionSetEditor(com.jd.blockchain.ledger.core.TransactionSetEditor) UserAccount(com.jd.blockchain.ledger.core.UserAccount) OperationHandleRegisteration(com.jd.blockchain.ledger.core.OperationHandleRegisteration) DefaultOperationHandleRegisteration(com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration) Test(org.junit.Test)

Aggregations

MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)61 Test (org.junit.Test)50 HashDigest (com.jd.blockchain.crypto.HashDigest)39 CryptoSetting (com.jd.blockchain.ledger.CryptoSetting)23 TreeOptions (com.jd.blockchain.ledger.merkletree.TreeOptions)21 VersioningKVData (com.jd.blockchain.storage.service.utils.VersioningKVData)18 KVEntry (com.jd.blockchain.ledger.merkletree.KVEntry)17 MerkleHashTrie (com.jd.blockchain.ledger.proof.MerkleHashTrie)15 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)14 Bytes (utils.Bytes)13 TransactionRequest (com.jd.blockchain.ledger.TransactionRequest)9 CryptoConfig (com.jd.blockchain.ledger.core.CryptoConfig)9 Random (java.util.Random)9 LedgerEditor (com.jd.blockchain.ledger.core.LedgerEditor)8 LedgerManager (com.jd.blockchain.ledger.core.LedgerManager)8 LedgerRepository (com.jd.blockchain.ledger.core.LedgerRepository)8 ArrayList (java.util.ArrayList)8 LedgerBlock (com.jd.blockchain.ledger.LedgerBlock)7 MerkleHashSortTree (com.jd.blockchain.ledger.merkletree.MerkleHashSortTree)7 CryptoProvider (com.jd.blockchain.crypto.CryptoProvider)6