Search in sources :

Example 1 with BooleanValueHolder

use of com.jd.blockchain.transaction.BooleanValueHolder 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)

Aggregations

HashDigest (com.jd.blockchain.crypto.HashDigest)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 TransactionBatchProcessor (com.jd.blockchain.ledger.core.TransactionBatchProcessor)1 TransactionBatchResultHandle (com.jd.blockchain.service.TransactionBatchResultHandle)1 BooleanValueHolder (com.jd.blockchain.transaction.BooleanValueHolder)1 TxBuilder (com.jd.blockchain.transaction.TxBuilder)1 Matchers.anyString (org.mockito.Matchers.anyString)1 TxTestContract (test.com.jd.blockchain.ledger.TxTestContract)1 TxTestContractImpl (test.com.jd.blockchain.ledger.TxTestContractImpl)1 Bytes (utils.Bytes)1