Search in sources :

Example 1 with DataVersionConflictException

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

the class TransactionBatchProcessorTest method testTxRollbackByVersionsConflict.

@Test
public void testTxRollbackByVersionsConflict() {
    final MemoryKVStorage STORAGE = new MemoryKVStorage();
    // 初始化账本到指定的存储库;
    HashDigest ledgerHash = LedgerTestUtils.initLedger(STORAGE, parti0, parti1, parti2, parti3);
    // 加载账本;
    LedgerManager ledgerManager = new LedgerManager();
    LedgerRepository ledgerRepo = ledgerManager.register(ledgerHash, STORAGE, LedgerDataStructure.MERKLE_TREE);
    // 验证参与方账户的存在;
    LedgerDataSet previousBlockDataset = ledgerRepo.getLedgerDataSet(ledgerRepo.getLatestBlock());
    UserAccount user0 = previousBlockDataset.getUserAccountSet().getAccount(parti0.getAddress());
    assertNotNull(user0);
    boolean partiRegistered = previousBlockDataset.getUserAccountSet().contains(parti0.getAddress());
    assertTrue(partiRegistered);
    // 注册数据账户;
    // 生成新区块;
    LedgerEditor newBlockEditor = ledgerRepo.createNextBlock();
    OperationHandleRegisteration opReg = new DefaultOperationHandleRegisteration();
    LedgerSecurityManager securityManager = getSecurityManager();
    TransactionBatchProcessor txbatchProcessor = new TransactionBatchProcessor(securityManager, newBlockEditor, ledgerRepo, opReg);
    BlockchainKeypair dataAccountKeypair = BlockchainKeyGenerator.getInstance().generate();
    TransactionRequest transactionRequest1 = LedgerTestUtils.createTxRequest_DataAccountReg_SHA256(dataAccountKeypair, ledgerHash, parti0, parti0);
    TransactionResponse txResp1 = txbatchProcessor.schedule(transactionRequest1);
    LedgerBlock newBlock = newBlockEditor.prepare();
    newBlockEditor.commit();
    assertEquals(TransactionState.SUCCESS, txResp1.getExecutionState());
    DataAccount dataAccount = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress());
    assertNotNull(dataAccount);
    // 正确写入 KV 数据;
    TransactionRequest txreq1 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K1", "V-1-1", -1, ledgerHash, parti0, parti0);
    TransactionRequest txreq2 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K2", "V-2-1", -1, ledgerHash, parti0, parti0);
    TransactionRequest txreq3 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K3", "V-3-1", -1, ledgerHash, parti0, parti0);
    // 连续写 K1,K1的版本将变为1;
    TransactionRequest txreq4 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K1", "V-1-2", 0, ledgerHash, parti0, parti0);
    newBlockEditor = ledgerRepo.createNextBlock();
    previousBlockDataset = ledgerRepo.getLedgerDataSet(ledgerRepo.getLatestBlock());
    txbatchProcessor = new TransactionBatchProcessor(securityManager, newBlockEditor, ledgerRepo, opReg);
    txbatchProcessor.schedule(txreq1);
    txbatchProcessor.schedule(txreq2);
    txbatchProcessor.schedule(txreq3);
    txbatchProcessor.schedule(txreq4);
    newBlock = newBlockEditor.prepare();
    newBlockEditor.commit();
    BytesValue v1_0 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K1", 0);
    BytesValue v1_1 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K1", 1);
    BytesValue v2 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K2", 0);
    BytesValue v3 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K3", 0);
    assertNotNull(v1_0);
    assertNotNull(v1_1);
    assertNotNull(v2);
    assertNotNull(v3);
    assertEquals("V-1-1", v1_0.getBytes().toUTF8String());
    assertEquals("V-1-2", v1_1.getBytes().toUTF8String());
    assertEquals("V-2-1", v2.getBytes().toUTF8String());
    assertEquals("V-3-1", v3.getBytes().toUTF8String());
    // 提交多笔数据写入的交易,包含存在数据版本冲突的交易,验证交易是否正确回滚;
    // 先写一笔正确的交易; k3 的版本将变为 1 ;
    TransactionRequest txreq5 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K3", "V-3-2", 0, ledgerHash, parti0, parti0);
    // 指定冲突的版本号,正确的应该是版本1;
    TransactionRequest txreq6 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K1", "V-1-3", 0, ledgerHash, parti0, parti0);
    newBlockEditor = ledgerRepo.createNextBlock();
    previousBlockDataset = ledgerRepo.getLedgerDataSet(ledgerRepo.getLatestBlock());
    txbatchProcessor = new TransactionBatchProcessor(securityManager, newBlockEditor, ledgerRepo, opReg);
    txbatchProcessor.schedule(txreq5);
    // 预期会产生版本冲突异常; DataVersionConflictionException;
    DataVersionConflictException versionConflictionException = null;
    try {
        txbatchProcessor.schedule(txreq6);
    } catch (DataVersionConflictException e) {
        versionConflictionException = e;
    }
    // assertNotNull(versionConflictionException);
    newBlock = newBlockEditor.prepare();
    newBlockEditor.commit();
    BytesValue v1 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K1");
    v3 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K3");
    // k1 的版本仍然为1,没有更新;
    long k1_version = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getVersion("K1");
    assertEquals(1, k1_version);
    long k3_version = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getVersion("K3");
    assertEquals(1, k3_version);
    assertNotNull(v1);
    assertNotNull(v3);
    assertEquals("V-1-2", v1.getBytes().toUTF8String());
    assertEquals("V-3-2", v3.getBytes().toUTF8String());
