Search in sources :

Example 11 with LedgerSecurityManager

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

the class ContractInvokingTest method getSecurityManager.

private static LedgerSecurityManager getSecurityManager() {
    LedgerSecurityManager securityManager = Mockito.mock(LedgerSecurityManager.class);
    SecurityPolicy securityPolicy = Mockito.mock(SecurityPolicy.class);
    when(securityPolicy.isEndpointEnable(any(LedgerPermission.class), any())).thenReturn(true);
    when(securityPolicy.isEndpointEnable(any(TransactionPermission.class), any())).thenReturn(true);
    when(securityPolicy.isNodeEnable(any(LedgerPermission.class), any())).thenReturn(true);
    when(securityPolicy.isNodeEnable(any(TransactionPermission.class), any())).thenReturn(true);
    when(securityManager.getSecurityPolicy(any(), any())).thenReturn(securityPolicy);
    return securityManager;
}
Also used : LedgerSecurityManager(com.jd.blockchain.ledger.core.LedgerSecurityManager) SecurityPolicy(com.jd.blockchain.ledger.SecurityPolicy)

Example 12 with LedgerSecurityManager

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

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

LedgerSecurityManager (com.jd.blockchain.ledger.core.LedgerSecurityManager)13 LedgerDataSet (com.jd.blockchain.ledger.core.LedgerDataSet)9 LedgerEditor (com.jd.blockchain.ledger.core.LedgerEditor)7 TransactionBatchProcessor (com.jd.blockchain.ledger.core.TransactionBatchProcessor)7 Test (org.junit.Test)7 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)6 DefaultOperationHandleRegisteration (com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration)6 LedgerManager (com.jd.blockchain.ledger.core.LedgerManager)6 LedgerRepository (com.jd.blockchain.ledger.core.LedgerRepository)6 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)6 HashDigest (com.jd.blockchain.crypto.HashDigest)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 SecurityPolicy (com.jd.blockchain.ledger.SecurityPolicy)4 UserAccount (com.jd.blockchain.ledger.core.UserAccount)4 LedgerPermission (com.jd.blockchain.ledger.LedgerPermission)3 TransactionPermission (com.jd.blockchain.ledger.TransactionPermission)3 LedgerSecurityManagerImpl (com.jd.blockchain.ledger.core.LedgerSecurityManagerImpl)3