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());
}
Aggregations