Skip to content

Commit

Permalink
refactor(core-p2p): add internal contracts and delete obsolete ones
Browse files Browse the repository at this point in the history
  • Loading branch information
faustbrian authored Nov 7, 2019
1 parent 0a4a659 commit afd1512
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 120 deletions.
3 changes: 0 additions & 3 deletions packages/core-kernel/src/contracts/p2p/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
export * from "./index";
export * from "./network-monitor";
export * from "./network-state";
export * from "./peer-communicator";
export * from "./peer-connector";
export * from "./peer-processor";
export * from "./peer-storage";
export * from "./peer-verifier";
export * from "./peer";
Expand Down
4 changes: 2 additions & 2 deletions packages/core-kernel/src/contracts/p2p/network-monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export interface INetworkMonitor {
syncWithNetwork(fromBlockHeight: number, maxParallelDownloads?: number): Promise<Interfaces.IBlockData[]>;
broadcastBlock(block: Interfaces.IBlock): Promise<void>;
broadcastTransactions(transactions: Interfaces.ITransaction[]): Promise<void>;
getServer(): SocketCluster;
setServer(server: SocketCluster): void;
getServer(): SocketCluster; // remove this
setServer(server: SocketCluster): void; // remove this
isColdStart(): boolean;
completeColdStart(): void;
stopServer(): void;
Expand Down
46 changes: 0 additions & 46 deletions packages/core-kernel/src/contracts/p2p/network-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,3 @@ export interface NetworkState {
getOverHeightBlockHeaders();
toJson();
}

export interface QuorumDetails {
/**
* Number of peers on same height, with same block and same slot. Used for
* quorum calculation.
*/
peersQuorum: number;

/**
* Number of peers which do not meet the quorum requirements. Used for
* quorum calculation.
*/
peersNoQuorum: number;

/**
* Number of overheight peers.
*/
peersOverHeight: number;

/**
* All overheight block headers grouped by id.
*/
peersOverHeightBlockHeaders: { [id: string]: any };

/**
* The following properties are not mutual exclusive for a peer
* and imply a peer is on the same `nodeHeight`.
*/

/**
* Number of peers that are on a different chain (forked).
*/
peersForked: number;

/**
* Number of peers with a different slot.
*/
peersDifferentSlot: number;

/**
* Number of peers where forging is not allowed.
*/
peersForgingNotAllowed: number;

getQuorum(): number;
}
22 changes: 0 additions & 22 deletions packages/core-kernel/src/contracts/p2p/peer-communicator.ts

This file was deleted.

18 changes: 0 additions & 18 deletions packages/core-kernel/src/contracts/p2p/peer-connector.ts

This file was deleted.

9 changes: 0 additions & 9 deletions packages/core-kernel/src/contracts/p2p/peer-processor.ts

This file was deleted.

4 changes: 3 additions & 1 deletion packages/core-p2p/src/event-listener.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Container, Contracts } from "@arkecosystem/core-kernel";

import { PeerConnector } from "./peer-connector";

