Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Merge branch 'release/2.1.0' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
shuse2 committed Apr 14, 2019
2 parents 466c68e + d2affb4 commit e3e8eef
Show file tree
Hide file tree
Showing 18 changed files with 235 additions and 166 deletions.
15 changes: 10 additions & 5 deletions packages/lisk-p2p/src/p2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ import {
P2PNetworkStatus,
P2PNodeInfo,
P2PPeerInfo,
P2PPeerSelectionForRequest,
P2PPeerSelectionForSend,
P2PPenalty,
P2PRequestPacket,
P2PResponsePacket,
Expand Down Expand Up @@ -230,11 +232,14 @@ export class P2P extends EventEmitter {
};

this._peerPool = new PeerPool({
connectTimeout: this._config.connectTimeout,
ackTimeout: this._config.ackTimeout,
peerSelectionForSendRequest: config.peerSelectionForSendRequest
? config.peerSelectionForSendRequest
: selectPeers,
connectTimeout: config.connectTimeout,
ackTimeout: config.ackTimeout,
peerSelectionForSend: config.peerSelectionForSend
? config.peerSelectionForSend
: (selectPeers as P2PPeerSelectionForSend),
peerSelectionForRequest: config.peerSelectionForRequest
? config.peerSelectionForRequest
: (selectPeers as P2PPeerSelectionForRequest),
peerSelectionForConnection: config.peerSelectionForConnection
? config.peerSelectionForConnection
: selectForConnection,
Expand Down
27 changes: 23 additions & 4 deletions packages/lisk-p2p/src/p2p_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@
*/
/* tslint:disable:no-empty-interface*/

export interface P2PRequestPacket {
export interface P2PPacket {
readonly data?: unknown;
}

export interface P2PRequestPacket extends P2PPacket {
readonly data?: unknown;
readonly procedure: string;
}

export interface P2PResponsePacket {
export interface P2PResponsePacket extends P2PPacket {
readonly data: unknown;
}

export interface P2PMessagePacket {
export interface P2PMessagePacket extends P2PPacket {
readonly data?: unknown;
readonly event: string;
}
Expand Down Expand Up @@ -76,7 +80,8 @@ export interface P2PConfig {
readonly nodeInfo: P2PNodeInfo;
readonly wsEngine?: string;
readonly discoveryInterval?: number;
readonly peerSelectionForSendRequest?: P2PPeerSelectionForSendRequest;
readonly peerSelectionForSend?: P2PPeerSelectionForSend;
readonly peerSelectionForRequest?: P2PPeerSelectionForRequest;
readonly peerSelectionForConnection?: P2PPeerSelectionForConnection;
readonly peerHandshakeCheck?: P2PCheckPeerCompatibility;
}
Expand Down Expand Up @@ -109,6 +114,20 @@ export type P2PPeerSelectionForSendRequest = (
numOfPeers?: number,
) => ReadonlyArray<P2PDiscoveredPeerInfo>;

export type P2PPeerSelectionForSend = (
peers: ReadonlyArray<P2PDiscoveredPeerInfo>,
nodeInfo?: P2PNodeInfo,
numOfPeers?: number,
messagePacket?: P2PMessagePacket
) => ReadonlyArray<P2PDiscoveredPeerInfo>;

export type P2PPeerSelectionForRequest = (
peers: ReadonlyArray<P2PDiscoveredPeerInfo>,
nodeInfo?: P2PNodeInfo,
numOfPeers?: number,
requestPacket?: P2PRequestPacket
) => ReadonlyArray<P2PDiscoveredPeerInfo>;

export type P2PPeerSelectionForConnection = (
peers: ReadonlyArray<P2PDiscoveredPeerInfo>,
nodeInfo?: P2PNodeInfo,
Expand Down
39 changes: 31 additions & 8 deletions packages/lisk-p2p/src/peer_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import {
P2PNodeInfo,
P2PPeerInfo,
P2PPeerSelectionForConnection,
P2PPeerSelectionForSendRequest,
P2PPeerSelectionForRequest,
P2PPeerSelectionForSend,
P2PRequestPacket,
P2PResponsePacket,
} from './p2p_types';
Expand Down Expand Up @@ -73,7 +74,8 @@ export {
interface PeerPoolConfig {
readonly connectTimeout?: number;
readonly ackTimeout?: number;
readonly peerSelectionForSendRequest: P2PPeerSelectionForSendRequest;
readonly peerSelectionForSend: P2PPeerSelectionForSend;
readonly peerSelectionForRequest: P2PPeerSelectionForRequest;
readonly peerSelectionForConnection: P2PPeerSelectionForConnection;
}

Expand Down Expand Up @@ -104,14 +106,16 @@ export class PeerPool extends EventEmitter {
) => void;
private readonly _handleFailedPeerInfoUpdate: (error: Error) => void;
private _nodeInfo: P2PNodeInfo | undefined;
private readonly _peerSelectForSendRequest: P2PPeerSelectionForSendRequest;
private readonly _peerSelectForSend: P2PPeerSelectionForSend;
private readonly _peerSelectForRequest: P2PPeerSelectionForRequest;
private readonly _peerSelectForConnection: P2PPeerSelectionForConnection;

public constructor(peerPoolConfig: PeerPoolConfig) {
super();
this._peerMap = new Map();
this._peerPoolConfig = peerPoolConfig;
this._peerSelectForSendRequest = peerPoolConfig.peerSelectionForSendRequest;
this._peerSelectForSend = peerPoolConfig.peerSelectionForSend;
this._peerSelectForRequest = peerPoolConfig.peerSelectionForRequest;
this._peerSelectForConnection = peerPoolConfig.peerSelectionForConnection;
// This needs to be an arrow function so that it can be used as a listener.
this._handlePeerRPC = (request: P2PRequest) => {
Expand Down Expand Up @@ -194,16 +198,35 @@ export class PeerPool extends EventEmitter {
return this._nodeInfo;
}

public selectPeers(
public selectPeersForRequest(
requestPacket?: P2PRequestPacket,
numOfPeers?: number,
): ReadonlyArray<P2PDiscoveredPeerInfo> {
const listOfPeerInfo = [...this._peerMap.values()].map(
(peer: Peer) => peer.peerInfo,
);
const selectedPeers = this._peerSelectForSendRequest(
const selectedPeers = this._peerSelectForRequest(
listOfPeerInfo,
this._nodeInfo,
numOfPeers,
requestPacket,
);

return selectedPeers;
}

public selectPeersForSend(
messagePacket?: P2PMessagePacket,
numOfPeers?: number,
): ReadonlyArray<P2PDiscoveredPeerInfo> {
const listOfPeerInfo = [...this._peerMap.values()].map(
(peer: Peer) => peer.peerInfo,
);
const selectedPeers = this._peerSelectForSend(
listOfPeerInfo,
this._nodeInfo,
numOfPeers,
messagePacket,
);

return selectedPeers;
Expand All @@ -212,7 +235,7 @@ export class PeerPool extends EventEmitter {
public async requestFromPeer(
packet: P2PRequestPacket,
): Promise<P2PResponsePacket> {
const selectedPeer = this.selectPeers(1);
const selectedPeer = this.selectPeersForRequest(packet, 1);

if (selectedPeer.length <= 0) {
throw new RequestFailError(
Expand All @@ -235,7 +258,7 @@ export class PeerPool extends EventEmitter {
}

public sendToPeers(message: P2PMessagePacket): void {
const selectedPeers = this.selectPeers();
const selectedPeers = this.selectPeersForSend(message);

selectedPeers.forEach((peerInfo: P2PDiscoveredPeerInfo) => {
const selectedPeerId = constructPeerIdFromPeerInfo(peerInfo);
Expand Down
17 changes: 10 additions & 7 deletions packages/lisk-p2p/test/integration/p2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { wait } from '../utils/helpers';
import { platform } from 'os';
import {
P2PPeerSelectionForSendRequest,
P2PPeerSelectionForSend,
P2PPeerSelectionForRequest,
P2PNodeInfo,
P2PDiscoveredPeerInfo,
P2PPeerSelectionForConnection,
Expand Down Expand Up @@ -94,8 +96,8 @@ describe('Integration tests for P2P library', () => {
seedPeers,
wsEngine: 'ws',
// A short connectTimeout and ackTimeout will make the node to give up on discovery quicker for our test.
connectTimeout: 100,
ackTimeout: 100,
connectTimeout: 1000,
ackTimeout: 1000,
// Set a different discoveryInterval for each node; that way they don't keep trying to discover each other at the same time.
discoveryInterval: DISCOVERY_INTERVAL + index * 11,
nodeInfo: {
Expand Down Expand Up @@ -330,6 +332,7 @@ describe('Integration tests for P2P library', () => {
return new P2P({
blacklistedPeers: [],
connectTimeout: 5000,
ackTimeout: 5000,
seedPeers,
wsEngine: 'ws',
nodeInfo: {
Expand Down Expand Up @@ -682,7 +685,6 @@ describe('Integration tests for P2P library', () => {
});

describe('Connected network: User custom selection algorithm is passed to each node', () => {
const DISCOVERY_INTERVAL = 200;
// Custom selection function that finds peers having common values for modules field for example.
const peerSelectionForSendRequest: P2PPeerSelectionForSendRequest = (
peersList: ReadonlyArray<P2PDiscoveredPeerInfo>,
Expand Down Expand Up @@ -744,9 +746,10 @@ describe('Integration tests for P2P library', () => {
return new P2P({
blacklistedPeers: [],
connectTimeout: 5000,
peerSelectionForSendRequest,
ackTimeout: 5000,
peerSelectionForSend: peerSelectionForSendRequest as P2PPeerSelectionForSend,
peerSelectionForRequest: peerSelectionForSendRequest as P2PPeerSelectionForRequest,
peerSelectionForConnection,
discoveryInterval: DISCOVERY_INTERVAL,
seedPeers,
wsEngine: 'ws',
nodeInfo: {
Expand Down Expand Up @@ -903,8 +906,8 @@ describe('Integration tests for P2P library', () => {
seedPeers,
wsEngine: 'ws',
// A short connectTimeout and ackTimeout will make the node to give up on discovery quicker for our test.
connectTimeout: 100,
ackTimeout: 100,
connectTimeout: 1000,
ackTimeout: 1000,
// Set a different discoveryInterval for each node; that way they don't keep trying to discover each other at the same time.
discoveryInterval: DISCOVERY_INTERVAL + index * 11,
nodeInfo: {
Expand Down
20 changes: 6 additions & 14 deletions packages/lisk-transactions/src/0_transfer_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
StateStorePrepare,
} from './base_transaction';
import { MAX_TRANSACTION_AMOUNT, TRANSFER_FEE } from './constants';
import { TransactionError } from './errors';
import { convertToAssetError, TransactionError } from './errors';
import { TransactionJSON } from './transaction_types';
import {
validateAddress,
Expand Down Expand Up @@ -66,9 +66,7 @@ export class TransferTransaction extends BaseTransaction {
}

public assetToJSON(): TransferAsset {
return {
...this.asset,
};
return this.asset;
}

public async prepare(store: StateStorePrepare): Promise<void> {
Expand All @@ -91,16 +89,10 @@ export class TransferTransaction extends BaseTransaction {

protected validateAsset(): ReadonlyArray<TransactionError> {
validator.validate(transferAssetFormatSchema, this.asset);
const errors = validator.errors
? validator.errors.map(
error =>
new TransactionError(
`'${error.dataPath}' ${error.message}`,
this.id,
error.dataPath,
),
)
: [];
const errors = convertToAssetError(
this.id,
validator.errors,
) as TransactionError[];

if (this.type !== TRANSACTION_TRANSFER_TYPE) {
errors.push(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
StateStorePrepare,
} from './base_transaction';
import { SIGNATURE_FEE } from './constants';
import { convertToTransactionError, TransactionError } from './errors';
import { convertToAssetError, TransactionError } from './errors';
import { TransactionJSON } from './transaction_types';
import { getId, validator } from './utils';

Expand Down Expand Up @@ -68,9 +68,7 @@ export class SecondSignatureTransaction extends BaseTransaction {
}

public assetToJSON(): object {
return {
...this.asset,
};
return this.asset;
}

public async prepare(store: StateStorePrepare): Promise<void> {
Expand Down Expand Up @@ -101,7 +99,7 @@ export class SecondSignatureTransaction extends BaseTransaction {

protected validateAsset(): ReadonlyArray<TransactionError> {
validator.validate(secondSignatureAssetFormatSchema, this.asset);
const errors = convertToTransactionError(
const errors = convertToAssetError(
this.id,
validator.errors,
) as TransactionError[];
Expand Down
8 changes: 3 additions & 5 deletions packages/lisk-transactions/src/2_delegate_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
StateStorePrepare,
} from './base_transaction';
import { DELEGATE_FEE } from './constants';
import { convertToTransactionError, TransactionError } from './errors';
import { convertToAssetError, TransactionError } from './errors';
import { Account, TransactionJSON } from './transaction_types';
import { validator } from './utils';

Expand Down Expand Up @@ -71,9 +71,7 @@ export class DelegateTransaction extends BaseTransaction {
}

public assetToJSON(): DelegateAsset {
return {
...this.asset,
};
return this.asset;
}

public async prepare(store: StateStorePrepare): Promise<void> {
Expand Down Expand Up @@ -107,7 +105,7 @@ export class DelegateTransaction extends BaseTransaction {

protected validateAsset(): ReadonlyArray<TransactionError> {
validator.validate(delegateAssetFormatSchema, this.asset);
const errors = convertToTransactionError(
const errors = convertToAssetError(
this.id,
validator.errors,
) as TransactionError[];
Expand Down
Loading

0 comments on commit e3e8eef

Please sign in to comment.