-
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.
Merge pull request #246 from LimeChain/feat/242-implement-trie-structure
Introduce FullClient
- Loading branch information
Showing
8 changed files
with
143 additions
and
98 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,15 +1,59 @@ | ||
package com.limechain; | ||
|
||
import com.limechain.lightclient.LightClient; | ||
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.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(); | ||
LightClient client = new LightClient(args, 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; | ||
|
||
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(); | ||
Signal.handle(new Signal("INT"), signal -> client.stop()); | ||
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); | ||
}); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.limechain.client; | ||
|
||
import com.limechain.network.Network; | ||
import com.limechain.rpc.server.AppBean; | ||
import com.limechain.sync.warpsync.WarpSyncMachine; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.java.Log; | ||
|
||
import java.util.logging.Level; | ||
|
||
@Log | ||
public class FullNode implements HostNode { | ||
/** | ||
* Starts the light client by instantiating all dependencies and services | ||
* | ||
* @implNote the RpcApp is assumed to have been started before starting the client, | ||
* as it relies on the application context | ||
*/ | ||
@SneakyThrows | ||
public void start() { | ||
// TODO: Initialize storage here (temporally) | ||
|
||
final Network network = AppBean.getBean(Network.class); | ||
network.start(); | ||
|
||
while (true) { | ||
if (!network.kademliaService.getBootNodePeerIds().isEmpty()) { | ||
if (network.kademliaService.getSuccessfulBootNodes() > 0) { | ||
break; | ||
} | ||
network.updateCurrentSelectedPeer(); | ||
} | ||
|
||
log.log(Level.INFO, "Waiting for peer connection..."); | ||
Thread.sleep(10000); // TODO: Maybe extract this number into an application.property as it's duplicated | ||
} | ||
|
||
log.log(Level.INFO, "Node successfully connected to a peer! Sync can start!"); | ||
AppBean.getBean(WarpSyncMachine.class).start(); | ||
} | ||
} |
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,12 @@ | ||
package com.limechain.client; | ||
|
||
public interface HostNode { | ||
/** | ||
* Starts the client by assigning all dependencies and services from the spring boot application's context | ||
* | ||
* @apiNote the RpcApp is assumed to have been started | ||
* before constructing the clients in our current implementations, | ||
* as starting the clients relies on the application context | ||
*/ | ||
void start(); | ||
} |
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: 0 additions & 54 deletions
54
src/test/java/com/limechain/lightclient/LightClientTest.java
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