Skip to content

Commit

Permalink
chore: remove crdt package; add blueprints package (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-roak authored Sep 18, 2024
1 parent 6b80e2f commit 7d36330
Show file tree
Hide file tree
Showing 47 changed files with 153 additions and 1,587 deletions.
2 changes: 1 addition & 1 deletion examples/canvas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"start": "ts-node ./src/index.ts"
},
"dependencies": {
"@topology-foundation/crdt": "0.1.1",
"@topology-foundation/blueprints": "0.1.1",
"@topology-foundation/network": "0.1.1",
"@topology-foundation/node": "0.1.1",
"@topology-foundation/object": "0.1.1",
Expand Down
2 changes: 1 addition & 1 deletion examples/canvas/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function paint_pixel(pixel: HTMLDivElement) {
random_int(256),
random_int(256),
];
canvasCRO.paint(node.networkNode.peerId, [x, y], painting);
canvasCRO.paint([x, y], painting);
const [r, g, b] = canvasCRO.pixel(x, y).color();
pixel.style.backgroundColor = `rgb(${r}, ${g}, ${b})`;
}
Expand Down
27 changes: 7 additions & 20 deletions examples/canvas/src/objects/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,18 @@ export class Canvas implements CRO {
}

splash(
nodeId: string,
offset: [number, number],
size: [number, number],
rgb: [number, number, number],
): void {
this._splash(nodeId, offset, size, rgb);
this._splash(offset, size, rgb);
}

