Search in sources :

Example 1 with TxTestContractImpl

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

the class ContractInvokingTest method testReadNewWritting.

// @Test
public void testReadNewWritting() {
    // 初始化账本到指定的存储库;
    HashDigest ledgerHash = initLedger(storage, parti0, parti1, parti2, parti3);
    // 重新加载账本;
    LedgerManager ledgerManager = new LedgerManager();
    LedgerRepository ledgerRepo = ledgerManager.register(ledgerHash, storage, LedgerDataStructure.MERKLE_TREE);
    // 创建合约处理器;
    ContractInvokingHandle contractInvokingHandle = new ContractInvokingHandle();
    // 创建和加载合约实例;
    BlockchainKeypair contractKey = BlockchainKeyGenerator.getInstance().generate();
    Bytes contractAddress = contractKey.getAddress();
    TxTestContractImpl contractInstance = new TxTestContractImpl();
    contractInvokingHandle.setup(contractAddress, TxTestContract.class, contractInstance);
    // 注册合约处理器;
    DefaultOperationHandleRegisteration opReg = new DefaultOperationHandleRegisteration();
    opReg.registerHandle(contractInvokingHandle);
    // 发布指定地址合约
    deploy(ledgerRepo, ledgerManager, opReg, ledgerHash, contractKey);
    // 创建新区块的交易处理器;
    LedgerBlock preBlock = ledgerRepo.getLatestBlock();
    LedgerDataSet previousBlockDataset = ledgerRepo.getLedgerDataSet(preBlock);
    // 加载合约
    LedgerEditor newBlockEditor = ledgerRepo.createNextBlock();
    TransactionBatchProcessor txbatchProcessor = new TransactionBatchProcessor(getSecurityManager(), newBlockEditor, ledgerRepo, opReg);
    String key = TxTestContractImpl.KEY;
    String value = "VAL";
    CryptoSetting cryptoSetting = ledgerRepo.getAdminInfo().getSettings().getCryptoSetting();
    TxBuilder txBuilder = new TxBuilder(ledgerHash, cryptoSetting.getHashAlgorithm());
    BlockchainKeypair kpDataAccount = BlockchainKeyGenerator.getInstance().generate();
    contractInstance.setDataAddress(kpDataAccount.getAddress());
    txBuilder.dataAccounts().register(kpDataAccount.getIdentity());
    TransactionRequestBuilder txReqBuilder1 = txBuilder.prepareRequest();
    txReqBuilder1.signAsEndpoint(parti0);
    txReqBuilder1.signAsNode(parti0);
    TransactionRequest txReq1 = txReqBuilder1.buildRequest();
    // 构建基于接口调用合约的交易请求,用于测试合约调用;
    txBuilder = new TxBuilder(ledgerHash, cryptoSetting.getHashAlgorithm());
    TxTestContract contractProxy = txBuilder.contract(contractAddress, TxTestContract.class);
    BooleanValueHolder readableHolder = decode(contractProxy.testReadable());
    TransactionRequestBuilder txReqBuilder2 = txBuilder.prepareRequest();
    txReqBuilder2.signAsEndpoint(parti0);
    txReqBuilder2.signAsNode(parti0);
    TransactionRequest txReq2 = txReqBuilder2.buildRequest();
    TransactionResponse resp1 = txbatchProcessor.schedule(txReq1);
    TransactionResponse resp2 = txbatchProcessor.schedule(txReq2);
    // 提交区块;
    TransactionBatchResultHandle txResultHandle = txbatchProcessor.prepare();
    txResultHandle.commit();
    BytesValue latestValue = ledgerRepo.getDataAccountSet().getAccount(kpDataAccount.getAddress()).getDataset().getValue(key, -1);
    System.out.printf("latest value=[%s] %s \r\n", latestValue.getType(), latestValue.getBytes().toUTF8String());
    boolean readable = readableHolder.get();
    assertTrue(readable);
    LedgerBlock latestBlock = ledgerRepo.getLatestBlock();
    assertEquals(preBlock.getHeight() + 1, latestBlock.getHeight());
    assertEquals(resp1.getBlockHeight(), latestBlock.getHeight());
    assertEquals(resp1.getBlockHash(), latestBlock.getHash());
}
Also used : LedgerManager(com.jd.blockchain.ledger.core.LedgerManager) DefaultOperationHandleRegisteration(com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration) TransactionBatchProcessor(com.jd.blockchain.ledger.core.TransactionBatchProcessor) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) TxBuilder(com.jd.blockchain.transaction.TxBuilder) Matchers.anyString(org.mockito.Matchers.anyString) LedgerRepository(com.jd.blockchain.ledger.core.LedgerRepository) LedgerDataSet(com.jd.blockchain.ledger.core.LedgerDataSet) TxTestContract(test.com.jd.blockchain.ledger.TxTestContract) Bytes(utils.Bytes) HashDigest(com.jd.blockchain.crypto.HashDigest) TxTestContractImpl(test.com.jd.blockchain.ledger.TxTestContractImpl) BooleanValueHolder(com.jd.blockchain.transaction.BooleanValueHolder) TransactionBatchResultHandle(com.jd.blockchain.service.TransactionBatchResultHandle)

