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: duplicate DRP call #432

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
74 changes: 45 additions & 29 deletions packages/object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ export interface DRPObjectConfig {
finality_config?: FinalityConfig;
}

export interface callFnResult {
drp: DRP;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
appliedOperationResult: any;
isACL: boolean;
}

export let log: Logger;

export class DRPObject implements ObjectPb.DRPObjectBase {
Expand Down Expand Up @@ -156,7 +163,16 @@ export class DRPObject implements ObjectPb.DRPObjectBase {
if (callerName?.startsWith("DRPObject.resolveConflicts")) {
return Reflect.apply(applyTarget, thisArg, args);
}
if (!callerName?.startsWith("Proxy.")) obj.callFn(fullPropKey, args, vertexType);
if (!callerName?.startsWith("Proxy.")) {
const {
drp: newDRP,
appliedOperationResult,
isACL,
} = obj.callFn(fullPropKey, args, vertexType);
if (!isACL) Object.assign(obj.drp as DRP, newDRP);
trungnotchung marked this conversation as resolved.
Show resolved Hide resolved
else Object.assign(obj.acl as ObjectACL, newDRP);
return appliedOperationResult;
}
return Reflect.apply(applyTarget, thisArg, args);
},
});
Expand All @@ -172,54 +188,54 @@ export class DRPObject implements ObjectPb.DRPObjectBase {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
args: any,
drpType: DrpType
) {
): callFnResult {
if (!this.hashGraph) {
throw new Error("Hashgraph is undefined");
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let preOperationDRP: any;
if (drpType === DrpType.ACL) {
preOperationDRP = this._computeObjectACL(this.hashGraph.getFrontier());
} else {
preOperationDRP = this._computeDRP(this.hashGraph.getFrontier());
}

const isACL = drpType === DrpType.ACL;
const computeFn = isACL ? this._computeObjectACL : this._computeDRP;
const vertexDependencies = this.hashGraph.getFrontier();
const preOperationDRP = computeFn(vertexDependencies);
trungnotchung marked this conversation as resolved.
Show resolved Hide resolved

const drp = cloneDeep(preOperationDRP);
const appliedOperationResult: callFnResult = { isACL, drp, appliedOperationResult: undefined };
try {
this._applyOperation(drp, { opType: fn, value: args, drpType });
appliedOperationResult.appliedOperationResult = this._applyOperation(drp, {
opType: fn,
value: args,
drpType,
});
} catch (e) {
log.error(`::drpObject::callFn: ${e}`);
return;
}

let stateChanged = false;
for (const key of Object.keys(preOperationDRP)) {
if (!deepEqual(preOperationDRP[key], drp[key])) {
stateChanged = true;
break;
}
return appliedOperationResult;
}

const stateChanged = Object.keys(preOperationDRP).some(
(key) => !deepEqual(preOperationDRP[key], drp[key])
);
trungnotchung marked this conversation as resolved.
Show resolved Hide resolved
if (!stateChanged) {
return;
return appliedOperationResult;
}

const vertexTimestamp = Date.now();
const vertexOperation = { drpType: drpType, opType: fn, value: args };
const vertexDependencies = this.hashGraph.getFrontier();
const vertex = ObjectPb.Vertex.create({
hash: computeHash(this.peerId, vertexOperation, vertexDependencies, vertexTimestamp),
peerId: this.peerId,
operation: vertexOperation,
dependencies: vertexDependencies,
timestamp: vertexTimestamp,
});
this.hashGraph.addToFrontier(vertex);

this.hashGraph.addToFrontier(vertex);
this._setState(vertex, this._getDRPState(drp));
this._initializeFinalityState(vertex.hash);

this.vertices.push(vertex);
this._notify("callFn", [vertex]);

return appliedOperationResult;
}

/* Merges the vertices into the hashgraph
Expand Down Expand Up @@ -334,18 +350,18 @@ export class DRPObject implements ObjectPb.DRPObjectBase {
}

try {
target[methodName](...value);
return target[methodName](...value);
} catch (e) {
throw new Error(`Error while applying operation ${opType}: ${e}`);
}
}

// compute the DRP based on all dependencies of the current vertex using partial linearization
private _computeDRP(
private _computeDRP = (
vertexDependencies: Hash[],
preCompute?: LcaAndOperations,
vertexOperation?: Operation
): DRP {
): DRP => {
trungnotchung marked this conversation as resolved.
Show resolved Hide resolved
if (!this.drp || !this.originalDRP) {
throw new Error("DRP is undefined");
}
Expand Down Expand Up @@ -375,13 +391,13 @@ export class DRPObject implements ObjectPb.DRPObjectBase {
}

return drp;
}
};

private _computeObjectACL(
private _computeObjectACL = (
vertexDependencies: Hash[],
preCompute?: LcaAndOperations,
vertexOperation?: Operation
): DRP {
): DRP => {
if (!this.acl || !this.originalObjectACL) {
throw new Error("ObjectACL is undefined");
}
Expand Down Expand Up @@ -410,7 +426,7 @@ export class DRPObject implements ObjectPb.DRPObjectBase {
}

return acl;
}
};

private computeLCA(vertexDependencies: string[]) {
if (!this.hashGraph) {
Expand Down
44 changes: 43 additions & 1 deletion packages/object/tests/drpobject.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { beforeEach, describe, expect, test } from "vitest";

import { DRPObject } from "../src/index.js";
import { SemanticsType } from "../dist/src/hashgraph/index.js";
import { ActionType } from "../dist/src/hashgraph/index.js";
import { DRP, DRPObject, ResolveConflictsType, Vertex } from "../src/index.js";

describe("AccessControl tests with RevokeWins resolution", () => {
beforeEach(() => {});
Expand All @@ -25,3 +27,43 @@ describe("AccessControl tests with RevokeWins resolution", () => {
expect(obj.drp).toBeUndefined();
});
});

describe("Hi", () => {
hoangquocvietuet marked this conversation as resolved.
Show resolved Hide resolved
let counter = 0;

class CounterDRP implements DRP {
semanticsType = SemanticsType.pair;

private _counter: number;

constructor() {
this._counter = 0;
}

test() {
this._counter++;
counter++;
return this._counter;
}

resolveConflicts(_: Vertex[]): ResolveConflictsType {
return { action: ActionType.Nop };
}
}

test("Detect duplicate call", () => {
const obj = new DRPObject({
peerId: "",
publicCredential: {
ed25519PublicKey: "cred",
blsPublicKey: "cred",
},
drp: new CounterDRP(),
});

const testDRP = obj.drp as CounterDRP;
expect(testDRP).toBeDefined();
const ret = testDRP.test();
expect(ret).toBe(counter);
});
});