paint(
nodeId: string,
offset: [number, number],
rgb: [number, number, number],
): void {
this._paint(nodeId, offset, rgb);
paint(offset: [number, number], rgb: [number, number, number]): void {
this._paint(offset, rgb);
}

private _splash(
nodeId: string,
offset: [number, number],
size: [number, number],
rgb: [number, number, number],
Expand All @@ -51,32 +45,25 @@ export class Canvas implements CRO {

for (let x = offset[0]; x < this.width || x < offset[0] + size[0]; x++) {
for (let y = offset[1]; y < this.height || y < offset[1] + size[1]; y++) {
this.canvas[x][y].paint(nodeId, rgb);
this.canvas[x][y].paint(rgb);
}
}
}

private _paint(
nodeId: string,
offset: [number, number],
rgb: [number, number, number],
): void {
if (offset[0] < 0 || this.canvas.length < offset[0]) return;
if (offset[1] < 0 || this.canvas[offset[0]].length < offset[1]) return;

this.canvas[offset[0]][offset[1]].paint(nodeId, rgb);
this.canvas[offset[0]][offset[1]].paint(rgb);
}

pixel(x: number, y: number): Pixel {
return this.canvas[x][y];
}

merge(peerCanvas: Canvas): void {
this.canvas.forEach((row, x) =>
row.forEach((pixel, y) => pixel.merge(peerCanvas.pixel(x, y))),
);
}

resolveConflicts(_): ResolveConflictsType {
return { action: ActionType.Nop };
}
Expand All @@ -90,12 +77,12 @@ export class Canvas implements CRO {
switch (op.type) {
case "splash": {
const [nodeId, offset, size, rgb] = op.value;
this._splash(nodeId, offset, size, rgb);
this._splash(offset, size, rgb);
break;
}
case "paint": {
const [nodeId, offset, rgb] = op.value;
this._paint(nodeId, offset, rgb);
this._paint(offset, rgb);
break;
}
}
Expand Down
39 changes: 13 additions & 26 deletions examples/canvas/src/objects/pixel.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,25 @@
import { GCounter } from "@topology-foundation/crdt";

export class Pixel {
red: GCounter;
green: GCounter;
blue: GCounter;
red: number;
green: number;
blue: number;

constructor() {
this.red = new GCounter({});
this.green = new GCounter({});
this.blue = new GCounter({});
constructor(red?: number, green?: number, blue?: number) {
this.red = red ?? 0;
this.green = green ?? 0;
this.blue = blue ?? 0;
}

color(): [number, number, number] {
return [
this.red.value() % 256,
this.green.value() % 256,
this.blue.value() % 256,
];
}

paint(nodeId: string, rgb: [number, number, number]): void {
this.red.increment(nodeId, rgb[0]);
this.green.increment(nodeId, rgb[1]);
this.blue.increment(nodeId, rgb[2]);
return [this.red % 256, this.green % 256, this.blue % 256];
}

counters(): [GCounter, GCounter, GCounter] {
counters(): [number, number, number] {
return [this.red, this.green, this.blue];
}

merge(peerPixel: Pixel): void {
const peerCounters = peerPixel.counters();
this.red.merge(peerCounters[0]);
this.green.merge(peerCounters[1]);
this.blue.merge(peerCounters[2]);
paint(rgb: [number, number, number]): void {
this.red += rgb[0];
this.green += rgb[1];
this.blue += rgb[2];
}
}
2 changes: 1 addition & 1 deletion examples/chat/asconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"options": {
"lib": {
"@topology-foundation/crdt": [
"../node_modules/@topology-foundation/crdt/src/index.asc.ts"
"./node_modules/@topology-foundation/blueprints/src/index.asc.ts"
]
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/chat/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "topology-example-chat",
"version": "0.1.1",
"description": "Topology Protocol Chat Exmaple",
"description": "Topology Protocol Chat Example",
"main": "src/index.ts",
"repository": "https://github.com/topology-foundation/ts-topology.git",
"license": "MIT",
Expand All @@ -13,7 +13,7 @@
"start": "ts-node ./src/index.ts"
},
"dependencies": {
"@topology-foundation/crdt": "0.1.1",
"@topology-foundation/blueprints": "0.1.1",
"@topology-foundation/network": "0.1.1",
"@topology-foundation/node": "0.1.1",
"@topology-foundation/object": "0.1.1",
Expand Down
4 changes: 2 additions & 2 deletions examples/chat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ const render = () => {
const element_chat = <HTMLDivElement>document.getElementById("chat");
element_chat.innerHTML = "";

if (chat.set.size === 0) {
if (chat.size === 0) {
const div = document.createElement("div");
div.innerHTML = "No messages yet";
div.style.padding = "10px";
element_chat.appendChild(div);
return;
}
for (const message of [...chat.set].sort()) {
for (const message of [...chat].sort()) {
const div = document.createElement("div");
div.innerHTML = message;
div.style.padding = "10px";
Expand Down
41 changes: 3 additions & 38 deletions examples/chat/src/objects/chat.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
// if it can't compile, append src/index.asc to the import path on runtime
import {
type GSet,
gset_add,
gset_create,
gset_merge,
} from "@topology-foundation/crdt";
import {
ActionType,
type CRO,
Expand All @@ -18,9 +11,9 @@ export class Chat implements CRO {
operations: string[] = ["addMessage"];
semanticsType: SemanticsType = SemanticsType.pair;
// store messages as strings in the format (timestamp, message, nodeId)
messages: GSet<string>;
messages: Set<string>;
constructor() {
this.messages = gset_create<string>();
this.messages = new Set<string>();
}

addMessage(timestamp: string, message: string, nodeId: string): void {
Expand All @@ -35,14 +28,10 @@ export class Chat implements CRO {
this.messages.add(`(${timestamp}, ${message}, ${nodeId})`);
}

getMessages(): GSet<string> {
getMessages(): Set<string> {
return this.messages;
}

merge(other: Chat): void {
this.messages.merge(other.messages);
}

resolveConflicts(vertices: Vertex[]): ResolveConflictsType {
return { action: ActionType.Nop };
}
Expand All @@ -54,27 +43,3 @@ export class Chat implements CRO {
}
}
}

export function createChat(): Chat {
return new Chat();
}

// @ts-ignore
export function addMessage(
chat: Chat,
timestamp: string,
message: string,
nodeId: string,
): void {
gset_add(chat.messages, `(${timestamp}, ${message}, ${nodeId})`);
}

// @ts-ignore
export function getMessages(chat: Chat): GSet<string> {
return chat.messages;
}

// @ts-ignore
export function merge(chat: Chat, other: Chat): void {
gset_merge(chat.messages, other.messages);
}
14 changes: 4 additions & 10 deletions packages/crdt/README.md → packages/blueprints/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Conflict-free Replicated Data Types (CRDTs)
# Topology Blueprints

This package contains the CRDT implementations intended to use as builtins for the Topology Protocol.
This package contains the CRO blueprints intended to be used by other CROs.

## Usage

This package is intended to be used as a dependency for the Topology Protocol. However, you can use it as a standalone package. For that, you can install it using:

```bash
# yarn
yarn add @topology-foundation/crdt
yarn add @topology-foundation/blueprints

# npm
npm install @topology-foundation/crdt
npm install @topology-foundation/blueprints
```

### Build
Expand All @@ -29,9 +29,3 @@ To run the tests, you can run:
```bash
yarn test
```

## CRDTs Implementations
- [x] G-Counter
- [x] PN-Counter
- [x] G-Set
- [x] 2P-Set
File renamed without changes.
38 changes: 38 additions & 0 deletions packages/blueprints/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@topology-foundation/blueprints",
"version": "0.1.1",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/topology-foundation/ts-topology.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"
},
"./wasm": {
"types": "./dist/src/index.d.ts",
"import": "./src/index.asc.ts"
}
},
"scripts": {
"asbuild": "yarn asbuild:debug && yarn asbuild:release",
"asbuild:debug": "asc --config asconfig.json --target debug",
"asbuild:release": "asc --config asconfig.json --target release",
"build": "tsc -b",
"clean": "rm -rf dist/ node_modules/",
"prepack": "tsc -b",
"test": "vitest"
},
"devDependencies": {
"@topology-foundation/object": "0.1.1",
"assemblyscript": "^0.27.29"
},
"dependencies": {
"@thi.ng/random": "^4.0.3"
}
}
File renamed without changes.
2 changes: 2 additions & 0 deletions packages/blueprints/src/index.asc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./AddWinsSet/index.js";
export * from "./PseudoRandomWinsSet/index.js";
2 changes: 2 additions & 0 deletions packages/blueprints/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./AddWinsSet/index.js";
export * from "./PseudoRandomWinsSet/index.js";
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, test } from "vitest";
import { AddWinsSet } from "../src/cros/AddWinsSet/index.js";
import { AddWinsSet } from "../src/AddWinsSet/index.js";

describe("HashGraph for AddWinSet tests", () => {
let cro: AddWinsSet<number>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, test } from "vitest";
import { PseudoRandomWinsSet } from "../src/cros/PseudoRandomWinsSet/index.js";
import { PseudoRandomWinsSet } from "../src/PseudoRandomWinsSet/index.js";

describe("HashGraph for PseudoRandomWinsSet tests", () => {
let cro: PseudoRandomWinsSet<number>;
Expand Down
File renamed without changes.
File renamed without changes.
43 changes: 0 additions & 43 deletions packages/crdt/package.json

This file was deleted.

Loading

0 comments on commit 7d36330

Please sign in to comment.