-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7743804
commit 1a15dd3
Showing
8 changed files
with
98 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,15 @@ | ||
package com.limechain; | ||
|
||
import com.limechain.config.HostConfig; | ||
import com.limechain.client.FullNode; | ||
import com.limechain.client.LightClient; | ||
import com.limechain.client.HostNode; | ||
import com.limechain.network.protocol.blockannounce.NodeRole; | ||
import com.limechain.rpc.server.AppBean; | ||
import com.limechain.lightclient.LightClient; | ||
import com.limechain.rpc.server.RpcApp; | ||
import lombok.extern.java.Log; | ||
import sun.misc.Signal; | ||
|
||
import java.util.logging.Level; | ||
|
||
@Log | ||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
// Instantiate and start the spring application, so we get the global context | ||
RpcApp rpcApp = new RpcApp(); | ||
rpcApp.start(args); | ||
|
||
// Figure out what client role we want to start | ||
HostConfig hostConfig = AppBean.getBean(HostConfig.class); | ||
final NodeRole nodeRole = hostConfig.getNodeRole(); | ||
HostNode client; | ||
LightClient client = new LightClient(args, rpcApp); | ||
|
||
switch (nodeRole) { | ||
case FULL -> { | ||
client = new FullNode(); | ||
} | ||
case LIGHT -> { | ||
client = new LightClient(); | ||
} | ||
case NONE -> { | ||
// This shouldn't happen. | ||
// TODO: don't use this enum for the CLI NodeRole option | ||
return; | ||
} | ||
default -> { | ||
log.log(Level.SEVERE, "Node role {0} not yet implemented.", nodeRole); | ||
return; | ||
} | ||
} | ||
|
||
// Start the client | ||
// NOTE: This starts the beans the client would need - mutates the global context | ||
client.start(); | ||
log.log(Level.INFO, "\uD83D\uDE80Started {0} client!", nodeRole); | ||
|
||
Signal.handle(new Signal("INT"), signal -> { | ||
rpcApp.stop(); // NOTE: rpcApp is responsible for stopping everything that could've been started | ||
|
||
// TODO: Maybe think of another place to hold the logic below | ||
log.log(Level.INFO, "\uD83D\uDED1Stopped {0} client!", nodeRole); | ||
System.exit(0); | ||
}); | ||
Signal.handle(new Signal("INT"), signal -> client.stop()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/test/java/com/limechain/lightclient/LightClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.limechain.lightclient; | ||
|
||
import com.limechain.network.Network; | ||
import com.limechain.rpc.server.RpcApp; | ||
import com.limechain.sync.warpsync.WarpSyncMachine; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
import static org.mockito.Mockito.doNothing; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
class LightClientTest { | ||
private LightClient lightClient; | ||
|
||
private RpcApp rpcApp; | ||
private String[] args; | ||
|
||
// Setting private fields. Not a good idea in general | ||
private void setPrivateField(String fieldName, Object value) | ||
throws NoSuchFieldException, IllegalAccessException { | ||
Field privateField = LightClient.class.getDeclaredField(fieldName); | ||
privateField.setAccessible(true); | ||
|
||
privateField.set(lightClient, value); | ||
} | ||
|
||
@BeforeEach | ||
public void setup() { | ||
rpcApp = mock(RpcApp.class); | ||
args = new String[]{"some args"}; | ||
|
||
lightClient = spy(new LightClient(args, rpcApp)); | ||
} | ||
|
||
@Test | ||
void lightClient_stop_invokesStopFunctions() throws NoSuchFieldException, IllegalAccessException { | ||
Network network = mock(Network.class); | ||
WarpSyncMachine warpSync = mock(WarpSyncMachine.class); | ||
|
||
setPrivateField("network", network); | ||
setPrivateField("warpSyncMachine", warpSync); | ||
doNothing().when(lightClient).doExit(); | ||
|
||
lightClient.stop(); | ||
|
||
verify(network, times(1)).stop(); | ||
verify(rpcApp, times(1)).stop(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters