Skip to content

Commit

Permalink
Merge branch 'main' into feat/timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
magnified103 committed Dec 29, 2024
2 parents b6920c1 + 4220e72 commit aa60e99
Show file tree
Hide file tree
Showing 19 changed files with 456 additions and 87 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ jobs:
run: |
pnpm install --no-frozen-lockfile
pnpm ci-test
- shell: bash
run: |
pnpm run coverage
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ docs/
node_modules/

**/tsconfig.tsbuildinfo
coverage/*
.vscode/*
10 changes: 5 additions & 5 deletions examples/chat/src/objects/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ import {
export class Chat implements DRP {
operations: string[] = ["addMessage"];
semanticsType: SemanticsType = SemanticsType.pair;
// store messages as strings in the format (timestamp, message, nodeId)
// store messages as strings in the format (timestamp, message, peerId)
messages: Set<string>;
constructor() {
this.messages = new Set<string>();
}

addMessage(timestamp: string, message: string, nodeId: string): void {
this._addMessage(timestamp, message, nodeId);
addMessage(timestamp: string, message: string, peerId: string): void {
this._addMessage(timestamp, message, peerId);
}

private _addMessage(
timestamp: string,
message: string,
nodeId: string,
peerId: string,
): void {
this.messages.add(`(${timestamp}, ${message}, ${nodeId})`);
this.messages.add(`(${timestamp}, ${message}, ${peerId})`);
}

getMessages(): Set<string> {
Expand Down
14 changes: 7 additions & 7 deletions examples/grid/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let peers: string[] = [];
let discoveryPeers: string[] = [];
let objectPeers: string[] = [];

const formatNodeId = (id: string): string => {
const formatPeerId = (id: string): string => {
return `${id.slice(0, 4)}...${id.slice(-4)}`;
};

Expand All @@ -25,7 +25,7 @@ const hashCode = (str: string): number => {
return hash;
};

const getColorForNodeId = (id: string): string => {
const getColorForPeerId = (id: string): string => {
if (!colorMap.has(id)) {
const hash = hashCode(id);
let r = (hash & 0xff0000) >> 16;
Expand Down Expand Up @@ -64,22 +64,22 @@ const render = () => {
}

const element_peerId = <HTMLDivElement>document.getElementById("peerId");
element_peerId.innerHTML = `<strong style="color: ${getColorForNodeId(node.networkNode.peerId)};">${formatNodeId(node.networkNode.peerId)}</strong>`;
element_peerId.innerHTML = `<strong style="color: ${getColorForPeerId(node.networkNode.peerId)};">${formatPeerId(node.networkNode.peerId)}</strong>`;

const element_peers = <HTMLDivElement>document.getElementById("peers");
element_peers.innerHTML = `[${peers.map((peer) => `<strong style="color: ${getColorForNodeId(peer)};">${formatNodeId(peer)}</strong>`).join(", ")}]`;
element_peers.innerHTML = `[${peers.map((peer) => `<strong style="color: ${getColorForPeerId(peer)};">${formatPeerId(peer)}</strong>`).join(", ")}]`;

const element_discoveryPeers = <HTMLDivElement>(
document.getElementById("discoveryPeers")
);
element_discoveryPeers.innerHTML = `[${discoveryPeers.map((peer) => `<strong style="color: ${getColorForNodeId(peer)};">${formatNodeId(peer)}</strong>`).join(", ")}]`;
element_discoveryPeers.innerHTML = `[${discoveryPeers.map((peer) => `<strong style="color: ${getColorForPeerId(peer)};">${formatPeerId(peer)}</strong>`).join(", ")}]`;

const element_objectPeers = <HTMLDivElement>(
document.getElementById("objectPeers")
);
element_objectPeers.innerHTML = !gridDRP
? ""
: `Your frens in GRID: [${objectPeers.map((peer) => `<strong style="color: ${getColorForNodeId(peer)};">${formatNodeId(peer)}</strong>`).join(", ")}]`;
: `Your frens in GRID: [${objectPeers.map((peer) => `<strong style="color: ${getColorForPeerId(peer)};">${formatPeerId(peer)}</strong>`).join(", ")}]`;

if (!gridDRP) return;
const users = gridDRP.getUsers();
Expand Down Expand Up @@ -179,7 +179,7 @@ async function addUser() {

gridDRP.addUser(
node.networkNode.peerId,
getColorForNodeId(node.networkNode.peerId),
getColorForPeerId(node.networkNode.peerId),
);
render();
}
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"build:packages": "pnpm --filter '@ts-drp/*' build",
"clean": "pnpm --filter '@ts-drp/*' clean && rm -r node_modules/ docs/",
"cli": "tsx ./packages/node/src/run.ts",
"coverage": "pnpm exec ts-node scripts/coverage.ts",
"docs": "typedoc",
"postinstall": "pnpm build:packages",
"proto-gen": "pnpm proto-gen:object && pnpm proto-gen:network",
Expand All @@ -34,8 +35,10 @@
"@biomejs/biome": "^1.8.3",
"@release-it-plugins/workspaces": "^4.2.0",
"@types/node": "^22.5.4",
"@vitest/coverage-v8": "2.1.8",
"assemblyscript": "^0.27.29",
"release-it": "^17.6.0",
"ts-node": "^10.9.2",
"ts-proto": "^2.2.4",
"tsx": "4.19.1",
"typedoc": "^0.26.6",
Expand Down
4 changes: 2 additions & 2 deletions packages/blueprints/tests/AddWinsSetWithACL.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ describe("AccessControl tests with RevokeWins resolution", () => {
const vertices = [
{
hash: "",
nodeId: "peer1",
peerId: "peer1",
operation: { type: "grant", value: "peer3" },
dependencies: [],
signature: "",
},
{
hash: "",
nodeId: "peer2",
peerId: "peer2",
operation: { type: "revoke", value: "peer3" },
dependencies: [],
signature: "",
Expand Down
18 changes: 9 additions & 9 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.

14 changes: 6 additions & 8 deletions packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,11 @@ export function drpObjectChangesHandler(

export async function signGeneratedVertices(node: DRPNode, vertices: Vertex[]) {
const signPromises = vertices.map(async (vertex) => {
if (vertex.nodeId !== node.networkNode.peerId || vertex.signature !== "") {
if (vertex.peerId !== node.networkNode.peerId || vertex.signature !== "") {
return;
}

await node.signVertexOperation(vertex);
await node.signVertex(vertex);
});

await Promise.all(signPromises);
Expand All @@ -232,7 +232,7 @@ export async function verifyIncomingVertices(
const vertices: Vertex[] = incomingVertices.map((vertex) => {
return {
hash: vertex.hash,
nodeId: vertex.nodeId,
peerId: vertex.peerId,
operation: {
type: vertex.operation?.type ?? "",
value: vertex.operation?.value,
Expand All @@ -255,15 +255,13 @@ export async function verifyIncomingVertices(

const signature = uint8ArrayFromString(vertex.signature, "base64");

const publicKey = acl.getPeerKey(vertex.nodeId);
const publicKey = acl.getPeerKey(vertex.peerId);
if (!publicKey) {
return null;
}

const publicKeyBytes = uint8ArrayFromString(publicKey, "base64");
const operationData = uint8ArrayFromString(
JSON.stringify(vertex.operation),
);
const data = uint8ArrayFromString(vertex.hash);

try {
const cryptoKey = await crypto.subtle.importKey(
Expand All @@ -278,7 +276,7 @@ export async function verifyIncomingVertices(
{ name: "Ed25519" },
cryptoKey,
signature,
operationData,
data,
);

return isValid ? vertex : null;
Expand Down
8 changes: 3 additions & 5 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,11 @@ export class DRPNode {
operations.syncObject(this, id, peerId);
}

async signVertexOperation(vertex: Vertex) {
if (vertex.nodeId !== this.networkNode.peerId) {
async signVertex(vertex: Vertex) {
if (vertex.peerId !== this.networkNode.peerId) {
log.error("::signVertexOperation: Invalid peer id");
return "";
}
vertex.signature = await this.networkNode.sign(
JSON.stringify(vertex.operation),
);
vertex.signature = await this.networkNode.sign(vertex.hash);
}
}
10 changes: 5 additions & 5 deletions packages/node/tests/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("DPRNode with verify and sign signature", () => {
const vertices = [
{
hash: "hash",
nodeId: "nodeId",
peerId: "peerId",
operation: {
type: "type",
value: "value",
Expand All @@ -48,7 +48,7 @@ describe("DPRNode with verify and sign signature", () => {
const vertices = [
{
hash: "hash",
nodeId: drpNode.networkNode.peerId,
peerId: drpNode.networkNode.peerId,
operation: {
type: "add",
value: 1,
Expand All @@ -65,7 +65,7 @@ describe("DPRNode with verify and sign signature", () => {
const vertices = [
{
hash: "hash",
nodeId: drpNode.networkNode.peerId,
peerId: drpNode.networkNode.peerId,
operation: {
type: "add",
value: 1,
Expand All @@ -83,7 +83,7 @@ describe("DPRNode with verify and sign signature", () => {
const vertices = [
{
hash: "hash",
nodeId: "peer1",
peerId: "peer1",
operation: {
type: "add",
value: 1,
Expand All @@ -103,7 +103,7 @@ describe("DPRNode with verify and sign signature", () => {
const vertices = [
{
hash: "hash",
nodeId: drpNode.networkNode.peerId,
peerId: drpNode.networkNode.peerId,
operation: {
type: "add",
value: 1,
Expand Down
3 changes: 2 additions & 1 deletion packages/object/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"devDependencies": {
"@bufbuild/protobuf": "^2.0.0",
"benchmark": "^2.1.4",
"tsx": "4.19.1"
"tsx": "4.19.1",
"es-toolkit": "1.30.1"
},
"dependencies": {
"@ts-drp/logger": "^0.4.4"
Expand Down
Loading

0 comments on commit aa60e99

Please sign in to comment.