Skip to content

Commit 8493819

Browse files
authored
Merge pull request #4958 from tronprotocol/feature/add_unit_tests
Add some unit tests
2 parents 7bac752 + d646847 commit 8493819

1 file changed

Lines changed: 175 additions & 8 deletions

File tree

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

Lines changed: 175 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,20 @@
1818

1919
package org.tron.core;
2020

21-
import static junit.framework.TestCase.fail;
2221
import static org.junit.Assert.assertArrayEquals;
2322
import static org.junit.Assert.assertEquals;
2423
import static org.junit.Assert.assertFalse;
2524
import static org.junit.Assert.assertNotNull;
2625
import static org.tron.core.config.Parameter.ChainConstant.TRX_PRECISION;
2726
import static org.tron.protos.contract.Common.ResourceCode.BANDWIDTH;
2827
import static org.tron.protos.contract.Common.ResourceCode.ENERGY;
29-
import static stest.tron.wallet.common.client.utils.PublicMethed.decode58Check;
3028

3129
import com.google.protobuf.Any;
3230
import com.google.protobuf.ByteString;
3331
import java.io.File;
34-
import java.util.ArrayList;
3532
import java.util.Arrays;
36-
import java.util.List;
33+
34+
import lombok.SneakyThrows;
3735
import lombok.extern.slf4j.Slf4j;
3836
import org.joda.time.DateTime;
3937
import org.junit.AfterClass;
@@ -55,26 +53,28 @@
5553
import org.tron.core.actuator.DelegateResourceActuator;
5654
import org.tron.core.actuator.FreezeBalanceActuator;
5755
import org.tron.core.actuator.UnfreezeBalanceV2Actuator;
58-
import org.tron.core.actuator.WithdrawExpireUnfreezeActuator;
5956
import org.tron.core.capsule.AccountCapsule;
6057
import org.tron.core.capsule.AssetIssueCapsule;
6158
import org.tron.core.capsule.BlockCapsule;
62-
import org.tron.core.capsule.DelegatedResourceAccountIndexCapsule;
59+
import org.tron.core.capsule.BytesCapsule;
60+
import org.tron.core.capsule.CodeCapsule;
61+
import org.tron.core.capsule.ContractCapsule;
6362
import org.tron.core.capsule.DelegatedResourceCapsule;
6463
import org.tron.core.capsule.ExchangeCapsule;
6564
import org.tron.core.capsule.ProposalCapsule;
6665
import org.tron.core.capsule.TransactionCapsule;
6766
import org.tron.core.capsule.TransactionInfoCapsule;
6867
import org.tron.core.capsule.TransactionResultCapsule;
69-
import org.tron.core.capsule.WitnessCapsule;
7068
import org.tron.core.config.DefaultConfig;
7169
import org.tron.core.config.args.Args;
7270
import org.tron.core.db.Manager;
7371
import org.tron.core.exception.ContractExeException;
7472
import org.tron.core.exception.ContractValidateException;
73+
import org.tron.core.exception.NonUniqueObjectException;
7574
import org.tron.core.store.DynamicPropertiesStore;
7675
import org.tron.core.utils.ProposalUtil.ProposalType;
7776
import org.tron.core.utils.TransactionUtil;
77+
import org.tron.core.vm.program.Program;
7878
import org.tron.protos.Protocol;
7979
import org.tron.protos.Protocol.AccountType;
8080
import org.tron.protos.Protocol.Block;
@@ -90,6 +90,7 @@
9090
import org.tron.protos.contract.BalanceContract;
9191
import org.tron.protos.contract.BalanceContract.TransferContract;
9292
import org.tron.protos.contract.Common;
93+
import org.tron.protos.contract.SmartContractOuterClass;
9394

9495

9596
@Slf4j
@@ -873,7 +874,6 @@ public void testGetAvailableUnfreezeCount() {
873874
}
874875
}
875876

876-
877877
@Test
878878
public void testGetCanWithdrawUnfreezeAmount() {
879879
long now = System.currentTimeMillis();
@@ -899,5 +899,172 @@ public void testGetCanWithdrawUnfreezeAmount() {
899899
Assert.assertEquals(16_000_000L * 2, message.getAmount());
900900
}
901901

902+
@Test
903+
public void testGetMemoFeePrices() {
904+
String memeFeeList = wallet.getMemoFeePrices();
905+
Assert.assertEquals("0:0", memeFeeList);
906+
}
907+
908+
@Test
909+
public void testGetChainParameters() {
910+
Protocol.ChainParameters params = wallet.getChainParameters();
911+
//getTotalEnergyAverageUsage & getTotalEnergyCurrentLimit have not ProposalType.
912+
Assert.assertEquals(ProposalType.values().length + 2, params.getChainParameterCount());
913+
}
914+
915+
@Test
916+
public void testGetAccountById() {
917+
AccountCapsule ownerCapsule =
918+
dbManager.getAccountStore().get(ByteArray.fromHexString(OWNER_ADDRESS));
919+
ownerCapsule.setAccountId(ByteString.copyFromUtf8("1001").toByteArray());
920+
chainBaseManager.getAccountIdIndexStore().put(ownerCapsule);
921+
Protocol.Account account = wallet.getAccountById(
922+
Protocol.Account.newBuilder().setAccountId(ByteString.copyFromUtf8("1001")).build());
923+
Assert.assertEquals(ownerCapsule.getAddress(),account.getAddress());
924+
}
925+
926+
@Test
927+
public void testGetAccountResource() {
928+
GrpcAPI.AccountResourceMessage accountResource =
929+
wallet.getAccountResource(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)));
930+
Assert.assertEquals(
931+
chainBaseManager.getDynamicPropertiesStore().getFreeNetLimit(),
932+
accountResource.getFreeNetLimit());
933+
Assert.assertEquals(0, accountResource.getFreeNetUsed());
934+
}
935+
936+
@Test
937+
public void testGetAssetIssueByName() {
938+
String assetName = "My_asset";
939+
String id = "10001";
940+
AssetIssueCapsule assetCapsule = new AssetIssueCapsule(ByteArray.fromHexString(OWNER_ADDRESS),
941+
id,assetName,"abbr", 1_000_000_000_000L,6);
942+
chainBaseManager.getAssetIssueStore().put(assetCapsule.createDbKey(), assetCapsule);
943+
chainBaseManager.getAssetIssueV2Store().put(assetCapsule.createDbV2Key(), assetCapsule);
944+
try {
945+
AssetIssueContract assetIssue =
946+
wallet.getAssetIssueByName(ByteString.copyFromUtf8(assetName));
947+
Assert.assertEquals(ByteString.copyFromUtf8(assetName),assetIssue.getName());
948+
Assert.assertEquals(id,assetIssue.getId());
949+
chainBaseManager.getDynamicPropertiesStore().saveAllowSameTokenName(1);
950+
assetIssue = wallet.getAssetIssueByName(ByteString.copyFromUtf8(assetName));
951+
Assert.assertEquals(ByteString.copyFromUtf8(assetName),assetIssue.getName());
952+
Assert.assertEquals(id,assetIssue.getId());
953+
} catch (NonUniqueObjectException e) {
954+
Assert.fail(e.getMessage());
955+
}
956+
chainBaseManager.getAssetIssueStore().delete(assetCapsule.createDbKey());
957+
chainBaseManager.getAssetIssueV2Store().delete(assetCapsule.createDbV2Key());
958+
chainBaseManager.getDynamicPropertiesStore().saveAllowSameTokenName(0);
959+
}
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+
}
9021069
}
9031070

0 commit comments

Comments
 (0)