Skip to content

Commit d646847

Browse files
authored
Merge pull request #4955 from daniel-cao-byte/feature/add_unit_tests
feat(test): add test for Wallet.estimateEnergy
2 parents ad69dd8 + 3b9b790 commit d646847

1 file changed

Lines changed: 116 additions & 0 deletions

File tree

framework/src/test/java/org/tron/core/WalletTest.java

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import com.google.protobuf.ByteString;
3131
import java.io.File;
3232
import java.util.Arrays;
33+
34+
import lombok.SneakyThrows;
3335
import lombok.extern.slf4j.Slf4j;
3436
import org.joda.time.DateTime;
3537
import org.junit.AfterClass;
@@ -54,6 +56,9 @@
5456
import org.tron.core.capsule.AccountCapsule;
5557
import org.tron.core.capsule.AssetIssueCapsule;
5658
import org.tron.core.capsule.BlockCapsule;
59+
import org.tron.core.capsule.BytesCapsule;
60+
import org.tron.core.capsule.CodeCapsule;
61+
import org.tron.core.capsule.ContractCapsule;
5762
import org.tron.core.capsule.DelegatedResourceCapsule;
5863
import org.tron.core.capsule.ExchangeCapsule;
5964
import org.tron.core.capsule.ProposalCapsule;
@@ -69,6 +74,7 @@
6974
import org.tron.core.store.DynamicPropertiesStore;
7075
import org.tron.core.utils.ProposalUtil.ProposalType;
7176
import org.tron.core.utils.TransactionUtil;
77+
import org.tron.core.vm.program.Program;
7278
import org.tron.protos.Protocol;
7379
import org.tron.protos.Protocol.AccountType;
7480
import org.tron.protos.Protocol.Block;
@@ -84,6 +90,7 @@
8490
import org.tron.protos.contract.BalanceContract;
8591
import org.tron.protos.contract.BalanceContract.TransferContract;
8692
import org.tron.protos.contract.Common;
93+
import org.tron.protos.contract.SmartContractOuterClass;
8794

8895

