Skip to content

Commit

Permalink
add timestamp check to the UPDATE handler
Browse files Browse the repository at this point in the history
  • Loading branch information
magnified103 committed Dec 17, 2024
1 parent e254b62 commit 934a11e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
30 changes: 17 additions & 13 deletions packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,11 @@ async function updateHandler(node: DRPNode, data: Uint8Array, sender: string) {
return false;
}

const [merged, _] = object.merge(
updateMessage.vertices.map((v) => {
return {
hash: v.hash,
nodeId: v.nodeId,
operation: {
type: v.operation?.type ?? "",
value: v.operation?.value,
},
dependencies: v.dependencies,
timestamp: v.timestamp,
};
}),
const verifiedVertices = verifyIncomingVertices(
object,
updateMessage.vertices,
);
const [merged, _] = object.merge(verifiedVertices);

if (!merged) {
await node.syncObject(updateMessage.objectId, sender);
Expand Down Expand Up @@ -220,3 +211,16 @@ export function drpObjectChangesHandler(
log.error("::createObject: Invalid origin function");
}
}

export function verifyIncomingVertices(
object: DRPObject,
incomingVertices: ObjectPb.Vertex[],
): Vertex[] {
const currentTimestamp = Date.now();

return incomingVertices.filter(
(vertex) =>
vertex.timestamp <= currentTimestamp &&
currentTimestamp - vertex.timestamp < object.vertexExpirationPeriod,
);
}
3 changes: 3 additions & 0 deletions packages/object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface IDRPObject extends ObjectPb.DRPObjectBase {
// snake_casing to match the JSON config
export interface DRPObjectConfig {
log_config?: LoggerOptions;
vertex_expiration_period?: number;
}

export let log: Logger;
Expand All @@ -58,6 +59,7 @@ export class DRPObject implements IDRPObject {
states: Map<string, DRPState>;
originalDRP: DRP;
subscriptions: DRPObjectCallback[];
vertexExpirationPeriod: number;

constructor(
nodeId: string,
Expand Down Expand Up @@ -92,6 +94,7 @@ export class DRPObject implements IDRPObject {
Object.getOwnPropertyDescriptors(structuredClone(drp)),
);
this.vertices = this.hashGraph.getAllVertices();
this.vertexExpirationPeriod = config?.vertex_expiration_period ?? Number.POSITIVE_INFINITY;
}

// This function is black magic, it allows us to intercept calls to the DRP object
Expand Down

0 comments on commit 934a11e

Please sign in to comment.