Skip to content

Commit 25b937d

Browse files
authored
Merge pull request #2003 from rsksmart/bootstrap-fix
Fixed Bootstrap cli tool
2 parents 0f0560a + 1f37c0b commit 25b937d

File tree

2 files changed

+25
-30
lines changed

2 files changed

+25
-30
lines changed

rskj-core/src/main/java/co/rsk/cli/tools/StartBootstrap.java

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,46 +58,43 @@ public StartBootstrap(RskContext ctx) {
5858
public static void main(String[] args) {
5959
setUpThread(Thread.currentThread());
6060

61-
try (RskContext ctx = new BootstrapRskContext(args)) {
62-
int result = new CommandLine(new StartBootstrap(ctx)).setUnmatchedArgumentsAllowed(true).execute(args);
61+
RskContext ctx = null;
62+
try {
63+
ctx = new BootstrapRskContext(args);
64+
65+
new CommandLine(new StartBootstrap(ctx)).setUnmatchedArgumentsAllowed(true).execute(args);
66+
} catch (Exception e) {
67+
logger.error("Main thread of RSK bootstrap node crashed", e);
6368

64-
if (result != 0) {
65-
throw new PicocliBadResultException(result);
69+
if (ctx != null) {
70+
ctx.close();
6671
}
72+
73+
System.exit(1);
6774
}
6875
}
6976

7077
@Override
71-
public Integer call() throws IOException {
78+
public Integer call() throws Exception {
7279
PreflightChecksUtils preflightChecks = new PreflightChecksUtils(ctx);
7380
Runtime runtime = Runtime.getRuntime();
74-
NodeStopper nodeStopper = System::exit;
7581

76-
runBootstrapNode(ctx, preflightChecks, runtime, nodeStopper);
82+
runBootstrapNode(ctx, preflightChecks, runtime);
7783
return 0;
7884
}
7985

8086
static void runBootstrapNode(@Nonnull RskContext ctx,
8187
@Nonnull PreflightChecksUtils preflightChecks,
82-
@Nonnull Runtime runtime,
83-
@Nonnull NodeStopper nodeStopper) {
84-
try {
85-
// make preflight checks
86-
preflightChecks.runChecks();
88+
@Nonnull Runtime runtime) throws Exception {
89+
// make preflight checks
90+
preflightChecks.runChecks();
8791

88-
// subscribe to shutdown hook
89-
runtime.addShutdownHook(new Thread(ctx::close, "stopper"));
92+
// subscribe to shutdown hook
93+
runtime.addShutdownHook(new Thread(ctx::close, "stopper"));
9094

91-
// start node runner
92-
NodeRunner runner = ctx.getNodeRunner();
93-
runner.run();
94-
} catch (Exception e) {
95-
logger.error("Main thread of RSK bootstrap node crashed", e);
96-
97-
ctx.close();
98-
99-
nodeStopper.stop(1);
100-
}
95+
// start node runner
96+
NodeRunner runner = ctx.getNodeRunner();
97+
runner.run();
10198
}
10299

103100
static void setUpThread(@Nonnull Thread thread) {

rskj-core/src/test/java/co/rsk/cli/tools/CliToolsTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import co.rsk.test.dsl.WorldDslProcessor;
3636
import co.rsk.trie.Trie;
3737
import co.rsk.util.NodeStopper;
38+
import co.rsk.util.PreflightCheckException;
3839
import co.rsk.util.PreflightChecksUtils;
3940
import com.fasterxml.jackson.databind.MapperFeature;
4041
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -567,25 +568,22 @@ void startBootstrap() throws Exception {
567568
doReturn(runner).when(ctx).getNodeRunner();
568569
PreflightChecksUtils preflightChecks = mock(PreflightChecksUtils.class);
569570
Runtime runtime = mock(Runtime.class);
570-
NodeStopper nodeStopper = mock(NodeStopper.class);
571571

572-
StartBootstrap.runBootstrapNode(ctx, preflightChecks, runtime, nodeStopper);
572+
StartBootstrap.runBootstrapNode(ctx, preflightChecks, runtime);
573573

574574
verify(preflightChecks, times(1)).runChecks();
575575
verify(runner, times(1)).run();
576576
verify(runtime, times(1)).addShutdownHook(threadCaptor.capture());
577577
assertEquals("stopper", threadCaptor.getValue().getName());
578578

579579
// check unhappy flow of bootstrap node start
580-
doThrow(new RuntimeException()).when(preflightChecks).runChecks();
580+
doThrow(new PreflightCheckException("")).when(preflightChecks).runChecks();
581581

582-
StartBootstrap.runBootstrapNode(ctx, preflightChecks, runtime, nodeStopper);
582+
Assertions.assertThrows(PreflightCheckException.class, () -> StartBootstrap.runBootstrapNode(ctx, preflightChecks, runtime));
583583

584584
verify(preflightChecks, times(2)).runChecks();
585585
verify(runner, times(1)).run();
586586
verify(runtime, times(1)).addShutdownHook(any());
587-
verify(ctx, times(1)).close();
588-
verify(nodeStopper, times(1)).stop(1);
589587
}
590588

591589
@Test

0 commit comments

Comments
 (0)