Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public enum ErrCode {
LOG_LOAD(1),
WITNESS_INIT(1),
RATE_LIMITER_INIT(1),
JDK_VERSION(1),
SOLID_NODE_INIT(0);

private final int code;
Expand Down
14 changes: 13 additions & 1 deletion framework/src/main/java/org/tron/core/config/args/Args.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.tron.core.config.args;

import static java.lang.System.exit;
import static org.fusesource.jansi.Ansi.ansi;
import static org.tron.common.math.Maths.max;
import static org.tron.common.math.Maths.min;
import static org.tron.core.Constant.ADD_PRE_FIX_BYTE_MAINNET;
Expand Down Expand Up @@ -45,6 +46,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.fusesource.jansi.AnsiConsole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.tron.common.arch.Arch;
Expand Down Expand Up @@ -374,7 +376,17 @@ private static Map<String, String[]> getOptionGroup() {
* set parameters.
*/
public static void setParam(final String[] args, final String confFileName) {
Arch.throwIfUnsupportedJavaVersion();
try {
Arch.throwIfUnsupportedJavaVersion();
} catch (UnsupportedOperationException e) {
AnsiConsole.systemInstall();
// To avoid confusion caused by silent execution when using -h or -v flags,
// errors are explicitly logged to the console in this context.
// Console output is not required for errors in other scenarios.
System.out.println(ansi().fgRed().a(e.getMessage()).reset());
AnsiConsole.systemUninstall();
throw new TronError(e, TronError.ErrCode.JDK_VERSION);
}
clearParam(); // reset all parameters to avoid the influence in test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method name throwIfUnsupportedJavaVersion can be modified as checkJdkCompatible ?
What's the output of three lines related with AnsiConsole ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throwIfUnsupportedJavaVersion is the more accurate and intention-revealing name because it clearly communicates that the method will throw an exception if the condition is not met.

This error will appear on the console to alert the user, rather than being quietly recorded in the log files.

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to show this infomation in console, this AnsiConsole should be placed in ExitManager, so all TronError has the same action.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid confusion caused by silent execution when using -h or -v flags, errors are explicitly logged to the console in this context. Console output is not required for errors in other scenarios.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get it.

JCommander.newBuilder().addObject(PARAMETER).build().parse(args);
if (PARAMETER.version) {
Expand Down
31 changes: 29 additions & 2 deletions framework/src/test/java/org/tron/core/exception/TronErrorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.tron.common.arch.Arch;
import org.tron.common.log.LogService;
import org.tron.common.parameter.RateLimiterInitialization;
import org.tron.common.utils.ReflectUtils;
Expand All @@ -41,7 +42,7 @@ public class TronErrorTest {
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@After
public void clearMocks() {
public void clearMocks() {
Mockito.clearAllCaches();
Args.clearParam();
}
Expand Down Expand Up @@ -115,12 +116,38 @@ public void rateLimiterServletInitTest() {

@Test
public void shutdownBlockTimeInitTest() {
Map<String,String> params = new HashMap<>();
Map<String, String> params = new HashMap<>();
params.put(Constant.NODE_SHUTDOWN_BLOCK_TIME, "0");
params.put("storage.db.directory", "database");
Config config = ConfigFactory.defaultOverrides().withFallback(
ConfigFactory.parseMap(params));
TronError thrown = assertThrows(TronError.class, () -> Args.setParam(config));
assertEquals(TronError.ErrCode.AUTO_STOP_PARAMS, thrown.getErrCode());
}

@Test
public void testThrowIfUnsupportedJavaVersion() {
runArchTest(true, false, true);
runArchTest(true, true, false);
runArchTest(false, false, false);
}

private void runArchTest(boolean isX86, boolean isJava8, boolean expectThrow) {
try (MockedStatic<Arch> mocked = mockStatic(Arch.class)) {
mocked.when(Arch::isX86).thenReturn(isX86);
mocked.when(Arch::isJava8).thenReturn(isJava8);
mocked.when(Arch::getOsArch).thenReturn("x86_64");
mocked.when(Arch::javaSpecificationVersion).thenReturn("17");
mocked.when(Arch::withAll).thenReturn("");
mocked.when(Arch::throwIfUnsupportedJavaVersion).thenCallRealMethod();

if (expectThrow) {
assertEquals(TronError.ErrCode.JDK_VERSION, assertThrows(
TronError.class, () -> Args.setParam(new String[]{}, Constant.TEST_CONF)).getErrCode());
} else {
Arch.throwIfUnsupportedJavaVersion();
}
}
}

}
4 changes: 2 additions & 2 deletions platform/src/main/java/common/org/tron/common/arch/Arch.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public static void throwIfUnsupportedJavaVersion() {
if (isX86() && !isJava8()) {
logger.info(withAll());
throw new UnsupportedOperationException(String.format(
"Java %s is required for %s architecture. Detected version %s",
"1.8 ", getOsArch(), javaSpecificationVersion()));
"Java %s is required for %s architecture. Detected version %s", "1.8",
getOsArch(), javaSpecificationVersion()));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not write Java 1.8 directly ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe either style is fine.

}

Expand Down