Skip to content

Commit 4d8d9b9

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 1323c54 commit 4d8d9b9

6 files changed

Lines changed: 41 additions & 19 deletions

File tree

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public class CommitteeConfig {
6969
private long consensusLogicOptimization = 0;
7070
private long allowTvmCancun = 0;
7171
private long allowTvmBlob = 0;
72+
private long allowTvmOsaka = 0;
7273
private long unfreezeDelayDays = 0;
7374
private long allowReceiptsMerkleRoot = 0;
7475
private long allowAccountAssetOptimization = 0;
@@ -92,22 +93,25 @@ public class CommitteeConfig {
9293
* uppercase letters) which causes ConfigBeanFactory key mismatch. These two fields
9394
* are excluded from automatic binding and handled manually after.
9495
*/
96+
private static final String PBFT_EXPIRE_NUM_KEY = "pBFTExpireNum";
97+
private static final String ALLOW_PBFT_KEY = "allowPBFT";
98+
9599
public static CommitteeConfig fromConfig(Config config) {
96100
Config section = config.getConfig("committee");
97101

98102
// ConfigBeanFactory derives key names from setter methods. For setPBFTExpireNum()
99103
// 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.
104+
// Add uppercase alias so ConfigBeanFactory can find it.
102105
Config aliased = section;
103-
if (section.hasPath("pBFTExpireNum") && !section.hasPath("PBFTExpireNum")) {
104-
aliased = aliased.withValue("PBFTExpireNum", section.getValue("pBFTExpireNum"));
106+
if (section.hasPath(PBFT_EXPIRE_NUM_KEY) && !section.hasPath("PBFTExpireNum")) {
107+
aliased = aliased.withValue("PBFTExpireNum", section.getValue(PBFT_EXPIRE_NUM_KEY));
105108
}
106109

107110
CommitteeConfig cc = ConfigBeanFactory.create(aliased, CommitteeConfig.class);
108111
// 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;
112+
cc.allowPBFT = section.hasPath(ALLOW_PBFT_KEY) ? section.getLong(ALLOW_PBFT_KEY) : 0;
113+
cc.pBFTExpireNum = section.hasPath(PBFT_EXPIRE_NUM_KEY)
114+
? section.getLong(PBFT_EXPIRE_NUM_KEY) : 20;
111115

112116
cc.postProcess();
113117
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
}

common/src/main/resources/reference.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,7 @@ committee = {
747747
consensusLogicOptimization = 0
748748
allowTvmCancun = 0
749749
allowTvmBlob = 0
750+
allowTvmOsaka = 0
750751
allowAccountAssetOptimization = 0
751752
allowAssetOptimization = 0
752753
allowNewReward = 0

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ private static void applyCommitteeConfig(CommitteeConfig cc) {
523523
PARAMETER.consensusLogicOptimization = cc.getConsensusLogicOptimization();
524524
PARAMETER.allowTvmCancun = cc.getAllowTvmCancun();
525525
PARAMETER.allowTvmBlob = cc.getAllowTvmBlob();
526+
PARAMETER.allowTvmOsaka = cc.getAllowTvmOsaka();
526527
PARAMETER.unfreezeDelayDays = cc.getUnfreezeDelayDays();
527528
// allowReceiptsMerkleRoot not in CommonParameter — skip for now
528529
PARAMETER.allowAccountAssetOptimization = cc.getAllowAccountAssetOptimization();
@@ -811,10 +812,6 @@ public static void applyConfigParams(
811812
eventConfig = EventConfig.fromConfig(config);
812813
applyEventConfig(eventConfig);
813814

814-
PARAMETER.allowTvmOsaka =
815-
config.hasPath(ConfigKey.COMMITTEE_ALLOW_TVM_OSAKA) ? config
816-
.getInt(ConfigKey.COMMITTEE_ALLOW_TVM_OSAKA) : 0;
817-
818815
logConfig();
819816
}
820817

@@ -1079,7 +1076,7 @@ private static void loadDnsPublishParameters(NodeConfig.DnsConfig dns,
10791076

10801077
if (dns.getChangeThreshold() > 0) {
10811078
publishConfig.setChangeThreshold(dns.getChangeThreshold());
1082-
} else if (dns.getChangeThreshold() != 0.0) {
1079+
} else if (Double.compare(dns.getChangeThreshold(), 0.0) != 0) {
10831080
logger.error("Check node.dns.changeThreshold, should be bigger than 0, default 0.1");
10841081
}
10851082

0 commit comments

Comments
 (0)