// todo: review the implementation
@Container.injectable()
export class EventListener {
@Container.inject(Container.Identifiers.EventDispatcherService)
private readonly emitter!: Contracts.Kernel.Events.EventDispatcher;

@Container.inject(Container.Identifiers.PeerConnector)
private readonly connector!: Contracts.P2P.PeerConnector;
private readonly connector!: PeerConnector;

@Container.inject(Container.Identifiers.PeerStorage)
private readonly storage!: Contracts.P2P.PeerStorage;
Expand Down
6 changes: 4 additions & 2 deletions packages/core-p2p/src/network-monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import SocketCluster from "socketcluster";

import { PeerData } from "./interfaces";
import { NetworkState } from "./network-state";
import { PeerCommunicator } from "./peer-communicator";
import { PeerProcessor } from "./peer-processor";
import { RateLimiter } from "./rate-limiter";
import { checkDNS, checkNTP } from "./utils";
import { buildRateLimiter } from "./utils/build-rate-limiter";
Expand All @@ -29,10 +31,10 @@ export class NetworkMonitor implements Contracts.P2P.INetworkMonitor {
private readonly emitter!: Contracts.Kernel.Events.EventDispatcher;

@Container.inject(Container.Identifiers.PeerCommunicator)
private readonly communicator!: Contracts.P2P.PeerCommunicator;
private readonly communicator!: PeerCommunicator;

@Container.inject(Container.Identifiers.PeerProcessor)
private readonly processor!: Contracts.P2P.PeerProcessor;
private readonly processor!: PeerProcessor;

@Container.inject(Container.Identifiers.PeerStorage)
private readonly storage!: Contracts.P2P.PeerStorage;
Expand Down
36 changes: 35 additions & 1 deletion packages/core-p2p/src/network-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,47 @@ import { Crypto, Interfaces } from "@arkecosystem/crypto";

import { NetworkStateStatus } from "./enums";

class QuorumDetails implements Contracts.P2P.QuorumDetails {
class QuorumDetails {
/**
* Number of peers on same height, with same block and same slot. Used for
* quorum calculation.
*/
public peersQuorum = 0;

/**
* Number of peers which do not meet the quorum requirements. Used for
* quorum calculation.
*/
public peersNoQuorum = 0;

/**
* Number of overheight peers.
*/
public peersOverHeight = 0;

/**
* All overheight block headers grouped by id.
*/
public peersOverHeightBlockHeaders: { [id: string]: any } = {};

/**
* The following properties are not mutual exclusive for a peer
* and imply a peer is on the same `nodeHeight`.
*/

/**
* Number of peers that are on a different chain (forked).
*/
public peersForked = 0;

/**
* Number of peers with a different slot.
*/
public peersDifferentSlot = 0;

/**
* Number of peers where forging is not allowed.
*/
public peersForgingNotAllowed = 0;

public getQuorum() {
Expand Down
5 changes: 3 additions & 2 deletions packages/core-p2p/src/peer-communicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import { SCClientSocket } from "socketcluster-client";
import { SocketErrors } from "./enums";
import { PeerPingTimeoutError, PeerStatusResponseError, PeerVerificationFailedError } from "./errors";
import { PeerConfig, PeerPingResponse } from "./interfaces";
import { PeerConnector } from "./peer-connector";
import { PeerVerifier } from "./peer-verifier";
import { createSchemas } from "./schemas";
import { isValidVersion, socketEmit } from "./utils";

// todo: review the implementation
@Container.injectable()
export class PeerCommunicator implements Contracts.P2P.PeerCommunicator {
export class PeerCommunicator {
@Container.inject(Container.Identifiers.Application)
private readonly app!: Contracts.Kernel.Application;

Expand All @@ -23,7 +24,7 @@ export class PeerCommunicator implements Contracts.P2P.PeerCommunicator {
private readonly emitter!: Contracts.Kernel.Events.EventDispatcher;

@Container.inject(Container.Identifiers.PeerConnector)
private readonly connector!: Contracts.P2P.PeerConnector;
private readonly connector!: PeerConnector;

public async downloadBlocks(peer: Contracts.P2P.Peer, fromBlockHeight: number): Promise<Interfaces.IBlockData[]> {
this.logger.debug(`Downloading blocks from height ${fromBlockHeight.toLocaleString()} via ${peer.ip}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/core-p2p/src/peer-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { create, SCClientSocket } from "socketcluster-client";

// todo: review the implementation
@Container.injectable()
export class PeerConnector implements Contracts.P2P.PeerConnector {
export class PeerConnector {
@Container.inject(Container.Identifiers.Application)
private readonly app!: Contracts.Kernel.Application;

Expand Down
18 changes: 9 additions & 9 deletions packages/core-p2p/src/peer-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { Container, Contracts, Enums, Providers, Utils as AppUtils } from "@arke
import { Utils } from "@arkecosystem/crypto";

import { Peer } from "./peer";
import { PeerConnector } from "./peer-connector";
import { PeerCommunicator } from "./peer-communicator";
import { isValidVersion, isWhitelisted } from "./utils";
import { AcceptNewPeerOptions } from "./types";

// todo: review the implementation
@Container.injectable()
export class PeerProcessor implements Contracts.P2P.PeerProcessor {
export class PeerProcessor {
public server: any;
public nextUpdateNetworkStatusScheduled: boolean = false;

Expand All @@ -20,10 +23,10 @@ export class PeerProcessor implements Contracts.P2P.PeerProcessor {
private readonly emitter!: Contracts.Kernel.Events.EventDispatcher;

@Container.inject(Container.Identifiers.PeerCommunicator)
private readonly communicator!: Contracts.P2P.PeerCommunicator;
private readonly communicator!: PeerCommunicator;

@Container.inject(Container.Identifiers.PeerConnector)
private readonly connector!: Contracts.P2P.PeerConnector;
private readonly connector!: PeerConnector;

@Container.inject(Container.Identifiers.PeerStorage)
private readonly storage!: Contracts.P2P.PeerStorage;
Expand All @@ -35,16 +38,13 @@ export class PeerProcessor implements Contracts.P2P.PeerProcessor {
this.emitter.listen("internal.milestone.changed", () => this.updatePeersAfterMilestoneChange());
}

public async validateAndAcceptPeer(
peer: Contracts.P2P.Peer,
options: Contracts.P2P.AcceptNewPeerOptions = {},
): Promise<void> {
public async validateAndAcceptPeer(peer: Contracts.P2P.Peer, options: AcceptNewPeerOptions = {}): Promise<void> {
if (this.validatePeerIp(peer, options)) {
await this.acceptNewPeer(peer, options);
}
}

public validatePeerIp(peer, options: Contracts.P2P.AcceptNewPeerOptions = {}): boolean {
public validatePeerIp(peer, options: AcceptNewPeerOptions = {}): boolean {
if (this.getConfig("disableDiscovery") && !this.storage.hasPendingPeer(peer.ip)) {
this.logger.warning(`Rejected ${peer.ip} because the relay is in non-discovery mode.`);
return false;
Expand Down Expand Up @@ -84,7 +84,7 @@ export class PeerProcessor implements Contracts.P2P.PeerProcessor {
}
}

private async acceptNewPeer(peer, options: Contracts.P2P.AcceptNewPeerOptions = {}): Promise<void> {
private async acceptNewPeer(peer, options: AcceptNewPeerOptions = {}): Promise<void> {
if (this.storage.getPeer(peer.ip)) {
return;
}
Expand Down
9 changes: 5 additions & 4 deletions packages/core-p2p/src/peer-verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import assert from "assert";
import { inspect } from "util";

import { Severity } from "./enums";
import { PeerCommunicator } from "./peer-communicator";

export class PeerVerificationResult implements Contracts.P2P.PeerVerificationResult {
export class PeerVerificationResult {
public constructor(readonly myHeight: number, readonly hisHeight: number, readonly highestCommonHeight: number) {}

get forked(): boolean {
Expand All @@ -30,18 +31,18 @@ export class PeerVerifier {
private logger!: Contracts.Kernel.Log.Logger;
private logPrefix!: string;

private communicator!: Contracts.P2P.PeerCommunicator;
private communicator!: PeerCommunicator;
private peer!: Contracts.P2P.Peer;

// // todo: make use of ioc
// public constructor(
// private readonly communicator: Contracts.P2P.PeerCommunicator,
// private readonly communicator: PeerCommunicator,
// private readonly peer: Contracts.P2P.Peer,
// ) {
// this.logPrefix = `Peer verify ${peer.ip}:`;
// }

public init(communicator: Contracts.P2P.PeerCommunicator, peer: Contracts.P2P.Peer) {
public init(communicator: PeerCommunicator, peer: Contracts.P2P.Peer) {
this.communicator = communicator;
this.peer = peer;
this.database = this.app.get<Contracts.Database.DatabaseService>(Container.Identifiers.DatabaseService);
Expand Down
5 changes: 5 additions & 0 deletions packages/core-p2p/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ export interface PeerService {
processor: PeerProcessor;
networkMonitor: NetworkMonitor;
}

export interface AcceptNewPeerOptions {
seed?: boolean;
lessVerbose?: boolean;
}

0 comments on commit afd1512

Please sign in to comment.