// // 验证正确性;
// ledgerManager = new LedgerManager();
// ledgerRepo = ledgerManager.register(ledgerHash, STORAGE);
// 
// LedgerBlock latestBlock = ledgerRepo.getLatestBlock();
// assertEquals(newBlock.getHash(), latestBlock.getHash());
// assertEquals(1, newBlock.getHeight());
// 
// LedgerTransaction tx1 = ledgerRepo.getTransactionSet()
// .get(transactionRequest1.getTransactionHash());
// 
// assertNotNull(tx1);
// assertEquals(TransactionState.SUCCESS, tx1.getExecutionState());
}
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) DataVersionConflictException(com.jd.blockchain.ledger.DataVersionConflictException) BytesValue(com.jd.blockchain.ledger.BytesValue) LedgerRepository(com.jd.blockchain.ledger.core.LedgerRepository) LedgerDataSet(com.jd.blockchain.ledger.core.LedgerDataSet) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest) DataAccount(com.jd.blockchain.ledger.core.DataAccount) TransactionResponse(com.jd.blockchain.ledger.TransactionResponse) HashDigest(com.jd.blockchain.crypto.HashDigest) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) UserAccount(com.jd.blockchain.ledger.core.UserAccount) OperationHandleRegisteration(com.jd.blockchain.ledger.core.OperationHandleRegisteration) DefaultOperationHandleRegisteration(com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration) Test(org.junit.Test)

Example 2 with DataVersionConflictException

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

the class DataAccountKVSetOperationHandle method doProcess.

@Override
protected void doProcess(DataAccountKVSetOperation kvWriteOp, LedgerTransactionContext transactionContext, TransactionRequestExtension requestContext, LedgerQuery ledger, OperationHandleContext handleContext, EventManager manager) {
    // 权限校验;
    SecurityPolicy securityPolicy = SecurityContext.getContextUsersPolicy();
    securityPolicy.checkEndpointPermission(LedgerPermission.WRITE_DATA_ACCOUNT, MultiIDsPolicy.AT_LEAST_ONE);
    // 操作账本;
    DataAccount account = transactionContext.getDataset().getDataAccountSet().getAccount(kvWriteOp.getAccountAddress());
    if (account == null) {
        throw new DataAccountDoesNotExistException(String.format("Data account doesn't exist! --[Address=%s]", kvWriteOp.getAccountAddress()));
    }
    // 写权限校验
    securityPolicy.checkDataPermission(account.getPermission(), DataPermissionType.WRITE);
    KVWriteEntry[] writeSet = kvWriteOp.getWriteSet();
    long v = -1L;
    for (KVWriteEntry kvw : writeSet) {
        v = account.getDataset().setValue(kvw.getKey(), TypedValue.wrap(kvw.getValue()), kvw.getExpectedVersion());
        if (v < 0) {
            throw new DataVersionConflictException();
        }
    }
}
Also used : DataAccount(com.jd.blockchain.ledger.core.DataAccount) DataAccountDoesNotExistException(com.jd.blockchain.ledger.DataAccountDoesNotExistException) SecurityPolicy(com.jd.blockchain.ledger.SecurityPolicy) KVWriteEntry(com.jd.blockchain.ledger.DataAccountKVSetOperation.KVWriteEntry) DataVersionConflictException(com.jd.blockchain.ledger.DataVersionConflictException)

Aggregations

DataVersionConflictException (com.jd.blockchain.ledger.DataVersionConflictException)2 DataAccount (com.jd.blockchain.ledger.core.DataAccount)2 HashDigest (com.jd.blockchain.crypto.HashDigest)1 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)1 BytesValue (com.jd.blockchain.ledger.BytesValue)1 DataAccountDoesNotExistException (com.jd.blockchain.ledger.DataAccountDoesNotExistException)1 KVWriteEntry (com.jd.blockchain.ledger.DataAccountKVSetOperation.KVWriteEntry)1 LedgerBlock (com.jd.blockchain.ledger.LedgerBlock)1 SecurityPolicy (com.jd.blockchain.ledger.SecurityPolicy)1 TransactionRequest (com.jd.blockchain.ledger.TransactionRequest)1 TransactionResponse (com.jd.blockchain.ledger.TransactionResponse)1 DefaultOperationHandleRegisteration (com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration)1 LedgerDataSet (com.jd.blockchain.ledger.core.LedgerDataSet)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 LedgerSecurityManager (com.jd.blockchain.ledger.core.LedgerSecurityManager)1 OperationHandleRegisteration (com.jd.blockchain.ledger.core.OperationHandleRegisteration)1 TransactionBatchProcessor (com.jd.blockchain.ledger.core.TransactionBatchProcessor)1 UserAccount (com.jd.blockchain.ledger.core.UserAccount)1