Skip to content

Commit

Permalink
add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
taihaofu committed Oct 26, 2018
1 parent 25e275f commit 97cd88b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
48 changes: 36 additions & 12 deletions src/test/java/org/tron/common/runtime/ProgramResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,26 @@
import org.tron.common.runtime.vm.DataWord;
import org.tron.common.runtime.vm.program.InternalTransaction;
import org.tron.common.storage.DepositImpl;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.ByteString;
import org.tron.common.utils.FileUtil;
import org.tron.core.Constant;
import org.tron.core.Wallet;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.capsule.TransactionCapsule;
import org.tron.core.capsule.TransactionInfoCapsule;
import org.tron.core.config.DefaultConfig;
import org.tron.core.config.args.Args;
import org.tron.core.db.Manager;
import org.tron.core.db.TransactionTrace;
import org.tron.core.exception.ContractExeException;
import org.tron.core.exception.ContractValidateException;
import org.tron.core.exception.ReceiptCheckErrException;
import org.tron.core.exception.VMIllegalException;
import org.tron.protos.Protocol;
import org.tron.protos.Protocol.AccountType;
import org.tron.protos.Protocol.Transaction;
import org.tron.protos.Protocol.TransactionInfo;


@Slf4j
Expand Down Expand Up @@ -287,10 +293,10 @@ public void successAndFailResultTest()
// ======================================= Test Success =======================================
byte[] triggerData1 = TVMTestUtils.parseABI("transfer(address,bool)",
params);
runtime = TVMTestUtils
.triggerContractWholeProcessReturnContractAddress(Hex.decode(OWNER_ADDRESS),
aContract, triggerData1,
0, 100000000, deposit, null);
Transaction trx1 = TVMTestUtils.generateTriggerSmartContractAndGetTransaction(Hex.decode(OWNER_ADDRESS), aContract,
triggerData1, 0, 100000000);
TransactionTrace traceSuccess = TVMTestUtils.processTransactionAndReturnTrace(trx1, deposit, null);
runtime = traceSuccess.getRuntime();
byte[] bContract = runtime.getResult().getHReturn();
List<InternalTransaction> internalTransactionsList = runtime.getResult().getInternalTransactions();
Assert.assertEquals(internalTransactionsList.get(0).getValue(), 10);
Expand All @@ -313,18 +319,19 @@ public void successAndFailResultTest()
Assert.assertEquals(internalTransactionsList.get(3).getTransferToAddress() , cContract);
Assert.assertEquals(internalTransactionsList.get(3).getNote(), "call");
Assert.assertEquals(internalTransactionsList.get(3).isRejected(), false);
checkTransactionInfo(traceSuccess,trx1,null, internalTransactionsList);

// ======================================= Test Fail =======================================
// set revert == true
params = Hex.toHexString(new DataWord(new DataWord(cContract).getLast20Bytes()).getData()) +
"0000000000000000000000000000000000000000000000000000000000000001";
byte[] triggerData2 = TVMTestUtils.parseABI("transfer(address,bool)",
params);
Transaction trx = TVMTestUtils.generateTriggerSmartContractAndGetTransaction(Hex.decode(OWNER_ADDRESS), aContract,
Transaction trx2 = TVMTestUtils.generateTriggerSmartContractAndGetTransaction(Hex.decode(OWNER_ADDRESS), aContract,
triggerData2, 0, 100000000);
runtime = TVMTestUtils.processTransactionAndReturnRuntime(trx, deposit, null);

byte[] bContract2 = Wallet.generateContractAddress(new TransactionCapsule(trx).getTransactionId().getBytes(), 0);
TransactionTrace traceFailed = TVMTestUtils.processTransactionAndReturnTrace(trx2, deposit, null);
runtime = traceFailed.getRuntime();
byte[] bContract2 = Wallet.generateContractAddress(new TransactionCapsule(trx2).getTransactionId().getBytes(), 0);
List<InternalTransaction> internalTransactionsListFail = runtime.getResult().getInternalTransactions();
Assert.assertEquals(internalTransactionsListFail.get(0).getValue(), 10);
Assert.assertEquals(internalTransactionsListFail.get(0).getSender(), aContract);
Expand All @@ -346,6 +353,7 @@ public void successAndFailResultTest()
Assert.assertEquals(internalTransactionsListFail.get(3).getTransferToAddress() , cContract);
Assert.assertEquals(internalTransactionsListFail.get(3).getNote(), "call");
Assert.assertEquals(internalTransactionsListFail.get(3).isRejected(), true);
checkTransactionInfo(traceFailed,trx2,null, internalTransactionsListFail);
}


