Skip to content

Commit ad7c8bb

Browse files
committed
fix(config): handle null config values and SonarCloud issues
- Replace null values (discovery.external.ip, trustNode) with empty string before ConfigBeanFactory binding for external config compat (system-test uses "external.ip = null" which ConfigBeanFactory cannot bind to String fields; recommend updating system-test to use "" instead) - Fix floating point comparison with Double.compare (java:S1244) - Extract duplicated string literals into constants/variables (java:S1192)
1 parent 19cadf7 commit ad7c8bb

5 files changed

Lines changed: 38 additions & 15 deletions

File tree

common/src/main/java/org/tron/core/config/args/CommitteeConfig.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,25 @@ public class CommitteeConfig {
9292
* uppercase letters) which causes ConfigBeanFactory key mismatch. These two fields
9393
* are excluded from automatic binding and handled manually after.
9494
*/
95+
private static final String PBFT_EXPIRE_NUM_KEY = "pBFTExpireNum";
96+
private static final String ALLOW_PBFT_KEY = "allowPBFT";
97+
9598
public static CommitteeConfig fromConfig(Config config) {
9699
Config section = config.getConfig("committee");
97100

98101
// ConfigBeanFactory derives key names from setter methods. For setPBFTExpireNum()
99102
// it expects "PBFTExpireNum" (capital P), but config.conf uses "pBFTExpireNum".
100-
// Similarly, getAllowPBFT() maps to "allowPBFT" which may be missing in test configs.
101-
// Add uppercase aliases so ConfigBeanFactory can find them.
103+
// Add uppercase alias so ConfigBeanFactory can find it.
102104
Config aliased = section;
103-
if (section.hasPath("pBFTExpireNum") && !section.hasPath("PBFTExpireNum")) {
104-
aliased = aliased.withValue("PBFTExpireNum", section.getValue("pBFTExpireNum"));
105+
if (section.hasPath(PBFT_EXPIRE_NUM_KEY) && !section.hasPath("PBFTExpireNum")) {
106+
aliased = aliased.withValue("PBFTExpireNum", section.getValue(PBFT_EXPIRE_NUM_KEY));
105107
}
106108

107109
CommitteeConfig cc = ConfigBeanFactory.create(aliased, CommitteeConfig.class);
108110
// Ensure the manually-named fields get the right values from the original keys
109-
cc.allowPBFT = section.hasPath("allowPBFT") ? section.getLong("allowPBFT") : 0;
110-
cc.pBFTExpireNum = section.hasPath("pBFTExpireNum") ? section.getLong("pBFTExpireNum") : 20;
111+
cc.allowPBFT = section.hasPath(ALLOW_PBFT_KEY) ? section.getLong(ALLOW_PBFT_KEY) : 0;
112+
cc.pBFTExpireNum = section.hasPath(PBFT_EXPIRE_NUM_KEY)
113+
? section.getLong(PBFT_EXPIRE_NUM_KEY) : 20;
111114

112115
cc.postProcess();
113116
return cc;

common/src/main/java/org/tron/core/config/args/EventConfig.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,15 @@ public static EventConfig fromConfig(Config config) {
8181

8282
// "native" is a Java reserved word, "topics" has optional fields per item —
8383
// strip both before binding, read manually
84-
Config bindable = section.withoutPath("native").withoutPath("topics")
84+
String nativeKey = "native";
85+
String topicsKey = "topics";
86+
Config bindable = section.withoutPath(nativeKey).withoutPath(topicsKey)
8587
.withoutPath("topicDefaults");
8688
EventConfig ec = ConfigBeanFactory.create(bindable, EventConfig.class);
8789

8890
// manually bind "native" sub-section
89-
Config nativeSection = section.hasPath("native")
90-
? section.getConfig("native") : ConfigFactory.empty();
91+
Config nativeSection = section.hasPath(nativeKey)
92+
? section.getConfig(nativeKey) : ConfigFactory.empty();
9193
ec.nativeQueue = new NativeConfig();
9294
if (nativeSection.hasPath("useNativeQueue")) {
9395
ec.nativeQueue.useNativeQueue = nativeSection.getBoolean("useNativeQueue");
@@ -100,9 +102,9 @@ public static EventConfig fromConfig(Config config) {
100102
}
101103

102104
// manually bind topics — each item may have optional fields
103-
if (section.hasPath("topics")) {
105+
if (section.hasPath(topicsKey)) {
104106
ec.topics = new ArrayList<>();
105-
for (com.typesafe.config.ConfigObject obj : section.getObjectList("topics")) {
107+
for (com.typesafe.config.ConfigObject obj : section.getObjectList(topicsKey)) {
106108
Config tc = obj.toConfig();
107109
TopicConfig topic = new TopicConfig();
108110
if (tc.hasPath("triggerName")) {

common/src/main/java/org/tron/core/config/args/MiscConfig.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ public static MiscConfig fromConfig(Config config) {
4343
// trx
4444
mc.trxReferenceBlock = config.hasPath("trx.reference.block")
4545
? config.getString("trx.reference.block") : "solid";
46-
if (config.hasPath("trx.expiration.timeInMilliseconds")
47-
&& config.getLong("trx.expiration.timeInMilliseconds") > 0) {
48-
mc.trxExpirationTimeInMilliseconds = config.getLong("trx.expiration.timeInMilliseconds");
46+
String trxExpirationKey = "trx.expiration.timeInMilliseconds";
47+
if (config.hasPath(trxExpirationKey)
48+
&& config.getLong(trxExpirationKey) > 0) {
49+
mc.trxExpirationTimeInMilliseconds = config.getLong(trxExpirationKey);
4950
}
5051

5152
// energy (note: config key has typo "enery" — preserved for backward compat)

common/src/main/java/org/tron/core/config/args/NodeConfig.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,13 @@ public static class DnsConfig {
360360
public static NodeConfig fromConfig(Config config) {
361361
Config section = config.getConfig("node");
362362

363+
// Replace null values with empty strings before bean binding.
364+
// Some external configs (e.g. system-test) set "discovery.external.ip = null"
365+
// which ConfigBeanFactory cannot bind to a String field.
366+
// Note: hasPath() returns false for null values, use hasPathOrNull() instead.
367+
section = replaceNullWithEmpty(section, "discovery.external.ip");
368+
section = replaceNullWithEmpty(section, "trustNode");
369+
363370
// Auto-bind all fields and sub-beans
364371
NodeConfig nc = ConfigBeanFactory.create(section, NodeConfig.class);
365372

@@ -474,4 +481,14 @@ private static String getString(Config config, String path, String defaultValue)
474481
return config.hasPath(path) ? config.getString(path) : defaultValue;
475482
}
476483

484+
// Replace a null config value with empty string so ConfigBeanFactory can bind it.
485+
// hasPath() returns false for null values; hasPathOrNull() returns true.
486+
private static Config replaceNullWithEmpty(Config config, String path) {
487+
if (config.hasPathOrNull(path) && config.getIsNull(path)) {
488+
return config.withValue(path,
489+
com.typesafe.config.ConfigValueFactory.fromAnyRef(""));
490+
}
491+
return config;
492+
}
493+
477494
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ private static void loadDnsPublishParameters(NodeConfig.DnsConfig dns,
10751075

10761076
if (dns.getChangeThreshold() > 0) {
10771077
publishConfig.setChangeThreshold(dns.getChangeThreshold());
1078-
} else if (dns.getChangeThreshold() != 0.0) {
1078+
} else if (Double.compare(dns.getChangeThreshold(), 0.0) != 0) {
10791079
logger.error("Check node.dns.changeThreshold, should be bigger than 0, default 0.1");
10801080
}
10811081

0 commit comments

Comments
 (0)