From 64ab5fef37a3a66bbe57b992e6e7c6562f338a4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9Cur=D0=B0d=20H=D0=B0mz=D0=B0?= Date: Mon, 29 Jan 2024 14:30:03 +0200 Subject: [PATCH] Change substream identifiers to use genesis hash (#283) * feat: Change substream identifiers to use genesis hash * chore: use genesis only when cli args are passed --- src/main/java/com/limechain/cli/Cli.java | 9 +++- .../java/com/limechain/cli/CliArguments.java | 12 ++--- .../java/com/limechain/network/Network.java | 44 ++++++++++--------- .../com/limechain/network/ProtocolUtils.java | 15 ++++--- .../limechain/rpc/config/CommonConfig.java | 4 +- 5 files changed, 48 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/limechain/cli/Cli.java b/src/main/java/com/limechain/cli/Cli.java index bb0fb8d33..95bfd2eab 100644 --- a/src/main/java/com/limechain/cli/Cli.java +++ b/src/main/java/com/limechain/cli/Cli.java @@ -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 @@ -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 @@ -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); @@ -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; } diff --git a/src/main/java/com/limechain/cli/CliArguments.java b/src/main/java/com/limechain/cli/CliArguments.java index 412853b76..25bd261e1 100644 --- a/src/main/java/com/limechain/cli/CliArguments.java +++ b/src/main/java/com/limechain/cli/CliArguments.java @@ -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) { } diff --git a/src/main/java/com/limechain/network/Network.java b/src/main/java/com/limechain/network/Network.java index 9bb0b7518..bcb5bd0bf 100644 --- a/src/main/java/com/limechain/network/Network.java +++ b/src/main/java/com/limechain/network/Network.java @@ -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; @@ -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 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 repository, CliArguments cliArgs) { boolean isLocalEnabled = hostConfig.getChain() == Chain.LOCAL; boolean clientMode = true; @@ -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); diff --git a/src/main/java/com/limechain/network/ProtocolUtils.java b/src/main/java/com/limechain/network/ProtocolUtils.java index 10f45fad4..41efab479 100644 --- a/src/main/java/com/limechain/network/ProtocolUtils.java +++ b/src/main/java/com/limechain/network/ProtocolUtils.java @@ -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); } diff --git a/src/main/java/com/limechain/rpc/config/CommonConfig.java b/src/main/java/com/limechain/rpc/config/CommonConfig.java index 0d7a430a6..a7e21d7a0 100644 --- a/src/main/java/com/limechain/rpc/config/CommonConfig.java +++ b/src/main/java/com/limechain/rpc/config/CommonConfig.java @@ -63,8 +63,8 @@ public SystemInfo systemInfo(HostConfig hostConfig, Network network) { @Bean public Network network(ChainService chainService, HostConfig hostConfig, KVRepository repository, - CliArguments cliArgs) { - return new Network(chainService, hostConfig, repository, cliArgs); + CliArguments cliArgs, GenesisBlockHash genesisBlockHash) { + return new Network(chainService, hostConfig, repository, cliArgs, genesisBlockHash); } @Bean