Expand Down Expand Up @@ -419,10 +427,10 @@ public void suicideResultTest()
// ======================================= Test Suicide =======================================
byte[] triggerData1 = TVMTestUtils.parseABI("suicide(address)",
params);
runtime = TVMTestUtils
.triggerContractWholeProcessReturnContractAddress(Hex.decode(OWNER_ADDRESS),
suicideContract, triggerData1,
0, 100000000, deposit, null);
Transaction trx = TVMTestUtils.generateTriggerSmartContractAndGetTransaction(Hex.decode(OWNER_ADDRESS), suicideContract,
triggerData1, 0, 100000000);
TransactionTrace trace = TVMTestUtils.processTransactionAndReturnTrace(trx, deposit, null);
runtime = trace.getRuntime();
List<InternalTransaction> internalTransactionsList = runtime.getResult().getInternalTransactions();
Assert.assertEquals(dbManager.getAccountStore().get(Hex.decode(TRANSFER_TO)).getBalance(), 1000);
Assert.assertEquals(dbManager.getAccountStore().get(suicideContract), null);
Expand All @@ -431,6 +439,7 @@ public void suicideResultTest()
Assert.assertEquals(internalTransactionsList.get(0).getTransferToAddress() , Hex.decode(TRANSFER_TO));
Assert.assertEquals(internalTransactionsList.get(0).getNote(), "suicide");
Assert.assertEquals(internalTransactionsList.get(0).isRejected(), false);
checkTransactionInfo(trace, trx,null, internalTransactionsList);
}

private byte[] deploySuicide()
Expand All @@ -456,6 +465,21 @@ private byte[] deploySuicide()
deposit, null);
}

public void checkTransactionInfo(TransactionTrace trace, Transaction trx, BlockCapsule block, List<InternalTransaction> internalTransactionsList ){
TransactionInfoCapsule trxInfoCapsule = TransactionInfoCapsule.buildInstance(new TransactionCapsule(trx),null, trace);
List<Protocol.InternalTransaction> internalTransactionListFromProtocol = trxInfoCapsule.getInstance().getInternalTransactionsList();
for(int i=0; i< internalTransactionListFromProtocol.size(); i++){
Assert.assertEquals(internalTransactionListFromProtocol.get(i).getRejected(), internalTransactionsList.get(i).isRejected());
Assert.assertEquals(internalTransactionListFromProtocol.get(i).getCallerAddress().toByteArray(),internalTransactionsList.get(i).getSender());
Assert.assertEquals(internalTransactionListFromProtocol.get(i).getHash().toByteArray(), internalTransactionsList.get(i).getHash());
Assert.assertEquals(new String(internalTransactionListFromProtocol.get(i).getNote().toByteArray()), internalTransactionsList.get(i).getNote());
Assert.assertEquals(internalTransactionListFromProtocol.get(i).getTransferToAddress().toByteArray(), internalTransactionsList.get(i).getTransferToAddress());
List<Protocol.InternalTransaction.CallValueInfo> callValueInfoListFromProtocol = internalTransactionListFromProtocol.get(i).getCallValueInfoList();
// for now only one callValue. will be change to list in future.
Assert.assertEquals(callValueInfoListFromProtocol.get(0).getCallValue(), internalTransactionsList.get(i).getValue());
}
}

/**
* Release resources.
*/
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/org/tron/common/runtime/TVMTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,22 @@ public static Runtime processTransactionAndReturnRuntime(Transaction trx,
return trace.getRuntime();
}

public static TransactionTrace processTransactionAndReturnTrace(Transaction trx,
DepositImpl deposit, BlockCapsule block)
throws ContractExeException, ContractValidateException, ReceiptCheckErrException, VMIllegalException {
TransactionCapsule trxCap = new TransactionCapsule(trx);
deposit.commit();
TransactionTrace trace = new TransactionTrace(trxCap, deposit.getDbManager());
// init
trace.init(block);
//exec
trace.exec();

trace.finalization();

return trace;
}


public static TVMTestResult deployContractAndReturnTVMTestResult(String contractName,
byte[] callerAddress,
Expand Down

0 comments on commit 97cd88b

Please sign in to comment.