Skip to content

Commit f9ba0e0

Browse files
committed
Merge remote-tracking branch 'origin/feature/unit_test' into release_v4.7.2
Signed-off-by: liuxincheng <lxcmyf@qq.com>
2 parents 4b3aeb5 + 1ee8957 commit f9ba0e0

130 files changed

Lines changed: 1735 additions & 4573 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

chainbase/src/main/java/org/tron/core/db2/core/SnapshotManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.Map;
1717
import java.util.Objects;
1818
import java.util.concurrent.ExecutionException;
19+
import java.util.concurrent.ExecutorService;
1920
import java.util.concurrent.Executors;
2021
import java.util.concurrent.Future;
2122
import java.util.concurrent.ScheduledExecutorService;
@@ -122,6 +123,7 @@ public void close() {
122123
exitThread.interrupt();
123124
// help GC
124125
exitThread = null;
126+
flushServices.values().forEach(ExecutorService::shutdown);
125127
} catch (Exception e) {
126128
logger.warn("exitThread interrupt error", e);
127129
}

framework/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ dependencies {
8686
compile "io.vavr:vavr:0.9.2"
8787
compile group: 'org.pf4j', name: 'pf4j', version: '2.5.0'
8888

89+
testImplementation group: 'org.springframework', name: 'spring-test', version: '5.2.0.RELEASE'
90+
8991
compile group: 'org.zeromq', name: 'jeromq', version: '0.5.0'
9092
compile project(":chainbase")
9193
compile project(":protocol")

framework/propPath

Whitespace-only changes.

framework/src/main/java/org/tron/core/capsule/utils/RLP.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public static long decodeLong(byte[] data, int index) {
204204
return value;
205205
}
206206

207-
private static String decodeStringItem(byte[] data, int index) {
207+
public static String decodeStringItem(byte[] data, int index) {
208208

209209
final byte[] valueBytes = decodeItemBytes(data, index);
210210

@@ -229,12 +229,12 @@ public static BigInteger decodeBigInteger(byte[] data, int index) {
229229
}
230230
}
231231

232-
private static byte[] decodeByteArray(byte[] data, int index) {
232+
public static byte[] decodeByteArray(byte[] data, int index) {
233233

234234
return decodeItemBytes(data, index);
235235
}
236236

237-
private static int nextItemLength(byte[] data, int index) {
237+
public static int nextItemLength(byte[] data, int index) {
238238

239239
if (index >= data.length) {
240240
return -1;

framework/src/main/java/org/tron/core/db/Manager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,6 +1912,7 @@ public NullifierStore getNullifierStore() {
19121912
public void closeAllStore() {
19131913
logger.info("******** Begin to close db. ********");
19141914
chainBaseManager.closeAllStore();
1915+
validateSignService.shutdown();
19151916
logger.info("******** End to close db. ********");
19161917
}
19171918

framework/src/main/java/org/tron/core/net/messagehandler/TransactionsMsgHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public void init() {
5353
}
5454

5555
public void close() {
56+
trxHandlePool.shutdown();
5657
smartContractExecutor.shutdown();
5758
}
5859

framework/src/main/java/org/tron/core/net/service/nodepersist/NodePersistService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ public class NodePersistService {
2727

2828
private ChainBaseManager chainBaseManager = ChainBaseManager.getInstance();
2929

30-
private Timer nodePersistTaskTimer = new Timer("NodePersistTaskTimer");
30+
private Timer nodePersistTaskTimer;
3131

3232
public void init() {
33+
nodePersistTaskTimer = new Timer("NodePersistTaskTimer");
3334
if (isNodePersist) {
3435
nodePersistTaskTimer.scheduleAtFixedRate(new TimerTask() {
3536
@Override
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package org.tron.common;
2+
3+
import com.google.protobuf.ByteString;
4+
import java.io.File;
5+
import javax.annotation.Resource;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.apache.commons.lang3.StringUtils;
8+
import org.junit.AfterClass;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.test.annotation.DirtiesContext;
11+
import org.springframework.test.context.ContextConfiguration;
12+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
13+
import org.tron.common.crypto.ECKey;
14+
import org.tron.common.parameter.CommonParameter;
15+
import org.tron.common.utils.FileUtil;
16+
import org.tron.common.utils.Sha256Hash;
17+
import org.tron.consensus.base.Param;
18+
import org.tron.core.ChainBaseManager;
19+
import org.tron.core.capsule.BlockCapsule;
20+
import org.tron.core.config.DefaultConfig;
21+
import org.tron.core.config.args.Args;
22+
import org.tron.core.db.Manager;
23+
import org.tron.protos.Protocol;
24+
25+
@Slf4j
26+
@RunWith(SpringJUnit4ClassRunner.class)
27+
@ContextConfiguration(classes = {DefaultConfig.class})
28+
@DirtiesContext
29+
public abstract class BaseTest {
30+
31+
protected static String dbPath;
32+
@Resource
33+
protected Manager dbManager;
34+
@Resource
35+
protected ChainBaseManager chainBaseManager;
36+
37+
@AfterClass
38+
public static void destroy() {
39+
Args.clearParam();
40+
if (StringUtils.isNotEmpty(dbPath) && FileUtil.deleteDir(new File(dbPath))) {
41+
logger.info("Release resources successful.");
42+
} else {
43+
logger.info("Release resources failure.");
44+
}
45+
}
46+
47+
public Protocol.Block getSignedBlock(ByteString witness, long time, byte[] privateKey) {
48+
long blockTime = System.currentTimeMillis() / 3000 * 3000;
49+
if (time != 0) {
50+
blockTime = time;
51+
} else {
52+
if (chainBaseManager.getHeadBlockId().getNum() != 0) {
53+
blockTime = chainBaseManager.getHeadBlockTimeStamp() + 3000;
54+
}
55+
}
56+
Param param = Param.getInstance();
57+
Param.Miner miner = param.new Miner(privateKey, witness, witness);
58+
BlockCapsule blockCapsule = dbManager
59+
.generateBlock(miner, time, System.currentTimeMillis() + 1000);
60+
Protocol.Block block = blockCapsule.getInstance();
61+
62+
Protocol.BlockHeader.raw raw = block.getBlockHeader().getRawData().toBuilder()
63+
.setParentHash(ByteString
64+
.copyFrom(chainBaseManager.getDynamicPropertiesStore()
65+
.getLatestBlockHeaderHash().getBytes()))
66+
.setNumber(chainBaseManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber() + 1)
67+
.setTimestamp(blockTime)
68+
.setWitnessAddress(witness)
69+
.build();
70+
71+
ECKey ecKey = ECKey.fromPrivate(privateKey);
72+
assert ecKey != null;
73+
ECKey.ECDSASignature signature = ecKey.sign(Sha256Hash.of(CommonParameter
74+
.getInstance().isECKeyCryptoEngine(), raw.toByteArray()).getBytes());
75+
ByteString sign = ByteString.copyFrom(signature.toByteArray());
76+
77+
Protocol.BlockHeader blockHeader = block.getBlockHeader().toBuilder()
78+
.setRawData(raw)
79+
.setWitnessSignature(sign)
80+
.build();
81+
82+
return block.toBuilder().setBlockHeader(blockHeader).build();
83+
}
84+
}

framework/src/test/java/org/tron/common/runtime/InheritanceTest.java

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
package org.tron.common.runtime;
22

3-
import java.io.File;
43
import lombok.extern.slf4j.Slf4j;
54
import org.bouncycastle.util.encoders.Hex;
6-
import org.junit.AfterClass;
7-
import org.junit.BeforeClass;
5+
import org.junit.Before;
86
import org.junit.Test;
97
import org.testng.Assert;
10-
import org.tron.common.application.Application;
11-
import org.tron.common.application.ApplicationFactory;
12-
import org.tron.common.application.TronApplicationContext;
13-
import org.tron.common.utils.FileUtil;
8+
import org.tron.common.BaseTest;
149
import org.tron.core.Constant;
1510
import org.tron.core.Wallet;
16-
import org.tron.core.config.DefaultConfig;
1711
import org.tron.core.config.args.Args;
18-
import org.tron.core.db.Manager;
1912
import org.tron.core.exception.ContractExeException;
2013
import org.tron.core.exception.ContractValidateException;
2114
import org.tron.core.exception.ReceiptCheckErrException;
@@ -25,54 +18,31 @@
2518
import org.tron.protos.Protocol.AccountType;
2619

2720
@Slf4j
28-
public class InheritanceTest {
21+
public class InheritanceTest extends BaseTest {
2922

30-
private static final String dbPath = "output_InheritanceTest";
3123
private static final String OWNER_ADDRESS;
32-
private static Runtime runtime;
33-
private static Manager dbManager;
34-
private static TronApplicationContext context;
35-
private static Application appT;
36-
private static RepositoryImpl repository;
24+
private RepositoryImpl repository;
3725

3826
static {
27+
dbPath = "output_InheritanceTest";
3928
Args.setParam(new String[]{"--output-directory", dbPath, "--debug"}, Constant.TEST_CONF);
40-
context = new TronApplicationContext(DefaultConfig.class);
41-
appT = ApplicationFactory.create(context);
4229
OWNER_ADDRESS = Wallet.getAddressPreFixString() + "abd4b9367799eaa3197fecb144eb71de1e049abc";
4330
}
4431

4532
/**
4633
* Init data.
4734
*/
48-
@BeforeClass
49-
public static void init() {
50-
dbManager = context.getBean(Manager.class);
35+
@Before
36+
public void init() {
5137
repository = RepositoryImpl.createRoot(StoreFactory.getInstance());
5238
repository.createAccount(Hex.decode(OWNER_ADDRESS), AccountType.Normal);
5339
repository.addBalance(Hex.decode(OWNER_ADDRESS), 100000000);
5440
}
5541

56-
/**
57-
* Release resources.
58-
*/
59-
@AfterClass
60-
public static void destroy() {
61-
Args.clearParam();
62-
context.destroy();
63-
if (FileUtil.deleteDir(new File(dbPath))) {
64-
logger.info("Release resources successful.");
65-
} else {
66-
logger.info("Release resources failure.");
67-
}
68-
}
69-
7042
/**
7143
* pragma solidity ^0.4.19;
72-
*
7344
* contract foo { uint256 public id=10; function getNumber() returns (uint256){return 100;}
7445
* function getName() returns (string){ return "foo"; } }
75-
*
7646
* contract bar is foo { function getName() returns (string) { return "bar"; } function getId()
7747
* returns(uint256){return id;} }
7848
*/
@@ -119,7 +89,7 @@ public void inheritanceTest()
11989

12090
/* ========================== CALL getName() return child value ============================= */
12191
byte[] triggerData1 = TvmTestUtils.parseAbi("getName()", "");
122-
runtime = TvmTestUtils
92+
Runtime runtime = TvmTestUtils
12393
.triggerContractWholeProcessReturnContractAddress(callerAddress, contractAddress,
12494
triggerData1, 0, 1000000, repository, null);
12595

framework/src/test/java/org/tron/common/runtime/InternalTransactionComplexTest.java

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
package org.tron.common.runtime;
22

3-
import java.io.File;
43
import lombok.extern.slf4j.Slf4j;
54
import org.bouncycastle.util.encoders.Hex;
6-
import org.junit.AfterClass;
7-
import org.junit.BeforeClass;
5+
import org.junit.Before;
86
import org.junit.Test;
97
import org.testng.Assert;
10-
import org.tron.common.application.Application;
11-
import org.tron.common.application.ApplicationFactory;
12-
import org.tron.common.application.TronApplicationContext;
8+
import org.tron.common.BaseTest;
139
import org.tron.common.runtime.vm.DataWord;
14-
import org.tron.common.utils.FileUtil;
1510
import org.tron.core.Constant;
1611
import org.tron.core.Wallet;
17-
import org.tron.core.config.DefaultConfig;
1812
import org.tron.core.config.args.Args;
19-
import org.tron.core.db.Manager;
2013
import org.tron.core.exception.ContractExeException;
2114
import org.tron.core.exception.ContractValidateException;
2215
import org.tron.core.exception.ReceiptCheckErrException;
@@ -26,55 +19,33 @@
2619
import org.tron.protos.Protocol.AccountType;
2720

2821
@Slf4j
29-
public class InternalTransactionComplexTest {
22+
public class InternalTransactionComplexTest extends BaseTest {
3023

31-
private static final String dbPath = "output_InternalTransactionComplexTest";
3224
private static final String OWNER_ADDRESS;
3325
private static Runtime runtime;
34-
private static Manager dbManager;
35-
private static TronApplicationContext context;
36-
private static Application appT;
3726
private static RepositoryImpl repository;
3827

3928
static {
29+
dbPath = "output_InternalTransactionComplexTest";
4030
Args.setParam(new String[]{"--output-directory", dbPath, "--debug", "--support-constant"},
4131
Constant.TEST_CONF);
42-
context = new TronApplicationContext(DefaultConfig.class);
43-
appT = ApplicationFactory.create(context);
4432
OWNER_ADDRESS = Wallet.getAddressPreFixString() + "abd4b9367799eaa3197fecb144eb71de1e049abc";
4533
}
4634

4735
/**
4836
* Init data.
4937
*/
50-
@BeforeClass
51-
public static void init() {
52-
dbManager = context.getBean(Manager.class);
38+
@Before
39+
public void init() {
5340
repository = RepositoryImpl.createRoot(StoreFactory.getInstance());
5441
repository.createAccount(Hex.decode(OWNER_ADDRESS), AccountType.Normal);
5542
repository.addBalance(Hex.decode(OWNER_ADDRESS), 100000000);
5643
}
5744

58-
/**
59-
* Release resources.
60-
*/
61-
@AfterClass
62-
public static void destroy() {
63-
Args.clearParam();
64-
context.destroy();
65-
if (FileUtil.deleteDir(new File(dbPath))) {
66-
logger.info("Release resources successful.");
67-
} else {
68-
logger.info("Release resources failure.");
69-
}
70-
}
71-
7245
/**
7346
* pragma solidity 0.4.24;
74-
*
7547
* // this is to test wither the TVM is returning vars from one contract calling another //
7648
* contract's functions.
77-
*
7849
* contract callerContract { // lets set up our instance of the new contract calledContract
7950
* CALLED_INSTANCE; // lets set the contract instance address in the constructor
8051
* constructor(address _addr) public { CALLED_INSTANCE = calledContract(_addr); } // lets create a
@@ -85,7 +56,6 @@ public static void destroy() {
8556
* in to temp vars (bool _bool, uint256 _uint, bytes32 _bytes32) = CALLED_INSTANCE.testReturns();
8657
* // lets write those temp vars to state testCallbackReturns_.someBool = _bool;
8758
* testCallbackReturns_.someUint = _uint; testCallbackReturns_.someBytes32 = _bytes32; } }
88-
*
8959
* contract calledContract { function testReturns() external pure returns(bool, uint256, bytes32)
9060
* { return(true, 314159, 0x123456); } }
9161
*/

0 commit comments

Comments
 (0)