Search in sources :

Example 16 with LedgerEditor

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

the class LedgerTestUtils method initLedger.

public static HashDigest initLedger(MemoryKVStorage storage, BlockchainKeypair... partiKeys) {
    // 创建初始化配置;
    LedgerInitSetting initSetting = LedgerTestUtils.createLedgerInitSetting(partiKeys);
    // 创建账本;
    LedgerEditor ldgEdt = LedgerTransactionalEditor.createEditor(initSetting, LEDGER_KEY_PREFIX, storage, storage, LedgerDataStructure.MERKLE_TREE);
    TransactionRequest genesisTxReq = LedgerTestUtils.createLedgerInitTxRequest_SHA256(partiKeys);
    LedgerTransactionContext genisisTxCtx = ldgEdt.newTransaction(genesisTxReq);
    LedgerDataSetEditor ldgDS = (LedgerDataSetEditor) genisisTxCtx.getDataset();
    for (int i = 0; i < partiKeys.length; i++) {
        UserAccount userAccount = ldgDS.getUserAccountSet().register(partiKeys[i].getAddress(), partiKeys[i].getPubKey());
        userAccount.setProperty("Name", "参与方-" + i, -1);
        userAccount.setProperty("Share", "" + (10 + i), -1);
    }
    TransactionResult tx = genisisTxCtx.commit(TransactionState.SUCCESS);
    assertEquals(genesisTxReq.getTransactionHash(), tx.getTransactionHash());
    assertEquals(0, tx.getBlockHeight());
    LedgerBlock block = ldgEdt.prepare();
    assertEquals(0, block.getHeight());
    assertNotNull(block.getHash());
    assertNull(block.getPreviousHash());
    // 创世区块的账本哈希为 null;
    assertNull(block.getLedgerHash());
    assertNotNull(block.getHash());
    // 提交数据,写入存储;
    ldgEdt.commit();
    HashDigest ledgerHash = block.getHash();
    return ledgerHash;
}
Also used : LedgerDataSetEditor(com.jd.blockchain.ledger.core.LedgerDataSetEditor) HashDigest(com.jd.blockchain.crypto.HashDigest) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) LedgerTransactionContext(com.jd.blockchain.ledger.core.LedgerTransactionContext) UserAccount(com.jd.blockchain.ledger.core.UserAccount)

Example 17 with LedgerEditor

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

the class BlockFullRollBackTest method testBlockFullkRollBack.

