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

refactor: use types #495

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/grid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"dependencies": {
"@ts-drp/node": "0.7.0",
"@ts-drp/object": "0.7.0",
"@ts-drp/tracer": "^0.7.0"
"@ts-drp/tracer": "^0.7.0",
"@ts-drp/types": "^0.7.0"
},
"devDependencies": {
"@types/node": "^22.5.4",
Expand Down
10 changes: 2 additions & 8 deletions examples/grid/src/objects/grid.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import {
ActionType,
type DRP,
type ResolveConflictsType,
SemanticsType,
type Vertex,
} from "@ts-drp/object";

import { ActionType, type DRP, type ResolveConflictsType, SemanticsType } from "@ts-drp/object";
import { type Vertex } from "@ts-drp/types";
export class Grid implements DRP {
semanticsType: SemanticsType = SemanticsType.pair;
positions: Map<string, { x: number; y: number }>;
Expand Down
69 changes: 35 additions & 34 deletions packages/blueprints/package.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
{
"name": "@ts-drp/blueprints",
"version": "0.7.0",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/topology-foundation/ts-drp.git"
},
"type": "module",
"types": "./dist/src/index.d.ts",
"files": [
"src",
"dist",
"!dist/test",
"!**/*.tsbuildinfo"
],
"exports": {
".": {
"types": "./dist/src/index.d.ts",
"import": "./dist/src/index.js"
}
},
"scripts": {
"build": "tsc -b",
"clean": "rm -rf dist/ node_modules/",
"prepack": "tsc -b",
"test": "vitest",
"watch": "tsc -b -w"
},
"devDependencies": {
"@ts-drp/object": "0.7.0"
},
"dependencies": {
"@thi.ng/random": "^4.1.0"
}
"name": "@ts-drp/blueprints",
"version": "0.7.0",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/topology-foundation/ts-drp.git"
},
"type": "module",
"types": "./dist/src/index.d.ts",
"files": [
"src",
"dist",
"!dist/test",
"!**/*.tsbuildinfo"
],
"exports": {
".": {
"types": "./dist/src/index.d.ts",
"import": "./dist/src/index.js"
}
},
"scripts": {
"build": "tsc -b",
"clean": "rm -rf dist/ node_modules/",
"prepack": "tsc -b",
"test": "vitest",
"watch": "tsc -b -w"
},
"devDependencies": {
"@ts-drp/object": "0.7.0"
},
"dependencies": {
"@thi.ng/random": "^4.1.0",
"@ts-drp/types": "^0.7.0"
}
}
9 changes: 2 additions & 7 deletions packages/blueprints/src/AddMul/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import {
ActionType,
type DRP,
type ResolveConflictsType,
SemanticsType,
type Vertex,
} from "@ts-drp/object";
import { ActionType, type DRP, type ResolveConflictsType, SemanticsType } from "@ts-drp/object";
import { type Vertex } from "@ts-drp/types";

