Search in sources :

Example 1 with UserAccountSetEditor

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

the class UserStateUpdateOperationHandle method doProcess.

@Override
protected void doProcess(UserStateUpdateOperation op, LedgerTransactionContext transactionContext, TransactionRequestExtension requestContext, LedgerQuery ledger, OperationHandleContext handleContext, EventManager manager) {
    // 权限校验;
    SecurityPolicy securityPolicy = SecurityContext.getContextUsersPolicy();
    securityPolicy.checkEndpointPermission(LedgerPermission.UPDATE_USER_STATE, MultiIDsPolicy.AT_LEAST_ONE);
    UserAccount user = transactionContext.getDataset().getUserAccountSet().getAccount(op.getUserAddress());
    if (null == user) {
        throw new UserDoesNotExistException(String.format("User doesn't exist! --[Address=%s]", op.getUserAddress()));
    }
    // REVOKE 状态不可再恢复
    if (user.getState() == AccountState.REVOKE) {
        throw new IllegalAccountStateException(String.format("Can not change user in REVOKE state! --[Address=%s]", op.getUserAddress()));
    }
    // 操作账本;
    ((UserAccountSetEditor) (transactionContext.getDataset().getUserAccountSet())).setState(op.getUserAddress(), op.getState());
}
Also used : UserAccountSetEditor(com.jd.blockchain.ledger.core.UserAccountSetEditor) SecurityPolicy(com.jd.blockchain.ledger.SecurityPolicy) IllegalAccountStateException(com.jd.blockchain.ledger.IllegalAccountStateException) UserDoesNotExistException(com.jd.blockchain.ledger.UserDoesNotExistException) UserAccount(com.jd.blockchain.ledger.core.UserAccount)

Example 2 with UserAccountSetEditor

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

the class UserRegisterOperationHandle method doProcess.

@Override
protected void doProcess(UserRegisterOperation op, LedgerTransactionContext transactionContext, TransactionRequestExtension requestContext, LedgerQuery ledger, OperationHandleContext handleContext, EventManager manager) {
    // 权限校验;
    SecurityPolicy securityPolicy = SecurityContext.getContextUsersPolicy();
    securityPolicy.checkEndpointPermission(LedgerPermission.REGISTER_USER, MultiIDsPolicy.AT_LEAST_ONE);
    // 证书模式下必须传递证书
    if (transactionContext.getDataset().getAdminDataset().getAdminSettings().getMetadata().getIdentityMode() == IdentityMode.CA) {
        if (StringUtils.isEmpty(op.getCertificate())) {
            throw new IllegalTransactionException("User certificate is empty!");
        }
        X509Certificate cert = CertificateUtils.parseCertificate(op.getCertificate());
        CertificateUtils.checkCertificateRolesAny(cert, CertificateRole.PEER, CertificateRole.GW, CertificateRole.USER);
        CertificateUtils.checkValidity(cert);
        X509Certificate[] ledgerCAs = CertificateUtils.parseCertificates(transactionContext.getDataset().getAdminDataset().getAdminSettings().getMetadata().getLedgerCertificates());
        X509Certificate[] issuers = CertificateUtils.findIssuers(cert, ledgerCAs);
        Arrays.stream(issuers).forEach(issuer -> CertificateUtils.checkCACertificate(issuer));
        CertificateUtils.checkValidityAny(issuers);
    }
    // 操作账本;
    BlockchainIdentity bid = op.getUserID();
    Bytes userAddress = bid.getAddress();
    ((UserAccountSetEditor) (transactionContext.getDataset().getUserAccountSet())).register(userAddress, bid.getPubKey(), op.getCertificate());
}
Also used : Bytes(utils.Bytes) IllegalTransactionException(com.jd.blockchain.ledger.IllegalTransactionException) UserAccountSetEditor(com.jd.blockchain.ledger.core.UserAccountSetEditor) SecurityPolicy(com.jd.blockchain.ledger.SecurityPolicy) BlockchainIdentity(com.jd.blockchain.ledger.BlockchainIdentity) X509Certificate(java.security.cert.X509Certificate)

Example 3 with UserAccountSetEditor

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

the class LedgerEditorTest method testRollback.

