Skip to content

Commit

Permalink
Change substream identifiers to use genesis hash (#283)
Browse files Browse the repository at this point in the history
* feat: Change substream identifiers to use genesis hash

* chore: use genesis only when cli args are passed
  • Loading branch information
ablax authored Jan 29, 2024
1 parent d7b7233 commit 64ab5fe
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 36 deletions.
9 changes: 7 additions & 2 deletions src/main/java/com/limechain/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.apache.commons.cli.ParseException;

import java.util.List;
import java.util.logging.Level;

/**
* Abstraction class around apache.commons.cli used to set arguments rules and parse node arguments
Expand All @@ -28,6 +27,7 @@ public class Cli {
public static final String NODE_KEY = "node-key";
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";

/**
* Holds CLI options
Expand Down Expand Up @@ -59,8 +59,9 @@ public CliArguments parseArgs(String[] args) {
// TODO: separation of enums; this NodeRole enum is used for blockannounce
// 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);

return new CliArguments(network, dbPath, dbRecreate, nodeKey, nodeMode);
return new CliArguments(network, dbPath, dbRecreate, nodeKey, nodeMode, noLgacyProtocols);
} catch (ParseException e) {
formatter.printHelp("Specify the network name - " + String.join(", ", validChains), options);
throw new CliArgsParseException("Failed to parse cli arguments", e);
Expand All @@ -80,18 +81,22 @@ private Options buildOptions() {
Option nodeKey = new Option(null, NODE_KEY, true, "HEX for secret Ed25519 key");
Option nodeMode = new Option("mode", NODE_MODE, true, "Node mode (light/full). " +
"Full by default.");
Option noLegacyProtocols = new Option(null, NO_LEGACY_PROTOCOLS, false,
"Doesn't use legacy protocols if set");

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

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

Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/limechain/cli/CliArguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
/**
* 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 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
*/
public record CliArguments(String network, String dbPath, boolean dbRecreate, String nodeKey, String nodeRole) {
public record CliArguments(String network, String dbPath, boolean dbRecreate, String nodeKey, String nodeRole,
boolean noLegacyProtocols) {
}
44 changes: 23 additions & 21 deletions src/main/java/com/limechain/network/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.limechain.chain.ChainService;
import com.limechain.cli.CliArguments;
import com.limechain.config.HostConfig;
import com.limechain.constants.GenesisBlockHash;
import com.limechain.network.kad.KademliaService;
import com.limechain.network.protocol.blockannounce.BlockAnnounceService;
import com.limechain.network.protocol.blockannounce.NodeRole;
Expand Down Expand Up @@ -85,20 +86,21 @@ public class Network {
* Manages if nodes running locally are going to be allowed
* Connects Kademlia to boot nodes
*
* @param chainService chain specification information containing boot nodes
* @param hostConfig host configuration containing current network
* @param repository
* @param cliArgs
* @param chainService chain specification information containing boot nodes
* @param hostConfig host configuration containing current network
* @param repository database repository
* @param cliArgs command line arguments
* @param genesisBlockHash genesis block hash
*/
public Network(ChainService chainService, HostConfig hostConfig, KVRepository<String, Object> repository,
CliArguments cliArgs) {
CliArguments cliArgs, GenesisBlockHash genesisBlockHash) {
this.bootNodes = chainService.getGenesis().getBootNodes();
this.chain = hostConfig.getChain();
this.nodeRole = hostConfig.getNodeRole();
this.initializeProtocols(chainService, hostConfig, repository, cliArgs);
this.initializeProtocols(chainService, genesisBlockHash, hostConfig, repository, cliArgs);
}

private void initializeProtocols(ChainService chainService, HostConfig hostConfig,
private void initializeProtocols(ChainService chainService, GenesisBlockHash genesisBlockHash, HostConfig hostConfig,
KVRepository<String, Object> repository, CliArguments cliArgs) {
boolean isLocalEnabled = hostConfig.getChain() == Chain.LOCAL;
boolean clientMode = true;
Expand All @@ -112,21 +114,21 @@ private void initializeProtocols(ChainService chainService, HostConfig hostConfi
Multihash hostId = Multihash.deserialize(hostBuilder.getPeerId().getBytes());

String pingProtocol = ProtocolUtils.PING_PROTOCOL;
//TODO: Add new protocolId format with genesis hash
String chainId = chainService.getGenesis().getProtocolId();
String legacyKadProtocolId = ProtocolUtils.getLegacyKadProtocol(chainId);
String legacyWarpProtocolId = ProtocolUtils.getLegacyWarpSyncProtocol(chainId);
String legacyLightProtocolId = ProtocolUtils.getLegacyLightMessageProtocol(chainId);
String legacySyncProtocolId = ProtocolUtils.getLegacySyncProtocol(chainId);
String legacyBlockAnnounceProtocolId = ProtocolUtils.getLegacyBlockAnnounceProtocol(chainId);
String grandpaProtocolId = ProtocolUtils.LEGACY_GRANDPA_PROTOCOL;
String transactionsProtocolId = ProtocolUtils.getTransactionsProtocol(chainId);

kademliaService = new KademliaService(legacyKadProtocolId, hostId, isLocalEnabled, clientMode);
lightMessagesService = new LightMessagesService(legacyLightProtocolId);
warpSyncService = new WarpSyncService(legacyWarpProtocolId);
syncService = new SyncService(legacySyncProtocolId);
blockAnnounceService = new BlockAnnounceService(legacyBlockAnnounceProtocolId);
String protocolId = cliArgs.noLegacyProtocols() ? genesisBlockHash.getGenesisHash().toString() : chainId;
String kadProtocolId = ProtocolUtils.getKadProtocol(chainId);
String warpProtocolId = ProtocolUtils.getWarpSyncProtocol(protocolId);
String lightProtocolId = ProtocolUtils.getLightMessageProtocol(protocolId);
String syncProtocolId = ProtocolUtils.getSyncProtocol(protocolId);
String blockAnnounceProtocolId = ProtocolUtils.getBlockAnnounceProtocol(protocolId);
String grandpaProtocolId = ProtocolUtils.getGrandpaProtocol(protocolId);
String transactionsProtocolId = ProtocolUtils.getTransactionsProtocol(protocolId);

kademliaService = new KademliaService(kadProtocolId, hostId, isLocalEnabled, clientMode);
lightMessagesService = new LightMessagesService(lightProtocolId);
warpSyncService = new WarpSyncService(warpProtocolId);
syncService = new SyncService(syncProtocolId);
blockAnnounceService = new BlockAnnounceService(blockAnnounceProtocolId);
grandpaService = new GrandpaService(grandpaProtocolId);
ping = new Ping(pingProtocol, new PingProtocol());
transactionsService = new TransactionsService(transactionsProtocolId);
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/com/limechain/network/ProtocolUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,31 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ProtocolUtils {
public static final String PING_PROTOCOL = "/ipfs/ping/1.0.0";
public static final String LEGACY_GRANDPA_PROTOCOL = "/paritytech/grandpa/1";

public static String getLegacyLightMessageProtocol(String chainId) {
public static String getLightMessageProtocol(String chainId) {
return String.format("/%s/light/2", chainId);
}

public static String getLegacyWarpSyncProtocol(String chainId) {
public static String getWarpSyncProtocol(String chainId) {
return String.format("/%s/sync/warp", chainId);
}

public static String getLegacySyncProtocol(String chainId) {
public static String getSyncProtocol(String chainId) {
return String.format("/%s/sync/2", chainId);
}

public static String getLegacyBlockAnnounceProtocol(String chainId) {
public static String getBlockAnnounceProtocol(String chainId) {
return String.format("/%s/block-announces/1", chainId);
}

public static String getLegacyKadProtocol(String chainId) {
public static String getKadProtocol(String chainId) {
return String.format("/%s/kad", chainId);
}

public static String getGrandpaProtocol(String chainId) {
return String.format("/%s/grandpa/1", chainId);
}

public static String getTransactionsProtocol(String chainId) {
return String.format("/%s/transactions/1", chainId);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/limechain/rpc/config/CommonConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public SystemInfo systemInfo(HostConfig hostConfig, Network network) {

@Bean
public Network network(ChainService chainService, HostConfig hostConfig, KVRepository<String, Object> repository,
CliArguments cliArgs) {
return new Network(chainService, hostConfig, repository, cliArgs);
CliArguments cliArgs, GenesisBlockHash genesisBlockHash) {
return new Network(chainService, hostConfig, repository, cliArgs, genesisBlockHash);
}

@Bean
Expand Down

0 comments on commit 64ab5fe

Please sign in to comment.