8996
@Slf4j
@@ -950,5 +957,114 @@ public void testGetAssetIssueByName() {
950957
chainBaseManager.getAssetIssueV2Store().delete(assetCapsule.createDbV2Key());
951958
chainBaseManager.getDynamicPropertiesStore().saveAllowSameTokenName(0);
952959
}
960+
961+
@Test
962+
@SneakyThrows
963+
public void testEstimateEnergy() {
964+
dbManager.getDynamicPropertiesStore().put("ALLOW_TVM_TRANSFER_TRC10".getBytes(),
965+
new BytesCapsule(ByteArray.fromHexString("0x01")));
966+
String contractAddress = "0x1A622D84ed49f01045f5f1a5AfcEb9c57e9cC3ca";
967+
968+
SmartContractOuterClass.SmartContract smartContract =
969+
SmartContractOuterClass.SmartContract.newBuilder().build();
970+
ContractCapsule capsule = new ContractCapsule(smartContract);
971+
dbManager.getContractStore().put(ByteArray.fromHexString(contractAddress), capsule);
972+
973+
String codeString = "608060405234801561001057600080fd5b50d3801561001d57600080fd5b50d28015"
974+
+ "61002a57600080fd5b50600436106100495760003560e01c806385bb7d69146100555761004a565b5b61"
975+
+ "0052610073565b50005b61005d610073565b60405161006a91906100b9565b60405180910390f35b6000"
976+
+ "80600090505b60028110156100a657808261009091906100d4565b915060018161009f91906100d4565b"
977+
+ "905061007b565b5090565b6100b38161012a565b82525050565b60006020820190506100ce6000830184"
978+
+ "6100aa565b92915050565b60006100df8261012a565b91506100ea8361012a565b9250827fffffffffff"
979+
+ "ffffffffffffffffffffffffffffffffffffffffffffffffffffff0382111561011f5761011e61013456"
980+
+ "5b5b828201905092915050565b6000819050919050565b7f4e487b710000000000000000000000000000"
981+
+ "0000000000000000000000000000600052601160045260246000fdfea26474726f6e58221220f3d01983"
982+
+ "23c67293b97323c101e294e6d2cac7fb29555292675277e11c275a4b64736f6c63430008060033";
983+
CodeCapsule codeCapsule = new CodeCapsule(ByteArray.fromHexString(codeString));
984+
dbManager.getCodeStore().put(ByteArray.fromHexString(contractAddress), codeCapsule);
985+
986+
SmartContractOuterClass.TriggerSmartContract contract =
987+
SmartContractOuterClass.TriggerSmartContract.newBuilder()
988+
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
989+
.setContractAddress(ByteString.copyFrom(
990+
ByteArray.fromHexString(
991+
contractAddress)))
992+
.build();
993+
TransactionCapsule trxCap = wallet.createTransactionCapsule(contract,
994+
ContractType.TriggerSmartContract);
995+
996+
GrpcAPI.TransactionExtention.Builder trxExtBuilder = GrpcAPI.TransactionExtention.newBuilder();
997+
GrpcAPI.Return.Builder retBuilder = GrpcAPI.Return.newBuilder();
998+
GrpcAPI.EstimateEnergyMessage.Builder estimateBuilder
999+
= GrpcAPI.EstimateEnergyMessage.newBuilder();
1000+
1001+
Args.getInstance().setEstimateEnergy(false);
1002+
try {
1003+
wallet.estimateEnergy(
1004+
contract, trxCap, trxExtBuilder, retBuilder, estimateBuilder);
1005+
Assert.fail();
1006+
} catch (ContractValidateException exception) {
1007+
assertEquals("this node does not support estimate energy", exception.getMessage());
1008+
}
1009+
1010+
Args.getInstance().setEstimateEnergy(true);
1011+
1012+
wallet.estimateEnergy(
1013+
contract, trxCap, trxExtBuilder, retBuilder, estimateBuilder);
1014+
GrpcAPI.EstimateEnergyMessage message = estimateBuilder.build();
1015+
Assert.assertTrue(message.getEnergyRequired() > 0);
1016+
}
1017+
1018+
@Test
1019+
@SneakyThrows
1020+
public void testEstimateEnergyOutOfTime() {
1021+
dbManager.getDynamicPropertiesStore().put("ALLOW_TVM_TRANSFER_TRC10".getBytes(),
1022+
new BytesCapsule(ByteArray.fromHexString("0x01")));
1023+
1024+
String contractAddress = "0x1A622D84ed49f01045f5f1a5AfcEb9c57e9cC3ca";
1025+
1026+
SmartContractOuterClass.SmartContract smartContract =
1027+
SmartContractOuterClass.SmartContract.newBuilder().build();
1028+
ContractCapsule capsule = new ContractCapsule(smartContract);
1029+
dbManager.getContractStore().put(ByteArray.fromHexString(contractAddress), capsule);
1030+
1031+
String codeString = "608060405234801561001057600080fd5b50d3801561001d57600080fd5b50d28015"
1032+
+ "61002a57600080fd5b50600436106100495760003560e01c806385bb7d69146100555761004a565b5b61"
1033+
+ "0052610073565b50005b61005d610073565b60405161006a91906100ae565b60405180910390f35b6000"
1034+
+ "80600090505b64e8d4a5100081101561009b57808261009491906100c9565b915061007b565b5090565b"
1035+
+ "6100a88161011f565b82525050565b60006020820190506100c3600083018461009f565b92915050565b"
1036+
+ "60006100d48261011f565b91506100df8361011f565b9250827fffffffffffffffffffffffffffffffff"
1037+
+ "ffffffffffffffffffffffffffffffff0382111561011457610113610129565b5b828201905092915050"
1038+
+ "565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000"
1039+
+ "000000600052601160045260246000fdfea26474726f6e58221220a7e1a6e6d17684029015a0b593b634"
1040+
+ "40f77e7eb8abd4297a3063e59f28086bf464736f6c63430008060033";
1041+
CodeCapsule codeCapsule = new CodeCapsule(ByteArray.fromHexString(codeString));
1042+
dbManager.getCodeStore().put(ByteArray.fromHexString(contractAddress), codeCapsule);
1043+
1044+
SmartContractOuterClass.TriggerSmartContract contract =
1045+
SmartContractOuterClass.TriggerSmartContract.newBuilder()
1046+
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
1047+
.setContractAddress(ByteString.copyFrom(
1048+
ByteArray.fromHexString(
1049+
contractAddress)))
1050+
.build();
1051+
TransactionCapsule trxCap = wallet.createTransactionCapsule(contract,
1052+
ContractType.TriggerSmartContract);
1053+
1054+
GrpcAPI.TransactionExtention.Builder trxExtBuilder = GrpcAPI.TransactionExtention.newBuilder();
1055+
GrpcAPI.Return.Builder retBuilder = GrpcAPI.Return.newBuilder();
1056+
GrpcAPI.EstimateEnergyMessage.Builder estimateBuilder
1057+
= GrpcAPI.EstimateEnergyMessage.newBuilder();
1058+
1059+
Args.getInstance().setEstimateEnergy(true);
1060+
1061+
try {
1062+
wallet.estimateEnergy(
1063+
contract, trxCap, trxExtBuilder, retBuilder, estimateBuilder);
1064+
Assert.fail("EstimateEnergy should throw exception!");
1065+
} catch (Program.OutOfTimeException ignored) {
1066+
Assert.assertTrue(true);
1067+
}
1068+
}
9531069
}
9541070

0 commit comments

Comments
 (0)