Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api-sync): update peers on header change #778

Merged
merged 5 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/api-sync/source/listeners/peers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class Peers extends AbstractListener<Contracts.P2P.Peer, Models.Peer> {
protected getEventMapping(): ListenerEventMapping {
return {
[Events.PeerEvent.Added]: ListenerEvent.OnAdded,
[Events.PeerEvent.Updated]: ListenerEvent.OnAdded, // upsert
[Events.PeerEvent.Removed]: ListenerEvent.OnRemoved,
};
}
Expand Down
1 change: 1 addition & 0 deletions packages/contracts/source/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export enum PeerEvent {
Disconnected = "peer.disconnected",
Disconnecting = "peer.disconnecting",
Removed = "peer.removed",
Updated = "peer.updated",
}

export enum ConsensusEvent {
Expand Down
3 changes: 3 additions & 0 deletions packages/p2p/source/peer-repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ describe<{
sandbox: Sandbox;
peerRepository: PeerRepository;
}>("PeerRepository", ({ it, assert, beforeEach }) => {
const eventDispatcher = { dispatch: () => {}, listen: () => {} };

beforeEach((context) => {
context.sandbox = new Sandbox();

context.sandbox.app.bind(Identifiers.Services.Queue.Factory).toConstantValue({});
context.sandbox.app.bind(Identifiers.ServiceProvider.Configuration).toConstantValue({});
context.sandbox.app.bind(Identifiers.Services.EventDispatcher.Service).toConstantValue(eventDispatcher);

context.peerRepository = context.sandbox.app.resolve(PeerRepository);
});
Expand Down
2 changes: 2 additions & 0 deletions packages/p2p/source/peer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ describe<{
}>("Peer", ({ it, assert, beforeEach, each }) => {
const ip = "167.184.53.78";
const port = 4000;
const eventDispatcher = { dispatch: () => {}, listen: () => {} };

beforeEach((context) => {
context.sandbox = new Sandbox();
context.sandbox.app.bind(Identifiers.Services.Queue.Factory).toConstantValue({});
context.sandbox.app.bind(Identifiers.Services.EventDispatcher.Service).toConstantValue(eventDispatcher);

context.peer = context.sandbox.app.resolve(Peer).init(ip, port);
});
Expand Down
16 changes: 15 additions & 1 deletion packages/p2p/source/peer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { inject, injectable } from "@mainsail/container";
import { Contracts, Identifiers } from "@mainsail/contracts";
import { Contracts, Events, Identifiers } from "@mainsail/contracts";
import { Types, Utils } from "@mainsail/kernel";
import dayjs, { Dayjs } from "dayjs";

Expand All @@ -10,6 +10,9 @@ export class Peer implements Contracts.P2P.Peer {
@inject(Identifiers.Services.Queue.Factory)
private readonly createQueue!: Types.QueueFactory;

@inject(Identifiers.Services.EventDispatcher.Service)
private readonly events!: Contracts.Kernel.EventDispatcher;

public ip!: string;

public port!: number;
Expand Down Expand Up @@ -54,7 +57,18 @@ export class Peer implements Contracts.P2P.Peer {
}

public set header(header: Contracts.P2P.HeaderData) {
const isUpdate = this.#header !== undefined;

this.#header = header;

if (!isUpdate) {
return;
}

const changed = header.height !== this.#header.height || header.version !== this.#header.version;
if (changed) {
void this.events.dispatch(Events.PeerEvent.Updated, this);
}
}

public recentlyPinged(): boolean {
Expand Down
2 changes: 2 additions & 0 deletions packages/p2p/source/service-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ describe<{
const server = { boot: async () => {}, dispose: async () => {}, initialize: async () => {} };
const service = { boot: async () => {}, dispose: async () => {} };
const peerDisposer = { disposePeers: async () => {} };
const eventDispatcher = { dispatch: () => {}, listen: () => {} };

beforeEach((context) => {
context.sandbox = new Sandbox();

context.sandbox.app.bind(Identifiers.Services.Trigger.Service).toConstantValue(triggerService);
context.sandbox.app.bind(Identifiers.Cryptography.Validator).toConstantValue(validator);
context.sandbox.app.bind(Identifiers.Services.EventDispatcher.Service).toConstantValue(eventDispatcher);

context.serviceProvider = context.sandbox.app.resolve(ServiceProvider);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ describe<{
controller: GetPeersController;
}>("GetPeersController", ({ it, assert, beforeEach, stub }) => {
const peerRepository = { getPeers: () => {} };
const eventDispatcher = { dispatch: () => {}, listen: () => {} };

beforeEach((context) => {
context.sandbox = new Sandbox();

context.sandbox.app.bind(Identifiers.P2P.Peer.Repository).toConstantValue(peerRepository);
context.sandbox.app.bind(Identifiers.Services.EventDispatcher.Service).toConstantValue(eventDispatcher);
context.sandbox.app.bind(Identifiers.Services.Queue.Factory).toConstantValue({});

context.controller = context.sandbox.app.resolve(GetPeersController);
Expand Down
Loading