Skip to content

Commit 8ca7085

Browse files
Merge pull request #6233 from halibobo1205/feat/services_init_opt
feat(service): exit when params init failed
2 parents da05a19 + c9b7fbc commit 8ca7085

31 files changed

+112
-75
lines changed

common/src/main/java/org/tron/core/exception/TronError.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public enum ErrCode {
4545
TRON_NET_SERVICE_INIT(1),
4646
ZCASH_INIT(1),
4747
LOG_LOAD(1),
48+
WITNESS_INIT(1),
49+
RATE_LIMITER_INIT(1),
4850
SOLID_NODE_INIT(0);
4951

5052
private final int code;

framework/src/main/java/org/tron/core/config/args/Args.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,8 @@ public static void setParam(final Config config) {
451451

452452
if (PARAMETER.isWitness()
453453
&& CollectionUtils.isEmpty(localWitnesses.getPrivateKeys())) {
454-
logger.warn("This is a witness node, but localWitnesses is null");
454+
throw new TronError("This is a witness node, but localWitnesses is null",
455+
TronError.ErrCode.WITNESS_INIT);
455456
}
456457

457458
if (config.hasPath(Constant.VM_SUPPORT_CONSTANT)) {
@@ -1142,7 +1143,7 @@ public static void setParam(final Config config) {
11421143
PARAMETER.shutdownBlockTime = new CronExpression(config.getString(
11431144
Constant.NODE_SHUTDOWN_BLOCK_TIME));
11441145
} catch (ParseException e) {
1145-
logger.error(e.getMessage(), e);
1146+
throw new TronError(e, TronError.ErrCode.AUTO_STOP_PARAMS);
11461147
}
11471148
}
11481149

framework/src/main/java/org/tron/core/services/http/RateLimiterServlet.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.tron.common.prometheus.MetricLabels;
1717
import org.tron.common.prometheus.Metrics;
1818
import org.tron.core.config.args.Args;
19+
import org.tron.core.exception.TronError;
1920
import org.tron.core.services.ratelimiter.GlobalRateLimiter;
2021
import org.tron.core.services.ratelimiter.RateLimiterContainer;
2122
import org.tron.core.services.ratelimiter.RuntimeData;
@@ -40,6 +41,7 @@ private void addRateContainer() {
4041
RateLimiterInitialization.HttpRateLimiterItem item = Args.getInstance()
4142
.getRateLimiterInitialization().getHttpMap().get(getClass().getSimpleName());
4243
boolean success = false;
44+
final String name = getClass().getSimpleName();
4345
if (item != null) {
4446
String cName = "";
4547
String params = "";
@@ -54,32 +56,35 @@ private void addRateContainer() {
5456
|| c == IPQPSRateLimiterAdapter.class) {
5557
constructor = c.getConstructor(String.class);
5658
obj = constructor.newInstance(params);
57-
container.add(KEY_PREFIX_HTTP, getClass().getSimpleName(), (IRateLimiter) obj);
59+
container.add(KEY_PREFIX_HTTP, name, (IRateLimiter) obj);
5860
} else {
5961
constructor = c.getConstructor();
6062
obj = constructor.newInstance(QpsStrategy.DEFAULT_QPS_PARAM);
61-
container.add(KEY_PREFIX_HTTP, getClass().getSimpleName(), (IRateLimiter) obj);
63+
container.add(KEY_PREFIX_HTTP, name, (IRateLimiter) obj);
6264
}
6365
success = true;
6466
} catch (Exception e) {
65-
logger.warn("failure to add the rate limiter strategy. servlet = {}, "
66-
+ "strategy name = {}, params = \"{}\".",
67-
getClass().getSimpleName(), cName, params);
67+
this.throwTronError(cName, params, name, e);
6868
}
6969
}
7070
if (!success) {
7171
// if the specific rate limiter strategy of servlet is not defined or fail to add,
7272
// then add a default Strategy.
7373
try {
7474
IRateLimiter rateLimiter = new DefaultBaseQqsAdapter(QpsStrategy.DEFAULT_QPS_PARAM);
75-
container.add(KEY_PREFIX_HTTP, getClass().getSimpleName(), rateLimiter);
75+
container.add(KEY_PREFIX_HTTP, name, rateLimiter);
7676
} catch (Exception e) {
77-
logger.warn("failure to add the default rate limiter strategy. servlet = {}.",
78-
getClass().getSimpleName());
77+
this.throwTronError("DefaultBaseQqsAdapter", QpsStrategy.DEFAULT_QPS_PARAM, name, e);
7978
}
8079
}
8180
}
8281

82+
private void throwTronError(String strategy, String params, String servlet, Exception e) {
83+
throw new TronError("failure to add the rate limiter strategy. servlet = " + servlet
84+
+ ", strategy name = " + strategy + ", params = \"" + params + "\".",
85+
e, TronError.ErrCode.RATE_LIMITER_INIT);
86+
}
87+
8388
@Override
8489
protected void service(HttpServletRequest req, HttpServletResponse resp)
8590
throws ServletException, IOException {

framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public class BandWidthRuntimeOutOfTimeTest extends BaseTest {
7373
"--output-directory", dbPath(),
7474
"--storage-db-directory", dbDirectory,
7575
"--storage-index-directory", indexDirectory,
76-
"-w",
7776
"--debug"
7877
},
7978
"config-test-mainnet.conf"

framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeWithCheckTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ public class BandWidthRuntimeOutOfTimeWithCheckTest extends BaseTest {
7474
"--output-directory", dbPath(),
7575
"--storage-db-directory", dbDirectory,
7676
"--storage-index-directory", indexDirectory,
77-
"-w",
7877
"--debug"
7978
},
8079
"config-test-mainnet.conf"

framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public static void init() {
6969
"--output-directory", dbPath(),
7070
"--storage-db-directory", dbDirectory,
7171
"--storage-index-directory", indexDirectory,
72-
"-w"
7372
},
7473
"config-test-mainnet.conf"
7574
);

framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeWithCheckTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public class BandWidthRuntimeWithCheckTest extends BaseTest {
7676
"--output-directory", dbPath(),
7777
"--storage-db-directory", dbDirectory,
7878
"--storage-index-directory", indexDirectory,
79-
"-w"
8079
},
8180
"config-test-mainnet.conf"
8281
);

