Skip to content

Commit

Permalink
network messages proto; fix node build for proto structure
Browse files Browse the repository at this point in the history
  • Loading branch information
d-roak committed Jul 25, 2024
1 parent 5e70d98 commit 5688d27
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 25 deletions.
5 changes: 2 additions & 3 deletions examples/canvas/src/objects/canvas.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TopologyObject } from "@topology-foundation/object";
import { IPixel, Pixel } from "./pixel";

export interface ICanvas extends TopologyObject {
export interface ICanvas {
width: number;
height: number;
canvas: IPixel[][];
Expand All @@ -20,13 +20,12 @@ export interface ICanvas extends TopologyObject {
merge(peerCanvas: Canvas): void;
}

export class Canvas extends TopologyObject implements ICanvas {
export class Canvas implements TopologyObject, ICanvas {
width: number;
height: number;
canvas: IPixel[][];

constructor(peerId: string, width: number, height: number) {
super(peerId);
this.width = width;
this.height = height;
this.canvas = Array.from(new Array(width), () =>
Expand Down
8 changes: 3 additions & 5 deletions examples/canvas/src/objects/pixel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { GCounter } from "@topology-foundation/crdt";
import { TopologyObject } from "@topology-foundation/object";

export interface IPixel extends TopologyObject {
export interface IPixel {
red: GCounter;
green: GCounter;
blue: GCounter;
Expand All @@ -11,13 +10,12 @@ export interface IPixel extends TopologyObject {
merge(peerPixel: IPixel): void;
}

export class Pixel extends TopologyObject implements IPixel {
export class Pixel implements IPixel {
red: GCounter;
green: GCounter;
blue: GCounter;

constructor(peerId: string) {
super(peerId);
constructor() {
this.red = new GCounter({});
this.green = new GCounter({});
this.blue = new GCounter({});
Expand Down
5 changes: 3 additions & 2 deletions packages/network/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { TopologyNetworkNodeConfig, TopologyNetworkNode } from "./node.js";
export { stringToStream, streamToString } from "./stream.js";
export * from "./node.js";
export * from "./stream.js";
export * from "./proto/messages_pb.js";
9 changes: 5 additions & 4 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ export class TopologyNode {
);
const local = this._objectStore.get(object["id"]);
if (local) {
local.merge(object);
// TODO: merge requires a merge function in wasm
// local.merge(object);
this._objectStore.put(object["id"], local);
}
}
Expand All @@ -99,7 +100,7 @@ export class TopologyNode {
}

createObject(object: TopologyObject) {
const objectId = object.getObjectId();
const objectId = object.id;
this.networkNode.subscribe(objectId);
this._objectStore.put(objectId, object);
}
Expand Down Expand Up @@ -158,14 +159,14 @@ export class TopologyNode {
}

updateObject(object: TopologyObject, update_data: string) {
this._objectStore.put(object.getObjectId(), object);
this._objectStore.put(object.id, object);
// not dialed, emitted through pubsub
const message = `{
"type": "object_update",
"data": [${uint8ArrayFromString(update_data)}]
}`;
this.networkNode.broadcastMessage(
object.getObjectId(),
object.id,
uint8ArrayFromString(message),
);
}
Expand Down
19 changes: 10 additions & 9 deletions packages/object/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import * as crypto from "crypto";
import { TopologyObject } from "./proto/object_pb.js";

export * from "./proto/index.js";
export * from "./proto/object_pb.js";

function newTopologyObject(peerId: string, abi?: string, bytecode?: string): TopologyObject {
const id = crypto
.createHash("sha256")
.update(abi ?? "")
.update(peerId)
.update(Math.floor(Math.random() * Number.MAX_VALUE).toString())
.digest("hex");
/* Creates a new TopologyObject
*/
export function newTopologyObject(peerId: string, id?: string, abi?: string, bytecode?: string): TopologyObject {
return {
id,
id: id ?? crypto
.createHash("sha256")
.update(abi ?? "")
.update(peerId)
.update(Math.floor(Math.random() * Number.MAX_VALUE).toString())
.digest("hex"),
abi: abi ?? "",
bytecode: bytecode ?? ""
};
Expand Down
1 change: 0 additions & 1 deletion packages/object/src/proto/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/object/src/proto/object.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
syntax = "proto3";

package topology.object;

message TopologyObject {
Expand Down

0 comments on commit 5688d27

Please sign in to comment.