Skip to content

Commit

Permalink
Handle race condition where Alfie initiates reconciliation before Bet…
Browse files Browse the repository at this point in the history
…ty realises there is an intersection.
  • Loading branch information
sgwilym committed May 15, 2024
1 parent 7a6ebd3 commit 8016dd4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
63 changes: 63 additions & 0 deletions src/wgps/reconciliation/reconciler_map.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Deferred, deferred } from "../../../deps.ts";
import { WgpsMessageValidationError, WillowError } from "../../errors.ts";
import { Reconciler } from "./reconciler.ts";

Expand Down Expand Up @@ -28,6 +29,21 @@ export class ReconcilerMap<
>
>();

private eventuallyMap = new Map<
string,
Deferred<
Reconciler<
NamespaceId,
SubspaceId,
PayloadDigest,
AuthorisationOpts,
AuthorisationToken,
Prefingerprint,
Fingerprint
>
>
>();

addReconciler(
aoiHandleOurs: bigint,
aoiHandleTheirs: bigint,
Expand Down Expand Up @@ -64,12 +80,59 @@ export class ReconcilerMap<
newInnerMap.set(aoiHandleTheirs, reconciler);

this.map.set(aoiHandleOurs, newInnerMap);

const maybeEventually = this.eventuallyMap.get(
`${aoiHandleOurs}_${aoiHandleTheirs}`,
);

if (maybeEventually) {
maybeEventually.resolve(reconciler);
}
}

getReconcilerEventually(
aoiHandleOurs: bigint,
aoiHandleTheirs: bigint,
): Promise<
Reconciler<
NamespaceId,
SubspaceId,
PayloadDigest,
AuthorisationOpts,
AuthorisationToken,
Prefingerprint,
Fingerprint
>
> {
try {
return Promise.resolve(
this.getReconciler(aoiHandleOurs, aoiHandleTheirs),
);
} catch {
const promise = deferred<
Reconciler<
NamespaceId,
SubspaceId,
PayloadDigest,
AuthorisationOpts,
AuthorisationToken,
Prefingerprint,
Fingerprint
>
>();

this.eventuallyMap.set(`${aoiHandleOurs}_${aoiHandleTheirs}`, promise);

return promise;
}
}

getReconciler(aoiHandleOurs: bigint, aoiHandleTheirs: bigint) {
const innerMap = this.map.get(aoiHandleOurs);

if (!innerMap) {
console.log("oh shit!!!");

throw new WillowError(
"Could not dereference one of our AOI handles to a reconciler",
);
Expand Down
5 changes: 4 additions & 1 deletion src/wgps/wgps_messenger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,12 +731,14 @@ export class WgpsMessenger<

// Begin handling decoded messages
onAsyncIterate(decodedMessages, (msg) => {
/*
console.log(
`%c${this.transport.role === IS_ALFIE ? "Alfie" : "Betty"} got: ${
messageNames[msg.kind]
}`,
`color: ${this.transport.role === IS_ALFIE ? "red" : "blue"}`,
);
*/

if (msg.kind === MsgKind.DataSendEntry) {
this.currentlyReceivedEntry = msg.entry;
Expand Down Expand Up @@ -1315,7 +1317,8 @@ export class WgpsMessenger<
) {
switch (message.kind) {
case MsgKind.ReconciliationSendFingerprint: {
const reconciler = this.reconcilerMap.getReconciler(
// Alfie may have initiated reconciliation before we realised there was even an intersection, so we need to wait until we've realised that intersection exists as well.
const reconciler = await this.reconcilerMap.getReconcilerEventually(
message.receiverHandle,
message.senderHandle,
);
Expand Down

0 comments on commit 8016dd4

Please sign in to comment.