Skip to content

Commit afd1512

Browse files
authored
refactor(core-p2p): add internal contracts and delete obsolete ones
1 parent 0a4a659 commit afd1512

14 files changed

+67
-120
lines changed

packages/core-kernel/src/contracts/p2p/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
export * from "./index";
22
export * from "./network-monitor";
33
export * from "./network-state";
4-
export * from "./peer-communicator";
5-
export * from "./peer-connector";
6-
export * from "./peer-processor";
74
export * from "./peer-storage";
85
export * from "./peer-verifier";
96
export * from "./peer";

packages/core-kernel/src/contracts/p2p/network-monitor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ export interface INetworkMonitor {
3535
syncWithNetwork(fromBlockHeight: number, maxParallelDownloads?: number): Promise<Interfaces.IBlockData[]>;
3636
broadcastBlock(block: Interfaces.IBlock): Promise<void>;
3737
broadcastTransactions(transactions: Interfaces.ITransaction[]): Promise<void>;
38-
getServer(): SocketCluster;
39-
setServer(server: SocketCluster): void;
38+
getServer(): SocketCluster; // remove this
39+
setServer(server: SocketCluster): void; // remove this
4040
isColdStart(): boolean;
4141
completeColdStart(): void;
4242
stopServer(): void;

packages/core-kernel/src/contracts/p2p/network-state.ts

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,3 @@ export interface NetworkState {
1212
getOverHeightBlockHeaders();
1313
toJson();
1414
}
15-
16-
export interface QuorumDetails {
17-
/**
18-
* Number of peers on same height, with same block and same slot. Used for
19-
* quorum calculation.
20-
*/
21-
peersQuorum: number;
22-
23-
/**
24-
* Number of peers which do not meet the quorum requirements. Used for
25-
* quorum calculation.
26-
*/
27-
peersNoQuorum: number;
28-
29-
/**
30-
* Number of overheight peers.
31-
*/
32-
peersOverHeight: number;
33-
34-
/**
35-
* All overheight block headers grouped by id.
36-
*/
37-
peersOverHeightBlockHeaders: { [id: string]: any };
38-
39-
/**
40-
* The following properties are not mutual exclusive for a peer
41-
* and imply a peer is on the same `nodeHeight`.
42-
*/
43-
44-
/**
45-
* Number of peers that are on a different chain (forked).
46-
*/
47-
peersForked: number;
48-
49-
/**
50-
* Number of peers with a different slot.
51-
*/
52-
peersDifferentSlot: number;
53-
54-
/**
55-
* Number of peers where forging is not allowed.
56-
*/
57-
peersForgingNotAllowed: number;
58-
59-
getQuorum(): number;
60-
}

packages/core-kernel/src/contracts/p2p/peer-communicator.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

packages/core-kernel/src/contracts/p2p/peer-connector.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

packages/core-kernel/src/contracts/p2p/peer-processor.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/core-p2p/src/event-listener.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { Container, Contracts } from "@arkecosystem/core-kernel";
22

3+
import { PeerConnector } from "./peer-connector";
4+
35
// todo: review the implementation
46
@Container.injectable()
57
export class EventListener {
68
@Container.inject(Container.Identifiers.EventDispatcherService)
79
private readonly emitter!: Contracts.Kernel.Events.EventDispatcher;
810

911
@Container.inject(Container.Identifiers.PeerConnector)
10-
private readonly connector!: Contracts.P2P.PeerConnector;
12+
private readonly connector!: PeerConnector;
1113

1214
@Container.inject(Container.Identifiers.PeerStorage)
1315
private readonly storage!: Contracts.P2P.PeerStorage;

packages/core-p2p/src/network-monitor.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import SocketCluster from "socketcluster";
55

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

3133
@Container.inject(Container.Identifiers.PeerCommunicator)
32-
private readonly communicator!: Contracts.P2P.PeerCommunicator;
34+
private readonly communicator!: PeerCommunicator;
3335

3436
@Container.inject(Container.Identifiers.PeerProcessor)
35-
private readonly processor!: Contracts.P2P.PeerProcessor;
37+
private readonly processor!: PeerProcessor;
3638

3739
@Container.inject(Container.Identifiers.PeerStorage)
3840
private readonly storage!: Contracts.P2P.PeerStorage;

packages/core-p2p/src/network-state.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,47 @@ import { Crypto, Interfaces } from "@arkecosystem/crypto";
33

44
import { NetworkStateStatus } from "./enums";
55

6-
class QuorumDetails implements Contracts.P2P.QuorumDetails {
6+
class QuorumDetails {
7+
/**
8+
* Number of peers on same height, with same block and same slot. Used for
9+
* quorum calculation.
10+
*/
711
public peersQuorum = 0;
12+
13+
/**
14+
* Number of peers which do not meet the quorum requirements. Used for
15+
* quorum calculation.
16+
*/
817
public peersNoQuorum = 0;
18+
19+
/**
20+
* Number of overheight peers.
21+
*/
922
public peersOverHeight = 0;
23+
24+
/**
25+
* All overheight block headers grouped by id.
26+
*/
1027
public peersOverHeightBlockHeaders: { [id: string]: any } = {};
28+
29+
/**
30+
* The following properties are not mutual exclusive for a peer
31+
* and imply a peer is on the same `nodeHeight`.
32+
*/
33+
34+
/**
35+
* Number of peers that are on a different chain (forked).
36+
*/
1137
public peersForked = 0;
38+
39+
/**
40+
* Number of peers with a different slot.
41+
*/
1242
public peersDifferentSlot = 0;
43+
44+
/**
45+
* Number of peers where forging is not allowed.
46+
*/
1347
public peersForgingNotAllowed = 0;
1448

1549
public getQuorum() {

packages/core-p2p/src/peer-communicator.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import { SCClientSocket } from "socketcluster-client";
66
import { SocketErrors } from "./enums";
77
import { PeerPingTimeoutError, PeerStatusResponseError, PeerVerificationFailedError } from "./errors";
88
import { PeerConfig, PeerPingResponse } from "./interfaces";
9+
import { PeerConnector } from "./peer-connector";
910
import { PeerVerifier } from "./peer-verifier";
1011
import { createSchemas } from "./schemas";
1112
import { isValidVersion, socketEmit } from "./utils";
1213

1314
// todo: review the implementation
1415
@Container.injectable()
15-
export class PeerCommunicator implements Contracts.P2P.PeerCommunicator {
16+
export class PeerCommunicator {
1617
@Container.inject(Container.Identifiers.Application)
1718
private readonly app!: Contracts.Kernel.Application;
1819

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

2526
@Container.inject(Container.Identifiers.PeerConnector)
26-
private readonly connector!: Contracts.P2P.PeerConnector;
27+
private readonly connector!: PeerConnector;
2728

2829
public async downloadBlocks(peer: Contracts.P2P.Peer, fromBlockHeight: number): Promise<Interfaces.IBlockData[]> {
2930
this.logger.debug(`Downloading blocks from height ${fromBlockHeight.toLocaleString()} via ${peer.ip}`);

packages/core-p2p/src/peer-connector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { create, SCClientSocket } from "socketcluster-client";
33

44
// todo: review the implementation
55
@Container.injectable()
6-
export class PeerConnector implements Contracts.P2P.PeerConnector {
6+
export class PeerConnector {
77
@Container.inject(Container.Identifiers.Application)
88
private readonly app!: Contracts.Kernel.Application;
99

packages/core-p2p/src/peer-processor.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ import { Container, Contracts, Enums, Providers, Utils as AppUtils } from "@arke
22
import { Utils } from "@arkecosystem/crypto";
33

44
import { Peer } from "./peer";
5+
import { PeerConnector } from "./peer-connector";
6+
import { PeerCommunicator } from "./peer-communicator";
57
import { isValidVersion, isWhitelisted } from "./utils";
8+
import { AcceptNewPeerOptions } from "./types";
69

710
// todo: review the implementation
811
@Container.injectable()
9-
export class PeerProcessor implements Contracts.P2P.PeerProcessor {
12+
export class PeerProcessor {
1013
public server: any;
1114
public nextUpdateNetworkStatusScheduled: boolean = false;
1215

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

2225
@Container.inject(Container.Identifiers.PeerCommunicator)
23-
private readonly communicator!: Contracts.P2P.PeerCommunicator;
26+
private readonly communicator!: PeerCommunicator;
2427

2528
@Container.inject(Container.Identifiers.PeerConnector)
26-
private readonly connector!: Contracts.P2P.PeerConnector;
29+
private readonly connector!: PeerConnector;
2730

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

38-
public async validateAndAcceptPeer(
39-
peer: Contracts.P2P.Peer,
40-
options: Contracts.P2P.AcceptNewPeerOptions = {},
41-
): Promise<void> {
41+
public async validateAndAcceptPeer(peer: Contracts.P2P.Peer, options: AcceptNewPeerOptions = {}): Promise<void> {
4242
if (this.validatePeerIp(peer, options)) {
4343
await this.acceptNewPeer(peer, options);
4444
}
4545
}
4646

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

87-
private async acceptNewPeer(peer, options: Contracts.P2P.AcceptNewPeerOptions = {}): Promise<void> {
87+
private async acceptNewPeer(peer, options: AcceptNewPeerOptions = {}): Promise<void> {
8888
if (this.storage.getPeer(peer.ip)) {
8989
return;
9090
}

packages/core-p2p/src/peer-verifier.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import assert from "assert";
44
import { inspect } from "util";
55

66
import { Severity } from "./enums";
7+
import { PeerCommunicator } from "./peer-communicator";
78

8-
export class PeerVerificationResult implements Contracts.P2P.PeerVerificationResult {
9+
export class PeerVerificationResult {
910
public constructor(readonly myHeight: number, readonly hisHeight: number, readonly highestCommonHeight: number) {}
1011

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

33-
private communicator!: Contracts.P2P.PeerCommunicator;
34+
private communicator!: PeerCommunicator;
3435
private peer!: Contracts.P2P.Peer;
3536

3637
// // todo: make use of ioc
3738
// public constructor(
38-
// private readonly communicator: Contracts.P2P.PeerCommunicator,
39+
// private readonly communicator: PeerCommunicator,
3940
// private readonly peer: Contracts.P2P.Peer,
4041
// ) {
4142
// this.logPrefix = `Peer verify ${peer.ip}:`;
4243
// }
4344

44-
public init(communicator: Contracts.P2P.PeerCommunicator, peer: Contracts.P2P.Peer) {
45+
public init(communicator: PeerCommunicator, peer: Contracts.P2P.Peer) {
4546
this.communicator = communicator;
4647
this.peer = peer;
4748
this.database = this.app.get<Contracts.Database.DatabaseService>(Container.Identifiers.DatabaseService);

packages/core-p2p/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ export interface PeerService {
1111
processor: PeerProcessor;
1212
networkMonitor: NetworkMonitor;
1313
}
14+
15+
export interface AcceptNewPeerOptions {
16+
seed?: boolean;
17+
lessVerbose?: boolean;
18+
}

0 commit comments

Comments
 (0)