@Test
public void testRollback() {
    BlockchainKeypair parti0 = LedgerTestUtils.createKeyPair("7VeRLBwqTAz8oRazEazeaEfqei46sk2FzvBgyHMUBJvrUEGT", "7VeRUm27GbrsX9HbQSZguChLp24HZYub6s5FJ7FjBht8BmbA");
    BlockchainKeypair parti1 = LedgerTestUtils.createKeyPair("7VeRNJasZp76ThmUkoAajJEduotS4JC6T9wzhz9TDPvjLCRk", "7VeRcBcPkTZ4hFwfcKRgFJWdDesHyysQWkKYC6xfPApbfvwQ");
    BlockchainKeypair parti2 = LedgerTestUtils.createKeyPair("7VeR7uSd7sqxkMp73936MoK7eUSmGPVrsmrwdekiR9fmvdYN", "7VeRUkgMXRegHHWhezv4LdJV6oQuSXo6Ezp2sjC2M5NTUWkz");
    BlockchainKeypair parti3 = LedgerTestUtils.createKeyPair("7VeR8X8fa9th42XSXvnuBLfR4v3dxjXq6jPfvF7nDPB2MTo1", "7VeRdreAev1E8ySsLWX7rRMArh5wHBTmZXKwNUuoVo7cBn6o");
    BlockchainKeypair[] participants = new BlockchainKeypair[] { parti0, parti1, parti2, parti3 };
    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 manager = new LedgerManager();
    LedgerRepository repo = manager.register(ledgerHash, STORAGE, LedgerDataStructure.MERKLE_TREE);
    LedgerBlock block = repo.getBlock(ledgerHash);
    assertNotNull(block);
    assertNotNull(block.getHash());
    assertEquals(0, block.getHeight());
    // 创建交易连续交易,验证中间的交易回滚是否影响前后的交易;
    BlockchainKeypair user1 = LedgerTestUtils.createKeyPair("7VeRKf3GFLFcBfzvtzmtyMXEoX2HYGEJ4j7CmHcnRV99W5Dp", "7VeRYQjeAaQY5Po8MMtmGNHA2SniqLXmJaZwBS5K8zTtMAU1");
    TransactionRequest req1 = LedgerTestUtils.createTxRequest_UserReg_SHA256(user1, ledgerHash, 1580315317127L, parti0, parti0);
    // 引发错误的参数:ts=1580315317127;
    // txhash=j5wPGKT5CUzwi8j6VfCWaP2p9YZ6WVWtMANp9HbHWzvhgG
    System.out.printf("\r\n ===||=== transactionRequest1.getTransactionContent().getHash()=[%s]\r\n", req1.getTransactionHash().toBase58());
    BlockchainKeypair user2 = LedgerTestUtils.createKeyPair("7VeRKSnDFveTfLLMsLZDmmhGmgf7i142XHgBFjnrKuS95tY3", "7VeRTiJ2TpQD9aBi29ajnqdntgoVBANmC3oCbHThKb5tzfTJ");
    TransactionRequest req2 = LedgerTestUtils.createTxRequest_MultiOPs_WithNotExistedDataAccount_SHA256(user2, ledgerHash, 202001202020L, parti0, parti0);
    System.out.printf("\r\n ===||=== transactionRequest2.getTransactionContent().getHash()=[%s]\r\n", req2.getTransactionHash().toBase58());
    BlockchainKeypair user3 = LedgerTestUtils.createKeyPair("7VeRDoaSexqLWKkaZyrQwdwSuE9n5nszduMrYBfYRfEkREQV", "7VeRdFtTuLfrzCYJzQ6enQUkGTc83ATgjr8WbmfjBQuTFpHt");
    TransactionRequest req3 = LedgerTestUtils.createTxRequest_UserReg_SHA256(user3, ledgerHash, 202001202020L, parti0, parti0);
    System.out.printf("\r\n ===||=== transactionRequest3.getTransactionContent().getHash()=[%s]\r\n", req3.getTransactionHash().toBase58());
    System.out.println("\r\n--------------- Start new Block 1 --------------\r\n");
    // 创建交易;
    LedgerEditor editor = repo.createNextBlock();
    System.out.println("\r\n--------------- Start new tx1 --------------\r\n");
    LedgerTransactionContext txctx1 = editor.newTransaction(req1);
    ((UserAccountSetEditor) (txctx1.getDataset().getUserAccountSet())).register(user1.getAddress(), user1.getPubKey());
    TransactionResult tx1 = txctx1.commit(TransactionState.SUCCESS);
    HashDigest txHash1 = tx1.getTransactionHash();
    System.out.println("\r\n--------------- Start new tx2 --------------\r\n");
    LedgerTransactionContext txctx2 = editor.newTransaction(req2);
    ((UserAccountSetEditor) (txctx2.getDataset().getUserAccountSet())).register(user2.getAddress(), user2.getPubKey());
    TransactionResult tx2 = txctx2.discardAndCommit(TransactionState.DATA_ACCOUNT_DOES_NOT_EXIST);
    HashDigest txHash2 = tx2.getTransactionHash();
    System.out.println("\r\n--------------- Start new tx3 --------------\r\n");
    LedgerTransactionContext txctx3 = editor.newTransaction(req3);
    ((UserAccountSetEditor) (txctx3.getDataset().getUserAccountSet())).register(user3.getAddress(), user3.getPubKey());
    TransactionResult tx3 = txctx3.commit(TransactionState.SUCCESS);
    HashDigest txHash3 = tx3.getTransactionHash();
    System.out.println("\r\n--------------- Start preparing new block 1 --------------\r\n");
    LedgerBlock block1 = editor.prepare();
    System.out.println("\r\n--------------- Start commiting new block 1 --------------\r\n");
    editor.commit();
    System.out.printf("\r\n--------------- End commiting new block 1 [Storage.Count=%s]--------------\r\n\r\n", STORAGE.getStorageCount());
    assertEquals(1, block1.getHeight());
    // 重新加载和验证;
    manager = new LedgerManager();
    repo = manager.register(ledgerHash, STORAGE, LedgerDataStructure.MERKLE_TREE);
    LedgerTransaction act_tx1 = repo.getTransactionSet().getTransaction(txHash1);
    LedgerTransaction act_tx2 = repo.getTransactionSet().getTransaction(txHash2);
    LedgerTransaction act_tx3 = repo.getTransactionSet().getTransaction(txHash3);
    assertNotNull(act_tx3);
    assertNotNull(act_tx2);
    assertNotNull(act_tx1);
}
Also used : LedgerManager(com.jd.blockchain.ledger.core.LedgerManager) LedgerBlock(com.jd.blockchain.ledger.LedgerBlock) TransactionResult(com.jd.blockchain.ledger.TransactionResult) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) LedgerTransactionContext(com.jd.blockchain.ledger.core.LedgerTransactionContext) LedgerRepository(com.jd.blockchain.ledger.core.LedgerRepository) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest) HashDigest(com.jd.blockchain.crypto.HashDigest) UserAccountSetEditor(com.jd.blockchain.ledger.core.UserAccountSetEditor) LedgerTransaction(com.jd.blockchain.ledger.LedgerTransaction) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) Test(org.junit.Test)

