Skip to content

Commit

Permalink
feat: parse uri without port
Browse files Browse the repository at this point in the history
This allows for node connection uris without a specified port to be
parsed and assigned a default port of 8885. This allows the port to be
left off in the rpc `Connect` call when connecting to the default port.
  • Loading branch information
sangaman committed Jan 15, 2019
1 parent 6084043 commit db91489
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 39 deletions.
4 changes: 2 additions & 2 deletions lib/service/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import RaidenClient, { RaidenInfo } from '../raidenclient/RaidenClient';
import { EventEmitter } from 'events';
import errors from './errors';
import { SwapClients, OrderSide, SwapRole } from '../types/enums';
import { parseUri, getUri, UriParts } from '../utils/utils';
import { parseUri, toUri, UriParts } from '../utils/uriUtils';
import * as lndrpc from '../proto/lndrpc_pb';
import { Pair, Order, OrderPortion } from '../types/orders';
import { PlaceOrderEvent } from '../types/orderBook';
Expand Down Expand Up @@ -228,7 +228,7 @@ class Service extends EventEmitter {

if (addresses && addresses.length > 0) {
addresses.forEach((address) => {
uris.push(getUri({ nodePubKey, host: address.host, port: address.port }));
uris.push(toUri({ nodePubKey, host: address.host, port: address.port }));
});
}

Expand Down
35 changes: 35 additions & 0 deletions lib/utils/uriUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import assert = require('assert');

export type UriParts = {
nodePubKey: string;
host: string;
port: number;
};

/**
* Creates a URI string from the public key, host and port.
*/
export const toUri = (uriParts: UriParts): string => {
const { nodePubKey, host, port } = uriParts;
return `${nodePubKey}@${host}:${port}`;
};

/**
* Splits a URI in the [pubkey]@[host]:[port] format into the public key, host and port components.
* If port is not present in the URI, it defaults to 8885.
*/
export const parseUri = (uri: string): UriParts => {
// A regex that splits the string by the symbols "@" and ":"
const split = uri.split(/[@:]+/);

assert(split.length === 3 || split.length === 2);

const port = split[2] !== undefined ? Number(split[2]) : 8885;
assert(!isNaN(port));

return {
port,
nodePubKey: split[0],
host: split[1],
};
};
31 changes: 0 additions & 31 deletions lib/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import http from 'http';
import p2pErrors from '../p2p/errors';
import { assert } from 'chai';
import { Pair } from '../types/orders';
import crypto from 'crypto';
import { promisify } from 'util';
import moment from 'moment';

export type UriParts = {
nodePubKey: string;
host: string;
port: number;
};

/**
* Gets the external IP of the node.
*/
Expand All @@ -38,30 +31,6 @@ export const getExternalIp = () => {
});
};

/**
* Creates a URI from the public key, host and port.
*/
export const getUri = (uriParts: UriParts): string => {
const { nodePubKey, host, port } = uriParts;
return `${nodePubKey}@${host}:${port}`;
};

/**
* Splits a URI into the public key, host and port.
*/
export const parseUri = (uri: string): UriParts => {
// A regex that splits the string by the symbols "@" and ":"
const split = uri.split(/[@:]+/);

assert(split.length === 3);

return {
nodePubKey: split[0],
host: split[1],
port: Number(split[2]),
};
};

/**
* Check whether a variable is a non-array object
*/
Expand Down
10 changes: 5 additions & 5 deletions test/p2p/sanity.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chai, { expect } from 'chai';
import Xud from '../../lib/Xud';
import chaiAsPromised from 'chai-as-promised';
import { getUri } from '../../lib/utils/utils';
import { toUri } from '../../lib/utils/uriUtils';
import { getUnusedPort } from '../utils';
import { DisconnectionReason, ReputationEvent } from '../../lib/types/enums';

Expand Down Expand Up @@ -51,8 +51,8 @@ describe('P2P Sanity Tests', () => {
await Promise.all([nodeOne.start(nodeOneConfig), nodeTwo.start(nodeTwoConfig)]);

nodeTwoPort = nodeTwo['pool']['listenPort']!;
nodeOneUri = getUri({ nodePubKey: nodeOne.nodePubKey, host: 'localhost', port: nodeOne['pool']['listenPort']! });
nodeTwoUri = getUri({ nodePubKey: nodeTwo.nodePubKey, host: 'localhost', port: nodeTwoPort });
nodeOneUri = toUri({ nodePubKey: nodeOne.nodePubKey, host: 'localhost', port: nodeOne['pool']['listenPort']! });
nodeTwoUri = toUri({ nodePubKey: nodeTwo.nodePubKey, host: 'localhost', port: nodeTwoPort });
});

it('should connect successfully', async () => {
Expand All @@ -77,7 +77,7 @@ describe('P2P Sanity Tests', () => {
});

it('should fail when connecting to an unexpected node pub key', async () => {
const connectPromise = nodeOne.service.connect({ nodeUri: getUri({
const connectPromise = nodeOne.service.connect({ nodeUri: toUri({
nodePubKey: 'thewrongpubkey',
host: 'localhost',
port: nodeTwoPort,
Expand All @@ -95,7 +95,7 @@ describe('P2P Sanity Tests', () => {

it('should fail connecting to a non-existing node', async () => {
const port = await getUnusedPort();
const connectPromise = nodeOne.service.connect({ nodeUri: getUri({ port, nodePubKey: 'notarealnodepubkey', host: 'localhost' }) });
const connectPromise = nodeOne.service.connect({ nodeUri: toUri({ port, nodePubKey: 'notarealnodepubkey', host: 'localhost' }) });
await expect(connectPromise).to.be.rejectedWith(`could not connect to peer at localhost:${port}`);
});

Expand Down
1 change: 0 additions & 1 deletion test/unit/NodeKey.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { expect } from 'chai';
import chaiAsPromised = require('chai-as-promised');
import NodeKey from '../../lib/nodekey/NodeKey';
import secp256k1 from 'secp256k1';

Expand Down

0 comments on commit db91489

Please sign in to comment.