Skip to content

Commit

Permalink
replace SocketAddress w/ addressUtils
Browse files Browse the repository at this point in the history
Removes the `SocketAddress` class to avoid confusion with the `Address` type and moves the functionality to `addressUtils` helper classes.
  • Loading branch information
sangaman committed Aug 15, 2018
1 parent 343fcd0 commit d10b05a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 57 deletions.
20 changes: 11 additions & 9 deletions lib/p2p/Peer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import assert from 'assert';
import net, { Socket } from 'net';
import { EventEmitter } from 'events';
import SocketAddress from './SocketAddress';
import Parser, { ParserError, ParserErrorType } from './Parser';
import * as packets from './packets/types';
import Logger from '../Logger';
Expand All @@ -10,6 +9,7 @@ import { OutgoingOrder } from '../types/orders';
import { Packet, PacketDirection, PacketType } from './packets';
import { HandshakeState, Address, NodeConnectionInfo } from '../types/p2p';
import errors from './errors';
import addressUtils from '../utils/addressUtils';

/** Key info about a peer for display purposes */
type PeerInfo = {
Expand Down Expand Up @@ -40,7 +40,7 @@ interface Peer {
/** Represents a remote XU peer */
class Peer extends EventEmitter {
// TODO: properties documentation
public socketAddress!: SocketAddress;
public socketAddress!: Address;
public inbound!: boolean;
public connected: boolean = false;
private opened: boolean = false;
Expand Down Expand Up @@ -77,7 +77,7 @@ class Peer extends EventEmitter {

public get info(): PeerInfo {
return {
address: this.socketAddress.toString(),
address: addressUtils.toString(this.socketAddress),
nodePubKey: this.handshakeState ? this.handshakeState.nodePubKey : undefined,
inbound: this.inbound,
pairs: this.handshakeState ? this.handshakeState.pairs : undefined,
Expand Down Expand Up @@ -108,7 +108,9 @@ class Peer extends EventEmitter {
public getStatus = (): string => {
let status: string;
if (this.connected) {
status = this.nodePubKey ? `Connected to peer ${this.nodePubKey}` : `Connected pre-handshake to peer ${this.socketAddress.toString()}`;
status = this.nodePubKey ?
`Connected to peer ${this.nodePubKey}` :
`Connected pre-handshake to peer ${addressUtils.toString(this.socketAddress)}`;
} else {
status = 'Not connected';
}
Expand All @@ -132,7 +134,7 @@ class Peer extends EventEmitter {

// TODO: Check that the peer's version is compatible with ours
if (nodePubKey && this.nodePubKey !== nodePubKey) {
throw errors.UNEXPECTED_NODE_PUB_KEY(this.nodePubKey!, nodePubKey, this.socketAddress.toString());
throw errors.UNEXPECTED_NODE_PUB_KEY(this.nodePubKey!, nodePubKey, addressUtils.toString(this.socketAddress));
}

// let the pool know that this peer is ready to go
Expand Down Expand Up @@ -381,7 +383,7 @@ class Peer extends EventEmitter {

const socket = net.connect(address.port, address.host);

this.socketAddress = new SocketAddress(address.host, address.port);
this.socketAddress = address;
this.inbound = false;
this.connected = false;

Expand All @@ -391,7 +393,7 @@ class Peer extends EventEmitter {
private accept = (socket: Socket): void => {
assert(!this.socket);

this.socketAddress = SocketAddress.fromSocket(socket);
this.socketAddress = addressUtils.fromSocket(socket);
this.inbound = true;
this.connected = true;

Expand All @@ -417,7 +419,7 @@ class Peer extends EventEmitter {
if (this.connected) {
// don't log anything on close if we've never connected as the connection failure is logged elsewhere
if (this.nodePubKey === undefined) {
this.logger.info(`Socket closed prior to handshake (${this.socketAddress.toString()})`);
this.logger.info(`Socket closed prior to handshake (${addressUtils.toString(this.socketAddress)})`);
} else if (hadError) {
this.logger.warn(`Peer ${this.nodePubKey} socket closed due to error`);
} else {
Expand All @@ -432,7 +434,7 @@ class Peer extends EventEmitter {
if (this.nodePubKey !== undefined) {
this.logger.debug(`Received data (${this.nodePubKey}): ${data.toString()}`);
} else {
this.logger.debug(`Received data (${this.socketAddress.toString()}): ${data.toString()}`);
this.logger.debug(`Received data (${addressUtils.toString(this.socketAddress)}): ${data.toString()}`);
}
this.parser.feed(data);
});
Expand Down
1 change: 0 additions & 1 deletion lib/p2p/PeerList.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Peer from './Peer';
import SocketAddress from './SocketAddress';

class PeerList {
private peers = new Map<string, Peer>();
Expand Down
6 changes: 3 additions & 3 deletions lib/p2p/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { EventEmitter } from 'events';
import errors from './errors';
import Peer, { PeerInfo } from './Peer';
import NodeList from './NodeList';
import SocketAddress from './SocketAddress';
import PeerList from './PeerList';
import P2PRepository from './P2PRepository';
import { Packet, PacketType, OrderPacket, OrderInvalidationPacket, GetOrdersPacket, NodesPacket, OrdersPacket, GetNodesPacket } from './packets';
import { PeerOrder, OutgoingOrder, OrderIdentifier } from '../types/orders';
import DB from '../db/DB';
import Logger from '../Logger';
import { HandshakeState, Address, NodeConnectionInfo } from '../types/p2p';
import addressUtils from '../utils/addressUtils';

type PoolConfig = {
listen: boolean;
Expand Down Expand Up @@ -54,7 +54,7 @@ class Pool extends EventEmitter {
this.listenPort = config.port;
this.addresses = [];
config.addresses.forEach((addressString) => {
const address = SocketAddress.fromString(addressString, config.port).toAddress();
const address = addressUtils.fromString(addressString, config.port);
this.addresses!.push(address);
});
}
Expand Down Expand Up @@ -184,7 +184,7 @@ class Pool extends EventEmitter {
const peer = this.peers.get(nodePubKey);
if (peer) {
peer.close();
this.logger.info(`Disconnected from ${peer.nodePubKey} ${peer.socketAddress.toString()}`);
this.logger.info(`Disconnected from ${peer.nodePubKey} @ ${addressUtils.toString(peer.socketAddress)}`);
} else {
throw(errors.NOT_CONNECTED(nodePubKey));
}
Expand Down
44 changes: 0 additions & 44 deletions lib/p2p/SocketAddress.ts

This file was deleted.

34 changes: 34 additions & 0 deletions lib/utils/addressUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Socket } from 'net';
import { Address } from '../types/p2p';
import assert from 'assert';

/** Helper methods for interacting with the [[Address]] type. */
const addressUtils = {
/**
* Create an [[Address]] using the remote host and port of a socket.
*/
fromSocket: (socket: Socket): Address => {
const { remoteAddress, remotePort } = socket;
assert(remoteAddress, 'socket must have a remoteAddress value');
assert(remotePort, 'socket must have a remotePort value');
return { host: remoteAddress!, port: remotePort! };
},

/**
* Create an [[Address]] from a string.
* @param addressString a string in the "{host}:{port}" format
* @param port a port number to use if no port is specified in the string, defaults to 8885
*/
fromString: (addressString: string, port = 8885): Address => {
const arr = addressString.split(':');
return {
host: arr[0],
port: arr[1] ? parseInt(arr[1], 10) : port,
};
},

/** Convert an [[Address]] to a string in the "{host}:{port}" format. */
toString: (address: Address) => `${address.host}:${address.port}`,
};

export default addressUtils;

0 comments on commit d10b05a

Please sign in to comment.