Skip to content

Commit

Permalink
feat: change vertexType to drpType, type to opType
Browse files Browse the repository at this point in the history
  • Loading branch information
anhnd350309 committed Jan 12, 2025
1 parent 85ae5dc commit 0d1544e
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 125 deletions.
6 changes: 3 additions & 3 deletions packages/blueprints/src/ACL/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,21 @@ export class ACL implements IACL, DRP {
if (!vertices[0].operation || !vertices[1].operation)
return { action: ActionType.Nop };
if (
vertices[0].operation.type === vertices[1].operation.type ||
vertices[0].operation.opType === vertices[1].operation.opType ||
vertices[0].operation.value !== vertices[1].operation.value
)
return { action: ActionType.Nop };

return this._conflictResolution === ACLConflictResolution.GrantWins
? {
action:
vertices[0].operation.type === "grant"
vertices[0].operation.opType === "grant"
? ActionType.DropRight
: ActionType.DropLeft,
}
: {
action:
vertices[0].operation.type === "grant"
vertices[0].operation.opType === "grant"
? ActionType.DropLeft
: ActionType.DropRight,
};
Expand Down
4 changes: 2 additions & 2 deletions packages/blueprints/src/AddWinsSet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ export class AddWinsSet<T> implements DRP {
if (
vertices[0].operation &&
vertices[1].operation &&
vertices[0].operation?.type !== vertices[1].operation?.type &&
vertices[0].operation?.opType !== vertices[1].operation?.opType &&
vertices[0].operation?.value === vertices[1].operation?.value
) {
return vertices[0].operation.type === "add"
return vertices[0].operation.opType === "add"
? { action: ActionType.DropRight }
: { action: ActionType.DropLeft };
}
Expand Down
12 changes: 6 additions & 6 deletions packages/blueprints/src/AddWinsSetWithACL/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ export class AddWinsSetWithACL<T> implements DRP {
if (!vertices[0].operation || !vertices[1].operation)
return { action: ActionType.Nop };
if (
vertices[0].operation.type === vertices[1].operation.type ||
vertices[0].operation.opType === vertices[1].operation.opType ||
vertices[0].operation.value !== vertices[1].operation.value
)
return { action: ActionType.Nop };

if (
["grant", "revoke"].includes(vertices[0].operation.type) &&
["grant", "revoke"].includes(vertices[1].operation.type)
["grant", "revoke"].includes(vertices[0].operation.opType) &&
["grant", "revoke"].includes(vertices[1].operation.opType)
) {
return this.acl.resolveConflicts(vertices);
}

if (
this.operations.includes(vertices[0].operation.type) &&
this.operations.includes(vertices[1].operation.type)
this.operations.includes(vertices[0].operation.opType) &&
this.operations.includes(vertices[1].operation.opType)
) {
return vertices[0].operation.type === "add"
return vertices[0].operation.opType === "add"
? { action: ActionType.DropRight }
: { action: ActionType.DropLeft };
}
Expand Down
42 changes: 21 additions & 21 deletions packages/network/src/proto/drp/object/v1/object_pb.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ export async function verifyIncomingVertices(
hash: vertex.hash,
peerId: vertex.peerId,
operation: {
type: vertex.operation?.type ?? "",
drpType: vertex.operation?.drpType ?? "",
opType: vertex.operation?.opType ?? "",
value: vertex.operation?.value,
vertexType: vertex.operation?.vertexType ?? "",
},
dependencies: vertex.dependencies,
timestamp: vertex.timestamp,
Expand Down
4 changes: 2 additions & 2 deletions packages/node/tests/node.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ACL } from "@topology-foundation/blueprints/src/ACL/index.js";
import { AddWinsSet } from "@topology-foundation/blueprints/src/index.js";
import { VertexTypeOperation } from "@topology-foundation/object/src/index.js";
import { DrpTypeOperation } from "@topology-foundation/object/src/index.js";
import { type DRP, DRPObject } from "@ts-drp/object";
import { beforeAll, beforeEach, describe, expect, test } from "vitest";
import {
Expand Down Expand Up @@ -108,7 +108,7 @@ describe("DPRNode with verify and sign signature", () => {
operation: {
type: "add",
value: 1,
vertexType: VertexTypeOperation.drp,
vertexType: DrpTypeOperation.Drp,
},
dependencies: [],
signature: "",
Expand Down
6 changes: 3 additions & 3 deletions packages/object/src/hashgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ export class HashGraph {
hash: HashGraph.rootHash,
peerId: "",
operation: {
type: OperationType.NOP,
drpType: "",
opType: OperationType.NOP,
value: null,
vertexType: "",
},
dependencies: [],
timestamp: -1,
Expand All @@ -104,7 +104,7 @@ export class HashGraph {
const vertex: Vertex = {
hash,
peerId: this.peerId,
operation: operation ?? { type: OperationType.NOP },
operation: operation ?? { opType: OperationType.NOP },
dependencies: deps,
timestamp: currentTimestamp,
signature: "",
Expand Down
44 changes: 21 additions & 23 deletions packages/object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ export interface LcaAndOperations {

export let log: Logger;

export enum VertexTypeOperation {
acl = "acl",
drp = "drp",
export enum DrpTypeOperation {
Acl = "ACL",
Drp = "DRP",
}

export class DRPObject implements IDRPObject {
Expand Down Expand Up @@ -103,10 +103,10 @@ export class DRPObject implements IDRPObject {
this.bytecode = new Uint8Array();
this.vertices = [];
this.drp = drp
? new Proxy(drp, this.proxyDRPHandler(VertexTypeOperation.drp))
? new Proxy(drp, this.proxyDRPHandler(DrpTypeOperation.Drp))
: null;
this.acl = acl
? new Proxy(acl, this.proxyDRPHandler(VertexTypeOperation.acl))
? new Proxy(acl, this.proxyDRPHandler(DrpTypeOperation.Acl))
: null;
this.hashGraph = new HashGraph(
peerId,
Expand All @@ -124,7 +124,7 @@ export class DRPObject implements IDRPObject {
resolveConflicts(vertices: Vertex[]): ResolveConflictsType {
if (
this.acl &&
vertices.some((v) => v.operation?.vertexType === VertexTypeOperation.acl)
vertices.some((v) => v.operation?.drpType === DrpTypeOperation.Acl)
) {
const acl = this.acl as IACL & DRP;
return acl.resolveConflicts(vertices);
Expand All @@ -134,7 +134,7 @@ export class DRPObject implements IDRPObject {
}

// This function is black magic, it allows us to intercept calls to the DRP object
proxyDRPHandler(vertexType: VertexTypeOperation): ProxyHandler<object> {
proxyDRPHandler(vertexType: DrpTypeOperation): ProxyHandler<object> {
const obj = this;
return {
get(target, propKey, receiver) {
Expand Down Expand Up @@ -174,17 +174,17 @@ export class DRPObject implements IDRPObject {
fn: string,
// biome-ignore lint: value can't be unknown because of protobuf
args: any,
vertexType: VertexTypeOperation,
drpType: DrpTypeOperation,
) {
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
let preOperationDRP: any;
if (vertexType === VertexTypeOperation.acl) {
if (drpType === DrpTypeOperation.Acl) {
preOperationDRP = this._computeACL(this.hashGraph.getFrontier());
} else {
preOperationDRP = this._computeDRP(this.hashGraph.getFrontier());
}
const drp = cloneDeep(preOperationDRP);
this._applyOperation(drp, { type: fn, value: args, vertexType });
this._applyOperation(drp, { opType: fn, value: args, drpType });

let stateChanged = false;
for (const key of Object.keys(preOperationDRP)) {
Expand All @@ -199,9 +199,9 @@ export class DRPObject implements IDRPObject {
}

const vertex = this.hashGraph.addToFrontier({
type: fn,
drpType: drpType,
opType: fn,
value: args,
vertexType,
});

this._setState(vertex, this._getDRPState(drp));
Expand Down Expand Up @@ -235,7 +235,7 @@ export class DRPObject implements IDRPObject {
}
const preComputeLca = this.computeLCA(vertex.dependencies);

if (vertex.operation.vertexType === VertexTypeOperation.drp) {
if (vertex.operation.drpType === DrpTypeOperation.Drp) {
const drp = this._computeDRP(vertex.dependencies, preComputeLca);
this.hashGraph.addVertex(
vertex.operation,
Expand Down Expand Up @@ -293,21 +293,21 @@ export class DRPObject implements IDRPObject {

// apply the operation to the DRP
private _applyOperation(drp: DRP, operation: Operation) {
const { type, value } = operation;
const { opType, value } = operation;

const typeParts = type.split(".");
const typeParts = opType.split(".");
// biome-ignore lint: target can be anything
let target: any = drp;
for (let i = 0; i < typeParts.length - 1; i++) {
target = target[typeParts[i]];
if (!target) {
throw new Error(`Invalid operation type: ${type}`);
throw new Error(`Invalid operation type: ${opType}`);
}
}

const methodName = typeParts[typeParts.length - 1];
if (typeof target[methodName] !== "function") {
throw new Error(`${type} is not a function`);
throw new Error(`${opType} is not a function`);
}

const args = Array.isArray(value) ? value : [value];
Expand Down Expand Up @@ -337,11 +337,10 @@ export class DRPObject implements IDRPObject {
}

for (const op of linearizedOperations) {
op.vertexType === VertexTypeOperation.drp &&
this._applyOperation(drp, op);
op.drpType === DrpTypeOperation.Drp && this._applyOperation(drp, op);
}
if (vertexOperation) {
vertexOperation.vertexType === VertexTypeOperation.drp &&
vertexOperation.drpType === DrpTypeOperation.Drp &&
this._applyOperation(drp, vertexOperation);
}

Expand Down Expand Up @@ -369,11 +368,10 @@ export class DRPObject implements IDRPObject {
acl[key] = value;
}
for (const op of linearizedOperations) {
op.vertexType === VertexTypeOperation.acl &&
this._applyOperation(acl, op);
op.drpType === DrpTypeOperation.Acl && this._applyOperation(acl, op);
}
if (vertexOperation) {
vertexOperation.vertexType === VertexTypeOperation.acl &&
vertexOperation.drpType === DrpTypeOperation.Acl &&
this._applyOperation(acl, vertexOperation);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/object/src/proto/drp/object/v1/object.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import "google/protobuf/struct.proto";
// Supposed to be the RIBLT stuff
message Vertex {
message Operation {
string type = 1;
google.protobuf.Value value = 2;
string vertex_type = 3;
string drp_type = 1;
string op_type = 2;
google.protobuf.Value value = 3;
}
string hash = 1;
string peer_id = 2;
Expand Down
Loading

0 comments on commit 0d1544e

Please sign in to comment.