Search in sources :

Example 11 with LedgerDataSet

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

Example 12 with LedgerDataSet

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

the class UncommittedLedgerQueryService method getUserPrivileges.

@Override
public UserPrivilegeSet getUserPrivileges(String userAddress) {
    LedgerDataSet ledgerDataQuery = transactionContext.getDataset();
    LedgerAdminDataSet previousAdminDataset = ledgerDataQuery.getAdminDataset();
    LedgerSecurityManager securityManager = new LedgerSecurityManagerImpl(previousAdminDataset.getAdminSettings().getRolePrivileges(), previousAdminDataset.getAdminSettings().getAuthorizations(), previousAdminDataset.getParticipantDataset(), ledgerDataQuery.getUserAccountSet());
    UserPrivilegeSet userPrivilegeSet = securityManager.getUserRolesPrivilegs(Bytes.fromBase58(userAddress));
    return userPrivilegeSet;
}
Also used : LedgerSecurityManager(com.jd.blockchain.ledger.core.LedgerSecurityManager) UserPrivilegeSet(com.jd.blockchain.ledger.UserPrivilegeSet) LedgerAdminDataSet(com.jd.blockchain.ledger.core.LedgerAdminDataSet) LedgerSecurityManagerImpl(com.jd.blockchain.ledger.core.LedgerSecurityManagerImpl) LedgerDataSet(com.jd.blockchain.ledger.core.LedgerDataSet)

Aggregations

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