Skip to content

Commit

Permalink
refactor: change nodeId to peerId (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
trungnotchung authored Dec 29, 2024
1 parent 89454ae commit d6e3dfa
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 72 deletions.
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
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.

6 changes: 3 additions & 3 deletions packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ 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;
}

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 @@ -254,7 +254,7 @@ 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;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class DRPNode {
}

async signVertexOperation(vertex: Vertex) {
if (vertex.nodeId !== this.networkNode.peerId) {
if (vertex.peerId !== this.networkNode.peerId) {
log.error("::signVertexOperation: Invalid peer id");
return "";
}
Expand Down
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
24 changes: 12 additions & 12 deletions packages/object/src/hashgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export type VertexDistance = {
};

export class HashGraph {
nodeId: string;
peerId: string;
resolveConflicts: (vertices: Vertex[]) => ResolveConflictsType;
semanticsType: SemanticsType;

Expand All @@ -64,7 +64,7 @@ export class HashGraph {
)
*/
static readonly rootHash: Hash =
"02465e287e3d086f12c6edd856953ca5ad0f01d6707bf8e410b4a601314c1ca5";
"a65c9cbd875fd3d602adb69a90adb98c4e2c3f26bdf3a2bf597f3548971f2c93";
private arePredecessorsFresh = false;
private reachablePredecessors: Map<Hash, BitSet> = new Map();
private topoSortedIndex: Map<Hash, number> = new Map();
Expand All @@ -73,17 +73,17 @@ export class HashGraph {
private currentBitsetSize = 1;

constructor(
nodeId: string,
peerId: string,
resolveConflicts: (vertices: Vertex[]) => ResolveConflictsType,
semanticsType: SemanticsType,
) {
this.nodeId = nodeId;
this.peerId = peerId;
this.resolveConflicts = resolveConflicts;
this.semanticsType = semanticsType;

const rootVertex: Vertex = {
hash: HashGraph.rootHash,
nodeId: "",
peerId: "",
operation: {
type: OperationType.NOP,
value: null,
Expand All @@ -101,11 +101,11 @@ export class HashGraph {

addToFrontier(operation: Operation): Vertex {
const deps = this.getFrontier();
const hash = computeHash(this.nodeId, operation, deps);
const hash = computeHash(this.peerId, operation, deps);

const vertex: Vertex = {
hash,
nodeId: this.nodeId,
peerId: this.peerId,
operation: operation ?? { type: OperationType.NOP },
dependencies: deps,
signature: "",
Expand Down Expand Up @@ -150,10 +150,10 @@ export class HashGraph {
addVertex(
operation: Operation,
deps: Hash[],
nodeId: string,
peerId: string,
signature: string,
): Hash {
const hash = computeHash(nodeId, operation, deps);
const hash = computeHash(peerId, operation, deps);
if (this.vertices.has(hash)) {
return hash; // Vertex already exists
}
Expand All @@ -166,7 +166,7 @@ export class HashGraph {

const vertex: Vertex = {
hash,
nodeId,
peerId,
operation,
dependencies: deps,
signature,
Expand Down Expand Up @@ -503,11 +503,11 @@ export class HashGraph {
}

function computeHash<T>(
nodeId: string,
peerId: string,
operation: Operation,
deps: Hash[],
): Hash {
const serialized = JSON.stringify({ operation, deps, nodeId });
const serialized = JSON.stringify({ operation, deps, peerId });
const hash = crypto.createHash("sha256").update(serialized).digest("hex");
return hash;
}
14 changes: 7 additions & 7 deletions packages/object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export interface DRPObjectConfig {
export let log: Logger;

export class DRPObject implements IDRPObject {
nodeId: string;
peerId: string;
id: string;
abi: string;
bytecode: Uint8Array;
Expand All @@ -70,28 +70,28 @@ export class DRPObject implements IDRPObject {
subscriptions: DRPObjectCallback[];

constructor(
nodeId: string,
peerId: string,
drp: DRP,
id?: string,
abi?: string,
config?: DRPObjectConfig,
) {
this.nodeId = nodeId;
this.peerId = peerId;
log = new Logger("drp::object", config?.log_config);
this.id =
id ??
crypto
.createHash("sha256")
.update(abi ?? "")
.update(nodeId)
.update(peerId)
.update(Math.floor(Math.random() * Number.MAX_VALUE).toString())
.digest("hex");
this.abi = abi ?? "";
this.bytecode = new Uint8Array();
this.vertices = [];
this.drp = drp ? new Proxy(drp, this.proxyDRPHandler()) : null;
this.hashGraph = new HashGraph(
nodeId,
peerId,
drp?.resolveConflicts?.bind(drp ?? this),
drp?.semanticsType,
);
Expand Down Expand Up @@ -130,7 +130,7 @@ export class DRPObject implements IDRPObject {

const serializedVertex = ObjectPb.Vertex.create({
hash: vertex.hash,
nodeId: vertex.nodeId,
peerId: vertex.peerId,
operation: vertex.operation,
dependencies: vertex.dependencies,
});
Expand All @@ -154,7 +154,7 @@ export class DRPObject implements IDRPObject {
this.hashGraph.addVertex(
vertex.operation,
vertex.dependencies,
vertex.nodeId,
vertex.peerId,
vertex.signature,
);

Expand Down
Loading

0 comments on commit d6e3dfa

Please sign in to comment.