Skip to content

Commit

Permalink
fix(api-sync): update peers on header change (#778)
Browse files Browse the repository at this point in the history
* add PeerEvent.Updated

* handle PeerEvent.Updated

* remove assert

* fix tests

* Empty commit

---------

Co-authored-by: sebastijankuzner <[email protected]>
  • Loading branch information
oXtxNt9U and sebastijankuzner authored Nov 26, 2024
1 parent 7081777 commit f454817
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 1 deletion.
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

0 comments on commit f454817

Please sign in to comment.