@Test
public void testBlockFullkRollBack() {
    final MemoryKVStorage STORAGE = new MemoryKVStorage();
    final MemoryKVStorage STORAGE_Mock = Mockito.spy(STORAGE);
    // 初始化账本到指定的存储库;
    ledgerHash = initLedger(STORAGE_Mock, parti0, parti1, parti2, parti3);
    System.out.println("---------- Ledger init OK !!! ----------");
    // 加载账本;
    LedgerManager ledgerManager = new LedgerManager();
    LedgerRepository ledgerRepo = ledgerManager.register(ledgerHash, STORAGE_Mock, LedgerDataStructure.MERKLE_TREE);
    // 构造存储错误,并产生区块回滚
    doThrow(BlockRollbackException.class).when(STORAGE_Mock).set(any(), any(), anyLong());
    LedgerEditor newBlockEditor = ledgerRepo.createNextBlock();
    OperationHandleRegisteration opReg = new DefaultOperationHandleRegisteration();
    LedgerSecurityManager securityManager = getSecurityManager();
    TransactionBatchProcessor txbatchProcessor = new TransactionBatchProcessor(securityManager, newBlockEditor, ledgerRepo, opReg);
    // 注册新用户;
    BlockchainKeypair userKeypair = BlockchainKeyGenerator.getInstance().generate();
    TransactionRequest transactionRequest = LedgerTestUtils.createTxRequest_UserReg(userKeypair, ledgerHash, parti0, parti0);
    TransactionResponse txResp = txbatchProcessor.schedule(transactionRequest);
    LedgerBlock newBlock = newBlockEditor.prepare();
    try {
        newBlockEditor.commit();
    } catch (BlockRollbackException e) {
        newBlockEditor.cancel();
    }
    // 验证正确性;
    ledgerManager = new LedgerManager();
    ledgerRepo = ledgerManager.register(ledgerHash, STORAGE_Mock, LedgerDataStructure.MERKLE_TREE);
    LedgerBlock latestBlock = ledgerRepo.getLatestBlock();
    assertEquals(ledgerRepo.getBlockHash(0), latestBlock.getHash());
    assertEquals(0, latestBlock.getHeight());
    LedgerDataSet ledgerDS = ledgerRepo.getLedgerDataSet(latestBlock);
    boolean existUser = ledgerDS.getUserAccountSet().contains(userKeypair.getAddress());
    assertFalse(existUser);
    doCallRealMethod().when(STORAGE_Mock).set(any(), any(), anyLong());
    // 区块正常提交
    // 生成新区块;
    LedgerEditor newBlockEditor1 = ledgerRepo.createNextBlock();
    OperationHandleRegisteration opReg1 = new DefaultOperationHandleRegisteration();
    LedgerSecurityManager securityManager1 = getSecurityManager();
    TransactionBatchProcessor txbatchProcessor1 = new TransactionBatchProcessor(securityManager1, newBlockEditor1, ledgerRepo, opReg1);
    // 注册新用户;
    BlockchainKeypair userKeypair1 = BlockchainKeyGenerator.getInstance().generate();
    TransactionRequest transactionRequest1 = LedgerTestUtils.createTxRequest_UserReg(userKeypair1, ledgerHash, parti0, parti0);
    TransactionResponse txResp1 = txbatchProcessor1.schedule(transactionRequest1);
    LedgerBlock newBlock1 = newBlockEditor1.prepare();
    try {
        newBlockEditor1.commit();
    } catch (BlockRollbackException e) {
        newBlockEditor1.cancel();
    }
    ledgerManager = new LedgerManager();
    ledgerRepo = ledgerManager.register(ledgerHash, STORAGE_Mock, LedgerDataStructure.MERKLE_TREE);
    LedgerBlock latestBlock1 = ledgerRepo.getLatestBlock();
    assertEquals(newBlock1.getHash(), latestBlock1.getHash());
    assertEquals(1, latestBlock1.getHeight());
    LedgerDataSet ledgerDS1 = ledgerRepo.getLedgerDataSet(latestBlock1);
    boolean existUser1 = ledgerDS1.getUserAccountSet().contains(userKeypair1.getAddress());
    assertTrue(existUser1);
}
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) BlockRollbackException(com.jd.blockchain.ledger.BlockRollbackException) LedgerDataSet(com.jd.blockchain.ledger.core.LedgerDataSet) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest) TransactionResponse(com.jd.blockchain.ledger.TransactionResponse) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) OperationHandleRegisteration(com.jd.blockchain.ledger.core.OperationHandleRegisteration) DefaultOperationHandleRegisteration(com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration) Test(org.junit.Test)

Aggregations

LedgerEditor (com.jd.blockchain.ledger.core.LedgerEditor)17 HashDigest (com.jd.blockchain.crypto.HashDigest)12 LedgerDataSet (com.jd.blockchain.ledger.core.LedgerDataSet)10 LedgerManager (com.jd.blockchain.ledger.core.LedgerManager)10 LedgerRepository (com.jd.blockchain.ledger.core.LedgerRepository)10 TransactionBatchProcessor (com.jd.blockchain.ledger.core.TransactionBatchProcessor)10 Test (org.junit.Test)10 LedgerBlock (com.jd.blockchain.ledger.LedgerBlock)9 UserAccount (com.jd.blockchain.ledger.core.UserAccount)9 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)8 TransactionRequest (com.jd.blockchain.ledger.TransactionRequest)8 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)8 DefaultOperationHandleRegisteration (com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration)7 LedgerSecurityManager (com.jd.blockchain.ledger.core.LedgerSecurityManager)7 LedgerTransactionContext (com.jd.blockchain.ledger.core.LedgerTransactionContext)7 LedgerDataSetEditor (com.jd.blockchain.ledger.core.LedgerDataSetEditor)6 TxBuilder (com.jd.blockchain.transaction.TxBuilder)6 TransactionResponse (com.jd.blockchain.ledger.TransactionResponse)5 OperationHandleRegisteration (com.jd.blockchain.ledger.core.OperationHandleRegisteration)5 TransactionBatchResultHandle (com.jd.blockchain.service.TransactionBatchResultHandle)5