Skip to content

Commit

Permalink
Add WGPS reconciliation tracking via events
Browse files Browse the repository at this point in the history
  • Loading branch information
xash committed Nov 21, 2024
1 parent b9e96fd commit 76488a8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/wgps/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class WgpsStatusEvent
extends CustomEvent<{ todo: number; all: number }> {
constructor(todo: number, all: number) {
super("status", {
detail: {
todo,
all,
},
});
}
}
19 changes: 18 additions & 1 deletion src/wgps/wgps_messenger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from "./types.ts";
import type { Intersection } from "./pai/types.ts";
import { onAsyncIterate } from "./util.ts";
import { WgpsStatusEvent } from "./events.ts";

import { GuaranteedQueue } from "./guaranteed_queue.ts";
import { AoiIntersectionFinder } from "./reconciliation/aoi_intersection_finder.ts";
Expand Down Expand Up @@ -186,7 +187,7 @@ export class WgpsMessenger<
SubspaceId,
PayloadDigest,
AuthorisationOpts,
> {
> extends EventTarget {
private closed = false;

private interests: Map<
Expand Down Expand Up @@ -436,6 +437,8 @@ export class WgpsMessenger<
AuthorisationOpts
>,
) {
super();

if (opts.maxPayloadSizePower < 0 || opts.maxPayloadSizePower > 64) {
throw new ValidationError(
"maxPayloadSizePower must be a natural number less than or equal to 64",
Expand Down Expand Up @@ -1065,6 +1068,8 @@ export class WgpsMessenger<
kind: MsgKind.ReconciliationTerminatePayload,
});
}

this.dispatchEvent(this.status());
});

// Whenever the area of interest intersection finder finds an intersection from the setup phase...
Expand Down Expand Up @@ -1120,6 +1125,7 @@ export class WgpsMessenger<
});

this.myRangeCounter.getNext();
this.dispatchEvent(this.status());
},
);

Expand Down Expand Up @@ -1353,6 +1359,7 @@ export class WgpsMessenger<
Number(this.yourRangeCounter.getNext()),
);

this.dispatchEvent(this.status());
break;
}
case MsgKind.ReconciliationAnnounceEntries: {
Expand Down Expand Up @@ -1391,6 +1398,7 @@ export class WgpsMessenger<
});
}

this.dispatchEvent(this.status());
break;
}
case MsgKind.ReconciliationSendEntry: {
Expand Down Expand Up @@ -1684,6 +1692,15 @@ export class WgpsMessenger<
this.handlesStaticTokensTheirs.bind(message.staticToken);
}

status(): WgpsStatusEvent {
const yourInfo = this.yourRangeCounter.info();
const myInfo = this.myRangeCounter.info();
return new WgpsStatusEvent(
Number(yourInfo.todo + myInfo.todo),
Number(yourInfo.all + myInfo.all),
);
}

close() {
this.closed = true;
this.transport.close();
Expand Down

0 comments on commit 76488a8

Please sign in to comment.