Browse Source

Fixed Test case !

tags/1.0.0
shaozhuguang 6 years ago
parent
commit
772eb27dd2
1 changed files with 44 additions and 4 deletions
  1. +44
    -4
      source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/ContractInvokingTest.java

+ 44
- 4
source/ledger/ledger-core/src/test/java/test/com/jd/blockchain/ledger/ContractInvokingTest.java View File

@@ -92,21 +92,26 @@ public class ContractInvokingTest {
DefaultOperationHandleRegisteration opReg = new DefaultOperationHandleRegisteration(); DefaultOperationHandleRegisteration opReg = new DefaultOperationHandleRegisteration();
opReg.insertAsTopPriority(contractInvokingHandle); opReg.insertAsTopPriority(contractInvokingHandle);


// 发布指定地址合约
deploy(ledgerRepo, ledgerManager, opReg, ledgerHash, contractKey);


// 创建新区块的交易处理器; // 创建新区块的交易处理器;
LedgerBlock preBlock = ledgerRepo.getLatestBlock(); LedgerBlock preBlock = ledgerRepo.getLatestBlock();
LedgerDataSet previousBlockDataset = ledgerRepo.getDataSet(ledgerRepo.getLatestBlock());
LedgerDataSet previousBlockDataset = ledgerRepo.getDataSet(preBlock);

// 加载合约
LedgerEditor newBlockEditor = ledgerRepo.createNextBlock(); LedgerEditor newBlockEditor = ledgerRepo.createNextBlock();
TransactionBatchProcessor txbatchProcessor = new TransactionBatchProcessor(newBlockEditor, previousBlockDataset, TransactionBatchProcessor txbatchProcessor = new TransactionBatchProcessor(newBlockEditor, previousBlockDataset,
opReg, ledgerManager); opReg, ledgerManager);


// 构建基于接口调用合约的交易请求,用于测试合约调用; // 构建基于接口调用合约的交易请求,用于测试合约调用;
Random rand = new Random();
TxBuilder txBuilder = new TxBuilder(ledgerHash); TxBuilder txBuilder = new TxBuilder(ledgerHash);
TestContract contractProxy = txBuilder.contract(contractAddress, TestContract.class); TestContract contractProxy = txBuilder.contract(contractAddress, TestContract.class);
TestContract contractProxy1 = txBuilder.contract(contractAddress, TestContract.class); TestContract contractProxy1 = txBuilder.contract(contractAddress, TestContract.class);


String asset = "AK"; String asset = "AK";
long issueAmount = rand.nextLong();
long issueAmount = new Random().nextLong();
when(contractInstance.issue(anyString(), anyLong())).thenReturn(issueAmount); when(contractInstance.issue(anyString(), anyLong())).thenReturn(issueAmount);
contractProxy.issue(asset, issueAmount); contractProxy.issue(asset, issueAmount);


@@ -137,12 +142,41 @@ public class ContractInvokingTest {
// 再验证一次结果; // 再验证一次结果;
assertEquals(1, opResults.length); assertEquals(1, opResults.length);
assertEquals(0, opResults[0].getIndex()); assertEquals(0, opResults[0].getIndex());
reallyRetnBytes = BinaryProtocol.encode(opResults[0].getResult(), BytesValue.class); reallyRetnBytes = BinaryProtocol.encode(opResults[0].getResult(), BytesValue.class);
assertArrayEquals(expectedRetnBytes, reallyRetnBytes); assertArrayEquals(expectedRetnBytes, reallyRetnBytes);


} }


private void deploy(LedgerRepository ledgerRepo, LedgerManager ledgerManager,
DefaultOperationHandleRegisteration opReg, HashDigest ledgerHash,
BlockchainKeypair contractKey) {
// 创建新区块的交易处理器;
LedgerBlock preBlock = ledgerRepo.getLatestBlock();
LedgerDataSet previousBlockDataset = ledgerRepo.getDataSet(preBlock);

// 加载合约
LedgerEditor newBlockEditor = ledgerRepo.createNextBlock();
TransactionBatchProcessor txbatchProcessor = new TransactionBatchProcessor(newBlockEditor, previousBlockDataset,
opReg, ledgerManager);

// 构建基于接口调用合约的交易请求,用于测试合约调用;
TxBuilder txBuilder = new TxBuilder(ledgerHash);
txBuilder.contracts().deploy(contractKey.getIdentity(), chainCode());
TransactionRequestBuilder txReqBuilder = txBuilder.prepareRequest();
txReqBuilder.signAsEndpoint(parti0);
txReqBuilder.signAsNode(parti0);
TransactionRequest txReq = txReqBuilder.buildRequest();

TransactionResponse resp = txbatchProcessor.schedule(txReq);
OperationResult[] opResults = resp.getOperationResults();
assertNull(opResults);

// 提交区块;
TransactionBatchResultHandle txResultHandle = txbatchProcessor.prepare();
txResultHandle.commit();
}

private HashDigest initLedger(MemoryKVStorage storage, BlockchainKeypair... partiKeys) { private HashDigest initLedger(MemoryKVStorage storage, BlockchainKeypair... partiKeys) {
// 创建初始化配置; // 创建初始化配置;
LedgerInitSetting initSetting = LedgerTestUtils.createLedgerInitSetting(partiKeys); LedgerInitSetting initSetting = LedgerTestUtils.createLedgerInitSetting(partiKeys);
@@ -180,4 +214,10 @@ public class ContractInvokingTest {
HashDigest ledgerHash = block.getHash(); HashDigest ledgerHash = block.getHash();
return ledgerHash; return ledgerHash;
} }

private byte[] chainCode() {
byte[] chainCode = new byte[1024];
new Random().nextBytes(chainCode);
return chainCode;
}
} }

Loading…
Cancel
Save