export class AddMulDRP implements DRP {
semanticsType = SemanticsType.pair;
Expand Down
9 changes: 2 additions & 7 deletions packages/blueprints/src/Map/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import {
ActionType,
type DRP,
type ResolveConflictsType,
SemanticsType,
type Vertex,
} from "@ts-drp/object";
import { ActionType, type DRP, type ResolveConflictsType, SemanticsType } from "@ts-drp/object";
import { type Vertex } from "@ts-drp/types";

export enum MapConflictResolution {
SetWins = 0,
Expand Down
6 changes: 3 additions & 3 deletions packages/network/tests/network.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GossipSub, MeshPeer } from "@chainsafe/libp2p-gossipsub";
import { Connection, IdentifyResult, Libp2p, SubscriptionChangeData } from "@libp2p/interface";
import { loadConfig } from "@ts-drp/node/src/config.js";
import { MessagesPb as NetworkPb } from "@ts-drp/types";
import { Message } from "@ts-drp/types";
import { raceEvent } from "race-event";
import { beforeAll, describe, expect, test, afterAll } from "vitest";

Expand Down Expand Up @@ -89,7 +89,7 @@ describe("DRPNetworkNode can connect & send messages", () => {
node2
.addMessageHandler(async ({ stream }) => {
const byteArray = await streamToUint8Array(stream);
const message = NetworkPb.Message.decode(byteArray);
const message = Message.decode(byteArray);
expect(Buffer.from(message.data).toString("utf-8")).toBe(data);
boolean = true;
resolve(true);
Expand Down Expand Up @@ -131,7 +131,7 @@ describe("DRPNetworkNode can connect & send messages", () => {
node2.subscribe(group);
const messageProcessed = new Promise((resolve) => {
node2.addGroupMessageHandler(group, async (e) => {
const message = NetworkPb.Message.decode(e.detail.msg.data);
const message = Message.decode(e.detail.msg.data);
expect(Buffer.from(message.data).toString("utf-8")).toBe(data);
boolean = true;
resolve(true);
Expand Down
3 changes: 2 additions & 1 deletion packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Stream } from "@libp2p/interface";
import { streamToUint8Array } from "@ts-drp/network";
import { type ACL, type DRPObject, HashGraph, type Vertex } from "@ts-drp/object";
import { type ACL, type DRPObject, HashGraph } from "@ts-drp/object";
import { type Vertex } from "@ts-drp/types";
import {
AggregatedAttestation,
Attestation,
Expand Down
37 changes: 19 additions & 18 deletions packages/node/tests/handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { SetDRP } from "@ts-drp/blueprints";
import { DRPNetworkNode, type DRPNetworkNodeConfig } from "@ts-drp/network";
import { DrpType } from "@ts-drp/object";
import { type DRPObject, ObjectACL } from "@ts-drp/object";
import { MessagesPb as NetworkPb } from "@ts-drp/types";
import { AttestationUpdate, Message, Sync, SyncAccept, Update } from "@ts-drp/types";
import { MessageType } from "@ts-drp/types/src/index.js";
import { raceEvent } from "race-event";
import { beforeAll, describe, expect, test, afterAll, vi } from "vitest";

Expand All @@ -24,7 +25,7 @@ describe("drpMessagesHandler inputs", () => {
"drp::node ::messageHandler: Stream and data are undefined"
);

const msg = NetworkPb.Message.create({
const msg = Message.create({
sender: node.networkNode.peerId,
type: -1,
data: new Uint8Array(),
Expand Down Expand Up @@ -159,11 +160,11 @@ describe("Handle message correctly", () => {

const vertices = drpObject.vertices;
await signGeneratedVertices(node2, vertices);
const message = NetworkPb.Message.create({
const message = Message.create({
sender: node2.networkNode.peerId,
type: NetworkPb.MessageType.MESSAGE_TYPE_UPDATE,
data: NetworkPb.Update.encode(
NetworkPb.Update.create({
type: MessageType.MESSAGE_TYPE_UPDATE,
data: Update.encode(
Update.create({
objectId: drpObject.id,
vertices: vertices,
})
Expand Down Expand Up @@ -191,11 +192,11 @@ describe("Handle message correctly", () => {
expect(drpObject.vertices.length).toBe(3);
expect(node1DrpObject?.vertices.length).toBe(5);

const message = NetworkPb.Message.create({
const message = Message.create({
sender: node1.networkNode.peerId,
type: NetworkPb.MessageType.MESSAGE_TYPE_SYNC,
data: NetworkPb.Sync.encode(
NetworkPb.Sync.create({
type: MessageType.MESSAGE_TYPE_SYNC,
data: Sync.encode(
Sync.create({
objectId: drpObject.id,
vertexHashes: node1.objectStore.get(drpObject.id)?.vertices.map((vertex) => vertex.hash),
})
Expand All @@ -216,11 +217,11 @@ describe("Handle message correctly", () => {
(node1DrpObject?.drp as SetDRP<number>).add(20);
expect(node1DrpObject?.vertices.length).toBe(7);
await signGeneratedVertices(node1, node1DrpObject?.vertices || []);
const message = NetworkPb.Message.create({
const message = Message.create({
sender: node1.networkNode.peerId,
type: NetworkPb.MessageType.MESSAGE_TYPE_SYNC_ACCEPT,
data: NetworkPb.SyncAccept.encode(
NetworkPb.SyncAccept.create({
type: MessageType.MESSAGE_TYPE_SYNC_ACCEPT,
data: SyncAccept.encode(
SyncAccept.create({
objectId: drpObject.id,
requested: node1DrpObject?.vertices,
requesting: [],
Expand All @@ -243,11 +244,11 @@ describe("Handle message correctly", () => {
signature: node1.credentialStore.signWithBls(vertex.hash),
};
});
const message = NetworkPb.Message.create({
const message = Message.create({
sender: node1.networkNode.peerId,
type: NetworkPb.MessageType.MESSAGE_TYPE_ATTESTATION_UPDATE,
data: NetworkPb.AttestationUpdate.encode(
NetworkPb.AttestationUpdate.create({
type: MessageType.MESSAGE_TYPE_ATTESTATION_UPDATE,
data: AttestationUpdate.encode(
AttestationUpdate.create({
objectId: drpObject.id,
attestations,
})
Expand Down
3 changes: 2 additions & 1 deletion packages/node/tests/node.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import bls from "@chainsafe/bls/herumi";
import { SetDRP } from "@ts-drp/blueprints";
import { ACLGroup, ObjectACL } from "@ts-drp/object";
import { type DRP, DRPObject, DrpType, type Vertex } from "@ts-drp/object";
import { type DRP, DRPObject, DrpType } from "@ts-drp/object";
import { type Vertex } from "@ts-drp/types";
import { beforeAll, beforeEach, describe, expect, test } from "vitest";

import {
Expand Down
4 changes: 3 additions & 1 deletion packages/object/src/acl/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ActionType, type ResolveConflictsType, SemanticsType, type Vertex } from "../index.js";
import { type Vertex } from "@ts-drp/types";

import { ActionType, type ResolveConflictsType, SemanticsType } from "../index.js";
import type { DRPPublicCredential } from "../interface.js";
import type { PeerPermissions } from "./interface.js";
import { type ACL, ACLConflictResolution, ACLGroup } from "./interface.js";
Expand Down
5 changes: 1 addition & 4 deletions packages/object/src/hashgraph/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Vertex_Operation as Operation, Vertex } from "@ts-drp/types";
import { Operation, Vertex } from "@ts-drp/types";

import { log } from "../index.js";
import { BitSet } from "./bitset.js";
Expand All @@ -7,9 +7,6 @@ import { linearizePairSemantics } from "../linearize/pairSemantics.js";
import { computeHash } from "../utils/computeHash.js";
import { ObjectSet } from "../utils/objectSet.js";

// Reexporting the Vertex and Operation types from the protobuf file
export type { Vertex, Operation };

export type Hash = string;

export enum OperationType {
Expand Down
8 changes: 1 addition & 7 deletions packages/object/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { Logger, type LoggerOptions } from "@ts-drp/logger";
import { IMetrics } from "@ts-drp/tracer";
import {
DRPObjectBase,
DRPState,
DRPStateEntry,
type Vertex_Operation as Operation,
type Vertex,
} from "@ts-drp/types";
import { DRPObjectBase, DRPState, DRPStateEntry, Operation, type Vertex } from "@ts-drp/types";
import { cloneDeep } from "es-toolkit";
import { deepEqual } from "fast-equals";
import * as crypto from "node:crypto";
Expand Down
9 changes: 2 additions & 7 deletions packages/object/src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { IMetrics } from "@ts-drp/tracer";
import { ObjectPb } from "@ts-drp/types";
import { type Vertex_Operation as Operation, Vertex } from "@ts-drp/types";
import { Operation, Vertex } from "@ts-drp/types";

import type { ResolveConflictsType, SemanticsType } from "./hashgraph/index.js";
import type { DRPObject } from "./index.js";
Expand All @@ -10,11 +9,7 @@ export enum DrpType {
DRP = "DRP",
}

export type DRPObjectCallback = (
object: DRPObject,
origin: string,
vertices: ObjectPb.Vertex[]
) => void;
export type DRPObjectCallback = (object: DRPObject, origin: string, vertices: Vertex[]) => void;

export interface DRPPublicCredential {
ed25519PublicKey: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/object/src/linearize/multipleSemantics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Vertex_Operation as Operation, Vertex } from "@ts-drp/types";
import { Operation, Vertex } from "@ts-drp/types";

import { ActionType, type Hash, type HashGraph } from "../hashgraph/index.js";
import type { ObjectSet } from "../utils/objectSet.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/object/src/linearize/pairSemantics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Vertex_Operation as Operation } from "@ts-drp/types";
import { Operation } from "@ts-drp/types";

import { ActionType, type Hash, type HashGraph } from "../hashgraph/index.js";
import type { ObjectSet } from "../utils/objectSet.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/object/src/utils/computeHash.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Vertex_Operation as Operation } from "@ts-drp/types";
import type { Operation } from "@ts-drp/types";
import * as crypto from "node:crypto";

import type { Hash } from "../hashgraph/index.js";
Expand Down
3 changes: 2 additions & 1 deletion packages/object/tests/drpobject.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { SetDRP } from "@ts-drp/blueprints/src/index.js";
import { type Vertex } from "@ts-drp/types";
import { beforeEach, describe, expect, it, test, vi } from "vitest";

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

const acl = new ObjectACL({
admins: new Map([
Expand Down
14 changes: 2 additions & 12 deletions packages/object/tests/hashgraph.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import { MapConflictResolution, MapDRP } from "@ts-drp/blueprints/src/Map/index.js";
import { SetDRP } from "@ts-drp/blueprints/src/Set/index.js";
import { Vertex } from "@ts-drp/types";
import { type Vertex, Operation } from "@ts-drp/types";
import { beforeAll, beforeEach, describe, expect, test, vi } from "vitest";

import { ObjectACL } from "../src/acl/index.js";
import { ActionType, SemanticsType } from "../src/hashgraph/index.js";
import {
ACLGroup,
DRP,
DRPObject,
DrpType,
Hash,
HashGraph,
newVertex,
type Operation,
} from "../src/index.js";
import { ACLGroup, DRP, DRPObject, DrpType, Hash, HashGraph, newVertex } from "../src/index.js";
import { ObjectSet } from "../src/utils/objectSet.js";

const acl = new ObjectACL({
Expand Down Expand Up @@ -107,7 +98,6 @@ describe("HashGraph construction tests", () => {
drp1.add(1);
drp2.add(2);
obj2.merge(obj1.hashGraph.getAllVertices());

expect(selfCheckConstraints(obj2.hashGraph)).toBe(true);

const linearOps = obj2.hashGraph.linearizeOperations();
Expand Down
6 changes: 4 additions & 2 deletions packages/object/tests/linearize.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { type Vertex } from "@ts-drp/types";
import { describe, expect, test, vi, beforeEach, afterEach } from "vitest";

import { SemanticsType, ActionType } from "../dist/src/hashgraph/index.js";
import { DrpType, HashGraph, newVertex, type Vertex } from "../src/index.js";
import { ActionType } from "../dist/src/hashgraph/index.js";
import { SemanticsType } from "../dist/src/hashgraph/index.js";
import { DrpType, HashGraph, newVertex } from "../src/index.js";
import { linearizeMultipleSemantics } from "../src/linearize/multipleSemantics.js";
import { linearizePairSemantics } from "../src/linearize/pairSemantics.js";
import { ObjectSet } from "../src/utils/objectSet.js";
Expand Down
Loading
Loading