Search in sources :

Example 11 with LedgerManager

use of com.jd.blockchain.ledger.core.LedgerManager 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

LedgerManager (com.jd.blockchain.ledger.core.LedgerManager)11 LedgerRepository (com.jd.blockchain.ledger.core.LedgerRepository)11 HashDigest (com.jd.blockchain.crypto.HashDigest)10 LedgerEditor (com.jd.blockchain.ledger.core.LedgerEditor)10 Test (org.junit.Test)10 DefaultOperationHandleRegisteration (com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration)8 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)8 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)7 LedgerBlock (com.jd.blockchain.ledger.LedgerBlock)7 LedgerDataSet (com.jd.blockchain.ledger.core.LedgerDataSet)7 TransactionBatchProcessor (com.jd.blockchain.ledger.core.TransactionBatchProcessor)7 TransactionRequest (com.jd.blockchain.ledger.TransactionRequest)6 LedgerSecurityManager (com.jd.blockchain.ledger.core.LedgerSecurityManager)6 TransactionResponse (com.jd.blockchain.ledger.TransactionResponse)5 OperationHandleRegisteration (com.jd.blockchain.ledger.core.OperationHandleRegisteration)5 UserAccount (com.jd.blockchain.ledger.core.UserAccount)5 TxBuilder (com.jd.blockchain.transaction.TxBuilder)4 LedgerTransaction (com.jd.blockchain.ledger.LedgerTransaction)3 LedgerTransactionContext (com.jd.blockchain.ledger.core.LedgerTransactionContext)3 Matchers.anyString (org.mockito.Matchers.anyString)3