Skip to content

Commit c03451d

Browse files
authored
Merge pull request #5133 from 317787106/hotfix/stable_testcase
fix(net): optimze test cases to make coverage stable
2 parents cc9a944 + 7a8ceee commit c03451d

13 files changed

Lines changed: 221 additions & 44 deletions

File tree

framework/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ test {
153153
exclude 'org/tron/common/runtime/vm/WithdrawRewardTest.class'
154154
}
155155
maxHeapSize = "1024m"
156+
doFirst {
157+
forkEvery = 100
158+
jvmArgs "-XX:MetaspaceSize=128m","-XX:MaxMetaspaceSize=256m", "-XX:+UseG1GC"
159+
}
156160
}
157161

158162
task stest(type: Test) {

framework/src/main/java/org/tron/core/net/TronNetService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,14 @@ public static boolean hasIpv4Stack(Set<String> ipSet) {
148148
}
149149

150150
private P2pConfig getConfig() {
151+
P2pConfig config = new P2pConfig();
152+
return updateConfig(config);
153+
}
154+
155+
private P2pConfig updateConfig(P2pConfig config) {
151156
List<InetSocketAddress> seeds = parameter.getSeedNode().getAddressList();
152157
seeds.addAll(nodePersistService.dbRead());
153158
logger.debug("Seed InetSocketAddress: {}", seeds);
154-
P2pConfig config = new P2pConfig();
155159
config.getSeedNodes().addAll(seeds);
156160
config.getActiveNodes().addAll(parameter.getActiveNodes());
157161
config.getTrustNodes().addAll(parameter.getPassiveNodes());

framework/src/main/resources/config.conf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,19 @@ node {
202202
solidityPort = 8091
203203
}
204204

205+
# use your ipv6 address for node discovery and tcp connection, default false
206+
enableIpv6 = false
207+
208+
# if your node's highest block num is below than all your pees', try to acquire new connection. default false
209+
effectiveCheckEnable = false
210+
211+
dns {
212+
# dns urls to get nodes, url format tree://{pubkey}@{domain}, default empty
213+
treeUrls = [
214+
#"tree://APFGGTFOBVE2ZNAB3CSMNNX6RRK3ODIRLP2AA5U4YFAA6MSYZUYTQ@nodes1.example.org",
215+
]
216+
}
217+
205218
rpc {
206219
port = 50051
207220
#solidityPort = 50061

framework/src/test/java/org/tron/common/config/args/ArgsTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@
1111

1212
public class ArgsTest {
1313

14+
private static final String dbPath = "output_arg_test";
15+
1416
@Before
1517
public void init() {
16-
Args.setParam(new String[]{"--output-directory", "output-directory", "--p2p-disable", "true",
18+
Args.setParam(new String[]{"--output-directory", dbPath, "--p2p-disable", "true",
1719
"--debug"}, Constant.TEST_CONF);
1820
}
1921

2022
@After
2123
public void destroy() {
2224
Args.clearParam();
23-
FileUtil.deleteDir(new File("output-directory"));
25+
FileUtil.deleteDir(new File(dbPath));
2426
}
2527

2628
@Test

framework/src/test/java/org/tron/common/logsfilter/EventParserTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.tron.common.crypto.Hash;
1212
import org.tron.common.runtime.TvmTestUtils;
1313
import org.tron.common.utils.ByteArray;
14+
import org.tron.common.utils.WalletUtil;
1415
import org.tron.core.Wallet;
1516
import org.tron.core.vm.LogInfoTriggerParser;
1617
import org.tron.protos.contract.SmartContractOuterClass.SmartContract.ABI;
@@ -57,6 +58,8 @@ public synchronized void testEventParser() {
5758
+ "000000000";
5859
ABI abi = TvmTestUtils.jsonStr2Abi(abiStr);
5960

61+
Assert.assertFalse(WalletUtil.isConstant(abi, new byte[3]));
62+
6063
byte[] data = ByteArray.fromHexString(dataStr);
6164
List<byte[]> topicList = new LinkedList<>();
6265
topicList.add(Hash.sha3(eventSign.getBytes()));

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,12 @@ public void testGetDelegatedResourceV2() {
728728

729729
Protocol.Account account = Protocol.Account.newBuilder()
730730
.setAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS))).build();
731+
732+
AccountCapsule accountCapsule = dbManager.getAccountStore()
733+
.get(ByteArray.fromHexString(OWNER_ADDRESS));
734+
accountCapsule.addAssetV2("testv2".getBytes(), 1L);
735+
dbManager.getAccountStore().put(accountCapsule.createDbKey(), accountCapsule);
736+
731737
wallet.getAccount(account);
732738
wallet.getProposalList();
733739
wallet.getWitnessList();

framework/src/test/java/org/tron/core/actuator/utils/ProposalUtilTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ public void validateCheck() {
328328
Assert.assertEquals("Bad chain parameter value, valid range is [0, 1_000_000_000_000L]",
329329
e.getMessage());
330330
}
331+
332+
forkUtils.getManager().getDynamicPropertiesStore()
333+
.statsByVersion(ForkBlockVersionEnum.ENERGY_LIMIT.getValue(), stats);
334+
forkUtils.reset();
331335
}
332336

333337
@Test

framework/src/test/java/org/tron/core/db/ManagerTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,13 @@ public void pushBlock() {
227227
}
228228
}
229229

230+
try {
231+
chainManager.getBlockIdByNum(-1);
232+
Assert.fail();
233+
} catch (ItemNotFoundException e) {
234+
Assert.assertTrue(true);
235+
}
236+
230237
Assert.assertTrue("hasBlocks is error", chainManager.hasBlocks());
231238
}
232239

@@ -933,4 +940,25 @@ private BlockCapsule createTestBlockCapsuleError(long time,
933940
blockCapsule.sign(ByteArray.fromHexString(addressToProvateKeys.get(witnessAddress)));
934941
return blockCapsule;
935942
}
943+
944+
@Test
945+
public void testExpireTransaction() {
946+
TransferContract tc =
947+
TransferContract.newBuilder()
948+
.setAmount(10)
949+
.setOwnerAddress(ByteString.copyFromUtf8("aaa"))
950+
.setToAddress(ByteString.copyFromUtf8("bbb"))
951+
.build();
952+
TransactionCapsule trx = new TransactionCapsule(tc, ContractType.TransferContract);
953+
long latestBlockTime = dbManager.getDynamicPropertiesStore().getLatestBlockHeaderTimestamp();
954+
trx.setExpiration(latestBlockTime - 100);
955+
try {
956+
dbManager.validateCommon(trx);
957+
Assert.fail();
958+
} catch (TransactionExpirationException e) {
959+
Assert.assertTrue(true);
960+
} catch (TooBigTransactionException e) {
961+
Assert.fail();
962+
}
963+
}
936964
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package org.tron.core.db;
2+
3+
import com.google.protobuf.ByteString;
4+
import java.io.File;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.junit.After;
7+
import org.junit.Assert;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import org.tron.api.GrpcAPI;
11+
import org.tron.api.GrpcAPI.Return.response_code;
12+
import org.tron.common.application.TronApplicationContext;
13+
import org.tron.common.parameter.CommonParameter;
14+
import org.tron.common.utils.ByteArray;
15+
import org.tron.common.utils.FileUtil;
16+
import org.tron.common.utils.Sha256Hash;
17+
import org.tron.core.Constant;
18+
import org.tron.core.Wallet;
19+
import org.tron.core.capsule.BlockCapsule;
20+
import org.tron.core.capsule.TransactionCapsule;
21+
import org.tron.core.config.DefaultConfig;
22+
import org.tron.core.config.args.Args;
23+
import org.tron.protos.Protocol.Transaction.Contract.ContractType;
24+
import org.tron.protos.contract.BalanceContract.TransferContract;
25+
26+
@Slf4j
27+
public class TransactionExpireTest {
28+
29+
private String dbPath = "output_expire_test";
30+
private TronApplicationContext context;
31+
private Wallet wallet;
32+
private Manager dbManager;
33+
private BlockCapsule blockCapsule;
34+
35+
@Before
36+
public void init() {
37+
Args.setParam(new String[] {"--output-directory", dbPath}, Constant.TEST_CONF);
38+
CommonParameter.PARAMETER.setMinEffectiveConnection(0);
39+
40+
context = new TronApplicationContext(DefaultConfig.class);
41+
wallet = context.getBean(Wallet.class);
42+
dbManager = context.getBean(Manager.class);
43+
44+
blockCapsule = new BlockCapsule(
45+
1,
46+
Sha256Hash.wrap(ByteString.copyFrom(
47+
ByteArray.fromHexString(
48+
"0304f784e4e7bae517bcab94c3e0c9214fb4ac7ff9d7d5a937d1f40031f87b81"))),
49+
1,
50+
ByteString.copyFromUtf8("testAddress"));
51+
dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderNumber(blockCapsule.getNum());
52+
dbManager.getDynamicPropertiesStore()
53+
.saveLatestBlockHeaderTimestamp(blockCapsule.getTimeStamp());
54+
dbManager.updateRecentBlock(blockCapsule);
55+
}
56+
57+
@After
58+
public void removeDb() {
59+
Args.clearParam();
60+
context.destroy();
61+
if (FileUtil.deleteDir(new File(dbPath))) {
62+
logger.info("Release resources successful.");
63+
} else {
64+
logger.info("Release resources failure.");
65+
}
66+
}
67+
68+
@Test
69+
public void testExpireTransaction() {
70+
TransferContract transferContract = TransferContract.newBuilder()
71+
.setAmount(1L)
72+
.setOwnerAddress(ByteString.copyFrom(Args.getLocalWitnesses()
73+
.getWitnessAccountAddress(CommonParameter.getInstance().isECKeyCryptoEngine())))
74+
.setToAddress(ByteString.copyFrom(ByteArray.fromHexString(
75+
(Wallet.getAddressPreFixString() + "A389132D6639FBDA4FBC8B659264E6B7C90DB086"))))
76+
.build();
77+
TransactionCapsule transactionCapsule =
78+
new TransactionCapsule(transferContract, ContractType.TransferContract);
79+
transactionCapsule.setReference(blockCapsule.getNum(), blockCapsule.getBlockId().getBytes());
80+
Assert.assertEquals(1, blockCapsule.getTimeStamp());
81+
82+
long blockTimeStamp = blockCapsule.getTimeStamp();
83+
transactionCapsule.setExpiration(blockTimeStamp - 1);
84+
transactionCapsule.sign(ByteArray.fromHexString(Args.getLocalWitnesses().getPrivateKey()));
85+
86+
GrpcAPI.Return result = wallet.broadcastTransaction(transactionCapsule.getInstance());
87+
Assert.assertEquals(response_code.TRANSACTION_EXPIRATION_ERROR, result.getCode());
88+
}
89+
}

framework/src/test/java/org/tron/core/net/BaseNet.java

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import java.util.concurrent.Executors;
2020
import java.util.concurrent.TimeUnit;
2121
import lombok.extern.slf4j.Slf4j;
22-
import org.junit.After;
23-
import org.junit.Before;
22+
import org.junit.AfterClass;
23+
import org.junit.BeforeClass;
2424
import org.tron.common.application.Application;
2525
import org.tron.common.application.ApplicationFactory;
2626
import org.tron.common.application.TronApplicationContext;
@@ -34,20 +34,20 @@
3434
import org.tron.core.services.RpcApiService;
3535

3636
@Slf4j
37-
public abstract class BaseNet {
37+
public class BaseNet {
3838

3939
private static String dbPath = "output-net";
4040
private static String dbDirectory = "net-database";
4141
private static String indexDirectory = "net-index";
4242
private static int port = 10000;
4343

44-
protected TronApplicationContext context;
44+
protected static TronApplicationContext context;
4545

46-
private RpcApiService rpcApiService;
47-
private Application appT;
48-
private TronNetDelegate tronNetDelegate;
46+
private static RpcApiService rpcApiService;
47+
private static Application appT;
48+
private static TronNetDelegate tronNetDelegate;
4949

50-
private ExecutorService executorService = Executors.newFixedThreadPool(1);
50+
private static ExecutorService executorService = Executors.newFixedThreadPool(1);
5151

5252
public static Channel connect(ByteToMessageDecoder decoder) throws InterruptedException {
5353
NioEventLoopGroup group = new NioEventLoopGroup(1);
@@ -73,49 +73,51 @@ protected void initChannel(Channel ch) throws Exception {
7373
return b.connect(Constant.LOCAL_HOST, port).sync().channel();
7474
}
7575

76-
@Before
77-
public void init() throws Exception {
78-
executorService.execute(new Runnable() {
79-
@Override
80-
public void run() {
81-
logger.info("Full node running.");
82-
Args.setParam(
83-
new String[]{
84-
"--output-directory", dbPath,
85-
"--storage-db-directory", dbDirectory,
86-
"--storage-index-directory", indexDirectory
87-
},
88-
"config.conf"
89-
);
90-
CommonParameter parameter = Args.getInstance();
91-
parameter.setNodeListenPort(port);
92-
parameter.getSeedNode().getAddressList().clear();
93-
parameter.setNodeExternalIp(Constant.LOCAL_HOST);
94-
context = new TronApplicationContext(DefaultConfig.class);
95-
appT = ApplicationFactory.create(context);
96-
rpcApiService = context.getBean(RpcApiService.class);
97-
appT.addService(rpcApiService);
98-
appT.initServices(parameter);
99-
appT.startServices();
100-
appT.startup();
101-
tronNetDelegate = context.getBean(TronNetDelegate.class);
102-
rpcApiService.blockUntilShutdown();
76+
@BeforeClass
77+
public static void init() throws Exception {
78+
executorService.execute(() -> {
79+
logger.info("Full node running.");
80+
Args.setParam(
81+
new String[]{
82+
"--output-directory", dbPath,
83+
"--storage-db-directory", dbDirectory,
84+
"--storage-index-directory", indexDirectory
85+
},
86+
"config.conf"
87+
);
88+
CommonParameter parameter = Args.getInstance();
89+
parameter.setNodeListenPort(port);
90+
parameter.getSeedNode().getAddressList().clear();
91+
parameter.setNodeExternalIp(Constant.LOCAL_HOST);
92+
context = new TronApplicationContext(DefaultConfig.class);
93+
appT = ApplicationFactory.create(context);
94+
rpcApiService = context.getBean(RpcApiService.class);
95+
appT.addService(rpcApiService);
96+
appT.initServices(parameter);
97+
appT.startServices();
98+
appT.startup();
99+
try {
100+
Thread.sleep(2000);
101+
} catch (InterruptedException e) {
102+
//ignore
103103
}
104+
tronNetDelegate = context.getBean(TronNetDelegate.class);
105+
rpcApiService.blockUntilShutdown();
104106
});
105107
int tryTimes = 0;
106-
while (++tryTimes < 100 && tronNetDelegate == null) {
107-
Thread.sleep(3000);
108-
}
108+
do {
109+
Thread.sleep(3000); //coverage consumerInvToSpread,consumerInvToFetch in AdvService.init
110+
} while (++tryTimes < 100 && tronNetDelegate == null);
109111
}
110112

111-
@After
112-
public void destroy() {
113+
@AfterClass
114+
public static void destroy() {
113115
Collection<PeerConnection> peerConnections = ReflectUtils
114116
.invokeMethod(tronNetDelegate, "getActivePeer");
115117
for (PeerConnection peer : peerConnections) {
116118
peer.getChannel().close();
117119
}
118-
120+
Args.clearParam();
119121
context.destroy();
120122
FileUtil.deleteDir(new File(dbPath));
121123
}

0 commit comments

Comments
 (0)