Skip to content

Commit

Permalink
Revert "WIP - moved parseSeedNodes to validation utils, using new URL…
Browse files Browse the repository at this point in the history
…(), supporting IPv4/IPv6"

This reverts commit f740eab.
  • Loading branch information
joshuakarp committed Feb 14, 2022
1 parent 850e9a7 commit 9195ce9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 52 deletions.
49 changes: 48 additions & 1 deletion src/bin/utils/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const parseHostOrHostname = validateParserToArgParser(
validationUtils.parseHostOrHostname,
);
const parsePort = validateParserToArgParser(validationUtils.parsePort);
const parseSeedNodes = validateParserToArgParser(validationUtils.parseSeedNodes);

function parseCoreCount(v: string): number | undefined {
if (v === 'all') {
Expand Down Expand Up @@ -89,6 +88,54 @@ function getDefaultSeedNodes(network: string): NodeMapping {
return seedNodes;
}

/**
* Seed nodes expected to be of form 'nodeId1@host:port;nodeId2@host:port;...'
* By default, any specified seed nodes (in CLI option, or environment variable)
* will overwrite the default nodes in src/config.ts.
* Special flag `<defaults>` indicates that the default seed
* nodes should be added to the starting seed nodes instead of being overwritten.
*/
function parseSeedNodes(rawSeedNodes: string): [NodeMapping, boolean] {
const seedNodeMappings: NodeMapping = {};
let defaults = false;
// If specifically set no seed nodes, then ensure we start with none
if (rawSeedNodes === '') return [seedNodeMappings, defaults];
const semicolonSeedNodes = rawSeedNodes.split(';');
for (const rawSeedNode of semicolonSeedNodes) {
// Empty string will occur if there's an extraneous ';' (e.g. at end of env)
if (rawSeedNode === '') continue;
// Append the default seed nodes if we encounter the special flag
if (rawSeedNode === '<defaults>') {
defaults = true;
continue;
}
// TODO: Fix as per #324
const idHostPort = rawSeedNode.split(/[@:]/);
if (idHostPort.length !== 3) {
throw new commander.InvalidOptionArgumentError(
`${rawSeedNode} is not of format 'nodeId@host:port'`,
);
}
const seedNodeId = nodesUtils.decodeNodeId(idHostPort[0]);
if (seedNodeId == null) {
throw new commander.InvalidOptionArgumentError(
`${idHostPort[0]} is not a valid node ID`,
);
}
if (!networkUtils.isHostname(idHostPort[1])) {
throw new commander.InvalidOptionArgumentError(
`${idHostPort[1]} is not a valid hostname`,
);
}
const port = parsePort(idHostPort[2]);
seedNodeMappings[seedNodeId] = {
host: idHostPort[1] as Host | Hostname,
port: port,
};
}
return [seedNodeMappings, defaults];
}

function parseNetwork(network: string): NodeMapping {
// Getting a list of network names from the config defaults
const networks = config.defaults.network;
Expand Down
52 changes: 1 addition & 51 deletions src/validation/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* The parse error message must focus on why the validation failed
* @module
*/
import type { NodeId, NodeMapping } from '../nodes/types';
import type { NodeId } from '../nodes/types';
import type { ProviderId, IdentityId } from '../identities/types';
import type { GestaltAction, GestaltId } from '../gestalts/types';
import type { VaultAction } from '../vaults/types';
Expand Down Expand Up @@ -173,55 +173,6 @@ function parsePort(data: any): Port {
return data;
}

/**
* Seed nodes expected to be of form 'nodeId1@host:port;nodeId2@host:port;...'
* By default, any specified seed nodes (in CLI option, or environment variable)
* will overwrite the default nodes in src/config.ts.
* Special flag `<defaults>` indicates that the default seed
* nodes should be added to the starting seed nodes instead of being overwritten.
*/
function parseSeedNodes(rawSeedNodes: string): [NodeMapping, boolean] {
const seedNodeMappings: NodeMapping = {};
let defaults = false;
// If specifically set no seed nodes, then ensure we start with none
if (rawSeedNodes === '') return [seedNodeMappings, defaults];
const semicolonSeedNodes = rawSeedNodes.split(';');
for (const rawSeedNode of semicolonSeedNodes) {
// Empty string will occur if there's an extraneous ';' (e.g. at end of env)
if (rawSeedNode === '') continue;
// Append the default seed nodes if we encounter the special flag
if (rawSeedNode === '<defaults>') {
defaults = true;
continue;
}
let idHostPort: URL;
try {
idHostPort = new URL(`pk://${rawSeedNode}`);
} catch (e) {
if (e instanceof TypeError) {
throw new validationErrors.ErrorParse(
`Seed node ${rawSeedNode} must be of format 'nodeId@host:port'`,
);
}
throw e;
}
console.log(idHostPort);
const rawNodeId = idHostPort.username;
const rawHost = idHostPort.hostname.replace(/\[|\]/g, ''); // strip square brackets
const rawPort = idHostPort.port;
if (rawNodeId == '' || rawHost == '' || rawPort == '') {
throw new validationErrors.ErrorParse(
`Seed node ${rawSeedNode} must be of format 'nodeId@host:port'`,
);
}
seedNodeMappings[parseNodeId(rawNodeId)] = {
host: parseHostOrHostname(rawHost),
port: parsePort(rawPort),
};
}
return [seedNodeMappings, defaults];
}

export {
parseInteger,
parseNumber,
Expand All @@ -236,5 +187,4 @@ export {
parseHostname,
parseHostOrHostname,
parsePort,
parseSeedNodes,
};

0 comments on commit 9195ce9

Please sign in to comment.