framework/src/test/java/org/tron/common/storage/leveldb/LevelDbDataSourceImplTest.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.junit.Assert.assertFalse;
2323
import static org.junit.Assert.assertNotNull;
2424
import static org.junit.Assert.assertNull;
25+
import static org.junit.Assert.assertThrows;
2526

2627
import com.google.common.collect.Maps;
2728
import com.google.common.collect.Sets;
@@ -41,9 +42,7 @@
4142
import org.junit.Assert;
4243
import org.junit.Before;
4344
import org.junit.ClassRule;
44-
import org.junit.Rule;
4545
import org.junit.Test;
46-
import org.junit.rules.ExpectedException;
4746
import org.junit.rules.TemporaryFolder;
4847
import org.tron.common.utils.ByteArray;
4948
import org.tron.common.utils.FileUtil;
@@ -74,9 +73,6 @@ public class LevelDbDataSourceImplTest {
7473
private byte[] key5 = "00000005aa".getBytes();
7574
private byte[] key6 = "00000006aa".getBytes();
7675

77-
@Rule
78-
public final ExpectedException exception = ExpectedException.none();
79-
8076
/**
8177
* Release resources.
8278
*/
@@ -351,12 +347,11 @@ public void prefixQueryTest() {
351347

352348
@Test
353349
public void initDbTest() {
354-
exception.expect(TronError.class);
355350
makeExceptionDb("test_initDb");
356351
LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl(
357352
Args.getInstance().getOutputDirectory(), "test_initDb");
358-
dataSource.initDB();
359-
dataSource.closeDB();
353+
TronError thrown = assertThrows(TronError.class, dataSource::initDB);
354+
assertEquals(TronError.ErrCode.LEVELDB_INIT, thrown.getErrCode());
360355
}
361356

362357
private void makeExceptionDb(String dbName) {

framework/src/test/java/org/tron/common/storage/leveldb/RocksDbDataSourceImplTest.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.Assert.assertFalse;
55
import static org.junit.Assert.assertNotNull;
66
import static org.junit.Assert.assertNull;
7+
import static org.junit.Assert.assertThrows;
78

89
import com.google.common.collect.Maps;
910
import com.google.common.collect.Sets;
@@ -23,9 +24,7 @@
2324
import org.junit.Assert;
2425
import org.junit.BeforeClass;
2526
import org.junit.ClassRule;
26-
import org.junit.Rule;
2727
import org.junit.Test;
28-
import org.junit.rules.ExpectedException;
2928
import org.junit.rules.TemporaryFolder;
3029
import org.tron.common.storage.rocksdb.RocksDbDataSourceImpl;
3130
import org.tron.common.utils.ByteArray;
@@ -56,9 +55,6 @@ public class RocksDbDataSourceImplTest {
5655
private byte[] key5 = "00000005aa".getBytes();
5756
private byte[] key6 = "00000006aa".getBytes();
5857

59-
@Rule
60-
public final ExpectedException exception = ExpectedException.none();
61-
6258
/**
6359
* Release resources.
6460
*/
@@ -393,12 +389,11 @@ public void prefixQueryTest() {
393389

394390
@Test
395391
public void initDbTest() {
396-
exception.expect(TronError.class);
397392
makeExceptionDb("test_initDb");
398393
RocksDbDataSourceImpl dataSource = new RocksDbDataSourceImpl(
399394
Args.getInstance().getOutputDirectory(), "test_initDb");
400-
dataSource.initDB();
401-
dataSource.closeDB();
395+
TronError thrown = assertThrows(TronError.class, dataSource::initDB);
396+
assertEquals(TronError.ErrCode.ROCKSDB_INIT, thrown.getErrCode());
402397
}
403398

404399
private void makeExceptionDb(String dbName) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class ForkControllerTest {
3131
@Before
3232
public void init() throws IOException {
3333
Args.setParam(new String[]{"-d",
34-
temporaryFolder.newFolder().toString(), "-w"}, Constant.TEST_CONF);
34+
temporaryFolder.newFolder().toString()}, Constant.TEST_CONF);
3535
context = new TronApplicationContext(DefaultConfig.class);
3636
dynamicPropertiesStore = context.getBean(DynamicPropertiesStore.class);
3737
chainBaseManager = context.getBean(ChainBaseManager.class);

framework/src/test/java/org/tron/core/capsule/AccountCapsuleTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class AccountCapsuleTest extends BaseTest {
3636
static AccountCapsule accountCapsule;
3737

3838
static {
39-
Args.setParam(new String[]{"-d", dbPath(), "-w"}, Constant.TEST_CONF);
39+
Args.setParam(new String[]{"-d", dbPath()}, Constant.TEST_CONF);
4040
OWNER_ADDRESS = Wallet.getAddressPreFixString() + "a06a17a49648a8ad32055c06f60fa14ae46df91234";
4141
}
4242

framework/src/test/java/org/tron/core/capsule/utils/AssetUtilTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class AssetUtilTest extends BaseTest {
2424

2525

2626
static {
27-
Args.setParam(new String[] {"-d", dbPath(), "-w"}, Constant.TEST_CONF);
27+
Args.setParam(new String[] {"-d", dbPath()}, Constant.TEST_CONF);
2828
}
2929

3030
public static byte[] randomBytes(int length) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void destroy() {
5757

5858
@Test
5959
public void get() {
60-
Args.setParam(new String[] {"-w", "-c", Constant.TEST_CONF}, Constant.TESTNET_CONF);
60+
Args.setParam(new String[] {"-c", Constant.TEST_CONF}, Constant.TESTNET_CONF);
6161

6262
CommonParameter parameter = Args.getInstance();
6363

@@ -132,7 +132,7 @@ public void get() {
132132
@Test
133133
public void testIpFromLibP2p()
134134
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
135-
Args.setParam(new String[] {"-w"}, Constant.TEST_CONF);
135+
Args.setParam(new String[] {}, Constant.TEST_CONF);
136136
CommonParameter parameter = Args.getInstance();
137137

138138
String configuredExternalIp = parameter.getNodeExternalIp();
@@ -153,7 +153,7 @@ public void testIpFromLibP2p()
153153
@Test
154154
public void testOldRewardOpt() {
155155
thrown.expect(IllegalArgumentException.class);
156-
Args.setParam(new String[] {"-w", "-c", "args-test.conf"}, Constant.TESTNET_CONF);
156+
Args.setParam(new String[] {"-c", "args-test.conf"}, Constant.TESTNET_CONF);
157157
}
158158

159159
@Test

framework/src/test/java/org/tron/core/consensus/DposServiceTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.google.protobuf.ByteString;
66
import java.lang.reflect.Field;
7+
import org.junit.AfterClass;
78
import org.junit.Assert;
89
import org.junit.Test;
910
import org.mockito.Mockito;
@@ -21,6 +22,12 @@
2122
public class DposServiceTest {
2223
DposService service = new DposService();
2324

25+
26+
@AfterClass
27+
public static void destroy() {
28+
Args.clearParam();
29+
}
30+
2431
@Test
2532
public void testValidBlockTime() throws Exception {
2633
long headTime = 1724036757000L;
@@ -53,8 +60,7 @@ public void testValidBlockTime() throws Exception {
5360

5461
@Test
5562
public void testValidSlot() throws Exception {
56-
Args.setParam(new String[] {"-w"}, Constant.TEST_CONF);
57-
CommonParameter parameter = Args.getInstance();
63+
Args.setParam(new String[] {}, Constant.TEST_CONF);
5864
long headTime = 1724036757000L;
5965
ByteString witness = ByteString.copyFrom(NetUtil.getNodeId());
6066
ByteString witness2 = ByteString.copyFrom(NetUtil.getNodeId());

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.junit.Before;
3333
import org.junit.Rule;
3434
import org.junit.Test;
35-
import org.junit.rules.ExpectedException;
3635
import org.junit.rules.TemporaryFolder;
3736
import org.tron.common.application.TronApplicationContext;
3837
import org.tron.common.crypto.ECKey;
@@ -115,8 +114,6 @@ public class ManagerTest extends BlockGenerate {
115114
private static BlockCapsule blockCapsule2;
116115
@Rule
117116
public TemporaryFolder temporaryFolder = new TemporaryFolder();
118-
@Rule
119-
public final ExpectedException exception = ExpectedException.none();
120117
private static AtomicInteger port = new AtomicInteger(0);
121118
private static String accountAddress =
122119
Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a1abc";
@@ -126,7 +123,7 @@ public class ManagerTest extends BlockGenerate {
126123
@Before
127124
public void init() throws IOException {
128125
Args.setParam(new String[]{"-d",
129-
temporaryFolder.newFolder().toString(), "-w"}, Constant.TEST_CONF);
126+
temporaryFolder.newFolder().toString()}, Constant.TEST_CONF);
130127
Args.getInstance().setNodeListenPort(10000 + port.incrementAndGet());
131128
context = new TronApplicationContext(DefaultConfig.class);
132129

@@ -726,7 +723,7 @@ public void fork()
726723
BadBlockException, TaposException, BadNumberBlockException, NonCommonBlockException,
727724
ReceiptCheckErrException, VMIllegalException, TooBigTransactionResultException,
728725
ZksnarkException, EventBloomException {
729-
Args.setParam(new String[]{"--witness"}, Constant.TEST_CONF);
726+
Args.setParam(new String[]{}, Constant.TEST_CONF);
730727
long size = chainManager.getBlockStore().size();
731728
// System.out.print("block store size:" + size + "\n");
732729
String key = PublicMethod.getRandomPrivateKey();
@@ -873,7 +870,7 @@ public void doNotSwitch()
873870
TaposException, BadNumberBlockException, NonCommonBlockException,
874871
ReceiptCheckErrException, VMIllegalException, TooBigTransactionResultException,
875872
ZksnarkException, EventBloomException {
876-
Args.setParam(new String[]{"--witness"}, Constant.TEST_CONF);
873+
Args.setParam(new String[]{}, Constant.TEST_CONF);
877874
long size = chainManager.getBlockStore().size();
878875
System.out.print("block store size:" + size + "\n");
879876
String key = PublicMethod.getRandomPrivateKey();
@@ -985,7 +982,7 @@ public void switchBack()
985982
BadBlockException, TaposException, BadNumberBlockException, NonCommonBlockException,
986983
ReceiptCheckErrException, VMIllegalException, TooBigTransactionResultException,
987984
ZksnarkException, EventBloomException {
988-
Args.setParam(new String[]{"--witness"}, Constant.TEST_CONF);
985+
Args.setParam(new String[]{}, Constant.TEST_CONF);
989986
long size = chainManager.getBlockStore().size();
990987
System.out.print("block store size:" + size + "\n");
991988
String key = PublicMethod.getRandomPrivateKey();;
@@ -1238,10 +1235,11 @@ public void testExpiration() {
12381235

12391236
@Test
12401237
public void blockTrigger() {
1241-
exception.expect(TronError.class);
12421238
Manager manager = spy(new Manager());
12431239
doThrow(new RuntimeException("postBlockTrigger mock")).when(manager).postBlockTrigger(any());
1244-
manager.blockTrigger(new BlockCapsule(Block.newBuilder().build()), 1, 1);
1240+
TronError thrown = Assert.assertThrows(TronError.class, () ->
1241+
manager.blockTrigger(new BlockCapsule(Block.newBuilder().build()), 1, 1));
1242+
Assert.assertEquals(TronError.ErrCode.EVENT_SUBSCRIBE_ERROR, thrown.getErrCode());
12451243
}
12461244

12471245
public void adjustBalance(AccountStore accountStore, byte[] accountAddress, long amount)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class TransactionStoreTest extends BaseTest {
5050
*/
5151
@BeforeClass
5252
public static void init() {
53-
Args.setParam(new String[]{"--output-directory", dbPath(), "-w"}, Constant.TEST_CONF);
53+
Args.setParam(new String[]{"--output-directory", dbPath()}, Constant.TEST_CONF);
5454
}
5555

5656
/**

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public class TransactionTraceTest extends BaseTest {
6565
"--output-directory", dbPath(),
6666
"--storage-db-directory", dbDirectory,
6767
"--storage-index-directory", indexDirectory,
68-
"-w",
6968
"--debug"
7069
},
7170
"config-test-mainnet.conf"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static void init() {
2020
String dbDirectory = "db_TransactionCache_test";
2121
String indexDirectory = "index_TransactionCache_test";
2222
Args.setParam(new String[]{"--output-directory", dbPath(), "--storage-db-directory",
23-
dbDirectory, "--storage-index-directory", indexDirectory, "-w"}, Constant.TEST_CONF);
23+
dbDirectory, "--storage-index-directory", indexDirectory}, Constant.TEST_CONF);
2424
}
2525

2626
@Test

framework/src/test/java/org/tron/core/db/api/AssetUpdateHelperTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class AssetUpdateHelperTest extends BaseTest {
2727
private static boolean init;
2828

2929
static {
30-
Args.setParam(new String[]{"-d", dbPath(), "-w"}, "config-test-index.conf");
30+
Args.setParam(new String[]{"-d", dbPath()}, "config-test-index.conf");
3131
Args.getInstance().setSolidityNode(true);
3232
}
3333

0 commit comments

Comments
 (0)