Skip to content

Commit

Permalink
Add full/warp sync configuration on fullnode (#291)
Browse files Browse the repository at this point in the history
* feat: add full/warp sync option

* test: add newest parameters to cli test

* chore: add value validaiton

* chore: fix comments

* refactor: throw exception on unexpected value
  • Loading branch information
ablax authored Jan 31, 2024
1 parent 64ab5fe commit 0708436
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 15 deletions.
19 changes: 18 additions & 1 deletion src/main/java/com/limechain/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.limechain.exception.CliArgsParseException;
import com.limechain.network.protocol.blockannounce.NodeRole;
import com.limechain.storage.DBInitializer;
import com.limechain.sync.SyncMode;
import lombok.Getter;
import lombok.extern.java.Log;
import org.apache.commons.cli.CommandLine;
Expand All @@ -13,6 +14,7 @@
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.jetbrains.annotations.NotNull;

import java.util.List;

Expand All @@ -28,6 +30,7 @@ public class Cli {
private static final String DB_RECREATE = "db-recreate";
private static final String NODE_MODE = "node-mode";
private static final String NO_LEGACY_PROTOCOLS = "no-legacy-protocols";
private static final String SYNC_MODE = "sync-mode";

/**
* Holds CLI options
Expand All @@ -40,6 +43,15 @@ public Cli() {
this.options = buildOptions();
}

@NotNull
private static SyncMode parseSyncMode(CommandLine cmd) {
try {
return SyncMode.valueOf(cmd.getOptionValue(SYNC_MODE, "warp").toUpperCase());
} catch (IllegalArgumentException e) {
throw new CliArgsParseException("Invalid sync mode provided, valid values - WARP or FULL", e);
}
}

/**
* Parses node launch arguments.
*
Expand All @@ -60,8 +72,9 @@ public CliArguments parseArgs(String[] args) {
// what does running the node in NodeMode NONE mean?
String nodeMode = cmd.getOptionValue(NODE_MODE, NodeRole.FULL.toString());
boolean noLgacyProtocols = cmd.hasOption(NO_LEGACY_PROTOCOLS);
SyncMode syncMode = parseSyncMode(cmd);

return new CliArguments(network, dbPath, dbRecreate, nodeKey, nodeMode, noLgacyProtocols);
return new CliArguments(network, dbPath, dbRecreate, nodeKey, nodeMode, noLgacyProtocols, syncMode);
} catch (ParseException e) {
formatter.printHelp("Specify the network name - " + String.join(", ", validChains), options);
throw new CliArgsParseException("Failed to parse cli arguments", e);
Expand All @@ -83,20 +96,24 @@ private Options buildOptions() {
"Full by default.");
Option noLegacyProtocols = new Option(null, NO_LEGACY_PROTOCOLS, false,
"Doesn't use legacy protocols if set");
Option syncMode = new Option(null, SYNC_MODE, true,
"Sync mode (warp/full) - warp by default");

networkOption.setRequired(false);
dbPathOption.setRequired(false);
dbClean.setRequired(false);
nodeKey.setRequired(false);
nodeMode.setRequired(false);
noLegacyProtocols.setRequired(false);
syncMode.setRequired(false);

result.addOption(networkOption);
result.addOption(dbPathOption);
result.addOption(dbClean);
result.addOption(nodeKey);
result.addOption(nodeMode);
result.addOption(noLegacyProtocols);
result.addOption(syncMode);
return result;
}

Expand Down
15 changes: 9 additions & 6 deletions src/main/java/com/limechain/cli/CliArguments.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.limechain.cli;

import com.limechain.sync.SyncMode;

/**
* Hold successfully parsed cli arguments
*
* @param network the network
* @param dbPath the DB path
* @param dbRecreate flag for recreating the database for current network
* @param nodeKey HEX for secret Ed25519 key
* @param legacyProtocols
* @param network the network
* @param dbPath the DB path
* @param dbRecreate flag for recreating the database for current network
* @param nodeKey HEX for secret Ed25519 key
* @param noLegacyProtocols flag for disabling legacy protocols
* @param syncMode the sync mode
*/
public record CliArguments(String network, String dbPath, boolean dbRecreate, String nodeKey, String nodeRole,
boolean noLegacyProtocols) {
boolean noLegacyProtocols, SyncMode syncMode) {
}
24 changes: 16 additions & 8 deletions src/main/java/com/limechain/client/FullNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.google.common.primitives.Bytes;
import com.limechain.chain.ChainService;
import com.limechain.cli.CliArguments;
import com.limechain.network.Network;
import com.limechain.rpc.server.AppBean;
import com.limechain.storage.KVRepository;
import com.limechain.storage.block.BlockStateHelper;
import com.limechain.sync.fullsync.FullSyncMachine;
import com.limechain.sync.warpsync.WarpSyncMachine;
import com.limechain.trie.structure.database.InsertTrieBuilder;
import com.limechain.trie.structure.database.TrieBuildException;
Expand Down Expand Up @@ -69,7 +71,13 @@ public void start() {

// Start syncing
log.log(Level.INFO, "Node successfully connected to a peer! Sync can start!");
AppBean.getBean(WarpSyncMachine.class).start();

CliArguments args = AppBean.getBean(CliArguments.class);
switch (args.syncMode()){
case FULL -> AppBean.getBean(FullSyncMachine.class).start();
case WARP -> AppBean.getBean(WarpSyncMachine.class).start();
default -> throw new IllegalStateException("Unexpected value: " + args.syncMode());
}
}

private Map<String, String> loadGenesisStorage() {
Expand All @@ -80,9 +88,9 @@ private Map<String, String> loadGenesisStorage() {
/**
* Inserts trie nodes into the key-value repository.
*
* @param db The key-value repository where trie nodes are to be stored.
* @param db The key-value repository where trie nodes are to be stored.
* @param insertTrieNodes The list of trie nodes to be inserted.
* @param entriesVersion The version number of the trie entries.
* @param entriesVersion The version number of the trie entries.
*/
private void insertStorage(KVRepository<String, Object> db, List<InsertTrieNode> insertTrieNodes,
int entriesVersion) {
Expand Down Expand Up @@ -124,12 +132,12 @@ private void insertTrieNodeStorage(KVRepository<String, Object> db, InsertTrieNo

/**
* Inserts the children of a given trie node into
the key-value repository.
@param db The key-value repository where trie node children are to be stored.
@param trieNode The trie node whose children are to be inserted.
* the key-value repository.
*
* @param db The key-value repository where trie node children are to be stored.
* @param trieNode The trie node whose children are to be inserted.
*/
private void insertChildren(KVRepository<String, Object> db, InsertTrieNode trieNode) {
private void insertChildren(KVRepository<String, Object> db, InsertTrieNode trieNode) {
String key = TRIE_NODE_CHILD_PREFIX + new String(trieNode.merkleValue());
List<byte[]> childrenMerkleValues = trieNode.childrenMerkleValues();

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/limechain/rpc/config/CommonConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.limechain.storage.DBInitializer;
import com.limechain.storage.DBRepository;
import com.limechain.storage.KVRepository;
import com.limechain.sync.fullsync.FullSyncMachine;
import com.limechain.sync.warpsync.SyncedState;
import com.limechain.sync.warpsync.WarpSyncMachine;
import org.springframework.boot.ApplicationArguments;
Expand Down Expand Up @@ -72,6 +73,11 @@ public WarpSyncMachine sync(Network network, ChainService chainService) {
return new WarpSyncMachine(network, chainService);
}

@Bean
public FullSyncMachine sync(Network network) {
return new FullSyncMachine(network);
}

@Bean
public GenesisBlockHash genesisBlockHash(ChainService chainService) {
return new GenesisBlockHash(chainService);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/limechain/sync/SyncMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.limechain.sync;

public enum SyncMode {
WARP,
FULL
}
18 changes: 18 additions & 0 deletions src/main/java/com/limechain/sync/fullsync/FullSyncMachine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.limechain.sync.fullsync;

import com.limechain.network.Network;
import lombok.Getter;

//TODO: Implement FullSyncMachine
@Getter
public class FullSyncMachine {
private final Network networkService;

public FullSyncMachine(final Network networkService) {
this.networkService = networkService;
}

public void start() {
networkService.sendNeighbourMessages();
}
}
6 changes: 6 additions & 0 deletions src/test/java/com/limechain/cli/CliTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ void buildOptions_buildsOptions() {
assertTrue(options.hasOption("network"));
assertTrue(options.hasOption("n"));
assertTrue(options.hasOption("db-path"));
assertTrue(options.hasOption("dbc"));
assertTrue(options.hasOption("node-key"));
assertTrue(options.hasOption("node-mode"));
assertTrue(options.hasOption("mode"));
assertTrue(options.hasOption("no-legacy-protocols"));
assertTrue(options.hasOption("sync-mode"));
assertEquals(0, options.getRequiredOptions().size());
}

Expand Down

0 comments on commit 0708436

Please sign in to comment.