Example 2 with TxTestContractImpl

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

the class ContractInvokingTest method testRollbackWhileVersionConfliction.

/**
 * 验证在合约方法中写入数据账户时,如果版本校验失败是否会引发异常而导致回滚;<br>
 * 期待正确的表现是引发异常而回滚当前交易;
 */
@Ignore("需要更新合约")
@Test
public void testRollbackWhileVersionConfliction() {
    // 初始化账本到指定的存储库;
    HashDigest ledgerHash = initLedger(storage, parti0, parti1, parti2, parti3);
    // 重新加载账本;
    LedgerManager ledgerManager = new LedgerManager();
    LedgerRepository ledgerRepo = ledgerManager.register(ledgerHash, storage, LedgerDataStructure.MERKLE_TREE);
    // 创建合约处理器;
    ContractInvokingHandle contractInvokingHandle = new ContractInvokingHandle();
    // 创建和加载合约实例;
    BlockchainKeypair contractKey = BlockchainKeyGenerator.getInstance().generate();
    Bytes contractAddress = contractKey.getAddress();
    TxTestContractImpl contractInstance = new TxTestContractImpl();
    contractInvokingHandle.setup(contractAddress, TxTestContract.class, contractInstance);
    // 注册合约处理器;
    DefaultOperationHandleRegisteration opReg = new DefaultOperationHandleRegisteration();
    opReg.registerHandle(contractInvokingHandle);
    // 发布指定地址合约
    deploy(ledgerRepo, ledgerManager, opReg, ledgerHash, contractKey);
    // 注册数据账户;
    BlockchainKeypair kpDataAccount = BlockchainKeyGenerator.getInstance().generate();
    contractInstance.setDataAddress(kpDataAccount.getAddress());
    registerDataAccount(ledgerRepo, ledgerManager, opReg, ledgerHash, kpDataAccount);
    // 调用合约
    // 构建基于接口调用合约的交易请求,用于测试合约调用;
    buildBlock(ledgerRepo, ledgerManager, opReg, new TxDefinitor() {

        @Override
        public void buildTx(TxBuilder txBuilder) {
            TxTestContract contractProxy = txBuilder.contract(contractAddress, TxTestContract.class);
            contractProxy.testRollbackWhileVersionConfliction(kpDataAccount.getAddress().toBase58(), "K1", "V1-0", -1);
            contractProxy.testRollbackWhileVersionConfliction(kpDataAccount.getAddress().toBase58(), "K2", "V2-0", -1);
        }
    });
    // 预期数据都能够正常写入;
    DataEntry<String, TypedValue> kv1 = ledgerRepo.getDataAccountSet().getAccount(kpDataAccount.getAddress()).getDataset().getDataEntry("K1", 0);
    DataEntry<String, TypedValue> kv2 = ledgerRepo.getDataAccountSet().getAccount(kpDataAccount.getAddress()).getDataset().getDataEntry("K2", 0);
    assertEquals(0, kv1.getVersion());
    assertEquals(0, kv2.getVersion());
    assertEquals("V1-0", kv1.getValue().stringValue());
    assertEquals("V2-0", kv2.getValue().stringValue());
    // 构建基于接口调用合约的交易请求,用于测试合约调用;
    buildBlock(ledgerRepo, ledgerManager, opReg, new TxDefinitor() {

        @Override
        public void buildTx(TxBuilder txBuilder) {
            TxTestContract contractProxy = txBuilder.contract(contractAddress, TxTestContract.class);
            contractProxy.testRollbackWhileVersionConfliction(kpDataAccount.getAddress().toBase58(), "K1", "V1-1", 0);
            contractProxy.testRollbackWhileVersionConfliction(kpDataAccount.getAddress().toBase58(), "K2", "V2-1", 0);
        }
    });
    // 预期数据都能够正常写入;
    kv1 = ledgerRepo.getDataAccountSet().getAccount(kpDataAccount.getAddress()).getDataset().getDataEntry("K1", 1);
    kv2 = ledgerRepo.getDataAccountSet().getAccount(kpDataAccount.getAddress()).getDataset().getDataEntry("K2", 1);
    assertEquals(1, kv1.getVersion());
    assertEquals(1, kv2.getVersion());
    assertEquals("V1-1", kv1.getValue().stringValue());
    assertEquals("V2-1", kv2.getValue().stringValue());
    // 构建基于接口调用合约的交易请求,用于测试合约调用;
    buildBlock(ledgerRepo, ledgerManager, opReg, new TxDefinitor() {

        @Override
        public void buildTx(TxBuilder txBuilder) {
            TxTestContract contractProxy = txBuilder.contract(contractAddress, TxTestContract.class);
            contractProxy.testRollbackWhileVersionConfliction(kpDataAccount.getAddress().toBase58(), "K1", "V1-2", 1);
            contractProxy.testRollbackWhileVersionConfliction(kpDataAccount.getAddress().toBase58(), "K2", "V2-2", // 预期会回滚;
            0);
        }
    });
    // 预期数据回滚,账本没有发生变更;
    kv1 = ledgerRepo.getDataAccountSet().getAccount(kpDataAccount.getAddress()).getDataset().getDataEntry("K1", 1);
    assertEquals(1, kv1.getVersion());
    assertEquals("V1-1", kv1.getValue().stringValue());
    kv1 = ledgerRepo.getDataAccountSet().getAccount(kpDataAccount.getAddress()).getDataset().getDataEntry("K1", 2);
    assertNull(kv1);
    kv2 = ledgerRepo.getDataAccountSet().getAccount(kpDataAccount.getAddress()).getDataset().getDataEntry("K2", 1);
    assertEquals(1, kv2.getVersion());
}
Also used : LedgerManager(com.jd.blockchain.ledger.core.LedgerManager) DefaultOperationHandleRegisteration(com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration) TxBuilder(com.jd.blockchain.transaction.TxBuilder) Matchers.anyString(org.mockito.Matchers.anyString) LedgerRepository(com.jd.blockchain.ledger.core.LedgerRepository) TxTestContract(test.com.jd.blockchain.ledger.TxTestContract) Bytes(utils.Bytes) HashDigest(com.jd.blockchain.crypto.HashDigest) TxTestContractImpl(test.com.jd.blockchain.ledger.TxTestContractImpl) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

HashDigest (com.jd.blockchain.crypto.HashDigest)2 DefaultOperationHandleRegisteration (com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration)2 LedgerManager (com.jd.blockchain.ledger.core.LedgerManager)2 LedgerRepository (com.jd.blockchain.ledger.core.LedgerRepository)2 TxBuilder (com.jd.blockchain.transaction.TxBuilder)2 Matchers.anyString (org.mockito.Matchers.anyString)2 TxTestContract (test.com.jd.blockchain.ledger.TxTestContract)2 TxTestContractImpl (test.com.jd.blockchain.ledger.TxTestContractImpl)2 Bytes (utils.Bytes)2 LedgerDataSet (com.jd.blockchain.ledger.core.LedgerDataSet)1 LedgerEditor (com.jd.blockchain.ledger.core.LedgerEditor)1 TransactionBatchProcessor (com.jd.blockchain.ledger.core.TransactionBatchProcessor)1 TransactionBatchResultHandle (com.jd.blockchain.service.TransactionBatchResultHandle)1 BooleanValueHolder (com.jd.blockchain.transaction.BooleanValueHolder)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1