Example 4 with UserAccountSetEditor

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

the class UserCAUpdateOperationHandle method doProcess.

@Override
protected void doProcess(UserCAUpdateOperation op, LedgerTransactionContext transactionContext, TransactionRequestExtension requestContext, LedgerQuery ledger, OperationHandleContext handleContext, EventManager manager) {
    // 权限校验;
    SecurityPolicy securityPolicy = SecurityContext.getContextUsersPolicy();
    securityPolicy.checkEndpointPermission(LedgerPermission.UPDATE_USER_CA, MultiIDsPolicy.AT_LEAST_ONE);
    UserAccount user = transactionContext.getDataset().getUserAccountSet().getAccount(op.getUserAddress());
    if (null == user) {
        throw new UserDoesNotExistException(String.format("User doesn't exist! --[Address=%s]", op.getUserAddress()));
    }
    if (user.getState() == AccountState.REVOKE) {
        throw new IllegalAccountStateException(String.format("Can not change user in REVOKE state! --[Address=%s]", op.getUserAddress()));
    }
    // 证书校验
    X509Certificate cert = CertificateUtils.parseCertificate(op.getCertificate());
    CertificateUtils.checkCertificateRolesAny(cert, CertificateRole.PEER, CertificateRole.GW, CertificateRole.USER);
    CertificateUtils.checkValidity(cert);
    X509Certificate[] ledgerCAs = CertificateUtils.parseCertificates(transactionContext.getDataset().getAdminDataset().getAdminSettings().getMetadata().getLedgerCertificates());
    X509Certificate[] issuers = CertificateUtils.findIssuers(cert, ledgerCAs);
    Arrays.stream(issuers).forEach(issuer -> CertificateUtils.checkCACertificate(issuer));
    CertificateUtils.checkValidityAny(issuers);
    // 操作账本;
    ((UserAccountSetEditor) (transactionContext.getDataset().getUserAccountSet())).setCertificate(op.getUserAddress(), op.getCertificate());
}
Also used : UserAccountSetEditor(com.jd.blockchain.ledger.core.UserAccountSetEditor) SecurityPolicy(com.jd.blockchain.ledger.SecurityPolicy) IllegalAccountStateException(com.jd.blockchain.ledger.IllegalAccountStateException) UserDoesNotExistException(com.jd.blockchain.ledger.UserDoesNotExistException) UserAccount(com.jd.blockchain.ledger.core.UserAccount) X509Certificate(java.security.cert.X509Certificate)

Aggregations

UserAccountSetEditor (com.jd.blockchain.ledger.core.UserAccountSetEditor)4 SecurityPolicy (com.jd.blockchain.ledger.SecurityPolicy)3 IllegalAccountStateException (com.jd.blockchain.ledger.IllegalAccountStateException)2 UserDoesNotExistException (com.jd.blockchain.ledger.UserDoesNotExistException)2 UserAccount (com.jd.blockchain.ledger.core.UserAccount)2 X509Certificate (java.security.cert.X509Certificate)2 HashDigest (com.jd.blockchain.crypto.HashDigest)1 BlockchainIdentity (com.jd.blockchain.ledger.BlockchainIdentity)1 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)1 IllegalTransactionException (com.jd.blockchain.ledger.IllegalTransactionException)1 LedgerBlock (com.jd.blockchain.ledger.LedgerBlock)1 LedgerTransaction (com.jd.blockchain.ledger.LedgerTransaction)1 TransactionRequest (com.jd.blockchain.ledger.TransactionRequest)1 TransactionResult (com.jd.blockchain.ledger.TransactionResult)1 LedgerEditor (com.jd.blockchain.ledger.core.LedgerEditor)1 LedgerManager (com.jd.blockchain.ledger.core.LedgerManager)1 LedgerRepository (com.jd.blockchain.ledger.core.LedgerRepository)1 LedgerTransactionContext (com.jd.blockchain.ledger.core.LedgerTransactionContext)1 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)1 Test (org.junit.Test)1