From 75fba7a1c242611415efbd2e112f262597470afd Mon Sep 17 00:00:00 2001 From: jiangyuanshu <317787106@qq.com> Date: Thu, 13 Apr 2023 14:37:36 +0800 Subject: [PATCH 1/7] fix(net): stable test case; add parameters in config --- .../main/java/org/tron/core/net/TronNetService.java | 6 +++--- framework/src/main/resources/config.conf | 13 +++++++++++++ .../java/org/tron/common/config/args/ArgsTest.java | 6 ++++-- .../tron/core/actuator/utils/ProposalUtilTest.java | 4 ++++ .../src/test/java/org/tron/core/db/ManagerTest.java | 7 +++++++ .../test/java/org/tron/core/net/MessageTest.java | 5 +++++ .../net/services/EffectiveCheckServiceTest.java | 13 +++++++++++++ framework/src/test/resources/config-test.conf | 4 ++++ 8 files changed, 53 insertions(+), 5 deletions(-) diff --git a/framework/src/main/java/org/tron/core/net/TronNetService.java b/framework/src/main/java/org/tron/core/net/TronNetService.java index 89767e31827..dda3be886dc 100644 --- a/framework/src/main/java/org/tron/core/net/TronNetService.java +++ b/framework/src/main/java/org/tron/core/net/TronNetService.java @@ -82,7 +82,8 @@ private static void setP2pConfig(P2pConfig config) { public void start() { try { init = true; - setP2pConfig(getConfig()); + P2pConfig config = new P2pConfig(); + setP2pConfig(getConfig(config)); p2pService.start(p2pConfig); p2pService.register(p2pEventHandler); advService.init(); @@ -147,11 +148,10 @@ public static boolean hasIpv4Stack(Set ipSet) { return false; } - private P2pConfig getConfig() { + private P2pConfig getConfig(P2pConfig config) { List seeds = parameter.getSeedNode().getAddressList(); seeds.addAll(nodePersistService.dbRead()); logger.debug("Seed InetSocketAddress: {}", seeds); - P2pConfig config = new P2pConfig(); config.getSeedNodes().addAll(seeds); config.getActiveNodes().addAll(parameter.getActiveNodes()); config.getTrustNodes().addAll(parameter.getPassiveNodes()); diff --git a/framework/src/main/resources/config.conf b/framework/src/main/resources/config.conf index 47635540017..67aa374eec3 100644 --- a/framework/src/main/resources/config.conf +++ b/framework/src/main/resources/config.conf @@ -202,6 +202,19 @@ node { solidityPort = 8091 } + # use your ipv6 address for node discovery and tcp connection, default false + enableIpv6 = false + + # if your node's highest block num is below than all your pees', try to acquire new connection. default false + effectiveCheckEnable = false + + dns { + # dns urls to get nodes, url format tree://{pubkey}@{domain}, default empty + treeUrls = [ + #"tree://APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ@nodes1.example.org", + ] + } + rpc { port = 50051 #solidityPort = 50061 diff --git a/framework/src/test/java/org/tron/common/config/args/ArgsTest.java b/framework/src/test/java/org/tron/common/config/args/ArgsTest.java index 2c85397eb1b..aa58d8e0fc9 100644 --- a/framework/src/test/java/org/tron/common/config/args/ArgsTest.java +++ b/framework/src/test/java/org/tron/common/config/args/ArgsTest.java @@ -11,16 +11,18 @@ public class ArgsTest { + private static final String dbPath = "output_arg_test"; + @Before public void init() { - Args.setParam(new String[]{"--output-directory", "output-directory", "--p2p-disable", "true", + Args.setParam(new String[]{"--output-directory", dbPath, "--p2p-disable", "true", "--debug"}, Constant.TEST_CONF); } @After public void destroy() { Args.clearParam(); - FileUtil.deleteDir(new File("output-directory")); + FileUtil.deleteDir(new File(dbPath)); } @Test diff --git a/framework/src/test/java/org/tron/core/actuator/utils/ProposalUtilTest.java b/framework/src/test/java/org/tron/core/actuator/utils/ProposalUtilTest.java index ed19516a608..4f409815f8b 100644 --- a/framework/src/test/java/org/tron/core/actuator/utils/ProposalUtilTest.java +++ b/framework/src/test/java/org/tron/core/actuator/utils/ProposalUtilTest.java @@ -357,6 +357,10 @@ public void validateCheck() { Assert.assertEquals("Bad chain parameter value, valid range is [0, 1_000_000_000_000L]", e.getMessage()); } + + forkUtils.getManager().getDynamicPropertiesStore() + .statsByVersion(ForkBlockVersionEnum.ENERGY_LIMIT.getValue(), stats); + forkUtils.reset(); } @Test diff --git a/framework/src/test/java/org/tron/core/db/ManagerTest.java b/framework/src/test/java/org/tron/core/db/ManagerTest.java index e7319d0ed0b..b7b29f49930 100755 --- a/framework/src/test/java/org/tron/core/db/ManagerTest.java +++ b/framework/src/test/java/org/tron/core/db/ManagerTest.java @@ -226,6 +226,13 @@ public void pushBlock() { } } + try { + chainManager.getBlockIdByNum(-1); + Assert.fail(); + } catch (ItemNotFoundException e) { + Assert.assertTrue(true); + } + Assert.assertTrue("hasBlocks is error", chainManager.hasBlocks()); } diff --git a/framework/src/test/java/org/tron/core/net/MessageTest.java b/framework/src/test/java/org/tron/core/net/MessageTest.java index d27f37214c5..5b81d18a599 100644 --- a/framework/src/test/java/org/tron/core/net/MessageTest.java +++ b/framework/src/test/java/org/tron/core/net/MessageTest.java @@ -107,6 +107,11 @@ public Class getAnswerMessage() { messageStatistics.addTcpOutMessage(message4); messageStatistics.addTcpInMessage(message5); messageStatistics.addTcpOutMessage(message5); + try { + Thread.sleep(2000);// so that gap > 1 in MessageCount.update method + } catch (InterruptedException e) { + //ignore + } messageStatistics.addTcpInMessage(message6); messageStatistics.addTcpOutMessage(message6); messageStatistics.addTcpInMessage(message7); diff --git a/framework/src/test/java/org/tron/core/net/services/EffectiveCheckServiceTest.java b/framework/src/test/java/org/tron/core/net/services/EffectiveCheckServiceTest.java index 5f68be84251..1c422047e10 100644 --- a/framework/src/test/java/org/tron/core/net/services/EffectiveCheckServiceTest.java +++ b/framework/src/test/java/org/tron/core/net/services/EffectiveCheckServiceTest.java @@ -1,6 +1,7 @@ package org.tron.core.net.services; import java.io.File; +import java.lang.reflect.Method; import java.net.InetSocketAddress; import org.junit.After; import org.junit.Assert; @@ -37,6 +38,18 @@ public void destroy() { FileUtil.deleteDir(new File(dbPath)); } + @Test + public void testNoIpv4() throws Exception { + TronNetService tronNetService = context.getBean(TronNetService.class); + Method privateMethod = tronNetService.getClass() + .getDeclaredMethod("getConfig", P2pConfig.class); + privateMethod.setAccessible(true); + P2pConfig config = new P2pConfig(); + config.setIp(null); + P2pConfig newConfig = (P2pConfig) privateMethod.invoke(tronNetService, config); + Assert.assertNotNull(newConfig.getIp()); + } + @Test public void testFind() { TronNetService tronNetService = context.getBean(TronNetService.class); diff --git a/framework/src/test/resources/config-test.conf b/framework/src/test/resources/config-test.conf index c9602ef977b..bef8239fd86 100644 --- a/framework/src/test/resources/config-test.conf +++ b/framework/src/test/resources/config-test.conf @@ -105,8 +105,12 @@ node { solidityPort = 8091 } + # use your ipv6 address for node discovery and tcp connection, default false enableIpv6 = false + # if your node's highest block num is below than all your pees', try to acquire new connection + effectiveCheckEnable = false + dns { # dns urls to get nodes, url format tree://{pubkey}@{domain}, default empty treeUrls = [ From 071a15bf69caebb59e42387ab0a144194b78c609 Mon Sep 17 00:00:00 2001 From: jiangyuanshu <317787106@qq.com> Date: Fri, 14 Apr 2023 12:15:52 +0800 Subject: [PATCH 2/7] fix(net):modify BaseNet --- .../main/java/org/tron/core/net/TronNetService.java | 10 +++++++--- framework/src/test/java/org/tron/core/net/BaseNet.java | 8 ++++---- .../core/net/services/EffectiveCheckServiceTest.java | 2 +- framework/src/test/resources/config-test.conf | 2 +- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/framework/src/main/java/org/tron/core/net/TronNetService.java b/framework/src/main/java/org/tron/core/net/TronNetService.java index dda3be886dc..2e9345a6794 100644 --- a/framework/src/main/java/org/tron/core/net/TronNetService.java +++ b/framework/src/main/java/org/tron/core/net/TronNetService.java @@ -82,8 +82,7 @@ private static void setP2pConfig(P2pConfig config) { public void start() { try { init = true; - P2pConfig config = new P2pConfig(); - setP2pConfig(getConfig(config)); + setP2pConfig(getConfig()); p2pService.start(p2pConfig); p2pService.register(p2pEventHandler); advService.init(); @@ -148,7 +147,12 @@ public static boolean hasIpv4Stack(Set ipSet) { return false; } - private P2pConfig getConfig(P2pConfig config) { + private P2pConfig getConfig(){ + P2pConfig config = new P2pConfig(); + return updateConfig(config); + } + + private P2pConfig updateConfig(P2pConfig config) { List seeds = parameter.getSeedNode().getAddressList(); seeds.addAll(nodePersistService.dbRead()); logger.debug("Seed InetSocketAddress: {}", seeds); diff --git a/framework/src/test/java/org/tron/core/net/BaseNet.java b/framework/src/test/java/org/tron/core/net/BaseNet.java index ca42dc2a65e..64d4b128543 100644 --- a/framework/src/test/java/org/tron/core/net/BaseNet.java +++ b/framework/src/test/java/org/tron/core/net/BaseNet.java @@ -103,9 +103,9 @@ public void run() { } }); int tryTimes = 0; - while (++tryTimes < 100 && tronNetDelegate == null) { - Thread.sleep(3000); - } + do { + Thread.sleep(3000); //coverage consumerInvToSpread,consumerInvToFetch in AdvService.init + } while (++tryTimes < 100 && tronNetDelegate == null); } @After @@ -115,7 +115,7 @@ public void destroy() { for (PeerConnection peer : peerConnections) { peer.getChannel().close(); } - + Args.clearParam(); context.destroy(); FileUtil.deleteDir(new File(dbPath)); } diff --git a/framework/src/test/java/org/tron/core/net/services/EffectiveCheckServiceTest.java b/framework/src/test/java/org/tron/core/net/services/EffectiveCheckServiceTest.java index 1c422047e10..aa42e0fcb89 100644 --- a/framework/src/test/java/org/tron/core/net/services/EffectiveCheckServiceTest.java +++ b/framework/src/test/java/org/tron/core/net/services/EffectiveCheckServiceTest.java @@ -42,7 +42,7 @@ public void destroy() { public void testNoIpv4() throws Exception { TronNetService tronNetService = context.getBean(TronNetService.class); Method privateMethod = tronNetService.getClass() - .getDeclaredMethod("getConfig", P2pConfig.class); + .getDeclaredMethod("updateConfig", P2pConfig.class); privateMethod.setAccessible(true); P2pConfig config = new P2pConfig(); config.setIp(null); diff --git a/framework/src/test/resources/config-test.conf b/framework/src/test/resources/config-test.conf index bef8239fd86..dea5d72961d 100644 --- a/framework/src/test/resources/config-test.conf +++ b/framework/src/test/resources/config-test.conf @@ -108,7 +108,7 @@ node { # use your ipv6 address for node discovery and tcp connection, default false enableIpv6 = false - # if your node's highest block num is below than all your pees', try to acquire new connection + # if your node's highest block num is below than all your pees', try to acquire new connection, default false effectiveCheckEnable = false dns { From 1b2024de69eeabc3545f4c4e63e21466c1616737 Mon Sep 17 00:00:00 2001 From: jiangyuanshu <317787106@qq.com> Date: Fri, 14 Apr 2023 12:17:34 +0800 Subject: [PATCH 3/7] fix(net):format code --- framework/src/main/java/org/tron/core/net/TronNetService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/src/main/java/org/tron/core/net/TronNetService.java b/framework/src/main/java/org/tron/core/net/TronNetService.java index 2e9345a6794..87d878be8c9 100644 --- a/framework/src/main/java/org/tron/core/net/TronNetService.java +++ b/framework/src/main/java/org/tron/core/net/TronNetService.java @@ -147,7 +147,7 @@ public static boolean hasIpv4Stack(Set ipSet) { return false; } - private P2pConfig getConfig(){ + private P2pConfig getConfig() { P2pConfig config = new P2pConfig(); return updateConfig(config); } From fbcbd6ee98c26e04dcc5e9765e1cdbd6995954ca Mon Sep 17 00:00:00 2001 From: jiangyuanshu <317787106@qq.com> Date: Fri, 14 Apr 2023 15:57:50 +0800 Subject: [PATCH 4/7] fix(net):optimize BaseNet --- .../test/java/org/tron/core/net/BaseNet.java | 76 ++++++++++--------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/framework/src/test/java/org/tron/core/net/BaseNet.java b/framework/src/test/java/org/tron/core/net/BaseNet.java index 64d4b128543..6af079a0197 100644 --- a/framework/src/test/java/org/tron/core/net/BaseNet.java +++ b/framework/src/test/java/org/tron/core/net/BaseNet.java @@ -19,8 +19,8 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import lombok.extern.slf4j.Slf4j; -import org.junit.After; -import org.junit.Before; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.tron.common.application.Application; import org.tron.common.application.ApplicationFactory; import org.tron.common.application.TronApplicationContext; @@ -34,20 +34,20 @@ import org.tron.core.services.RpcApiService; @Slf4j -public abstract class BaseNet { +public class BaseNet { private static String dbPath = "output-net"; private static String dbDirectory = "net-database"; private static String indexDirectory = "net-index"; private static int port = 10000; - protected TronApplicationContext context; + protected static TronApplicationContext context; - private RpcApiService rpcApiService; - private Application appT; - private TronNetDelegate tronNetDelegate; + private static RpcApiService rpcApiService; + private static Application appT; + private static TronNetDelegate tronNetDelegate; - private ExecutorService executorService = Executors.newFixedThreadPool(1); + private static ExecutorService executorService = Executors.newFixedThreadPool(1); public static Channel connect(ByteToMessageDecoder decoder) throws InterruptedException { NioEventLoopGroup group = new NioEventLoopGroup(1); @@ -73,34 +73,36 @@ protected void initChannel(Channel ch) throws Exception { return b.connect(Constant.LOCAL_HOST, port).sync().channel(); } - @Before - public void init() throws Exception { - executorService.execute(new Runnable() { - @Override - public void run() { - logger.info("Full node running."); - Args.setParam( - new String[]{ - "--output-directory", dbPath, - "--storage-db-directory", dbDirectory, - "--storage-index-directory", indexDirectory - }, - "config.conf" - ); - CommonParameter parameter = Args.getInstance(); - parameter.setNodeListenPort(port); - parameter.getSeedNode().getAddressList().clear(); - parameter.setNodeExternalIp(Constant.LOCAL_HOST); - context = new TronApplicationContext(DefaultConfig.class); - appT = ApplicationFactory.create(context); - rpcApiService = context.getBean(RpcApiService.class); - appT.addService(rpcApiService); - appT.initServices(parameter); - appT.startServices(); - appT.startup(); - tronNetDelegate = context.getBean(TronNetDelegate.class); - rpcApiService.blockUntilShutdown(); + @BeforeClass + public static void init() throws Exception { + executorService.execute(() -> { + logger.info("Full node running."); + Args.setParam( + new String[]{ + "--output-directory", dbPath, + "--storage-db-directory", dbDirectory, + "--storage-index-directory", indexDirectory + }, + "config.conf" + ); + CommonParameter parameter = Args.getInstance(); + parameter.setNodeListenPort(port); + parameter.getSeedNode().getAddressList().clear(); + parameter.setNodeExternalIp(Constant.LOCAL_HOST); + context = new TronApplicationContext(DefaultConfig.class); + appT = ApplicationFactory.create(context); + rpcApiService = context.getBean(RpcApiService.class); + appT.addService(rpcApiService); + appT.initServices(parameter); + appT.startServices(); + appT.startup(); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + //ignore } + tronNetDelegate = context.getBean(TronNetDelegate.class); + rpcApiService.blockUntilShutdown(); }); int tryTimes = 0; do { @@ -108,8 +110,8 @@ public void run() { } while (++tryTimes < 100 && tronNetDelegate == null); } - @After - public void destroy() { + @AfterClass + public static void destroy() { Collection peerConnections = ReflectUtils .invokeMethod(tronNetDelegate, "getActivePeer"); for (PeerConnection peer : peerConnections) { From 403da93b5c07b2994bd4cfbd4b62cd07bd7f45b0 Mon Sep 17 00:00:00 2001 From: jiangyuanshu <317787106@qq.com> Date: Fri, 14 Apr 2023 19:25:30 +0800 Subject: [PATCH 5/7] fix(net):optimize BaseNet --- framework/src/test/java/org/tron/core/net/BaseNet.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/src/test/java/org/tron/core/net/BaseNet.java b/framework/src/test/java/org/tron/core/net/BaseNet.java index 6af079a0197..805f8aa76a4 100644 --- a/framework/src/test/java/org/tron/core/net/BaseNet.java +++ b/framework/src/test/java/org/tron/core/net/BaseNet.java @@ -97,7 +97,7 @@ public static void init() throws Exception { appT.startServices(); appT.startup(); try { - Thread.sleep(1000); + Thread.sleep(2000); } catch (InterruptedException e) { //ignore } From 4e8152677f3ee5f03e0cafe55fafb663304f5483 Mon Sep 17 00:00:00 2001 From: jiangyuanshu <317787106@qq.com> Date: Tue, 18 Apr 2023 16:44:51 +0800 Subject: [PATCH 6/7] optimize test case --- .../common/logsfilter/EventParserTest.java | 3 +++ .../test/java/org/tron/core/WalletTest.java | 6 ++++++ .../java/org/tron/core/db/ManagerTest.java | 21 +++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/framework/src/test/java/org/tron/common/logsfilter/EventParserTest.java b/framework/src/test/java/org/tron/common/logsfilter/EventParserTest.java index a9b06c0e039..b5339156d9a 100644 --- a/framework/src/test/java/org/tron/common/logsfilter/EventParserTest.java +++ b/framework/src/test/java/org/tron/common/logsfilter/EventParserTest.java @@ -11,6 +11,7 @@ import org.tron.common.crypto.Hash; import org.tron.common.runtime.TvmTestUtils; import org.tron.common.utils.ByteArray; +import org.tron.common.utils.WalletUtil; import org.tron.core.Wallet; import org.tron.core.vm.LogInfoTriggerParser; import org.tron.protos.contract.SmartContractOuterClass.SmartContract.ABI; @@ -57,6 +58,8 @@ public synchronized void testEventParser() { + "000000000"; ABI abi = TvmTestUtils.jsonStr2Abi(abiStr); + Assert.assertFalse(WalletUtil.isConstant(abi, new byte[3])); + byte[] data = ByteArray.fromHexString(dataStr); List topicList = new LinkedList<>(); topicList.add(Hash.sha3(eventSign.getBytes())); diff --git a/framework/src/test/java/org/tron/core/WalletTest.java b/framework/src/test/java/org/tron/core/WalletTest.java index 1d8ae810dfd..bfbbbbf8e48 100644 --- a/framework/src/test/java/org/tron/core/WalletTest.java +++ b/framework/src/test/java/org/tron/core/WalletTest.java @@ -755,6 +755,12 @@ public void testGetDelegatedResourceV2() { Protocol.Account account = Protocol.Account.newBuilder() .setAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS))).build(); + + AccountCapsule accountCapsule = dbManager.getAccountStore() + .get(ByteArray.fromHexString(OWNER_ADDRESS)); + accountCapsule.addAssetV2("testv2".getBytes(), 1L); + dbManager.getAccountStore().put(accountCapsule.createDbKey(), accountCapsule); + wallet.getAccount(account); wallet.getProposalList(); wallet.getWitnessList(); diff --git a/framework/src/test/java/org/tron/core/db/ManagerTest.java b/framework/src/test/java/org/tron/core/db/ManagerTest.java index b7b29f49930..5c4f10dc738 100755 --- a/framework/src/test/java/org/tron/core/db/ManagerTest.java +++ b/framework/src/test/java/org/tron/core/db/ManagerTest.java @@ -938,4 +938,25 @@ private BlockCapsule createTestBlockCapsuleError(long time, blockCapsule.sign(ByteArray.fromHexString(addressToProvateKeys.get(witnessAddress))); return blockCapsule; } + + @Test + public void testExpireTransaction() { + TransferContract tc = + TransferContract.newBuilder() + .setAmount(10) + .setOwnerAddress(ByteString.copyFromUtf8("aaa")) + .setToAddress(ByteString.copyFromUtf8("bbb")) + .build(); + TransactionCapsule trx = new TransactionCapsule(tc, ContractType.TransferContract); + long latestBlockTime = dbManager.getDynamicPropertiesStore().getLatestBlockHeaderTimestamp(); + trx.setExpiration(latestBlockTime - 100); + try { + dbManager.validateCommon(trx); + Assert.fail(); + } catch (TransactionExpirationException e) { + Assert.assertTrue(true); + } catch (TooBigTransactionException e) { + Assert.fail(); + } + } } From cd9e5347562b7369fa7d1a060b2160ee9b968e8b Mon Sep 17 00:00:00 2001 From: jiangyuanshu <317787106@qq.com> Date: Wed, 19 Apr 2023 16:50:51 +0800 Subject: [PATCH 7/7] fix(net):descrease memory cost of test cases --- framework/build.gradle | 4 + .../vm/BandWidthRuntimeOutOfTimeTest.java | 2 + ...andWidthRuntimeOutOfTimeWithCheckTest.java | 2 + .../tron/core/db/TransactionExpireTest.java | 89 +++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 framework/src/test/java/org/tron/core/db/TransactionExpireTest.java diff --git a/framework/build.gradle b/framework/build.gradle index f4f5134bf6e..85d01ed11b0 100644 --- a/framework/build.gradle +++ b/framework/build.gradle @@ -151,6 +151,10 @@ test { exclude 'org/tron/common/runtime/vm/WithdrawRewardTest.class' } maxHeapSize = "1024m" + doFirst { + forkEvery = 100 + jvmArgs "-XX:MetaspaceSize=128m","-XX:MaxMetaspaceSize=256m", "-XX:+UseG1GC" + } } task stest(type: Test) { diff --git a/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeTest.java b/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeTest.java index f8a4f1ef0bb..bb751d56941 100644 --- a/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeTest.java +++ b/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeTest.java @@ -133,6 +133,8 @@ public static void destroy() { Args.clearParam(); context.destroy(); FileUtil.deleteDir(new File(dbPath)); + FileUtil.deleteDir(new File(dbDirectory)); + FileUtil.deleteDir(new File(indexDirectory)); } @Test diff --git a/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeWithCheckTest.java b/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeWithCheckTest.java index 68fa9e12fa7..5d015aa9356 100644 --- a/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeWithCheckTest.java +++ b/framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeWithCheckTest.java @@ -135,6 +135,8 @@ public static void destroy() { Args.clearParam(); context.destroy(); FileUtil.deleteDir(new File(dbPath)); + FileUtil.deleteDir(new File(dbDirectory)); + FileUtil.deleteDir(new File(indexDirectory)); } @Test diff --git a/framework/src/test/java/org/tron/core/db/TransactionExpireTest.java b/framework/src/test/java/org/tron/core/db/TransactionExpireTest.java new file mode 100644 index 00000000000..26b1e7e7bc1 --- /dev/null +++ b/framework/src/test/java/org/tron/core/db/TransactionExpireTest.java @@ -0,0 +1,89 @@ +package org.tron.core.db; + +import com.google.protobuf.ByteString; +import java.io.File; +import lombok.extern.slf4j.Slf4j; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.tron.api.GrpcAPI; +import org.tron.api.GrpcAPI.Return.response_code; +import org.tron.common.application.TronApplicationContext; +import org.tron.common.parameter.CommonParameter; +import org.tron.common.utils.ByteArray; +import org.tron.common.utils.FileUtil; +import org.tron.common.utils.Sha256Hash; +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.config.DefaultConfig; +import org.tron.core.config.args.Args; +import org.tron.protos.Protocol.Transaction.Contract.ContractType; +import org.tron.protos.contract.BalanceContract.TransferContract; + +@Slf4j +public class TransactionExpireTest { + + private String dbPath = "output_expire_test"; + private TronApplicationContext context; + private Wallet wallet; + private Manager dbManager; + private BlockCapsule blockCapsule; + + @Before + public void init() { + Args.setParam(new String[] {"--output-directory", dbPath}, Constant.TEST_CONF); + CommonParameter.PARAMETER.setMinEffectiveConnection(0); + + context = new TronApplicationContext(DefaultConfig.class); + wallet = context.getBean(Wallet.class); + dbManager = context.getBean(Manager.class); + + blockCapsule = new BlockCapsule( + 1, + Sha256Hash.wrap(ByteString.copyFrom( + ByteArray.fromHexString( + "0304f784e4e7bae517bcab94c3e0c9214fb4ac7ff9d7d5a937d1f40031f87b81"))), + 1, + ByteString.copyFromUtf8("testAddress")); + dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderNumber(blockCapsule.getNum()); + dbManager.getDynamicPropertiesStore() + .saveLatestBlockHeaderTimestamp(blockCapsule.getTimeStamp()); + dbManager.updateRecentBlock(blockCapsule); + } + + @After + public void removeDb() { + Args.clearParam(); + context.destroy(); + if (FileUtil.deleteDir(new File(dbPath))) { + logger.info("Release resources successful."); + } else { + logger.info("Release resources failure."); + } + } + + @Test + public void testExpireTransaction() { + TransferContract transferContract = TransferContract.newBuilder() + .setAmount(1L) + .setOwnerAddress(ByteString.copyFrom(Args.getLocalWitnesses() + .getWitnessAccountAddress(CommonParameter.getInstance().isECKeyCryptoEngine()))) + .setToAddress(ByteString.copyFrom(ByteArray.fromHexString( + (Wallet.getAddressPreFixString() + "A389132D6639FBDA4FBC8B659264E6B7C90DB086")))) + .build(); + TransactionCapsule transactionCapsule = + new TransactionCapsule(transferContract, ContractType.TransferContract); + transactionCapsule.setReference(blockCapsule.getNum(), blockCapsule.getBlockId().getBytes()); + Assert.assertEquals(1, blockCapsule.getTimeStamp()); + + long blockTimeStamp = blockCapsule.getTimeStamp(); + transactionCapsule.setExpiration(blockTimeStamp - 1); + transactionCapsule.sign(ByteArray.fromHexString(Args.getLocalWitnesses().getPrivateKey())); + + GrpcAPI.Return result = wallet.broadcastTransaction(transactionCapsule.getInstance()); + Assert.assertEquals(response_code.TRANSACTION_EXPIRATION_ERROR, result.getCode()); + } +}