Skip to content

Commit c3994e0

Browse files
committed
feat(test): add test for Wallet.estimateEnergy
1 parent ad69dd8 commit c3994e0

1 file changed

Lines changed: 94 additions & 0 deletions

File tree

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

Lines changed: 94 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,92 @@ 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 = "608060405234801561001057600080fd5b50d3801561001d57600080fd5b50d2801561002a57600080fd5b50600436106100495760003560e01c806385bb7d69146100555761004a565b5b610052610073565b50005b61005d610073565b60405161006a91906100b9565b60405180910390f35b600080600090505b60028110156100a657808261009091906100d4565b915060018161009f91906100d4565b905061007b565b5090565b6100b38161012a565b82525050565b60006020820190506100ce60008301846100aa565b92915050565b60006100df8261012a565b91506100ea8361012a565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0382111561011f5761011e610134565b5b828201905092915050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fdfea26474726f6e58221220f3d0198323c67293b97323c101e294e6d2cac7fb29555292675277e11c275a4b64736f6c63430008060033";
974+
CodeCapsule codeCapsule = new CodeCapsule(ByteArray.fromHexString(codeString));
975+
dbManager.getCodeStore().put(ByteArray.fromHexString(contractAddress), codeCapsule);
976+
977+
SmartContractOuterClass.TriggerSmartContract contract =
978+
SmartContractOuterClass.TriggerSmartContract.newBuilder()
979+
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
980+
.setContractAddress(ByteString.copyFrom(
981+
ByteArray.fromHexString(
982+
contractAddress)))
983+
.build();
984+
TransactionCapsule trxCap = wallet.createTransactionCapsule(contract, ContractType.TriggerSmartContract);
985+
986+
GrpcAPI.TransactionExtention.Builder trxExtBuilder = GrpcAPI.TransactionExtention.newBuilder();
987+
GrpcAPI.Return.Builder retBuilder = GrpcAPI.Return.newBuilder();
988+
GrpcAPI.EstimateEnergyMessage.Builder estimateBuilder
989+
= GrpcAPI.EstimateEnergyMessage.newBuilder();
990+
991+
try {
992+
wallet.estimateEnergy(
993+
contract, trxCap, trxExtBuilder, retBuilder, estimateBuilder);
994+
Assert.fail();
995+
} catch (ContractValidateException exception) {
996+
assertEquals("this node does not support estimate energy", exception.getMessage());
997+
}
998+
999+
Args.getInstance().setEstimateEnergy(true);
1000+
1001+
wallet.estimateEnergy(
1002+
contract, trxCap, trxExtBuilder, retBuilder, estimateBuilder);
1003+
GrpcAPI.EstimateEnergyMessage message = estimateBuilder.build();
1004+
Assert.assertTrue(message.getEnergyRequired() > 0);
1005+
}
1006+
1007+
@Test
1008+
@SneakyThrows
1009+
public void testEstimateEnergyOutOfTime() {
1010+
dbManager.getDynamicPropertiesStore().put("ALLOW_TVM_TRANSFER_TRC10".getBytes(),
1011+
new BytesCapsule(ByteArray.fromHexString("0x01")));
1012+
1013+
String contractAddress = "0x1A622D84ed49f01045f5f1a5AfcEb9c57e9cC3ca";
1014+
1015+
SmartContractOuterClass.SmartContract smartContract =
1016+
SmartContractOuterClass.SmartContract.newBuilder().build();
1017+
ContractCapsule capsule = new ContractCapsule(smartContract);
1018+
dbManager.getContractStore().put(ByteArray.fromHexString(contractAddress), capsule);
1019+
1020+
String codeString = "608060405234801561001057600080fd5b50d3801561001d57600080fd5b50d2801561002a57600080fd5b50600436106100495760003560e01c806385bb7d69146100555761004a565b5b610052610073565b50005b61005d610073565b60405161006a91906100ae565b60405180910390f35b600080600090505b64e8d4a5100081101561009b57808261009491906100c9565b915061007b565b5090565b6100a88161011f565b82525050565b60006020820190506100c3600083018461009f565b92915050565b60006100d48261011f565b91506100df8361011f565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0382111561011457610113610129565b5b828201905092915050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fdfea26474726f6e58221220a7e1a6e6d17684029015a0b593b63440f77e7eb8abd4297a3063e59f28086bf464736f6c63430008060033";
1021+
CodeCapsule codeCapsule = new CodeCapsule(ByteArray.fromHexString(codeString));
1022+
dbManager.getCodeStore().put(ByteArray.fromHexString(contractAddress), codeCapsule);
1023+
1024+
SmartContractOuterClass.TriggerSmartContract contract =
1025+
SmartContractOuterClass.TriggerSmartContract.newBuilder()
1026+
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
1027+
.setContractAddress(ByteString.copyFrom(
1028+
ByteArray.fromHexString(
1029+
contractAddress)))
1030+
.build();
1031+
TransactionCapsule trxCap = wallet.createTransactionCapsule(contract, ContractType.TriggerSmartContract);
1032+
1033+
GrpcAPI.TransactionExtention.Builder trxExtBuilder = GrpcAPI.TransactionExtention.newBuilder();
1034+
GrpcAPI.Return.Builder retBuilder = GrpcAPI.Return.newBuilder();
1035+
GrpcAPI.EstimateEnergyMessage.Builder estimateBuilder
1036+
= GrpcAPI.EstimateEnergyMessage.newBuilder();
1037+
1038+
Args.getInstance().setEstimateEnergy(true);
1039+
1040+
try {
1041+
wallet.estimateEnergy(
1042+
contract, trxCap, trxExtBuilder, retBuilder, estimateBuilder);
1043+
Assert.fail("EstimateEnergy should throw exception!");
1044+
} catch (Program.OutOfTimeException ignored) {
1045+
}
1046+
}
9531047
}
9541048

0 commit comments

Comments
 (0)